Load icons manually on Windows
[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 ipc_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_ipc_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         id_card.pixbuf = find_icon ("avatar-default", 48);
201         id_card.services = {"email","jabber","irc"};
202
203         return id_card;
204     }
205
206     private void add_id_card_data (IdCard id_card)
207     {
208         TreeIter iter;
209
210         this.listmodel.append (out iter);
211         listmodel.set (iter,
212                        Columns.IDCARD_COL, id_card,
213                        Columns.LOGO_COL, id_card.pixbuf,
214                        Columns.ISSUER_COL, id_card.issuer,
215                        Columns.USERNAME_COL, id_card.username,
216                        Columns.PASSWORD_COL, id_card.password);
217     }
218
219     private void remove_id_card_data (IdCard id_card)
220     {
221         TreeIter iter;
222         string issuer;
223
224         if (listmodel.get_iter_first (out iter))
225         {
226             do
227             {
228                 listmodel.get (iter,
229                                Columns.ISSUER_COL, out issuer);
230
231                 if (id_card.issuer == issuer)
232                 {
233                     listmodel.remove (iter);
234                     break;
235                 }
236             }
237             while (listmodel.iter_next (ref iter));
238         }
239     }
240
241     private void add_id_card_widget (IdCard id_card)
242     {
243         var id_card_widget = new IdCardWidget (id_card);
244
245         this.custom_vbox.add_id_card_widget (id_card_widget);
246
247         id_card_widget.details_id.connect (details_identity_cb);
248         id_card_widget.remove_id.connect (remove_identity_cb);
249         id_card_widget.send_id.connect (send_identity_cb);
250         id_card_widget.expanded.connect (this.custom_vbox.receive_expanded_event);
251         id_card_widget.expanded.connect (fill_details);
252     }
253
254     private void add_identity (AddIdentityDialog dialog)
255     {
256         var id_card = get_id_card_data (dialog);
257
258         this.identities_manager.id_card_list.prepend (id_card);
259         this.identities_manager.store_id_cards ();
260         this.identities_manager.store_gss_eap_id_file (id_card);
261
262         add_id_card_data (id_card);
263         add_id_card_widget (id_card);
264     }
265
266     private void add_identity_cb ()
267     {
268         var dialog = new AddIdentityDialog ();
269         var result = dialog.run ();
270
271         switch (result) {
272         case ResponseType.OK:
273             add_identity (dialog);
274             break;
275         default:
276             break;
277         }
278         dialog.destroy ();
279     }
280
281     private void remove_id_card_widget (IdCardWidget id_card_widget)
282     {
283         remove_id_card_data (id_card_widget.id_card);
284
285         this.custom_vbox.remove_id_card_widget (id_card_widget);
286     }
287
288     private void remove_identity (IdCardWidget id_card_widget)
289     {
290         var id_card = id_card_widget.id_card;
291
292         this.identities_manager.id_card_list.remove (id_card);
293         this.identities_manager.store_id_cards ();
294         this.identities_manager.store_gss_eap_id_file (null);
295
296         remove_id_card_widget (id_card_widget);
297     }
298
299     private void redraw_id_card_widgets ()
300     {
301         TreeIter iter;
302         IdCard id_card;
303
304         var children = this.custom_vbox.get_children ();
305         foreach (var id_card_widget in children)
306             id_card_widget.destroy();
307
308         if (filter.get_iter_first (out iter))
309         {
310             do
311             {
312                 filter.get (iter,
313                             Columns.IDCARD_COL, out id_card);
314
315                 add_id_card_widget (id_card);
316             }
317             while (filter.iter_next (ref iter));
318         }
319     }
320
321     private void remove_identity_cb (IdCardWidget id_card_widget)
322     {
323         var id_card = id_card_widget.id_card;
324
325         var dialog = new MessageDialog (null,
326                                         DialogFlags.DESTROY_WITH_PARENT,
327                                         MessageType.INFO,
328                                         Gtk.ButtonsType.YES_NO,
329                                         _("Are you sure you want to delete %s ID Card?"), id_card.issuer);
330         var result = dialog.run ();
331         switch (result) {
332         case ResponseType.YES:
333             remove_identity (id_card_widget);
334             break;
335         default:
336             break;
337         }
338         dialog.destroy ();
339     }
340
341     public void set_callback (owned SourceFunc callback)
342     {
343         this.callback = (owned) callback;
344     }
345
346     public void send_identity_cb (IdCardWidget id_card_widget)
347     {
348         this.hide ();
349         this.selected_id_card_widget = id_card_widget;
350         this.callback ();
351     }
352
353     private void label_make_bold (Label label)
354     {
355         var font_desc = new Pango.FontDescription ();
356
357         font_desc.set_weight (Pango.Weight.BOLD);
358
359         /* This will only affect the weight of the font, the rest is
360          * from the current state of the widget, which comes from the
361          * theme or user prefs, since the font desc only has the
362          * weight flag turned on.
363          */
364         label.modify_font (font_desc);
365     }
366
367     private void fill_services_vbox (IdCard id_card)
368     {
369         int i = 0;
370         var n_columns = id_card.services.length;
371
372         var services_table = new Table (n_columns, 2, false);
373         services_table.set_col_spacings (10);
374         services_table.set_row_spacings (10);
375         this.services_internal_vbox.add (services_table);
376
377         foreach (string service in id_card.services)
378         {
379             var label = new Label (service);
380             label.set_alignment (0, (float) 0.5);
381 #if VALA_0_12
382             var remove_button = new Button.from_stock (Stock.REMOVE);
383 #else
384             var remove_button = new Button.from_stock (STOCK_REMOVE);
385 #endif
386             services_table.attach_defaults (label, 0, 1, i, i+1);
387             services_table.attach_defaults (remove_button, 1, 2, i, i+1);
388             i++;
389         }
390         this.services_internal_vbox.show_all ();
391     }
392
393     private void on_about_action ()
394     {
395         string[] authors = {
396             "Javier Jardón <jjardon@codethink.co.uk>",
397             null
398         };
399
400         const string copyright = "Copyright 2011 JANET";
401
402         const string license =
403 "
404     This program is free software: you can redistribute it and/or modify
405     it under the terms of the GNU General Public License as published by
406     the Free Software Foundation, either version 3 of the License, or
407     (at your option) any later version.
408
409     This program is distributed in the hope that it will be useful,
410     but WITHOUT ANY WARRANTY; without even the implied warranty of
411     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
412     GNU General Public License for more details.
413
414     You should have received a copy of the GNU General Public License
415     along with this program.  If not, see <http://www.gnu.org/licenses/>.
416 ";
417
418         Gtk.show_about_dialog (this,
419             "comments", _("Moonshot project UI"),
420             "copyright", copyright,
421             "website", "http://www.project-moonshot.org/",
422             "license", license,
423             "website-label", _("Visit the Moonshot project web site"),
424             "authors", authors,
425             "translator-credits", _("translator-credits"),
426             null
427         );
428     }
429
430     private Gtk.ActionEntry[] create_actions() {
431         Gtk.ActionEntry[] actions = new Gtk.ActionEntry[0];
432
433         Gtk.ActionEntry filemenu = { "FileMenuAction",
434                                      null,
435                                      N_("_File"),
436                                      null, null, null };
437         actions += filemenu;
438         Gtk.ActionEntry add = { "AddIdCardAction",
439 #if VALA_0_12
440                                 Stock.ADD,
441 #else
442                                 STOCK.ADD,
443 #endif
444                                 N_("Add ID Card"),
445                                 null,
446                                 N_("Add a new ID Card"),
447                                 add_identity_cb };
448         actions += add;
449         Gtk.ActionEntry quit = { "QuitAction",
450 #if VALA_0_12
451                                  Stock.QUIT,
452 #else
453                                  STOCK.QUIT,
454 #endif
455                                  N_("Quit"),
456                                  "<control>Q",
457                                  N_("Quit the application"),
458                                  Gtk.main_quit };
459         actions += quit;
460
461         Gtk.ActionEntry helpmenu = { "HelpMenuAction",
462                                      null,
463                                      N_("_Help"),
464                                      null, null, null };
465         actions += helpmenu;
466         Gtk.ActionEntry about = { "AboutAction",
467 #if VALA_0_12
468                                   Stock.ABOUT,
469 #else
470                                   STOCK.ABOUT,
471 #endif
472                                   N_("About"),
473                                   null,
474                                   N_("About this application"),
475                                   on_about_action };
476         actions += about;
477
478         return actions;
479     }
480
481
482     private void create_ui_manager ()
483     {
484         Gtk.ActionGroup action_group = new Gtk.ActionGroup ("GeneralActionGroup");
485         action_group.add_actions (create_actions (), this);
486         ui_manager.insert_action_group (action_group, 0);
487         try
488         {
489             ui_manager.add_ui_from_string (layout, -1);
490         }
491         catch (Error e)
492         {
493             stderr.printf ("%s\n", e.message);
494         }
495         ui_manager.ensure_update ();
496     }
497
498     private void build_ui()
499     {
500         create_ui_manager ();
501
502         this.search_entry = new Entry();
503
504         set_atk_name_description (search_entry, _("Search entry"), _("Search for a specific ID Card"));
505         this.search_entry.set_icon_from_pixbuf (EntryIconPosition.PRIMARY,
506                                                 find_icon_sized ("edit-find-symbolic", Gtk.IconSize.MENU));
507         this.search_entry.set_icon_tooltip_text (EntryIconPosition.PRIMARY,
508                                                  _("Search identity or service"));
509         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, false);
510
511         this.search_entry.set_icon_from_pixbuf (EntryIconPosition.SECONDARY,
512                                                 find_icon_sized ("edit-clear-symbolic", Gtk.IconSize.MENU));
513         this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
514                                                  _("Clear the current search"));
515         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
516
517
518         this.search_entry.icon_press.connect (search_entry_icon_press_cb);
519         this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
520         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
521
522         this.custom_vbox = new CustomVBox (false, 6);
523
524         var viewport = new Viewport (null, null);
525         viewport.set_border_width (6);
526         viewport.set_shadow_type (ShadowType.NONE);
527         viewport.add (custom_vbox);
528         var scroll = new ScrolledWindow (null, null);
529         scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
530         scroll.set_shadow_type (ShadowType.IN);
531         scroll.add_with_viewport (viewport);
532
533         var vbox_left = new VBox (false, 0);
534         vbox_left.pack_start (search_entry, false, false, 6);
535         vbox_left.pack_start (scroll, true, true, 0);
536         vbox_left.set_size_request (WINDOW_WIDTH, 0);
537
538         var login_vbox_title = new Label (_("Login: "));
539         label_make_bold (login_vbox_title);
540         login_vbox_title.set_alignment (0, (float) 0.5);
541         var username_label = new Label (_("Username:"));
542         username_label.set_alignment (1, (float) 0.5);
543         this.username_entry = new Entry ();
544         var password_label = new Label (_("Password:"));
545         password_label.set_alignment (1, (float) 0.5);
546         this.password_entry = new Entry ();
547         password_entry.set_invisible_char ('*');
548         password_entry.set_visibility (false);
549         var remember_checkbutton = new CheckButton.with_label (_("Remember password"));
550         var login_table = new Table (3, 3, false);
551         login_table.set_col_spacings (10);
552         login_table.set_row_spacings (10);
553         login_table.attach_defaults (username_label, 0, 1, 0, 1);
554         login_table.attach_defaults (username_entry, 1, 2, 0, 1);
555         login_table.attach_defaults (password_label, 0, 1, 1, 2);
556         login_table.attach_defaults (password_entry, 1, 2, 1, 2);
557         login_table.attach_defaults (remember_checkbutton,  1, 2, 2, 3);
558         var login_vbox_alignment = new Alignment (0, 0, 0, 0);
559         login_vbox_alignment.set_padding (0, 0, 12, 0);
560         login_vbox_alignment.add (login_table);
561         var login_vbox = new VBox (false, 6);
562         login_vbox.pack_start (login_vbox_title, false, true, 0);
563         login_vbox.pack_start (login_vbox_alignment, false, true, 0);
564
565         var services_vbox_title = new Label (_("Services:"));
566         label_make_bold (services_vbox_title);
567         services_vbox_title.set_alignment (0, (float) 0.5);
568         var services_vbox_alignment = new Alignment (0, 0, 0, 0);
569         services_vbox_alignment.set_padding (0, 0, 12, 0);
570         this.services_internal_vbox = new VBox (true, 6);
571         services_vbox_alignment.add (services_internal_vbox);
572         var services_vbox = new VBox (false, 6);
573         services_vbox.pack_start (services_vbox_title, false, true, 0);
574         services_vbox.pack_start (services_vbox_alignment, false, true, 0);
575
576         this.vbox_rigth = new VBox (false, 18);
577         vbox_rigth.pack_start (login_vbox, false, true, 0);
578         vbox_rigth.pack_start (services_vbox, false, true, 0);
579
580         var hbox = new HBox (false, 12);
581         hbox.pack_start (vbox_left, false, false, 0);
582         hbox.pack_start (vbox_rigth, false, false, 0);
583
584         var main_vbox = new VBox (false, 0);
585         main_vbox.set_border_width (12);
586         var menubar = this.ui_manager.get_widget ("/MenuBar");
587         main_vbox.pack_start (menubar, false, false, 0);
588         main_vbox.pack_start (hbox, true, true, 0);
589         add (main_vbox);
590
591         main_vbox.show_all();
592         this.vbox_rigth.hide ();
593     }
594
595     private void set_atk_name_description (Widget widget, string name, string description)
596     {
597        var atk_widget = widget.get_accessible ();
598
599        atk_widget.set_name (name);
600        atk_widget.set_description (description);
601     }
602
603     private void connect_signals()
604     {
605         this.destroy.connect (Gtk.main_quit);
606     }
607
608     private void init_ipc_server ()
609     {
610 #if IPC_MSRPC
611         /* Errors will currently be sent via g_log - ie. to an
612          * obtrusive message box, on Windows
613          */
614         this.ipc_server = MoonshotServer.get_instance ();
615         MoonshotServer.start (this);
616 #else
617         try {
618             var conn = DBus.Bus.get (DBus.BusType.SESSION);
619             dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
620                                                        "/org/freedesktop/DBus",
621                                                        "org.freedesktop.DBus");
622
623             // try to register service in session bus
624             uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
625             assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
626
627             this.ipc_server = new MoonshotServer (this);
628             conn.register_object ("/org/janet/moonshot", ipc_server);
629
630         }
631         catch (DBus.Error e)
632         {
633             stderr.printf ("%s\n", e.message);
634         }
635 #endif
636     }
637
638     public static int main(string[] args)
639     {
640         Gtk.init(ref args);
641
642 #if OS_WIN32
643         // Force specific theme settings on Windows without requiring a gtkrc file
644         Gtk.Settings settings = Gtk.Settings.get_default ();
645         settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
646         settings.set_long_property ("gtk-menu-images", 0, "moonshot");
647 #endif
648
649         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
650         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
651         Intl.textdomain (Config.GETTEXT_PACKAGE);
652
653         var window = new MainWindow();
654         window.show ();
655
656         Gtk.main();
657
658         return 0;
659     }
660 }