Checkpointing non-working services edit code before I refactor it
authorDan Breslau <dbreslau@painless-security.com>
Wed, 27 Jul 2016 00:26:18 +0000 (20:26 -0400)
committerDan Breslau <dbreslau@painless-security.com>
Wed, 27 Jul 2016 00:26:18 +0000 (20:26 -0400)
src/moonshot-identity-dialog.vala
src/moonshot-identity-management-view.vala

index dacf4f9..9734c82 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2014, JANET(UK)
+ * Copyright (c) 2016, JANET(UK)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -40,12 +40,16 @@ static const string CANCEL = STOCK_CANCEL;
 #endif
 
 
-class AddIdentityDialog : Dialog
+class IdentityDialog : Dialog
 {
+    private static MoonshotLogger logger = get_logger("IdentityDialog");
+
     static const string displayname_labeltext = _("Display Name");
     static const string issuer_labeltext = _("Issuer");
     static const string username_labeltext = _("Username");
     static const string password_labeltext = _("Password");
+
+    private IdentityManagerView parent;
     private Entry displayname_entry;
     private Label displayname_label;
     private Entry issuer_entry;
@@ -78,30 +82,51 @@ class AddIdentityDialog : Dialog
         get { return remember_checkbutton.active; }
     }
 
-    public AddIdentityDialog()
+    public IdentityDialog(IdentityManagerView parent)
+    {
+        this.with_idcard(null, _("Add ID Card"), parent);
+    }
+
+    public IdentityDialog.with_idcard(IdCard? a_card, string title, IdentityManagerView parent)
     {
-        this.set_title(_("Add ID Card"));
+        bool is_new_card = false;
+        if (a_card == null)
+        {
+            is_new_card = true;
+        }
+
+        IdCard card = a_card ?? new IdCard();
+        this.set_title(title);
         this.set_modal(true);
+        this.set_transient_for(parent);
+        this.parent = parent;
 
-        this.add_buttons(_("Add ID Card"), ResponseType.OK,
-                         CANCEL, ResponseType.CANCEL);
+        this.add_buttons(_("OK"), ResponseType.OK, CANCEL, ResponseType.CANCEL);
         var content_area = this.get_content_area();
         ((Box) content_area).set_spacing(12);
         
         displayname_label = new Label(@"$displayname_labeltext:");
         displayname_label.set_alignment(1,(float) 0.5);
         displayname_entry = new Entry();
+        displayname_entry.set_text(card.display_name);
+
         issuer_label = new Label(@"$issuer_labeltext:");
         issuer_label.set_alignment(1,(float) 0.5);
         this.issuer_entry = new Entry();
+        this.issuer_entry.set_text(card.issuer);
+
         username_label = new Label(@"$username_labeltext:");
         username_label.set_alignment(1,(float) 0.5);
         this.username_entry = new Entry();
+        this.username_entry.set_text(card.username);
+
         password_label = new Label(@"$password_labeltext:");
         password_label.set_alignment(1,(float) 0.5);
         this.password_entry = new Entry();
         password_entry.set_invisible_char('*');
         password_entry.set_visibility(false);
+        password_entry.set_text(card.password);
+
         this.remember_checkbutton = new CheckButton.with_label(_("Remember password"));
         this.message_label = new Label("");
         message_label.set_visible(false);
@@ -131,6 +156,26 @@ class AddIdentityDialog : Dialog
         vbox.set_border_width(6);
         vbox.pack_start(table, false, false, 0);
 
+
+        var services_vbox_title = new Label(_("Services:"));
+        label_make_bold(services_vbox_title);
+        services_vbox_title.set_alignment(0, (float) 0.5);
+        
+        var services_internal_vbox = new VBox(true, 6);
+
+        var services_vbox_alignment = new Alignment(0, 0, 0, 1);
+        services_vbox_alignment.set_padding(6, 6, 6, 6);
+        services_vbox_alignment.add(services_internal_vbox);
+        var services_vscroll = new ScrolledWindow(null, null);
+        services_vscroll.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
+        services_vscroll.set_shadow_type(ShadowType.IN);
+        services_vscroll.add_with_viewport(services_vbox_alignment);
+
+        var services_vbox = new VBox(false, 6);
+        services_vbox.pack_start(services_vbox_title, false, false, 0);
+        services_vbox.pack_start(services_vscroll, true, true, 0);
+
+
         ((Container) content_area).add(vbox);
 
         this.set_border_width(6);
@@ -202,4 +247,90 @@ class AddIdentityDialog : Dialog
 
         atk_widget.add_relationship(relationship, atk_target_widget);
     }
