Application: Pass Controller instance to to plugin objects directly

Most of the plugin implementation classes require instances of the
controller and other objects obtained from the application instance,
rather than the application instance itself. So pass that and the other
objects in instead of the application.
This commit is contained in:
Michael Gratton 2020-04-06 09:53:05 +10:00 committed by Michael James Gratton
parent 778cb2d637
commit 60f3fac66e
4 changed files with 39 additions and 78 deletions

View file

@ -162,7 +162,12 @@ internal class Application.Controller : Geary.BaseObject {
}
this.plugins = new PluginManager(this.application);
this.plugins = new PluginManager(
this.application,
this,
this.application.config,
this.application.get_app_plugins_dir()
);
// Migrate configuration if necessary.
Migrate.xdg_config_dir(this.application.get_user_data_directory(),

View file

@ -23,11 +23,11 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
private class EmailStoreImpl : Geary.BaseObject, Plugin.EmailStore {
private Client backing;
private Controller controller;
public EmailStoreImpl(Client backing) {
this.backing = backing;
public EmailStoreImpl(Controller controller) {
this.controller = controller;
}
public async Gee.Collection<Plugin.Email> get_email(
@ -63,7 +63,7 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
foreach (var account in accounts.keys) {
AccountContext context =
this.backing.controller.get_context_for_account(account);
this.controller.get_context_for_account(account);
Gee.Collection<Geary.Email> batch =
yield context.emails.list_email_by_sparse_id_async(
accounts.get(account),
@ -118,7 +118,7 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
internal EmailImpl(Geary.Email backing,
Geary.AccountInformation account) {
Geary.AccountInformation account) {
this.backing = backing;
this.account = account;
Geary.RFC822.Subject? subject = this.backing.subject;
@ -171,7 +171,7 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
}
private Client application;
private Controller application;
private Gee.Set<EmailStoreImpl> stores =
new Gee.HashSet<EmailStoreImpl>();
@ -179,7 +179,7 @@ internal class Application.EmailStoreFactory : Geary.BaseObject {
/**
* Constructs a new factory instance.
*/
public EmailStoreFactory(Client application) throws GLib.Error {
public EmailStoreFactory(Controller application) throws GLib.Error {
this.application = application;
}

View file

@ -24,13 +24,13 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
"(sv)"
);
private Client application;
private Controller controller;
private Gee.Map<Geary.Folder,FolderImpl> folders;
public FolderStoreImpl(Client application,
public FolderStoreImpl(Controller controller,
Gee.Map<Geary.Folder,FolderImpl> folders) {
this.application = application;
this.controller = controller;
this.folders = folders;
}
@ -45,7 +45,7 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
var id = target as EmailStoreFactory.IdImpl;
var folders = new Gee.LinkedList<Plugin.Folder>();
AccountContext context =
this.application.controller.get_context_for_account(id.account);
this.controller.get_context_for_account(id.account);
if (id != null && context != null) {
Gee.MultiMap<Geary.EmailIdentifier,Geary.FolderPath>? multi_folders =
yield context.account.get_containing_folders_async(
@ -157,8 +157,7 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
}
private Client application;
private Geary.Engine engine;
private Controller controller;
private Gee.Map<Geary.AccountInformation,AccountImpl> accounts =
new Gee.HashMap<Geary.AccountInformation,AccountImpl>();
@ -171,39 +170,23 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
/**
* Constructs a new factory instance.
*/
public FolderStoreFactory(Client application) throws GLib.Error {
this.application = application;
this.engine = application.engine;
this.engine.account_available.connect(on_account_available);
this.engine.account_unavailable.connect(on_account_unavailable);
foreach (Geary.Account account in this.engine.get_accounts()) {
add_account(account.information);
}
application.window_added.connect(on_window_added);
foreach (MainWindow main in this.application.get_main_windows()) {
main.notify["selected-folder"].connect(on_folder_selected);
}
public FolderStoreFactory(Controller controller) throws GLib.Error {
this.controller = controller;
}
/** Clearing all state of the store. */
public void destroy() throws GLib.Error {
this.application.window_added.disconnect(on_window_added);
foreach (FolderStoreImpl store in this.stores) {
store.destroy();
}
this.stores.clear();
this.engine.account_available.disconnect(on_account_available);
this.engine.account_unavailable.disconnect(on_account_unavailable);
foreach (Geary.Account account in this.engine.get_accounts()) {
remove_account(account.information);
}
this.folders.clear();
}
/** Constructs a new folder store for use by plugin contexts. */
public Plugin.FolderStore new_folder_store() {
var store = new FolderStoreImpl(this.application, this.folders);
var store = new FolderStoreImpl(this.controller, this.folders);
this.stores.add(store);
return store;
}
@ -229,41 +212,9 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
}
private void add_account(Geary.AccountInformation added) {
try {
this.accounts.set(added, new AccountImpl(added));
Geary.Account account = this.engine.get_account(added);
account.folders_available_unavailable.connect(
on_folders_available_unavailable
);
account.folders_use_changed.connect(
on_folders_use_changed
);
add_folders(account.list_folders());
} catch (GLib.Error err) {
warning(
"Failed to add account %s to folder store: %s",
added.id, err.message
);
}
}
private void remove_account(Geary.AccountInformation removed) {
try {
Geary.Account account = this.engine.get_account(removed);
account.folders_available_unavailable.disconnect(
on_folders_available_unavailable
);
account.folders_use_changed.disconnect(
on_folders_use_changed
);
remove_folders(account.list_folders());
this.accounts.unset(removed);
} catch (GLib.Error err) {
warning(
"Error removing account %s from folder store: %s",
removed.id, err.message
);
}
}
private void add_folders(Gee.Collection<Geary.Folder> to_add) {

View file

@ -157,6 +157,8 @@ public class Application.PluginManager : GLib.Object {
private Client application;
private Controller controller;
private Configuration config;
private Peas.Engine plugins;
private bool is_shutdown = false;
private string trusted_path;
@ -172,19 +174,24 @@ public class Application.PluginManager : GLib.Object {
new Gee.HashMap<Peas.PluginInfo,EmailPluginContext>();
public PluginManager(Client application) throws GLib.Error {
internal PluginManager(Client application,
Controller controller,
Configuration config,
GLib.File trusted_plugin_path) throws GLib.Error {
this.application = application;
this.controller = controller;
this.config = config;
this.plugins = Peas.Engine.get_default();
this.folders_factory = new FolderStoreFactory(application);
this.email_factory = new EmailStoreFactory(application);
this.folders_factory = new FolderStoreFactory(controller);
this.email_factory = new EmailStoreFactory(controller);
this.trusted_path = application.get_app_plugins_dir().get_path();
this.plugins.add_search_path(trusted_path, null);
this.trusted_path = trusted_plugin_path.get_path();
this.plugins.add_search_path(this.trusted_path, null);
this.plugins.load_plugin.connect_after(on_load_plugin);
this.plugins.unload_plugin.connect(on_unload_plugin);
string[] optional_names = application.config.get_optional_plugins();
string[] optional_names = this.config.get_optional_plugins();
foreach (Peas.PluginInfo info in this.plugins.get_plugin_list()) {
string name = info.get_module_name();
try {
@ -234,11 +241,10 @@ public class Application.PluginManager : GLib.Object {
this.plugins.load_plugin(plugin);
loaded = true;
string name = plugin.get_module_name();
string[] optional_names =
this.application.config.get_optional_plugins();
string[] optional_names = this.config.get_optional_plugins();
if (!(name in optional_names)) {
optional_names += name;
this.application.config.set_optional_plugins(optional_names);
this.config.set_optional_plugins(optional_names);
}
}
return loaded;
@ -252,15 +258,14 @@ public class Application.PluginManager : GLib.Object {
this.plugins.unload_plugin(plugin);
unloaded = true;
string name = plugin.get_module_name();
string[] old_names =
this.application.config.get_optional_plugins();
string[] old_names = this.config.get_optional_plugins();
string[] new_names = new string[0];
for (int i = 0; i < old_names.length; i++) {
if (old_names[i] != name) {
new_names += old_names[i];
}
}
this.application.config.set_optional_plugins(new_names);
this.config.set_optional_plugins(new_names);
}
return unloaded;
}
@ -333,7 +338,7 @@ public class Application.PluginManager : GLib.Object {
var folder = plugin as Plugin.FolderExtension;
if (folder != null) {
folder.folders = new FolderPluginContext(
this.application,
this.controller.application,
this.folders_factory,
plugin_application.action_group_name
);