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