Fixed (again) how the Add, Edit, Remove, and Send buttons are enabled/disabled
[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     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 CustomVBox custom_vbox;
48     private VBox service_prompt_vbox;
49     private Label no_identity_title;
50     private Button edit_button;
51     private Button remove_button;
52
53     private Button send_button;
54     
55     private Gtk.ListStore* listmodel;
56     private TreeModelFilter filter;
57
58     internal IdentityManagerModel identities_manager;
59     private unowned SList<IdCard>    candidates;
60
61     public GLib.Queue<IdentityRequest> request_queue;
62
63     private enum Columns
64     {
65         IDCARD_COL,
66         LOGO_COL,
67         ISSUER_COL,
68         USERNAME_COL,
69         PASSWORD_COL,
70         N_COLUMNS
71     }
72
73     private const string menu_layout =
74     "<menubar name='MenuBar'>" +
75     "        <menu name='HelpMenu' action='HelpMenuAction'>" +
76     "             <menuitem name='About' action='AboutAction' />" +
77     "        </menu>" +
78     "</menubar>";
79
80     public IdentityManagerView(IdentityManagerApp app) {
81         parent_app = app;
82         #if OS_MACOS
83             osxApp = OSXApplication.get_instance();
84         #endif
85         identities_manager = parent_app.model;
86         request_queue = new GLib.Queue<IdentityRequest>();
87         this.title = "Moonshot Identity Selector";
88         this.set_position(WindowPosition.CENTER);
89         set_default_size(WINDOW_WIDTH, WINDOW_HEIGHT);
90         build_ui();
91         setup_list_model(); 
92         load_id_cards(); 
93         connect_signals();
94     }
95     
96     public void on_card_list_changed() {
97         load_id_cards();
98     }
99     
100     private bool visible_func(TreeModel model, TreeIter iter)
101     {
102         IdCard id_card;
103
104         model.get(iter,
105                   Columns.IDCARD_COL, out id_card);
106
107         if (id_card == null)
108             return false;
109         
110         if (candidates != null)
111         {
112             bool is_candidate = false;
113             foreach (IdCard candidate in candidates)
114             {
115                 if (candidate == id_card)
116                     is_candidate = true;
117             }
118             if (!is_candidate)
119                 return false;
120         }
121         
122         string entry_text = search_entry.get_text();
123         if (entry_text == null || entry_text == "")
124         {
125             return true;
126         }
127
128         foreach (string search_text in entry_text.split(" "))
129         {
130             if (search_text == "")
131                 continue;
132          
133
134             string search_text_casefold = search_text.casefold();
135
136             if (id_card.issuer != null)
137             {
138                 string issuer_casefold = id_card.issuer;
139
140                 if (issuer_casefold.contains(search_text_casefold))
141                     return true;
142             }
143
144             if (id_card.display_name != null)
145             {
146                 string display_name_casefold = id_card.display_name.casefold();
147               
148                 if (display_name_casefold.contains(search_text_casefold))
149                     return true;
150             }
151             
152             if (id_card.services.length > 0)
153             {
154                 foreach (string service in id_card.services)
155                 {
156                     string service_casefold = service.casefold();
157
158                     if (service_casefold.contains(search_text_casefold))
159                         return true;
160                 }
161             }
162         }
163         return false;
164     }
165
166     private void setup_list_model()
167     {
168         this.listmodel = new Gtk.ListStore(Columns.N_COLUMNS, typeof(IdCard),
169                                            typeof(Gdk.Pixbuf),
170                                            typeof(string),
171                                            typeof(string),
172                                            typeof(string));
173         this.filter = new TreeModelFilter(listmodel, null);
174
175         filter.set_visible_func(visible_func);
176     }
177
178     private void search_entry_icon_press_cb(EntryIconPosition pos, Gdk.Event event)
179     {
180         if (pos == EntryIconPosition.PRIMARY)
181         {
182             print("Search entry icon pressed\n");
183         }
184         else
185         {
186             this.search_entry.set_text("");
187         }
188     }
189
190     private void search_entry_text_changed_cb()
191     {
192         this.filter.refilter();
193         redraw_id_card_widgets();
194
195         var has_text = this.search_entry.get_text_length() > 0;
196         this.search_entry.set_icon_sensitive(EntryIconPosition.PRIMARY, has_text);
197         this.search_entry.set_icon_sensitive(EntryIconPosition.SECONDARY, has_text);
198     }
199
200     private bool search_entry_key_press_event_cb(Gdk.EventKey e)
201     {
202         if(Gdk.keyval_name(e.keyval) == "Escape")
203             this.search_entry.set_text("");
204
205         // Continue processing this event, since the
206         // text entry functionality needs to see it too.
207         return false;
208     }
209
210     private void load_id_cards() {
211         logger.trace("load_id_cards");
212
213         string current_idcard_nai = null;
214         if (this.custom_vbox.current_idcard != null) {
215             current_idcard_nai = custom_vbox.current_idcard.id_card.nai;
216             custom_vbox.current_idcard = null;
217         }
218
219         custom_vbox.clear();
220         this.listmodel->clear();
221         LinkedList<IdCard> card_list = identities_manager.get_card_list() ;
222         if (card_list == null) {
223             return;
224         }
225
226         foreach (IdCard id_card in card_list) {
227             add_id_card_data(id_card);
228             IdCardWidget id_card_widget = add_id_card_widget(id_card);
229             if (id_card_widget.id_card.nai == current_idcard_nai) {
230                 // fill_details(id_card_widget.id_card);
231                 id_card_widget.expand();
232             }
233         }
234     }
235     
236     private IdCard update_id_card_data(IdentityDialog dialog, IdCard id_card)
237     {
238         id_card.display_name = dialog.display_name;
239         id_card.issuer = dialog.issuer;
240         id_card.username = dialog.username;
241         id_card.password = dialog.password;
242         id_card.store_password = dialog.store_password;
243         id_card.services = dialog.get_services();
244
245         return id_card;
246     }
247
248     private void add_id_card_data(IdCard id_card)
249     {
250         TreeIter   iter;
251         Gdk.Pixbuf pixbuf;
252         this.listmodel->append(out iter);
253         pixbuf = get_pixbuf(id_card);
254         listmodel->set(iter,
255                        Columns.IDCARD_COL, id_card,
256                        Columns.LOGO_COL, pixbuf,
257                        Columns.ISSUER_COL, id_card.issuer,
258                        Columns.USERNAME_COL, id_card.username,
259                        Columns.PASSWORD_COL, id_card.password);
260     }
261
262     private void remove_id_card_data(IdCard id_card)
263     {
264         TreeIter iter;
265         string issuer;
266
267         if (listmodel->get_iter_first(out iter))
268         {
269             do
270             {
271                 listmodel->get(iter,
272                                Columns.ISSUER_COL, out issuer);
273
274                 if (id_card.issuer == issuer)
275                 {
276                     listmodel->remove(iter);
277                     break;
278                 }
279             }
280             while (listmodel->iter_next(ref iter));
281         }
282     }
283
284     private IdCardWidget add_id_card_widget(IdCard id_card)
285     {
286         var id_card_widget = new IdCardWidget(id_card);
287         this.custom_vbox.add_id_card_widget(id_card_widget);
288         id_card_widget.expanded.connect(this.widget_selected_cb);
289         id_card_widget.collapsed.connect(this.widget_unselected_cb);
290         return id_card_widget;
291     }
292
293     private void widget_selected_cb(IdCardWidget id_card_widget)
294     {
295         this.remove_button.set_sensitive(true);
296         this.edit_button.set_sensitive(true);
297         this.custom_vbox.receive_expanded_event(id_card_widget);
298
299         if (this.request_queue.length > 0)
300              this.send_button.set_sensitive(true);
301     }
302
303     private void widget_unselected_cb(IdCardWidget id_card_widget)
304     {
305         this.remove_button.set_sensitive(false);
306         this.edit_button.set_sensitive(false);
307         this.custom_vbox.receive_collapsed_event(id_card_widget);
308
309         this.send_button.set_sensitive(false);
310     }
311
312     public bool add_identity(IdCard id_card, bool force_flat_file_store)
313     {
314         #if OS_MACOS
315         /* 
316          * TODO: We should have a confirmation dialog, but currently it will crash on Mac OS
317          * so for now we will install silently
318          */
319         var ret = Gtk.ResponseType.YES;
320         #else
321         Gtk.MessageDialog dialog;
322         IdCard? prev_id = identities_manager.find_id_card(id_card.nai, force_flat_file_store);
323         logger.trace("add_identity: find_id_card returned " + (prev_id != null ? "non-null" : "null"));
324         if (prev_id!=null) {
325             int flags = prev_id.Compare(id_card);
326             logger.trace("add_identity: compare returned " + flags.to_string());
327             if (flags == 0) {
328                 return false; // no changes, no need to update
329             } else if ((flags & (1 << IdCard.DiffFlags.DISPLAY_NAME)) != 0) {
330                 dialog = new Gtk.MessageDialog(this,
331                                                Gtk.DialogFlags.DESTROY_WITH_PARENT,
332                                                Gtk.MessageType.QUESTION,
333                                                Gtk.ButtonsType.YES_NO,
334                                                _("Would you like to replace ID Card '%s' using nai '%s' with the new ID Card '%s'?"),
335                                                prev_id.display_name,
336                                                prev_id.nai,
337                                                id_card.display_name);
338             } else {
339                 dialog = new Gtk.MessageDialog(this,
340                                                Gtk.DialogFlags.DESTROY_WITH_PARENT,
341                                                Gtk.MessageType.QUESTION,
342                                                Gtk.ButtonsType.YES_NO,
343                                                _("Would you like to update ID Card '%s' using nai '%s'?"),
344                                                id_card.display_name,
345                                                id_card.nai);
346             }
347         } else {
348             dialog = new Gtk.MessageDialog(this,
349                                            Gtk.DialogFlags.DESTROY_WITH_PARENT,
350                                            Gtk.MessageType.QUESTION,
351                                            Gtk.ButtonsType.YES_NO,
352                                            _("Would you like to add '%s' ID Card to the ID Card Organizer?"),
353                                            id_card.display_name);
354         }
355         var ret = dialog.run();
356         dialog.destroy();
357         #endif
358
359         if (ret == Gtk.ResponseType.YES) {
360             this.identities_manager.add_card(id_card, force_flat_file_store);
361             return true;
362         }
363         return false;
364     }
365
366     private void add_identity_cb()
367     {
368         var dialog = new IdentityDialog(this);
369         int result = ResponseType.CANCEL;
370         while (!dialog.complete)
371             result = dialog.run();
372
373         switch (result) {
374         case ResponseType.OK:
375             this.identities_manager.add_card(update_id_card_data(dialog, new IdCard()), false);
376             break;
377         default:
378             break;
379         }
380         dialog.destroy();
381     }
382
383     private void edit_identity_cb(IdCard card)
384     {
385         var dialog = new IdentityDialog.with_idcard(card, _("Edit Identity"), this);
386         int result = ResponseType.CANCEL;
387         while (!dialog.complete)
388             result = dialog.run();
389
390         switch (result) {
391         case ResponseType.OK:
392             this.identities_manager.update_card(update_id_card_data(dialog, card));
393             break;
394         default:
395             break;
396         }
397         dialog.destroy();
398     }
399
400     private void remove_identity(IdCardWidget id_card_widget)
401     {
402         var id_card = id_card_widget.id_card;
403         this.custom_vbox.remove_id_card_widget(id_card_widget);
404
405         this.identities_manager.remove_card(id_card);
406
407         // Nothing is selected, so disable buttons
408         this.edit_button.set_sensitive(false);
409         this.remove_button.set_sensitive(false);
410         this.send_button.set_sensitive(false);
411     }
412
413     private void redraw_id_card_widgets()
414     {
415         logger.trace("redraw_id_card_widgets");
416
417         TreeIter iter;
418         IdCard id_card;
419
420         var children = this.custom_vbox.get_children();
421         this.custom_vbox.clear();
422
423         if (filter.get_iter_first(out iter))
424         {
425             do
426             {
427                 filter.get(iter,
428                            Columns.IDCARD_COL, out id_card);
429
430                 add_id_card_widget(id_card);
431             }
432             while (filter.iter_next(ref iter));
433         }
434     }
435
436     private void remove_identity_cb(IdCardWidget id_card_widget)
437     {
438         var id_card = id_card_widget.id_card;
439
440         bool remove = WarningDialog.confirm(this, 
441                                             Markup.printf_escaped(
442                                                 "<span font-weight='heavy'>You are about to remove the identity '%s'.</span>",
443                                                 id_card.display_name)
444                                             + "\n\nAre you sure you want to do this?",
445                                             "delete_idcard");
446         if (remove) 
447             remove_identity(id_card_widget);
448     }
449
450     private void set_prompting_service(string service)
451     {
452         clear_selection_prompts();
453
454         var prompting_service = new Label(_("Identity requested for service:\n%s").printf(service));
455         prompting_service.set_line_wrap(true);
456
457         // left-align
458         prompting_service.set_alignment(0, (float )0.5);
459
460         var selection_prompt = new Label(_("Select your identity:"));
461         selection_prompt.set_alignment(0, 1);
462
463         this.service_prompt_vbox.pack_start(prompting_service, false, false, 12);
464         this.service_prompt_vbox.pack_start(selection_prompt, false, false, 2);
465         this.service_prompt_vbox.show_all();
466     }
467
468     private void clear_selection_prompts()
469     {
470         var list = service_prompt_vbox.get_children();
471         foreach (Widget w in list)
472         {
473             service_prompt_vbox.remove(w);
474         }
475     }
476
477
478     public void queue_identity_request(IdentityRequest request)
479     {
480         if (this.request_queue.is_empty())
481         { /* setup widgets */
482             candidates = request.candidates;
483             filter.refilter();
484             redraw_id_card_widgets();
485             set_prompting_service(request.service);
486             make_visible();
487         }
488         this.request_queue.push_tail(request);
489     }
490
491
492     /** Makes the window visible, or at least, notifies the user that the window
493      * wants to be visible.
494      *
495      * This differs from show() in that show() does not guarantee that the 
496      * window will be moved to the foreground. Actually, neither does this
497      * method, because the user's settings and window manager may affect the
498      * behavior significantly.
499      */
500     public void make_visible()
501     {
502         set_urgency_hint(true);
503         present();
504     }
505
506     public IdCard check_add_password(IdCard identity, IdentityRequest request, IdentityManagerModel model)
507     {
508         IdCard retval = identity;
509         bool idcard_has_pw = (identity.password != null) && (identity.password != "");
510         bool request_has_pw = (request.password != null) && (request.password != "");
511         if ((!idcard_has_pw) && (!identity.IsNoIdentity())) {
512             if (request_has_pw) {
513                 identity.password = request.password;
514                 retval = model.update_card(identity);
515             } else {
516                 var dialog = new AddPasswordDialog(identity, request);
517                 var result = dialog.run();
518
519                 switch (result) {
520                 case ResponseType.OK:
521                     identity.password = dialog.password;
522                     identity.store_password = dialog.remember;
523                     if (dialog.remember)
524                         identity.temporary = false;
525                     retval = model.update_card(identity);
526                     break;
527                 default:
528                     identity = null;
529                     break;
530                 }
531                 dialog.destroy();
532             }
533         }
534         return retval;
535     }
536
537     private void send_identity_cb(IdCard id)
538     {
539         send_button.set_sensitive(false);
540
541         IdCard identity = id;
542         return_if_fail(request_queue.length > 0);
543
544         candidates = null;
545         var request = this.request_queue.pop_head();
546         identity = check_add_password(identity, request, identities_manager);
547         if (this.request_queue.is_empty())
548         {
549             candidates = null;
550             clear_selection_prompts();
551             if (!parent_app.explicitly_launched) {
552 // The following occasionally causes the app to exit without sending the dbus
553 // reply, so for now we just don't exit
554 //                Gtk.main_quit();
555 // just hide instead
556                 this.hide();
557             }
558         } else {
559             IdentityRequest next = this.request_queue.peek_head();
560             candidates = next.candidates;
561             set_prompting_service(next.service);
562         }
563         filter.refilter();
564         redraw_id_card_widgets();
565
566         if ((identity != null) && (!identity.IsNoIdentity()))
567             parent_app.default_id_card = identity;
568
569         request.return_identity(identity);
570     }
571
572     // private void label_make_bold(Label label)
573     // {
574     //     var font_desc = new Pango.FontDescription();
575
576     //     font_desc.set_weight(Pango.Weight.BOLD);
577
578     //     /* This will only affect the weight of the font, the rest is
579     //      * from the current state of the widget, which comes from the
580     //      * theme or user prefs, since the font desc only has the
581     //      * weight flag turned on.
582     //      */
583     //     label.modify_font(font_desc);
584     // }
585
586     private void on_about_action()
587     {
588         string copyright = "Copyright 2011, 2016 JANET";
589
590         string license =
591         """
592 Copyright (c) 2011, 2016 JANET(UK)
593 All rights reserved.
594
595 Redistribution and use in source and binary forms, with or without
596 modification, are permitted provided that the following conditions
597 are met:
598
599 1. Redistributions of source code must retain the above copyright
600    notice, this list of conditions and the following disclaimer.
601
602 2. Redistributions in binary form must reproduce the above copyright
603    notice, this list of conditions and the following disclaimer in the
604    documentation and/or other materials provided with the distribution.
605
606 3. Neither the name of JANET(UK) nor the names of its contributors
607    may be used to endorse or promote products derived from this software
608    without specific prior written permission.
609
610 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"
611 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
612 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
613 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
614 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
615 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
616 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
617 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
618 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
619 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
620 SUCH DAMAGE.
621 """;
622
623         Gtk.show_about_dialog(this,
624                               "comments", _("Moonshot project UI"),
625                               "copyright", copyright,
626                               "website", Config.PACKAGE_URL,
627                               "version", Config.PACKAGE_VERSION,
628                               "license", license,
629                               "website-label", _("Visit the Moonshot project web site"),
630                               "translator-credits", _("translator-credits"),
631                               null
632             );
633     }
634
635     private Gtk.ActionEntry[] create_actions() {
636         Gtk.ActionEntry[] actions = new Gtk.ActionEntry[0];
637
638         Gtk.ActionEntry helpmenu = { "HelpMenuAction",
639                                      null,
640                                      N_("_Help"),
641                                      null, null, null };
642         actions += helpmenu;
643         Gtk.ActionEntry about = { "AboutAction",
644                                   #if VALA_0_12
645                                   Stock.ABOUT,
646                                   #else
647                                   STOCK_ABOUT,
648                                   #endif
649                                   N_("About"),
650                                   null,
651                                   N_("About this application"),
652                                   on_about_action };
653         actions += about;
654
655         return actions;
656     }
657
658
659     private void create_ui_manager()
660     {
661         Gtk.ActionGroup action_group = new Gtk.ActionGroup("GeneralActionGroup");
662         action_group.add_actions(create_actions(), this);
663         ui_manager.insert_action_group(action_group, 0);
664         try
665         {
666             ui_manager.add_ui_from_string(menu_layout, -1);
667         }
668         catch (Error e)
669         {
670             stderr.printf("%s\n", e.message);
671             logger.error("create_ui_manager: Caught error: " + e.message);
672         }
673         ui_manager.ensure_update();
674     }
675
676     private void build_ui()
677     {
678         // Note: On Debian7/Gtk+2, the menu bar remains gray. This doesn't happen on Debian8/Gtk+3.
679         Gdk.Color white = Gdk.Color();
680         white.red = white.green = white.blue = 65535;
681         this.modify_bg(StateType.NORMAL, white);
682
683         create_ui_manager();
684
685         this.search_entry = new Entry();
686
687         set_atk_name_description(search_entry, _("Search entry"), _("Search for a specific ID Card"));
688         this.search_entry.set_icon_from_pixbuf(EntryIconPosition.PRIMARY,
689                                                find_icon_sized("edit-find", Gtk.IconSize.MENU));
690         this.search_entry.set_icon_tooltip_text(EntryIconPosition.PRIMARY,
691                                                 _("Search for an identity or service"));
692         this.search_entry.set_icon_sensitive(EntryIconPosition.PRIMARY, false);
693
694         this.search_entry.set_icon_from_pixbuf(EntryIconPosition.SECONDARY,
695                                                find_icon_sized("process-stop", Gtk.IconSize.MENU));
696         this.search_entry.set_icon_tooltip_text(EntryIconPosition.SECONDARY,
697                                                 _("Clear the current search"));
698         this.search_entry.set_icon_sensitive(EntryIconPosition.SECONDARY, false);
699
700
701         this.search_entry.icon_press.connect(search_entry_icon_press_cb);
702         this.search_entry.notify["text"].connect(search_entry_text_changed_cb);
703         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
704         this.search_entry.set_width_chars(30);
705
706
707         this.custom_vbox = new CustomVBox(this, false, 2);
708
709         var viewport = new Viewport(null, null);
710         viewport.set_border_width(2);
711         viewport.set_shadow_type(ShadowType.NONE);
712         viewport.add(custom_vbox);
713         var id_scrollwin = new ScrolledWindow(null, null);
714         id_scrollwin.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
715         id_scrollwin.set_shadow_type(ShadowType.IN);
716         id_scrollwin.add_with_viewport(viewport);
717
718         service_prompt_vbox = new VBox(false, 0);
719
720         var vbox_left = new VBox(false, 0);
721         vbox_left.pack_start(service_prompt_vbox, false, false, 12);
722
723         var search_hbox = new HBox(false, 6);
724         search_hbox.pack_end(search_entry, false, false, 0);
725         //// var search_label = new Label(_("Search:"));
726         //// search_label.set_alignment(1, (float) 0.5);
727         //// set_atk_relation(search_label, search_entry, Atk.RelationType.LABEL_FOR);
728         //// search_hbox.pack_end(search_label, false, false, 6);
729
730         var full_search_label = new Label(_("Search for an identity or service"));
731         full_search_label.set_alignment(1, 0);
732         var search_vbox = new VBox(false, 4);
733         search_vbox.pack_start(full_search_label, false, false, 0);
734         search_vbox.pack_start(search_hbox, false, false, 0);
735
736         var inner_left_vbox = new VBox(false, 6);
737         inner_left_vbox.pack_start(search_vbox, false, false, 6);
738 //        inner_left_vbox.pack_start(selection_prompt, false, false, 6);
739         inner_left_vbox.pack_start(id_scrollwin, true, true, 0);
740
741         var id_and_button_box = new HBox(false, 6);
742         id_and_button_box.pack_start(inner_left_vbox, true, true, 6);
743         vbox_left.pack_start(id_and_button_box, true, true, 0);
744         // vbox_left.pack_start(prompting_service, false, false, 6);
745         vbox_left.set_size_request(WINDOW_WIDTH, 0);
746
747         this.no_identity_title = new Label(_("No Identity: Send this identity to services which should not use Moonshot"));
748         no_identity_title.set_alignment(0, (float ) 0.5);
749         no_identity_title.set_line_wrap(true);
750         no_identity_title.show();
751
752         this.vbox_right = new VBox(false, 6);
753
754         var add_button = new Button.with_label(_("Add"));
755         add_button.clicked.connect((w) => {add_identity_cb();});
756
757         this.edit_button = new Button.with_label(_("Edit"));
758         edit_button.clicked.connect((w) => {edit_identity_cb(custom_vbox.current_idcard.id_card);});
759         edit_button.set_sensitive(false);
760
761         this.remove_button = new Button.with_label(_("Remove"));
762         remove_button.clicked.connect((w) => {remove_identity_cb(custom_vbox.current_idcard);});
763         remove_button.set_sensitive(false);
764
765         this.send_button = new Button.with_label(_("Send"));
766         send_button.clicked.connect((w) => {send_identity_cb(custom_vbox.current_idcard.id_card);});
767         // send_button.set_visible(false);
768         send_button.set_sensitive(false);
769
770         var empty_box = new VBox(false, 0);
771         empty_box.set_size_request(0, 0);
772         vbox_right.pack_start(empty_box, false, false, 14);
773         vbox_right.pack_start(add_button, false, false, 6);
774         vbox_right.pack_start(edit_button, false, false, 6);
775         vbox_right.pack_start(remove_button, false, false, 6);
776         vbox_right.pack_start(send_button, false, false, 24);
777
778         id_and_button_box.pack_start(vbox_right, false, false, 0);
779
780         var main_vbox = new VBox(false, 0);
781
782         // Note: This places a border above the menubar. Is that what we want?
783         main_vbox.set_border_width(12);
784
785 #if OS_MACOS
786         // hide the  File | Quit menu item which is now on the Mac Menu
787 //        Gtk.Widget quit_item =  this.ui_manager.get_widget("/MenuBar/FileMenu/Quit");
788 //        quit_item.hide();
789         
790         Gtk.MenuShell menushell = this.ui_manager.get_widget("/MenuBar") as Gtk.MenuShell;
791         menushell.modify_bg(StateType.NORMAL, white);
792
793         osxApp.set_menu_bar(menushell);
794         osxApp.set_use_quartz_accelerators(true);
795         osxApp.sync_menu_bar();
796         osxApp.ready();
797 #else
798         var menubar = this.ui_manager.get_widget("/MenuBar");
799         main_vbox.pack_start(menubar, false, false, 0);
800         menubar.modify_bg(StateType.NORMAL, white);
801 #endif
802         main_vbox.pack_start(vbox_left, true, true, 0);
803         add(main_vbox);
804         main_vbox.show_all();
805     } 
806
807     private void set_atk_name_description(Widget widget, string name, string description)
808     {
809         var atk_widget = widget.get_accessible();
810
811         atk_widget.set_name(name);
812         atk_widget.set_description(description);
813     }
814
815     private void connect_signals()
816     {
817         this.destroy.connect(Gtk.main_quit);
818         this.identities_manager.card_list_changed.connect(this.on_card_list_changed);
819     }
820 }