+
+    private static void label_make_bold(Label label)
+    {
+        var font_desc = new Pango.FontDescription();
+
+        font_desc.set_weight(Pango.Weight.BOLD);
+
+        /* This will only affect the weight of the font. The rest is
+         * from the current state of the widget, which comes from the
+         * theme or user prefs, since the font desc only has the
+         * weight flag turned on.
+         */
+        label.modify_font(font_desc);
+    }
+
+    private void fill_services_vbox(IdCard id_card, VBox services_internal_vbox)
+    {
+        logger.trace("fill_services_vbox");
+
+        // var children = services_internal_vbox.get_children();
+        // foreach (var widget in children) {
+        //     services_internal_vbox.remove(widget);
+        // }
+
+        int i = 0;
+        var n_rows = id_card.services.length;
+
+        var services_table = new Table(n_rows, 2, false);
+        services_table.set_col_spacings(10);
+        services_table.set_row_spacings(10);
+        services_internal_vbox.pack_start(services_table, true, false, 0);
+        
+        foreach (string service in id_card.services)
+        {
+            var label = new Label(service);
+            label.set_alignment(0, (float) 0.5);
+#if VALA_0_12
+            var remove_button = new Button.from_stock(Stock.REMOVE);
+#else
+            var remove_button = new Button.from_stock(STOCK_REMOVE);
+#endif
+
+            remove_button.clicked.connect((remove_button) =>
+                {
+                    var dialog = new Gtk.MessageDialog(this,
+                                                       Gtk.DialogFlags.DESTROY_WITH_PARENT,
+                                                       Gtk.MessageType.QUESTION,
+                                                       Gtk.ButtonsType.YES_NO,
+                                                       _("Are you sure you want to stop '%s' ID Card from being used with %s?"),
+                                                       id_card.display_name,
+                                                       service);
+                    var ret = dialog.run();
+                    dialog.hide();
+              
+                    if (ret == Gtk.ResponseType.YES)
+                    {
+                        if (id_card != null) {
+                            SList<string> services = new SList<string>();
+                
+                            foreach (string srv in id_card.services)
+                            {
+                                //if (srv == candidate) //!!TODO: If srv is the service to be removed
+                                //    continue;
+                                services.append(srv);
+                            }
+                
+                            id_card.services = new string[services.length()];
+                            for (int j = 0; j < id_card.services.length; j++)
+                            {
+                                id_card.services[j] = services.nth_data(j);
+                            }
+                
+                            parent.identities_manager.update_card(id_card);
+                        }
+                    }
+              
+                });
+            services_table.attach_defaults(label, 0, 1, i, i+1);
+            services_table.attach_defaults(remove_button, 1, 2, i, i+1);
+            i++;
+        }
+
+        // services_vbox.show_all();
+    }
+
+
 }
