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) &&
196 ((identity.password == null) || (identity.password == "")))
197 identity.password = request.password;
198 if (identity == null) {
200 // create a temp identity
201 string[] components = request.nai.split("@", 2);
202 identity = new IdCard();
203 identity.display_name = request.nai;
204 identity.username = components[0];
205 if (components.length > 1)
206 identity.issuer = components[1];
207 identity.password = request.password;
213 /* TODO: If candidate list empty return fail */
215 if (confirm && (view != null))
217 if (!explicitly_launched)
219 view.queue_identity_request(request);
223 // Send back the identity (we can't directly run the
224 // callback because we may be being called from a 'yield')
228 identity = view.check_add_password(identity, request, model);
230 request.return_identity (identity);
231 // The following occasionally causes the app to exit without sending the dbus
232 // reply, so for now we just don't exit
233 // if (!explicitly_launched)
234 // Idle.add( () => { Gtk.main_quit(); return false; } );
241 private bool match_service_pattern (string service, string pattern)
243 var pspec = new PatternSpec (pattern);
244 return pspec.match_string (service);
248 private void init_ipc_server ()
250 // Errors will currently be sent via g_log - ie. to an
251 // obtrusive message box, on Windows
253 this.ipc_server = MoonshotServer.get_instance ();
254 MoonshotServer.start (this);
257 private void init_ipc_server ()
260 var conn = DBus.Bus.get (DBus.BusType.SESSION);
261 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
262 "/org/freedesktop/DBus",
263 "org.freedesktop.DBus");
265 // try to register service in session bus
266 uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
267 if (reply == DBus.RequestNameReply.PRIMARY_OWNER)
269 this.ipc_server = new MoonshotServer (this);
270 conn.register_object ("/org/janet/moonshot", ipc_server);
274 DBus.Object manager_proxy = conn.get_object ("org.janet.Moonshot",
275 "/org/janet/moonshot",
276 "org.janet.Moonshot");
277 if (manager_proxy != null)
278 manager_proxy.call("ShowUi", out e, GLib.Type.INVALID, typeof(bool), out shown, GLib.Type.INVALID);
281 GLib.error ("Couldn't own name org.janet.Moonshot on dbus or show previously launched identity manager.");
283 stdout.printf("Showed previously launched identity manager.\n");
284 GLib.Process.exit(0);
290 stderr.printf ("%s\n", e.message);
294 private void bus_acquired_cb (DBusConnection conn)
297 conn.register_object ("/org/janet/moonshot", ipc_server);
301 stderr.printf ("%s\n", e.message);
305 private void init_ipc_server ()
307 this.ipc_server = new MoonshotServer (this);
308 GLib.Bus.own_name (GLib.BusType.SESSION,
309 "org.janet.Moonshot",
310 GLib.BusNameOwnerFlags.NONE,
316 IIdentityManager manager = Bus.get_proxy_sync (BusType.SESSION, name, "/org/janet/moonshot");
317 shown = manager.show_ui();
318 } catch (IOError e) {
321 GLib.error ("Couldn't own name %s on dbus or show previously launched identity manager.", name);
323 stdout.printf("Showed previously launched identity manager.\n");
324 GLib.Process.exit(0);
331 static bool explicitly_launched = true;
332 static bool use_flat_file_store = false;
333 const GLib.OptionEntry[] options = {
334 {"dbus-launched",0,GLib.OptionFlags.REVERSE,GLib.OptionArg.NONE,
335 ref explicitly_launched,"launch for dbus rpc use",null},
336 {"flat-file-store",0,0,GLib.OptionArg.NONE,
337 ref use_flat_file_store,"force use of flat file identity store (used by default only for headless operation)",null},
342 public static int main(string[] args){
344 bool headless = false;
346 bool headless = GLib.Environment.get_variable("DISPLAY") == null;
351 var opt_context = new OptionContext(null);
352 opt_context.set_help_enabled (true);
353 opt_context.add_main_entries (options, null);
354 opt_context.parse(ref args);
355 } catch (OptionError e) {
356 stdout.printf(_("error: %s\n"),e.message);
357 stdout.printf(_("Run '%s --help' to see a full list of available options\n"), args[0]);
360 explicitly_launched = false;
363 if (!Gtk.init_with_args(ref args, _(""), options, null)) {
364 stdout.printf(_("unable to initialize window\n"));
367 } catch (GLib.Error e) {
368 stdout.printf(_("error: %s\n"),e.message);
369 stdout.printf(_("Run '%s --help' to see a full list of available options\n"), args[0]);
372 gtk_available = true;
376 // Force specific theme settings on Windows without requiring a gtkrc file
377 Gtk.Settings settings = Gtk.Settings.get_default ();
378 settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
379 settings.set_long_property ("gtk-menu-images", 0, "moonshot");
382 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
383 Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
384 Intl.textdomain (Config.GETTEXT_PACKAGE);
387 var app = new IdentityManagerApp(headless, use_flat_file_store);
388 app.explicitly_launched = explicitly_launched;
390 if (app.explicitly_launched) {
396 MainLoop loop = new MainLoop();