Application.FolderStoreFactory: Move get_folder_from_variant to factory

Make the from variant method accessible to the client as per email
factory, so the client can reuse it.
This commit is contained in:
Michael Gratton 2020-04-18 18:54:22 +10:00 committed by Michael James Gratton
parent aff599821a
commit 0b0efd026a

View file

@ -24,15 +24,15 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
"(sv)" "(sv)"
); );
private Gee.Map<Geary.Folder,FolderImpl> folders; private weak FolderStoreFactory factory;
public FolderStoreImpl(Gee.Map<Geary.Folder,FolderImpl> folders) { public FolderStoreImpl(FolderStoreFactory factory) {
this.folders = folders; this.factory = factory;
} }
public Gee.Collection<Plugin.Folder> get_folders() { public Gee.Collection<Plugin.Folder> get_folders() {
return this.folders.values.read_only_view; return this.factory.folders.values.read_only_view;
} }
public async Gee.Collection<Plugin.Folder> list_containing_folders( public async Gee.Collection<Plugin.Folder> list_containing_folders(
@ -51,7 +51,7 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
if (multi_folders != null) { if (multi_folders != null) {
foreach (var path in multi_folders.get(id.backing)) { foreach (var path in multi_folders.get(id.backing)) {
var folder = context.account.get_folder(path); var folder = context.account.get_folder(path);
folders.add(this.folders.get(folder)); folders.add(this.factory.folders.get(folder));
} }
} }
} }
@ -59,19 +59,12 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
} }
public Plugin.Folder? get_folder_from_variant(GLib.Variant variant) { public Plugin.Folder? get_folder_from_variant(GLib.Variant variant) {
Plugin.Folder? found = null; var folder = this.factory.get_folder_from_variant(variant);
// XXX this is pretty inefficient return this.factory.folders.get(folder);
foreach (var folder in this.folders.values) {
if (folder.to_variant().equal(variant)) {
found = folder;
break;
}
}
return found;
} }
internal void destroy() { internal void destroy() {
this.folders = Gee.Map.empty(); // no-op
} }
} }
@ -160,7 +153,7 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
/** Constructs a new folder store for use by plugin contexts. */ /** Constructs a new folder store for use by plugin contexts. */
public Plugin.FolderStore new_folder_store() { public Plugin.FolderStore new_folder_store() {
var store = new FolderStoreImpl(this.folders); var store = new FolderStoreImpl(this);
this.stores.add(store); this.stores.add(store);
return store; return store;
} }
@ -191,6 +184,30 @@ internal class Application.FolderStoreFactory : Geary.BaseObject {
return (impl != null) ? impl.backing : null; return (impl != null) ? impl.backing : null;
} }
/** Returns the folder context for the given plugin folder id. */
public Geary.Folder? get_folder_from_variant(GLib.Variant target) {
string id = (string) target.get_child_value(0);
AccountContext? context = null;
foreach (var key in this.accounts.keys) {
if (key.account.information.id == id) {
context = key;
break;
}
}
Geary.Folder? folder = null;
if (context != null) {
try {
Geary.FolderPath? path = context.account.to_folder_path(
target.get_child_value(1).get_variant()
);
folder = context.account.get_folder(path);
} catch (GLib.Error err) {
debug("Could not find account/folder %s", err.message);
}
}
return folder;
}
internal void add_account(AccountContext added) { internal void add_account(AccountContext added) {
added.folders_available.connect(on_folders_available); added.folders_available.connect(on_folders_available);
added.folders_unavailable.connect(on_folders_unavailable); added.folders_unavailable.connect(on_folders_unavailable);