update_card() returns modified card
[moonshot-ui.git] / src / moonshot-identity-request.vala
index 9538db5..cc436ac 100644 (file)
@@ -1,61 +1,91 @@
-delegate void ReturnIdentityCallback (IdentityRequest request);
+public delegate void ReturnIdentityCallback (IdentityRequest request);
 
-class IdentityRequest : Object {
+public class IdentityRequest : Object {
     public IdCard? id_card = null;
     public bool complete = false;
+    public bool select_default = false;
 
-    private MainWindow main_window;
-    private string nai;
-    private string password;
-    private string certificate;
+    private IdentityManagerApp parent_app;
+    public string nai;
+    public string password;
+    public string service;
+    public SList<IdCard> candidates;
 
-    // Only one of these is used, we must support two types for
-    // the DBus and MSRPC servers.
-    ReturnIdentityCallback return_identity_cb = null;
-    SourceFunc source_func_cb = null;
+    ReturnIdentityCallback callback = null;
 
-    public IdentityRequest (MainWindow                   main_window,
+    public IdentityRequest (IdentityManagerApp           app,
                             string                       nai,
                             string                       password,
-                            string                       certificate)
+                            string                       service)
     {
-        this.main_window = main_window;
+        this.parent_app = app;
         this.nai = nai;
         this.password = password;
-        this.certificate = certificate;
+        this.service = service;
     }
 
-    public void set_return_identity_callback (owned ReturnIdentityCallback cb)
+    public IdentityRequest.default (IdentityManagerApp app)
     {
-#if VALA_0_12
-        this.return_identity_cb = ((owned) cb);
-#else
-        this.return_identity_cb = ((IdCard) => cb (IdCard));
-#endif
+        this.parent_app = app;
+        this.select_default = true;
     }
 
-    public void set_source_func_callback (owned SourceFunc cb)
+    public void set_callback (owned ReturnIdentityCallback cb)
     {
 #if VALA_0_12
-        this.source_func_cb = ((owned) cb);
+        this.callback = ((owned) cb);
 #else
-        this.source_func_cb = (() => cb ());
+        this.callback = ((IdCard) => cb (IdCard));
 #endif
     }
 
-    public void execute () {
-        main_window.select_identity (this);
+    public bool execute () {
+        parent_app.select_identity (this);
+
+        /* This function works as a GSourceFunc, so it can be passed to
+         * the main loop from other threads
+         */
+        return false;
     }
 
     public void return_identity (IdCard? id_card) {
         this.id_card = id_card;
         this.complete = true;
 
-        if (return_identity_cb != null)
-            return_identity_cb (this);
-        else if (source_func_cb != null)
-            source_func_cb ();
-        else
-            warn_if_reached ();
+        /* update id_card service list */
+        if (id_card != null && this.service != null && this.service != "")
+        {
+            bool duplicate_service = false;
+
+            foreach (string service in id_card.services)
+            {
+                if (service == this.service)
+                    duplicate_service = true;
+            }
+            if (duplicate_service == false)
+            {
+                string[] services = new string[id_card.services.length + 1];
+
+                for (int i = 0; i < id_card.services.length; i++)
+                    services[i] = id_card.services[i];
+
+                services[id_card.services.length] = this.service;
+                id_card.services = services;
+
+                this.id_card = this.parent_app.model.update_card (id_card);
+            }
+        }
+
+        return_if_fail (callback != null);
+        callback (this);
     }
+
+#if OS_WIN32
+    /* For synchronisation between RPC thread and main loop. Because
+     * these objects are not refcounted, it's best to tie them to the
+     * lifecycle of the IdentityRequest object.
+     */
+    public Mutex mutex;
+    public Cond cond;
+#endif
 }