Minor formatting changes
[moonshot-ui.git] / src / moonshot-add-dialog.vala
1 /*
2  * Copyright (c) 2011-2014, 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 AddIdentityDialog : Dialog
44 {
45     static const string displayname_labeltext = _("Display Name");
46     static const string issuer_labeltext = _("Issuer");
47     static const string username_labeltext = _("Username");
48     static const string password_labeltext = _("Password");
49     private Entry displayname_entry;
50     private Label displayname_label;
51     private Entry issuer_entry;
52     private Label issuer_label;
53     private Entry username_entry;
54     private Label username_label;
55     private Entry password_entry;
56     private Label password_label;
57     private CheckButton remember_checkbutton;
58     private Label message_label;
59     public bool complete;
60     
61     public string display_name {
62         get { return displayname_entry.get_text(); }
63     }
64
65     public string issuer {
66         get { return issuer_entry.get_text(); }
67     }
68
69     public string username {
70         get { return username_entry.get_text(); }
71     }
72
73     public string password {
74         get { return password_entry.get_text(); }
75     }
76
77     public bool store_password {
78         get { return remember_checkbutton.active; }
79     }
80
81     public AddIdentityDialog()
82     {
83         this.set_title(_("Add ID Card"));
84         this.set_modal(true);
85
86         this.add_buttons(_("Add ID Card"), ResponseType.OK,
87                          CANCEL, ResponseType.CANCEL);
88         var content_area = this.get_content_area();
89         ((Box) content_area).set_spacing(12);
90         
91         displayname_label = new Label(@"$displayname_labeltext:");
92         displayname_label.set_alignment(1,(float) 0.5);
93         displayname_entry = new Entry();
94         issuer_label = new Label(@"$issuer_labeltext:");
95         issuer_label.set_alignment(1,(float) 0.5);
96         this.issuer_entry = new Entry();
97         username_label = new Label(@"$username_labeltext:");
98         username_label.set_alignment(1,(float) 0.5);
99         this.username_entry = new Entry();
100         password_label = new Label(@"$password_labeltext:");
101         password_label.set_alignment(1,(float) 0.5);
102         this.password_entry = new Entry();
103         password_entry.set_invisible_char('*');
104         password_entry.set_visibility(false);
105         this.remember_checkbutton = new CheckButton.with_label(_("Remember password"));
106         this.message_label = new Label("");
107         message_label.set_visible(false);
108
109         set_atk_relation(displayname_label, displayname_entry, Atk.RelationType.LABEL_FOR);
110         set_atk_relation(issuer_label, issuer_entry, Atk.RelationType.LABEL_FOR);
111         set_atk_relation(username_label, username_entry, Atk.RelationType.LABEL_FOR);
112         set_atk_relation(password_entry, password_entry, Atk.RelationType.LABEL_FOR);
113
114         var table = new Table(6, 2, false);
115         table.set_col_spacings(10);
116         table.set_row_spacings(10);
117         
118         table.attach_defaults(message_label, 0, 2, 0, 1);
119         table.attach_defaults(displayname_label, 0, 1, 1, 2);
120         table.attach_defaults(displayname_entry, 1, 2, 1, 2);
121         table.attach_defaults(issuer_label, 0, 1, 2, 3);
122         table.attach_defaults(issuer_entry, 1, 2, 2, 3);
123         table.attach_defaults(username_label, 0, 1, 3, 4);
124         table.attach_defaults(username_entry, 1, 2, 3, 4);
125         table.attach_defaults(password_label, 0, 1, 4, 5);
126         table.attach_defaults(password_entry, 1, 2, 4, 5);
127         table.attach_defaults(remember_checkbutton,  1, 2, 5, 6);
128
129         this.response.connect(on_response);
130         var vbox = new VBox(false, 0);
131         vbox.set_border_width(6);
132         vbox.pack_start(table, false, false, 0);
133
134         ((Container) content_area).add(vbox);
135
136         this.set_border_width(6);
137         this.set_resizable(false);
138         this.show_all();
139     }
140
141     private static string update_preamble(string preamble)
142     {
143         if (preamble == "")
144             return _("Missing required field: ");
145         return _("Missing required fields: ");
146     }
147
148     private static string update_message(string old_message, string new_item)
149     {
150         string message;
151         if (old_message == "")
152             message = new_item;
153         else
154             message = old_message + ", " + new_item;
155         return message;
156     }
157
158     private static void check_field(string field, Label label, string fieldname, ref string preamble, ref string message)
159     {
160         if (field != "") {
161             label.set_markup(@"$fieldname:");
162             return;
163         }
164         label.set_markup(@"<span foreground=\"red\">$fieldname:</span>");
165         preamble = update_preamble(preamble);
166         message = update_message(message, fieldname);
167     }
168
169     private bool check_fields()
170     {
171         string preamble = "";
172         string message = "";
173         string password_test = store_password ? password : "not required";
174         check_field(display_name, displayname_label, displayname_labeltext, ref preamble, ref message);
175         check_field(issuer, issuer_label, issuer_labeltext, ref preamble, ref message);
176         check_field(username, username_label, username_labeltext, ref preamble, ref message);
177         check_field(password_test, password_label, password_labeltext, ref preamble, ref message);
178         if (message != "") {
179             message_label.set_visible(true);
180             message_label.set_markup(@"<span foreground=\"red\">$preamble$message</span>");
181             return false;
182         }
183         return true;
184     }
185
186     private void on_response(Dialog source, int response_id)
187     {
188         switch (response_id) {
189         case ResponseType.OK:
190             complete = check_fields();
191             break;
192         case ResponseType.CANCEL:
193             complete = true;
194             break;
195         }
196     }
197
198     private void set_atk_relation(Widget widget, Widget target_widget, Atk.RelationType relationship)
199     {
200         var atk_widget = widget.get_accessible();
201         var atk_target_widget = target_widget.get_accessible();
202
203         atk_widget.add_relationship(relationship, atk_target_widget);
204     }
205 }