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;
65 public string display_name {
66 get { return displayname_entry.get_text(); }
69 public string issuer {
70 get { return issuer_entry.get_text(); }
73 public string username {
74 get { return username_entry.get_text(); }
77 public string password {
78 get { return password_entry.get_text(); }
81 public bool store_password {
82 get { return remember_checkbutton.active; }
85 public IdentityDialog(IdentityManagerView parent)
87 this.with_idcard(null, _("Add ID Card"), parent);
90 public IdentityDialog.with_idcard(IdCard? a_card, string title, IdentityManagerView parent)
92 bool is_new_card = false;
98 IdCard card = a_card ?? new IdCard();
99 this.set_title(title);
100 this.set_modal(true);
101 this.set_transient_for(parent);
102 this.parent = parent;
104 this.add_buttons(_("OK"), ResponseType.OK, CANCEL, ResponseType.CANCEL);
105 var content_area = this.get_content_area();
106 ((Box) content_area).set_spacing(12);
108 displayname_label = new Label(@"$displayname_labeltext:");
109 displayname_label.set_alignment(1,(float) 0.5);
110 displayname_entry = new Entry();
111 displayname_entry.set_text(card.display_name);
113 issuer_label = new Label(@"$issuer_labeltext:");
114 issuer_label.set_alignment(1,(float) 0.5);
115 this.issuer_entry = new Entry();
116 this.issuer_entry.set_text(card.issuer);
118 username_label = new Label(@"$username_labeltext:");
119 username_label.set_alignment(1,(float) 0.5);
120 this.username_entry = new Entry();
121 this.username_entry.set_text(card.username);
123 password_label = new Label(@"$password_labeltext:");
124 password_label.set_alignment(1,(float) 0.5);
125 this.password_entry = new Entry();
126 password_entry.set_invisible_char('*');
127 password_entry.set_visibility(false);
128 password_entry.set_text(card.password);
130 this.remember_checkbutton = new CheckButton.with_label(_("Remember password"));
131 this.message_label = new Label("");
132 message_label.set_visible(false);
134 set_atk_relation(displayname_label, displayname_entry, Atk.RelationType.LABEL_FOR);
135 set_atk_relation(issuer_label, issuer_entry, Atk.RelationType.LABEL_FOR);
136 set_atk_relation(username_label, username_entry, Atk.RelationType.LABEL_FOR);
137 set_atk_relation(password_entry, password_entry, Atk.RelationType.LABEL_FOR);
139 var table = new Table(6, 2, false);
140 table.set_col_spacings(10);
141 table.set_row_spacings(10);
143 table.attach_defaults(message_label, 0, 2, 0, 1);
144 table.attach_defaults(displayname_label, 0, 1, 1, 2);
145 table.attach_defaults(displayname_entry, 1, 2, 1, 2);
146 table.attach_defaults(issuer_label, 0, 1, 2, 3);
147 table.attach_defaults(issuer_entry, 1, 2, 2, 3);
148 table.attach_defaults(username_label, 0, 1, 3, 4);
149 table.attach_defaults(username_entry, 1, 2, 3, 4);
150 table.attach_defaults(password_label, 0, 1, 4, 5);
151 table.attach_defaults(password_entry, 1, 2, 4, 5);
152 table.attach_defaults(remember_checkbutton, 1, 2, 5, 6);
154 this.response.connect(on_response);
155 var vbox = new VBox(false, 0);
156 vbox.set_border_width(6);
157 vbox.pack_start(table, false, false, 0);
160 var services_vbox_title = new Label(_("Services:"));
161 label_make_bold(services_vbox_title);
162 services_vbox_title.set_alignment(0, (float) 0.5);
164 var services_internal_vbox = new VBox(true, 6);
166 var services_vbox_alignment = new Alignment(0, 0, 0, 1);
167 services_vbox_alignment.set_padding(6, 6, 6, 6);
168 services_vbox_alignment.add(services_internal_vbox);
169 var services_vscroll = new ScrolledWindow(null, null);
170 services_vscroll.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
171 services_vscroll.set_shadow_type(ShadowType.IN);
172 services_vscroll.add_with_viewport(services_vbox_alignment);
174 var services_vbox = new VBox(false, 6);
175 services_vbox.pack_start(services_vbox_title, false, false, 0);
176 services_vbox.pack_start(services_vscroll, true, true, 0);
179 ((Container) content_area).add(vbox);
181 this.set_border_width(6);
182 this.set_resizable(false);
186 private static string update_preamble(string preamble)
189 return _("Missing required field: ");
190 return _("Missing required fields: ");
193 private static string update_message(string old_message, string new_item)
196 if (old_message == "")
199 message = old_message + ", " + new_item;
203 private static void check_field(string field, Label label, string fieldname, ref string preamble, ref string message)
206 label.set_markup(@"$fieldname:");
209 label.set_markup(@"<span foreground=\"red\">$fieldname:</span>");
210 preamble = update_preamble(preamble);
211 message = update_message(message, fieldname);
214 private bool check_fields()
216 string preamble = "";
218 string password_test = store_password ? password : "not required";
219 check_field(display_name, displayname_label, displayname_labeltext, ref preamble, ref message);
220 check_field(issuer, issuer_label, issuer_labeltext, ref preamble, ref message);
221 check_field(username, username_label, username_labeltext, ref preamble, ref message);
222 check_field(password_test, password_label, password_labeltext, ref preamble, ref message);
224 message_label.set_visible(true);
225 message_label.set_markup(@"<span foreground=\"red\">$preamble$message</span>");
231 private void on_response(Dialog source, int response_id)
233 switch (response_id) {
234 case ResponseType.OK:
235 complete = check_fields();
237 case ResponseType.CANCEL:
243 private void set_atk_relation(Widget widget, Widget target_widget, Atk.RelationType relationship)
245 var atk_widget = widget.get_accessible();
246 var atk_target_widget = target_widget.get_accessible();
248 atk_widget.add_relationship(relationship, atk_target_widget);
251 private static void label_make_bold(Label label)
253 var font_desc = new Pango.FontDescription();
255 font_desc.set_weight(Pango.Weight.BOLD);
257 /* This will only affect the weight of the font. The rest is
258 * from the current state of the widget, which comes from the
259 * theme or user prefs, since the font desc only has the
260 * weight flag turned on.
262 label.modify_font(font_desc);
265 private void fill_services_vbox(IdCard id_card, VBox services_internal_vbox)
267 logger.trace("fill_services_vbox");
269 // var children = services_internal_vbox.get_children();
270 // foreach (var widget in children) {
271 // services_internal_vbox.remove(widget);
275 var n_rows = id_card.services.length;
277 var services_table = new Table(n_rows, 2, false);
278 services_table.set_col_spacings(10);
279 services_table.set_row_spacings(10);
280 services_internal_vbox.pack_start(services_table, true, false, 0);
282 foreach (string service in id_card.services)
284 var label = new Label(service);
285 label.set_alignment(0, (float) 0.5);
287 var remove_button = new Button.from_stock(Stock.REMOVE);
289 var remove_button = new Button.from_stock(STOCK_REMOVE);
292 remove_button.clicked.connect((remove_button) =>
294 var dialog = new Gtk.MessageDialog(this,
295 Gtk.DialogFlags.DESTROY_WITH_PARENT,
296 Gtk.MessageType.QUESTION,
297 Gtk.ButtonsType.YES_NO,
298 _("Are you sure you want to stop '%s' ID Card from being used with %s?"),
299 id_card.display_name,
301 var ret = dialog.run();
304 if (ret == Gtk.ResponseType.YES)
306 if (id_card != null) {
307 SList<string> services = new SList<string>();
309 foreach (string srv in id_card.services)
311 //if (srv == candidate) //!!TODO: If srv is the service to be removed
313 services.append(srv);
316 id_card.services = new string[services.length()];
317 for (int j = 0; j < id_card.services.length; j++)
319 id_card.services[j] = services.nth_data(j);
322 parent.identities_manager.update_card(id_card);
327 services_table.attach_defaults(label, 0, 1, i, i+1);
328 services_table.attach_defaults(remove_button, 1, 2, i, i+1);
332 // services_vbox.show_all();