2 * Copyright (c) 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 // Defined here as workaround for emacs vala-mode indentation failure.
37 static const string CANCEL = Stock.CANCEL;
39 static const string CANCEL = STOCK_CANCEL;
43 class IdentityDialog : Dialog
45 private static Gdk.Color white = make_color(65535, 65535, 65535);
46 private static Gdk.Color selected_color = make_color(0xd9 << 8, 0xf7 << 8, 65535);
47 private static Gdk.Color alt_color = make_color(0xf2 << 8, 0xf2 << 8, 0xf2 << 8);
49 private static Gdk.Color make_color(uint16 red, uint16 green, uint16 blue)
51 Gdk.Color color = Gdk.Color();
59 private static MoonshotLogger logger = get_logger("IdentityDialog");
61 static const string displayname_labeltext = _("Display Name");
62 static const string realm_labeltext = _("Realm");
63 static const string username_labeltext = _("Username");
64 static const string password_labeltext = _("Password");
66 private IdentityManagerView parent;
67 private Entry displayname_entry;
68 private Label displayname_label;
69 private Entry realm_entry;
70 private Label realm_label;
71 private Entry username_entry;
72 private Label username_label;
73 private Entry password_entry;
74 private Label password_label;
75 private CheckButton remember_checkbutton;
76 private Label message_label;
80 private Label selected_item = null;
82 public string display_name {
83 get { return displayname_entry.get_text(); }
86 public string issuer {
87 get { return realm_entry.get_text(); }
90 public string username {
91 get { return username_entry.get_text(); }
94 public string password {
95 get { return password_entry.get_text(); }
98 public bool store_password {
99 get { return remember_checkbutton.active; }
102 internal string[] get_services()
104 return card.services;
107 public IdentityDialog(IdentityManagerView parent)
109 this.with_idcard(null, _("Add ID Card"), parent);
112 public IdentityDialog.with_idcard(IdCard? a_card, string title, IdentityManagerView parent)
114 bool is_new_card = false;
120 card = a_card ?? new IdCard();
121 this.set_title(title);
122 this.set_modal(true);
123 this.set_transient_for(parent);
124 this.parent = parent;
126 this.add_buttons(_("OK"), ResponseType.OK, CANCEL, ResponseType.CANCEL);
127 Box content_area = (Box) this.get_content_area();
129 displayname_label = new Label(@"$displayname_labeltext:");
130 displayname_label.set_alignment(0, (float) 0.5);
131 displayname_entry = new Entry();
132 displayname_entry.set_text(card.display_name);
133 displayname_entry.set_width_chars(40);
135 realm_label = new Label(@"$realm_labeltext:");
136 realm_label.set_alignment(0, (float) 0.5);
137 realm_entry = new Entry();
138 realm_entry.set_text(card.issuer);
139 realm_entry.set_width_chars(60);
141 username_label = new Label(@"$username_labeltext:");
142 username_label.set_alignment(0, (float) 0.5);
143 username_entry = new Entry();
144 username_entry.set_text(card.username);
145 username_entry.set_width_chars(40);
147 password_label = new Label(@"$password_labeltext:");
148 password_label.set_alignment(0, (float) 0.5);
149 password_entry = new Entry();
150 password_entry.set_invisible_char('*');
151 password_entry.set_visibility(false);
152 password_entry.set_text(card.password);
153 password_entry.set_width_chars(40);
155 remember_checkbutton = new CheckButton.with_label(_("Remember password"));
156 message_label = new Label("");
157 message_label.set_visible(false);
159 set_atk_relation(displayname_label, displayname_entry, Atk.RelationType.LABEL_FOR);
160 set_atk_relation(realm_label, realm_entry, Atk.RelationType.LABEL_FOR);
161 set_atk_relation(username_label, username_entry, Atk.RelationType.LABEL_FOR);
162 set_atk_relation(password_entry, password_entry, Atk.RelationType.LABEL_FOR);
164 content_area.pack_start(message_label, false, false, 6);
165 add_as_vbox(content_area, displayname_label, displayname_entry);
166 add_as_vbox(content_area, username_label, username_entry);
167 add_as_vbox(content_area, realm_label, realm_entry);
168 add_as_vbox(content_area, password_label, password_entry);
170 // var entries = new VBox(false, 6);
171 // add_as_vbox(entries, displayname_label, displayname_entry);
172 // add_as_vbox(entries, realm_label, realm_entry);
173 // add_as_vbox(entries, username_label, username_entry);
174 // add_as_vbox(entries, password_label, password_entry);
175 // content_area.pack_start(entries, false, false, 0);
177 var remember_hbox = new HBox(false, 40);
178 remember_hbox.pack_start(new HBox(false, 0), false, false, 0);
179 remember_hbox.pack_start(remember_checkbutton, false, false, 0);
180 content_area.pack_start(remember_hbox, false, false, 2);
181 // content_area.pack_start(remember_checkbutton, false, false, 2);
183 this.response.connect(on_response);
184 content_area.set_border_width(6);
188 var services_vbox = make_services_vbox();
189 content_area.pack_start(services_vbox);
192 this.set_border_width(6);
193 this.set_resizable(false);
194 this.modify_bg(StateType.NORMAL, white);
198 private static void add_as_vbox(Box content_area, Label label, Entry entry)
200 VBox vbox = new VBox(false, 2);
202 vbox.pack_start(label, false, false, 0);
203 vbox.pack_start(entry, false, false, 0);
205 // Hack to prevent the text entries from stretching horizontally
206 HBox hbox = new HBox(false, 0);
207 hbox.pack_start(vbox, false, false, 0);
208 content_area.pack_start(hbox, false, false, 6);
211 private static string update_preamble(string preamble)
214 return _("Missing required field: ");
215 return _("Missing required fields: ");
218 private static string update_message(string old_message, string new_item)
221 if (old_message == "")
224 message = old_message + ", " + new_item;
228 private static void check_field(string field, Label label, string fieldname, ref string preamble, ref string message)
231 label.set_markup(@"$fieldname:");
234 label.set_markup(@"<span foreground=\"red\">$fieldname:</span>");
235 preamble = update_preamble(preamble);
236 message = update_message(message, fieldname);
239 private bool check_fields()
241 string preamble = "";
243 string password_test = store_password ? password : "not required";
244 check_field(display_name, displayname_label, displayname_labeltext, ref preamble, ref message);
245 check_field(username, username_label, username_labeltext, ref preamble, ref message);
246 check_field(issuer, realm_label, realm_labeltext, ref preamble, ref message);
247 check_field(password_test, password_label, password_labeltext, ref preamble, ref message);
249 message_label.set_visible(true);
250 message_label.set_markup(@"<span foreground=\"red\">$preamble$message</span>");
256 private void on_response(Dialog source, int response_id)
258 switch (response_id) {
259 case ResponseType.OK:
260 complete = check_fields();
262 case ResponseType.CANCEL:
268 private void set_atk_relation(Widget widget, Widget target_widget, Atk.RelationType relationship)
270 var atk_widget = widget.get_accessible();
271 var atk_target_widget = target_widget.get_accessible();
273 atk_widget.add_relationship(relationship, atk_target_widget);
276 private static void label_make_bold(Label label)
278 var font_desc = new Pango.FontDescription();
280 font_desc.set_weight(Pango.Weight.BOLD);
282 /* This will only affect the weight of the font. The rest is
283 * from the current state of the widget, which comes from the
284 * theme or user prefs, since the font desc only has the
285 * weight flag turned on.
287 label.modify_font(font_desc);
290 private VBox make_services_vbox()
292 logger.trace("make_services_vbox");
294 var services_vbox_alignment = new Alignment(0, 0, 0, 1);
295 services_vbox_alignment.set_padding(6, 6, 6, 6);
296 var services_vscroll = new ScrolledWindow(null, null);
297 services_vscroll.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
298 services_vscroll.set_shadow_type(ShadowType.IN);
299 services_vscroll.set_size_request(0, 60);
300 services_vscroll.add_with_viewport(services_vbox_alignment);
303 var remove_button = new Button.from_stock(Stock.REMOVE);
305 var remove_button = new Button.from_stock(STOCK_REMOVE);
307 remove_button.set_sensitive(false);
310 var services_table = new Table(card.services.length, 1, false);
311 services_table.set_row_spacings(5);
313 var table_button_hbox = new HBox(false, 6);
314 table_button_hbox.pack_start(services_vscroll, true, true, 6);
316 // Hack to prevent the button from growing vertically
317 VBox fixed_height = new VBox(false, 0);
318 fixed_height.pack_start(remove_button, false, false, 0);
319 table_button_hbox.pack_start(fixed_height, false, false, 6);
320 services_vbox_alignment.add(services_table);
322 var services_vbox_title = new Label(_("Services:"));
323 label_make_bold(services_vbox_title);
324 services_vbox_title.set_alignment(0, (float) 0.5);
326 var services_vbox = new VBox(false, 6);
327 services_vbox.pack_start(services_vbox_title, false, false, 6);
328 services_vbox.pack_start(table_button_hbox, true, true, 6);
331 foreach (string service in card.services)
333 var label = new Label(service);
334 label.set_alignment(0, (float) 0);
336 EventBox event_box = new EventBox();
337 event_box.add(label);
338 event_box.button_press_event.connect(() =>
340 var state = label.get_state();
341 if (selected_item == label)
344 selected_item.modify_bg(state, white);
345 selected_item = null;
346 remove_button.set_sensitive(false);
350 if (selected_item != null)
353 selected_item.modify_bg(state, white);
354 selected_item = null;
358 selected_item = label;
359 selected_item.modify_bg(state, selected_color);
360 remove_button.set_sensitive(true);
365 services_table.attach_defaults(event_box, 0, 1, i, i+1);
369 remove_button.clicked.connect((remove_button) =>
371 var dialog = new Gtk.MessageDialog(this,
372 Gtk.DialogFlags.DESTROY_WITH_PARENT,
373 Gtk.MessageType.QUESTION,
374 Gtk.ButtonsType.YES_NO,
375 _("You are about to remove the service '%s'. Are you sure you want to do this?"),
376 selected_item.label);
377 var ret = dialog.run();
380 if (ret == Gtk.ResponseType.YES)
383 SList<string> services = new SList<string>();
385 foreach (string srv in card.services)
387 if (srv != selected_item.label)
388 services.append(srv);
391 card.services = new string[services.length()];
392 for (int j = 0; j < card.services.length; j++)
394 card.services[j] = services.nth_data(j);
397 services_table.remove(selected_item.parent);
398 selected_item = null;
399 remove_button.set_sensitive(false);
405 return services_vbox;