Add null check
[moonshot-ui.git] / src / moonshot-identities-manager.vala
1 using Gee;
2
3 public class Password {
4 #if GNOME_KEYRING
5     private unowned string _password;
6     public string password {
7         get {
8             return _password;
9         }
10         set {
11             if (_password != null) {
12                 GnomeKeyring.memory_free((void *)_password);
13                 _password = null;
14             }
15             if (value != null)
16                 _password = GnomeKeyring.memory_strdup(value); 
17         }
18     }
19 #else
20     public string password { get; set; default = null; }
21 #endif
22
23     public Password(string in_password) {
24         password = in_password;
25     }
26
27     ~Password() {
28         password = null;
29     }
30 }
31
32 public class PasswordHashTable : Object {
33     private HashTable<string, Password> password_table;
34
35     private static string ComputeHashKey(IdCard card, IIdentityCardStore store) {
36         return "%s_store_%d".printf( card.display_name, store.get_store_type() );
37     }
38
39     public void CachePassword(IdCard card, IIdentityCardStore store) {
40         password_table.replace(ComputeHashKey(card, store), new Password(card.password));
41     }
42
43     public void RemovePassword(IdCard card, IIdentityCardStore store) {
44         password_table.remove(ComputeHashKey(card, store));
45     }
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;
50         }
51     }
52     public PasswordHashTable() {
53         password_table = new HashTable<string, Password>(GLib.str_hash, GLib.str_equal);
54     }
55 }
56
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()) {
67                 return -1;
68              } else if (id_b.IsNoIdentity() && !id_a.IsNoIdentity()) {
69                 return 1;
70              }
71              return strcmp(id_a.display_name, id_b.display_name);
72          });
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);
78              }
79          }
80          return identities;
81     }
82     public signal void card_list_changed();
83
84     /* This method finds a valid display name */
85     public bool display_name_is_valid (string name,
86                                        out string? candidate)
87     {
88         if (&candidate != null)
89           candidate = null;
90         foreach (IdCard id_card in this.get_card_list())
91         {
92           if (id_card.display_name == name)
93           {
94             if (&candidate != null)
95             {
96               for (int i=0; i<1000; i++)
97               {
98                 string tmp = "%s %d".printf (name, i);
99                 if (display_name_is_valid (tmp, null))
100                 {
101                   candidate = tmp;
102                   break;
103                 }
104               }
105             }
106             return false;
107           }
108         }
109         return true;
110     }
111
112     public void add_card(IdCard card, bool force_flat_file_store) {
113         if (card.temporary)
114             return;
115
116         string candidate;
117         IIdentityCardStore.StoreType saved_store_type = get_store_type();
118
119         if (force_flat_file_store)
120             set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
121
122         if (!display_name_is_valid (card.display_name, out candidate))
123         {
124           card.display_name = candidate;
125         }
126
127         if (!card.store_password)
128             password_table.CachePassword(card, store);
129         store.add_card(card);
130         set_store_type(saved_store_type);
131         card_list_changed();
132      }
133
134      public IdCard update_card(IdCard card) {
135         IdCard retval;
136         if (card.temporary) {
137             retval = card;
138             return retval;
139         }
140             
141         if (!card.store_password)
142             password_table.CachePassword(card, store);
143         else
144             password_table.RemovePassword(card, store);
145         retval = store.update_card(card);
146         card_list_changed();
147         return retval;
148      }
149
150      public void remove_card(IdCard card) {
151         password_table.RemovePassword(card, store);
152         store.remove_card(card);
153         card_list_changed();
154      }
155
156      public void set_store_type(IIdentityCardStore.StoreType type) {
157          if ((store != null) && (store.get_store_type() == type))
158              return;
159          switch (type) {
160 #if GNOME_KEYRING
161              case IIdentityCardStore.StoreType.KEYRING:
162                  store = new KeyringStore();
163                  break;
164 #endif
165              case IIdentityCardStore.StoreType.FLAT_FILE:
166              default:
167                  store = new LocalFlatFileStore();
168                  break;
169          }
170      }
171
172      public IIdentityCardStore.StoreType get_store_type() {
173          return store.get_store_type();
174      }
175
176      public bool HasNonTrivialIdentities() {
177          foreach (IdCard card in this.store.get_card_list()) {
178              // The 'NoIdentity' card is non-trivial if it has services or rules.
179              // All other cards are automatically non-trivial.
180              if ((!card.IsNoIdentity()) || 
181                  (card.services.length > 0) ||
182                  (card.rules.length > 0)) {
183                  return true;
184              }
185          }
186          return false;
187      }
188
189
190     private IdentityManagerApp parent;
191
192     public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
193         parent = parent_app;
194         password_table = new PasswordHashTable();
195         set_store_type(store_type);
196     }
197 }