fix HasNonTrivialIdentities
[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, bool force_flat_file_store) {
53         string candidate;
54         IIdentityCardStore.StoreType saved_store_type = get_store_type();
55
56         if (force_flat_file_store)
57             set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
58
59         if (!display_name_is_valid (card.display_name, out candidate))
60         {
61           card.display_name = candidate;
62         }
63
64         store.add_card(card);
65         set_store_type(saved_store_type);
66         card_list_changed();
67      }
68
69      public void update_card(IdCard card) {
70         store.update_card(card);
71         card_list_changed();
72      }
73
74      public void remove_card(IdCard card) {
75         store.remove_card(card);
76         card_list_changed();
77      }
78
79      public void set_store_type(IIdentityCardStore.StoreType type) {
80          if ((store != null) && (store.get_store_type() == type))
81              return;
82          switch (type) {
83 #if GNOME_KEYRING
84              case IIdentityCardStore.StoreType.KEYRING:
85                  store = new KeyringStore();
86                  break;
87 #endif
88              case IIdentityCardStore.StoreType.FLAT_FILE:
89              default:
90                  store = new LocalFlatFileStore();
91                  break;
92          }
93      }
94
95      public IIdentityCardStore.StoreType get_store_type() {
96          return store.get_store_type();
97      }
98
99      public bool HasNonTrivialIdentities() {
100          foreach (IdCard card in this.store.get_card_list()) {
101              // The 'NoIdentity' card is non-trivial if it has services or rules.
102              // All other cards are automatically non-trivial.
103              if ((!card.IsNoIdentity()) || 
104                  (card.services.length > 0) ||
105                  (card.rules.length > 0)) {
106                  return true;
107              }
108          }
109          return false;
110      }
111
112
113     private IdentityManagerApp parent;
114
115     public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
116         parent = parent_app;
117         set_store_type(store_type);
118     }
119 }