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