Save the Id Card data in the file every time we create a new Id Card
[moonshot-ui.git] / src / moonshot-identities-manager.vala
1 class IdentitiesManager : Object {
2
3     public SList<IdCard> id_card_list;
4
5     private const string FILE_NAME = "identities.txt";
6
7     public IdentitiesManager ()
8     {
9         var key_file = new KeyFile ();
10
11         try
12         {
13             key_file.load_from_file (FILE_NAME, KeyFileFlags.NONE);
14         }
15         catch (Error e)
16         {
17             stdout.printf("Error: %s\n", e.message);
18         }
19
20         var identities_uris = key_file.get_groups ();
21         foreach (string identity in identities_uris)
22         {
23             try
24             {
25                 IdCard id_card = new IdCard ();
26
27                 id_card.issuer = key_file.get_string (identity, "Issuer");
28                 id_card.username = key_file.get_string (identity, "Username");
29                 id_card.password = key_file.get_string (identity, "Password");
30                 id_card.services = key_file.get_string_list (identity, "Services");
31
32                 id_card_list.prepend (id_card);
33             }
34             catch (Error e)
35             {
36                 stdout.printf ("Error:  %s\n", e.message);
37             }
38         }
39     }
40
41     public void store_id_cards ()
42     {
43         var key_file = new KeyFile ();
44
45         foreach (IdCard id_card in this.id_card_list)
46         {
47             key_file.set_string (id_card.issuer, "Issuer", id_card.issuer);
48             key_file.set_string (id_card.issuer, "Username", id_card.username);
49             key_file.set_string (id_card.issuer, "Password", id_card.password);
50             key_file.set_string_list (id_card.issuer, "Services", id_card.services);
51         }
52
53         var text = key_file.to_data (null);
54
55         try
56         {
57             FileUtils.set_contents (FILE_NAME, text, -1);
58         }
59         catch (Error e)
60         {
61             stdout.printf ("Error:  %s\n", e.message);
62         }
63     }
64
65 }