[geary/mjog/email-templates: 50/72] Application.PluginManager: Move plugin account mgmt from folder factory



commit 5a94d4bd018e182f6a40db029ebc90ae641648c4
Author: Michael Gratton <mike vee net>
Date:   Tue Apr 7 09:16:51 2020 +1000

    Application.PluginManager: Move plugin account mgmt from folder factory
    
    Move implementation and management of Plugin.Account instances from
    FolderStoreFactory here, so that other objects (e.g. plugin composers)
    can access the implementation's backing account context.

 .../application-folder-store-factory.vala          | 91 ++++------------------
 .../application/application-plugin-manager.vala    | 68 ++++++++++++++--
 2 files changed, 80 insertions(+), 79 deletions(-)
---
diff --git a/src/client/application/application-folder-store-factory.vala 
b/src/client/application/application-folder-store-factory.vala
index 70d2e2f3..b7027da0 100644
--- a/src/client/application/application-folder-store-factory.vala
+++ b/src/client/application/application-folder-store-factory.vala
@@ -81,25 +81,6 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
     }
 
 
-    private class AccountImpl : Geary.BaseObject, Plugin.Account {
-
-
-        public string display_name {
-            get { return this.backing.account.information.display_name; }
-        }
-
-
-        /** The underlying backing account context for this account. */
-        internal AccountContext backing { get; private set; }
-
-
-        public AccountImpl(AccountContext backing) {
-            this.backing = backing;
-        }
-
-    }
-
-
     private class FolderImpl : Geary.BaseObject, Plugin.Folder {
 
 
@@ -125,13 +106,14 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
         public Plugin.Account? account {
             get { return this._account; }
         }
-        private AccountImpl? _account;
+        private PluginManager.AccountImpl? _account;
 
         // The underlying folder being represented
         internal FolderContext backing { get; private set; }
 
 
-        public FolderImpl(FolderContext backing, AccountImpl? account) {
+        public FolderImpl(FolderContext backing,
+                          PluginManager.AccountImpl? account) {
             this.backing = backing;
             this._account = account;
             this._persistent_id = ID_FORMAT.printf(
@@ -159,8 +141,7 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
 
     private Controller controller;
 
-    private Gee.Map<Geary.AccountInformation,AccountImpl> accounts =
-        new Gee.HashMap<Geary.AccountInformation,AccountImpl>();
+    private Gee.Map<Geary.AccountInformation,PluginManager.AccountImpl> accounts;
     private Gee.Map<Geary.Folder,FolderImpl> folders =
         new Gee.HashMap<Geary.Folder,FolderImpl>();
     private Gee.Set<FolderStoreImpl> stores =
@@ -170,21 +151,14 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
     /**
      * Constructs a new factory instance.
      */
-    public FolderStoreFactory(Controller controller) throws GLib.Error {
+    public FolderStoreFactory(Controller controller,
+                              Gee.Map<Geary.AccountInformation,PluginManager.AccountImpl> accounts) {
         this.controller = controller;
-        this.controller.account_available.connect(
-            on_account_available
-        );
-        this.controller.account_unavailable.connect(
-            on_account_unavailable
-        );
-        foreach (var context in controller.get_account_contexts()) {
-            add_account(context);
-        }
         this.controller.application.window_added.connect(on_window_added);
         foreach (var main in this.controller.application.get_main_windows()) {
             main.notify["selected-folder"].connect(on_folder_selected);
         }
+        this.accounts = accounts;
     }
 
     /** Clearing all state of the store. */
@@ -194,18 +168,6 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
             store.destroy();
         }
         this.stores.clear();
-
-        this.controller.account_available.disconnect(
-            on_account_available
-        );
-        this.controller.account_unavailable.disconnect(
-            on_account_unavailable
-        );
-        // take a copy of the key set so the iterator doesn't asplode
-        // as accounts are removed
-        foreach (var context in this.accounts.values.to_array()) {
-            remove_account(context.backing);
-        }
         this.folders.clear();
     }
 
@@ -225,12 +187,6 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
         }
     }
 
-    /** Returns the folder context for the given plugin folder. */
-    public AccountContext get_account_context(Plugin.Account plugin) {
-        AccountImpl? impl = plugin as AccountImpl;
-        return (impl != null) ? impl.backing : null;
-    }
-
     /** Returns the plugin folder for the given engine folder. */
     public Plugin.Folder? get_plugin_folder(Geary.Folder engine) {
         return this.folders.get(engine);
@@ -248,23 +204,18 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
         return (impl != null) ? impl.backing : null;
     }
 
-    private void add_account(AccountContext added) {
-        if (!this.accounts.has_key(added.account.information)) {
-            this.accounts.set(added.account.information, new AccountImpl(added));
-            added.folders_available.connect(on_folders_available);
-            added.folders_unavailable.connect(on_folders_unavailable);
-            added.account.folders_use_changed.connect(on_folders_use_changed);
-            add_folders(added.get_folders());
-         }
+    internal void add_account(AccountContext added) {
+        added.folders_available.connect(on_folders_available);
+        added.folders_unavailable.connect(on_folders_unavailable);
+        added.account.folders_use_changed.connect(on_folders_use_changed);
+        add_folders(added.get_folders());
      }
 
-    private void remove_account(AccountContext removed) {
-        if (this.accounts.unset(removed.account.information)) {
-            removed.folders_available.disconnect(on_folders_available);
-            removed.folders_unavailable.disconnect(on_folders_unavailable);
-            removed.account.folders_use_changed.disconnect(on_folders_use_changed);
-            remove_folders(removed.get_folders());
-        }
+    internal void remove_account(AccountContext removed) {
+        removed.folders_available.disconnect(on_folders_available);
+        removed.folders_unavailable.disconnect(on_folders_unavailable);
+        removed.account.folders_use_changed.disconnect(on_folders_use_changed);
+        remove_folders(removed.get_folders());
     }
 
     private void add_folders(Gee.Collection<FolderContext> to_add) {
@@ -311,14 +262,6 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
         ).to_linked_list().read_only_view;
     }
 
-    private void on_account_available(AccountContext available) {
-        add_account(available);
-    }
-
-    private void on_account_unavailable(AccountContext unavailable) {
-        remove_account(unavailable);
-    }
-
     private void on_folders_available(AccountContext account,
                                       Gee.Collection<FolderContext> available) {
         add_folders(available);
diff --git a/src/client/application/application-plugin-manager.vala 
b/src/client/application/application-plugin-manager.vala
index b52473fc..da03f2ed 100644
--- a/src/client/application/application-plugin-manager.vala
+++ b/src/client/application/application-plugin-manager.vala
@@ -66,11 +66,11 @@ public class Application.PluginManager : GLib.Object {
 
         public Plugin.Composer new_composer(Plugin.Account source)
             throws Plugin.Error {
-            AccountContext? account = this.folders.get_account_context(source);
-            if (account == null) {
-                throw new Plugin.Error.NOT_SUPPORTED("No such account");
+            var impl = source as AccountImpl;
+            if (impl == null) {
+                throw new Plugin.Error.NOT_SUPPORTED("Not a valid account");
             }
-            return new ComposerImpl(this.backing, account);
+            return new ComposerImpl(this.backing, impl.backing);
         }
 
         public void register_action(GLib.Action action) {
@@ -150,6 +150,25 @@ public class Application.PluginManager : GLib.Object {
     }
 
 
+    internal class AccountImpl : Geary.BaseObject, Plugin.Account {
+
+
+        public string display_name {
+            get { return this.backing.account.information.display_name; }
+        }
+
+
+        /** The underlying backing account context for this account. */
+        internal AccountContext backing { get; private set; }
+
+
+        public AccountImpl(AccountContext backing) {
+            this.backing = backing;
+        }
+
+    }
+
+
     private class ComposerImpl : Geary.BaseObject, Plugin.Composer {
 
 
@@ -196,6 +215,8 @@ public class Application.PluginManager : GLib.Object {
     private bool is_shutdown = false;
     private string trusted_path;
 
+    private Gee.Map<Geary.AccountInformation,AccountImpl> plugin_accounts =
+        new Gee.HashMap<Geary.AccountInformation,AccountImpl>();
     private FolderStoreFactory folders_factory;
     private EmailStoreFactory email_factory;
 
@@ -215,7 +236,9 @@ public class Application.PluginManager : GLib.Object {
         this.controller = controller;
         this.config = config;
         this.plugins = Peas.Engine.get_default();
-        this.folders_factory = new FolderStoreFactory(controller);
+        this.folders_factory = new FolderStoreFactory(
+            controller, this.plugin_accounts.read_only_view
+        );
         this.email_factory = new EmailStoreFactory(controller);
 
         this.trusted_path = trusted_plugin_path.get_path();
@@ -241,6 +264,16 @@ public class Application.PluginManager : GLib.Object {
                 warning("Plugin %s not available: %s", name, err.message);
             }
         }
+
+        this.controller.account_available.connect(
+            on_account_available
+        );
+        this.controller.account_unavailable.connect(
+            on_account_unavailable
+        );
+        foreach (var context in this.controller.get_account_contexts()) {
+            add_account(context);
+        }
     }
 
     /** Returns the engine folder for the given plugin folder, if any. */
@@ -305,6 +338,13 @@ public class Application.PluginManager : GLib.Object {
 
     internal void close() throws GLib.Error {
         this.is_shutdown = true;
+
+        this.controller.account_unavailable.disconnect(on_account_unavailable);
+        this.controller.account_available.disconnect(on_account_available);
+        foreach (var context in this.controller.get_account_contexts()) {
+            remove_account(context);
+        }
+
         this.plugins.set_loaded_plugins(null);
         this.plugins.garbage_collect();
         this.folders_factory.destroy();
@@ -323,6 +363,16 @@ public class Application.PluginManager : GLib.Object {
         return this.email_contexts.values.read_only_view;
     }
 
+    internal void add_account(AccountContext added) {
+        this.plugin_accounts.set(added.account.information, new AccountImpl(added));
+        this.folders_factory.add_account(added);
+    }
+
+    internal void remove_account(AccountContext removed) {
+        this.folders_factory.remove_account(removed);
+        this.plugin_accounts.unset(removed.account.information);
+    }
+
     private void on_load_plugin(Peas.PluginInfo info) {
         var plugin_application = new ApplicationImpl(
             info, this.application, this.folders_factory
@@ -463,4 +513,12 @@ public class Application.PluginManager : GLib.Object {
         this.plugin_set.unset(context.info);
     }
 
+    private void on_account_available(AccountContext available) {
+        add_account(available);
+    }
+
+    private void on_account_unavailable(AccountContext unavailable) {
+        remove_account(unavailable);
+    }
+
 }


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