Checkpointing: ID Selector (main dialog) now looks as specified by reskinning wirefra...
[moonshot-ui.git] / src / moonshot-identity-management-view.vala
1 /*
2  * Copyright (c) 2011-2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31 */
32 using Gee;
33 using Gtk;
34
35 public class IdentityManagerView : Window {
36     static MoonshotLogger logger = get_logger("IdentityManagerView");
37
38     private const int WINDOW_WIDTH = 700;
39     private const int WINDOW_HEIGHT = 500;
40     protected IdentityManagerApp parent_app;
41     #if OS_MACOS
42         public OSXApplication osxApp;
43     #endif
44     private UIManager ui_manager = new UIManager();
45     private Entry search_entry;
46     private VBox vbox_right;
47     // private VBox login_vbox;
48     // private VBox services_vbox;
49     private CustomVBox custom_vbox;
50     // private VBox services_internal_vbox;
51     // private ScrolledWindow services_vscroll;
52     // private Entry issuer_entry;
53     // private Entry username_entry;
54     // private Entry password_entry;
55     private Label prompting_service;
56     private Label no_identity_title;
57     // private CheckButton remember_checkbutton;
58     // private Button update_password_button;
59
60     private Gtk.ListStore* listmodel;
61     private TreeModelFilter filter;
62
63     public IdentityManagerModel identities_manager;
64     private unowned SList<IdCard>    candidates;
65
66     public GLib.Queue<IdentityRequest> request_queue;
67
68     // private HashTable<Gtk.Button, string> service_button_map;
69
70     private enum Columns
71     {
72         IDCARD_COL,
73         LOGO_COL,
74         ISSUER_COL,
75         USERNAME_COL,
76         PASSWORD_COL,
77         N_COLUMNS
78     }
79
80     private const string menu_layout =
81     "<menubar name='MenuBar'>" +
82     "        <menu name='HelpMenu' action='HelpMenuAction'>" +
83     "             <menuitem name='About' action='AboutAction' />" +
84     "        </menu>" +
85     "</menubar>";
86
87     public IdentityManagerView(IdentityManagerApp app) {
88         parent_app = app;
89         #if OS_MACOS
90             osxApp = OSXApplication.get_instance();
91         #endif
92         identities_manager = parent_app.model;
93         request_queue = new GLib.Queue<IdentityRequest>();
94         // service_button_map = new HashTable<Gtk.Button, string>(direct_hash, direct_equal);
95         this.title = "Moonshot Identity Selector";
96         this.set_position(WindowPosition.CENTER);
97         set_default_size(WINDOW_WIDTH, WINDOW_HEIGHT);
98         build_ui();
99         setup_list_model(); 
100         load_id_cards(); 
101         connect_signals();
102     }
103     
104     public void on_card_list_changed() {
105         load_id_cards();
106     }
107     
108     private bool visible_func(TreeModel model, TreeIter iter)
109     {
110         IdCard id_card;
111
112         model.get(iter,
113                   Columns.IDCARD_COL, out id_card);
114
115         if (id_card == null)
116             return false;
117         
118         if (candidates != null)
119         {
120             bool is_candidate = false;
121             foreach (IdCard candidate in candidates)
122             {
123                 if (candidate == id_card)
124                     is_candidate = true;
125             }
126             if (!is_candidate)
127                 return false;
128         }
129         
130         string entry_text = search_entry.get_text();
131         if (entry_text == null || entry_text == "")
132         {
133             return true;
134         }
135
136         foreach (string search_text in entry_text.split(" "))
137         {
138             if (search_text == "")
139                 continue;
140          
141
142             string search_text_casefold = search_text.casefold();
143
144             if (id_card.issuer != null)
145             {
146                 string issuer_casefold = id_card.issuer;
147
148                 if (issuer_casefold.contains(search_text_casefold))
149                     return true;
150             }
151
152             if (id_card.display_name != null)
153             {
154                 string display_name_casefold = id_card.display_name.casefold();
155               
156                 if (display_name_casefold.contains(search_text_casefold))
157                     return true;
158             }
159             
160             if (id_card.services.length > 0)
161             {
162                 foreach (string service in id_card.services)
163                 {
164                     string service_casefold = service.casefold();
165
166                     if (service_casefold.contains(search_text_casefold))
167                         return true;
168                 }
169             }
170         }
171         return false;
172     }
173
174     private void setup_list_model()
175     {
176         this.listmodel = new Gtk.ListStore(Columns.N_COLUMNS, typeof(IdCard),
177                                            typeof(Gdk.Pixbuf),
178                                            typeof(string),
179                                            typeof(string),
180                                            typeof(string));
181         this.filter = new TreeModelFilter(listmodel, null);
182
183         filter.set_visible_func(visible_func);
184     }
185
186     private void search_entry_icon_press_cb(EntryIconPosition pos, Gdk.Event event)
187     {
188         if (pos == EntryIconPosition.PRIMARY)
189         {
190             print("Search entry icon pressed\n");
191         }
192         else
193         {
194             this.search_entry.set_text("");
195         }
196     }
197
198     private void search_entry_text_changed_cb()
199     {
200         this.filter.refilter();
201         redraw_id_card_widgets();
202
203         var has_text = this.search_entry.get_text_length() > 0;
204         this.search_entry.set_icon_sensitive(EntryIconPosition.PRIMARY, has_text);
205         this.search_entry.set_icon_sensitive(EntryIconPosition.SECONDARY, has_text);
206
207 //        this.vbox_right.set_visible(false);
208     }
209
210     private bool search_entry_key_press_event_cb(Gdk.EventKey e)
211     {
212         if(Gdk.keyval_name(e.keyval) == "Escape")
213             this.search_entry.set_text("");
214
215         // Continue processing this event, since the
216         // text entry functionality needs to see it too.
217         return false;
218     }
219
220     // private void update_password_cb()
221     // {
222     //     if (this.custom_vbox.current_idcard != null) {
223     //         var identity = this.custom_vbox.current_idcard.id_card;
224     //         var dialog = new AddPasswordDialog(identity, null);
225     //         var result = dialog.run();
226
227     //         switch (result) {
228     //         case ResponseType.OK:
229     //             identity.password = dialog.password;
230     //             identity.store_password = dialog.remember;
231     //             if (dialog.remember)
232     //                 identity.temporary = false;
233     //             identity = identities_manager.update_card(identity);
234     //             break;
235     //         default:
236     //             break;
237     //         }
238     //         dialog.destroy();
239     //     }
240     // }
241
242     private void load_id_cards() {
243         logger.trace("load_id_cards");
244
245         string current_idcard_nai = null;
246         if (this.custom_vbox.current_idcard != null) {
247             current_idcard_nai = custom_vbox.current_idcard.id_card.nai;
248             custom_vbox.current_idcard = null;
249         }
250         var children = this.custom_vbox.get_children();
251
252         custom_vbox.clear();
253         this.listmodel->clear();
254         LinkedList<IdCard> card_list = identities_manager.get_card_list() ;
255         if (card_list == null) {
256             return;
257         }
258
259         foreach (IdCard id_card in card_list) {
260             add_id_card_data(id_card);
261             IdCardWidget id_card_widget = add_id_card_widget(id_card);
262             if (id_card_widget.id_card.nai == current_idcard_nai) {
263                 // fill_details(id_card_widget.id_card);
264                 id_card_widget.expand();
265             }
266         }
267     }
268     
269     // private void fill_details(IdCard id_card)
270     // {
271     //     logger.trace("fill_details: id_card=%s".printf(id_card == null ? "null" : "non-null"));
272
273     //     if (id_card != null) {
274     //         if (id_card.display_name == IdCard.NO_IDENTITY) {
275     //             logger.trace("fill_details: Displaying title for NO_IDENTITY");
276     //             login_vbox.hide();
277     //             no_identity_title.show_all();
278     //         } else {
279     //             logger.trace("fill_details: Displaying details for selected card");
280     //             // this.issuer_entry.set_text(id_card.issuer);
281     //             // this.username_entry.set_text(id_card.username);
282     //             // this.password_entry.set_text(id_card.password ?? "");
283     //             this.remember_checkbutton.active = id_card.store_password;
284     //             no_identity_title.hide();
285     //             login_vbox.show_all();              
286     //         }
287
288     //         fill_services_vbox(id_card);
289     //     }
290     // }
291
292     // private void show_details(IdCard id_card)
293     // {
294     //     this.vbox_right.set_visible(!vbox_right.get_visible());
295
296     //     if (this.vbox_right.get_visible() == false)
297     //     {
298     //         this.resize(WINDOW_WIDTH, WINDOW_HEIGHT);
299     //     }
300     // }
301
302     // private void details_identity_cb(IdCardWidget id_card_widget)
303     // {
304     //     fill_details(id_card_widget.id_card);
305     //     show_details(id_card_widget.id_card);
306     // }
307
308     private IdCard get_id_card_data(AddIdentityDialog dialog)
309     {
310         var id_card = new IdCard();
311
312         id_card.display_name = dialog.display_name;
313         id_card.issuer = dialog.issuer;
314         id_card.username = dialog.username;
315         id_card.password = dialog.password;
316         id_card.store_password = dialog.store_password;
317         id_card.services = {};
318
319         return id_card;
320     }
321
322     private void add_id_card_data(IdCard id_card)
323     {
324         TreeIter   iter;
325         Gdk.Pixbuf pixbuf;
326         this.listmodel->append(out iter);
327         pixbuf = get_pixbuf(id_card);
328         listmodel->set(iter,
329                        Columns.IDCARD_COL, id_card,
330                        Columns.LOGO_COL, pixbuf,
331                        Columns.ISSUER_COL, id_card.issuer,
332                        Columns.USERNAME_COL, id_card.username,
333                        Columns.PASSWORD_COL, id_card.password);
334     }
335
336     private void remove_id_card_data(IdCard id_card)
337     {
338         TreeIter iter;
339         string issuer;
340
341         if (listmodel->get_iter_first(out iter))
342         {
343             do
344             {
345                 listmodel->get(iter,
346                                Columns.ISSUER_COL, out issuer);
347
348                 if (id_card.issuer == issuer)
349                 {
350                     listmodel->remove(iter);
351                     break;
352                 }
353             }
354             while (listmodel->iter_next(ref iter));
355         }
356     }
357
358     private IdCardWidget add_id_card_widget(IdCard id_card)
359     {
360         var id_card_widget = new IdCardWidget(id_card);
361         this.custom_vbox.add_id_card_widget(id_card_widget);
362 //        id_card_widget.details_id.connect(details_identity_cb);
363 //        id_card_widget.remove_id.connect(remove_identity_cb);
364 //        id_card_widget.send_id.connect((w) => send_identity_cb(w.id_card));
365         id_card_widget.expanded.connect(this.custom_vbox.receive_expanded_event);
366         // id_card_widget.expanded.connect((w) => fill_details(w.id_card));
367         return id_card_widget;
368     }
369
370     public bool add_identity(IdCard id_card, bool force_flat_file_store)
371     {
372         #if OS_MACOS
373         /* 
374          * TODO: We should have a confirmation dialog, but currently it will crash on Mac OS
375          * so for now we will install silently
376          */
377         var ret = Gtk.ResponseType.YES;
378         #else
379         Gtk.MessageDialog dialog;
380         IdCard? prev_id = identities_manager.find_id_card(id_card.nai, force_flat_file_store);
381         if (prev_id!=null) {
382             int flags = prev_id.Compare(id_card);
383             if (flags == 0) {
384                 return false; // no changes, no need to update
385             } else if ((flags & (1 << IdCard.DiffFlags.DISPLAY_NAME)) != 0) {
386                 dialog = new Gtk.MessageDialog(this,
387                                                Gtk.DialogFlags.DESTROY_WITH_PARENT,
388                                                Gtk.MessageType.QUESTION,
389                                                Gtk.ButtonsType.YES_NO,
390                                                _("Would you like to replace ID Card '%s' using nai '%s' with the new ID Card '%s'?"),
391                                                prev_id.display_name,
392                                                prev_id.nai,
393                                                id_card.display_name);
394             } else {
395                 dialog = new Gtk.MessageDialog(this,
396                                                Gtk.DialogFlags.DESTROY_WITH_PARENT,
397                                                Gtk.MessageType.QUESTION,
398                                                Gtk.ButtonsType.YES_NO,
399                                                _("Would you like to update ID Card '%s' using nai '%s'?"),
400                                                id_card.display_name,
401                                                id_card.nai);
402             }
403         } else {
404             dialog = new Gtk.MessageDialog(this,
405                                            Gtk.DialogFlags.DESTROY_WITH_PARENT,
406                                            Gtk.MessageType.QUESTION,
407                                            Gtk.ButtonsType.YES_NO,
408                                            _("Would you like to add '%s' ID Card to the ID Card Organizer?"),
409                                            id_card.display_name);
410         }
411         var ret = dialog.run();
412         dialog.destroy();
413         #endif
414
415         if (ret == Gtk.ResponseType.YES) {
416             this.identities_manager.add_card(id_card, force_flat_file_store);
417             return true;
418         }
419         return false;
420     }
421
422     private void add_identity_cb()
423     {
424         var dialog = new AddIdentityDialog();
425         int result = ResponseType.CANCEL;
426         while (!dialog.complete)
427             result = dialog.run();
428
429         switch (result) {
430         case ResponseType.OK:
431             this.identities_manager.add_card(get_id_card_data(dialog), false);
432             break;
433         default:
434             break;
435         }
436         dialog.destroy();
437     }
438
439     private void edit_identity_cb()
440     {
441         //!!TODO
442     }
443
444     private void remove_identity(IdCardWidget id_card_widget)
445     {
446         var id_card = id_card_widget.id_card;
447         this.custom_vbox.remove_id_card_widget(id_card_widget);
448
449         this.identities_manager.remove_card(id_card);
450     }
451
452     private void redraw_id_card_widgets()
453     {
454         logger.trace("redraw_id_card_widgets");
455
456         TreeIter iter;
457         IdCard id_card;
458
459         var children = this.custom_vbox.get_children();
460         this.custom_vbox.clear();
461
462         if (filter.get_iter_first(out iter))
463         {
464             do
465             {
466                 filter.get(iter,
467                            Columns.IDCARD_COL, out id_card);
468
469                 add_id_card_widget(id_card);
470             }
471             while (filter.iter_next(ref iter));
472         }
473     }
474
475     private void remove_identity_cb(IdCardWidget id_card_widget)
476     {
477         var id_card = id_card_widget.id_card;
478
479         var dialog = new MessageDialog(this,
480                                        DialogFlags.DESTROY_WITH_PARENT,
481                                        MessageType.QUESTION,
482                                        Gtk.ButtonsType.YES_NO,
483                                        _("Are you sure you want to delete %s ID Card?"), id_card.issuer);
484         var result = dialog.run();
485         switch (result) {
486         case ResponseType.YES:
487             remove_identity(id_card_widget);
488             break;
489         default:
490             break;
491         }
492         dialog.destroy();
493     }
494
495     public void set_prompting_service(string service)
496     {
497         prompting_service.set_label( _("Identity requested for service: %s").printf(service) );
498     }
499
500     public void queue_identity_request(IdentityRequest request)
501     {
502         if (this.request_queue.is_empty())
503         { /* setup widgets */
504             candidates = request.candidates;
505             filter.refilter();
506             redraw_id_card_widgets();
507             set_prompting_service(request.service);
508             make_visible();
509         }
510         this.request_queue.push_tail(request);
511     }
512
513
514     /** Makes the window visible, or at least, notifies the user that the window
515      * wants to be visible.
516      *
517      * This differs from show() in that show() does not guarantee that the 
518      * window will be moved to the foreground. Actually, neither does this
519      * method, because the user's settings and window manager may affect the
520      * behavior significantly.
521      */
522     public void make_visible()
523     {
524         set_urgency_hint(true);
525         present();
526     }
527
528     public IdCard check_add_password(IdCard identity, IdentityRequest request, IdentityManagerModel model)
529     {
530         IdCard retval = identity;
531         bool idcard_has_pw = (identity.password != null) && (identity.password != "");
532         bool request_has_pw = (request.password != null) && (request.password != "");
533         if ((!idcard_has_pw) && (!identity.IsNoIdentity())) {
534             if (request_has_pw) {
535                 identity.password = request.password;
536                 retval = model.update_card(identity);
537             } else {
538                 var dialog = new AddPasswordDialog(identity, request);
539                 var result = dialog.run();
540
541                 switch (result) {
542                 case ResponseType.OK:
543                     identity.password = dialog.password;
544                     identity.store_password = dialog.remember;
545                     if (dialog.remember)
546                         identity.temporary = false;
547                     retval = model.update_card(identity);
548                     break;
549                 default:
550                     identity = null;
551                     break;
552                 }
553                 dialog.destroy();
554             }
555         }
556         return retval;
557     }
558
559     private void send_identity_cb(IdCard id)
560     {
561         IdCard identity = id;
562         return_if_fail(request_queue.length > 0);
563
564         candidates = null;
565         var request = this.request_queue.pop_head();
566         identity = check_add_password(identity, request, identities_manager);
567         if (this.request_queue.is_empty())
568         {
569             candidates = null;
570             prompting_service.set_label(_(""));
571             if (!parent_app.explicitly_launched) {
572 // The following occasionally causes the app to exit without sending the dbus
573 // reply, so for now we just don't exit
574 //                Gtk.main_quit();
575 // just hide instead
576                 this.hide();
577             }
578         } else {
579             IdentityRequest next = this.request_queue.peek_head();
580             candidates = next.candidates;
581             set_prompting_service(next.service);
582         }
583         filter.refilter();
584         redraw_id_card_widgets();
585
586         if ((identity != null) && (!identity.IsNoIdentity()))
587             parent_app.default_id_card = identity;
588
589         request.return_identity(identity);
590     }
591
592     private void label_make_bold(Label label)
593     {
594         var font_desc = new Pango.FontDescription();
595
596         font_desc.set_weight(Pango.Weight.BOLD);
597
598         /* This will only affect the weight of the font, the rest is
599          * from the current state of the widget, which comes from the
600          * theme or user prefs, since the font desc only has the
601          * weight flag turned on.
602          */
603         label.modify_font(font_desc);
604     }
605
606 //     private void fill_services_vbox(IdCard id_card)
607 //     {
608 //         logger.trace("fill_services_vbox");
609
610 //         var children = this.services_internal_vbox.get_children();
611 //         foreach (var widget in children) {
612 //             services_internal_vbox.remove(widget);
613 //         }
614
615 //         int i = 0;
616 //         var n_rows = id_card.services.length;
617
618 //         var services_table = new Table(n_rows, 2, false);
619 //         services_table.set_col_spacings(10);
620 //         services_table.set_row_spacings(10);
621 //         this.services_internal_vbox.pack_start(services_table, true, false, 0);
622         
623 //         service_button_map.remove_all();
624
625 //         foreach (string service in id_card.services)
626 //         {
627 //             var label = new Label(service);
628 //             label.set_alignment(0, (float) 0.5);
629 // #if VALA_0_12
630 //             var remove_button = new Button.from_stock(Stock.REMOVE);
631 // #else
632 //             var remove_button = new Button.from_stock(STOCK_REMOVE);
633 // #endif
634
635
636 //             service_button_map.insert(remove_button, service);
637             
638 //             remove_button.clicked.connect((remove_button) =>
639 //                 {
640 //                     var candidate = service_button_map.lookup(remove_button);
641 //                     if (candidate == null)
642 //                         return;
643 //                     var dialog = new Gtk.MessageDialog(this,
644 //                                                        Gtk.DialogFlags.DESTROY_WITH_PARENT,
645 //                                                        Gtk.MessageType.QUESTION,
646 //                                                        Gtk.ButtonsType.YES_NO,
647 //                                                        _("Are you sure you want to stop '%s' ID Card from being used with %s?"),
648 //                                                        custom_vbox.current_idcard.id_card.display_name,
649 //                                                        candidate);
650 //                     var ret = dialog.run();
651 //                     dialog.hide();
652               
653 //                     if (ret == Gtk.ResponseType.YES)
654 //                     {
655 //                         IdCard idcard = custom_vbox.current_idcard.id_card;
656 //                         if (idcard != null) {
657 //                             SList<string> services = new SList<string>();
658                 
659 //                             foreach (string srv in idcard.services)
660 //                             {
661 //                                 if (srv == candidate)
662 //                                     continue;
663 //                                 services.append(srv);
664 //                             }
665                 
666 //                             idcard.services = new string[services.length()];
667 //                             for (int j = 0; j < idcard.services.length; j++)
668 //                             {
669 //                                 idcard.services[j] = services.nth_data(j);
670 //                             }
671                 
672 //                             identities_manager.update_card(idcard);
673 //                         }
674 //                     }
675               
676 //                 });
677 //             services_table.attach_defaults(label, 0, 1, i, i+1);
678 //             services_table.attach_defaults(remove_button, 1, 2, i, i+1);
679 //             i++;
680 //         }
681
682 //         services_vbox.show_all();
683 //     }
684
685     private void on_about_action()
686     {
687         string copyright = "Copyright 2011, 2016 JANET";
688
689         string license =
690         """
691 Copyright (c) 2011, 2016 JANET(UK)
692 All rights reserved.
693
694 Redistribution and use in source and binary forms, with or without
695 modification, are permitted provided that the following conditions
696 are met:
697
698 1. Redistributions of source code must retain the above copyright
699    notice, this list of conditions and the following disclaimer.
700
701 2. Redistributions in binary form must reproduce the above copyright
702    notice, this list of conditions and the following disclaimer in the
703    documentation and/or other materials provided with the distribution.
704
705 3. Neither the name of JANET(UK) nor the names of its contributors
706    may be used to endorse or promote products derived from this software
707    without specific prior written permission.
708
709 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"
710 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
711 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
712 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
713 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
714 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
715 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
716 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
717 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
718 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
719 SUCH DAMAGE.
720 """;
721
722         Gtk.show_about_dialog(this,
723                               "comments", _("Moonshot project UI"),
724                               "copyright", copyright,
725                               "website", Config.PACKAGE_URL,
726                               "version", Config.PACKAGE_VERSION,
727                               "license", license,
728                               "website-label", _("Visit the Moonshot project web site"),
729 //                              "authors", authors,
730                               "translator-credits", _("translator-credits"),
731                               null
732             );
733     }
734
735     private Gtk.ActionEntry[] create_actions() {
736         Gtk.ActionEntry[] actions = new Gtk.ActionEntry[0];
737
738         // Gtk.ActionEntry add = { "AddIdCardAction",
739         //                         #if VALA_0_12
740         //                         Stock.ADD,
741         //                         #else
742         //                         STOCK_ADD,
743         //                         #endif
744         //                         N_("Add ID Card"),
745         //                         null,
746         //                         N_("Add a new ID Card"),
747         //                         add_identity_manual_cb };
748         // actions += add;
749
750         Gtk.ActionEntry helpmenu = { "HelpMenuAction",
751                                      null,
752                                      N_("_Help"),
753                                      null, null, null };
754         actions += helpmenu;
755         Gtk.ActionEntry about = { "AboutAction",
756                                   #if VALA_0_12
757                                   Stock.ABOUT,
758                                   #else
759                                   STOCK_ABOUT,
760                                   #endif
761                                   N_("About"),
762                                   null,
763                                   N_("About this application"),
764                                   on_about_action };
765         actions += about;
766
767         return actions;
768     }
769
770
771     private void create_ui_manager()
772     {
773         Gtk.ActionGroup action_group = new Gtk.ActionGroup("GeneralActionGroup");
774         action_group.add_actions(create_actions(), this);
775         ui_manager.insert_action_group(action_group, 0);
776         try
777         {
778             ui_manager.add_ui_from_string(menu_layout, -1);
779         }
780         catch (Error e)
781         {
782             stderr.printf("%s\n", e.message);
783             logger.error("create_ui_manager: Caught error: " + e.message);
784         }
785         ui_manager.ensure_update();
786     }
787
788     private void build_ui()
789     {
790         create_ui_manager();
791
792         this.search_entry = new Entry();
793
794         set_atk_name_description(search_entry, _("Search entry"), _("Search for a specific ID Card"));
795         this.search_entry.set_icon_from_pixbuf(EntryIconPosition.PRIMARY,
796                                                find_icon_sized("edit-find", Gtk.IconSize.MENU));
797 //                                                find_icon_sized("edit-find-symbolic", Gtk.IconSize.MENU));
798         this.search_entry.set_icon_tooltip_text(EntryIconPosition.PRIMARY,
799                                                 _("Search for an identity or service"));
800         this.search_entry.set_icon_sensitive(EntryIconPosition.PRIMARY, false);
801
802         this.search_entry.set_icon_from_pixbuf(EntryIconPosition.SECONDARY,
803                                                find_icon_sized("process-stop", Gtk.IconSize.MENU));
804 //                                                find_icon_sized("edit-clear-symbolic", Gtk.IconSize.MENU));
805         this.search_entry.set_icon_tooltip_text(EntryIconPosition.SECONDARY,
806                                                 _("Clear the current search"));
807         this.search_entry.set_icon_sensitive(EntryIconPosition.SECONDARY, false);
808
809
810         this.search_entry.icon_press.connect(search_entry_icon_press_cb);
811         this.search_entry.notify["text"].connect(search_entry_text_changed_cb);
812         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
813         this.search_entry.set_width_chars(30);
814
815
816         this.custom_vbox = new CustomVBox(this, false, 2);
817
818         var viewport = new Viewport(null, null);
819         viewport.set_border_width(2);
820         viewport.set_shadow_type(ShadowType.NONE);
821         viewport.add(custom_vbox);
822         var id_scrollwin = new ScrolledWindow(null, null);
823         id_scrollwin.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
824         id_scrollwin.set_shadow_type(ShadowType.IN);
825         id_scrollwin.add_with_viewport(viewport);
826         this.prompting_service = new Label(_(""));
827         // left-align
828         prompting_service.set_alignment(0, (float )0.5);
829
830         var vbox_left = new VBox(false, 0);
831         // vbox_left.pack_start(search_entry, false, false, 6);
832         // vbox_left.pack_start(id_scrollwin, true, true, 6);
833
834         var search_hbox = new HBox(false, 6);
835         var search_label = new Label(_("Search:"));
836         search_label.set_alignment(1, (float) 0.5);
837         set_atk_relation(search_label, search_entry, Atk.RelationType.LABEL_FOR);
838         search_hbox.pack_end(search_entry, false, false, 0);
839         search_hbox.pack_end(search_label, false, false, 6);
840
841         var full_search_label = new Label(_("Search for an identity or service"));
842         full_search_label.set_alignment(1, 1);
843         var search_vbox = new VBox(false, 4);
844         search_vbox.pack_start(search_hbox, false, false, 0);
845         search_vbox.pack_start(full_search_label, false, false, 0);
846
847         var inner_left_vbox = new VBox(false, 6);
848         inner_left_vbox.pack_start(search_vbox, false, false, 6);
849         inner_left_vbox.pack_start(id_scrollwin, true, true, 0);
850
851         var id_and_button_box = new HBox(false, 6);
852         id_and_button_box.pack_start(inner_left_vbox, true, true, 6);
853         vbox_left.pack_start(id_and_button_box, true, true, 0);
854         vbox_left.pack_start(prompting_service, false, false, 6);
855         vbox_left.set_size_request(WINDOW_WIDTH, 0);
856
857         this.no_identity_title = new Label(_("No Identity: Send this identity to services which should not use Moonshot"));
858         no_identity_title.set_alignment(0, (float ) 0.5);
859         no_identity_title.set_line_wrap(true);
860         no_identity_title.show();
861
862         var login_vbox_title = new Label(_("Login: "));
863         label_make_bold(login_vbox_title);
864         login_vbox_title.set_alignment(0, (float) 0.5);
865         // var issuer_label = new Label(_("Issuer:"));
866         // issuer_label.set_alignment(1, (float) 0.5);
867         // this.issuer_entry = new Entry();
868         // issuer_entry.set_can_focus(false);
869         // var username_label = new Label(_("Username:"));
870         // username_label.set_alignment(1, (float) 0.5);
871         // this.username_entry = new Entry();
872         // username_entry.set_can_focus(false);
873         // var password_label = new Label(_("Password:"));
874         // password_label.set_alignment(1, (float) 0.5);
875         // this.password_entry = new Entry();
876         // password_entry.set_invisible_char('*');
877         // password_entry.set_visibility(false);
878         // password_entry.set_sensitive(false);
879         // this.remember_checkbutton = new CheckButton.with_label(_("Remember password"));
880         // remember_checkbutton.set_sensitive(false);
881
882         // set_atk_relation(issuer_label, issuer_entry, Atk.RelationType.LABEL_FOR);
883         // set_atk_relation(username_label, username_entry, Atk.RelationType.LABEL_FOR);
884         // set_atk_relation(password_entry, password_entry, Atk.RelationType.LABEL_FOR);
885
886         // // Create the login_vbox. This starts off hidden, because the first card we
887         // // display, by default, is NO_IDENTITY.
888         // var login_table = new Table(5, 2, false);
889         // login_table.set_col_spacings(10);
890         // login_table.set_row_spacings(10);
891         // login_table.attach_defaults(issuer_label, 0, 1, 0, 1);
892         // login_table.attach_defaults(issuer_entry, 1, 2, 0, 1);
893         // login_table.attach_defaults(username_label, 0, 1, 1, 2);
894         // login_table.attach_defaults(username_entry, 1, 2, 1, 2);
895         // login_table.attach_defaults(password_label, 0, 1, 2, 3);
896         // login_table.attach_defaults(password_entry, 1, 2, 2, 3);
897         // login_table.attach_defaults(remember_checkbutton,  1, 2, 3, 4);
898         // login_table.attach_defaults(update_password_button, 0, 1, 4, 5);
899         // var login_vbox_alignment = new Alignment(0, 0, 0, 0);
900         // login_vbox_alignment.set_padding(0, 0, 12, 0);
901         // login_vbox_alignment.add(login_table);
902         // this.login_vbox = new VBox(false, 6);
903         // login_vbox.pack_start(login_vbox_title, false, true, 0);
904         // login_vbox.pack_start(login_vbox_alignment, false, true, 0);
905         // login_vbox.hide();
906
907         // var services_vbox_title = new Label(_("Services:"));
908         // label_make_bold(services_vbox_title);
909         // services_vbox_title.set_alignment(0, (float) 0.5);
910         
911         // this.services_internal_vbox = new VBox(true, 6);
912
913         // var services_vbox_alignment = new Alignment(0, 0, 0, 1);
914         // services_vbox_alignment.set_padding(6, 6, 6, 6);
915         // services_vbox_alignment.add(services_internal_vbox);
916         // services_vscroll = new ScrolledWindow(null, null);
917         // services_vscroll.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
918         // services_vscroll.set_shadow_type(ShadowType.IN);
919         // services_vscroll.add_with_viewport(services_vbox_alignment);
920
921         // services_vbox = new VBox(false, 6);
922         this.vbox_right = new VBox(false, 6);
923         // services_vbox.pack_start(services_vbox_title, false, false, 0);
924         // services_vbox.pack_start(services_vscroll, true, true, 0);
925
926         // vbox_right.pack_start(no_identity_title, true, false, 0);
927         // vbox_right.pack_start(login_vbox, false, false, 0);
928         // vbox_right.pack_start(services_vbox, true, true, 0);
929
930         var add_button = new Button.with_label(_("Add"));
931         add_button.clicked.connect((w) => {add_identity_cb();});
932
933         var edit_button = new Button.with_label(_("Edit"));
934         edit_button.clicked.connect((w) => {edit_identity_cb();});
935
936         var remove_button = new Button.with_label(_("Remove"));
937         remove_button.clicked.connect((w) => {remove_identity_cb(custom_vbox.current_idcard);});
938
939         var send_button = new Button.with_label(_("Send"));
940         send_button.clicked.connect((w) => {send_identity_cb(custom_vbox.current_idcard.id_card);});
941
942
943         var empty_box = new VBox(false, 0);
944         empty_box.set_size_request(0, 0);
945         vbox_right.pack_start(empty_box, false, false, 14);
946         vbox_right.pack_start(add_button, false, false, 6);
947         vbox_right.pack_start(edit_button, false, false, 6);
948         vbox_right.pack_start(remove_button, false, false, 6);
949         vbox_right.pack_start(send_button, false, false, 24);
950
951         //var hbox = new HBox(false, 12);
952         // hbox.pack_start(vbox_left, true, true, 0);
953         // hbox.pack_start(vbox_right, false, false, 0);
954         id_and_button_box.pack_start(vbox_right, false, false, 0);
955         var main_vbox = new VBox(false, 0);
956         main_vbox.set_border_width(12);
957
958 #if OS_MACOS
959         // hide the  File | Quit menu item which is now on the Mac Menu
960         Gtk.Widget quit_item =  this.ui_manager.get_widget("/MenuBar/FileMenu/Quit");
961         quit_item.hide();
962         
963         Gtk.MenuShell menushell = this.ui_manager.get_widget("/MenuBar") as Gtk.MenuShell;
964         osxApp.set_menu_bar(menushell);
965         osxApp.set_use_quartz_accelerators(true);
966         osxApp.sync_menu_bar();
967         osxApp.ready();
968 #else
969         var menubar = this.ui_manager.get_widget("/MenuBar");
970         main_vbox.pack_start(menubar, false, false, 0);
971 #endif
972         main_vbox.pack_start(vbox_left, true, true, 0);
973         add(main_vbox);
974         main_vbox.show_all();
975     } 
976
977     private void set_atk_name_description(Widget widget, string name, string description)
978     {
979         var atk_widget = widget.get_accessible();
980
981         atk_widget.set_name(name);
982         atk_widget.set_description(description);
983     }
984
985     private void connect_signals()
986     {
987         this.destroy.connect(Gtk.main_quit);
988         this.identities_manager.card_list_changed.connect(this.on_card_list_changed);
989     }
990
991     private static void set_atk_relation(Widget widget, Widget target_widget, Atk.RelationType relationship)
992     {
993         var atk_widget = widget.get_accessible();
994         var atk_target_widget = target_widget.get_accessible();
995
996         atk_widget.add_relationship(relationship, atk_target_widget);
997     }
998 }