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