-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
EncryptView.vala
300 lines (243 loc) · 10 KB
/
EncryptView.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright 2018–2021 elementary, Inc. (https://elementary.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class EncryptView : AbstractInstallerView {
private ErrorRevealer confirm_entry_revealer;
private ErrorRevealer pw_error_revealer;
private Gtk.Button next_button;
private Granite.ValidatedEntry confirm_entry;
private ValidatedEntry pw_entry;
private Gtk.LevelBar pw_levelbar;
private const string SKIP_STRING = _("Don’t Encrypt");
public EncryptView () {
Object (cancellable: true);
}
construct {
var image = new Gtk.Image.from_icon_name ("drive-harddisk", Gtk.IconSize.INVALID) {
pixel_size = 128
};
var overlay_image = new Gtk.Image.from_icon_name ("security-high", Gtk.IconSize.INVALID) {
pixel_size = 64,
halign = Gtk.Align.END,
valign = Gtk.Align.END
};
var overlay = new Gtk.Overlay () {
child = image,
halign = CENTER,
width_request = 60
};
overlay.add_overlay (overlay_image);
var title_label = new Gtk.Label (_("Enable Drive Encryption"));
var details_label = new Gtk.Label (_("Encrypt this device's drive if required for added protection, but be sure you understand:")) {
hexpand = true,
max_width_chars = 1, // Make Gtk wrap, but not expand the window
wrap = true,
xalign = 0
};
details_label.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);
var protect_row = new DescriptionRow (
_("Data will only be protected from others with physical access to this device when it is shut down."),
"security-high-symbolic",
"green"
);
var restart_row = new DescriptionRow (
_("The encryption password will be required each time this device is turned on. Store it somewhere safe."),
"system-reboot-symbolic",
"blue"
);
var keyboard_row = new DescriptionRow (
_("A built-in or USB keyboard will be required to type the encryption password each time this device is turned on."),
"input-keyboard-symbolic",
"slate"
);
var description_box = new Gtk.Box (VERTICAL, 32) {
valign = CENTER
};
description_box.add (details_label);
description_box.add (protect_row);
description_box.add (restart_row);
description_box.add (keyboard_row);
var description = new Gtk.Label (
_("If you forget the encryption password, <b>you will not be able to recover data.</b> This is a unique password for this device, not the password for your user account.")
) {
hexpand = true,
margin_bottom = 12,
max_width_chars = 1, // Make Gtk wrap, but not expand the window
use_markup = true,
wrap = true,
xalign = 0
};
var pw_label = new Granite.HeaderLabel (_("Choose Encryption Password"));
pw_error_revealer = new ErrorRevealer (".");
pw_error_revealer.label_widget.get_style_context ().add_class (Gtk.STYLE_CLASS_WARNING);
pw_entry = new ValidatedEntry ();
pw_entry.visibility = false;
pw_levelbar = new Gtk.LevelBar.for_interval (0.0, 100.0);
pw_levelbar.set_mode (Gtk.LevelBarMode.CONTINUOUS);
pw_levelbar.add_offset_value ("low", 50.0);
pw_levelbar.add_offset_value ("high", 75.0);
pw_levelbar.add_offset_value ("middle", 75.0);
var confirm_label = new Granite.HeaderLabel (_("Confirm Password"));
confirm_entry = new Granite.ValidatedEntry () {
sensitive = false,
visibility = false
};
confirm_entry_revealer = new ErrorRevealer (".");
confirm_entry_revealer.label_widget.get_style_context ().add_class (Gtk.STYLE_CLASS_ERROR);
var password_box = new Gtk.Box (VERTICAL, 3) {
valign = CENTER
};
password_box.add (description);
password_box.add (pw_label);
password_box.add (pw_entry);
password_box.add (pw_levelbar);
password_box.add (pw_error_revealer);
password_box.add (confirm_label);
password_box.add (confirm_entry);
password_box.add (confirm_entry_revealer);
var stack = new Gtk.Stack () {
transition_type = SLIDE_LEFT_RIGHT,
vexpand = true
};
stack.add (description_box);
stack.add (password_box);
title_area.add (overlay);
title_area.add (title_label);
content_area.add (stack);
var back_button = new Gtk.Button.with_label (_("Back"));
var encrypt_button = new Gtk.Button.with_label (_("Choose Password"));
next_button = new Gtk.Button.with_label (_(SKIP_STRING)) {
can_default = true
};
next_button.get_style_context ().add_class (Gtk.STYLE_CLASS_SUGGESTED_ACTION);
action_box_end.add (back_button);
action_box_end.add (encrypt_button);
action_box_end.add (next_button);
next_button.grab_focus ();
back_button.clicked.connect (() => {
stack.visible_child = description_box;
next_button.label = _(SKIP_STRING);
next_button.sensitive = true;
back_button.hide ();
encrypt_button.show ();
});
encrypt_button.clicked.connect (() => {
stack.visible_child = password_box;
next_button.label = _("Set Encryption Password");
update_next_button ();
back_button.show ();
encrypt_button.hide ();
pw_entry.grab_focus ();
});
next_button.clicked.connect (() => {
if (stack.visible_child == password_box) {
Configuration.get_default ().encryption_password = pw_entry.text;
}
((Hdy.Deck) get_parent ()).navigate (FORWARD);
});
pw_entry.changed.connect (() => {
pw_entry.is_valid = check_password ();
confirm_entry.is_valid = confirm_password ();
update_next_button ();
});
confirm_entry.changed.connect (() => {
confirm_entry.is_valid = confirm_password ();
update_next_button ();
});
show_all ();
back_button.hide ();
}
private bool check_password () {
if (pw_entry.text == "") {
confirm_entry.text = "";
confirm_entry.sensitive = false;
pw_levelbar.value = 0;
pw_entry.set_icon_from_icon_name (Gtk.EntryIconPosition.SECONDARY, null);
pw_error_revealer.reveal_child = false;
} else {
confirm_entry.sensitive = true;
var pwquality = new PasswordQuality.Settings ();
void* error;
var quality = pwquality.check (pw_entry.text, null, null, out error);
if (quality >= 0) {
pw_entry.set_icon_from_icon_name (Gtk.EntryIconPosition.SECONDARY, "process-completed-symbolic");
pw_error_revealer.reveal_child = false;
pw_levelbar.value = quality;
} else {
pw_entry.set_icon_from_icon_name (Gtk.EntryIconPosition.SECONDARY, "dialog-warning-symbolic");
pw_error_revealer.reveal_child = true;
pw_error_revealer.label = ((PasswordQuality.Error) quality).to_string (error);
pw_levelbar.value = 0;
}
return true;
}
return false;
}
private bool confirm_password () {
if (confirm_entry.text != "") {
if (pw_entry.text != confirm_entry.text) {
confirm_entry_revealer.label = _("Passwords do not match");
confirm_entry_revealer.reveal_child = true;
} else {
confirm_entry_revealer.reveal_child = false;
return true;
}
} else {
confirm_entry_revealer.reveal_child = false;
}
return false;
}
private void update_next_button () {
if (pw_entry.is_valid && confirm_entry.is_valid) {
next_button.sensitive = true;
next_button.has_default = true;
} else {
next_button.sensitive = false;
}
}
private class ValidatedEntry : Gtk.Entry {
public bool is_valid { get; set; default = false; }
construct {
activates_default = true;
}
}
private class ErrorRevealer : Gtk.Box {
public bool reveal_child { get; set; }
public Gtk.Label label_widget { get; private set; }
public string label { get; construct set; }
public ErrorRevealer (string label) {
Object (label: label);
}
construct {
label_widget = new Gtk.Label (label) {
halign = END,
justify = RIGHT,
max_width_chars = 55,
use_markup = true,
wrap = true,
xalign = 1
};
label_widget.get_style_context ().add_class (Granite.STYLE_CLASS_SMALL_LABEL);
var revealer = new Gtk.Revealer () {
child = label_widget,
transition_type = CROSSFADE
};
add (revealer);
bind_property ("reveal-child", revealer, "reveal-child");
bind_property ("label", label_widget, "label");
}
}
}