Add flatstore-users
[moonshot-ui.git] / src / moonshot-identity-manager-app.vala
index cff2824..03088eb 100644 (file)
@@ -1,12 +1,22 @@
 using Gee;
 using Gtk;
 
+#if IPC_DBUS
+[DBus (name = "org.janet.Moonshot")]
+interface IIdentityManager : GLib.Object {
+#if IPC_DBUS_GLIB
+    public abstract bool show_ui() throws DBus.Error;
+#else
+    public abstract bool show_ui() throws IOError;
+#endif
+}
+#endif
 
 public class IdentityManagerApp {
     public IdentityManagerModel model;
     public IdCard default_id_card;
     public bool explicitly_launched;
-    private IdentityManagerView view;
+    public IdentityManagerView view;
     private MoonshotServer ipc_server;
 
 #if OS_MACOS
@@ -29,8 +39,25 @@ public class IdentityManagerApp {
         if (view != null) view.show();    
     }
        
-    public IdentityManagerApp (bool headless) {
-        model = new IdentityManagerModel(this);
+    public IdentityManagerApp (bool headless, bool use_flat_file_store) {
+        use_flat_file_store |= UserForcesFlatFileStore();
+#if GNOME_KEYRING
+        bool keyring_available = (!use_flat_file_store) && GnomeKeyring.is_available();
+#else
+        bool keyring_available = false;
+#endif
+        IIdentityCardStore.StoreType store_type;
+        if (headless || use_flat_file_store || !keyring_available)
+            store_type = IIdentityCardStore.StoreType.FLAT_FILE;
+        else
+            store_type = IIdentityCardStore.StoreType.KEYRING;
+
+        model = new IdentityManagerModel(this, store_type);
+        /* if headless, but we have nothing in the flat file store
+         * and keyring is available, switch to keyring */
+        if (headless && keyring_available && !use_flat_file_store && !model.HasNonTrivialIdentities())
+            model.set_store_type(IIdentityCardStore.StoreType.KEYRING);
+
         if (!headless)
             view = new IdentityManagerView(this);
         LinkedList<IdCard> card_list = model.get_card_list() ;
@@ -50,10 +77,10 @@ public class IdentityManagerApp {
 #endif
     }
 
-    public bool add_identity (IdCard id) {
-        /* TODO: add to store here irrespective of view's existence */
-        if (view != null) return view.add_identity(id);
-        return false;
+    public bool add_identity (IdCard id, bool force_flat_file_store) {
+        if (view != null) return view.add_identity(id, force_flat_file_store);
+        model.add_card(id, force_flat_file_store);
+        return true;
     }
 
     public void select_identity (IdentityRequest request) {
@@ -166,8 +193,23 @@ public class IdentityManagerApp {
             }
             if (identity == null)
                 identity = request.candidates.nth_data (0);
-            if (identity == null)
-                confirm = true;
+            if ((identity != null) && 
+                ((identity.password == null) || (identity.password == "")))
+                identity.password = request.password;
+            if (identity == null) {
+                if (has_nai) {
+                    // create a temp identity
+                    string[] components = request.nai.split("@", 2);
+                    identity = new IdCard();
+                    identity.display_name = request.nai;
+                    identity.username = components[0];
+                    if (components.length > 1)
+                        identity.issuer = components[1];
+                    identity.password = request.password;
+                } else {
+                    confirm = true;
+                }
+            }
 
             /* TODO: If candidate list empty return fail */
             
@@ -183,9 +225,14 @@ public class IdentityManagerApp {
         // callback because we may be being called from a 'yield')
         Idle.add(
             () => {
-                request.return_identity (identity); 
-                if (!explicitly_launched)
-                    Idle.add( () => { Gtk.main_quit(); return false; } );
+                if (view != null) {
+                    identity = view.check_add_password(identity, request, model);
+                }
+                request.return_identity (identity);
+// The following occasionally causes the app to exit without sending the dbus
+// reply, so for now we just don't exit
+//                if (!explicitly_launched)
+//                    Idle.add( () => { Gtk.main_quit(); return false; } );
                 return false;
             }
         );
@@ -205,12 +252,11 @@ public class IdentityManagerApp {
         // obtrusive message box, on Windows
         //
         this.ipc_server = MoonshotServer.get_instance ();
-        MoonshotServer.start (this.view);
+        MoonshotServer.start (this);
     }
 #elif IPC_DBUS_GLIB
     private void init_ipc_server ()
     {
         try {
             var conn = DBus.Bus.get (DBus.BusType.SESSION);
             dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
@@ -219,10 +265,26 @@ public class IdentityManagerApp {
 
             // try to register service in session bus
             uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
-            assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
-
-            this.ipc_server = new MoonshotServer (this);
-            conn.register_object ("/org/janet/moonshot", ipc_server);
+            if (reply == DBus.RequestNameReply.PRIMARY_OWNER)
+            {
+                this.ipc_server = new MoonshotServer (this);
+                conn.register_object ("/org/janet/moonshot", ipc_server);
+            } else {
+                bool shown=false;
+                GLib.Error e;
+                DBus.Object manager_proxy = conn.get_object ("org.janet.Moonshot",
+                                                             "/org/janet/moonshot",
+                                                             "org.janet.Moonshot");
+                if (manager_proxy != null)
+                    manager_proxy.call("ShowUi", out e, GLib.Type.INVALID, typeof(bool), out shown, GLib.Type.INVALID);
+
+                if (!shown) {
+                    GLib.error ("Couldn't own name org.janet.Moonshot on dbus or show previously launched identity manager.");
+                } else {
+                    stdout.printf("Showed previously launched identity manager.\n");
+                    GLib.Process.exit(0);
+                }
+            }
         }
         catch (DBus.Error e)
         {
@@ -250,16 +312,30 @@ public class IdentityManagerApp {
                            bus_acquired_cb,
                            (conn, name) => {},
                            (conn, name) => {
-                               error ("Couldn't own name %s on DBus.", name);
+                               bool shown=false;
+                               try {
+                                   IIdentityManager manager = Bus.get_proxy_sync (BusType.SESSION, name, "/org/janet/moonshot");
+                                   shown = manager.show_ui();
+                               } catch (IOError e) {
+                               }
+                               if (!shown) {
+                                   GLib.error ("Couldn't own name %s on dbus or show previously launched identity manager.", name);
+                               } else {
+                                   stdout.printf("Showed previously launched identity manager.\n");
+                                   GLib.Process.exit(0);
+                               }
                            });
     }
 #endif
 }
 
 static bool explicitly_launched = true;
+static bool use_flat_file_store = false;
 const GLib.OptionEntry[] options = {
-    {"DBusLaunch",0,GLib.OptionFlags.REVERSE,GLib.OptionArg.NONE,
+    {"dbus-launched",0,GLib.OptionFlags.REVERSE,GLib.OptionArg.NONE,
      ref explicitly_launched,"launch for dbus rpc use",null},
+    {"flat-file-store",0,0,GLib.OptionArg.NONE,
+     ref use_flat_file_store,"force use of flat file identity store (used by default only for headless operation)",null},
     {null}
 };
 
@@ -272,14 +348,29 @@ public static int main(string[] args){
 #endif
 
         if (headless) {
+            try {
+                var opt_context = new OptionContext(null);
+                opt_context.set_help_enabled (true);
+                opt_context.add_main_entries (options, null);
+                opt_context.parse(ref args);
+            } catch (OptionError e) {
+                stdout.printf(_("error: %s\n"),e.message);
+                stdout.printf(_("Run '%s --help' to see a full list of available options\n"), args[0]);
+                return -1;
+            }
             explicitly_launched = false;
         } else {
             try {
-                Gtk.init_with_args(ref args, _(""), options, null);
+                if (!Gtk.init_with_args(ref args, _(""), options, null)) {
+                    stdout.printf(_("unable to initialize window\n"));
+                    return -1;
+                }
             } catch (GLib.Error e) {
                 stdout.printf(_("error: %s\n"),e.message);
-                stdout.printf(_("Run '%s --help' to see a full list of available options"), args[0]);
+                stdout.printf(_("Run '%s --help' to see a full list of available options\n"), args[0]);
+                return -1;
             }
+            gtk_available = true;
         }
 
 #if OS_WIN32
@@ -294,7 +385,7 @@ public static int main(string[] args){
         Intl.textdomain (Config.GETTEXT_PACKAGE);
        
           
-        var app = new IdentityManagerApp(headless);
+        var app = new IdentityManagerApp(headless, use_flat_file_store);
         app.explicitly_launched = explicitly_launched;
         
        if (app.explicitly_launched) {