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 MoonshotLogger logger = get_logger("IdentityDialog");
47 static const string displayname_labeltext = _("Display Name");
48 static const string issuer_labeltext = _("Issuer");
49 static const string username_labeltext = _("Username");
50 static const string password_labeltext = _("Password");
52 private IdentityManagerView parent;
53 private Entry displayname_entry;
54 private Label displayname_label;
55 private Entry issuer_entry;
56 private Label issuer_label;
57 private Entry username_entry;
58 private Label username_label;
59 private Entry password_entry;
60 private Label password_label;
61 private CheckButton remember_checkbutton;
62 private Label message_label;
66 private Label selected_item = null;
68 public string display_name {
69 get { return displayname_entry.get_text(); }
72 public string issuer {
73 get { return issuer_entry.get_text(); }
76 public string username {
77 get { return username_entry.get_text(); }
80 public string password {
81 get { return password_entry.get_text(); }
84 public bool store_password {
85 get { return remember_checkbutton.active; }
88 internal string[] get_services()
93 public IdentityDialog(IdentityManagerView parent)
95 this.with_idcard(null, _("Add ID Card"), parent);
98 public IdentityDialog.with_idcard(IdCard? a_card, string title, IdentityManagerView parent)
100 bool is_new_card = false;
106 card = a_card ?? new IdCard();
107 this.set_title(title);
108 this.set_modal(true);
109 this.set_transient_for(parent);
110 this.parent = parent;
112 this.add_buttons(_("OK"), ResponseType.OK, CANCEL, ResponseType.CANCEL);
113 var content_area = this.get_content_area();
114 ((Box) content_area).set_spacing(12);
116 displayname_label = new Label(@"$displayname_labeltext:");
117 displayname_label.set_alignment(1,(float) 0.5);
118 displayname_entry = new Entry();
119 displayname_entry.set_text(card.display_name);
121 issuer_label = new Label(@"$issuer_labeltext:");
122 issuer_label.set_alignment(1,(float) 0.5);
123 this.issuer_entry = new Entry();
124 this.issuer_entry.set_text(card.issuer);
126 username_label = new Label(@"$username_labeltext:");
127 username_label.set_alignment(1,(float) 0.5);
128 this.username_entry = new Entry();
129 this.username_entry.set_text(card.username);
131 password_label = new Label(@"$password_labeltext:");
132 password_label.set_alignment(1,(float) 0.5);
133 this.password_entry = new Entry();
134 password_entry.set_invisible_char('*');
135 password_entry.set_visibility(false);
136 password_entry.set_text(card.password);
138 this.remember_checkbutton = new CheckButton.with_label(_("Remember password"));
139 this.message_label = new Label("");
140 message_label.set_visible(false);
142 set_atk_relation(displayname_label, displayname_entry, Atk.RelationType.LABEL_FOR);
143 set_atk_relation(issuer_label, issuer_entry, Atk.RelationType.LABEL_FOR);
144 set_atk_relation(username_label, username_entry, Atk.RelationType.LABEL_FOR);
145 set_atk_relation(password_entry, password_entry, Atk.RelationType.LABEL_FOR);
147 var table = new Table(6, 2, false);
148 table.set_col_spacings(10);
149 table.set_row_spacings(10);
151 table.attach_defaults(message_label, 0, 2, 0, 1);
152 table.attach_defaults(displayname_label, 0, 1, 1, 2);
153 table.attach_defaults(displayname_entry, 1, 2, 1, 2);
154 table.attach_defaults(issuer_label, 0, 1, 2, 3);
155 table.attach_defaults(issuer_entry, 1, 2, 2, 3);
156 table.attach_defaults(username_label, 0, 1, 3, 4);
157 table.attach_defaults(username_entry, 1, 2, 3, 4);
158 table.attach_defaults(password_label, 0, 1, 4, 5);
159 table.attach_defaults(password_entry, 1, 2, 4, 5);
160 table.attach_defaults(remember_checkbutton, 1, 2, 5, 6);
162 this.response.connect(on_response);
163 var vbox = new VBox(false, 0);
164 vbox.set_border_width(6);
165 vbox.pack_start(table, false, false, 0);
169 var services_vbox = make_services_vbox();
170 vbox.pack_start(services_vbox);
173 ((Container) content_area).add(vbox);
175 this.set_border_width(6);
176 this.set_resizable(false);
180 private static string update_preamble(string preamble)
183 return _("Missing required field: ");
184 return _("Missing required fields: ");
187 private static string update_message(string old_message, string new_item)
190 if (old_message == "")
193 message = old_message + ", " + new_item;
197 private static void check_field(string field, Label label, string fieldname, ref string preamble, ref string message)
200 label.set_markup(@"$fieldname:");
203 label.set_markup(@"<span foreground=\"red\">$fieldname:</span>");
204 preamble = update_preamble(preamble);
205 message = update_message(message, fieldname);
208 private bool check_fields()
210 string preamble = "";
212 string password_test = store_password ? password : "not required";
213 check_field(display_name, displayname_label, displayname_labeltext, ref preamble, ref message);
214 check_field(issuer, issuer_label, issuer_labeltext, ref preamble, ref message);
215 check_field(username, username_label, username_labeltext, ref preamble, ref message);
216 check_field(password_test, password_label, password_labeltext, ref preamble, ref message);
218 message_label.set_visible(true);
219 message_label.set_markup(@"<span foreground=\"red\">$preamble$message</span>");
225 private void on_response(Dialog source, int response_id)
227 switch (response_id) {
228 case ResponseType.OK:
229 complete = check_fields();
231 case ResponseType.CANCEL:
237 private void set_atk_relation(Widget widget, Widget target_widget, Atk.RelationType relationship)
239 var atk_widget = widget.get_accessible();
240 var atk_target_widget = target_widget.get_accessible();
242 atk_widget.add_relationship(relationship, atk_target_widget);
245 private static void label_make_bold(Label label)
247 var font_desc = new Pango.FontDescription();
249 font_desc.set_weight(Pango.Weight.BOLD);
251 /* This will only affect the weight of the font. The rest is
252 * from the current state of the widget, which comes from the
253 * theme or user prefs, since the font desc only has the
254 * weight flag turned on.
256 label.modify_font(font_desc);
259 private VBox make_services_vbox()
261 logger.trace("make_services_vbox");
263 var services_vbox_alignment = new Alignment(0, 0, 0, 1);
264 services_vbox_alignment.set_padding(6, 6, 6, 6);
265 var services_vscroll = new ScrolledWindow(null, null);
266 services_vscroll.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
267 services_vscroll.set_shadow_type(ShadowType.IN);
268 services_vscroll.set_size_request(0, 60);
269 services_vscroll.add_with_viewport(services_vbox_alignment);
272 var remove_button = new Button.from_stock(Stock.REMOVE);
274 var remove_button = new Button.from_stock(STOCK_REMOVE);
276 remove_button.set_sensitive(false);
279 var services_table = new Table(card.services.length, 1, false);
280 services_table.set_row_spacings(5);
282 var table_button_hbox = new HBox(false, 6);
283 table_button_hbox.pack_start(services_vscroll, true, true, 6);
284 table_button_hbox.pack_start(remove_button, false, false, 6);
285 services_vbox_alignment.add(services_table);
287 var services_vbox_title = new Label(_("Services:"));
288 label_make_bold(services_vbox_title);
289 services_vbox_title.set_alignment(0, (float) 0.5);
291 var services_vbox = new VBox(false, 6);
292 services_vbox.pack_start(services_vbox_title, false, false, 6);
293 services_vbox.pack_start(table_button_hbox, true, true, 6);
296 var selected_color = Gdk.Color();
297 selected_color.red = 0xd9 << 8;
298 selected_color.green = 0xf7 << 8;
299 selected_color.blue = 65535;
301 var unselected_color = Gdk.Color();
302 unselected_color.red = 65535;
303 unselected_color.green = 65535;
304 unselected_color.blue = 65535;
307 foreach (string service in card.services)
309 var label = new Label(service);
310 label.set_alignment(0, (float) 0);
312 EventBox event_box = new EventBox();
313 event_box.add(label);
314 event_box.button_press_event.connect(() =>
316 var state = label.get_state();
317 if (selected_item == label)
320 selected_item.modify_bg(state, unselected_color);
321 selected_item = null;
322 remove_button.set_sensitive(false);
326 if (selected_item != null)
329 selected_item.modify_bg(state, unselected_color);
330 selected_item = null;
334 selected_item = label;
335 selected_item.modify_bg(state, selected_color);
336 remove_button.set_sensitive(true);
341 services_table.attach_defaults(event_box, 0, 1, i, i+1);
345 remove_button.clicked.connect((remove_button) =>
347 var dialog = new Gtk.MessageDialog(this,
348 Gtk.DialogFlags.DESTROY_WITH_PARENT,
349 Gtk.MessageType.QUESTION,
350 Gtk.ButtonsType.YES_NO,
351 _("You are about to remove the service '%s'. Are you sure you want to do this?"),
352 selected_item.label);
353 var ret = dialog.run();
356 if (ret == Gtk.ResponseType.YES)
359 SList<string> services = new SList<string>();
361 foreach (string srv in card.services)
363 if (srv != selected_item.label)
364 services.append(srv);
367 card.services = new string[services.length()];
368 for (int j = 0; j < card.services.length; j++)
370 card.services[j] = services.nth_data(j);
373 services_table.remove(selected_item.parent);
374 selected_item = null;
375 remove_button.set_sensitive(false);
381 return services_vbox;