Checkpointing non-working services edit code before I refactor it
[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 MoonshotLogger logger = get_logger("IdentityDialog");
46
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");
51
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;
63     public bool complete;
64     
65     public string display_name {
66         get { return displayname_entry.get_text(); }
67     }
68
69     public string issuer {
70         get { return issuer_entry.get_text(); }
71     }
72
73     public string username {
74         get { return username_entry.get_text(); }
75     }
76
77     public string password {
78         get { return password_entry.get_text(); }
79     }
80
81     public bool store_password {
82         get { return remember_checkbutton.active; }
83     }
84
85     public IdentityDialog(IdentityManagerView parent)
86     {
87         this.with_idcard(null, _("Add ID Card"), parent);
88     }
89
90     public IdentityDialog.with_idcard(IdCard? a_card, string title, IdentityManagerView parent)
91     {
92         bool is_new_card = false;
93         if (a_card == null)
94         {
95             is_new_card = true;
96         }
97
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;
103
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);
107         
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);
112
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);
117
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);
122
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);
129
130         this.remember_checkbutton = new CheckButton.with_label(_("Remember password"));
131         this.message_label = new Label("");
132         message_label.set_visible(false);
133
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);
138
139         var table = new Table(6, 2, false);
140         table.set_col_spacings(10);
141         table.set_row_spacings(10);
142         
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);
153
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);
158
159
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);
163         
164         var services_internal_vbox = new VBox(true, 6);
165
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);
173
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);
177
178
179         ((Container) content_area).add(vbox);
180
181         this.set_border_width(6);
182         this.set_resizable(false);
183         this.show_all();
184     }
185
186     private static string update_preamble(string preamble)
187     {
188         if (preamble == "")
189             return _("Missing required field: ");
190         return _("Missing required fields: ");
191     }
192
193     private static string update_message(string old_message, string new_item)
194     {
195         string message;
196         if (old_message == "")
197             message = new_item;
198         else
199             message = old_message + ", " + new_item;
200         return message;
201     }
202
203     private static void check_field(string field, Label label, string fieldname, ref string preamble, ref string message)
204     {
205         if (field != "") {
206             label.set_markup(@"$fieldname:");
207             return;
208         }
209         label.set_markup(@"<span foreground=\"red\">$fieldname:</span>");
210         preamble = update_preamble(preamble);
211         message = update_message(message, fieldname);
212     }
213
214     private bool check_fields()
215     {
216         string preamble = "";
217         string message = "";
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);
223         if (message != "") {
224             message_label.set_visible(true);
225             message_label.set_markup(@"<span foreground=\"red\">$preamble$message</span>");
226             return false;
227         }
228         return true;
229     }
230
231     private void on_response(Dialog source, int response_id)
232     {
233         switch (response_id) {
234         case ResponseType.OK:
235             complete = check_fields();
236             break;
237         case ResponseType.CANCEL:
238             complete = true;
239             break;
240         }
241     }
242
243     private void set_atk_relation(Widget widget, Widget target_widget, Atk.RelationType relationship)
244     {
245         var atk_widget = widget.get_accessible();
246         var atk_target_widget = target_widget.get_accessible();
247
248         atk_widget.add_relationship(relationship, atk_target_widget);
249     }
250
251     private static void label_make_bold(Label label)
252     {
253         var font_desc = new Pango.FontDescription();
254
255         font_desc.set_weight(Pango.Weight.BOLD);
256
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.
261          */
262         label.modify_font(font_desc);
263     }
264
265     private void fill_services_vbox(IdCard id_card, VBox services_internal_vbox)
266     {
267         logger.trace("fill_services_vbox");
268
269         // var children = services_internal_vbox.get_children();
270         // foreach (var widget in children) {
271         //     services_internal_vbox.remove(widget);
272         // }
273
274         int i = 0;
275         var n_rows = id_card.services.length;
276
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);
281         
282         foreach (string service in id_card.services)
283         {
284             var label = new Label(service);
285             label.set_alignment(0, (float) 0.5);
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
292             remove_button.clicked.connect((remove_button) =>
293                 {
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,
300                                                        service);
301                     var ret = dialog.run();
302                     dialog.hide();
303               
304                     if (ret == Gtk.ResponseType.YES)
305                     {
306                         if (id_card != null) {
307                             SList<string> services = new SList<string>();
308                 
309                             foreach (string srv in id_card.services)
310                             {
311                                 //if (srv == candidate) //!!TODO: If srv is the service to be removed
312                                 //    continue;
313                                 services.append(srv);
314                             }
315                 
316                             id_card.services = new string[services.length()];
317                             for (int j = 0; j < id_card.services.length; j++)
318                             {
319                                 id_card.services[j] = services.nth_data(j);
320                             }
321                 
322                             parent.identities_manager.update_card(id_card);
323                         }
324                     }
325               
326                 });
327             services_table.attach_defaults(label, 0, 1, i, i+1);
328             services_table.attach_defaults(remove_button, 1, 2, i, i+1);
329             i++;
330         }
331
332         // services_vbox.show_all();
333     }
334
335
336 }