Translatability tweaks
[moonshot-ui.git] / src / moonshot-identity-management-view.vala
index b10ff0c..55932f7 100644 (file)
 */
 using Gee;
 using Gtk;
+using WebProvisioning;
 
 public class IdentityManagerView : Window {
     static MoonshotLogger logger = get_logger("IdentityManagerView");
 
+    bool use_flat_file_store = false;
+
     // The latest year in which Moonshot sources were modified.
     private static int LATEST_EDIT_YEAR = 2016;
 
@@ -67,6 +70,8 @@ public class IdentityManagerView : Window {
 
     private IdCard selected_idcard = null;
 
+    private string import_directory = null;
+
     private enum Columns
     {
         IDCARD_COL,
@@ -84,14 +89,16 @@ public class IdentityManagerView : Window {
     "        </menu>" +
     "</menubar>";
 
-    public IdentityManagerView(IdentityManagerApp app) {
+    public IdentityManagerView(IdentityManagerApp app, bool use_flat_file_store) {
         parent_app = app;
+        this.use_flat_file_store = use_flat_file_store;
+
         #if OS_MACOS
             osxApp = OSXApplication.get_instance();
         #endif
         identities_manager = parent_app.model;
         request_queue = new GLib.Queue<IdentityRequest>();
-        this.title = "Moonshot Identity Selector";
+        this.title = _("Moonshot Identity Selector");
         this.set_position(WindowPosition.CENTER);
         set_default_size(WINDOW_WIDTH, WINDOW_HEIGHT);
         build_ui();
@@ -247,28 +254,6 @@ public class IdentityManagerView : Window {
                        Columns.PASSWORD_COL, id_card.password);
     }
 
-    // private void remove_id_card_data(IdCard id_card)
-    // {
-    //     TreeIter iter;
-    //     string issuer;
-
-    //     if (listmodel->get_iter_first(out iter))
-    //     {
-    //         do
-    //         {
-    //             listmodel->get(iter,
-    //                            Columns.ISSUER_COL, out issuer);
-
-    //             if (id_card.issuer == issuer)
-    //             {
-    //                 listmodel->remove(iter);
-    //                 break;
-    //             }
-    //         }
-    //         while (listmodel->iter_next(ref iter));
-    //     }
-    // }
-
     private IdCardWidget add_id_card_widget(IdCard id_card)
     {
         logger.trace("add_id_card_widget: id_card.nai='%s'; selected nai='%s'"
@@ -314,7 +299,7 @@ public class IdentityManagerView : Window {
         this.send_button.set_sensitive(false);
     }
 
-    public bool add_identity(IdCard id_card, bool force_flat_file_store)
+    public bool add_identity(IdCard id_card, bool force_flat_file_store, out ArrayList<IdCard>? old_duplicates=null)
     {
         #if OS_MACOS
         /* 
@@ -331,6 +316,10 @@ public class IdentityManagerView : Window {
             int flags = prev_id.Compare(id_card);
             logger.trace("add_identity: compare returned " + flags.to_string());
             if (flags == 0) {
+                if (&old_duplicates != null) {
+                    old_duplicates = new ArrayList<IdCard>();
+                }
+
                 return false; // no changes, no need to update
             } else if ((flags & (1 << IdCard.DiffFlags.DISPLAY_NAME)) != 0) {
                 dialog = new Gtk.MessageDialog(this,
@@ -363,10 +352,15 @@ public class IdentityManagerView : Window {
         #endif
 
         if (ret == Gtk.ResponseType.YES) {
-            this.identities_manager.add_card(id_card, force_flat_file_store);
+            this.identities_manager.add_card(id_card, force_flat_file_store, out old_duplicates);
             return true;
         }
-        return false;
+        else {
+            if (&old_duplicates != null) {
+                old_duplicates = new ArrayList<IdCard>();
+            }
+            return false;
+        }
     }
 
     private void add_identity_cb()
@@ -443,9 +437,9 @@ public class IdentityManagerView : Window {
     {
         bool remove = WarningDialog.confirm(this, 
                                             Markup.printf_escaped(
-                                                "<span font-weight='heavy'>You are about to remove the identity '%s'.</span>",
+                                                "<span font-weight='heavy'>" + _("You are about to remove the identity '%s'.") + "</span>",
                                                 id_card.display_name)
-                                            + "\n\nAre you sure you want to do this?",
+                                            + "\n\n" + _("Are you sure you want to do this?"),
                                             "delete_idcard");
         if (remove) 
             remove_identity(id_card);
@@ -754,7 +748,7 @@ SUCH DAMAGE.
         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
         this.search_entry.set_width_chars(24);
 
-        var search_label_markup =_("<small>") + search_tooltip_text + _("</small>");
+        var search_label_markup ="<small>" + search_tooltip_text + "</small>";
         var full_search_label = new Label(null);
         full_search_label.set_markup(search_label_markup);
         full_search_label.set_alignment(1, 0);
@@ -790,7 +784,11 @@ SUCH DAMAGE.
         var add_button = new Button.with_label(_("Add"));
         add_button.clicked.connect((w) => {add_identity_cb();});
         top_table.attach(make_rigid(add_button), num_cols - button_width, num_cols, row, row + 1, fill, fill, 0, 0);
-        logger.trace("build_ui: row spacing for row %d is %u".printf(row, top_table.get_row_spacing(row)));
+        row++;
+
+        var import_button = new Button.with_label(_("Import"));
+        import_button.clicked.connect((w) => {import_identities_cb();});
+        top_table.attach(make_rigid(import_button), num_cols - button_width, num_cols, row, row + 1, fill, fill, 0, 0);
         row++;
 
         this.edit_button = new Button.with_label(_("Edit"));
@@ -870,9 +868,9 @@ SUCH DAMAGE.
         if (selection_in_progress()) {
             var result = WarningDialog.confirm(this,
                                                Markup.printf_escaped(
-                                                   _("<span font-weight='heavy'>Do you wish to use the %s service?</span>"),
+                                                   "<span font-weight='heavy'>" + _("Do you wish to use the %s service?") + "</span>",
                                                    this.request_queue.peek_head().service)
-                                               + _("\n\nSelect Yes to select an ID for this service, or No to cancel"),
+                                               + "\n\n" + _("Select Yes to select an ID for this service, or No to cancel"),
                                                "close_moonshot_window");
             if (result) {
                 // Prevent other handlers from handling this event; this keeps the window open.
@@ -893,4 +891,66 @@ SUCH DAMAGE.
         return fixed_height;
     }
 
+    private void import_identities_cb() {
+        var dialog = new FileChooserDialog(_("Import File"),
+                                           this,
+                                           FileChooserAction.OPEN,
+                                           _("Cancel"),ResponseType.CANCEL,
+                                           _("Open"), ResponseType.ACCEPT,
+                                           null);
+
+        if (import_directory != null) {
+            dialog.set_current_folder(import_directory);
+        }
+
+        if (dialog.run() == ResponseType.ACCEPT)
+        {
+            // Save the parent directory to use as default for next save
+            string filename = dialog.get_filename();
+            var file  = File.new_for_path(filename);
+            import_directory = file.get_parent().get_path();
+
+            int import_count = 0;
+
+            var webp = new Parser(filename);
+            dialog.destroy();
+            webp.parse();
+            logger.trace(@"import_identities_cb: Have $(webp.cards.length) IdCards");
+            foreach (IdCard card in webp.cards)
+            {
+
+                if (card == null) {
+                    logger.trace(@"import_identities_cb: Skipping null IdCard");
+                    continue;
+                }
+
+                if (!card.trust_anchor.is_empty()) {
+                    string ta_datetime_added = TrustAnchor.format_datetime_now();
+                    card.trust_anchor.set_datetime_added(ta_datetime_added);
+                    logger.trace("import_identities_cb : Set ta_datetime_added for '%s' to '%s'; ca_cert='%s'; server_cert='%s'"
+                                 .printf(card.display_name, ta_datetime_added, card.trust_anchor.ca_cert, card.trust_anchor.server_cert));
+                }
+
+
+                bool result = add_identity(card, use_flat_file_store);
+                if (result) {
+                    logger.trace(@"import_identities_cb: Added or updated '$(card.display_name)'");
+                    import_count++;
+                }
+                else {
+                    logger.trace(@"import_identities_cb: Did not add or update '$(card.display_name)'");
+                }
+            }
+            var msg_dialog = new Gtk.MessageDialog(this,
+                                               Gtk.DialogFlags.DESTROY_WITH_PARENT,
+                                               Gtk.MessageType.INFO,
+                                               Gtk.ButtonsType.OK,
+                                               _("Import completed. %d Identities were added or updated."),
+                                               import_count);
+            msg_dialog.run();
+            msg_dialog.destroy();
+        }
+        dialog.destroy();
+    }
+
 }