Save the Id Card data in the file every time we remove a Id Card
[moonshot-ui.git] / src / moonshot-window.vala
1 using Gtk;
2
3 class MainWindow : Window
4 {
5     private const int WINDOW_WIDTH = 400;
6     private const int WINDOW_HEIGHT = 500;
7
8     private Entry search_entry;
9     private VBox vbox_rigth;
10     private CustomVBox custom_vbox;
11
12     private Entry username_entry;
13     private Entry password_entry;
14
15     private ListStore listmodel;
16
17     private IdentitiesManager identities_manager;
18
19     private enum Columns
20     {
21         IDCARD_COL,
22         LOGO_COL,
23         ISSUER_COL,
24         USERNAME_COL,
25         PASSWORD_COL,
26         N_COLUMNS
27     }
28
29     public MainWindow()
30     {
31         this.title = "Moonshoot";
32         this.position = WindowPosition.CENTER;
33         set_default_size (WINDOW_WIDTH, WINDOW_HEIGHT);
34
35         build_ui();
36         setup_identities_list();
37         load_id_cards();
38         connect_signals();
39     }
40
41     private void search_entry_icon_press_cb (EntryIconPosition pos, Gdk.Event event)
42     {
43         if (pos == EntryIconPosition.PRIMARY)
44         {
45             print ("Search entry icon pressed\n");
46         }
47         else
48         {
49             this.search_entry.set_text ("");
50         }
51     }
52
53     private void search_entry_text_changed_cb ()
54     {
55         var has_text = this.search_entry.get_text_length () > 0;
56         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, has_text);
57         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, has_text);
58
59         this.custom_vbox.new_text_in_search_entry (search_entry.get_text ());
60
61         this.vbox_rigth.set_visible (false);
62         this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
63     }
64
65     private bool search_entry_key_press_event_cb (Gdk.EventKey e)
66     {
67         if(Gdk.keyval_name(e.keyval) == "Escape")
68            this.search_entry.set_text("");
69
70         // Continue processing this event, since the
71         // text entry functionality needs to see it too.
72         return false;
73     }
74
75     private void setup_identities_list ()
76     {
77         this.listmodel = new ListStore (Columns.N_COLUMNS, typeof (IdCard),
78                                                            typeof (Gdk.Pixbuf),
79                                                            typeof (string),
80                                                            typeof (string),
81                                                            typeof (string));
82     }
83
84     private void load_id_cards ()
85     {
86         this.identities_manager = new IdentitiesManager ();
87
88         foreach (IdCard id_card in identities_manager.id_card_list)
89         {
90             add_id_card_widget (id_card);
91         }
92     }
93
94     private void fill_details (IdCardWidget id_card_widget)
95     {
96        var id_card = id_card_widget.id_card;
97        this.username_entry.set_text (id_card.username);
98        this.password_entry.set_text (id_card.password);
99     }
100
101     private void show_details (IdCard id_card)
102     {
103        this.vbox_rigth.set_visible (!vbox_rigth.get_visible ());
104
105        if (this.vbox_rigth.get_visible () == false)
106        {
107            this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
108        }
109     }
110
111     private void details_identity_cb (IdCardWidget id_card_widget)
112     {
113        fill_details (id_card_widget);
114        show_details (id_card_widget.id_card);
115     }
116
117     private IdCard get_id_card_data (AddIdentityDialog dialog)
118     {
119         var id_card = new IdCard ();
120
121         id_card.issuer = dialog.issuer;
122         id_card.username = dialog.username;
123         id_card.password = dialog.password;
124
125         var icon_theme = IconTheme.get_default ();
126         try
127         {
128             id_card.pixbuf = icon_theme.load_icon ("avatar-default",
129                                                    48,
130                                                    IconLookupFlags.FORCE_SIZE);
131         }
132         catch (Error e)
133         {
134             id_card.pixbuf = null;
135             stdout.printf("Error: %s\n", e.message);
136         }
137
138         id_card.services = {"email","jabber","irc"};
139
140         return id_card;
141     }
142
143     private void add_id_card_widget (IdCard id_card)
144     {
145         var id_card_widget = new IdCardWidget (id_card);
146
147         this.custom_vbox.add_id_card_widget (id_card_widget);
148
149         id_card_widget.details_id.connect (details_identity_cb);
150         id_card_widget.remove_id.connect (remove_identity_cb);
151         id_card_widget.expanded.connect (this.custom_vbox.receive_expanded_event);
152         id_card_widget.expanded.connect (fill_details);
153     }
154
155     private void add_identity (AddIdentityDialog dialog)
156     {
157         var id_card = get_id_card_data (dialog);
158
159         this.identities_manager.id_card_list.prepend (id_card);
160         this.identities_manager.store_id_cards ();
161
162         add_id_card_widget (id_card);
163     }
164
165     private void add_identity_cb ()
166     {
167         var dialog = new AddIdentityDialog ();
168         var result = dialog.run ();
169
170         switch (result) {
171         case ResponseType.OK:
172             add_identity (dialog);
173             break;
174         default:
175             break;
176         }
177         dialog.destroy ();
178     }
179
180     private void remove_id_card_widget (IdCardWidget id_card_widget)
181     {
182         this.custom_vbox.remove_id_card_widget (id_card_widget);
183     }
184
185     private void remove_identity (IdCardWidget id_card_widget)
186     {
187         var id_card = id_card_widget.id_card;
188
189         this.identities_manager.id_card_list.remove (id_card);
190         this.identities_manager.store_id_cards ();
191
192         remove_id_card_widget (id_card_widget);
193     }
194
195     private void remove_identity_cb (IdCardWidget id_card_widget)
196     {
197         var id_card = id_card_widget.id_card;
198
199         var dialog = new MessageDialog (null,
200                                         DialogFlags.DESTROY_WITH_PARENT,
201                                         MessageType.INFO,
202                                         Gtk.ButtonsType.YES_NO,
203                                         _("Are you sure you want to delete %s ID Card?"), id_card.issuer);
204         var result = dialog.run ();
205         switch (result) {
206         case ResponseType.YES:
207             remove_identity (id_card_widget);
208             break;
209         default:
210             break;
211         }
212         dialog.destroy ();
213     }
214
215     private void label_make_bold (Label label)
216     {
217         var font_desc = new Pango.FontDescription ();
218
219         font_desc.set_weight (Pango.Weight.BOLD);
220
221         /* This will only affect the weight of the font, the rest is
222          * from the current state of the widget, which comes from the
223          * theme or user prefs, since the font desc only has the
224          * weight flag turned on.
225          */
226         label.modify_font (font_desc);
227     }
228
229     private void build_ui()
230     {
231         this.search_entry = new Entry();
232
233         this.search_entry.set_icon_from_icon_name (EntryIconPosition.PRIMARY,
234                                                    "edit-find-symbolic");
235         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, false);
236         this.search_entry.set_icon_tooltip_text (EntryIconPosition.PRIMARY,
237                                                  _("Search identity or service"));
238
239         this.search_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY,
240                                                    "edit-clear-symbolic");
241         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
242         this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
243                                                  _("Clear the current search"));
244
245         this.search_entry.icon_press.connect (search_entry_icon_press_cb);
246         this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
247         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
248
249         this.custom_vbox = new CustomVBox (false, 6);
250
251         var viewport = new Viewport (null, null);
252         viewport.set_border_width (6);
253         viewport.set_shadow_type (ShadowType.NONE);
254         viewport.add (custom_vbox);
255         var scroll = new ScrolledWindow (null, null);
256         scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
257         scroll.set_shadow_type (ShadowType.IN);
258         scroll.add_with_viewport (viewport);
259
260         var button_add = new ToolButton (null, null);
261         button_add.set_icon_name ("list-add-symbolic");
262         button_add.clicked.connect (add_identity_cb);
263         var button_toolbar = new Toolbar ();
264         button_toolbar.insert (button_add, 0);
265
266         var vbox_left = new VBox (false, 0);
267         vbox_left.pack_start (search_entry, false, false, 6);
268         vbox_left.pack_start (scroll, true, true, 0);
269         vbox_left.pack_start (button_toolbar, false, false, 0);
270         vbox_left.set_size_request (WINDOW_WIDTH, 0);
271
272         var login_vbox_title = new Label (_("Login: "));
273         label_make_bold (login_vbox_title);
274         login_vbox_title.set_alignment (0, (float) 0.5);
275         var username_label = new Label (_("Username:"));
276         username_label.set_alignment (1, (float) 0.5);
277         this.username_entry = new Entry ();
278         var password_label = new Label (_("Password:"));
279         password_label.set_alignment (1, (float) 0.5);
280         this.password_entry = new Entry ();
281         password_entry.set_invisible_char ('*');
282         password_entry.set_visibility (false);
283         var remember_checkbutton = new CheckButton.with_label (_("Remember password"));
284         var login_table = new Table (3, 3, false);
285         login_table.set_col_spacings (10);
286         login_table.set_row_spacings (10);
287         login_table.attach_defaults (username_label, 0, 1, 0, 1);
288         login_table.attach_defaults (username_entry, 1, 2, 0, 1);
289         login_table.attach_defaults (password_label, 0, 1, 1, 2);
290         login_table.attach_defaults (password_entry, 1, 2, 1, 2);
291         login_table.attach_defaults (remember_checkbutton,  1, 2, 2, 3);
292         var login_vbox_alignment = new Alignment (0, 0, 0, 0);
293         login_vbox_alignment.set_padding (0, 0, 12, 0);
294         login_vbox_alignment.add (login_table);
295         var login_vbox = new VBox (false, 6);
296         login_vbox.pack_start (login_vbox_title, false, true, 0);
297         login_vbox.pack_start (login_vbox_alignment, false, true, 0);
298
299         var services_vbox_title = new Label (_("Services:"));
300         label_make_bold (services_vbox_title);
301         services_vbox_title.set_alignment (0, (float) 0.5);
302         var email_label = new Label (_("Email"));
303         var email_remove_button = new Button.from_stock (Stock.REMOVE);
304         var im_label = new Label (_("IM"));
305         var im_remove_button = new Button.from_stock (Stock.REMOVE);
306         var services_table = new Table (2, 2, false);
307         services_table.set_col_spacings (10);
308         services_table.set_row_spacings (10);
309         services_table.attach_defaults (email_label, 0, 1, 0, 1);
310         services_table.attach_defaults (email_remove_button, 1, 2, 0, 1);
311         services_table.attach_defaults (im_label, 0, 1, 1, 2);
312         services_table.attach_defaults (im_remove_button, 1, 2, 1, 2);
313         var services_vbox_alignment = new Alignment (0, 0, 0, 0);
314         services_vbox_alignment.set_padding (0, 0, 12, 0);
315         services_vbox_alignment.add (services_table);
316         var services_vbox = new VBox (false, 6);
317         services_vbox.pack_start (services_vbox_title, false, true, 0);
318         services_vbox.pack_start (services_vbox_alignment, false, true, 0);
319
320         this.vbox_rigth = new VBox (false, 18);
321         vbox_rigth.pack_start (login_vbox, false, true, 0);
322         vbox_rigth.pack_start (services_vbox, false, true, 0);
323
324         var hbox = new HBox (false, 12);
325         hbox.pack_start (vbox_left, false, false, 0);
326         hbox.pack_start (vbox_rigth, false, false, 0);
327
328         var main_vbox = new VBox (false, 12);
329         main_vbox.pack_start (hbox, true, true, 0);
330         main_vbox.set_border_width (12);
331         add (main_vbox);
332
333         main_vbox.show_all();
334         this.vbox_rigth.hide ();
335     }
336
337     private void connect_signals()
338     {
339         this.destroy.connect (Gtk.main_quit);
340     }
341
342     public static int main(string[] args)
343     {
344         Gtk.init(ref args);
345
346         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
347         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
348         Intl.textdomain (Config.GETTEXT_PACKAGE);
349
350         var window = new MainWindow();
351         window.show ();
352
353         Gtk.main();
354
355         return 0;
356     }
357 }