identity manager: support add identities in headless mode
[moonshot-ui.git] / src / moonshot-server.vala
index 681d0dd..2bff71d 100644 (file)
@@ -3,11 +3,21 @@
 [DBus (name = "org.janet.Moonshot")]
 public class MoonshotServer : Object {
 
-    private IdentityManagerView main_window;
+    private IdentityManagerApp parent_app;
 
-    public MoonshotServer (Gtk.Window window)
+    public MoonshotServer (IdentityManagerApp app)
     {
-        this.main_window = (IdentityManagerView) window;
+        this.parent_app = app;
+    }
+
+    public bool show_ui()
+    {
+        if (parent_app.view == null) {
+            return false;
+        }
+        parent_app.show();
+        parent_app.explicitly_launched = true;
+        return true;
     }
 
     public async bool get_identity (string nai,
@@ -20,7 +30,7 @@ public class MoonshotServer : Object {
                                     out string subject_name_constraint,
                                     out string subject_alt_name_constraint)
     {
-        var request = new IdentityRequest (main_window,
+        var request = new IdentityRequest (parent_app,
                                            nai,
                                            password,
                                            service);
@@ -37,7 +47,7 @@ public class MoonshotServer : Object {
 
         var id_card = request.id_card;
 
-        if (id_card != null) {
+        if ((id_card != null) && (id_card.display_name != IdCard.NO_IDENTITY)) {
             nai_out = id_card.nai;
             password_out = id_card.password;
 
@@ -72,7 +82,7 @@ public class MoonshotServer : Object {
                                             out string subject_name_constraint,
                                             out string subject_alt_name_constraint)
     {
-        var request = new IdentityRequest.default (main_window);
+        var request = new IdentityRequest.default (parent_app);
         request.set_callback ((IdentityRequest) => get_default_identity.callback());
         request.execute ();
         yield;
@@ -115,15 +125,15 @@ public class MoonshotServer : Object {
 
     public bool install_id_card (string   display_name,
                                  string   user_name,
-                                 string   password,
-                                 string   realm,
-                                 string[] rules_patterns,
-                                 string[] rules_always_confirm,
-                                 string[] services,
-                                 string   ca_cert,
-                                 string   subject,
-                                 string   subject_alt,
-                                 string   server_cert)
+                                 string   ?password,
+                                 string   ?realm,
+                                 string[] ?rules_patterns,
+                                 string[] ?rules_always_confirm,
+                                 string[] ?services,
+                                 string   ?ca_cert,
+                                 string   ?subject,
+                                 string   ?subject_alt,
+                                 string   ?server_cert)
     {
       IdCard idcard = new IdCard ();
 
@@ -148,16 +158,17 @@ public class MoonshotServer : Object {
         }
       }
 
-      return this.main_window.add_identity (idcard);
+      return parent_app.add_identity (idcard);
     }
 
 
-    public bool install_from_file (string file_name)
+    public int install_from_file (string file_name)
     {
     var webp = new WebProvisioning.Parser (file_name);
+
     webp.parse();
     bool result = false;
-    
+    int installed_cards = 0;
     foreach (IdCard card in WebProvisioning.cards)
     {
       string[] rules_patterns = {};
@@ -175,6 +186,7 @@ public class MoonshotServer : Object {
           i++;
         }
       } 
+
       result = install_id_card (card.display_name,
                                 card.username,
                                 card.password,
@@ -186,11 +198,12 @@ public class MoonshotServer : Object {
                                 card.trust_anchor.subject,
                                 card.trust_anchor.subject_alt,
                                 card.trust_anchor.server_cert);
-       }
-
-    return true;
+      if (result) {
+        installed_cards++;
+      }
     }
-
+    return installed_cards;
+  }
 }
 
 
@@ -264,7 +277,7 @@ public class MoonshotServer : Object {
 
         var id_card = request.id_card;
 
-        if (id_card == null) {
+        if (id_card != null) {
             // The strings are freed by the RPC runtime
             nai_out = id_card.nai;
             password_out = id_card.password;