Debian Codegraph @

src/dialogs.vala

raw
1 /* dialogs.vala
2 *
3 * Copyright (C) Red Hat, Inc
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Marek Kasik <[email protected]>
19 *
20 */
21
22 namespace Connections {
23 [GtkTemplate (ui = "/org/gnome/Connections/ui/dialog.ui")]
24 private class DialogsWindow : Hdy.Window {
25 private bool change_verification;
26 private string certificate_fingerprint;
27 private string connection_name;
28 private bool certificate_verification_show_flag;
29 private bool certificate_change_verification_show_flag;
30 private bool authentication_show_flag;
31 private bool need_username;
32 private bool need_password;
33 private bool need_domain;
34
35 [GtkChild]
36 public unowned Gtk.Stack content_stack;
37
38 [GtkChild]
39 public unowned Gtk.Stack header_stack;
40
41 [GtkChild]
42 private unowned Gtk.Label text_label;
43
44 [GtkChild]
45 private unowned Gtk.Label fingerprint_value;
46
47 [GtkChild]
48 private unowned Gtk.Label certificate_change_text_label;
49
50 [GtkChild]
51 private unowned Gtk.Button connect_button;
52
53 [GtkChild]
54 private unowned Gtk.Switch delete_local_certificate_switch;
55
56 [GtkChild]
57 private unowned Gtk.Image show_password_icon;
58
59 [GtkChild]
60 private unowned Gtk.Entry username_entry;
61
62 [GtkChild]
63 private unowned Gtk.Entry password_entry;
64
65 [GtkChild]
66 private unowned Gtk.Entry domain_entry;
67
68 [GtkChild]
69 private unowned Gtk.Label authentication_text;
70
71 [GtkChild]
72 private unowned Hdy.HeaderBar dialog_headerbar;
73
74 [GtkChild]
75 private unowned Hdy.ActionRow username_row;
76
77 [GtkChild]
78 private unowned Hdy.ActionRow password_row;
79
80 [GtkChild]
81 private unowned Hdy.ActionRow domain_row;
82
83 [GtkChild]
84 private unowned Gtk.Button show_password_button;
85
86 [GtkChild]
87 private unowned Gtk.MenuButton help_domain_button;
88
89 [GtkChild]
90 private unowned Gtk.Button cancel_authentication_button;
91
92 [GtkChild]
93 private unowned Gtk.Button authenticate_authentication_button;
94
95 private Mutex mutex;
96
97 construct {
98 valign = Gtk.Align.START;
99 halign = Gtk.Align.CENTER;
100
101 certificate_verification_show_flag = false;
102 certificate_change_verification_show_flag = false;
103 authentication_show_flag = false;
104 mutex = Mutex ();
105
106 try {
107 var style_provider = new Gtk.CssProvider ();
108 var css_style = "button#authenticate_button { border-radius: 0px; border-bottom: 0px; border-right: 0px; }
109 button#cancel_button { border-radius: 0px; border-bottom: 0px; border-left: 0px; border-right: 0px; }";
110 style_provider.load_from_data (css_style, css_style.length);
111 Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (),
112 style_provider,
113 Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
114 } catch (GLib.Error error) {
115 warning ("Failed to load CSS: %s", error.message);
116 }
117
118 Timeout.add (50, check_show_flags);
119
120 delete_event.connect (hide_when_delete);
121
122 // Set up proper focus chain: username_entry -> password_entry -> show_password_button ->
123 // domain_entry -> help_domain_button -> authenticate_authentication_button -> cancel_authentication_button
124 username_entry.focus.connect ((direction) => {
125 switch (direction) {
126 case Gtk.DirectionType.TAB_FORWARD:
127 if (password_row.visible) {
128 password_entry.grab_focus ();
129 } else if (domain_row.visible) {
130 domain_entry.grab_focus ();
131 } else {
132 authenticate_authentication_button.grab_focus ();
133 }
134 return true;
135 case Gtk.DirectionType.TAB_BACKWARD:
136 cancel_authentication_button.grab_focus ();
137 return true;
138 default:
139 return false;
140 }
141 });
142
143 password_entry.focus.connect ((direction) => {
144 switch (direction) {
145 case Gtk.DirectionType.TAB_FORWARD:
146 show_password_button.grab_focus ();
147 return true;
148 case Gtk.DirectionType.TAB_BACKWARD:
149 if (username_row.visible) {
150 username_entry.grab_focus ();
151 } else {
152 cancel_authentication_button.grab_focus ();
153 }
154 return true;
155 default:
156 return false;
157 }
158 });
159
160 show_password_button.focus.connect ((direction) => {
161 switch (direction) {
162 case Gtk.DirectionType.TAB_FORWARD:
163 if (domain_row.visible) {
164 domain_entry.grab_focus ();
165 } else {
166 authenticate_authentication_button.grab_focus ();
167 }
168 return true;
169 case Gtk.DirectionType.TAB_BACKWARD:
170 password_entry.grab_focus ();
171 return true;
172 default:
173 return false;
174 }
175 });
176
177 domain_entry.focus.connect ((direction) => {
178 switch (direction) {
179 case Gtk.DirectionType.TAB_FORWARD:
180 help_domain_button.grab_focus ();
181 return true;
182 case Gtk.DirectionType.TAB_BACKWARD:
183 if (password_row.visible) {
184 show_password_button.grab_focus ();
185 } else if (username_row.visible) {
186 username_entry.grab_focus ();
187 } else {
188 cancel_authentication_button.grab_focus ();
189 }
190 return true;
191 default:
192 return false;
193 }
194 });
195
196 help_domain_button.focus.connect ((direction) => {
197 switch (direction) {
198 case Gtk.DirectionType.TAB_FORWARD:
199 authenticate_authentication_button.grab_focus ();
200 return true;
201 case Gtk.DirectionType.TAB_BACKWARD:
202 domain_entry.grab_focus ();
203 return true;
204 default:
205 return false;
206 }
207 });
208
209 authenticate_authentication_button.focus.connect ((direction) => {
210 switch (direction) {
211 case Gtk.DirectionType.TAB_FORWARD:
212 case Gtk.DirectionType.DOWN:
213 cancel_authentication_button.grab_focus ();
214 return true;
215 case Gtk.DirectionType.TAB_BACKWARD:
216 case Gtk.DirectionType.UP:
217 if (domain_row.visible) {
218 help_domain_button.grab_focus ();
219 } else if (password_row.visible) {
220 show_password_button.grab_focus ();
221 } else if (username_row.visible) {
222 username_entry.grab_focus ();
223 } else {
224 cancel_authentication_button.grab_focus ();
225 }
226 return true;
227 default:
228 return false;
229 }
230 });
231
232 cancel_authentication_button.focus.connect ((direction) => {
233 switch (direction) {
234 case Gtk.DirectionType.TAB_FORWARD:
235 case Gtk.DirectionType.DOWN:
236 // Cycle back to the first visible entry
237 if (username_row.visible) {
238 username_entry.grab_focus ();
239 } else if (password_row.visible) {
240 password_entry.grab_focus ();
241 } else if (domain_row.visible) {
242 domain_entry.grab_focus ();
243 } else {
244 authenticate_authentication_button.grab_focus ();
245 }
246 return true;
247 case Gtk.DirectionType.TAB_BACKWARD:
248 case Gtk.DirectionType.UP:
249 authenticate_authentication_button.grab_focus ();
250 return true;
251 default:
252 return false;
253 }
254 });
255 }
256
257 bool check_show_flags () {
258 bool scvf, sccvf, saf;
259
260 mutex.lock ();
261
262 scvf = certificate_verification_show_flag;
263 if (certificate_verification_show_flag)
264 certificate_verification_show_flag = false;
265
266 sccvf = certificate_change_verification_show_flag;
267 if (certificate_change_verification_show_flag)
268 certificate_change_verification_show_flag = false;
269
270 saf = authentication_show_flag;
271 if (authentication_show_flag)
272 authentication_show_flag = false;
273
274 mutex.unlock ();
275
276 if (scvf) {
277 show_certificate_verification_real ();
278 }
279
280 if (sccvf) {
281 show_certificate_change_verification_real ();
282 }
283
284 if (saf) {
285 show_authentication_real ();
286 }
287
288 return true;
289 }
290
291 string split_fingerprint (string fingerprint) {
292 string[] fp = fingerprint.split (":");
293 string[] tmpv = {};
294
295 if (fp.length == 32) {
296 for (var i = 0 ; i < 4 ; i++) {
297 tmpv += string.joinv (":", fp[i * 8: i * 8 + 4]) + " " + string.joinv (":", fp[i * 8 + 4: i * 8 + 8]);
298 }
299 string fp2 = string.joinv ("\n", tmpv).up ();
300
301 return fp2;
302 } else {
303 return "";
304 }
305 }
306
307 public delegate void CertificateVerifyFunc (bool verified);
308 CertificateVerifyFunc certificate_verify_func;
309
310 private void show_certificate_verification_real () {
311 mutex.lock ();
312
313 text_label.label = _("Connecting to “%s” for the first time. To be sure you're connecting to the machine it claims to be, please verify the fingerprints match. This process is only done once.").printf (connection_name);
314
315 fingerprint_value.label = split_fingerprint (certificate_fingerprint);
316
317 mutex.unlock ();
318
319 content_stack.set_visible_child_name ("certificate-verification");
320 header_stack.set_visible_child_name ("verify-button-page");
321 content_stack.show ();
322 header_stack.show ();
323 dialog_headerbar.show ();
324 present ();
325 }
326
327 /* It is safe to call this from other threads as is usually the case. */
328 public void show_certificate_verification (string name,
329 string fingerprint,
330 owned CertificateVerifyFunc? certificate_verify_func_in) {
331 mutex.lock ();
332
333 change_verification = false;
334 connection_name = name.dup ();
335 certificate_fingerprint = fingerprint.dup ();
336 certificate_verify_func = certificate_verify_func_in;
337 certificate_verification_show_flag = true;
338
339 mutex.unlock ();
340 }
341
342 public delegate void CertificateChangeVerifyFunc (bool verified);
343 CertificateChangeVerifyFunc certificate_change_verify_func;
344 public void show_certificate_change_verification_real () {
345 mutex.lock ();
346
347 certificate_change_text_label.label = _("The remote server “%s” certificate doesn't match local copy. It may be someone's pretending to be the server.").printf (connection_name);
348
349 fingerprint_value.label = split_fingerprint (certificate_fingerprint);
350
351 mutex.unlock ();
352
353 content_stack.set_visible_child_name ("certificate-change-verification");
354 header_stack.set_visible_child_name ("connect-button-page");
355 content_stack.show ();
356 header_stack.show ();
357 dialog_headerbar.show ();
358 present ();
359 }
360
361 /* It is safe to call this from other threads as is usually the case. */
362 public void show_certificate_change_verification (string name,
363 string fingerprint,
364 owned CertificateChangeVerifyFunc? certificate_change_verify_func_in) {
365 mutex.lock ();
366
367 change_verification = true;
368 connection_name = name.dup ();
369 certificate_fingerprint = fingerprint.dup ();
370 certificate_change_verify_func = certificate_change_verify_func_in;
371 certificate_change_verification_show_flag = true;
372
373 mutex.unlock ();
374 }
375
376 public delegate void AuthenticationFunc (string username,
377 string password,
378 string domain);
379 AuthenticationFunc authentication_func;
380 public void show_authentication_real () {
381 mutex.lock ();
382
383 username_row.set_visible (need_username);
384 password_row.set_visible (need_password);
385 domain_row.set_visible (need_domain);
386
387 authentication_text.label = _("The remote server “%s” requires the following authentication details to continue to connect:").printf (connection_name);
388
389 mutex.unlock ();
390
391 content_stack.set_visible_child_name ("authentication");
392 content_stack.show ();
393 dialog_headerbar.hide ();
394 present ();
395
396 // Set focus to the first visible entry field
397 if (need_username) {
398 username_entry.grab_focus ();
399 } else if (need_password) {
400 password_entry.grab_focus ();
401 } else if (need_domain) {
402 domain_entry.grab_focus ();
403 }
404 }
405
406 /* It is safe to call this from other threads as is usually the case. */
407 public void show_authentication (string name,
408 bool need_username_in,
409 bool need_password_in,
410 bool need_domain_in,
411 owned AuthenticationFunc? authentication_func_in) {
412 mutex.lock ();
413
414 need_username = need_username_in;
415 need_password = need_password_in;
416 need_domain = need_domain_in;
417 connection_name = name.dup ();
418 authentication_func = authentication_func_in;
419 authentication_show_flag = true;
420
421 mutex.unlock ();
422 }
423
424 [GtkCallback]
425 private void cancel_connection () {
426 mutex.lock ();
427
428 if (change_verification) {
429 if (certificate_change_verify_func != null)
430 certificate_change_verify_func (false);
431 } else {
432 if (certificate_verify_func != null)
433 certificate_verify_func (false);
434 }
435
436 mutex.unlock ();
437
438 dismiss ();
439 }
440
441 [GtkCallback]
442 private void verify_button_clicked_cb () {
443 mutex.lock ();
444
445 if (change_verification) {
446 if (certificate_change_verify_func != null)
447 certificate_change_verify_func (true);
448 } else {
449 if (certificate_verify_func != null)
450 certificate_verify_func (true);
451 }
452
453 mutex.unlock ();
454
455 dismiss ();
456 }
457
458 [GtkCallback]
459 private void connect_button_clicked_cb () {
460 show_certificate_verification_real ();
461 }
462
463 [GtkCallback]
464 private void delete_local_certificate_switch_changed () {
465 connect_button.sensitive = delete_local_certificate_switch.active;
466 }
467
468 [GtkCallback]
469 private bool on_key_pressed (Gtk.Widget widget, Gdk.EventKey event) {
470 if (event.keyval == Gdk.Key.Escape) {
471 cancel_connection ();
472 return true;
473 }
474
475 return false;
476 }
477
478 [GtkCallback]
479 private void show_password_button_clicked () {
480 if (password_entry.visibility) {
481 show_password_icon.icon_name = "view-reveal-symbolic";
482 password_entry.visibility = false;
483 } else {
484 show_password_icon.icon_name = "user-not-tracked-symbolic";
485 password_entry.visibility = true;
486 }
487 }
488
489 [GtkCallback]
490 private void cancel_authentication_clicked () {
491 mutex.lock ();
492
493 hide ();
494
495 if (authentication_func != null) {
496 authentication_func ("", "", "");
497 authentication_func = null;
498 }
499
500 mutex.unlock ();
501
502 dismiss ();
503 }
504
505 [GtkCallback]
506 private void authenticate_button_clicked () {
507 mutex.lock ();
508
509 hide ();
510
511 if (authentication_func != null) {
512 authentication_func (username_entry.text, password_entry.text, domain_entry.text);
513 authentication_func = null;
514 }
515
516 mutex.unlock ();
517
518 dismiss ();
519 }
520
521 [GtkCallback]
522 private void on_username_entry_activated () {
523 authenticate_button_clicked ();
524 }
525
526 [GtkCallback]
527 private void on_password_entry_activated () {
528 authenticate_button_clicked ();
529 }
530
531 [GtkCallback]
532 private void on_domain_entry_activated () {
533 authenticate_button_clicked ();
534 }
535
536 private void dismiss () {
537 hide ();
538 username_entry.text = "";
539 password_entry.text = "";
540 domain_entry.text = "";
541 delete_local_certificate_switch.set_active (false);
542 }
543
544 private bool hide_when_delete () {
545 cancel_connection ();
546 return true;
547 }
548 }
549 }