Do not exit app when responding to identity request, just hide.
[moonshot-ui.git] / src / moonshot-identity-manager-app.vala
1 using Gee;
2 using Gtk;
3
4
5 public class IdentityManagerApp {
6     public IdentityManagerModel model;
7     public IdCard default_id_card;
8     public bool explicitly_launched;
9     private IdentityManagerView view;
10     private MoonshotServer ipc_server;
11
12 #if OS_MACOS
13         public OSXApplication osxApp;
14   
15     // the signal handler function.
16     // the current instance of our app class is passed in the 
17     // id_manager_app_instanceparameter 
18         public static bool on_osx_open_files (OSXApplication osx_app_instance, 
19                                         string file_name, 
20                                         IdentityManagerApp id_manager_app_instance ) {
21     int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
22     return true;
23         }
24 #endif
25
26     private const int WINDOW_WIDTH = 400;
27     private const int WINDOW_HEIGHT = 500;
28     public void show() {
29         if (view != null) view.show();    
30     }
31         
32     public IdentityManagerApp (bool headless) {
33         model = new IdentityManagerModel(this);
34         if (!headless)
35             view = new IdentityManagerView(this);
36         LinkedList<IdCard> card_list = model.get_card_list() ;
37         if (card_list.size > 0)
38             this.default_id_card = card_list.first();
39
40         init_ipc_server ();
41
42 #if OS_MACOS
43
44         osxApp = OSXApplication.get_instance();
45         // The 'correct' way of connrcting wont work in Mac OS with Vala 0.12   e.g.    
46         //              osxApp.ns_application_open_file.connect(install_from_file);
47         // so we have to use this old way
48         Signal.connect(osxApp, "NSApplicationOpenFile", (GLib.Callback)(on_osx_open_files), this);
49
50 #endif
51     }
52
53     public bool add_identity (IdCard id) {
54         /* TODO: add to store here irrespective of view's existence */
55         if (view != null) return view.add_identity(id);
56         return false;
57     }
58
59     public void select_identity (IdentityRequest request) {
60         IdCard identity = null;
61
62         if (request.select_default)
63         {
64             identity = default_id_card;
65         }
66
67         if (identity == null)
68         {
69             bool has_nai = request.nai != null && request.nai != "";
70             bool has_srv = request.service != null && request.service != "";
71             bool confirm = false;
72             IdCard nai_provided = null;
73
74             foreach (IdCard id in model.get_card_list())
75             {
76                 /* If NAI matches we add id card to the candidate list */
77                 if (has_nai && request.nai == id.nai)
78                 {
79                     nai_provided = id;
80                     request.candidates.append (id);
81                     continue;
82                 }
83
84                 /* If any service matches we add id card to the candidate list */
85                 if (has_srv)
86                 {
87                     foreach (string srv in id.services)
88                     {
89                         if (request.service == srv)
90                         {
91                             request.candidates.append (id);
92                             continue;
93                         }
94                     }
95                 }
96             }
97
98             /* If more than one candidate we dissasociate service from all ids */
99             if (has_srv && request.candidates.length() > 1)
100             {
101                 foreach (IdCard id in request.candidates)
102                 {
103                     int i = 0;
104                     SList<string> services_list = null;
105                     bool has_service = false;
106
107                     foreach (string srv in id.services)
108                     {
109                         if (srv == request.service)
110                         {
111                             has_service = true;
112                             continue;
113                         }
114                         services_list.append (srv);
115                     }
116                     
117                     if (!has_service)
118                         continue;
119
120                     if (services_list.length () == 0)
121                     {
122                         id.services = {};
123                         continue;
124                     }
125
126                     string[] services = new string[services_list.length ()];
127                     foreach (string srv in services_list)
128                     {
129                         services[i] = srv;
130                         i++;
131                     }
132
133                     id.services = services;
134                 }
135             }
136
137 //            model.store_id_cards ();
138
139             /* If there are no candidates we use the service matching rules */
140             if (request.candidates.length () == 0)
141             {
142                 foreach (IdCard id in model.get_card_list())
143                 {
144                     foreach (Rule rule in id.rules)
145                     {
146                         if (!match_service_pattern (request.service, rule.pattern))
147                             continue;
148
149                         request.candidates.append (id);
150
151                         if (rule.always_confirm == "true")
152                             confirm = true;
153                     }
154                 }
155             }
156             
157             if (request.candidates.length () > 1)
158             {
159                 if (has_nai && nai_provided != null)
160                 {
161                     identity = nai_provided;
162                     confirm = false;
163                 }
164                 else
165                     confirm = true;
166             }
167             if (identity == null)
168                 identity = request.candidates.nth_data (0);
169             if (identity == null)
170                 confirm = true;
171
172             /* TODO: If candidate list empty return fail */
173             
174             if (confirm && (view != null))
175             {
176                 if (!explicitly_launched)
177                     show();
178                 view.queue_identity_request(request);
179                 return;
180             }
181         }
182         // Send back the identity (we can't directly run the
183         // callback because we may be being called from a 'yield')
184         Idle.add(
185             () => {
186                 request.return_identity (identity);
187 // The following occasionally causes the app to exit without sending the dbus
188 // reply, so for now we just don't exit
189 //                if (!explicitly_launched)
190 //                    Idle.add( () => { Gtk.main_quit(); return false; } );
191                 return false;
192             }
193         );
194         return;
195     }
196
197     private bool match_service_pattern (string service, string pattern)
198     {
199         var pspec = new PatternSpec (pattern);
200         return pspec.match_string (service);
201     }   
202     
203 #if IPC_MSRPC
204     private void init_ipc_server ()
205     {
206         // Errors will currently be sent via g_log - ie. to an
207         // obtrusive message box, on Windows
208         //
209         this.ipc_server = MoonshotServer.get_instance ();
210         MoonshotServer.start (this.view);
211     }
212 #elif IPC_DBUS_GLIB
213     private void init_ipc_server ()
214     {
215  
216         try {
217             var conn = DBus.Bus.get (DBus.BusType.SESSION);
218             dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
219                                                        "/org/freedesktop/DBus",
220                                                        "org.freedesktop.DBus");
221
222             // try to register service in session bus
223             uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
224             assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
225
226             this.ipc_server = new MoonshotServer (this);
227             conn.register_object ("/org/janet/moonshot", ipc_server);
228         }
229         catch (DBus.Error e)
230         {
231             stderr.printf ("%s\n", e.message);
232         }
233     }
234 #else
235     private void bus_acquired_cb (DBusConnection conn)
236     {
237         try {
238             conn.register_object ("/org/janet/moonshot", ipc_server);
239         }
240         catch (Error e)
241         {
242             stderr.printf ("%s\n", e.message);
243         }
244     }
245
246     private void init_ipc_server ()
247     {
248         this.ipc_server = new MoonshotServer (this);
249         GLib.Bus.own_name (GLib.BusType.SESSION,
250                            "org.janet.Moonshot",
251                            GLib.BusNameOwnerFlags.NONE,
252                            bus_acquired_cb,
253                            (conn, name) => {},
254                            (conn, name) => {
255                                error ("Couldn't own name %s on DBus.", name);
256                            });
257     }
258 #endif
259 }
260
261 static bool explicitly_launched = true;
262 const GLib.OptionEntry[] options = {
263     {"DBusLaunch",0,GLib.OptionFlags.REVERSE,GLib.OptionArg.NONE,
264      ref explicitly_launched,"launch for dbus rpc use",null},
265     {null}
266 };
267
268
269 public static int main(string[] args){
270 #if IPC_MSRPC
271         bool headless = false;
272 #else
273         bool headless = GLib.Environment.get_variable("DISPLAY") == null;
274 #endif
275
276         if (headless) {
277             explicitly_launched = false;
278         } else {
279             try {
280                 Gtk.init_with_args(ref args, _(""), options, null);
281             } catch (GLib.Error e) {
282                 stdout.printf(_("error: %s\n"),e.message);
283                 stdout.printf(_("Run '%s --help' to see a full list of available options"), args[0]);
284             }
285         }
286
287 #if OS_WIN32
288         // Force specific theme settings on Windows without requiring a gtkrc file
289         Gtk.Settings settings = Gtk.Settings.get_default ();
290         settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
291         settings.set_long_property ("gtk-menu-images", 0, "moonshot");
292 #endif
293
294         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
295         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
296         Intl.textdomain (Config.GETTEXT_PACKAGE);
297        
298            
299         var app = new IdentityManagerApp(headless);
300         app.explicitly_launched = explicitly_launched;
301         
302         if (app.explicitly_launched) {
303             app.show();
304         }
305
306         if (headless) {
307 #if !IPC_MSRPC
308             MainLoop loop = new MainLoop();
309             loop.run();
310 #endif
311         } else {
312             Gtk.main();
313         }
314
315         return 0;
316     }
317