[geary/mjog/folder-actions: 7/7] Plugin.FolderStore: Allow plugins to get folders from variant id



commit 55a9bfaf56604e513d2afe4603abea5507c3fb3d
Author: Michael Gratton <mike vee net>
Date:   Fri Mar 20 15:21:26 2020 +1100

    Plugin.FolderStore: Allow plugins to get folders from variant id
    
    Add FolderStore::get_folder_for_variant and ::folder_variant_type,
    provide an initial implemetnation and update API docs to point to this
    use case.

 .../application-folder-store-factory.vala            | 20 +++++++++++++++++++-
 src/client/plugin/plugin-folder-store.vala           | 16 ++++++++++++++++
 src/client/plugin/plugin-folder.vala                 |  6 +++++-
 3 files changed, 40 insertions(+), 2 deletions(-)
---
diff --git a/src/client/application/application-folder-store-factory.vala 
b/src/client/application/application-folder-store-factory.vala
index b5839f49..e68d0436 100644
--- a/src/client/application/application-folder-store-factory.vala
+++ b/src/client/application/application-folder-store-factory.vala
@@ -17,6 +17,13 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
     private class FolderStoreImpl : Geary.BaseObject, Plugin.FolderStore {
 
 
+        public override GLib.VariantType folder_variant_type {
+            get { return this._folder_variant_type; }
+        }
+        private GLib.VariantType _folder_variant_type = new GLib.VariantType(
+            "(sv)"
+        );
+
         private Gee.Map<Geary.Folder,FolderImpl> folders;
 
 
@@ -24,11 +31,22 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
             this.folders = folders;
         }
 
-        /** Returns a read-only set of all known folders. */
         public Gee.Collection<Plugin.Folder> get_folders() {
             return this.folders.values.read_only_view;
         }
 
+        public Plugin.Folder? get_folder_from_variant(GLib.Variant variant) {
+            Plugin.Folder? found = null;
+            // XXX this is pretty inefficient
+            foreach (var folder in this.folders.values) {
+                if (folder.to_variant().equal(variant)) {
+                    found = folder;
+                    break;
+                }
+            }
+            return found;
+        }
+
         internal void destroy() {
             this.folders = Gee.Map.empty();
         }
diff --git a/src/client/plugin/plugin-folder-store.vala b/src/client/plugin/plugin-folder-store.vala
index dd088e1e..3cc8b977 100644
--- a/src/client/plugin/plugin-folder-store.vala
+++ b/src/client/plugin/plugin-folder-store.vala
@@ -15,6 +15,14 @@
 public interface Plugin.FolderStore : Geary.BaseObject {
 
 
+    /**
+     * The type of the folder identifiers
+     *
+     * @see Folder.to_variant
+     * @see get_folder_from_variant
+     */
+    public abstract GLib.VariantType folder_variant_type { get; }
+
     /** Emitted when new folders are available. */
     public signal void folders_available(Gee.Collection<Folder> available);
 
@@ -31,5 +39,13 @@ public interface Plugin.FolderStore : Geary.BaseObject {
     /** Returns a read-only set of all known folders. */
     public abstract Gee.Collection<Folder> get_folders();
 
+    /**
+     * Returns the folder specified by the given identifier, if any.
+     *
+     * @see Folder.to_variant
+     * @see folder_variant_type
+     */
+    public abstract Folder? get_folder_from_variant(GLib.Variant id);
+
 
 }
diff --git a/src/client/plugin/plugin-folder.vala b/src/client/plugin/plugin-folder.vala
index 0765dff1..85bb8375 100644
--- a/src/client/plugin/plugin-folder.vala
+++ b/src/client/plugin/plugin-folder.vala
@@ -32,8 +32,12 @@ public interface Plugin.Folder : Geary.BaseObject {
     /**
      * Returns a variant identifying this account and folder.
      *
-     * This value is suitable to be used as the `show-folder`
+     * This value can be used to obtain folders from {@link
+     * FolderStore}, and is suitable to be used as the `show-folder`
      * application action parameter.
+     *
+     * @see FolderStore.get_folder_from_variant
+     * @see folder_variant_type
      */
     public abstract GLib.Variant to_variant();
 


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