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