4 [DBus (name = "org.janet.Moonshot")]
5 interface IIdentityManager : GLib.Object {
6 public abstract bool show_ui() throws IOError;
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;
17 public OSXApplication osxApp;
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,
24 IdentityManagerApp id_manager_app_instance ) {
25 int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
30 private const int WINDOW_WIDTH = 400;
31 private const int WINDOW_HEIGHT = 500;
33 if (view != null) view.show();
36 public IdentityManagerApp (bool headless) {
37 model = new IdentityManagerModel(this);
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();
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);
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);
63 public void select_identity (IdentityRequest request) {
64 IdCard identity = null;
66 if (request.select_default)
68 identity = default_id_card;
73 bool has_nai = request.nai != null && request.nai != "";
74 bool has_srv = request.service != null && request.service != "";
76 IdCard nai_provided = null;
78 foreach (IdCard id in model.get_card_list())
80 /* If NAI matches we add id card to the candidate list */
81 if (has_nai && request.nai == id.nai)
84 request.candidates.append (id);
88 /* If any service matches we add id card to the candidate list */
91 foreach (string srv in id.services)
93 if (request.service == srv)
95 request.candidates.append (id);
102 /* If more than one candidate we dissasociate service from all ids */
103 if (has_srv && request.candidates.length() > 1)
105 foreach (IdCard id in request.candidates)
108 SList<string> services_list = null;
109 bool has_service = false;
111 foreach (string srv in id.services)
113 if (srv == request.service)
118 services_list.append (srv);
124 if (services_list.length () == 0)
130 string[] services = new string[services_list.length ()];
131 foreach (string srv in services_list)
137 id.services = services;
141 // model.store_id_cards ();
143 /* If there are no candidates we use the service matching rules */
144 if (request.candidates.length () == 0)
146 foreach (IdCard id in model.get_card_list())
148 foreach (Rule rule in id.rules)
150 if (!match_service_pattern (request.service, rule.pattern))
153 request.candidates.append (id);
155 if (rule.always_confirm == "true")
161 if (request.candidates.length () > 1)
163 if (has_nai && nai_provided != null)
165 identity = nai_provided;
171 if (identity == null)
172 identity = request.candidates.nth_data (0);
173 if (identity == null)
176 /* TODO: If candidate list empty return fail */
178 if (confirm && (view != null))
180 if (!explicitly_launched)
182 view.queue_identity_request(request);
186 // Send back the identity (we can't directly run the
187 // callback because we may be being called from a 'yield')
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; } );
201 private bool match_service_pattern (string service, string pattern)
203 var pspec = new PatternSpec (pattern);
204 return pspec.match_string (service);
208 private void init_ipc_server ()
210 // Errors will currently be sent via g_log - ie. to an
211 // obtrusive message box, on Windows
213 this.ipc_server = MoonshotServer.get_instance ();
214 MoonshotServer.start (this.view);
217 private void init_ipc_server ()
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");
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);
230 this.ipc_server = new MoonshotServer (this);
231 conn.register_object ("/org/janet/moonshot", ipc_server);
235 stderr.printf ("%s\n", e.message);
239 private void bus_acquired_cb (DBusConnection conn)
242 conn.register_object ("/org/janet/moonshot", ipc_server);
246 stderr.printf ("%s\n", e.message);
250 private void init_ipc_server ()
252 this.ipc_server = new MoonshotServer (this);
253 GLib.Bus.own_name (GLib.BusType.SESSION,
254 "org.janet.Moonshot",
255 GLib.BusNameOwnerFlags.NONE,
261 IIdentityManager manager = Bus.get_proxy_sync (BusType.SESSION, name, "/org/janet/moonshot");
262 shown = manager.show_ui();
263 } catch (IOError e) {
266 GLib.error ("Couldn't own name %s on dbus or show previously launched identity manager.", name);
268 stdout.printf("Showed previously launched identity manager.\n");
269 GLib.Process.exit(0);
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},
284 public static int main(string[] args){
286 bool headless = false;
288 bool headless = GLib.Environment.get_variable("DISPLAY") == null;
292 explicitly_launched = false;
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]);
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");
309 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
310 Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
311 Intl.textdomain (Config.GETTEXT_PACKAGE);
314 var app = new IdentityManagerApp(headless);
315 app.explicitly_launched = explicitly_launched;
317 if (app.explicitly_launched) {
323 MainLoop loop = new MainLoop();