Track whether launched by user or by dbus (LP: #1166456)
[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                 if (!explicitly_launched)
188                     Idle.add( () => { Gtk.main_quit(); return false; } );
189                 return false;
190             }
191         );
192         return;
193     }
194
195     private bool match_service_pattern (string service, string pattern)
196     {
197         var pspec = new PatternSpec (pattern);
198         return pspec.match_string (service);
199     }   
200     
201 #if IPC_MSRPC
202     private void init_ipc_server ()
203     {
204         // Errors will currently be sent via g_log - ie. to an
205         // obtrusive message box, on Windows
206         //
207         this.ipc_server = MoonshotServer.get_instance ();
208         MoonshotServer.start (this.view);
209     }
210 #elif IPC_DBUS_GLIB
211     private void init_ipc_server ()
212     {
213  
214         try {
215             var conn = DBus.Bus.get (DBus.BusType.SESSION);
216             dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
217                                                        "/org/freedesktop/DBus",
218                                                        "org.freedesktop.DBus");
219
220             // try to register service in session bus
221             uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
222             assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
223
224             this.ipc_server = new MoonshotServer (this);
225             conn.register_object ("/org/janet/moonshot", ipc_server);
226         }
227         catch (DBus.Error e)
228         {
229             stderr.printf ("%s\n", e.message);
230         }
231     }
232 #else
233     private void bus_acquired_cb (DBusConnection conn)
234     {
235         try {
236             conn.register_object ("/org/janet/moonshot", ipc_server);
237         }
238         catch (Error e)
239         {
240             stderr.printf ("%s\n", e.message);
241         }
242     }
243
244     private void init_ipc_server ()
245     {
246         this.ipc_server = new MoonshotServer (this);
247         GLib.Bus.own_name (GLib.BusType.SESSION,
248                            "org.janet.Moonshot",
249                            GLib.BusNameOwnerFlags.NONE,
250                            bus_acquired_cb,
251                            (conn, name) => {},
252                            (conn, name) => {
253                                error ("Couldn't own name %s on DBus.", name);
254                            });
255     }
256 #endif
257 }
258
259 static bool explicitly_launched = true;
260 const GLib.OptionEntry[] options = {
261     {"DBusLaunch",0,GLib.OptionFlags.REVERSE,GLib.OptionArg.NONE,
262      ref explicitly_launched,"launch for dbus rpc use",null},
263     {null}
264 };
265
266
267 public static int main(string[] args){
268 #if IPC_MSRPC
269         bool headless = false;
270 #else
271         bool headless = GLib.Environment.get_variable("DISPLAY") == null;
272 #endif
273
274         if (headless) {
275             explicitly_launched = false;
276         } else {
277             try {
278                 Gtk.init_with_args(ref args, _(""), options, null);
279             } catch (GLib.Error e) {
280                 stdout.printf(_("error: %s\n"),e.message);
281                 stdout.printf(_("Run '%s --help' to see a full list of available options"), args[0]);
282             }
283         }
284
285 #if OS_WIN32
286         // Force specific theme settings on Windows without requiring a gtkrc file
287         Gtk.Settings settings = Gtk.Settings.get_default ();
288         settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
289         settings.set_long_property ("gtk-menu-images", 0, "moonshot");
290 #endif
291
292         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
293         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
294         Intl.textdomain (Config.GETTEXT_PACKAGE);
295        
296            
297         var app = new IdentityManagerApp(headless);
298         app.explicitly_launched = explicitly_launched;
299         
300         if (app.explicitly_launched) {
301             app.show();
302         }
303
304         if (headless) {
305 #if !IPC_MSRPC
306             MainLoop loop = new MainLoop();
307             loop.run();
308 #endif
309         } else {
310             Gtk.main();
311         }
312
313         return 0;
314     }
315