[seahorse] Save/Restore last selected sidebar entry



commit 1040cc5fc422210ef0eeecee48140a3ce8d0bd20
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Tue Jun 16 20:09:10 2020 +0200

    Save/Restore last selected sidebar entry
    
    This is a regression that popped up after migrating to `GtkListBox` for
    the Sidebar and the KeyManager.
    
    Fixes https://gitlab.gnome.org/GNOME/seahorse/-/issues/264

 src/key-manager.vala | 35 +++++++++++++++++++++++++++++------
 src/sidebar.vala     | 30 +++++++++++++++++++++++++++---
 2 files changed, 56 insertions(+), 9 deletions(-)
---
diff --git a/src/key-manager.vala b/src/key-manager.vala
index 57d7189f..caac9fce 100644
--- a/src/key-manager.vala
+++ b/src/key-manager.vala
@@ -407,14 +407,38 @@ public class Seahorse.KeyManager : Catalog {
     private void on_focus_place(SimpleAction action, Variant? param) {
         string? uri_prefix = param.get_string();
         if (uri_prefix != null) {
-            this.sidebar.set_focused_place(uri_prefix);
+            this.sidebar.set_focused_place_for_scheme(uri_prefix);
         }
     }
 
     private Gcr.Collection setup_sidebar() {
         this.sidebar = new Sidebar();
 
-        /* Make sure we update the empty state on any change */
+        // On setup, select the LRU place from our last session (if any).
+        // Since libsecret inits asynchronously, wait a bit beforehand.
+        Timeout.add (200, () => {
+            var last_keyring = this.settings.get_strv("keyrings-selected");
+            if (last_keyring == null || last_keyring.length == 0)
+                return GLib.Source.REMOVE;
+
+            unowned string uri = last_keyring[0];
+
+            debug("Selecting last used place %s", uri);
+            if (this.sidebar.set_focused_place_for_uri(uri))
+                return GLib.Source.REMOVE;
+
+            // If that didn't work, try do a attempt by matching the scheme.
+            // FIXME should do this poperly (not always getting proper URIs atm)
+            var uri_scheme = uri.split(":")[0];
+            if (this.sidebar.set_focused_place_for_scheme(uri_scheme))
+                return GLib.Source.REMOVE;
+
+            // Still nothing. Can happen on first run -> just select first entry
+            this.sidebar.select_row(this.sidebar.get_row_at_index(0));
+            return GLib.Source.REMOVE;
+        });
+
+        // Make sure we update the empty state on any change
         this.sidebar.selected_rows_changed.connect(on_sidebar_selected_rows_changed);
         this.sidebar.current_collection_changed.connect((sidebar) => { check_empty_state (); });
 
@@ -436,11 +460,10 @@ public class Seahorse.KeyManager : Catalog {
         show_item_list_pane();
 
         Place? place = this.sidebar.get_focused_place();
-        if (place != null)
-            this.right_header.title = place.label;
+        return_if_fail (place != null);
 
-        // FIXME
-        //this.settings.set_strv("keyrings-selected", );
+        this.right_header.title = place.label;
+        this.settings.set_strv("keyrings-selected", { place.uri });
     }
 
     public override List<weak Backend> get_backends() {
diff --git a/src/sidebar.vala b/src/sidebar.vala
index 461e3547..e712a0a9 100644
--- a/src/sidebar.vala
+++ b/src/sidebar.vala
@@ -241,14 +241,38 @@ public class Seahorse.Sidebar : Gtk.ListBox {
         return (row != null)? row.place : null;
     }
 
-    public void set_focused_place(string uri_prefix) {
+    /**
+     * Tries to find a place with the given URI and selects it.
+     *
+     * @return Whether a matching place was found
+     */
+    public bool set_focused_place_for_uri(string uri) {
+        // First, try to see if we have an exact match
         foreach (var row in get_children()) {
             var item = (SidebarItem) row;
-            if (item.place.uri.has_prefix(uri_prefix)) {
+            if (item.place.uri == uri) {
                 select_row(item);
-                break;
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
+     * Tries to find a place with the given URI scheme and selects it.
+     *
+     * @return Whether a matching place was found
+     */
+    public bool set_focused_place_for_scheme(string uri_scheme) {
+        foreach (var row in get_children()) {
+            var item = (SidebarItem) row;
+            if (item.place.uri.has_prefix(uri_scheme)) {
+                select_row(item);
+                return true;
             }
         }
+        return false;
     }
 
     public List<weak Backend>? get_backends() {


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