Change the format of the .gss_eap_id
[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         id_card.password = id_card_data[1];
105         id_card_data = id_card_data[0].split ("@", 2);
106         id_card.username = id_card_data[0];
107         id_card.issuer = id_card_data[1];
108         id_card.services = {"email","jabber","irc"};
109
110         var icon_theme = Gtk.IconTheme.get_default ();
111         try
112         {
113             id_card.pixbuf = icon_theme.load_icon ("avatar-default",
114                                                    48,
115                                                    Gtk.IconLookupFlags.FORCE_SIZE);
116         }
117         catch (Error e)
118         {
119             id_card.pixbuf = null;
120             stdout.printf("Error: %s\n", e.message);
121         }
122
123         return id_card;
124     }
125
126     public void store_gss_eap_id_file (IdCard ?id_card)
127     {
128         string text = "";
129
130         if (id_card != null)
131             text = id_card.username + "@" + id_card.issuer + "\n" + id_card.password;
132
133         var filename = Path.build_filename (Environment.get_home_dir (),
134                                             ".gss_eap_id");
135         try
136         {
137             FileUtils.set_contents (filename, text, -1);
138         }
139         catch (Error e)
140         {
141             stdout.printf ("Error:  %s\n", e.message);
142         }
143     }
144 }