Merge branch 'master' of ssh://69.25.196.28:822/srv/git/moonshot-ui
[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         id_card_list = new SList<IdCard>();
10         var key_file = new KeyFile ();
11
12         var path = get_data_dir ();
13         var filename = Path.build_filename (path, FILE_NAME);
14
15         try
16         {
17             key_file.load_from_file (filename, KeyFileFlags.NONE);
18         }
19         catch (Error e)
20         {
21             stdout.printf("Error: %s\n", e.message);
22             return;
23         }
24
25         var identities_uris = key_file.get_groups ();
26         foreach (string identity in identities_uris)
27         {
28             try
29             {
30                 IdCard id_card = new IdCard ();
31
32                 id_card.issuer = key_file.get_string (identity, "Issuer");
33                 id_card.username = key_file.get_string (identity, "Username");
34                 id_card.password = key_file.get_string (identity, "Password");
35                 id_card.services = key_file.get_string_list (identity, "Services");
36                 id_card.display_name = key_file.get_string (identity, "DisplayName");
37                 id_card.pixbuf = find_icon ("avatar-default", 48);
38
39                 id_card_list.prepend (id_card);
40             }
41             catch (Error e)
42             {
43                 stdout.printf ("Error:  %s\n", e.message);
44             }
45         }
46     }
47
48     public void store_id_cards ()
49     {
50         var key_file = new KeyFile ();
51
52         foreach (IdCard id_card in this.id_card_list)
53         {
54             /*string[] rules_patterns = new string[id_card.rules.length];
55             string[] rules_always_conf = new string[id_card.rules.length];
56             
57             for (int i=0; i<id_card.rules.length; i++)
58             {
59               rules_patterns[i] = id_card.rules[i].pattern;
60               rules_always_conf[i] = id_card.rules[i].always_confirm;
61             }
62             */
63             key_file.set_string (id_card.display_name, "Issuer", id_card.issuer ?? "");
64             key_file.set_string (id_card.display_name, "DisplayName", id_card.display_name ?? "");
65             key_file.set_string (id_card.display_name, "Username", id_card.username ?? "");
66             key_file.set_string (id_card.display_name, "Password", id_card.password ?? "");
67             key_file.set_string_list (id_card.display_name, "Services", id_card.services ?? {});
68             /*
69             if (id_card.rules.length > 0)
70             {
71               key_file.set_string_list (id_card.display_name, "Rules-Patterns", rules_patterns);
72               key_file.set_string_list (id_card.display_name, "Rules-AlwaysConfirm", rules_always_conf);
73             }
74             // Trust anchor 
75             key_file.set_string (id_card.display_name, "CA-Cert", id_card.trust_anchor.ca_cert ?? "");
76             key_file.set_string (id_card.display_name, "Subject", id_card.trust_anchor.subject ?? "");
77             key_file.set_string (id_card.display_name, "SubjectAlt", id_card.trust_anchor.subject_alt ?? "");
78             key_file.set_string (id_card.display_name, "ServerCert", id_card.trust_anchor.server_cert ?? "");
79             */
80         }
81
82         var text = key_file.to_data (null);
83
84         try
85         {
86             var path = get_data_dir ();
87             var filename = Path.build_filename (path, FILE_NAME);
88             FileUtils.set_contents (filename, text, -1);
89         }
90         catch (Error e)
91         {
92             stdout.printf ("Error:  %s\n", e.message);
93         }
94     }
95
96     private string get_data_dir()
97     {
98         string path;
99
100         path = Path.build_filename (Environment.get_user_data_dir (),
101                                     Config.PACKAGE_TARNAME);
102         if (!FileUtils.test (path, FileTest.EXISTS))
103         {
104             DirUtils.create (path, 0700);
105         }
106
107         return path;
108     }
109 }