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) {
113 IIdentityCardStore.StoreType saved_store_type = get_store_type();
115 if (force_flat_file_store)
116 set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
118 if (!display_name_is_valid (card.display_name, out candidate))
120 card.display_name = candidate;
123 if (!card.store_password)
124 password_table.CachePassword(card, store);
125 store.add_card(card);
126 set_store_type(saved_store_type);
130 public void update_card(IdCard card) {
131 if (!card.store_password)
132 password_table.CachePassword(card, store);
134 password_table.RemovePassword(card, store);
135 store.update_card(card);
139 public void remove_card(IdCard card) {
140 password_table.RemovePassword(card, store);
141 store.remove_card(card);
145 public void set_store_type(IIdentityCardStore.StoreType type) {
146 if ((store != null) && (store.get_store_type() == type))
150 case IIdentityCardStore.StoreType.KEYRING:
151 store = new KeyringStore();
154 case IIdentityCardStore.StoreType.FLAT_FILE:
156 store = new LocalFlatFileStore();
161 public IIdentityCardStore.StoreType get_store_type() {
162 return store.get_store_type();
165 public bool HasNonTrivialIdentities() {
166 foreach (IdCard card in this.store.get_card_list()) {
167 // The 'NoIdentity' card is non-trivial if it has services or rules.
168 // All other cards are automatically non-trivial.
169 if ((!card.IsNoIdentity()) ||
170 (card.services.length > 0) ||
171 (card.rules.length > 0)) {
179 private IdentityManagerApp parent;
181 public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
183 password_table = new PasswordHashTable();
184 set_store_type(store_type);