[geary/mjog/email-templates: 56/72] Plugin.EmailStore: Add methods for getting email ids from variants



commit aff599821a07aa775a907a9e23ea94a3d02f2164
Author: Michael Gratton <mike vee net>
Date:   Sat Apr 18 18:39:29 2020 +1000

    Plugin.EmailStore: Add methods for getting email ids from variants
    
    Implement these in Application.EmailStoreFactory's implementation of
    the interface.
    
    This switches the variant format used to `(account_id,email_id)` to
    match that used for folders, and so a folder doesn't need to be
    specified when showing an email.
    
    This breaks opening email from desktop notifications, but that will be
    fixed shortly.

 .../application-email-store-factory.vala           | 72 +++++++++++++++++++---
 src/client/plugin/plugin-email-store.vala          | 16 +++++
 2 files changed, 80 insertions(+), 8 deletions(-)
---
diff --git a/src/client/application/application-email-store-factory.vala 
b/src/client/application/application-email-store-factory.vala
index 7b8322e4..9ac04e24 100644
--- a/src/client/application/application-email-store-factory.vala
+++ b/src/client/application/application-email-store-factory.vala
@@ -23,14 +23,19 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
     private class EmailStoreImpl : Geary.BaseObject, Plugin.EmailStore {
 
 
-        private Gee.Map<AccountContext,PluginManager.AccountImpl> accounts;
+        private weak EmailStoreFactory factory;
 
 
-        public EmailStoreImpl(
-            Gee.Map<AccountContext,PluginManager.AccountImpl> accounts
-        ) {
-            this.accounts = accounts;
+        public EmailStoreImpl(EmailStoreFactory factory) {
+            this.factory = factory;
+        }
+
+        public override GLib.VariantType email_identifier_variant_type {
+            get { return this._email_id_variant_type; }
         }
+        private GLib.VariantType _email_id_variant_type = new GLib.VariantType(
+            "(sv)"
+        );
 
         public async Gee.Collection<Plugin.Email> get_email(
             Gee.Collection<Plugin.EmailIdentifier> plugin_ids,
@@ -73,7 +78,9 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
                 if (batch != null) {
                     foreach (var email in batch) {
                         emails.add(
-                            new EmailImpl(email, this.accounts.get(context))
+                            new EmailImpl(
+                                email,
+                                this.factory.accounts.get(context))
                         );
                     }
                 }
@@ -82,6 +89,21 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
             return emails;
         }
 
+        public Plugin.EmailIdentifier? get_email_identifier_from_variant(
+            GLib.Variant variant
+        ) {
+            var account = this.factory.get_account_from_variant(variant);
+            var id = this.factory.get_email_identifier_from_variant(variant);
+            IdImpl? plugin_id = null;
+            if (account != null && id != null) {
+                var plugin_account = this.factory.accounts.get(account);
+                if (plugin_account != null) {
+                    plugin_id = new IdImpl(id, plugin_account);
+                }
+            }
+            return plugin_id;
+        }
+
         internal void destroy() {
             // noop
         }
@@ -148,7 +170,10 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
         }
 
         public GLib.Variant to_variant() {
-            return this.backing.to_variant();
+            return new GLib.Variant.tuple({
+                    this._account.backing.account.information.id,
+                        new GLib.Variant.variant(this.backing.to_variant())
+            });
         }
 
         public bool equal_to(Plugin.EmailIdentifier other) {
@@ -194,7 +219,7 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
 
     /** Constructs a new email store for use by plugin contexts. */
     public Plugin.EmailStore new_email_store() {
-        var store = new EmailStoreImpl(this.accounts);
+        var store = new EmailStoreImpl(this);
         this.stores.add(store);
         return store;
     }
@@ -229,4 +254,35 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
         return new EmailImpl(engine, this.accounts.get(account));
     }
 
+    /** Returns the account context for the given plugin email id. */
+    public AccountContext get_account_from_variant(GLib.Variant target) {
+        AccountContext? account = null;
+        string id = (string) target.get_child_value(0);
+        foreach (var context in this.accounts.keys) {
+            var info = context.account.information;
+            if (info.id == id) {
+                account = context;
+                break;
+            }
+        }
+        return account;
+    }
+
+    /** Returns the engine email id for the given plugin email id. */
+    public Geary.EmailIdentifier?
+        get_email_identifier_from_variant(GLib.Variant target) {
+        Geary.EmailIdentifier? id = null;
+        var context = get_account_from_variant(target);
+        if (context != null) {
+            try {
+                id = context.account.to_email_identifier(
+                    target.get_child_value(1).get_variant()
+                );
+            } catch (GLib.Error err) {
+                debug("Invalid email folder id: %s", err.message);
+            }
+        }
+        return id;
+    }
+
 }
diff --git a/src/client/plugin/plugin-email-store.vala b/src/client/plugin/plugin-email-store.vala
index 4f3dcef6..89727d3e 100644
--- a/src/client/plugin/plugin-email-store.vala
+++ b/src/client/plugin/plugin-email-store.vala
@@ -16,6 +16,14 @@
 public interface Plugin.EmailStore : Geary.BaseObject {
 
 
+    /**
+     * The type of variant email identifiers.
+     *
+     * @see EmailIdentifier.to_variant
+     * @see get_email_identifier_from_variant
+     */
+    public abstract GLib.VariantType email_identifier_variant_type { get; }
+
     /** Emitted when an email has been displayed in the UI. */
     public signal void email_displayed(Email sent);
 
@@ -28,4 +36,12 @@ public interface Plugin.EmailStore : Geary.BaseObject {
         GLib.Cancellable? cancellable
     ) throws GLib.Error;
 
+    /**
+     * Returns the email identifier specified by the given variant, if any.
+     *
+     * @see EmailIdentifier.to_variant
+     * @see email_identifier_variant_type
+     */
+    public abstract EmailIdentifier? get_email_identifier_from_variant(GLib.Variant id);
+
 }


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