Fix defines for headless operation
[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     private IdentityManagerView view;
9     private MoonshotServer ipc_server;
10
11 #if OS_MACOS
12         public OSXApplication osxApp;
13   
14     // the signal handler function.
15     // the current instance of our app class is passed in the 
16     // id_manager_app_instanceparameter 
17         public static bool on_osx_open_files (OSXApplication osx_app_instance, 
18                                         string file_name, 
19                                         IdentityManagerApp id_manager_app_instance ) {
20     int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
21     return true;
22         }
23 #endif
24
25     private const int WINDOW_WIDTH = 400;
26     private const int WINDOW_HEIGHT = 500;
27     public void show() {
28         if (view != null) view.show();    
29     }
30         
31     public IdentityManagerApp (bool headless) {
32         model = new IdentityManagerModel(this);
33         if (!headless)
34             view = new IdentityManagerView(this);
35         LinkedList<IdCard> card_list = model.get_card_list() ;
36         if (card_list.size > 0)
37             this.default_id_card = card_list.first();
38
39         init_ipc_server ();
40
41 #if OS_MACOS
42
43         osxApp = OSXApplication.get_instance();
44         // The 'correct' way of connrcting wont work in Mac OS with Vala 0.12   e.g.    
45         //              osxApp.ns_application_open_file.connect(install_from_file);
46         // so we have to use this old way
47         Signal.connect(osxApp, "NSApplicationOpenFile", (GLib.Callback)(on_osx_open_files), this);
48
49 #endif
50         if (view != null) view.show();
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
170             /* TODO: If candidate list empty return fail */
171             
172             if (confirm && (view != null))
173             {
174                 view.queue_identity_request(request);
175                 return;
176             }
177         }
178         // Send back the identity (we can't directly run the
179         // callback because we may be being called from a 'yield')
180         Idle.add (() => { request.return_identity (identity); return false; });
181         return;
182     }
183
184     private bool match_service_pattern (string service, string pattern)
185     {
186         var pspec = new PatternSpec (pattern);
187         return pspec.match_string (service);
188     }   
189     
190 #if IPC_MSRPC
191     private void init_ipc_server ()
192     {
193         // Errors will currently be sent via g_log - ie. to an
194         // obtrusive message box, on Windows
195         //
196         this.ipc_server = MoonshotServer.get_instance ();
197         MoonshotServer.start (this.view);
198     }
199 #elif IPC_DBUS_GLIB
200     private void init_ipc_server ()
201     {
202  
203         try {
204             var conn = DBus.Bus.get (DBus.BusType.SESSION);
205             dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
206                                                        "/org/freedesktop/DBus",
207                                                        "org.freedesktop.DBus");
208
209             // try to register service in session bus
210             uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
211             assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
212
213             this.ipc_server = new MoonshotServer (this);
214             conn.register_object ("/org/janet/moonshot", ipc_server);
215         }
216         catch (DBus.Error e)
217         {
218             stderr.printf ("%s\n", e.message);
219         }
220     }
221 #else
222     private void bus_acquired_cb (DBusConnection conn)
223     {
224         try {
225             conn.register_object ("/org/janet/moonshot", ipc_server);
226         }
227         catch (Error e)
228         {
229             stderr.printf ("%s\n", e.message);
230         }
231     }
232
233     private void init_ipc_server ()
234     {
235         this.ipc_server = new MoonshotServer (this);
236         GLib.Bus.own_name (GLib.BusType.SESSION,
237                            "org.janet.Moonshot",
238                            GLib.BusNameOwnerFlags.NONE,
239                            bus_acquired_cb,
240                            (conn, name) => {},
241                            (conn, name) => {
242                                error ("Couldn't own name %s on DBus.", name);
243                            });
244     }
245 #endif
246 }
247
248
249 public static int main(string[] args){
250 #if IPC_MSRPC
251         bool headless = false;
252 #else
253         bool headless = GLib.Environment.get_variable("DISPLAY") == null;
254 #endif
255         if (!headless)
256             Gtk.init(ref args);
257
258 #if OS_WIN32
259         // Force specific theme settings on Windows without requiring a gtkrc file
260         Gtk.Settings settings = Gtk.Settings.get_default ();
261         settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
262         settings.set_long_property ("gtk-menu-images", 0, "moonshot");
263 #endif
264
265         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
266         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
267         Intl.textdomain (Config.GETTEXT_PACKAGE);
268        
269            
270         var app = new IdentityManagerApp(headless);
271         
272         app.show();
273
274         if (headless) {
275 #if !IPC_MSRPC
276             MainLoop loop = new MainLoop();
277             loop.run();
278 #endif
279         } else {
280             Gtk.main();
281         }
282
283         return 0;
284     }
285