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