Add Id Card remotely
[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         var path = get_data_dir ();
12         var filename = Path.build_filename (path, FILE_NAME);
13
14         try
15         {
16             key_file.load_from_file (filename, KeyFileFlags.NONE);
17         }
18         catch (Error e)
19         {
20             stdout.printf("Error: %s\n", e.message);
21         }
22
23         var identities_uris = key_file.get_groups ();
24         foreach (string identity in identities_uris)
25         {
26             try
27             {
28                 IdCard id_card = new IdCard ();
29
30                 id_card.issuer = key_file.get_string (identity, "Issuer");
31                 id_card.username = key_file.get_string (identity, "Username");
32                 id_card.password = key_file.get_string (identity, "Password");
33                 id_card.services = key_file.get_string_list (identity, "Services");
34                 id_card.nai = id_card.username + "@" + id_card.issuer;
35
36                 id_card_list.prepend (id_card);
37             }
38             catch (Error e)
39             {
40                 stdout.printf ("Error:  %s\n", e.message);
41             }
42         }
43     }
44
45     public void store_id_cards ()
46     {
47         var key_file = new KeyFile ();
48
49         foreach (IdCard id_card in this.id_card_list)
50         {
51             key_file.set_string (id_card.issuer, "Issuer", id_card.issuer);
52             key_file.set_string (id_card.issuer, "Username", id_card.username);
53             key_file.set_string (id_card.issuer, "Password", id_card.password);
54             key_file.set_string_list (id_card.issuer, "Services", id_card.services);
55         }
56
57         var text = key_file.to_data (null);
58
59         try
60         {
61             var path = get_data_dir ();
62             var filename = Path.build_filename (path, FILE_NAME);
63             FileUtils.set_contents (filename, text, -1);
64         }
65         catch (Error e)
66         {
67             stdout.printf ("Error:  %s\n", e.message);
68         }
69     }
70
71     private string get_data_dir()
72     {
73         string path;
74
75         path = Path.build_filename (Environment.get_user_data_dir (),
76                                     Config.PACKAGE_TARNAME);
77         if (!FileUtils.test (path, FileTest.EXISTS))
78         {
79             DirUtils.create (path, 0700);
80         }
81
82         return path;
83     }
84
85     public IdCard? load_gss_eap_id_file ()
86     {
87         IdCard id_card = new IdCard();
88         string text;
89         string id_card_data[2];
90
91         var filename = Path.build_filename (Environment.get_home_dir (),
92                                             ".gss_eap_id");
93         try {
94             FileUtils.get_contents (filename, out text, null);
95         }
96         catch (Error e)
97         {
98             return null;
99         }
100
101         if (text == "")
102             return null;
103
104         id_card_data = text.split ("\n", 2);
105         if (id_card_data[1] != "")
106             id_card.password = id_card_data[1];
107         id_card_data = id_card_data[0].split ("@", 2);
108         id_card.username = id_card_data[0];
109         id_card.issuer = id_card_data[1];
110         id_card.services = {};
111         id_card.nai = id_card.username + "@" + id_card.issuer;
112         id_card.pixbuf = find_icon ("avatar-default", 48);
113
114         return id_card;
115     }
116
117     public void store_gss_eap_id_file (IdCard ?id_card)
118     {
119         string text = "";
120
121         if (id_card != null)
122             text = id_card.username + "@" + id_card.issuer + "\n" + id_card.password;
123
124         var filename = Path.build_filename (Environment.get_home_dir (),
125                                             ".gss_eap_id");
126         try
127         {
128             FileUtils.set_contents (filename, text, -1);
129         }
130         catch (Error e)
131         {
132             stdout.printf ("Error:  %s\n", e.message);
133         }
134     }
135 }