3 class MainWindow : Window
5 private const int WINDOW_WIDTH = 400;
6 private const int WINDOW_HEIGHT = 500;
8 private Entry search_entry;
9 private VBox vbox_rigth;
10 private CustomVBox custom_vbox;
11 private VBox services_internal_vbox;
13 private Entry username_entry;
14 private Entry password_entry;
16 private ListStore listmodel;
17 private TreeModelFilter filter;
19 private IdentitiesManager identities_manager;
21 private MoonshotServer dbus_server;
23 public IdCardWidget selected_id_card_widget;
25 private SourceFunc callback;
39 this.title = "Moonshoot";
40 this.position = WindowPosition.CENTER;
41 set_default_size (WINDOW_WIDTH, WINDOW_HEIGHT);
44 setup_identities_list();
45 load_gss_eap_id_file();
51 private bool visible_func (TreeModel model, TreeIter iter)
55 string issuer_casefold;
56 string search_text_casefold;
59 Columns.ISSUER_COL, out issuer);
60 search_text = this.search_entry.get_text ();
62 if (issuer == null || search_text == null)
65 issuer_casefold = issuer.casefold ();
66 search_text_casefold = search_text.casefold ();
68 if (issuer_casefold.contains (search_text_casefold))
74 private void setup_identities_list ()
76 this.listmodel = new ListStore (Columns.N_COLUMNS, typeof (IdCard),
81 this.filter = new TreeModelFilter (listmodel, null);
83 filter.set_visible_func (visible_func);
86 private void search_entry_icon_press_cb (EntryIconPosition pos, Gdk.Event event)
88 if (pos == EntryIconPosition.PRIMARY)
90 print ("Search entry icon pressed\n");
94 this.search_entry.set_text ("");
98 private void search_entry_text_changed_cb ()
100 this.filter.refilter ();
101 redraw_id_card_widgets ();
103 var has_text = this.search_entry.get_text_length () > 0;
104 this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, has_text);
105 this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, has_text);
107 this.vbox_rigth.set_visible (false);
108 this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
111 private bool search_entry_key_press_event_cb (Gdk.EventKey e)
113 if(Gdk.keyval_name(e.keyval) == "Escape")
114 this.search_entry.set_text("");
116 // Continue processing this event, since the
117 // text entry functionality needs to see it too.
121 private void load_gss_eap_id_file ()
125 this.identities_manager = new IdentitiesManager ();
127 id_card = this.identities_manager.load_gss_eap_id_file ();
130 add_id_card_data (id_card);
131 add_id_card_widget (id_card);
135 private void load_id_cards ()
137 this.identities_manager = new IdentitiesManager ();
139 foreach (IdCard id_card in identities_manager.id_card_list)
141 add_id_card_data (id_card);
142 add_id_card_widget (id_card);
146 private void fill_details (IdCardWidget id_card_widget)
148 var id_card = id_card_widget.id_card;
149 this.username_entry.set_text (id_card.username);
150 this.password_entry.set_text (id_card.password);
152 var children = this.services_internal_vbox.get_children ();
153 foreach (var hbox in children)
155 fill_services_vbox (id_card_widget.id_card);
158 private void show_details (IdCard id_card)
160 this.vbox_rigth.set_visible (!vbox_rigth.get_visible ());
162 if (this.vbox_rigth.get_visible () == false)
164 this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
168 private void details_identity_cb (IdCardWidget id_card_widget)
170 fill_details (id_card_widget);
171 show_details (id_card_widget.id_card);
174 private IdCard get_id_card_data (AddIdentityDialog dialog)
176 var id_card = new IdCard ();
178 id_card.issuer = dialog.issuer;
179 if (id_card.issuer == "")
180 id_card.issuer = "Issuer";
181 id_card.username = dialog.username;
182 id_card.password = dialog.password;
184 var icon_theme = IconTheme.get_default ();
187 id_card.pixbuf = icon_theme.load_icon ("avatar-default",
189 IconLookupFlags.FORCE_SIZE);
193 id_card.pixbuf = null;
194 stdout.printf("Error: %s\n", e.message);
197 id_card.services = {"email","jabber","irc"};
202 private void add_id_card_data (IdCard id_card)
206 this.listmodel.append (out iter);
208 Columns.IDCARD_COL, id_card,
209 Columns.LOGO_COL, id_card.pixbuf,
210 Columns.ISSUER_COL, id_card.issuer,
211 Columns.USERNAME_COL, id_card.username,
212 Columns.PASSWORD_COL, id_card.password);
215 private void remove_id_card_data (IdCard id_card)
220 if (listmodel.get_iter_first (out iter))
225 Columns.ISSUER_COL, out issuer);
227 if (id_card.issuer == issuer)
229 listmodel.remove (iter);
233 while (listmodel.iter_next (ref iter));
237 private void add_id_card_widget (IdCard id_card)
239 var id_card_widget = new IdCardWidget (id_card);
241 this.custom_vbox.add_id_card_widget (id_card_widget);
243 id_card_widget.details_id.connect (details_identity_cb);
244 id_card_widget.remove_id.connect (remove_identity_cb);
245 id_card_widget.send_id.connect (send_identity_cb);
246 id_card_widget.expanded.connect (this.custom_vbox.receive_expanded_event);
247 id_card_widget.expanded.connect (fill_details);
250 private void add_identity (AddIdentityDialog dialog)
252 var id_card = get_id_card_data (dialog);
254 this.identities_manager.id_card_list.prepend (id_card);
255 this.identities_manager.store_id_cards ();
256 this.identities_manager.store_gss_eap_id_file (id_card);
258 add_id_card_data (id_card);
259 add_id_card_widget (id_card);
262 private void add_identity_cb ()
264 var dialog = new AddIdentityDialog ();
265 var result = dialog.run ();
268 case ResponseType.OK:
269 add_identity (dialog);
277 private void remove_id_card_widget (IdCardWidget id_card_widget)
279 remove_id_card_data (id_card_widget.id_card);
281 this.custom_vbox.remove_id_card_widget (id_card_widget);
284 private void remove_identity (IdCardWidget id_card_widget)
286 var id_card = id_card_widget.id_card;
288 this.identities_manager.id_card_list.remove (id_card);
289 this.identities_manager.store_id_cards ();
290 this.identities_manager.store_gss_eap_id_file (null);
292 remove_id_card_widget (id_card_widget);
295 private void redraw_id_card_widgets ()
300 var children = this.custom_vbox.get_children ();
301 foreach (var id_card_widget in children)
302 id_card_widget.destroy();
304 if (filter.get_iter_first (out iter))
309 Columns.IDCARD_COL, out id_card);
311 add_id_card_widget (id_card);
313 while (filter.iter_next (ref iter));
317 private void remove_identity_cb (IdCardWidget id_card_widget)
319 var id_card = id_card_widget.id_card;
321 var dialog = new MessageDialog (null,
322 DialogFlags.DESTROY_WITH_PARENT,
324 Gtk.ButtonsType.YES_NO,
325 _("Are you sure you want to delete %s ID Card?"), id_card.issuer);
326 var result = dialog.run ();
328 case ResponseType.YES:
329 remove_identity (id_card_widget);
337 public void set_callback (SourceFunc callback)
339 this.callback = callback;
342 public void send_identity_cb (IdCardWidget id_card_widget)
344 this.selected_id_card_widget = id_card_widget;
348 private void label_make_bold (Label label)
350 var font_desc = new Pango.FontDescription ();
352 font_desc.set_weight (Pango.Weight.BOLD);
354 /* This will only affect the weight of the font, the rest is
355 * from the current state of the widget, which comes from the
356 * theme or user prefs, since the font desc only has the
357 * weight flag turned on.
359 label.modify_font (font_desc);
362 private void fill_services_vbox (IdCard id_card)
365 var n_columns = id_card.services.length;
367 var services_table = new Table (n_columns, 2, false);
368 services_table.set_col_spacings (10);
369 services_table.set_row_spacings (10);
370 this.services_internal_vbox.add (services_table);
372 foreach (string service in id_card.services)
374 var label = new Label (service);
375 label.set_alignment (0, (float) 0.5);
377 var remove_button = new Button.from_stock (Stock.REMOVE);
379 var remove_button = new Button.from_stock (STOCK_REMOVE);
381 services_table.attach_defaults (label, 0, 1, i, i+1);
382 services_table.attach_defaults (remove_button, 1, 2, i, i+1);
385 this.services_internal_vbox.show_all ();
388 private void build_ui()
390 this.search_entry = new Entry();
392 set_atk_name_description (search_entry, _("Search entry"), _("Search for a specific ID Card"));
393 this.search_entry.set_icon_from_icon_name (EntryIconPosition.PRIMARY,
394 "edit-find-symbolic");
395 this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, false);
396 this.search_entry.set_icon_tooltip_text (EntryIconPosition.PRIMARY,
397 _("Search identity or service"));
399 this.search_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY,
400 "edit-clear-symbolic");
401 this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
402 this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
403 _("Clear the current search"));
405 this.search_entry.icon_press.connect (search_entry_icon_press_cb);
406 this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
407 this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
409 this.custom_vbox = new CustomVBox (false, 6);
411 var viewport = new Viewport (null, null);
412 viewport.set_border_width (6);
413 viewport.set_shadow_type (ShadowType.NONE);
414 viewport.add (custom_vbox);
415 var scroll = new ScrolledWindow (null, null);
416 scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
417 scroll.set_shadow_type (ShadowType.IN);
418 scroll.add_with_viewport (viewport);
420 var button_add = new ToolButton (null, null);
421 button_add.set_icon_name ("list-add-symbolic");
422 set_atk_name_description (button_add, _("Add"), _("Add new ID Card"));
423 button_add.clicked.connect (add_identity_cb);
424 var button_toolbar = new Toolbar ();
425 button_toolbar.insert (button_add, 0);
427 var vbox_left = new VBox (false, 0);
428 vbox_left.pack_start (search_entry, false, false, 6);
429 vbox_left.pack_start (scroll, true, true, 0);
430 vbox_left.pack_start (button_toolbar, false, false, 0);
431 vbox_left.set_size_request (WINDOW_WIDTH, 0);
433 var login_vbox_title = new Label (_("Login: "));
434 label_make_bold (login_vbox_title);
435 login_vbox_title.set_alignment (0, (float) 0.5);
436 var username_label = new Label (_("Username:"));
437 username_label.set_alignment (1, (float) 0.5);
438 this.username_entry = new Entry ();
439 var password_label = new Label (_("Password:"));
440 password_label.set_alignment (1, (float) 0.5);
441 this.password_entry = new Entry ();
442 password_entry.set_invisible_char ('*');
443 password_entry.set_visibility (false);
444 var remember_checkbutton = new CheckButton.with_label (_("Remember password"));
445 var login_table = new Table (3, 3, false);
446 login_table.set_col_spacings (10);
447 login_table.set_row_spacings (10);
448 login_table.attach_defaults (username_label, 0, 1, 0, 1);
449 login_table.attach_defaults (username_entry, 1, 2, 0, 1);
450 login_table.attach_defaults (password_label, 0, 1, 1, 2);
451 login_table.attach_defaults (password_entry, 1, 2, 1, 2);
452 login_table.attach_defaults (remember_checkbutton, 1, 2, 2, 3);
453 var login_vbox_alignment = new Alignment (0, 0, 0, 0);
454 login_vbox_alignment.set_padding (0, 0, 12, 0);
455 login_vbox_alignment.add (login_table);
456 var login_vbox = new VBox (false, 6);
457 login_vbox.pack_start (login_vbox_title, false, true, 0);
458 login_vbox.pack_start (login_vbox_alignment, false, true, 0);
460 var services_vbox_title = new Label (_("Services:"));
461 label_make_bold (services_vbox_title);
462 services_vbox_title.set_alignment (0, (float) 0.5);
463 var services_vbox_alignment = new Alignment (0, 0, 0, 0);
464 services_vbox_alignment.set_padding (0, 0, 12, 0);
465 this.services_internal_vbox = new VBox (true, 6);
466 services_vbox_alignment.add (services_internal_vbox);
467 var services_vbox = new VBox (false, 6);
468 services_vbox.pack_start (services_vbox_title, false, true, 0);
469 services_vbox.pack_start (services_vbox_alignment, false, true, 0);
471 this.vbox_rigth = new VBox (false, 18);
472 vbox_rigth.pack_start (login_vbox, false, true, 0);
473 vbox_rigth.pack_start (services_vbox, false, true, 0);
475 var hbox = new HBox (false, 12);
476 hbox.pack_start (vbox_left, false, false, 0);
477 hbox.pack_start (vbox_rigth, false, false, 0);
479 var main_vbox = new VBox (false, 12);
480 main_vbox.pack_start (hbox, true, true, 0);
481 main_vbox.set_border_width (12);
484 main_vbox.show_all();
485 this.vbox_rigth.hide ();
488 private void set_atk_name_description (Widget widget, string name, string description)
490 var atk_widget = widget.get_accessible ();
492 atk_widget.set_name (name);
493 atk_widget.set_description (description);
496 private void connect_signals()
498 this.destroy.connect (Gtk.main_quit);
501 private void init_dbus_server ()
504 var conn = DBus.Bus.get (DBus.BusType.SESSION);
505 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
506 "/org/freedesktop/DBus",
507 "org.freedesktop.DBus");
509 // try to register service in session bus
510 uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
511 assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
513 this.dbus_server = new MoonshotServer (this);
514 conn.register_object ("/org/janet/moonshot", dbus_server);
519 stderr.printf ("%s\n", e.message);
523 public static int main(string[] args)
527 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
528 Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
529 Intl.textdomain (Config.GETTEXT_PACKAGE);
531 var window = new MainWindow();