2 * Copyright (c) 2011-2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
35 public class IdentityManagerView : Window {
36 static MoonshotLogger logger = get_logger("IdentityManagerView");
38 private const int WINDOW_WIDTH = 700;
39 private const int WINDOW_HEIGHT = 500;
40 protected IdentityManagerApp parent_app;
42 public OSXApplication osxApp;
44 private UIManager ui_manager = new UIManager();
45 private Entry search_entry;
46 private VBox vbox_right;
47 private CustomVBox custom_vbox;
48 private Label prompting_service;
49 private Label no_identity_title;
50 private Button edit_button;
51 private Button remove_button;
53 private Button send_button;
55 private Gtk.ListStore* listmodel;
56 private TreeModelFilter filter;
58 internal IdentityManagerModel identities_manager;
59 private unowned SList<IdCard> candidates;
61 public GLib.Queue<IdentityRequest> request_queue;
73 private const string menu_layout =
74 "<menubar name='MenuBar'>" +
75 " <menu name='HelpMenu' action='HelpMenuAction'>" +
76 " <menuitem name='About' action='AboutAction' />" +
80 public IdentityManagerView(IdentityManagerApp app) {
83 osxApp = OSXApplication.get_instance();
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);
96 public void on_card_list_changed() {
100 private bool visible_func(TreeModel model, TreeIter iter)
105 Columns.IDCARD_COL, out id_card);
110 if (candidates != null)
112 bool is_candidate = false;
113 foreach (IdCard candidate in candidates)
115 if (candidate == id_card)
122 string entry_text = search_entry.get_text();
123 if (entry_text == null || entry_text == "")
128 foreach (string search_text in entry_text.split(" "))
130 if (search_text == "")
134 string search_text_casefold = search_text.casefold();
136 if (id_card.issuer != null)
138 string issuer_casefold = id_card.issuer;
140 if (issuer_casefold.contains(search_text_casefold))
144 if (id_card.display_name != null)
146 string display_name_casefold = id_card.display_name.casefold();
148 if (display_name_casefold.contains(search_text_casefold))
152 if (id_card.services.length > 0)
154 foreach (string service in id_card.services)
156 string service_casefold = service.casefold();
158 if (service_casefold.contains(search_text_casefold))
166 private void setup_list_model()
168 this.listmodel = new Gtk.ListStore(Columns.N_COLUMNS, typeof(IdCard),
173 this.filter = new TreeModelFilter(listmodel, null);
175 filter.set_visible_func(visible_func);
178 private void search_entry_icon_press_cb(EntryIconPosition pos, Gdk.Event event)
180 if (pos == EntryIconPosition.PRIMARY)
182 print("Search entry icon pressed\n");
186 this.search_entry.set_text("");
190 private void search_entry_text_changed_cb()
192 this.filter.refilter();
193 redraw_id_card_widgets();
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);
200 private bool search_entry_key_press_event_cb(Gdk.EventKey e)
202 if(Gdk.keyval_name(e.keyval) == "Escape")
203 this.search_entry.set_text("");
205 // Continue processing this event, since the
206 // text entry functionality needs to see it too.
210 private void load_id_cards() {
211 logger.trace("load_id_cards");
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;
218 var children = this.custom_vbox.get_children();
221 this.listmodel->clear();
222 LinkedList<IdCard> card_list = identities_manager.get_card_list() ;
223 if (card_list == null) {
227 foreach (IdCard id_card in card_list) {
228 add_id_card_data(id_card);
229 IdCardWidget id_card_widget = add_id_card_widget(id_card);
230 if (id_card_widget.id_card.nai == current_idcard_nai) {
231 // fill_details(id_card_widget.id_card);
232 id_card_widget.expand();
237 private IdCard update_id_card_data(IdentityDialog dialog, IdCard id_card)
239 id_card.display_name = dialog.display_name;
240 id_card.issuer = dialog.issuer;
241 id_card.username = dialog.username;
242 id_card.password = dialog.password;
243 id_card.store_password = dialog.store_password;
244 id_card.services = dialog.get_services();
249 private void add_id_card_data(IdCard id_card)
253 this.listmodel->append(out iter);
254 pixbuf = get_pixbuf(id_card);
256 Columns.IDCARD_COL, id_card,
257 Columns.LOGO_COL, pixbuf,
258 Columns.ISSUER_COL, id_card.issuer,
259 Columns.USERNAME_COL, id_card.username,
260 Columns.PASSWORD_COL, id_card.password);
263 private void remove_id_card_data(IdCard id_card)
268 if (listmodel->get_iter_first(out iter))
273 Columns.ISSUER_COL, out issuer);
275 if (id_card.issuer == issuer)
277 listmodel->remove(iter);
281 while (listmodel->iter_next(ref iter));
285 private IdCardWidget add_id_card_widget(IdCard id_card)
287 var id_card_widget = new IdCardWidget(id_card);
288 this.custom_vbox.add_id_card_widget(id_card_widget);
289 id_card_widget.expanded.connect(this.widget_selected_cb);
290 return id_card_widget;
293 private void widget_selected_cb(IdCardWidget id_card_widget)
295 this.remove_button.set_sensitive(true);
296 this.edit_button.set_sensitive(true);
297 this.custom_vbox.receive_expanded_event(id_card_widget);
299 if (this.request_queue.length > 0)
300 this.send_button.set_sensitive(true);
303 public bool add_identity(IdCard id_card, bool force_flat_file_store)
307 * TODO: We should have a confirmation dialog, but currently it will crash on Mac OS
308 * so for now we will install silently
310 var ret = Gtk.ResponseType.YES;
312 Gtk.MessageDialog dialog;
313 IdCard? prev_id = identities_manager.find_id_card(id_card.nai, force_flat_file_store);
315 int flags = prev_id.Compare(id_card);
317 return false; // no changes, no need to update
318 } else if ((flags & (1 << IdCard.DiffFlags.DISPLAY_NAME)) != 0) {
319 dialog = new Gtk.MessageDialog(this,
320 Gtk.DialogFlags.DESTROY_WITH_PARENT,
321 Gtk.MessageType.QUESTION,
322 Gtk.ButtonsType.YES_NO,
323 _("Would you like to replace ID Card '%s' using nai '%s' with the new ID Card '%s'?"),
324 prev_id.display_name,
326 id_card.display_name);
328 dialog = new Gtk.MessageDialog(this,
329 Gtk.DialogFlags.DESTROY_WITH_PARENT,
330 Gtk.MessageType.QUESTION,
331 Gtk.ButtonsType.YES_NO,
332 _("Would you like to update ID Card '%s' using nai '%s'?"),
333 id_card.display_name,
337 dialog = new Gtk.MessageDialog(this,
338 Gtk.DialogFlags.DESTROY_WITH_PARENT,
339 Gtk.MessageType.QUESTION,
340 Gtk.ButtonsType.YES_NO,
341 _("Would you like to add '%s' ID Card to the ID Card Organizer?"),
342 id_card.display_name);
344 var ret = dialog.run();
348 if (ret == Gtk.ResponseType.YES) {
349 this.identities_manager.add_card(id_card, force_flat_file_store);
355 private void add_identity_cb()
357 var dialog = new IdentityDialog(this);
358 int result = ResponseType.CANCEL;
359 while (!dialog.complete)
360 result = dialog.run();
363 case ResponseType.OK:
364 this.identities_manager.add_card(update_id_card_data(dialog, new IdCard()), false);
372 private void edit_identity_cb(IdCard card)
374 var dialog = new IdentityDialog.with_idcard(card, _("Edit Identity"), this);
375 int result = ResponseType.CANCEL;
376 while (!dialog.complete)
377 result = dialog.run();
380 case ResponseType.OK:
381 this.identities_manager.update_card(update_id_card_data(dialog, card));
389 private void remove_identity(IdCardWidget id_card_widget)
391 var id_card = id_card_widget.id_card;
392 this.custom_vbox.remove_id_card_widget(id_card_widget);
394 this.identities_manager.remove_card(id_card);
396 // Nothing is selected, so disable buttons
397 this.edit_button.set_sensitive(false);
398 this.remove_button.set_sensitive(false);
399 this.send_button.set_sensitive(false);
402 private void redraw_id_card_widgets()
404 logger.trace("redraw_id_card_widgets");
409 var children = this.custom_vbox.get_children();
410 this.custom_vbox.clear();
412 if (filter.get_iter_first(out iter))
417 Columns.IDCARD_COL, out id_card);
419 add_id_card_widget(id_card);
421 while (filter.iter_next(ref iter));
425 private void remove_identity_cb(IdCardWidget id_card_widget)
427 var id_card = id_card_widget.id_card;
429 var dialog = new MessageDialog(this,
430 DialogFlags.DESTROY_WITH_PARENT,
431 MessageType.QUESTION,
432 Gtk.ButtonsType.YES_NO,
433 _("Are you sure you want to delete %s ID Card?"), id_card.issuer);
434 var result = dialog.run();
436 case ResponseType.YES:
437 remove_identity(id_card_widget);
445 public void set_prompting_service(string service)
447 prompting_service.set_label( _("Identity requested for service: %s").printf(service) );
450 public void queue_identity_request(IdentityRequest request)
452 if (this.request_queue.is_empty())
453 { /* setup widgets */
454 candidates = request.candidates;
456 redraw_id_card_widgets();
457 set_prompting_service(request.service);
460 this.request_queue.push_tail(request);
464 /** Makes the window visible, or at least, notifies the user that the window
465 * wants to be visible.
467 * This differs from show() in that show() does not guarantee that the
468 * window will be moved to the foreground. Actually, neither does this
469 * method, because the user's settings and window manager may affect the
470 * behavior significantly.
472 public void make_visible()
474 set_urgency_hint(true);
478 public IdCard check_add_password(IdCard identity, IdentityRequest request, IdentityManagerModel model)
480 IdCard retval = identity;
481 bool idcard_has_pw = (identity.password != null) && (identity.password != "");
482 bool request_has_pw = (request.password != null) && (request.password != "");
483 if ((!idcard_has_pw) && (!identity.IsNoIdentity())) {
484 if (request_has_pw) {
485 identity.password = request.password;
486 retval = model.update_card(identity);
488 var dialog = new AddPasswordDialog(identity, request);
489 var result = dialog.run();
492 case ResponseType.OK:
493 identity.password = dialog.password;
494 identity.store_password = dialog.remember;
496 identity.temporary = false;
497 retval = model.update_card(identity);
509 private void send_identity_cb(IdCard id)
511 IdCard identity = id;
512 return_if_fail(request_queue.length > 0);
515 var request = this.request_queue.pop_head();
516 identity = check_add_password(identity, request, identities_manager);
517 if (this.request_queue.is_empty())
520 prompting_service.set_label(_(""));
521 if (!parent_app.explicitly_launched) {
522 // The following occasionally causes the app to exit without sending the dbus
523 // reply, so for now we just don't exit
529 IdentityRequest next = this.request_queue.peek_head();
530 candidates = next.candidates;
531 set_prompting_service(next.service);
534 redraw_id_card_widgets();
536 if ((identity != null) && (!identity.IsNoIdentity()))
537 parent_app.default_id_card = identity;
539 request.return_identity(identity);
542 // private void label_make_bold(Label label)
544 // var font_desc = new Pango.FontDescription();
546 // font_desc.set_weight(Pango.Weight.BOLD);
548 // /* This will only affect the weight of the font, the rest is
549 // * from the current state of the widget, which comes from the
550 // * theme or user prefs, since the font desc only has the
551 // * weight flag turned on.
553 // label.modify_font(font_desc);
556 private void on_about_action()
558 string copyright = "Copyright 2011, 2016 JANET";
562 Copyright (c) 2011, 2016 JANET(UK)
565 Redistribution and use in source and binary forms, with or without
566 modification, are permitted provided that the following conditions
569 1. Redistributions of source code must retain the above copyright
570 notice, this list of conditions and the following disclaimer.
572 2. Redistributions in binary form must reproduce the above copyright
573 notice, this list of conditions and the following disclaimer in the
574 documentation and/or other materials provided with the distribution.
576 3. Neither the name of JANET(UK) nor the names of its contributors
577 may be used to endorse or promote products derived from this software
578 without specific prior written permission.
580 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"
581 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
582 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
583 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
584 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
585 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
586 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
587 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
588 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
589 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
593 Gtk.show_about_dialog(this,
594 "comments", _("Moonshot project UI"),
595 "copyright", copyright,
596 "website", Config.PACKAGE_URL,
597 "version", Config.PACKAGE_VERSION,
599 "website-label", _("Visit the Moonshot project web site"),
600 "translator-credits", _("translator-credits"),
605 private Gtk.ActionEntry[] create_actions() {
606 Gtk.ActionEntry[] actions = new Gtk.ActionEntry[0];
608 Gtk.ActionEntry helpmenu = { "HelpMenuAction",
613 Gtk.ActionEntry about = { "AboutAction",
621 N_("About this application"),
629 private void create_ui_manager()
631 Gtk.ActionGroup action_group = new Gtk.ActionGroup("GeneralActionGroup");
632 action_group.add_actions(create_actions(), this);
633 ui_manager.insert_action_group(action_group, 0);
636 ui_manager.add_ui_from_string(menu_layout, -1);
640 stderr.printf("%s\n", e.message);
641 logger.error("create_ui_manager: Caught error: " + e.message);
643 ui_manager.ensure_update();
646 private void build_ui()
648 // Note: On Debian7/Gtk+2, the menu bar remains gray. This doesn't happen on Debian8/Gtk+3.
649 Gdk.Color white = Gdk.Color();
650 white.red = white.green = white.blue = 65535;
651 this.modify_bg(StateType.NORMAL, white);
655 this.search_entry = new Entry();
657 set_atk_name_description(search_entry, _("Search entry"), _("Search for a specific ID Card"));
658 this.search_entry.set_icon_from_pixbuf(EntryIconPosition.PRIMARY,
659 find_icon_sized("edit-find", Gtk.IconSize.MENU));
660 this.search_entry.set_icon_tooltip_text(EntryIconPosition.PRIMARY,
661 _("Search for an identity or service"));
662 this.search_entry.set_icon_sensitive(EntryIconPosition.PRIMARY, false);
664 this.search_entry.set_icon_from_pixbuf(EntryIconPosition.SECONDARY,
665 find_icon_sized("process-stop", Gtk.IconSize.MENU));
666 this.search_entry.set_icon_tooltip_text(EntryIconPosition.SECONDARY,
667 _("Clear the current search"));
668 this.search_entry.set_icon_sensitive(EntryIconPosition.SECONDARY, false);
671 this.search_entry.icon_press.connect(search_entry_icon_press_cb);
672 this.search_entry.notify["text"].connect(search_entry_text_changed_cb);
673 this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
674 this.search_entry.set_width_chars(30);
677 this.custom_vbox = new CustomVBox(this, false, 2);
679 var viewport = new Viewport(null, null);
680 viewport.set_border_width(2);
681 viewport.set_shadow_type(ShadowType.NONE);
682 viewport.add(custom_vbox);
683 var id_scrollwin = new ScrolledWindow(null, null);
684 id_scrollwin.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
685 id_scrollwin.set_shadow_type(ShadowType.IN);
686 id_scrollwin.add_with_viewport(viewport);
687 this.prompting_service = new Label(_(""));
689 prompting_service.set_alignment(0, (float )0.5);
691 var vbox_left = new VBox(false, 0);
693 var search_hbox = new HBox(false, 6);
694 var search_label = new Label(_("Search:"));
695 search_label.set_alignment(1, (float) 0.5);
696 set_atk_relation(search_label, search_entry, Atk.RelationType.LABEL_FOR);
697 search_hbox.pack_end(search_entry, false, false, 0);
698 search_hbox.pack_end(search_label, false, false, 6);
700 var full_search_label = new Label(_("Search for an identity or service"));
701 full_search_label.set_alignment(1, 1);
702 var search_vbox = new VBox(false, 4);
703 search_vbox.pack_start(search_hbox, false, false, 0);
704 search_vbox.pack_start(full_search_label, false, false, 0);
706 var inner_left_vbox = new VBox(false, 6);
707 inner_left_vbox.pack_start(search_vbox, false, false, 6);
708 inner_left_vbox.pack_start(id_scrollwin, true, true, 0);
710 var id_and_button_box = new HBox(false, 6);
711 id_and_button_box.pack_start(inner_left_vbox, true, true, 6);
712 vbox_left.pack_start(id_and_button_box, true, true, 0);
713 vbox_left.pack_start(prompting_service, false, false, 6);
714 vbox_left.set_size_request(WINDOW_WIDTH, 0);
716 this.no_identity_title = new Label(_("No Identity: Send this identity to services which should not use Moonshot"));
717 no_identity_title.set_alignment(0, (float ) 0.5);
718 no_identity_title.set_line_wrap(true);
719 no_identity_title.show();
721 this.vbox_right = new VBox(false, 6);
723 var add_button = new Button.with_label(_("Add"));
724 add_button.clicked.connect((w) => {add_identity_cb();});
726 this.edit_button = new Button.with_label(_("Edit"));
727 edit_button.clicked.connect((w) => {edit_identity_cb(custom_vbox.current_idcard.id_card);});
728 edit_button.set_sensitive(false);
730 this.remove_button = new Button.with_label(_("Remove"));
731 remove_button.clicked.connect((w) => {remove_identity_cb(custom_vbox.current_idcard);});
732 remove_button.set_sensitive(false);
734 send_button = new Button.with_label(_("Send"));
735 send_button.clicked.connect((w) => {send_identity_cb(custom_vbox.current_idcard.id_card);});
736 // send_button.set_visible(false);
737 send_button.set_sensitive(false);
739 var empty_box = new VBox(false, 0);
740 empty_box.set_size_request(0, 0);
741 vbox_right.pack_start(empty_box, false, false, 14);
742 vbox_right.pack_start(add_button, false, false, 6);
743 vbox_right.pack_start(edit_button, false, false, 6);
744 vbox_right.pack_start(remove_button, false, false, 6);
745 vbox_right.pack_start(send_button, false, false, 24);
747 id_and_button_box.pack_start(vbox_right, false, false, 0);
748 var main_vbox = new VBox(false, 0);
749 main_vbox.set_border_width(12);
752 // hide the File | Quit menu item which is now on the Mac Menu
753 Gtk.Widget quit_item = this.ui_manager.get_widget("/MenuBar/FileMenu/Quit");
756 Gtk.MenuShell menushell = this.ui_manager.get_widget("/MenuBar") as Gtk.MenuShell;
757 osxApp.set_menu_bar(menushell);
758 osxApp.set_use_quartz_accelerators(true);
759 osxApp.sync_menu_bar();
762 var menubar = this.ui_manager.get_widget("/MenuBar");
763 main_vbox.pack_start(menubar, false, false, 0);
765 main_vbox.pack_start(vbox_left, true, true, 0);
767 main_vbox.show_all();
770 private void set_atk_name_description(Widget widget, string name, string description)
772 var atk_widget = widget.get_accessible();
774 atk_widget.set_name(name);
775 atk_widget.set_description(description);
778 private void connect_signals()
780 this.destroy.connect(Gtk.main_quit);
781 this.identities_manager.card_list_changed.connect(this.on_card_list_changed);
784 private static void set_atk_relation(Widget widget, Widget target_widget, Atk.RelationType relationship)
786 var atk_widget = widget.get_accessible();
787 var atk_target_widget = target_widget.get_accessible();
789 atk_widget.add_relationship(relationship, atk_target_widget);