0c8dfb46a83460e0a7b693020ac94e6d414602d9
[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         candidate = null;
89         foreach (IdCard id_card in this.get_card_list())
90         {
91           if (id_card.display_name == name)
92           {
93             if (&candidate != null)
94             {
95               for (int i=0; i<1000; i++)
96               {
97                 string tmp = "%s %d".printf (name, i);
98                 if (display_name_is_valid (tmp, null))
99                 {
100                   candidate = tmp;
101                   break;
102                 }
103               }
104             }
105             return false;
106           }
107         }
108         return true;
109     }
110
111     public void add_card(IdCard card, bool force_flat_file_store) {
112         string candidate;
113         IIdentityCardStore.StoreType saved_store_type = get_store_type();
114
115         if (force_flat_file_store)
116             set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
117
118         if (!display_name_is_valid (card.display_name, out candidate))
119         {
120           card.display_name = candidate;
121         }
122
123         if (!card.store_password)
124             password_table.CachePassword(card, store);
125         store.add_card(card);
126         set_store_type(saved_store_type);
127         card_list_changed();
128      }
129
130      public IdCard update_card(IdCard card) {
131         IdCard retval;
132         if (!card.store_password)
133             password_table.CachePassword(card, store);
134         else
135             password_table.RemovePassword(card, store);
136         retval = store.update_card(card);
137         card_list_changed();
138         return retval;
139      }
140
141      public void remove_card(IdCard card) {
142         password_table.RemovePassword(card, store);
143         store.remove_card(card);
144         card_list_changed();
145      }
146
147      public void set_store_type(IIdentityCardStore.StoreType type) {
148          if ((store != null) && (store.get_store_type() == type))
149              return;
150          switch (type) {
151 #if GNOME_KEYRING
152              case IIdentityCardStore.StoreType.KEYRING:
153                  store = new KeyringStore();
154                  break;
155 #endif
156              case IIdentityCardStore.StoreType.FLAT_FILE:
157              default:
158                  store = new LocalFlatFileStore();
159                  break;
160          }
161      }
162
163      public IIdentityCardStore.StoreType get_store_type() {
164          return store.get_store_type();
165      }
166
167      public bool HasNonTrivialIdentities() {
168          foreach (IdCard card in this.store.get_card_list()) {
169              // The 'NoIdentity' card is non-trivial if it has services or rules.
170              // All other cards are automatically non-trivial.
171              if ((!card.IsNoIdentity()) || 
172                  (card.services.length > 0) ||
173                  (card.rules.length > 0)) {
174                  return true;
175              }
176          }
177          return false;
178      }
179
180
181     private IdentityManagerApp parent;
182
183     public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
184         parent = parent_app;
185         password_table = new PasswordHashTable();
186         set_store_type(store_type);
187     }
188 }