5 [DBus (name = "org.janet.Moonshot")]
6 interface IIdentityManager : GLib.Object {
8 public abstract bool show_ui() throws DBus.Error;
10 public abstract bool show_ui() throws IOError;
15 public class IdentityManagerApp {
16 public IdentityManagerModel model;
17 public IdCard default_id_card;
18 public bool explicitly_launched;
19 public IdentityManagerView view;
20 private MoonshotServer ipc_server;
23 public OSXApplication osxApp;
25 // the signal handler function.
26 // the current instance of our app class is passed in the
27 // id_manager_app_instanceparameter
28 public static bool on_osx_open_files (OSXApplication osx_app_instance,
30 IdentityManagerApp id_manager_app_instance ) {
31 int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
36 private const int WINDOW_WIDTH = 400;
37 private const int WINDOW_HEIGHT = 500;
39 if (view != null) view.show();
42 public IdentityManagerApp (bool headless, bool use_flat_file_store) {
44 bool keyring_available = GnomeKeyring.is_available();
46 bool keyring_available = false;
48 IIdentityCardStore.StoreType store_type;
49 if (headless || use_flat_file_store || !keyring_available)
50 store_type = IIdentityCardStore.StoreType.FLAT_FILE;
52 store_type = IIdentityCardStore.StoreType.KEYRING;
54 model = new IdentityManagerModel(this, store_type);
55 /* if headless, but we have nothing in the flat file store
56 * and keyring is available, switch to keyring */
57 if (headless && keyring_available && !use_flat_file_store && !model.HasNonTrivialIdentities())
58 model.set_store_type(IIdentityCardStore.StoreType.KEYRING);
61 view = new IdentityManagerView(this);
62 LinkedList<IdCard> card_list = model.get_card_list() ;
63 if (card_list.size > 0)
64 this.default_id_card = card_list.first();
70 osxApp = OSXApplication.get_instance();
71 // The 'correct' way of connrcting wont work in Mac OS with Vala 0.12 e.g.
72 // osxApp.ns_application_open_file.connect(install_from_file);
73 // so we have to use this old way
74 Signal.connect(osxApp, "NSApplicationOpenFile", (GLib.Callback)(on_osx_open_files), this);
79 public bool add_identity (IdCard id, bool force_flat_file_store) {
80 if (view != null) return view.add_identity(id, force_flat_file_store);
81 model.add_card(id, force_flat_file_store);
85 public void select_identity (IdentityRequest request) {
86 IdCard identity = null;
88 if (request.select_default)
90 identity = default_id_card;
95 bool has_nai = request.nai != null && request.nai != "";
96 bool has_srv = request.service != null && request.service != "";
98 IdCard nai_provided = null;
100 foreach (IdCard id in model.get_card_list())
102 /* If NAI matches we add id card to the candidate list */
103 if (has_nai && request.nai == id.nai)
106 request.candidates.append (id);
110 /* If any service matches we add id card to the candidate list */
113 foreach (string srv in id.services)
115 if (request.service == srv)
117 request.candidates.append (id);
124 /* If more than one candidate we dissasociate service from all ids */
125 if (has_srv && request.candidates.length() > 1)
127 foreach (IdCard id in request.candidates)
130 SList<string> services_list = null;
131 bool has_service = false;
133 foreach (string srv in id.services)
135 if (srv == request.service)
140 services_list.append (srv);
146 if (services_list.length () == 0)
152 string[] services = new string[services_list.length ()];
153 foreach (string srv in services_list)
159 id.services = services;
163 // model.store_id_cards ();
165 /* If there are no candidates we use the service matching rules */
166 if (request.candidates.length () == 0)
168 foreach (IdCard id in model.get_card_list())
170 foreach (Rule rule in id.rules)
172 if (!match_service_pattern (request.service, rule.pattern))
175 request.candidates.append (id);
177 if (rule.always_confirm == "true")
183 if (request.candidates.length () > 1)
185 if (has_nai && nai_provided != null)
187 identity = nai_provided;
193 if (identity == null)
194 identity = request.candidates.nth_data (0);
195 if (identity == null)
198 /* TODO: If candidate list empty return fail */
200 if (confirm && (view != null))
202 if (!explicitly_launched)
204 view.queue_identity_request(request);
208 // Send back the identity (we can't directly run the
209 // callback because we may be being called from a 'yield')
213 view.check_add_password(identity, request, model);
215 request.return_identity (identity);
216 // The following occasionally causes the app to exit without sending the dbus
217 // reply, so for now we just don't exit
218 // if (!explicitly_launched)
219 // Idle.add( () => { Gtk.main_quit(); return false; } );
226 private bool match_service_pattern (string service, string pattern)
228 var pspec = new PatternSpec (pattern);
229 return pspec.match_string (service);
233 private void init_ipc_server ()
235 // Errors will currently be sent via g_log - ie. to an
236 // obtrusive message box, on Windows
238 this.ipc_server = MoonshotServer.get_instance ();
239 MoonshotServer.start (this);
242 private void init_ipc_server ()
245 var conn = DBus.Bus.get (DBus.BusType.SESSION);
246 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
247 "/org/freedesktop/DBus",
248 "org.freedesktop.DBus");
250 // try to register service in session bus
251 uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
252 if (reply == DBus.RequestNameReply.PRIMARY_OWNER)
254 this.ipc_server = new MoonshotServer (this);
255 conn.register_object ("/org/janet/moonshot", ipc_server);
259 DBus.Object manager_proxy = conn.get_object ("org.janet.Moonshot",
260 "/org/janet/moonshot",
261 "org.janet.Moonshot");
262 if (manager_proxy != null)
263 manager_proxy.call("ShowUi", out e, GLib.Type.INVALID, typeof(bool), out shown, GLib.Type.INVALID);
266 GLib.error ("Couldn't own name org.janet.Moonshot on dbus or show previously launched identity manager.");
268 stdout.printf("Showed previously launched identity manager.\n");
269 GLib.Process.exit(0);
275 stderr.printf ("%s\n", e.message);
279 private void bus_acquired_cb (DBusConnection conn)
282 conn.register_object ("/org/janet/moonshot", ipc_server);
286 stderr.printf ("%s\n", e.message);
290 private void init_ipc_server ()
292 this.ipc_server = new MoonshotServer (this);
293 GLib.Bus.own_name (GLib.BusType.SESSION,
294 "org.janet.Moonshot",
295 GLib.BusNameOwnerFlags.NONE,
301 IIdentityManager manager = Bus.get_proxy_sync (BusType.SESSION, name, "/org/janet/moonshot");
302 shown = manager.show_ui();
303 } catch (IOError e) {
306 GLib.error ("Couldn't own name %s on dbus or show previously launched identity manager.", name);
308 stdout.printf("Showed previously launched identity manager.\n");
309 GLib.Process.exit(0);
316 static bool explicitly_launched = true;
317 static bool use_flat_file_store = false;
318 const GLib.OptionEntry[] options = {
319 {"dbus-launched",0,GLib.OptionFlags.REVERSE,GLib.OptionArg.NONE,
320 ref explicitly_launched,"launch for dbus rpc use",null},
321 {"flat-file-store",0,0,GLib.OptionArg.NONE,
322 ref use_flat_file_store,"force use of flat file identity store (used by default only for headless operation)",null},
327 public static int main(string[] args){
329 bool headless = false;
331 bool headless = GLib.Environment.get_variable("DISPLAY") == null;
335 explicitly_launched = false;
338 if (!Gtk.init_with_args(ref args, _(""), options, null)) {
339 stdout.printf(_("unable to initialize window\n"));
342 } catch (GLib.Error e) {
343 stdout.printf(_("error: %s\n"),e.message);
344 stdout.printf(_("Run '%s --help' to see a full list of available options\n"), args[0]);
350 // Force specific theme settings on Windows without requiring a gtkrc file
351 Gtk.Settings settings = Gtk.Settings.get_default ();
352 settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
353 settings.set_long_property ("gtk-menu-images", 0, "moonshot");
356 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
357 Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
358 Intl.textdomain (Config.GETTEXT_PACKAGE);
361 var app = new IdentityManagerApp(headless, use_flat_file_store);
362 app.explicitly_launched = explicitly_launched;
364 if (app.explicitly_launched) {
370 MainLoop loop = new MainLoop();