Cleaning up compilation warnings
[moonshot-ui.git] / src / moonshot-identity-management-view.vala
1 /*
2  * Copyright (c) 2011-2016, 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     // The latest year in which Moonshot sources were modified.
39     private static int LATEST_EDIT_YEAR = 2016;
40
41     public static Gdk.Color white = make_color(65535, 65535, 65535);
42
43     private const int WINDOW_WIDTH = 700;
44     private const int WINDOW_HEIGHT = 500;
45     protected IdentityManagerApp parent_app;
46     #if OS_MACOS
47         public OSXApplication osxApp;
48     #endif
49     private UIManager ui_manager = new UIManager();
50     private Entry search_entry;
51     private CustomVBox custom_vbox;
52     private VBox service_prompt_vbox;
53     private Button edit_button;
54     private Button remove_button;
55
56     private Button send_button;
57     
58     private Gtk.ListStore* listmodel;
59     private TreeModelFilter filter;
60
61     internal IdentityManagerModel identities_manager;
62     private unowned SList<IdCard>    candidates;
63
64     private GLib.Queue<IdentityRequest> request_queue;
65
66     internal CheckButton remember_identity_binding = null;
67
68     private IdCard selected_idcard = null;
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         this.title = "Moonshot Identity Selector";
95         this.set_position(WindowPosition.CENTER);
96         set_default_size(WINDOW_WIDTH, WINDOW_HEIGHT);
97         build_ui();
98         setup_list_model(); 
99         load_id_cards(); 
100         connect_signals();
101     }
102     
103     private void on_card_list_changed() {
104         logger.trace("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.size > 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_text_changed_cb()
187     {
188         this.filter.refilter();
189         redraw_id_card_widgets();
190     }
191
192     private bool search_entry_key_press_event_cb(Gdk.EventKey e)
193     {
194         if(Gdk.keyval_name(e.keyval) == "Escape")
195             this.search_entry.set_text("");
196
197         // Continue processing this event, since the
198         // text entry functionality needs to see it too.
199         return false;
200     }
201
202     private void load_id_cards() {
203         logger.trace("load_id_cards");
204
205         custom_vbox.clear();
206         this.listmodel->clear();
207         LinkedList<IdCard> card_list = identities_manager.get_card_list() ;
208         if (card_list == null) {
209             return;
210         }
211
212         foreach (IdCard id_card in card_list) {
213             logger.trace(@"load_id_cards: Loading card with display name '$(id_card.display_name)'");
214             add_id_card_data(id_card);
215             add_id_card_widget(id_card);
216         }
217     }
218     
219     private IdCard update_id_card_data(IdentityDialog dialog, IdCard id_card)
220     {
221         id_card.display_name = dialog.display_name;
222         id_card.issuer = dialog.issuer;
223         id_card.username = dialog.username;
224         id_card.password = dialog.password;
225         id_card.store_password = dialog.store_password;
226
227         id_card.update_services_from_list(dialog.get_services());
228
229         if (dialog.clear_trust_anchor) {
230             id_card.clear_trust_anchor();
231         }
232
233         return id_card;
234     }
235
236     private void add_id_card_data(IdCard id_card)
237     {
238         TreeIter   iter;
239         Gdk.Pixbuf pixbuf;
240         this.listmodel->append(out iter);
241         pixbuf = get_pixbuf(id_card);
242         listmodel->set(iter,
243                        Columns.IDCARD_COL, id_card,
244                        Columns.LOGO_COL, pixbuf,
245                        Columns.ISSUER_COL, id_card.issuer,
246                        Columns.USERNAME_COL, id_card.username,
247                        Columns.PASSWORD_COL, id_card.password);
248     }
249
250     // private void remove_id_card_data(IdCard id_card)
251     // {
252     //     TreeIter iter;
253     //     string issuer;
254
255     //     if (listmodel->get_iter_first(out iter))
256     //     {
257     //         do
258     //         {
259     //             listmodel->get(iter,
260     //                            Columns.ISSUER_COL, out issuer);
261
262     //             if (id_card.issuer == issuer)
263     //             {
264     //                 listmodel->remove(iter);
265     //                 break;
266     //             }
267     //         }
268     //         while (listmodel->iter_next(ref iter));
269     //     }
270     // }
271
272     private IdCardWidget add_id_card_widget(IdCard id_card)
273     {
274         logger.trace("add_id_card_widget: id_card.nai='%s'; selected nai='%s'"
275                      .printf(id_card.nai, 
276                              this.selected_idcard == null ? "[null selection]" : this.selected_idcard.nai));
277
278
279         var id_card_widget = new IdCardWidget(id_card, this);
280         this.custom_vbox.add_id_card_widget(id_card_widget);
281         id_card_widget.expanded.connect(this.widget_selected_cb);
282         id_card_widget.collapsed.connect(this.widget_unselected_cb);
283
284         if (this.selected_idcard != null && this.selected_idcard.nai == id_card.nai) {
285             logger.trace(@"add_id_card_widget: Expanding selected idcard widget");
286             id_card_widget.expand();
287         }
288         return id_card_widget;
289     }
290
291     private void widget_selected_cb(IdCardWidget id_card_widget)
292     {
293         logger.trace(@"widget_selected_cb: id_card_widget.id_card.display_name='$(id_card_widget.id_card.display_name)'");
294
295         this.selected_idcard = id_card_widget.id_card;
296         bool allow_removes = !id_card_widget.id_card.is_no_identity();
297         this.remove_button.set_sensitive(allow_removes);
298         this.edit_button.set_sensitive(true);
299         this.custom_vbox.receive_expanded_event(id_card_widget);
300
301         if (this.selection_in_progress())
302              this.send_button.set_sensitive(true);
303     }
304
305     private void widget_unselected_cb(IdCardWidget id_card_widget)
306     {
307         logger.trace(@"widget_unselected_cb: id_card_widget.id_card.display_name='$(id_card_widget.id_card.display_name)'");
308
309         this.selected_idcard = null;
310         this.remove_button.set_sensitive(false);
311         this.edit_button.set_sensitive(false);
312         this.custom_vbox.receive_collapsed_event(id_card_widget);
313
314         this.send_button.set_sensitive(false);
315     }
316
317     public bool add_identity(IdCard id_card, bool force_flat_file_store)
318     {
319         #if OS_MACOS
320         /* 
321          * TODO: We should have a confirmation dialog, but currently it will crash on Mac OS
322          * so for now we will install silently
323          */
324         var ret = Gtk.ResponseType.YES;
325         #else
326         Gtk.MessageDialog dialog;
327         IdCard? prev_id = identities_manager.find_id_card(id_card.nai, force_flat_file_store);
328         logger.trace("add_identity(flat=%s, card='%s'): find_id_card returned %s"
329                      .printf(force_flat_file_store.to_string(), id_card.display_name, (prev_id != null ? prev_id.display_name : "null")));
330         if (prev_id!=null) {
331             int flags = prev_id.Compare(id_card);
332             logger.trace("add_identity: compare returned " + flags.to_string());
333             if (flags == 0) {
334                 return false; // no changes, no need to update
335             } else if ((flags & (1 << IdCard.DiffFlags.DISPLAY_NAME)) != 0) {
336                 dialog = new Gtk.MessageDialog(this,
337                                                Gtk.DialogFlags.DESTROY_WITH_PARENT,
338                                                Gtk.MessageType.QUESTION,
339                                                Gtk.ButtonsType.YES_NO,
340                                                _("Would you like to replace ID Card '%s' using nai '%s' with the new ID Card '%s'?"),
341                                                prev_id.display_name,
342                                                prev_id.nai,
343                                                id_card.display_name);
344             } else {
345                 dialog = new Gtk.MessageDialog(this,
346                                                Gtk.DialogFlags.DESTROY_WITH_PARENT,
347                                                Gtk.MessageType.QUESTION,
348                                                Gtk.ButtonsType.YES_NO,
349                                                _("Would you like to update ID Card '%s' using nai '%s'?"),
350                                                id_card.display_name,
351                                                id_card.nai);
352             }
353         } else {
354             dialog = new Gtk.MessageDialog(this,
355                                            Gtk.DialogFlags.DESTROY_WITH_PARENT,
356                                            Gtk.MessageType.QUESTION,
357                                            Gtk.ButtonsType.YES_NO,
358                                            _("Would you like to add '%s' ID Card to the ID Card Organizer?"),
359                                            id_card.display_name);
360         }
361         var ret = dialog.run();
362         dialog.destroy();
363         #endif
364
365         if (ret == Gtk.ResponseType.YES) {
366             this.identities_manager.add_card(id_card, force_flat_file_store);
367             return true;
368         }
369         return false;
370     }
371
372     private void add_identity_cb()
373     {
374         var dialog = new IdentityDialog(this);
375         int result = ResponseType.CANCEL;
376         while (!dialog.complete)
377             result = dialog.run();
378
379         switch (result) {
380         case ResponseType.OK:
381             this.identities_manager.add_card(update_id_card_data(dialog, new IdCard()), false);
382             break;
383         default:
384             break;
385         }
386         dialog.destroy();
387     }
388
389     private void edit_identity_cb(IdCard card)
390     {
391         var dialog = new IdentityDialog.with_idcard(card, _("Edit Identity"), this);
392         int result = ResponseType.CANCEL;
393         while (!dialog.complete)
394             result = dialog.run();
395
396         switch (result) {
397         case ResponseType.OK:
398             this.identities_manager.update_card(update_id_card_data(dialog, card));
399             break;
400         default:
401             break;
402         }
403         dialog.destroy();
404     }
405
406     private void remove_identity(IdCard id_card)
407     {
408         logger.trace(@"remove_identity: id_card.display_name='$(id_card.display_name)'");
409         if (id_card != this.selected_idcard) {
410             logger.error("remove_identity: id_card != this.selected_idcard!");
411         }
412
413         this.selected_idcard = null;
414         this.identities_manager.remove_card(id_card);
415
416         // Nothing is selected, so disable buttons
417         this.edit_button.set_sensitive(false);
418         this.remove_button.set_sensitive(false);
419         this.send_button.set_sensitive(false);
420     }
421
422     private void redraw_id_card_widgets()
423     {
424         TreeIter iter;
425         IdCard id_card;
426
427         this.custom_vbox.clear();
428
429         if (filter.get_iter_first(out iter))
430         {
431             do
432             {
433                 filter.get(iter,
434                            Columns.IDCARD_COL, out id_card);
435
436                 add_id_card_widget(id_card);
437             }
438             while (filter.iter_next(ref iter));
439         }
440     }
441
442     private void remove_identity_cb(IdCard id_card)
443     {
444         bool remove = WarningDialog.confirm(this, 
445                                             Markup.printf_escaped(
446                                                 "<span font-weight='heavy'>You are about to remove the identity '%s'.</span>",
447                                                 id_card.display_name)
448                                             + "\n\nAre you sure you want to do this?",
449                                             "delete_idcard");
450         if (remove) 
451             remove_identity(id_card);
452     }
453
454     private void set_prompting_service(string service)
455     {
456         clear_selection_prompts();
457
458         var prompting_service = new Label(_("Identity requested for service:\n%s").printf(service));
459         prompting_service.set_line_wrap(true);
460
461         // left-align
462         prompting_service.set_alignment(0, (float )0.5);
463
464         var selection_prompt = new Label(_("Select your identity:"));
465         selection_prompt.set_alignment(0, 1);
466
467         this.service_prompt_vbox.pack_start(prompting_service, false, false, 12);
468         this.service_prompt_vbox.pack_start(selection_prompt, false, false, 2);
469         this.service_prompt_vbox.show_all();
470     }
471
472     private void clear_selection_prompts()
473     {
474         var list = service_prompt_vbox.get_children();
475         foreach (Widget w in list)
476         {
477             service_prompt_vbox.remove(w);
478         }
479     }
480
481
482     public void queue_identity_request(IdentityRequest request)
483     {
484         bool queue_was_empty = !this.selection_in_progress();
485         this.request_queue.push_tail(request);
486
487         if (queue_was_empty)
488         { /* setup widgets */
489             candidates = request.candidates;
490             filter.refilter();
491             redraw_id_card_widgets();
492             set_prompting_service(request.service);
493             remember_identity_binding.show();
494
495             if (this.selected_idcard != null
496                 && this.custom_vbox.find_idcard_widget(this.selected_idcard) != null) {
497                 // A widget is already selected, and has not been filtered out of the display via search
498                 send_button.set_sensitive(true);
499             }
500
501             make_visible();
502         }
503     }
504
505
506     /** Makes the window visible, or at least, notifies the user that the window
507      * wants to be visible.
508      *
509      * This differs from show() in that show() does not guarantee that the 
510      * window will be moved to the foreground. Actually, neither does this
511      * method, because the user's settings and window manager may affect the
512      * behavior significantly.
513      */
514     public void make_visible()
515     {
516         set_urgency_hint(true);
517         present();
518     }
519
520     public IdCard check_add_password(IdCard identity, IdentityRequest request, IdentityManagerModel model)
521     {
522         logger.trace(@"check_add_password");
523         IdCard retval = identity;
524         bool idcard_has_pw = (identity.password != null) && (identity.password != "");
525         bool request_has_pw = (request.password != null) && (request.password != "");
526         if ((!idcard_has_pw) && (!identity.is_no_identity())) {
527             if (request_has_pw) {
528                 identity.password = request.password;
529                 retval = model.update_card(identity);
530             } else {
531                 var dialog = new AddPasswordDialog(identity, request);
532                 var result = dialog.run();
533
534                 switch (result) {
535                 case ResponseType.OK:
536                     identity.password = dialog.password;
537                     identity.store_password = dialog.remember;
538                     if (dialog.remember)
539                         identity.temporary = false;
540                     retval = model.update_card(identity);
541                     break;
542                 default:
543                     identity = null;
544                     break;
545                 }
546                 dialog.destroy();
547             }
548         }
549         return retval;
550     }
551
552     private void send_identity_cb(IdCard id)
553     {
554         return_if_fail(this.selection_in_progress());
555
556         if (!check_and_confirm_trust_anchor(id)) {
557             // Allow user to pick again
558             return;
559         }
560
561         var request = this.request_queue.pop_head();
562         var identity = check_add_password(id, request, identities_manager);
563         send_button.set_sensitive(false);
564
565         candidates = null;
566       
567         if (!this.selection_in_progress())
568         {
569             candidates = null;
570             clear_selection_prompts();
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.is_no_identity()))
587             parent_app.default_id_card = identity;
588
589         request.return_identity(identity, remember_identity_binding.active);
590
591         remember_identity_binding.active = false;
592         remember_identity_binding.hide();
593     }
594
595     private bool check_and_confirm_trust_anchor(IdCard id)
596     {
597         if (!id.trust_anchor.is_empty() && id.trust_anchor.get_anchor_type() == TrustAnchor.TrustAnchorType.SERVER_CERT) {
598             if (!id.trust_anchor.user_verified) {
599
600                 bool ret = false;
601                 int result = ResponseType.CANCEL;
602                 var dialog = new TrustAnchorDialog(id, this);
603                 while (!dialog.complete)
604                     result = dialog.run();
605
606                 switch (result) {
607                 case ResponseType.OK:
608                     id.trust_anchor.user_verified = true;
609                     ret = true;
610                     break;
611                 default:
612                     break;
613                 }
614
615                 dialog.destroy();
616                 return ret;
617             }
618         }
619         return true;
620     }
621
622     private void on_about_action()
623     {
624         string copyright = "Copyright (c) 2011, %d JANET".printf(LATEST_EDIT_YEAR);
625
626         string license =
627         """
628 Copyright (c) 2011, %d JANET(UK)
629 All rights reserved.
630
631 Redistribution and use in source and binary forms, with or without
632 modification, are permitted provided that the following conditions
633 are met:
634
635 1. Redistributions of source code must retain the above copyright
636    notice, this list of conditions and the following disclaimer.
637
638 2. Redistributions in binary form must reproduce the above copyright
639    notice, this list of conditions and the following disclaimer in the
640    documentation and/or other materials provided with the distribution.
641
642 3. Neither the name of JANET(UK) nor the names of its contributors
643    may be used to endorse or promote products derived from this software
644    without specific prior written permission.
645
646 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"
647 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
648 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
649 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
650 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
651 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
652 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
653 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
654 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
655 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
656 SUCH DAMAGE.
657 """.printf(LATEST_EDIT_YEAR);
658
659         AboutDialog about = new AboutDialog();
660
661         about.set_comments(_("Moonshot project UI"));
662         about.set_copyright(copyright);
663         about.set_website(Config.PACKAGE_URL);
664         about.set_website_label(_("Visit the Moonshot project web site"));
665
666         // Note: The package version is configured at the top of moonshot/ui/configure.ac
667         about.set_version(Config.PACKAGE_VERSION);
668         about.set_license(license);
669         about.set_modal(true);
670         about.set_transient_for(this);
671         about.response.connect((a, b) => {about.destroy();});
672         about.modify_bg(StateType.NORMAL, white);
673         
674         about.run();
675     }
676
677     private Gtk.ActionEntry[] create_actions() {
678         Gtk.ActionEntry[] actions = new Gtk.ActionEntry[0];
679
680         Gtk.ActionEntry helpmenu = { "HelpMenuAction",
681                                      null,
682                                      N_("_Help"),
683                                      null, null, null };
684         actions += helpmenu;
685         Gtk.ActionEntry about = { "AboutAction",
686                                   #if VALA_0_12
687                                   Stock.ABOUT,
688                                   #else
689                                   STOCK_ABOUT,
690                                   #endif
691                                   N_("About"),
692                                   null,
693                                   N_("About this application"),
694                                   on_about_action };
695         actions += about;
696
697         return actions;
698     }
699
700
701     private void create_ui_manager()
702     {
703         Gtk.ActionGroup action_group = new Gtk.ActionGroup("GeneralActionGroup");
704         action_group.add_actions(create_actions(), this);
705         ui_manager.insert_action_group(action_group, 0);
706         try
707         {
708             ui_manager.add_ui_from_string(menu_layout, -1);
709         }
710         catch (Error e)
711         {
712             stderr.printf("%s\n", e.message);
713             logger.error("create_ui_manager: Caught error: " + e.message);
714         }
715         ui_manager.ensure_update();
716     }
717
718     private void build_ui()
719     {
720         // Note: On Debian7/Gtk+2, the menu bar remains gray. This doesn't happen on Debian8/Gtk+3.
721         this.modify_bg(StateType.NORMAL, white);
722
723         create_ui_manager();
724
725         int num_rows = 18;
726         int num_cols = 8;
727         int button_width = 1;
728
729         Table top_table = new Table(num_rows, 10, false);
730         top_table.set_border_width(12);
731
732         AttachOptions fill_and_expand = AttachOptions.EXPAND | AttachOptions.FILL;
733         AttachOptions fill = AttachOptions.FILL;
734         int row = 0;
735
736         service_prompt_vbox = new VBox(false, 0);
737         top_table.attach(service_prompt_vbox, 0, 1, row, row + 1, fill_and_expand, fill_and_expand, 12, 0);
738         row++;
739
740         string search_tooltip_text = _("Search for an identity or service");
741         this.search_entry = new Entry();
742
743         set_atk_name_description(search_entry, _("Search entry"), _("Search for a specific ID Card"));
744         this.search_entry.set_icon_from_pixbuf(EntryIconPosition.SECONDARY,
745                                                find_icon_sized("edit-find", Gtk.IconSize.MENU));
746         this.search_entry.set_icon_tooltip_text(EntryIconPosition.SECONDARY,
747                                                 search_tooltip_text);
748
749         this.search_entry.set_tooltip_text(search_tooltip_text);
750
751         this.search_entry.set_icon_sensitive(EntryIconPosition.SECONDARY, false);
752
753         this.search_entry.notify["text"].connect(search_entry_text_changed_cb);
754         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
755         this.search_entry.set_width_chars(24);
756
757         var search_label_markup =_("<small>") + search_tooltip_text + _("</small>");
758         var full_search_label = new Label(null);
759         full_search_label.set_markup(search_label_markup);
760         full_search_label.set_alignment(1, 0);
761
762         var search_vbox = new VBox(false, 0);
763         search_vbox.pack_start(search_entry, false, false, 0);
764         var search_spacer = new Alignment(0, 0, 0, 0);
765         search_spacer.set_size_request(0, 2);
766         search_vbox.pack_start(search_spacer, false, false, 0);
767         search_vbox.pack_start(full_search_label, false, false, 0);
768
769         // Overlap with the service_prompt_box
770         top_table.attach(search_vbox, 5, num_cols - button_width, row - 1, row + 1, fill_and_expand, fill, 0, 12);
771         row++;
772
773         this.custom_vbox = new CustomVBox(this, false, 2);
774
775         var viewport = new Viewport(null, null);
776         viewport.set_border_width(2);
777         viewport.set_shadow_type(ShadowType.NONE);
778         viewport.add(custom_vbox);
779         var id_scrollwin = new ScrolledWindow(null, null);
780         id_scrollwin.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
781         id_scrollwin.set_shadow_type(ShadowType.IN);
782         id_scrollwin.add_with_viewport(viewport);
783         top_table.attach(id_scrollwin, 0, num_cols - 1, row, num_rows - 1, fill_and_expand, fill_and_expand, 6, 0);
784
785         // Right below id_scrollwin:
786         remember_identity_binding = new CheckButton.with_label(_("Remember my identity choice for this service"));
787         remember_identity_binding.active = false;
788         top_table.attach(remember_identity_binding, 0, num_cols / 2, num_rows - 1, num_rows, fill_and_expand, fill_and_expand, 3, 0);
789
790         var add_button = new Button.with_label(_("Add"));
791         add_button.clicked.connect((w) => {add_identity_cb();});
792         top_table.attach(make_rigid(add_button), num_cols - button_width, num_cols, row, row + 1, fill, fill, 0, 0);
793         logger.trace("build_ui: row spacing for row %d is %u".printf(row, top_table.get_row_spacing(row)));
794         row++;
795
796         this.edit_button = new Button.with_label(_("Edit"));
797         edit_button.clicked.connect((w) => {edit_identity_cb(this.selected_idcard);});
798         edit_button.set_sensitive(false);
799         top_table.attach(make_rigid(edit_button), num_cols - button_width, num_cols, row, row + 1, fill, fill, 0, 0);
800         row++;
801
802         this.remove_button = new Button.with_label(_("Remove"));
803         remove_button.clicked.connect((w) => {remove_identity_cb(this.selected_idcard);});
804         remove_button.set_sensitive(false);
805         top_table.attach(make_rigid(remove_button), num_cols - button_width, num_cols, row, row + 1, fill, fill, 0, 0);
806         row++;
807
808         // push the send button down another row.
809         row++;
810         this.send_button = new Button.with_label(_("Send"));
811         send_button.clicked.connect((w) => {send_identity_cb(this.selected_idcard);});
812         // send_button.set_visible(false);
813         send_button.set_sensitive(false);
814         top_table.attach(make_rigid(send_button), num_cols - button_width, num_cols, row, row + 1, fill, fill, 0, 0);
815         row++;
816
817         var main_vbox = new VBox(false, 0);
818
819 #if OS_MACOS
820         // hide the  File | Quit menu item which is now on the Mac Menu
821 //        Gtk.Widget quit_item =  this.ui_manager.get_widget("/MenuBar/FileMenu/Quit");
822 //        quit_item.hide();
823         
824         Gtk.MenuShell menushell = this.ui_manager.get_widget("/MenuBar") as Gtk.MenuShell;
825         menushell.modify_bg(StateType.NORMAL, white);
826
827         osxApp.set_menu_bar(menushell);
828         osxApp.set_use_quartz_accelerators(true);
829         osxApp.sync_menu_bar();
830         osxApp.ready();
831 #else
832         var menubar = this.ui_manager.get_widget("/MenuBar");
833         main_vbox.pack_start(menubar, false, false, 0);
834         menubar.modify_bg(StateType.NORMAL, white);
835 #endif
836         main_vbox.pack_start(top_table, true, true, 6);
837
838         add(main_vbox);
839         main_vbox.show_all();
840
841         if (!this.selection_in_progress())
842             remember_identity_binding.hide();
843     } 
844
845     internal bool selection_in_progress() {
846         return !this.request_queue.is_empty();
847     }
848
849     private void set_atk_name_description(Widget widget, string name, string description)
850     {
851         var atk_widget = widget.get_accessible();
852
853         atk_widget.set_name(name);
854         atk_widget.set_description(description);
855     }
856
857     private void connect_signals()
858     {
859         this.destroy.connect(() => {
860                 logger.trace("Destroy event; calling Gtk.main_quit()");
861                 Gtk.main_quit();
862             });
863         this.identities_manager.card_list_changed.connect(this.on_card_list_changed);
864         this.delete_event.connect(() => {return confirm_quit();});
865     }
866
867     private bool confirm_quit() {
868         logger.trace("delete_event intercepted; selection_in_progress()=" + selection_in_progress().to_string());
869
870         if (selection_in_progress()) {
871             var result = WarningDialog.confirm(this,
872                                                Markup.printf_escaped(
873                                                    _("<span font-weight='heavy'>Do you wish to use the %s service?</span>"),
874                                                    this.request_queue.peek_head().service)
875                                                + _("\n\nSelect Yes to select an ID for this service, or No to cancel"),
876                                                "close_moonshot_window");
877             if (result) {
878                 // Prevent other handlers from handling this event; this keeps the window open.
879                 return true; 
880             }
881         }
882
883         // Allow the window deletion to proceed.
884         return false;
885     }
886
887     private static Widget make_rigid(Button button) 
888     {
889         // Hack to prevent the button from growing vertically
890         VBox fixed_height = new VBox(false, 0);
891         fixed_height.pack_start(button, false, false, 0);
892
893         return fixed_height;
894     }
895
896 }