X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fmoonshot-identities-manager.vala;h=a1cac6beb819321aeb7ec0bde3eca8f65c32a128;hb=3483be3d73af11f2bf7ce3318514c4fce50bebf4;hp=a0506d819bc0284b5eb67948029714be76c589ec;hpb=5b68b364f2c62fa43111b5a53c7040d34205ba20;p=moonshot-ui.git diff --git a/src/moonshot-identities-manager.vala b/src/moonshot-identities-manager.vala index a0506d8..a1cac6b 100644 --- a/src/moonshot-identities-manager.vala +++ b/src/moonshot-identities-manager.vala @@ -86,56 +86,58 @@ public class PasswordHashTable : Object { } public class IdentityManagerModel : Object { + static MoonshotLogger logger = get_logger("IdentityManagerModel"); + private const string FILE_NAME = "identities.txt"; private PasswordHashTable password_table; private IIdentityCardStore store; public LinkedList get_card_list() { - var identities = store.get_card_list(); - identities.sort( (a, b) => { - IdCard id_a = (IdCard )a; - IdCard id_b = (IdCard )b; - if (id_a.IsNoIdentity() && !id_b.IsNoIdentity()) { - return -1; - } else if (id_b.IsNoIdentity() && !id_a.IsNoIdentity()) { - return 1; - } - return strcmp(id_a.display_name, id_b.display_name); - }); - if (identities.is_empty || !identities[0].IsNoIdentity()) - identities.insert(0, IdCard.NewNoIdentity()); - foreach (IdCard id_card in identities) { - if (!id_card.store_password) { - password_table.RetrievePassword(id_card, store); - } - } - return identities; + var identities = store.get_card_list(); + identities.sort((a, b) => { + IdCard id_a = (IdCard )a; + IdCard id_b = (IdCard )b; + if (id_a.is_no_identity() && !id_b.is_no_identity()) { + return -1; + } else if (id_b.is_no_identity() && !id_a.is_no_identity()) { + return 1; + } + return strcmp(id_a.display_name, id_b.display_name); + }); + if (identities.is_empty || !identities[0].is_no_identity()) + identities.insert(0, IdCard.NewNoIdentity()); + foreach (IdCard id_card in identities) { + if (!id_card.store_password) { + password_table.RetrievePassword(id_card, store); + } + } + return identities; } public signal void card_list_changed(); /* This method finds a valid display name */ - public bool display_name_is_valid (string name, - out string? candidate) + public bool display_name_is_valid(string name, + out string? candidate) { if (&candidate != null) - candidate = null; - foreach (IdCard id_card in this.get_card_list()) + candidate = null; + foreach (IdCard id_card in this.store.get_card_list()) { - if (id_card.display_name == name) - { - if (&candidate != null) + if (id_card.display_name == name) { - for (int i=0; i<1000; i++) - { - string tmp = "%s %d".printf (name, i); - if (display_name_is_valid (tmp, null)) + if (&candidate != null) { - candidate = tmp; - break; + for (int i = 0; i < 1000; i++) + { + string tmp = "%s %d".printf(name, i); + if (display_name_is_valid(tmp, null)) + { + candidate = tmp; + break; + } + } } - } + return false; } - return false; - } } return true; } @@ -145,16 +147,17 @@ public class IdentityManagerModel : Object { bool duplicate_found = false; bool found = false; do { - var cards = get_card_list(); - found = false; - foreach (IdCard id_card in cards) { - if ((card != id_card) && (id_card.nai == card.nai)) { - stdout.printf("removing duplicate id for '%s'\n", card.nai); - remove_card_internal(id_card); - found = duplicate_found = true; - break; - } - } + var cards = this.store.get_card_list(); + found = false; + foreach (IdCard id_card in cards) { + if ((card != id_card) && (id_card.nai == card.nai)) { + stdout.printf("removing duplicate id for '%s'\n", card.nai); + logger.trace("removing duplicate id for '%s'\n".printf(card.nai)); + remove_card_internal(id_card); + found = duplicate_found = true; + break; + } + } } while (found); return duplicate_found; } @@ -179,8 +182,10 @@ public class IdentityManagerModel : Object { } public void add_card(IdCard card, bool force_flat_file_store) { - if (card.temporary) + if (card.temporary) { + logger.trace("add_card: card is temporary; returning."); return; + } string candidate; IIdentityCardStore.StoreType saved_store_type = get_store_type(); @@ -190,19 +195,25 @@ public class IdentityManagerModel : Object { remove_duplicates(card); - if (!display_name_is_valid (card.display_name, out candidate)) + if (!display_name_is_valid(card.display_name, out candidate)) { - card.display_name = candidate; + card.display_name = candidate; } if (!card.store_password) password_table.CachePassword(card, store); + + logger.trace("add_card: Storing card '%s' with services: '%s'" + .printf(card.display_name, card.get_services_string("; "))); + store.add_card(card); set_store_type(saved_store_type); card_list_changed(); - } + } + + public IdCard update_card(IdCard card) { + logger.trace("update_card"); - public IdCard update_card(IdCard card) { IdCard retval; if (card.temporary) { retval = card; @@ -216,60 +227,78 @@ public class IdentityManagerModel : Object { retval = store.update_card(card); card_list_changed(); return retval; - } + } - private bool remove_card_internal(IdCard card) { - if (card.temporary) - return false; - password_table.RemovePassword(card, store); - return store.remove_card(card); - } + private bool remove_card_internal(IdCard card) { + if (card.temporary) + return false; + password_table.RemovePassword(card, store); + return store.remove_card(card); + } - public bool remove_card(IdCard card) { - if (remove_card_internal(card)) { + public bool remove_card(IdCard card) { + if (remove_card_internal(card)) { card_list_changed(); return true; - } - return false; - } - - 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() { - foreach (IdCard card in this.store.get_card_list()) { - // The 'NoIdentity' card is non-trivial if it has services or rules. - // All other cards are automatically non-trivial. - if ((!card.IsNoIdentity()) || - (card.services.length > 0) || - (card.rules.length > 0)) { - return true; - } - } - return false; - } + } + return false; + } + + // The name is misleading: This not only sets the store type, + // it also creates a new store instance, which loads the card data. + 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; + } + + // Loop through the loaded IDs. If any trust anchors are old enough that we didn't record + // the datetime_added, add it now. + string before_now = _("Before ") + TrustAnchor.format_datetime_now(); + bool save_needed = false; + foreach (IdCard id in this.store.get_card_list()) { + if (!id.trust_anchor.is_empty() && id.trust_anchor.datetime_added == "") { + logger.trace("set_store_type : Set ta_datetime_added for old trust anchor on '%s' to '%s'".printf(id.display_name, before_now)); + id.trust_anchor.set_datetime_added(before_now); + save_needed = true; + } + } + if (save_needed) { + this.store.store_id_cards(); + } + } + + public IIdentityCardStore.StoreType get_store_type() { + return store.get_store_type(); + } + + public bool HasNonTrivialIdentities() { + foreach (IdCard card in this.store.get_card_list()) { + // The 'NoIdentity' card is non-trivial if it has services or rules. + // All other cards are automatically non-trivial. + if ((!card.is_no_identity()) || + (card.services.size > 0) || + (card.rules.length > 0)) { + return true; + } + } + return false; + } private IdentityManagerApp parent; public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) { + logger.trace("IdentityManagerModel: store_type=" + store_type.to_string()); parent = parent_app; password_table = new PasswordHashTable(); set_store_type(store_type);