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