Disallow editing the text fields of the No Identity card
[moonshot-ui.git] / src / moonshot-identity-dialog.vala
1 /*
2  * Copyright (c) 2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31 */
32 using Gtk;
33
34
35 // Defined here as workaround for emacs vala-mode indentation failure.
36 #if VALA_0_12
37 static const string CANCEL = Stock.CANCEL;
38 #else
39 static const string CANCEL = STOCK_CANCEL;
40 #endif
41
42
43 class IdentityDialog : Dialog
44 {
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
48     private static MoonshotLogger logger = get_logger("IdentityDialog");
49
50     static const string displayname_labeltext = _("Display Name");
51     static const string realm_labeltext = _("Realm");
52     static const string username_labeltext = _("Username");
53     static const string password_labeltext = _("Password");
54
55     private Entry displayname_entry;
56     private Label displayname_label;
57     private Entry realm_entry;
58     private Label realm_label;
59     private Entry username_entry;
60     private Label username_label;
61     private Entry password_entry;
62     private Label password_label;
63     private CheckButton remember_checkbutton;
64     private Label message_label;
65     public bool complete;
66     private IdCard card;
67
68     private Label selected_item = null;
69
70     public string display_name {
71         get { return displayname_entry.get_text(); }
72     }
73
74     public string issuer {
75         get { return realm_entry.get_text(); }
76     }
77
78     public string username {
79         get { return username_entry.get_text(); }
80     }
81
82     public string password {
83         get { return password_entry.get_text(); }
84     }
85
86     public bool store_password {
87         get { return remember_checkbutton.active; }
88     }
89
90     internal string[] get_services()
91     {
92         return card.services;
93     }
94
95     public IdentityDialog(IdentityManagerView parent)
96     {
97         this.with_idcard(null, _("Add ID Card"), parent);
98     }
99
100     public IdentityDialog.with_idcard(IdCard? a_card, string title, IdentityManagerView parent)
101     {
102         bool is_new_card = false;
103         if (a_card == null)
104         {
105             is_new_card = true;
106         }
107
108         card = a_card ?? new IdCard();
109         this.set_title(title);
110         this.set_modal(true);
111         this.set_transient_for(parent);
112
113         this.add_buttons(_("OK"), ResponseType.OK, CANCEL, ResponseType.CANCEL);
114         Box content_area = (Box) this.get_content_area();
115
116         displayname_label = new Label(@"$displayname_labeltext:");
117         displayname_label.set_alignment(0, (float) 0.5);
118         displayname_entry = new Entry();
119         displayname_entry.set_text(card.display_name);
120         displayname_entry.set_width_chars(40);
121
122         realm_label = new Label(@"$realm_labeltext:");
123         realm_label.set_alignment(0, (float) 0.5);
124         realm_entry = new Entry();
125         realm_entry.set_text(card.issuer);
126         realm_entry.set_width_chars(60);
127
128         username_label = new Label(@"$username_labeltext:");
129         username_label.set_alignment(0, (float) 0.5);
130         username_entry = new Entry();
131         username_entry.set_text(card.username);
132         username_entry.set_width_chars(40);
133
134         password_label = new Label(@"$password_labeltext:");
135         password_label.set_alignment(0, (float) 0.5);
136
137         remember_checkbutton = new CheckButton.with_label(_("Remember password"));
138         remember_checkbutton.active = card.store_password;
139
140         password_entry = new Entry();
141         password_entry.set_invisible_char('*');
142         password_entry.set_visibility(false);
143         password_entry.set_width_chars(40);
144         password_entry.set_text(card.password);
145
146         message_label = new Label("");
147         message_label.set_visible(false);
148
149         set_atk_relation(displayname_label, displayname_entry, Atk.RelationType.LABEL_FOR);
150         set_atk_relation(realm_label, realm_entry, Atk.RelationType.LABEL_FOR);
151         set_atk_relation(username_label, username_entry, Atk.RelationType.LABEL_FOR);
152         set_atk_relation(password_label, password_entry, Atk.RelationType.LABEL_FOR);
153
154         content_area.pack_start(message_label, false, false, 6);
155         add_as_vbox(content_area, displayname_label, displayname_entry);
156         add_as_vbox(content_area, username_label, username_entry);
157         add_as_vbox(content_area, realm_label, realm_entry);
158         add_as_vbox(content_area, password_label, password_entry);
159
160         // var entries = new VBox(false, 6);
161         // add_as_vbox(entries, displayname_label, displayname_entry);
162         // add_as_vbox(entries, realm_label, realm_entry);
163         // add_as_vbox(entries, username_label, username_entry);
164         // add_as_vbox(entries, password_label, password_entry);
165         // content_area.pack_start(entries, false, false, 0);
166
167         var remember_hbox = new HBox(false, 40);
168         remember_hbox.pack_start(new HBox(false, 0), false, false, 0);
169         remember_hbox.pack_start(remember_checkbutton, false, false, 0);
170         content_area.pack_start(remember_hbox, false, false, 2);
171         // content_area.pack_start(remember_checkbutton, false, false, 2);
172
173         this.response.connect(on_response);
174         content_area.set_border_width(6);
175
176         if (!is_new_card)
177         {
178             var services_vbox = make_services_vbox();
179             content_area.pack_start(services_vbox);
180         }
181
182         if (card.is_no_identity())
183         {
184             displayname_entry.set_sensitive(false);
185             realm_entry.set_sensitive(false);
186             username_entry.set_sensitive(false);
187             password_entry.set_sensitive(false);
188             remember_checkbutton.set_sensitive(false);
189         }
190
191         this.set_border_width(6);
192         this.set_resizable(false);
193         this.modify_bg(StateType.NORMAL, white);
194         this.show_all();
195     }
196
197     private static void add_as_vbox(Box content_area, Label label, Entry entry)
198     {
199         VBox vbox = new VBox(false, 2);
200
201         vbox.pack_start(label, false, false, 0);
202         vbox.pack_start(entry, false, false, 0);
203
204         // Hack to prevent the text entries from stretching horizontally
205         HBox hbox = new HBox(false, 0);
206         hbox.pack_start(vbox, false, false, 0);
207         content_area.pack_start(hbox, false, false, 6);
208     }
209
210     private static string update_preamble(string preamble)
211     {
212         if (preamble == "")
213             return _("Missing required field: ");
214         return _("Missing required fields: ");
215     }
216
217     private static string update_message(string old_message, string new_item)
218     {
219         string message;
220         if (old_message == "")
221             message = new_item;
222         else
223             message = old_message + ", " + new_item;
224         return message;
225     }
226
227     private static void check_field(string field, Label label, string fieldname, ref string preamble, ref string message)
228     {
229         if (field != "") {
230             label.set_markup(@"$fieldname:");
231             return;
232         }
233         label.set_markup(@"<span foreground=\"red\">$fieldname:</span>");
234         preamble = update_preamble(preamble);
235         message = update_message(message, fieldname);
236     }
237
238     private bool check_fields()
239     {
240         string preamble = "";
241         string message = "";
242         string password_test = store_password ? password : "not required";
243         if (!card.is_no_identity())
244         {
245             check_field(display_name, displayname_label, displayname_labeltext, ref preamble, ref message);
246             check_field(username, username_label, username_labeltext, ref preamble, ref message);
247             check_field(issuer, realm_label, realm_labeltext, ref preamble, ref message);
248             check_field(password_test, password_label, password_labeltext, ref preamble, ref message);
249         }
250         if (message != "") {
251             message_label.set_visible(true);
252             message_label.set_markup(@"<span foreground=\"red\">$preamble$message</span>");
253             return false;
254         }
255         return true;
256     }
257
258     private void on_response(Dialog source, int response_id)
259     {
260         switch (response_id) {
261         case ResponseType.OK:
262             complete = check_fields();
263             break;
264         case ResponseType.CANCEL:
265             complete = true;
266             break;
267         }
268     }
269
270     private static void label_make_bold(Label label)
271     {
272         var font_desc = new Pango.FontDescription();
273
274         font_desc.set_weight(Pango.Weight.BOLD);
275
276         /* This will only affect the weight of the font. The rest is
277          * from the current state of the widget, which comes from the
278          * theme or user prefs, since the font desc only has the
279          * weight flag turned on.
280          */
281         label.modify_font(font_desc);
282     }
283
284     private VBox make_services_vbox()
285     {
286         logger.trace("make_services_vbox");
287
288         var services_vbox_alignment = new Alignment(0, 0, 0, 1);
289         var services_vscroll = new ScrolledWindow(null, null);
290         services_vscroll.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
291         services_vscroll.set_shadow_type(ShadowType.IN);
292         services_vscroll.set_size_request(0, 60);
293         services_vscroll.add_with_viewport(services_vbox_alignment);
294
295 #if VALA_0_12
296         var remove_button = new Button.from_stock(Stock.REMOVE);
297 #else
298         var remove_button = new Button.from_stock(STOCK_REMOVE);
299 #endif
300         remove_button.set_sensitive(false);
301
302
303         var services_table = new Table(card.services.length, 1, false);
304         services_table.set_row_spacings(1);
305         services_table.set_col_spacings(0);
306         services_table.modify_bg(StateType.NORMAL, white);
307
308         var table_button_hbox = new HBox(false, 6);
309         table_button_hbox.pack_start(services_vscroll, true, true, 6);
310
311         // Hack to prevent the button from growing vertically
312         VBox fixed_height = new VBox(false, 0);
313         fixed_height.pack_start(remove_button, false, false, 0);
314         table_button_hbox.pack_start(fixed_height, false, false, 6);
315
316         // A table doesn't have a background color, so put it in an EventBox, and
317         // set the EventBox's background color instead.
318         EventBox table_bg = new EventBox();
319         table_bg.modify_bg(StateType.NORMAL, white);
320         table_bg.add(services_table);
321         services_vbox_alignment.add(table_bg);
322
323         var services_vbox_title = new Label(_("Services:"));
324         label_make_bold(services_vbox_title);
325         services_vbox_title.set_alignment(0, (float) 0.5);
326
327         var services_vbox = new VBox(false, 6);
328         services_vbox.pack_start(services_vbox_title, false, false, 6);
329         services_vbox.pack_start(table_button_hbox, true, true, 6);
330
331         int i = 0;
332         foreach (string service in card.services)
333         {
334             var label = new Label(service);
335             label.set_alignment((float) 0, (float) 0);
336
337             EventBox event_box = new EventBox();
338             event_box.modify_bg(StateType.NORMAL, white);
339             event_box.add(label);
340             event_box.button_press_event.connect(() =>
341                 {
342                     var state = label.get_state();
343                     logger.trace("button_press_callback: Label state=" + state.to_string() + " setting bg to " + white.to_string());
344
345                     if (selected_item == label)
346                     {
347                         // Deselect
348                         selected_item.parent.modify_bg(state, white);
349                         selected_item = null;
350                         remove_button.set_sensitive(false);
351                     }
352                     else
353                     {
354                         if (selected_item != null)
355                         {
356                             // Deselect
357                             selected_item.parent.modify_bg(state, white);
358                             selected_item = null;
359                         }
360
361                         // Select
362                         selected_item = label;
363                         selected_item.parent.modify_bg(state, selected_color);
364                         remove_button.set_sensitive(true);
365                     }
366                     return false;
367                 });
368
369             AttachOptions opts = AttachOptions.EXPAND | AttachOptions.FILL;
370             services_table.attach(event_box, 0, 1, i, i+1, opts, opts, 3, 0);
371             i++;
372         }
373
374         remove_button.clicked.connect((remove_button) =>
375             {
376                 var result = WarningDialog.confirm(this,
377                                                    Markup.printf_escaped(
378                                                        "<span font-weight='heavy'>You are about to remove the service '%s'.</span>",
379                                                        selected_item.label)
380                                                    + "\n\nAre you sure you want to do this?",
381                                                    "delete_service");
382
383                 if (result)
384                 {
385                     if (card != null) {
386                         SList<string> services = new SList<string>();
387
388                         foreach (string srv in card.services)
389                         {
390                             if (srv != selected_item.label)
391                                 services.append(srv);
392                         }
393
394                         card.services = new string[services.length()];
395                         for (int j = 0; j < card.services.length; j++)
396                         {
397                             card.services[j] = services.nth_data(j);
398                         }
399
400                         services_table.remove(selected_item.parent);
401                         selected_item = null;
402                         remove_button.set_sensitive(false);
403                     }
404                 }
405
406             });
407
408         return services_vbox;
409     }
410
411
412 }