Implement gnome keyring data store.
[moonshot-ui.git] / src / moonshot-identities-manager.vala
1 using Gee;
2
3 public class IdentityManagerModel : Object {
4     private const string FILE_NAME = "identities.txt";
5
6     private IIdentityCardStore store;
7     public LinkedList<IdCard>  get_card_list() {
8          var identities = store.get_card_list();
9          identities.sort( (a, b) => {
10              IdCard id_a = (IdCard )a;
11              IdCard id_b = (IdCard )b;
12              if (id_a.IsNoIdentity() && !id_b.IsNoIdentity()) {
13                 return -1;
14              } else if (id_b.IsNoIdentity() && !id_a.IsNoIdentity()) {
15                 return 1;
16              }
17              return strcmp(id_a.display_name, id_b.display_name);
18          });
19          if (identities.is_empty || !identities[0].IsNoIdentity())
20              identities.insert(0, IdCard.NewNoIdentity());
21          return identities;
22     }
23     public signal void card_list_changed();
24
25     /* This method finds a valid display name */
26     public bool display_name_is_valid (string name,
27                                        out string? candidate)
28     {
29         candidate = null;
30         foreach (IdCard id_card in this.get_card_list())
31         {
32           if (id_card.display_name == name)
33           {
34             if (&candidate != null)
35             {
36               for (int i=0; i<1000; i++)
37               {
38                 string tmp = "%s %d".printf (name, i);
39                 if (display_name_is_valid (tmp, null))
40                 {
41                   candidate = tmp;
42                   break;
43                 }
44               }
45             }
46             return false;
47           }
48         }
49         return true;
50     }
51
52     public void add_card(IdCard card) {
53         string candidate;
54
55         if (!display_name_is_valid (card.display_name, out candidate))
56         {
57           card.display_name = candidate;
58         }
59
60         store.add_card(card);
61         card_list_changed();
62      }
63
64      public void update_card(IdCard card) {
65         store.update_card(card);
66         card_list_changed();
67      }
68
69      public void remove_card(IdCard card) {
70         store.remove_card(card);
71         card_list_changed();
72      }
73
74     private IdentityManagerApp parent;
75
76     public IdentityManagerModel(IdentityManagerApp parent_app, bool use_flat_file_store) {
77         parent = parent_app;
78 #if IPC_MSRPC
79         store = new LocalFlatFileStore();
80 #else
81         if (use_flat_file_store)
82             store = new LocalFlatFileStore();
83         else
84             store = new KeyringStore();
85 #endif
86     }
87 }