-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathMainWindow.vala
More file actions
527 lines (438 loc) · 17.9 KB
/
MainWindow.vala
File metadata and controls
527 lines (438 loc) · 17.9 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
* SPDX-License-Identifier: GPL-2.0-or-later
* SPDX-FileCopyrightText: 2018-2025 elementary, Inc. (https://elementary.io)
*
* Authors: Corentin Noël <corentin@elementary.io>
*/
public class Greeter.MainWindow : Gtk.ApplicationWindow {
public LightDM.Greeter lightdm_greeter { private get; construct; }
private Pantheon.Desktop.Greeter? desktop_greeter;
private GLib.Queue<unowned Greeter.UserCard> user_cards;
private Gtk.SizeGroup card_size_group;
private Hdy.Carousel carousel;
private Greeter.Settings settings;
private GLib.Settings gsettings;
private Gtk.Revealer datetime_revealer;
private Greeter.DateTimeWidget datetime_widget;
private unowned LightDM.UserList lightdm_user_list;
private int current_user_card_index = -1;
private unowned Greeter.BaseCard? current_card = null;
private bool installer_mode = false;
private Gtk.EventControllerKey key_controller;
public MainWindow (LightDM.Greeter lightdm_greeter) {
Object (lightdm_greeter: lightdm_greeter);
}
construct {
app_paintable = true;
decorated = false;
set_visual (get_screen ().get_rgba_visual ());
gsettings = new GLib.Settings ("io.elementary.greeter");
settings = new Greeter.Settings ();
lightdm_greeter.show_message.connect (show_message);
lightdm_greeter.show_prompt.connect (show_prompt);
lightdm_greeter.authentication_complete.connect (authentication_complete);
var guest_login_button = new Gtk.Button.with_label (_("Log in as Guest"));
var manual_login_button = new Gtk.ToggleButton.with_label (_("Manual Login…"));
var extra_login_box = new Gtk.Box (HORIZONTAL, 12) {
homogeneous = true,
halign = CENTER,
valign = END,
vexpand = true
};
if (lightdm_greeter.has_guest_account_hint) {
extra_login_box.add (guest_login_button);
}
if (lightdm_greeter.show_manual_login_hint) {
extra_login_box.add (manual_login_button);
}
datetime_widget = new Greeter.DateTimeWidget ();
datetime_revealer = new Gtk.Revealer () {
child = datetime_widget,
transition_type = CROSSFADE,
valign = CENTER,
vexpand = true
};
user_cards = new GLib.Queue<unowned Greeter.UserCard> ();
var manual_card = new Greeter.ManualCard ();
carousel = new Hdy.Carousel () {
allow_long_swipes = true,
vexpand = true
};
var manual_login_stack = new Gtk.Stack () {
transition_type = Gtk.StackTransitionType.CROSSFADE
};
manual_login_stack.add (carousel);
manual_login_stack.add (manual_card);
var main_box = new Gtk.Box (VERTICAL, 24) {
margin_top = 24,
margin_bottom = 24
};
main_box.add (datetime_revealer);
main_box.add (manual_login_stack);
main_box.add (extra_login_box);
child = main_box;
manual_login_button.toggled.connect (() => {
if (manual_login_button.active) {
if (lightdm_greeter.in_authentication) {
try {
lightdm_greeter.cancel_authentication ();
} catch (Error e) {
critical (e.message);
}
}
manual_login_stack.visible_child = manual_card;
current_card = manual_card;
} else {
if (lightdm_greeter.in_authentication) {
try {
lightdm_greeter.cancel_authentication ();
} catch (Error e) {
critical (e.message);
}
}
manual_login_stack.visible_child = carousel;
current_card = user_cards.peek_nth (current_user_card_index);
try {
lightdm_greeter.authenticate (((UserCard) current_card).lightdm_user.name);
} catch (Error e) {
critical (e.message);
}
}
});
guest_login_button.clicked.connect (() => {
try {
lightdm_greeter.authenticate_as_guest ();
} catch (Error e) {
critical (e.message);
}
});
card_size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
card_size_group.add_widget (extra_login_box);
card_size_group.add_widget (manual_card);
lightdm_greeter.bind_property ("hide-users-hint", manual_login_button, "sensitive", GLib.BindingFlags.SYNC_CREATE | GLib.BindingFlags.INVERT_BOOLEAN);
lightdm_greeter.bind_property ("hide-users-hint", manual_login_button, "active", GLib.BindingFlags.SYNC_CREATE);
lightdm_user_list = LightDM.UserList.get_instance ();
lightdm_user_list.user_added.connect (() => {
load_users.begin ();
});
manual_card.do_connect_username.connect (do_connect_username);
manual_card.do_connect.connect (do_connect);
key_controller = new Gtk.EventControllerKey (this) {
propagation_phase = CAPTURE
};
key_controller.key_pressed.connect ((keyval, keycode, state) => {
if (!(current_card is UserCard)) {
return Gdk.EVENT_PROPAGATE;
}
unowned var focused_entry = (Gtk.Entry) get_focus ();
if (focused_entry == null || !focused_entry.is_ancestor (current_card) || focused_entry.text != "") {
return Gdk.EVENT_PROPAGATE;
}
var ltr = Gtk.StateFlags.DIR_LTR in get_state_flags ();
if (keyval == Gdk.Key.Left) {
if (ltr) {
go_previous ();
} else {
go_next ();
}
return Gdk.EVENT_STOP;
} else if (keyval == Gdk.Key.Right) {
if (ltr) {
go_next ();
} else {
go_previous ();
}
return Gdk.EVENT_STOP;
}
return Gdk.EVENT_PROPAGATE;
});
carousel.page_changed.connect (handle_page_changed);
load_users.begin (() => {
/* A significant delay is required in order for the window and card to be focused at
* at boot. TODO: Find whether boot sequence can be tweaked to fix this.
*/
Timeout.add (500, () => {
get_style_context ().add_class ("initialized");
if (current_card != null) {
current_card.grab_focus ();
}
return Source.REMOVE;
});
});
maximize ();
if (settings.activate_numlock) {
try {
Process.spawn_async (null, { "numlockx", "on" }, null, SpawnFlags.SEARCH_PATH, null, null);
} catch (Error e) {
warning ("Unable to spawn numlockx to set numlock state");
}
}
main_box.realize.connect (init_panel);
}
private void init_panel () {
if (Gdk.Display.get_default () is Gdk.Wayland.Display) {
// We have to wrap in Idle otherwise the Meta.Window of the WaylandSurface in Gala is still null
Idle.add_once (init_wl);
} else {
init_x ();
}
}
private static Wl.RegistryListener registry_listener;
private void init_wl () {
registry_listener.global = registry_handle_global;
unowned var display = Gdk.Display.get_default ();
if (display is Gdk.Wayland.Display) {
unowned var wl_display = ((Gdk.Wayland.Display) display).get_wl_display ();
var wl_registry = wl_display.get_registry ();
wl_registry.add_listener (
registry_listener,
this
);
if (wl_display.roundtrip () < 0) {
return;
}
}
}
public void registry_handle_global (Wl.Registry wl_registry, uint32 name, string @interface, uint32 version) {
if (@interface == "io_elementary_pantheon_shell_v1") {
var desktop_shell = wl_registry.bind<Pantheon.Desktop.Shell> (name, ref Pantheon.Desktop.Shell.iface, uint32.min (version, 1));
unowned var window = get_window ();
if (window is Gdk.Wayland.Window) {
unowned var wl_surface = ((Gdk.Wayland.Window) window).get_wl_surface ();
desktop_greeter = desktop_shell.get_greeter (wl_surface);
desktop_greeter.init ();
}
}
}
private void init_x () {
var display = Gdk.Display.get_default ();
if (display is Gdk.X11.Display) {
unowned var xdisplay = ((Gdk.X11.Display) display).get_xdisplay ();
var window = ((Gdk.X11.Window) get_window ()).get_xid ();
var prop = xdisplay.intern_atom ("_MUTTER_HINTS", false);
var value = "greeter=1";
xdisplay.change_property (window, prop, X.XA_STRING, 8, 0, (uchar[]) value, value.length);
}
}
private void show_message (string text, LightDM.MessageType type) {
var messagetext = Greeter.FPrintUtils.string_to_messagetext (text);
switch (messagetext) {
case Greeter.FPrintUtils.MessageText.FPRINT_TIMEOUT:
case Greeter.FPrintUtils.MessageText.FPRINT_ERROR:
case Greeter.FPrintUtils.MessageText.OTHER:
current_card.use_fingerprint = false;
break;
default:
current_card.use_fingerprint = true;
break;
}
}
private void show_prompt (string text, LightDM.PromptType type = LightDM.PromptType.QUESTION) {
if (current_card is ManualCard) {
if (type == LightDM.PromptType.SECRET) {
((ManualCard) current_card).ask_password ();
} else {
((ManualCard) current_card).wrong_username ();
}
}
}
// Called after the credentials are checked, might be authenticated or not.
private void authentication_complete () {
var user_card = current_card as Greeter.UserCard;
if (user_card != null) {
gsettings.set_string ("last-user", user_card.lightdm_user.name);
}
if (lightdm_greeter.is_authenticated) {
try {
unowned var session = current_card.selected_session_type;
// If the greeter is running on the install medium, check if the Installer has signalled
// that it wants the greeter to launch the live (demo) session by means of touching a file
if (installer_mode) {
var demo_mode_file = File.new_for_path ("/var/lib/lightdm/demo-mode");
if (demo_mode_file.query_exists ()) {
demo_mode_file.@delete ();
session = "pantheon";
} else {
session = "installer";
}
}
lightdm_greeter.start_session_sync (session);
return;
} catch (Error e) {
var error_dialog = new Granite.MessageDialog.with_image_from_icon_name (
_("Unable to Log In"),
_("Starting the session has failed."),
"dialog-error",
Gtk.ButtonsType.CLOSE
);
error_dialog.show_error_details (e.message);
error_dialog.present ();
error_dialog.response.connect (error_dialog.destroy);
}
}
if (user_card != null) {
try {
lightdm_greeter.authenticate (user_card.lightdm_user.name);
} catch (Error e) {
critical (e.message);
}
}
current_card.wrong_credentials ();
carousel.interactive = true;
}
private async void load_users () {
// Check if the installer is installed
var installer_desktop = new DesktopAppInfo ("io.elementary.installer.desktop");
if (installer_desktop != null) {
installer_mode = true;
}
if (lightdm_user_list.length > 0) {
datetime_revealer.reveal_child = true;
lightdm_user_list.users.foreach ((user) => {
add_card (user);
});
unowned string? select_user = lightdm_greeter.select_user_hint;
var user_to_select = select_user != null ? select_user : gsettings.get_string ("last-user");
bool user_selected = false;
user_cards.head.foreach ((card) => {
if (card.lightdm_user.name == user_to_select) {
carousel.scroll_to (card);
user_selected = true;
}
});
if (!user_selected) {
unowned var user_card = user_cards.peek_head ();
user_card.show_input = true;
carousel.scroll_to (user_card);
}
} else {
datetime_revealer.reveal_child = false;
/* We're not certain that scaling factor will change, but try to wait for GSD in case it does */
Timeout.add (500, () => {
try {
var initial_setup = AppInfo.create_from_commandline ("io.elementary.initial-setup", null, GLib.AppInfoCreateFlags.NONE);
initial_setup.launch (null, null);
} catch (Error e) {
string error_text = _("Unable to Launch Initial Setup");
critical ("%s: %s", error_text, e.message);
var error_dialog = new Granite.MessageDialog.with_image_from_icon_name (
error_text,
_("Initial Setup creates your first user. Without it, you will not be able to log in and may need to reinstall the OS."),
"dialog-error",
Gtk.ButtonsType.CLOSE
);
error_dialog.show_error_details (e.message);
error_dialog.present ();
error_dialog.response.connect (error_dialog.destroy);
}
return Source.REMOVE;
});
}
}
private void add_card (LightDM.User lightdm_user) {
var user_card = new Greeter.UserCard (lightdm_user);
user_card.show_all ();
user_card.do_connect.connect (do_connect);
user_card.click_gesture.pressed.connect ((gesture, n_press, x, y) => {
assert (gesture.widget is UserCard);
var _user_card = (UserCard) gesture.widget;
if (!_user_card.show_input) {
carousel.scroll_to (_user_card);
_user_card.grab_focus ();
}
});
user_card.go_left.connect (() => {
if (Gtk.StateFlags.DIR_LTR in get_state_flags ()) {
go_previous ();
} else {
go_next ();
}
});
user_card.go_right.connect (() => {
if (Gtk.StateFlags.DIR_LTR in get_state_flags ()) {
go_next ();
} else {
go_previous ();
}
});
carousel.add (user_card);
card_size_group.add_widget (user_card);
user_cards.push_tail (user_card);
}
private void handle_page_changed (uint index) {
if (index == current_user_card_index) {
return;
}
unowned var user_card = user_cards.peek_nth (index);
if (user_card == null) {
return;
}
if (current_card != null && current_card is UserCard) {
((UserCard) current_card).show_input = false;
}
current_user_card_index = (int) index;
current_card = user_card;
datetime_widget.is_24h = user_card.is_24h;
user_card.set_settings ();
user_card.show_input = true;
user_card.grab_focus ();
if (user_card.lightdm_user.session != null) {
application.activate_action ("select-session", new GLib.Variant.string (user_card.lightdm_user.session));
}
if (lightdm_greeter.in_authentication) {
try {
lightdm_greeter.cancel_authentication ();
} catch (Error e) {
critical (e.message);
}
}
try {
lightdm_greeter.authenticate (user_card.lightdm_user.name);
} catch (Error e) {
critical (e.message);
}
}
private void do_connect_username (string username) {
if (lightdm_greeter.in_authentication) {
try {
lightdm_greeter.cancel_authentication ();
} catch (Error e) {
critical (e.message);
}
}
try {
lightdm_greeter.authenticate (username);
} catch (Error e) {
critical (e.message);
}
}
private void do_connect (string? credential) {
if (credential != null) {
try {
lightdm_greeter.respond (credential);
} catch (Error e) {
critical (e.message);
}
}
carousel.interactive = false;
carousel.scroll_to (current_card);
}
private void go_previous () {
if (!carousel.interactive) {
return;
}
unowned Greeter.UserCard? next_card = user_cards.peek_nth (current_user_card_index - 1);
if (next_card != null) {
carousel.scroll_to (next_card);
}
}
private void go_next () {
if (!carousel.interactive) {
return;
}
unowned Greeter.UserCard? next_card = user_cards.peek_nth (current_user_card_index + 1);
if (next_card != null) {
carousel.scroll_to (next_card);
}
}
}