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