Fix S928 [bug] 924920 Mac port - provisioning crashes on exit
[moonshot-ui.git] / src / moonshot-identity-manager-app.vala
1 using Gtk;
2
3
4 class IdentityManagerApp {
5     public IdentityManagerModel model;
6     private IdentityManagerView view;
7     private MoonshotServer ipc_server;
8
9 #if OS_MACOS
10         public OSXApplication osxApp;
11   
12     // the signal handler function.
13     // the current instance of our app class is passed in the 
14     // id_manager_app_instanceparameter 
15         public static bool on_osx_open_files (OSXApplication osx_app_instance, 
16                                         string file_name, 
17                                         IdentityManagerApp id_manager_app_instance ) {
18     int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
19     return true;
20         }
21 #endif
22
23     private const int WINDOW_WIDTH = 400;
24     private const int WINDOW_HEIGHT = 500;
25     public void show() {
26         view.show();    
27     }
28         
29     public IdentityManagerApp () {
30         model = new IdentityManagerModel(this);
31         view = new IdentityManagerView(this);
32         init_ipc_server ();
33
34 #if OS_MACOS
35
36         osxApp = OSXApplication.get_instance();
37         // The 'correct' way of connrcting wont work in Mac OS with Vala 0.12   e.g.    
38         //              osxApp.ns_application_open_file.connect(install_from_file);
39         // so we have to use this old way
40         Signal.connect(osxApp, "NSApplicationOpenFile", (GLib.Callback)(on_osx_open_files), this);
41
42 #endif
43         view.show();
44     }   
45     
46 #if IPC_MSRPC
47     private void init_ipc_server ()
48     {
49         // Errors will currently be sent via g_log - ie. to an
50         // obtrusive message box, on Windows
51         //
52         this.ipc_server = MoonshotServer.get_instance ();
53         MoonshotServer.start (this.view);
54     }
55 #elif IPC_DBUS_GLIB
56     private void init_ipc_server ()
57     {
58  
59         try {
60             var conn = DBus.Bus.get (DBus.BusType.SESSION);
61             dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
62                                                        "/org/freedesktop/DBus",
63                                                        "org.freedesktop.DBus");
64
65             // try to register service in session bus
66             uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
67             assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
68
69             this.ipc_server = new MoonshotServer (this.view);
70             conn.register_object ("/org/janet/moonshot", ipc_server);
71         }
72         catch (DBus.Error e)
73         {
74             stderr.printf ("%s\n", e.message);
75         }
76     }
77 #else
78     private void bus_acquired_cb (DBusConnection conn)
79     {
80         try {
81             conn.register_object ("/org/janet/moonshot", ipc_server);
82         }
83         catch (Error e)
84         {
85             stderr.printf ("%s\n", e.message);
86         }
87     }
88
89     private void init_ipc_server ()
90     {
91         this.ipc_server = new MoonshotServer (this.view);
92         GLib.Bus.own_name (GLib.BusType.SESSION,
93                            "org.janet.Moonshot",
94                            GLib.BusNameOwnerFlags.NONE,
95                            bus_acquired_cb,
96                            (conn, name) => {},
97                            (conn, name) => {
98                                error ("Couldn't own name %s on DBus.", name);
99                            });
100     }
101 #endif
102 }
103
104
105 public static int main(string[] args){
106         Gtk.init(ref args);
107
108 #if OS_WIN32
109         // Force specific theme settings on Windows without requiring a gtkrc file
110         Gtk.Settings settings = Gtk.Settings.get_default ();
111         settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
112         settings.set_long_property ("gtk-menu-images", 0, "moonshot");
113 #endif
114
115         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
116         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
117         Intl.textdomain (Config.GETTEXT_PACKAGE);
118        
119            
120         var app = new IdentityManagerApp();
121         
122         app.show();
123
124         Gtk.main();
125
126         return 0;
127     }
128