3 public class Password {
5 private unowned string _password;
6 public string password {
11 if (_password != null) {
12 GnomeKeyring.memory_free((void *)_password);
16 _password = GnomeKeyring.memory_strdup(value);
20 public string password { get; set; default = null; }
23 public Password(string in_password) {
24 password = in_password;
32 public class PasswordHashTable : Object {
33 private HashTable<string, Password> password_table;
35 private static string ComputeHashKey(IdCard card, IIdentityCardStore store) {
36 return "%s_store_%d".printf( card.display_name, store.get_store_type() );
39 public void CachePassword(IdCard card, IIdentityCardStore store) {
40 password_table.replace(ComputeHashKey(card, store), new Password(card.password));
43 public void RemovePassword(IdCard card, IIdentityCardStore store) {
44 password_table.remove(ComputeHashKey(card, store));
46 public void RetrievePassword(IdCard card, IIdentityCardStore store) {
47 weak Password password = password_table.lookup(ComputeHashKey(card, store));
48 if (password != null) {
49 card.password = password.password;
52 public PasswordHashTable() {
53 password_table = new HashTable<string, Password>(GLib.str_hash, GLib.str_equal);
57 public class IdentityManagerModel : Object {
58 private const string FILE_NAME = "identities.txt";
59 private PasswordHashTable password_table;
60 private IIdentityCardStore store;
61 public LinkedList<IdCard> get_card_list() {
62 var identities = store.get_card_list();
63 identities.sort( (a, b) => {
64 IdCard id_a = (IdCard )a;
65 IdCard id_b = (IdCard )b;
66 if (id_a.IsNoIdentity() && !id_b.IsNoIdentity()) {
68 } else if (id_b.IsNoIdentity() && !id_a.IsNoIdentity()) {
71 return strcmp(id_a.display_name, id_b.display_name);
73 if (identities.is_empty || !identities[0].IsNoIdentity())
74 identities.insert(0, IdCard.NewNoIdentity());
75 foreach (IdCard id_card in identities) {
76 if (!id_card.store_password) {
77 password_table.RetrievePassword(id_card, store);
82 public signal void card_list_changed();
84 /* This method finds a valid display name */
85 public bool display_name_is_valid (string name,
86 out string? candidate)
89 foreach (IdCard id_card in this.get_card_list())
91 if (id_card.display_name == name)
93 if (&candidate != null)
95 for (int i=0; i<1000; i++)
97 string tmp = "%s %d".printf (name, i);
98 if (display_name_is_valid (tmp, null))
111 public void add_card(IdCard card, bool force_flat_file_store) {
116 IIdentityCardStore.StoreType saved_store_type = get_store_type();
118 if (force_flat_file_store)
119 set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
121 if (!display_name_is_valid (card.display_name, out candidate))
123 card.display_name = candidate;
126 if (!card.store_password)
127 password_table.CachePassword(card, store);
128 store.add_card(card);
129 set_store_type(saved_store_type);
133 public IdCard update_card(IdCard card) {
135 if (card.temporary) {
140 if (!card.store_password)
141 password_table.CachePassword(card, store);
143 password_table.RemovePassword(card, store);
144 retval = store.update_card(card);
149 public void remove_card(IdCard card) {
150 password_table.RemovePassword(card, store);
151 store.remove_card(card);
155 public void set_store_type(IIdentityCardStore.StoreType type) {
156 if ((store != null) && (store.get_store_type() == type))
160 case IIdentityCardStore.StoreType.KEYRING:
161 store = new KeyringStore();
164 case IIdentityCardStore.StoreType.FLAT_FILE:
166 store = new LocalFlatFileStore();
171 public IIdentityCardStore.StoreType get_store_type() {
172 return store.get_store_type();
175 public bool HasNonTrivialIdentities() {
176 foreach (IdCard card in this.store.get_card_list()) {
177 // The 'NoIdentity' card is non-trivial if it has services or rules.
178 // All other cards are automatically non-trivial.
179 if ((!card.IsNoIdentity()) ||
180 (card.services.length > 0) ||
181 (card.rules.length > 0)) {
189 private IdentityManagerApp parent;
191 public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
193 password_table = new PasswordHashTable();
194 set_store_type(store_type);