[seahorse/wip/nielsdg/place-category: 1/2] sidebar: Order by Passwords, Keys and Certificates



commit 630e34cb3536598d9dad5566c575f0bddd06c381
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Fri Jan 31 09:18:17 2020 +0100

    sidebar: Order by Passwords, Keys and Certificates
    
    We add a notion of a "category" to the `SeahorsePlace` object. That way,
    we can have a cleaner design in the sidebar, where we can group GPG and
    SSH keys together in a more general "Keys" aggregation.

 common/place.vala             | 26 +++++++++++++++
 gkr/gkr-keyring.vala          |  4 +++
 pgp/seahorse-gpgme-keyring.c  | 30 ++++++++++++-----
 pgp/seahorse-server-source.c  | 34 ++++++++++++-------
 pgp/seahorse-unknown-source.c | 66 ++++++++++++++++++++++---------------
 pkcs11/pkcs11-token.vala      |  4 +++
 src/sidebar.vala              | 76 ++++++++-----------------------------------
 ssh/source.vala               |  4 +++
 8 files changed, 135 insertions(+), 109 deletions(-)
---
diff --git a/common/place.vala b/common/place.vala
index c1fd343b..3beee304 100644
--- a/common/place.vala
+++ b/common/place.vala
@@ -23,10 +23,36 @@
  * An example of this is a keyring.
  */
 public interface Seahorse.Place : Gcr.Collection {
+
+    /**
+     * We generally divide a SeahorsePlace in some high level categories.
+     *
+     * These are then used to group places in the sidebar.
+     */
+    public enum Category {
+        PASSWORDS,
+        KEYS,
+        CERTIFICATES;
+
+        public unowned string to_string() {
+            switch (this) {
+                case Category.PASSWORDS:
+                    return _("Passwords");
+                case Category.KEYS:
+                    return _("Keys");
+                case Category.CERTIFICATES:
+                    return _("Certificates");
+            }
+
+            return_val_if_reached(null);
+        }
+    }
+
     public abstract string label { owned get; set; }
     public abstract string description { owned get; }
     public abstract string uri { owned get; }
     public abstract Icon icon { owned get; }
+    public abstract Category category { owned get; }
 
     /**
      * In some cases, we do not want to show the Place in the sidebar
diff --git a/gkr/gkr-keyring.vala b/gkr/gkr-keyring.vala
index ed233748..a7c8113b 100644
--- a/gkr/gkr-keyring.vala
+++ b/gkr/gkr-keyring.vala
@@ -47,6 +47,10 @@ public class Keyring : Secret.Collection, Gcr.Collection, Place, Deletable, Lock
                owned get { return new GLib.ThemedIcon("folder"); }
        }
 
+    public Place.Category category {
+        get { return Place.Category.PASSWORDS; }
+    }
+
     private GLib.ActionGroup? _actions = null;
     public GLib.ActionGroup? actions {
         owned get { return this._actions; }
diff --git a/pgp/seahorse-gpgme-keyring.c b/pgp/seahorse-gpgme-keyring.c
index 26bf0b23..19a834a6 100644
--- a/pgp/seahorse-gpgme-keyring.c
+++ b/pgp/seahorse-gpgme-keyring.c
@@ -47,11 +47,12 @@
 #include <locale.h>
 
 enum {
-       PROP_0,
-       PROP_LABEL,
-       PROP_DESCRIPTION,
-       PROP_ICON,
-       PROP_URI,
+    PROP_0,
+    PROP_LABEL,
+    PROP_DESCRIPTION,
+    PROP_ICON,
+    PROP_CATEGORY,
+    PROP_URI,
     PROP_ACTIONS,
     PROP_ACTION_PREFIX,
     PROP_MENU_MODEL,
@@ -777,6 +778,12 @@ seahorse_gpgme_keyring_get_icon (SeahorsePlace *place)
        return g_themed_icon_new (GCR_ICON_GNUPG);
 }
 
+static SeahorsePlaceCategory
+seahorse_gpgme_keyring_get_category (SeahorsePlace *place)
+{
+    return SEAHORSE_PLACE_CATEGORY_KEYS;
+}
+
 static GActionGroup *
 seahorse_gpgme_keyring_get_actions (SeahorsePlace *place)
 {
@@ -825,9 +832,12 @@ seahorse_gpgme_keyring_get_property (GObject *obj,
        case PROP_DESCRIPTION:
                g_value_take_string (value, seahorse_gpgme_keyring_get_description (place));
                break;
-       case PROP_ICON:
-               g_value_take_object (value, seahorse_gpgme_keyring_get_icon (place));
-               break;
+    case PROP_ICON:
+        g_value_take_object (value, seahorse_gpgme_keyring_get_icon (place));
+        break;
+    case PROP_CATEGORY:
+        g_value_set_enum (value, seahorse_gpgme_keyring_get_category (place));
+        break;
        case PROP_URI:
                g_value_take_string (value, seahorse_gpgme_keyring_get_uri (place));
                break;
@@ -922,6 +932,7 @@ seahorse_gpgme_keyring_class_init (SeahorseGpgmeKeyringClass *klass)
        g_object_class_override_property (gobject_class, PROP_DESCRIPTION, "description");
        g_object_class_override_property (gobject_class, PROP_URI, "uri");
        g_object_class_override_property (gobject_class, PROP_ICON, "icon");
+    g_object_class_override_property (gobject_class, PROP_CATEGORY, "category");
     g_object_class_override_property (gobject_class, PROP_ACTIONS, "actions");
     g_object_class_override_property (gobject_class, PROP_ACTION_PREFIX, "action-prefix");
     g_object_class_override_property (gobject_class, PROP_MENU_MODEL, "menu-model");
@@ -937,7 +948,8 @@ seahorse_gpgme_keyring_place_iface (SeahorsePlaceIface *iface)
     iface->get_action_prefix = seahorse_gpgme_keyring_get_action_prefix;
     iface->get_menu_model = seahorse_gpgme_keyring_get_menu_model;
        iface->get_description = seahorse_gpgme_keyring_get_description;
-       iface->get_icon = seahorse_gpgme_keyring_get_icon;
+    iface->get_icon = seahorse_gpgme_keyring_get_icon;
+    iface->get_category = seahorse_gpgme_keyring_get_category;
        iface->get_label = seahorse_gpgme_keyring_get_label;
        iface->set_label = seahorse_gpgme_keyring_set_label;
        iface->get_uri = seahorse_gpgme_keyring_get_uri;
diff --git a/pgp/seahorse-server-source.c b/pgp/seahorse-server-source.c
index da74be0c..c23ddb1e 100644
--- a/pgp/seahorse-server-source.c
+++ b/pgp/seahorse-server-source.c
@@ -47,6 +47,7 @@ enum {
     PROP_LABEL,
     PROP_DESCRIPTION,
     PROP_ICON,
+    PROP_CATEGORY,
     PROP_KEY_SERVER,
     PROP_URI,
     PROP_ACTIONS,
@@ -93,7 +94,8 @@ seahorse_server_source_class_init (SeahorseServerSourceClass *klass)
 
        g_object_class_override_property (gobject_class, PROP_LABEL, "label");
        g_object_class_override_property (gobject_class, PROP_DESCRIPTION, "description");
-       g_object_class_override_property (gobject_class, PROP_ICON, "icon");
+    g_object_class_override_property (gobject_class, PROP_ICON, "icon");
+    g_object_class_override_property (gobject_class, PROP_CATEGORY, "category");
        g_object_class_override_property (gobject_class, PROP_ACTIONS, "actions");
        g_object_class_override_property (gobject_class, PROP_ACTION_PREFIX, "action-prefix");
        g_object_class_override_property (gobject_class, PROP_MENU_MODEL, "menu-model");
@@ -186,6 +188,12 @@ seahorse_server_source_get_icon (SeahorsePlace* self)
        return g_themed_icon_new (NULL);
 }
 
+static SeahorsePlaceCategory
+seahorse_server_source_get_category (SeahorsePlace *place)
+{
+    return SEAHORSE_PLACE_CATEGORY_KEYS;
+}
+
 static GActionGroup *
 seahorse_server_source_get_actions (SeahorsePlace* self)
 {
@@ -213,16 +221,17 @@ seahorse_server_source_get_show_if_empty (SeahorsePlace *place)
 static void
 seahorse_server_source_place_iface (SeahorsePlaceIface *iface)
 {
-       iface->load = seahorse_server_source_load;
-       iface->load_finish = seahorse_server_source_load_finish;
+    iface->load = seahorse_server_source_load;
+    iface->load_finish = seahorse_server_source_load_finish;
     iface->get_actions = seahorse_server_source_get_actions;
     iface->get_action_prefix = seahorse_server_source_get_action_prefix;
     iface->get_menu_model = seahorse_server_source_get_menu_model;
-       iface->get_description = seahorse_server_source_get_description;
-       iface->get_icon = seahorse_server_source_get_icon;
-       iface->get_label = seahorse_server_source_get_label;
-       iface->set_label = seahorse_server_source_set_label;
-       iface->get_uri = seahorse_server_source_get_uri;
+    iface->get_description = seahorse_server_source_get_description;
+    iface->get_icon = seahorse_server_source_get_icon;
+    iface->get_category = seahorse_server_source_get_category;
+    iface->get_label = seahorse_server_source_get_label;
+    iface->set_label = seahorse_server_source_set_label;
+    iface->get_uri = seahorse_server_source_get_uri;
     iface->get_show_if_empty = seahorse_server_source_get_show_if_empty;
 }
 
@@ -298,9 +307,12 @@ seahorse_server_get_property (GObject *obj,
        case PROP_URI:
                g_value_take_string (value, seahorse_server_source_get_uri (place));
                break;
-       case PROP_ICON:
-               g_value_take_object (value, seahorse_server_source_get_icon (place));
-               break;
+    case PROP_ICON:
+        g_value_take_object (value, seahorse_server_source_get_icon (place));
+        break;
+    case PROP_CATEGORY:
+        g_value_set_enum (value, seahorse_server_source_get_category (place));
+        break;
     case PROP_ACTIONS:
         g_value_set_object (value, seahorse_server_source_get_actions (place));
         break;
diff --git a/pgp/seahorse-unknown-source.c b/pgp/seahorse-unknown-source.c
index 7bd721fb..a4f4a308 100644
--- a/pgp/seahorse-unknown-source.c
+++ b/pgp/seahorse-unknown-source.c
@@ -30,14 +30,15 @@
 #include <glib/gi18n.h>
 
 enum {
-       PROP_0,
-       PROP_LABEL,
-       PROP_DESCRIPTION,
-       PROP_ICON,
-       PROP_URI,
-       PROP_ACTIONS,
-       PROP_ACTION_PREFIX,
-       PROP_MENU_MODEL,
+    PROP_0,
+    PROP_LABEL,
+    PROP_DESCRIPTION,
+    PROP_ICON,
+    PROP_CATEGORY,
+    PROP_URI,
+    PROP_ACTIONS,
+    PROP_ACTION_PREFIX,
+    PROP_MENU_MODEL,
     PROP_SHOW_IF_EMPTY,
     N_PROPS
 };
@@ -114,6 +115,12 @@ seahorse_unknown_source_get_icon (SeahorsePlace* self)
        return NULL;
 }
 
+static SeahorsePlaceCategory
+seahorse_unknown_source_get_category (SeahorsePlace *place)
+{
+    return SEAHORSE_PLACE_CATEGORY_KEYS;
+}
+
 static GActionGroup *
 seahorse_unknown_source_get_actions (SeahorsePlace* self)
 {
@@ -156,9 +163,12 @@ seahorse_unknown_source_get_property (GObject *obj,
        case PROP_URI:
                g_value_take_string (value, seahorse_unknown_source_get_uri (place));
                break;
-       case PROP_ICON:
-               g_value_take_object (value, seahorse_unknown_source_get_icon (place));
-               break;
+    case PROP_ICON:
+        g_value_take_object (value, seahorse_unknown_source_get_icon (place));
+        break;
+    case PROP_CATEGORY:
+        g_value_set_enum (value, seahorse_unknown_source_get_category (place));
+        break;
     case PROP_ACTIONS:
         g_value_take_object (value, seahorse_unknown_source_get_actions (place));
         break;
@@ -214,13 +224,14 @@ seahorse_unknown_source_class_init (SeahorseUnknownSourceClass *klass)
        gobject_class->set_property = seahorse_unknown_source_set_property;
        gobject_class->finalize = seahorse_unknown_source_finalize;
 
-       g_object_class_override_property (gobject_class, PROP_LABEL, "label");
-       g_object_class_override_property (gobject_class, PROP_DESCRIPTION, "description");
-       g_object_class_override_property (gobject_class, PROP_ICON, "icon");
-       g_object_class_override_property (gobject_class, PROP_ACTIONS, "actions");
-       g_object_class_override_property (gobject_class, PROP_ACTION_PREFIX, "action-prefix");
-       g_object_class_override_property (gobject_class, PROP_MENU_MODEL, "menu-model");
-       g_object_class_override_property (gobject_class, PROP_URI, "uri");
+    g_object_class_override_property (gobject_class, PROP_LABEL, "label");
+    g_object_class_override_property (gobject_class, PROP_DESCRIPTION, "description");
+    g_object_class_override_property (gobject_class, PROP_ICON, "icon");
+    g_object_class_override_property (gobject_class, PROP_CATEGORY, "category");
+    g_object_class_override_property (gobject_class, PROP_ACTIONS, "actions");
+    g_object_class_override_property (gobject_class, PROP_ACTION_PREFIX, "action-prefix");
+    g_object_class_override_property (gobject_class, PROP_MENU_MODEL, "menu-model");
+    g_object_class_override_property (gobject_class, PROP_URI, "uri");
     g_object_class_override_property (gobject_class, PROP_SHOW_IF_EMPTY, "show-if-empty");
 }
 
@@ -258,15 +269,16 @@ seahorse_unknown_source_collection_iface (GcrCollectionIface *iface)
 static void
 seahorse_unknown_source_place_iface (SeahorsePlaceIface *iface)
 {
-       iface->load = seahorse_unknown_source_load;
-       iface->load_finish = seahorse_unknown_source_load_finish;
-       iface->get_actions = seahorse_unknown_source_get_actions;
-       iface->get_menu_model = seahorse_unknown_source_get_menu_model;
-       iface->get_description = seahorse_unknown_source_get_description;
-       iface->get_icon = seahorse_unknown_source_get_icon;
-       iface->get_label = seahorse_unknown_source_get_label;
-       iface->set_label = seahorse_unknown_source_set_label;
-       iface->get_uri = seahorse_unknown_source_get_uri;
+    iface->load = seahorse_unknown_source_load;
+    iface->load_finish = seahorse_unknown_source_load_finish;
+    iface->get_actions = seahorse_unknown_source_get_actions;
+    iface->get_menu_model = seahorse_unknown_source_get_menu_model;
+    iface->get_description = seahorse_unknown_source_get_description;
+    iface->get_icon = seahorse_unknown_source_get_icon;
+    iface->get_category = seahorse_unknown_source_get_category;
+    iface->get_label = seahorse_unknown_source_get_label;
+    iface->set_label = seahorse_unknown_source_set_label;
+    iface->get_uri = seahorse_unknown_source_get_uri;
     iface->get_show_if_empty = seahorse_unknown_source_get_show_if_empty;
 }
 
diff --git a/pkcs11/pkcs11-token.vala b/pkcs11/pkcs11-token.vala
index 497ee2b7..28db57fc 100644
--- a/pkcs11/pkcs11-token.vala
+++ b/pkcs11/pkcs11-token.vala
@@ -100,6 +100,10 @@ public class Token : GLib.Object, Gcr.Collection, Place, Lockable {
                }
        }
 
+    public Place.Category category {
+        get { return Place.Category.CERTIFICATES; }
+    }
+
     public GLib.ActionGroup? actions {
         owned get { return null; }
     }
diff --git a/src/sidebar.vala b/src/sidebar.vala
index c2f19fa7..ce4035d0 100644
--- a/src/sidebar.vala
+++ b/src/sidebar.vala
@@ -60,12 +60,6 @@ public class Seahorse.Sidebar : Gtk.ListBox {
             foreach (weak GLib.Object obj in backend.get_objects())
                 on_place_added(backend, obj);
         }
-
-        this.backends.sort((a, b) => {
-            int ordera = order_from_backend((Backend) a);
-            int orderb = order_from_backend((Backend) b);
-            return ordera - orderb;
-        });
     }
 
     private void on_place_added(Gcr.Collection? backend, GLib.Object place_obj) {
@@ -114,80 +108,38 @@ public class Seahorse.Sidebar : Gtk.ListBox {
 
     private void place_header_cb(Gtk.ListBoxRow row, Gtk.ListBoxRow? before) {
         Seahorse.Place place = ((SidebarItem) row).place;
-        string scheme = Uri.parse_scheme(place.uri);
 
         // We don't need a title iff
         // * there is no previous row
-        // * the previous row is from another backend
+        // * the previous row is in the same category
         if (before != null) {
             Seahorse.Place before_place = ((SidebarItem) before).place;
-            if (Uri.parse_scheme(before_place.uri) == scheme)
-                return;
-        }
-
-        // Find the backend that has the given scheme
-        foreach (var b in this.backends) {
-            if (place in b) {
-                var label = new Gtk.Label(b.label);
-                label.tooltip_text = b.description;
-                label.get_style_context().add_class("seahorse-sidebar-item-header");
-                label.xalign = 0f;
-                label.margin_start = 9;
-                label.margin_top = 6;
-                label.margin_bottom = 3;
-                label.show();
-                row.set_header(label);
+            if (place.category == before_place.category)
                 return;
-            }
         }
 
-        warning("Couldn't find backend for place %s", place.label);
+        var label = new Gtk.Label(place.category.to_string());
+        label.get_style_context().add_class("seahorse-sidebar-item-header");
+        label.xalign = 0f;
+        label.margin_start = 9;
+        label.margin_top = 6;
+        label.margin_bottom = 3;
+        label.show();
+        row.set_header(label);
     }
 
     private int compare_places(GLib.Object obj_a, GLib.Object obj_b) {
         Seahorse.Place a = (Seahorse.Place) obj_a;
         Seahorse.Place b = (Seahorse.Place) obj_b;
 
-        // First of all, order the backends (SSH vs GPG)
-        // Since there is no easy way to map a place to its original backend,
-        // we can use the URI scheme
-        var a_scheme = GLib.Uri.parse_scheme(a.uri);
-        var b_scheme = GLib.Uri.parse_scheme(b.uri);
-        if (a_scheme != b_scheme)
-            return order_from_scheme(b_scheme) - order_from_scheme(a_scheme);
+        // First of all, order the categories
+        if (a.category != b.category)
+            return ((int) a.category) - ((int) b.category);
 
-        // In the same backend, order alphabetically
+        // In the same category, order alphabetically
         return a.label.casefold().collate(b.label.casefold());
     }
 
-    private struct BackendEntry {
-        unowned string name;
-        unowned string scheme;
-    }
-    // Note that this is really the reverse order
-    const BackendEntry[] BACKEND_ORDER = {
-        { "pkcs11", "pkcs11" },
-        { "pgp", "gnupg" },
-        { "ssh", "openssh" },
-        { "gkr", "secret-service" },
-    };
-
-    private static int order_from_backend (Backend backend) {
-        for (int i = 0; i < BACKEND_ORDER.length; i++)
-            if (backend.name == BACKEND_ORDER[i].name)
-                return i;
-
-        return BACKEND_ORDER.length + 1;
-    }
-
-    private static int order_from_scheme(string scheme) {
-        for (int i = 0; i < BACKEND_ORDER.length; i++)
-            if (scheme == BACKEND_ORDER[i].scheme)
-                return i;
-
-        return BACKEND_ORDER.length + 1;
-    }
-
     private void on_row_selected(Gtk.ListBoxRow? row) {
         debug("Updating objects");
 
diff --git a/ssh/source.vala b/ssh/source.vala
index 5e840499..68843a5c 100644
--- a/ssh/source.vala
+++ b/ssh/source.vala
@@ -55,6 +55,10 @@ public class Seahorse.Ssh.Source : GLib.Object, Gcr.Collection, Seahorse.Place {
         owned get { return new ThemedIcon(Gcr.ICON_HOME_DIRECTORY); }
     }
 
+    public Place.Category category {
+        get { return Place.Category.KEYS; }
+    }
+
     public GLib.ActionGroup? actions {
         owned get { return null; }
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]