moonshot-window: align the services labels to the left
[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     private VBox services_internal_vbox;
12
13     private Entry username_entry;
14     private Entry password_entry;
15
16     private ListStore listmodel;
17     private TreeModelFilter filter;
18
19     private IdentitiesManager identities_manager;
20
21     private MoonshotServer dbus_server;
22
23     public IdCardWidget selected_id_card_widget;
24
25     private SourceFunc callback;
26
27     private enum Columns
28     {
29         IDCARD_COL,
30         LOGO_COL,
31         ISSUER_COL,
32         USERNAME_COL,
33         PASSWORD_COL,
34         N_COLUMNS
35     }
36
37     public MainWindow()
38     {
39         this.title = "Moonshoot";
40         this.position = WindowPosition.CENTER;
41         set_default_size (WINDOW_WIDTH, WINDOW_HEIGHT);
42
43         build_ui();
44         setup_identities_list();
45         load_gss_eap_id_file();
46         //load_id_cards();
47         connect_signals();
48         init_dbus_server();
49     }
50
51     private bool visible_func (TreeModel model, TreeIter iter)
52     {
53         string issuer;
54         string search_text;
55         string issuer_casefold;
56         string search_text_casefold;
57
58         model.get (iter,
59                    Columns.ISSUER_COL, out issuer);
60         search_text = this.search_entry.get_text ();
61
62         if (issuer == null || search_text == null)
63             return false;
64
65         issuer_casefold = issuer.casefold ();
66         search_text_casefold = search_text.casefold ();
67
68         if (issuer_casefold.contains (search_text_casefold))
69             return true;
70
71         return false;
72     }
73
74     private void setup_identities_list ()
75     {
76        this.listmodel = new ListStore (Columns.N_COLUMNS, typeof (IdCard),
77                                                           typeof (Gdk.Pixbuf),
78                                                           typeof (string),
79                                                           typeof (string),
80                                                           typeof (string));
81       this.filter = new TreeModelFilter (listmodel, null);
82
83       filter.set_visible_func (visible_func);
84     }
85
86     private void search_entry_icon_press_cb (EntryIconPosition pos, Gdk.Event event)
87     {
88         if (pos == EntryIconPosition.PRIMARY)
89         {
90             print ("Search entry icon pressed\n");
91         }
92         else
93         {
94             this.search_entry.set_text ("");
95         }
96     }
97
98     private void search_entry_text_changed_cb ()
99     {
100         this.filter.refilter ();
101         redraw_id_card_widgets ();
102
103         var has_text = this.search_entry.get_text_length () > 0;
104         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, has_text);
105         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, has_text);
106
107         this.vbox_rigth.set_visible (false);
108         this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
109     }
110
111     private bool search_entry_key_press_event_cb (Gdk.EventKey e)
112     {
113         if(Gdk.keyval_name(e.keyval) == "Escape")
114            this.search_entry.set_text("");
115
116         // Continue processing this event, since the
117         // text entry functionality needs to see it too.
118         return false;
119     }
120
121     private void load_gss_eap_id_file ()
122     {
123         IdCard id_card;
124
125         this.identities_manager = new IdentitiesManager ();
126
127         id_card = this.identities_manager.load_gss_eap_id_file ();
128         if (id_card != null)
129         {
130             add_id_card_data (id_card);
131             add_id_card_widget (id_card);
132         }
133     }
134
135     private void load_id_cards ()
136     {
137         this.identities_manager = new IdentitiesManager ();
138
139         foreach (IdCard id_card in identities_manager.id_card_list)
140         {
141             add_id_card_data (id_card);
142             add_id_card_widget (id_card);
143         }
144     }
145
146     private void fill_details (IdCardWidget id_card_widget)
147     {
148        var id_card = id_card_widget.id_card;
149        this.username_entry.set_text (id_card.username);
150        this.password_entry.set_text (id_card.password);
151
152        var children = this.services_internal_vbox.get_children ();
153        foreach (var hbox in children)
154            hbox.destroy();
155        fill_services_vbox (id_card_widget.id_card);
156     }
157
158     private void show_details (IdCard id_card)
159     {
160        this.vbox_rigth.set_visible (!vbox_rigth.get_visible ());
161
162        if (this.vbox_rigth.get_visible () == false)
163        {
164            this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
165        }
166     }
167
168     private void details_identity_cb (IdCardWidget id_card_widget)
169     {
170        fill_details (id_card_widget);
171        show_details (id_card_widget.id_card);
172     }
173
174     private IdCard get_id_card_data (AddIdentityDialog dialog)
175     {
176         var id_card = new IdCard ();
177
178         id_card.issuer = dialog.issuer;
179         if (id_card.issuer == "")
180             id_card.issuer = "Issuer";
181         id_card.username = dialog.username;
182         id_card.password = dialog.password;
183
184         var icon_theme = IconTheme.get_default ();
185         try
186         {
187             id_card.pixbuf = icon_theme.load_icon ("avatar-default",
188                                                    48,
189                                                    IconLookupFlags.FORCE_SIZE);
190         }
191         catch (Error e)
192         {
193             id_card.pixbuf = null;
194             stdout.printf("Error: %s\n", e.message);
195         }
196
197         id_card.services = {"email","jabber","irc"};
198
199         return id_card;
200     }
201
202     private void add_id_card_data (IdCard id_card)
203     {
204         TreeIter iter;
205
206         this.listmodel.append (out iter);
207         listmodel.set (iter,
208                        Columns.IDCARD_COL, id_card,
209                        Columns.LOGO_COL, id_card.pixbuf,
210                        Columns.ISSUER_COL, id_card.issuer,
211                        Columns.USERNAME_COL, id_card.username,
212                        Columns.PASSWORD_COL, id_card.password);
213     }
214
215     private void remove_id_card_data (IdCard id_card)
216     {
217         TreeIter iter;
218         string issuer;
219
220         if (listmodel.get_iter_first (out iter))
221         {
222             do
223             {
224                 listmodel.get (iter,
225                                Columns.ISSUER_COL, out issuer);
226
227                 if (id_card.issuer == issuer)
228                 {
229                     listmodel.remove (iter);
230                     break;
231                 }
232             }
233             while (listmodel.iter_next (ref iter));
234         }
235     }
236
237     private void add_id_card_widget (IdCard id_card)
238     {
239         var id_card_widget = new IdCardWidget (id_card);
240
241         this.custom_vbox.add_id_card_widget (id_card_widget);
242
243         id_card_widget.details_id.connect (details_identity_cb);
244         id_card_widget.remove_id.connect (remove_identity_cb);
245         id_card_widget.send_id.connect (send_identity_cb);
246         id_card_widget.expanded.connect (this.custom_vbox.receive_expanded_event);
247         id_card_widget.expanded.connect (fill_details);
248     }
249
250     private void add_identity (AddIdentityDialog dialog)
251     {
252         var id_card = get_id_card_data (dialog);
253
254         this.identities_manager.id_card_list.prepend (id_card);
255         this.identities_manager.store_id_cards ();
256         this.identities_manager.store_gss_eap_id_file (id_card);
257
258         add_id_card_data (id_card);
259         add_id_card_widget (id_card);
260     }
261
262     private void add_identity_cb ()
263     {
264         var dialog = new AddIdentityDialog ();
265         var result = dialog.run ();
266
267         switch (result) {
268         case ResponseType.OK:
269             add_identity (dialog);
270             break;
271         default:
272             break;
273         }
274         dialog.destroy ();
275     }
276
277     private void remove_id_card_widget (IdCardWidget id_card_widget)
278     {
279         remove_id_card_data (id_card_widget.id_card);
280
281         this.custom_vbox.remove_id_card_widget (id_card_widget);
282     }
283
284     private void remove_identity (IdCardWidget id_card_widget)
285     {
286         var id_card = id_card_widget.id_card;
287
288         this.identities_manager.id_card_list.remove (id_card);
289         this.identities_manager.store_id_cards ();
290         this.identities_manager.store_gss_eap_id_file (null);
291
292         remove_id_card_widget (id_card_widget);
293     }
294
295     private void redraw_id_card_widgets ()
296     {
297         TreeIter iter;
298         IdCard id_card;
299
300         var children = this.custom_vbox.get_children ();
301         foreach (var id_card_widget in children)
302             id_card_widget.destroy();
303
304         if (filter.get_iter_first (out iter))
305         {
306             do
307             {
308                 filter.get (iter,
309                             Columns.IDCARD_COL, out id_card);
310
311                 add_id_card_widget (id_card);
312             }
313             while (filter.iter_next (ref iter));
314         }
315     }
316
317     private void remove_identity_cb (IdCardWidget id_card_widget)
318     {
319         var id_card = id_card_widget.id_card;
320
321         var dialog = new MessageDialog (null,
322                                         DialogFlags.DESTROY_WITH_PARENT,
323                                         MessageType.INFO,
324                                         Gtk.ButtonsType.YES_NO,
325                                         _("Are you sure you want to delete %s ID Card?"), id_card.issuer);
326         var result = dialog.run ();
327         switch (result) {
328         case ResponseType.YES:
329             remove_identity (id_card_widget);
330             break;
331         default:
332             break;
333         }
334         dialog.destroy ();
335     }
336
337     public void set_callback (SourceFunc callback)
338     {
339         this.callback = callback;
340     }
341
342     public void send_identity_cb (IdCardWidget id_card_widget)
343     {
344         this.selected_id_card_widget = id_card_widget;
345         this.callback ();
346     }
347
348     private void label_make_bold (Label label)
349     {
350         var font_desc = new Pango.FontDescription ();
351
352         font_desc.set_weight (Pango.Weight.BOLD);
353
354         /* This will only affect the weight of the font, the rest is
355          * from the current state of the widget, which comes from the
356          * theme or user prefs, since the font desc only has the
357          * weight flag turned on.
358          */
359         label.modify_font (font_desc);
360     }
361
362     private void fill_services_vbox (IdCard id_card)
363     {
364         int i = 0;
365         var n_columns = id_card.services.length;
366
367         var services_table = new Table (n_columns, 2, false);
368         services_table.set_col_spacings (10);
369         services_table.set_row_spacings (10);
370         this.services_internal_vbox.add (services_table);
371
372         foreach (string service in id_card.services)
373         {
374             var label = new Label (service);
375             label.set_alignment (0, (float) 0.5);
376 #if VALA_0_12
377             var remove_button = new Button.from_stock (Stock.REMOVE);
378 #else
379             var remove_button = new Button.from_stock (STOCK_REMOVE);
380 #endif
381             services_table.attach_defaults (label, 0, 1, i, i+1);
382             services_table.attach_defaults (remove_button, 1, 2, i, i+1);
383             i++;
384         }
385         this.services_internal_vbox.show_all ();
386     }
387
388     private void build_ui()
389     {
390         this.search_entry = new Entry();
391
392         set_atk_name_description (search_entry, _("Search entry"), _("Search for a specific ID Card"));
393         this.search_entry.set_icon_from_icon_name (EntryIconPosition.PRIMARY,
394                                                    "edit-find-symbolic");
395         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, false);
396         this.search_entry.set_icon_tooltip_text (EntryIconPosition.PRIMARY,
397                                                  _("Search identity or service"));
398
399         this.search_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY,
400                                                    "edit-clear-symbolic");
401         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
402         this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
403                                                  _("Clear the current search"));
404
405         this.search_entry.icon_press.connect (search_entry_icon_press_cb);
406         this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
407         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
408
409         this.custom_vbox = new CustomVBox (false, 6);
410
411         var viewport = new Viewport (null, null);
412         viewport.set_border_width (6);
413         viewport.set_shadow_type (ShadowType.NONE);
414         viewport.add (custom_vbox);
415         var scroll = new ScrolledWindow (null, null);
416         scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
417         scroll.set_shadow_type (ShadowType.IN);
418         scroll.add_with_viewport (viewport);
419
420         var button_add = new ToolButton (null, null);
421         button_add.set_icon_name ("list-add-symbolic");
422         set_atk_name_description (button_add, _("Add"), _("Add new ID Card"));
423         button_add.clicked.connect (add_identity_cb);
424         var button_toolbar = new Toolbar ();
425         button_toolbar.insert (button_add, 0);
426
427         var vbox_left = new VBox (false, 0);
428         vbox_left.pack_start (search_entry, false, false, 6);
429         vbox_left.pack_start (scroll, true, true, 0);
430         vbox_left.pack_start (button_toolbar, false, false, 0);
431         vbox_left.set_size_request (WINDOW_WIDTH, 0);
432
433         var login_vbox_title = new Label (_("Login: "));
434         label_make_bold (login_vbox_title);
435         login_vbox_title.set_alignment (0, (float) 0.5);
436         var username_label = new Label (_("Username:"));
437         username_label.set_alignment (1, (float) 0.5);
438         this.username_entry = new Entry ();
439         var password_label = new Label (_("Password:"));
440         password_label.set_alignment (1, (float) 0.5);
441         this.password_entry = new Entry ();
442         password_entry.set_invisible_char ('*');
443         password_entry.set_visibility (false);
444         var remember_checkbutton = new CheckButton.with_label (_("Remember password"));
445         var login_table = new Table (3, 3, false);
446         login_table.set_col_spacings (10);
447         login_table.set_row_spacings (10);
448         login_table.attach_defaults (username_label, 0, 1, 0, 1);
449         login_table.attach_defaults (username_entry, 1, 2, 0, 1);
450         login_table.attach_defaults (password_label, 0, 1, 1, 2);
451         login_table.attach_defaults (password_entry, 1, 2, 1, 2);
452         login_table.attach_defaults (remember_checkbutton,  1, 2, 2, 3);
453         var login_vbox_alignment = new Alignment (0, 0, 0, 0);
454         login_vbox_alignment.set_padding (0, 0, 12, 0);
455         login_vbox_alignment.add (login_table);
456         var login_vbox = new VBox (false, 6);
457         login_vbox.pack_start (login_vbox_title, false, true, 0);
458         login_vbox.pack_start (login_vbox_alignment, false, true, 0);
459
460         var services_vbox_title = new Label (_("Services:"));
461         label_make_bold (services_vbox_title);
462         services_vbox_title.set_alignment (0, (float) 0.5);
463         var services_vbox_alignment = new Alignment (0, 0, 0, 0);
464         services_vbox_alignment.set_padding (0, 0, 12, 0);
465         this.services_internal_vbox = new VBox (true, 6);
466         services_vbox_alignment.add (services_internal_vbox);
467         var services_vbox = new VBox (false, 6);
468         services_vbox.pack_start (services_vbox_title, false, true, 0);
469         services_vbox.pack_start (services_vbox_alignment, false, true, 0);
470
471         this.vbox_rigth = new VBox (false, 18);
472         vbox_rigth.pack_start (login_vbox, false, true, 0);
473         vbox_rigth.pack_start (services_vbox, false, true, 0);
474
475         var hbox = new HBox (false, 12);
476         hbox.pack_start (vbox_left, false, false, 0);
477         hbox.pack_start (vbox_rigth, false, false, 0);
478
479         var main_vbox = new VBox (false, 12);
480         main_vbox.pack_start (hbox, true, true, 0);
481         main_vbox.set_border_width (12);
482         add (main_vbox);
483
484         main_vbox.show_all();
485         this.vbox_rigth.hide ();
486     }
487
488     private void set_atk_name_description (Widget widget, string name, string description)
489     {
490        var atk_widget = widget.get_accessible ();
491
492        atk_widget.set_name (name);
493        atk_widget.set_description (description);
494     }
495
496     private void connect_signals()
497     {
498         this.destroy.connect (Gtk.main_quit);
499     }
500
501     private void init_dbus_server ()
502     {
503         try {
504             var conn = DBus.Bus.get (DBus.BusType.SESSION);
505             dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
506                                                        "/org/freedesktop/DBus",
507                                                        "org.freedesktop.DBus");
508
509             // try to register service in session bus
510             uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
511             assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
512
513             this.dbus_server = new MoonshotServer (this);
514             conn.register_object ("/org/janet/moonshot", dbus_server);
515
516         }
517         catch (DBus.Error e)
518         {
519             stderr.printf ("%s\n", e.message);
520         }
521     }
522
523     public static int main(string[] args)
524     {
525         Gtk.init(ref args);
526
527         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
528         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
529         Intl.textdomain (Config.GETTEXT_PACKAGE);
530
531         var window = new MainWindow();
532         window.show ();
533
534         Gtk.main();
535
536         return 0;
537     }
538 }