index 80480a5..369f455 100644 (file)
@@ -56,11 +56,13 @@ public class IdentityManagerView : Window {
     private Label no_identity_title;
     // private CheckButton remember_checkbutton;
     // private Button update_password_button;
+    private Button edit_button;
+    private Button remove_button;
 
     private Gtk.ListStore* listmodel;
     private TreeModelFilter filter;
 
-    public IdentityManagerModel identities_manager;
+    internal IdentityManagerModel identities_manager;
     private unowned SList<IdCard>    candidates;
 
     public GLib.Queue<IdentityRequest> request_queue;
@@ -305,10 +307,8 @@ public class IdentityManagerView : Window {
     //     show_details(id_card_widget.id_card);
     // }
 
-    private IdCard get_id_card_data(AddIdentityDialog dialog)
+    private IdCard update_id_card_data(IdentityDialog dialog, IdCard id_card)
     {
-        var id_card = new IdCard();
-
         id_card.display_name = dialog.display_name;
         id_card.issuer = dialog.issuer;
         id_card.username = dialog.username;
@@ -362,11 +362,18 @@ public class IdentityManagerView : Window {
 //        id_card_widget.details_id.connect(details_identity_cb);
 //        id_card_widget.remove_id.connect(remove_identity_cb);
 //        id_card_widget.send_id.connect((w) => send_identity_cb(w.id_card));
-        id_card_widget.expanded.connect(this.custom_vbox.receive_expanded_event);
+        id_card_widget.expanded.connect(this.widget_selected_cb);
         // id_card_widget.expanded.connect((w) => fill_details(w.id_card));
         return id_card_widget;
     }
 
+    private void widget_selected_cb(IdCardWidget id_card_widget)
+    {
+        remove_button.set_sensitive(true);
+        this.edit_button.set_sensitive(true);
+        this.custom_vbox.receive_expanded_event(id_card_widget);
+    }
+
     public bool add_identity(IdCard id_card, bool force_flat_file_store)
     {
         #if OS_MACOS
@@ -421,14 +428,14 @@ public class IdentityManagerView : Window {
 
     private void add_identity_cb()
     {
-        var dialog = new AddIdentityDialog();
+        var dialog = new IdentityDialog(this);
         int result = ResponseType.CANCEL;
         while (!dialog.complete)
             result = dialog.run();
 
         switch (result) {
         case ResponseType.OK:
-            this.identities_manager.add_card(get_id_card_data(dialog), false);
+            this.identities_manager.add_card(update_id_card_data(dialog, new IdCard()), false);
             break;
         default:
             break;
@@ -436,9 +443,21 @@ public class IdentityManagerView : Window {
         dialog.destroy();
     }
 
-    private void edit_identity_cb()
+    private void edit_identity_cb(IdCard card)
     {
-        //!!TODO
+        var dialog = new IdentityDialog.with_idcard(card, _("Edit Identity"), this);
+        int result = ResponseType.CANCEL;
+        while (!dialog.complete)
+            result = dialog.run();
+
+        switch (result) {
+        case ResponseType.OK:
+            this.identities_manager.update_card(update_id_card_data(dialog, card));
+            break;
+        default:
+            break;
+        }
+        dialog.destroy();
     }
 
     private void remove_identity(IdCardWidget id_card_widget)
@@ -447,6 +466,10 @@ public class IdentityManagerView : Window {
         this.custom_vbox.remove_id_card_widget(id_card_widget);
 
         this.identities_manager.remove_card(id_card);
+
+        // Nothing is selected, so disable edit and remove buttons
+        this.edit_button.set_sensitive(false);
+        this.remove_button.set_sensitive(false);
     }
 
     private void redraw_id_card_widgets()
@@ -589,19 +612,19 @@ public class IdentityManagerView : Window {
         request.return_identity(identity);
     }
 
-    private void label_make_bold(Label label)
-    {
-        var font_desc = new Pango.FontDescription();
+    // private void label_make_bold(Label label)
+    // {
+    //     var font_desc = new Pango.FontDescription();
 
-        font_desc.set_weight(Pango.Weight.BOLD);
+    //     font_desc.set_weight(Pango.Weight.BOLD);
 
-        /* This will only affect the weight of the font, the rest is
-         * from the current state of the widget, which comes from the
-         * theme or user prefs, since the font desc only has the
-         * weight flag turned on.
-         */
-        label.modify_font(font_desc);
-    }
+    //     /* This will only affect the weight of the font, the rest is
+    //      * from the current state of the widget, which comes from the
+    //      * theme or user prefs, since the font desc only has the
+    //      * weight flag turned on.
+    //      */
+    //     label.modify_font(font_desc);
+    // }
 
 //     private void fill_services_vbox(IdCard id_card)
 //     {
@@ -859,9 +882,9 @@ SUCH DAMAGE.
         no_identity_title.set_line_wrap(true);
         no_identity_title.show();
 
-        var login_vbox_title = new Label(_("Login: "));
-        label_make_bold(login_vbox_title);
-        login_vbox_title.set_alignment(0, (float) 0.5);
+        // var login_vbox_title = new Label(_("Login: "));
+        // label_make_bold(login_vbox_title);
+        // login_vbox_title.set_alignment(0, (float) 0.5);
         // var issuer_label = new Label(_("Issuer:"));
         // issuer_label.set_alignment(1, (float) 0.5);
         // this.issuer_entry = new Entry();
@@ -930,15 +953,17 @@ SUCH DAMAGE.
         var add_button = new Button.with_label(_("Add"));
         add_button.clicked.connect((w) => {add_identity_cb();});
 
-        var edit_button = new Button.with_label(_("Edit"));
-        edit_button.clicked.connect((w) => {edit_identity_cb();});
+        this.edit_button = new Button.with_label(_("Edit"));
+        edit_button.clicked.connect((w) => {edit_identity_cb(custom_vbox.current_idcard.id_card);});
+        edit_button.set_sensitive(false);
 
-        var remove_button = new Button.with_label(_("Remove"));
+        this.remove_button = new Button.with_label(_("Remove"));
         remove_button.clicked.connect((w) => {remove_identity_cb(custom_vbox.current_idcard);});
+        remove_button.set_sensitive(false);
 
         var send_button = new Button.with_label(_("Send"));
         send_button.clicked.connect((w) => {send_identity_cb(custom_vbox.current_idcard.id_card);});
-
+        send_button.set_visible(false);
 
         var empty_box = new VBox(false, 0);
         empty_box.set_size_request(0, 0);