4 public class IdentityManagerView : Window {
5 private const int WINDOW_WIDTH = 400;
6 private const int WINDOW_HEIGHT = 500;
7 protected IdentityManagerApp parent_app;
9 public OSXApplication osxApp;
11 private UIManager ui_manager = new UIManager();
12 private Entry search_entry;
13 private VBox vbox_right;
14 private CustomVBox custom_vbox;
15 private VBox services_internal_vbox;
17 private Entry username_entry;
18 private Entry password_entry;
20 private ListStore* listmodel;
21 private TreeModelFilter filter;
23 public IdentityManagerModel identities_manager;
24 private unowned SList<IdCard> candidates;
26 public GLib.Queue<IdentityRequest> request_queue;
28 private HashTable<Gtk.Button, string> service_button_map;
40 private const string layout =
41 "<menubar name='MenuBar'>" +
42 " <menu name='FileMenu' action='FileMenuAction'>" +
43 " <menuitem name='AddIdCard' action='AddIdCardAction' />" +
45 " <menuitem name='Quit' action='QuitAction' />" +
48 " <menu name='HelpMenu' action='HelpMenuAction'>" +
49 " <menuitem name='About' action='AboutAction' />" +
53 public IdentityManagerView(IdentityManagerApp app) {
56 osxApp = OSXApplication.get_instance();
58 identities_manager = parent_app.model;
59 request_queue = new GLib.Queue<IdentityRequest>();
60 service_button_map = new HashTable<Gtk.Button, string> (direct_hash, direct_equal);
61 this.title = "Moonshot Identity Selector";
62 this.set_position (WindowPosition.CENTER);
63 set_default_size (WINDOW_WIDTH, WINDOW_HEIGHT);
70 public void on_card_list_changed () {
74 private bool visible_func (TreeModel model, TreeIter iter)
79 Columns.IDCARD_COL, out id_card);
84 if (candidates != null)
86 bool is_candidate = false;
87 foreach (IdCard candidate in candidates)
89 if (candidate == id_card)
96 string entry_text = search_entry.get_text ();
97 if (entry_text == null || entry_text == "")
102 foreach (string search_text in entry_text.split(" "))
104 if (search_text == "")
108 string search_text_casefold = search_text.casefold ();
110 if (id_card.issuer != null)
112 string issuer_casefold = id_card.issuer;
114 if (issuer_casefold.contains (search_text_casefold))
118 if (id_card.display_name != null)
120 string display_name_casefold = id_card.display_name.casefold ();
122 if (display_name_casefold.contains (search_text_casefold))
126 if (id_card.services.length > 0)
128 foreach (string service in id_card.services)
130 string service_casefold = service.casefold ();
132 if (service_casefold.contains (search_text_casefold))
140 private void setup_list_model ()
142 this.listmodel = new ListStore (Columns.N_COLUMNS, typeof (IdCard),
147 this.filter = new TreeModelFilter (listmodel, null);
149 filter.set_visible_func (visible_func);
152 private void search_entry_icon_press_cb (EntryIconPosition pos, Gdk.Event event)
154 if (pos == EntryIconPosition.PRIMARY)
156 print ("Search entry icon pressed\n");
160 this.search_entry.set_text ("");
164 private void search_entry_text_changed_cb ()
166 this.filter.refilter ();
167 redraw_id_card_widgets ();
169 var has_text = this.search_entry.get_text_length () > 0;
170 this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, has_text);
171 this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, has_text);
173 this.vbox_right.set_visible (false);
176 private bool search_entry_key_press_event_cb (Gdk.EventKey e)
178 if(Gdk.keyval_name(e.keyval) == "Escape")
179 this.search_entry.set_text("");
181 // Continue processing this event, since the
182 // text entry functionality needs to see it too.
186 private void load_id_cards () {
187 var children = this.custom_vbox.get_children ();
188 foreach (var id_card_widget in children) {
189 remove_id_card_widget((IdCardWidget)id_card_widget);
191 this.listmodel->clear();
192 LinkedList<IdCard> card_list = identities_manager.get_card_list() ;
193 if (card_list == null) {
197 foreach (IdCard id_card in card_list) {
198 add_id_card_data (id_card);
199 add_id_card_widget (id_card);
203 private void fill_details (IdCardWidget id_card_widget)
205 var id_card = id_card_widget.id_card;
206 this.username_entry.set_text (id_card.username);
207 this.password_entry.set_text (id_card.password ?? "");
209 var children = this.services_internal_vbox.get_children ();
210 foreach (var hbox in children)
211 services_internal_vbox.remove(hbox);
212 fill_services_vbox (id_card_widget.id_card);
213 // identities_manager.store_id_cards();
216 private void show_details (IdCard id_card)
218 this.vbox_right.set_visible (!vbox_right.get_visible ());
220 if (this.vbox_right.get_visible () == false)
222 this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
226 private void details_identity_cb (IdCardWidget id_card_widget)
228 fill_details (id_card_widget);
229 show_details (id_card_widget.id_card);
232 private IdCard get_id_card_data (AddIdentityDialog dialog)
234 var id_card = new IdCard ();
236 id_card.display_name = dialog.display_name;
237 id_card.issuer = dialog.issuer;
238 if (id_card.issuer == "")
239 id_card.issuer = "Issuer";
240 id_card.username = dialog.username;
241 id_card.password = dialog.password;
242 id_card.services = {};
243 id_card.set_data("pixbuf", find_icon ("avatar-default", 48));
248 private void add_id_card_data (IdCard id_card)
252 this.listmodel->append (out iter);
253 pixbuf = id_card.get_data("pixbuf");
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);
262 private void remove_id_card_data (IdCard id_card)
267 if (listmodel->get_iter_first (out iter))
271 listmodel->get (iter,
272 Columns.ISSUER_COL, out issuer);
274 if (id_card.issuer == issuer)
276 listmodel->remove (iter);
280 while (listmodel->iter_next (ref iter));
284 private void add_id_card_widget (IdCard id_card)
286 var id_card_widget = new IdCardWidget (id_card);
287 this.custom_vbox.add_id_card_widget (id_card_widget);
288 id_card_widget.details_id.connect (details_identity_cb);
289 id_card_widget.remove_id.connect (remove_identity_cb);
290 id_card_widget.send_id.connect ((w) => send_identity_cb (w.id_card));
291 id_card_widget.expanded.connect (this.custom_vbox.receive_expanded_event);
292 id_card_widget.expanded.connect (fill_details);
295 /* This method finds a valid display name */
296 public bool display_name_is_valid (string name,
297 out string? candidate)
299 foreach (IdCard id_card in identities_manager.get_card_list())
301 if (id_card.display_name == name)
303 if (&candidate != null)
305 for (int i=0; i<1000; i++)
307 string tmp = "%s %d".printf (name, i);
308 if (display_name_is_valid (tmp, null))
322 public void insert_id_card (IdCard id_card)
326 if (!display_name_is_valid (id_card.display_name, out candidate))
328 id_card.display_name = candidate;
331 this.identities_manager.add_card(id_card);
334 public bool add_identity (IdCard id_card)
338 * TODO: We should have a confirmation dialog, but currently it will crash on Mac OS
339 * so for now we will install silently
341 var ret = Gtk.ResponseType.YES;
344 var dialog = new Gtk.MessageDialog (this,
345 Gtk.DialogFlags.DESTROY_WITH_PARENT,
346 Gtk.MessageType.QUESTION,
347 Gtk.ButtonsType.YES_NO,
348 _("Would you like to add '%s' ID Card to the ID Card Organizer?"),
349 id_card.display_name);
351 var ret = dialog.run ();
355 if (ret == Gtk.ResponseType.YES) {
356 id_card.set_data ("pixbuf", find_icon ("avatar-default", 48));
357 this.insert_id_card (id_card);
364 private void add_identity_manual_cb ()
366 var dialog = new AddIdentityDialog ();
367 var result = dialog.run ();
370 case ResponseType.OK:
371 insert_id_card (get_id_card_data (dialog));
379 private void remove_id_card_widget (IdCardWidget id_card_widget) {
380 this.custom_vbox.remove_id_card_widget (id_card_widget);
383 private void remove_identity (IdCardWidget id_card_widget)
385 var id_card = id_card_widget.id_card;
386 remove_id_card_widget (id_card_widget);
388 this.identities_manager.remove_card(id_card);
391 private void redraw_id_card_widgets ()
396 var children = this.custom_vbox.get_children ();
397 foreach (var id_card_widget in children)
398 remove_id_card_widget((IdCardWidget )id_card_widget); //id_card_widget.destroy();
400 if (filter.get_iter_first (out iter))
405 Columns.IDCARD_COL, out id_card);
407 add_id_card_widget (id_card);
409 while (filter.iter_next (ref iter));
413 private void remove_identity_cb (IdCardWidget id_card_widget)
415 var id_card = id_card_widget.id_card;
417 var dialog = new MessageDialog (this,
418 DialogFlags.DESTROY_WITH_PARENT,
419 MessageType.QUESTION,
420 Gtk.ButtonsType.YES_NO,
421 _("Are you sure you want to delete %s ID Card?"), id_card.issuer);
422 var result = dialog.run ();
424 case ResponseType.YES:
425 remove_identity (id_card_widget);
433 public void queue_identity_request(IdentityRequest request)
435 if (this.request_queue.is_empty())
436 { /* setup widgets */
437 candidates = request.candidates;
439 redraw_id_card_widgets ();
442 this.request_queue.push_tail (request);
445 public void send_identity_cb (IdCard identity)
447 return_if_fail (request_queue.length > 0);
450 var request = this.request_queue.pop_head ();
451 bool reset_password = false;
453 if (identity.password == null)
455 var dialog = new AddPasswordDialog ();
456 var result = dialog.run ();
459 case ResponseType.OK:
460 identity.password = dialog.password;
461 reset_password = ! dialog.remember;
471 if (this.request_queue.is_empty())
474 if (!parent_app.explicitly_launched)
477 candidates = this.request_queue.peek_head().candidates;
480 redraw_id_card_widgets ();
482 if (identity != null)
483 parent_app.default_id_card = identity;
485 request.return_identity (identity);
488 identity.password = null;
492 private void label_make_bold (Label label)
494 var font_desc = new Pango.FontDescription ();
496 font_desc.set_weight (Pango.Weight.BOLD);
498 /* This will only affect the weight of the font, the rest is
499 * from the current state of the widget, which comes from the
500 * theme or user prefs, since the font desc only has the
501 * weight flag turned on.
503 label.modify_font (font_desc);
506 private void fill_services_vbox (IdCard id_card)
509 var n_columns = id_card.services.length;
511 var services_table = new Table (n_columns, 2, false);
512 services_table.set_col_spacings (10);
513 services_table.set_row_spacings (10);
514 this.services_internal_vbox.add (services_table);
516 service_button_map.remove_all ();
518 foreach (string service in id_card.services)
520 var label = new Label (service);
521 label.set_alignment (0, (float) 0.5);
523 var remove_button = new Button.from_stock (Stock.REMOVE);
525 var remove_button = new Button.from_stock (STOCK_REMOVE);
529 service_button_map.insert (remove_button, service);
531 remove_button.clicked.connect ((remove_button) =>
533 var dialog = new Gtk.MessageDialog (this,
534 Gtk.DialogFlags.DESTROY_WITH_PARENT,
535 Gtk.MessageType.QUESTION,
536 Gtk.ButtonsType.YES_NO,
537 _("Are you sure you want to stop '%s' ID Card from being used with %s?"),
538 custom_vbox.current_idcard.id_card.display_name,
540 var ret = dialog.run();
543 if (ret == Gtk.ResponseType.YES)
545 IdCard idcard = custom_vbox.current_idcard.id_card;
546 var candidate = service_button_map.lookup (remove_button);
548 SList<string> services = new SList<string>();
550 foreach (string srv in idcard.services)
552 if (srv == candidate)
554 services.append (srv);
557 idcard.services = new string[services.length()];
558 for (int j=0; j<idcard.services.length; j++)
560 idcard.services[j] = services.nth_data(j);
563 var children = services_internal_vbox.get_children ();
564 foreach (var hbox in children)
565 services_internal_vbox.remove(hbox);
567 fill_services_vbox (idcard);
568 custom_vbox.current_idcard.update_id_card_label ();
569 identities_manager.update_card(idcard);
573 services_table.attach_defaults (label, 0, 1, i, i+1);
574 services_table.attach_defaults (remove_button, 1, 2, i, i+1);
577 this.services_internal_vbox.show_all ();
580 private void on_about_action ()
583 "Javier Jardón <jjardon@codethink.co.uk>",
584 "Sam Thursfield <samthursfield@codethink.co.uk>",
585 "Alberto Ruiz <alberto.ruiz@codethink.co.uk>",
589 string copyright = "Copyright 2011 JANET";
593 Copyright (c) 2011, JANET(UK)
596 Redistribution and use in source and binary forms, with or without
597 modification, are permitted provided that the following conditions
600 1. Redistributions of source code must retain the above copyright
601 notice, this list of conditions and the following disclaimer.
603 2. Redistributions in binary form must reproduce the above copyright
604 notice, this list of conditions and the following disclaimer in the
605 documentation and/or other materials provided with the distribution.
607 3. Neither the name of JANET(UK) nor the names of its contributors
608 may be used to endorse or promote products derived from this software
609 without specific prior written permission.
611 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"
612 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
613 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
614 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
615 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
616 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
617 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
618 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
619 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
620 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
624 Gtk.show_about_dialog (this,
625 "comments", _("Moonshot project UI"),
626 "copyright", copyright,
627 "website", Config.PACKAGE_URL,
628 "version", Config.PACKAGE_VERSION,
630 "website-label", _("Visit the Moonshot project web site"),
632 "translator-credits", _("translator-credits"),
637 private Gtk.ActionEntry[] create_actions() {
638 Gtk.ActionEntry[] actions = new Gtk.ActionEntry[0];
640 Gtk.ActionEntry filemenu = { "FileMenuAction",
645 Gtk.ActionEntry add = { "AddIdCardAction",
653 N_("Add a new ID Card"),
654 add_identity_manual_cb };
656 Gtk.ActionEntry quit = { "QuitAction",
664 N_("Quit the application"),
668 Gtk.ActionEntry helpmenu = { "HelpMenuAction",
673 Gtk.ActionEntry about = { "AboutAction",
681 N_("About this application"),
689 private void create_ui_manager ()
691 Gtk.ActionGroup action_group = new Gtk.ActionGroup ("GeneralActionGroup");
692 action_group.add_actions (create_actions (), this);
693 ui_manager.insert_action_group (action_group, 0);
696 ui_manager.add_ui_from_string (layout, -1);
700 stderr.printf ("%s\n", e.message);
702 ui_manager.ensure_update ();
705 private void build_ui()
707 create_ui_manager ();
709 this.search_entry = new Entry();
711 set_atk_name_description (search_entry, _("Search entry"), _("Search for a specific ID Card"));
712 this.search_entry.set_icon_from_pixbuf (EntryIconPosition.PRIMARY,
713 find_icon_sized ("edit-find", Gtk.IconSize.MENU));
714 // find_icon_sized ("edit-find-symbolic", Gtk.IconSize.MENU));
715 this.search_entry.set_icon_tooltip_text (EntryIconPosition.PRIMARY,
716 _("Search identity or service"));
717 this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, false);
719 this.search_entry.set_icon_from_pixbuf (EntryIconPosition.SECONDARY,
720 find_icon_sized ("process-stop", Gtk.IconSize.MENU));
721 // find_icon_sized ("edit-clear-symbolic", Gtk.IconSize.MENU));
722 this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
723 _("Clear the current search"));
724 this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
727 this.search_entry.icon_press.connect (search_entry_icon_press_cb);
728 this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
729 this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
731 this.custom_vbox = new CustomVBox (this, false, 6);
733 var viewport = new Viewport (null, null);
734 viewport.set_border_width (6);
735 viewport.set_shadow_type (ShadowType.NONE);
736 viewport.add (custom_vbox);
737 var scroll = new ScrolledWindow (null, null);
738 scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
739 scroll.set_shadow_type (ShadowType.IN);
740 scroll.add_with_viewport (viewport);
742 var vbox_left = new VBox (false, 0);
743 vbox_left.pack_start (search_entry, false, false, 6);
744 vbox_left.pack_start (scroll, true, true, 0);
745 vbox_left.set_size_request (WINDOW_WIDTH, 0);
747 var login_vbox_title = new Label (_("Login: "));
748 label_make_bold (login_vbox_title);
749 login_vbox_title.set_alignment (0, (float) 0.5);
750 var username_label = new Label (_("Username:"));
751 username_label.set_alignment (1, (float) 0.5);
752 this.username_entry = new Entry ();
753 var password_label = new Label (_("Password:"));
754 password_label.set_alignment (1, (float) 0.5);
755 this.password_entry = new Entry ();
756 password_entry.set_invisible_char ('*');
757 password_entry.set_visibility (false);
758 var remember_checkbutton = new CheckButton.with_label (_("Remember password"));
759 var login_table = new Table (3, 3, false);
760 login_table.set_col_spacings (10);
761 login_table.set_row_spacings (10);
762 login_table.attach_defaults (username_label, 0, 1, 0, 1);
763 login_table.attach_defaults (username_entry, 1, 2, 0, 1);
764 login_table.attach_defaults (password_label, 0, 1, 1, 2);
765 login_table.attach_defaults (password_entry, 1, 2, 1, 2);
766 login_table.attach_defaults (remember_checkbutton, 1, 2, 2, 3);
767 var login_vbox_alignment = new Alignment (0, 0, 0, 0);
768 login_vbox_alignment.set_padding (0, 0, 12, 0);
769 login_vbox_alignment.add (login_table);
770 var login_vbox = new VBox (false, 6);
771 login_vbox.pack_start (login_vbox_title, false, true, 0);
772 login_vbox.pack_start (login_vbox_alignment, false, true, 0);
774 var services_vbox_title = new Label (_("Services:"));
775 label_make_bold (services_vbox_title);
776 services_vbox_title.set_alignment (0, (float) 0.5);
777 var services_vbox_alignment = new Alignment (0, 0, 0, 0);
778 services_vbox_alignment.set_padding (0, 0, 12, 0);
779 this.services_internal_vbox = new VBox (true, 6);
780 services_vbox_alignment.add (services_internal_vbox);
781 var services_vbox = new VBox (false, 6);
782 services_vbox.pack_start (services_vbox_title, false, true, 0);
783 services_vbox.pack_start (services_vbox_alignment, false, true, 0);
785 this.vbox_right = new VBox (false, 18);
786 vbox_right.pack_start (login_vbox, false, true, 0);
787 vbox_right.pack_start (services_vbox, false, true, 0);
789 var hbox = new HBox (false, 12);
790 hbox.pack_start (vbox_left, true, true, 0);
791 hbox.pack_start (vbox_right, false, false, 0);
793 var main_vbox = new VBox (false, 0);
794 main_vbox.set_border_width (12);
797 // hide the File | Quit menu item which is now on the Mac Menu
798 Gtk.Widget quit_item = this.ui_manager.get_widget("/MenuBar/FileMenu/Quit");
801 Gtk.MenuShell menushell = this.ui_manager.get_widget("/MenuBar") as Gtk.MenuShell;
802 osxApp.set_menu_bar(menushell);
803 osxApp.set_use_quartz_accelerators(true);
804 osxApp.sync_menu_bar();
807 var menubar = this.ui_manager.get_widget ("/MenuBar");
808 main_vbox.pack_start (menubar, false, false, 0);
810 main_vbox.pack_start (hbox, true, true, 0);
812 main_vbox.show_all();
813 this.vbox_right.hide ();
816 private void set_atk_name_description (Widget widget, string name, string description)
818 var atk_widget = widget.get_accessible ();
820 atk_widget.set_name (name);
821 atk_widget.set_description (description);
824 private void connect_signals()
826 this.destroy.connect (Gtk.main_quit);
827 this.identities_manager.card_list_changed.connect(this.on_card_list_changed);