From 76fb165db41005f8b520c1d4ffe90fb64bff7118 Mon Sep 17 00:00:00 2001 From: Kevin Wasserman Date: Fri, 13 Sep 2013 18:19:13 -0400 Subject: [PATCH] Support dynamic switching of card store type In headless mode, first try flat file store but switch to keyring, if available, if the flat file store has no cards (unless flat file store was explicitly specified on commandline). --- src/moonshot-idcard-store.vala | 6 ++++++ src/moonshot-identities-manager.vala | 37 +++++++++++++++++++++++++-------- src/moonshot-identity-manager-app.vala | 18 +++++++++++++++- src/moonshot-keyring-store.vala | 4 ++++ src/moonshot-local-flat-file-store.vala | 4 ++++ 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/moonshot-idcard-store.vala b/src/moonshot-idcard-store.vala index 8f2f8ec..4303713 100644 --- a/src/moonshot-idcard-store.vala +++ b/src/moonshot-idcard-store.vala @@ -1,9 +1,15 @@ using Gee; public interface IIdentityCardStore : Object { // Methods + public enum StoreType { + FLAT_FILE, + KEYRING + } + public abstract void add_card(IdCard card); public abstract void remove_card(IdCard card); public abstract void update_card(IdCard card); + public abstract StoreType get_store_type(); public abstract LinkedList get_card_list(); } diff --git a/src/moonshot-identities-manager.vala b/src/moonshot-identities-manager.vala index 64a87b9..c6def5f 100644 --- a/src/moonshot-identities-manager.vala +++ b/src/moonshot-identities-manager.vala @@ -71,17 +71,36 @@ public class IdentityManagerModel : Object { card_list_changed(); } + public void set_store_type(IIdentityCardStore.StoreType type) { + if ((store != null) && (store.get_store_type() == type)) + return; + switch (type) { +#if GNOME_KEYRING + case IIdentityCardStore.StoreType.KEYRING: + store = new KeyringStore(); + break; +#endif + case IIdentityCardStore.StoreType.FLAT_FILE: + default: + store = new LocalFlatFileStore(); + break; + } + } + + public IIdentityCardStore.StoreType get_store_type() { + return store.get_store_type(); + } + + public bool HasNonTrivialIdentities() { + var identities = store.get_card_list(); + return !identities.is_empty; + } + + private IdentityManagerApp parent; - public IdentityManagerModel(IdentityManagerApp parent_app, bool use_flat_file_store) { + public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) { parent = parent_app; -#if IPC_MSRPC - store = new LocalFlatFileStore(); -#else - if (use_flat_file_store) - store = new LocalFlatFileStore(); - else - store = new KeyringStore(); -#endif + set_store_type(store_type); } } diff --git a/src/moonshot-identity-manager-app.vala b/src/moonshot-identity-manager-app.vala index cb9c196..4237319 100644 --- a/src/moonshot-identity-manager-app.vala +++ b/src/moonshot-identity-manager-app.vala @@ -40,7 +40,23 @@ public class IdentityManagerApp { } public IdentityManagerApp (bool headless, bool use_flat_file_store) { - model = new IdentityManagerModel(this, headless || use_flat_file_store); +#if GNOME_KEYRING + bool keyring_available = GnomeKeyring.is_available(); +#else + bool keyring_available = false; +#endif + IIdentityCardStore.StoreType store_type; + if (headless || use_flat_file_store || !keyring_available) + store_type = IIdentityCardStore.StoreType.FLAT_FILE; + else + store_type = IIdentityCardStore.StoreType.KEYRING; + + model = new IdentityManagerModel(this, store_type); + /* if headless, but we have nothing in the flat file store + * and keyring is available, switch to keyring */ + if (headless && keyring_available && !use_flat_file_store && !model.HasNonTrivialIdentities()) + model.set_store_type(IIdentityCardStore.StoreType.KEYRING); + if (!headless) view = new IdentityManagerView(this); LinkedList card_list = model.get_card_list() ; diff --git a/src/moonshot-keyring-store.vala b/src/moonshot-keyring-store.vala index 96c036c..5b63173 100644 --- a/src/moonshot-keyring-store.vala +++ b/src/moonshot-keyring-store.vala @@ -23,6 +23,10 @@ public class KeyringStore : Object, IIdentityCardStore { store_id_cards (); } + public IIdentityCardStore.StoreType get_store_type() { + return IIdentityCardStore.StoreType.KEYRING; + } + public LinkedList get_card_list() { return id_card_list; } diff --git a/src/moonshot-local-flat-file-store.vala b/src/moonshot-local-flat-file-store.vala index 41f8273..1610efd 100644 --- a/src/moonshot-local-flat-file-store.vala +++ b/src/moonshot-local-flat-file-store.vala @@ -23,6 +23,10 @@ public class LocalFlatFileStore : Object, IIdentityCardStore { public LinkedList get_card_list() { return id_card_list; } + + public IIdentityCardStore.StoreType get_store_type() { + return IIdentityCardStore.StoreType.FLAT_FILE; + } private void load_id_cards() { id_card_list.clear(); -- 2.1.4