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
37 // Defined here as workaround for emacs vala-mode indentation failure.
39 static const string CANCEL = Stock.CANCEL;
41 static const string CANCEL = STOCK_CANCEL;
45 class IdentityDialog : Dialog
47 private static Gdk.Color white = make_color(65535, 65535, 65535);
48 private static Gdk.Color selected_color = make_color(0xd9 << 8, 0xf7 << 8, 65535);
50 private static MoonshotLogger logger = get_logger("IdentityDialog");
52 static const string displayname_labeltext = _("Display Name");
53 static const string realm_labeltext = _("Realm");
54 static const string username_labeltext = _("Username");
55 static const string password_labeltext = _("Password");
57 private Entry displayname_entry;
58 private Label displayname_label;
59 private Entry realm_entry;
60 private Label realm_label;
61 private Entry username_entry;
62 private Label username_label;
63 private Entry password_entry;
64 private Label password_label;
65 private CheckButton remember_checkbutton;
66 private Label message_label;
70 private Label selected_item = null;
72 public string display_name {
73 get { return displayname_entry.get_text(); }
76 public string issuer {
77 get { return realm_entry.get_text(); }
80 public string username {
81 get { return username_entry.get_text(); }
84 public string password {
85 get { return password_entry.get_text(); }
88 public bool store_password {
89 get { return remember_checkbutton.active; }
92 internal ArrayList<string> get_services()
97 public IdentityDialog(IdentityManagerView parent)
99 this.with_idcard(null, _("Add ID Card"), parent);
102 public IdentityDialog.with_idcard(IdCard? a_card, string title, IdentityManagerView parent)
104 bool is_new_card = false;
110 card = a_card ?? new IdCard();
111 this.set_title(title);
112 this.set_modal(true);
113 this.set_transient_for(parent);
115 this.add_buttons(_("OK"), ResponseType.OK, CANCEL, ResponseType.CANCEL);
116 Box content_area = (Box) this.get_content_area();
118 displayname_label = new Label(@"$displayname_labeltext:");
119 displayname_label.set_alignment(0, (float) 0.5);
120 displayname_entry = new Entry();
121 displayname_entry.set_text(card.display_name);
122 displayname_entry.set_width_chars(40);
124 realm_label = new Label(@"$realm_labeltext:");
125 realm_label.set_alignment(0, (float) 0.5);
126 realm_entry = new Entry();
127 realm_entry.set_text(card.issuer);
128 realm_entry.set_width_chars(60);
130 username_label = new Label(@"$username_labeltext:");
131 username_label.set_alignment(0, (float) 0.5);
132 username_entry = new Entry();
133 username_entry.set_text(card.username);
134 username_entry.set_width_chars(40);
136 password_label = new Label(@"$password_labeltext:");
137 password_label.set_alignment(0, (float) 0.5);
139 remember_checkbutton = new CheckButton.with_label(_("Remember password"));
140 remember_checkbutton.active = card.store_password;
142 password_entry = new Entry();
143 password_entry.set_invisible_char('*');
144 password_entry.set_visibility(false);
145 password_entry.set_width_chars(40);
146 password_entry.set_text(card.password);
148 message_label = new Label("");
149 message_label.set_visible(false);
151 set_atk_relation(displayname_label, displayname_entry, Atk.RelationType.LABEL_FOR);
152 set_atk_relation(realm_label, realm_entry, Atk.RelationType.LABEL_FOR);
153 set_atk_relation(username_label, username_entry, Atk.RelationType.LABEL_FOR);
154 set_atk_relation(password_label, password_entry, Atk.RelationType.LABEL_FOR);
156 content_area.pack_start(message_label, false, false, 6);
157 add_as_vbox(content_area, displayname_label, displayname_entry);
158 add_as_vbox(content_area, username_label, username_entry);
159 add_as_vbox(content_area, realm_label, realm_entry);
160 add_as_vbox(content_area, password_label, password_entry);
162 // var entries = new VBox(false, 6);
163 // add_as_vbox(entries, displayname_label, displayname_entry);
164 // add_as_vbox(entries, realm_label, realm_entry);
165 // add_as_vbox(entries, username_label, username_entry);
166 // add_as_vbox(entries, password_label, password_entry);
167 // content_area.pack_start(entries, false, false, 0);
169 var remember_hbox = new HBox(false, 40);
170 remember_hbox.pack_start(new HBox(false, 0), false, false, 0);
171 remember_hbox.pack_start(remember_checkbutton, false, false, 0);
172 content_area.pack_start(remember_hbox, false, false, 2);
173 // content_area.pack_start(remember_checkbutton, false, false, 2);
175 this.response.connect(on_response);
176 content_area.set_border_width(6);
180 var services_vbox = make_services_vbox();
181 content_area.pack_start(services_vbox);
184 if (card.is_no_identity())
186 displayname_entry.set_sensitive(false);
187 realm_entry.set_sensitive(false);
188 username_entry.set_sensitive(false);
189 password_entry.set_sensitive(false);
190 remember_checkbutton.set_sensitive(false);
193 this.set_border_width(6);
194 this.set_resizable(false);
195 this.modify_bg(StateType.NORMAL, white);
199 private static void add_as_vbox(Box content_area, Label label, Entry entry)
201 VBox vbox = new VBox(false, 2);
203 vbox.pack_start(label, false, false, 0);
204 vbox.pack_start(entry, false, false, 0);
206 // Hack to prevent the text entries from stretching horizontally
207 HBox hbox = new HBox(false, 0);
208 hbox.pack_start(vbox, false, false, 0);
209 content_area.pack_start(hbox, false, false, 6);
212 private static string update_preamble(string preamble)
215 return _("Missing required field: ");
216 return _("Missing required fields: ");
219 private static string update_message(string old_message, string new_item)
222 if (old_message == "")
225 message = old_message + ", " + new_item;
229 private static void check_field(string field, Label label, string fieldname, ref string preamble, ref string message)
232 label.set_markup(@"$fieldname:");
235 label.set_markup(@"<span foreground=\"red\">$fieldname:</span>");
236 preamble = update_preamble(preamble);
237 message = update_message(message, fieldname);
240 private bool check_fields()
242 string preamble = "";
244 string password_test = store_password ? password : "not required";
245 if (!card.is_no_identity())
247 check_field(display_name, displayname_label, displayname_labeltext, ref preamble, ref message);
248 check_field(username, username_label, username_labeltext, ref preamble, ref message);
249 check_field(issuer, realm_label, realm_labeltext, ref preamble, ref message);
250 check_field(password_test, password_label, password_labeltext, ref preamble, ref message);
253 message_label.set_visible(true);
254 message_label.set_markup(@"<span foreground=\"red\">$preamble$message</span>");
260 private void on_response(Dialog source, int response_id)
262 switch (response_id) {
263 case ResponseType.OK:
264 complete = check_fields();
266 case ResponseType.CANCEL:
272 private static void label_make_bold(Label label)
274 var font_desc = new Pango.FontDescription();
276 font_desc.set_weight(Pango.Weight.BOLD);
278 /* This will only affect the weight of the font. The rest is
279 * from the current state of the widget, which comes from the
280 * theme or user prefs, since the font desc only has the
281 * weight flag turned on.
283 label.modify_font(font_desc);
286 private VBox make_services_vbox()
288 logger.trace("make_services_vbox");
290 var services_vbox_alignment = new Alignment(0, 0, 1, 0);
291 var services_vscroll = new ScrolledWindow(null, null);
292 services_vscroll.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
293 services_vscroll.set_shadow_type(ShadowType.IN);
294 services_vscroll.set_size_request(0, 60);
295 services_vscroll.add_with_viewport(services_vbox_alignment);
298 var remove_button = new Button.from_stock(Stock.REMOVE);
300 var remove_button = new Button.from_stock(STOCK_REMOVE);
302 remove_button.set_sensitive(false);
305 var services_table = new Table(card.services.size, 1, false);
306 services_table.set_row_spacings(1);
307 services_table.set_col_spacings(0);
308 services_table.modify_bg(StateType.NORMAL, white);
310 var table_button_hbox = new HBox(false, 6);
311 table_button_hbox.pack_start(services_vscroll, true, true, 6);
313 // Hack to prevent the button from growing vertically
314 VBox fixed_height = new VBox(false, 0);
315 fixed_height.pack_start(remove_button, false, false, 0);
316 table_button_hbox.pack_start(fixed_height, false, false, 6);
318 // A table doesn't have a background color, so put it in an EventBox, and
319 // set the EventBox's background color instead.
320 EventBox table_bg = new EventBox();
321 table_bg.modify_bg(StateType.NORMAL, white);
322 table_bg.add(services_table);
323 services_vbox_alignment.add(table_bg);
325 var services_vbox_title = new Label(_("Services:"));
326 label_make_bold(services_vbox_title);
327 services_vbox_title.set_alignment(0, (float) 0.5);
329 var services_vbox = new VBox(false, 6);
330 services_vbox.pack_start(services_vbox_title, false, false, 6);
331 services_vbox.pack_start(table_button_hbox, true, true, 6);
334 foreach (string service in card.services)
336 var label = new Label(service);
337 label.set_alignment((float) 0, (float) 0);
340 EventBox event_box = new EventBox();
341 event_box.modify_bg(StateType.NORMAL, white);
342 event_box.add(label);
343 event_box.button_press_event.connect(() =>
345 var state = label.get_state();
346 logger.trace("button_press_callback: Label state=" + state.to_string() + " setting bg to " + white.to_string());
348 if (selected_item == label)
351 selected_item.parent.modify_bg(state, white);
352 selected_item = null;
353 remove_button.set_sensitive(false);
357 if (selected_item != null)
360 selected_item.parent.modify_bg(state, white);
361 selected_item = null;
365 selected_item = label;
366 selected_item.parent.modify_bg(state, selected_color);
367 remove_button.set_sensitive(true);
372 services_table.attach_defaults(event_box, 0, 1, i, i+1);
376 remove_button.clicked.connect((remove_button) =>
378 var result = WarningDialog.confirm(this,
379 Markup.printf_escaped(
380 "<span font-weight='heavy'>You are about to remove the service '%s'.</span>",
382 + "\n\nAre you sure you want to do this?",
388 card.services.remove(selected_item.label);
389 services_table.remove(selected_item.parent);
390 selected_item = null;
391 remove_button.set_sensitive(false);
397 return services_vbox;