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