-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEndSessionDialog.vala
More file actions
187 lines (161 loc) · 6.47 KB
/
EndSessionDialog.vala
File metadata and controls
187 lines (161 loc) · 6.47 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
/*
* SPDX-License-Identifier: GPL-2.0-or-later
* SPDX-FileCopyrightText: 2019-2024 elementary, Inc. (https://elementary.io)
* 2011-2015 Tom Beckmann
*/
/*
* docs taken from unity indicator-session's
* src/backend-dbus/org.gnome.SessionManager.EndSessionDialog.xml
*/
public enum QuickSettings.EndSessionDialogType {
LOGOUT = 0,
SHUTDOWN = 1,
RESTART = 2
}
public class QuickSettings.EndSessionDialog : Granite.MessageDialog {
public signal void reboot ();
public signal void shutdown ();
public signal void logout ();
public signal void cancelled ();
public EndSessionDialogType dialog_type { get; construct; }
private Gtk.CheckButton? updates_check_button;
private Gtk.EventControllerKey key_controller;
public EndSessionDialog (QuickSettings.EndSessionDialogType type) {
Object (dialog_type: type);
}
construct {
string button_text;
switch (dialog_type) {
case EndSessionDialogType.LOGOUT:
image_icon = new ThemedIcon ("system-log-out");
primary_text = _("Are you sure you want to Log Out?");
secondary_text = _("This will close all open applications.");
button_text = _("Log Out");
break;
case EndSessionDialogType.SHUTDOWN:
case EndSessionDialogType.RESTART:
image_icon = new ThemedIcon ("system-shutdown");
primary_text = _("Are you sure you want to Shut Down?");
secondary_text = _("This will close all open applications and turn off this device.");
button_text = _("Shut Down");
break;
default:
warn_if_reached ();
break;
}
var image = new Gtk.Image.from_icon_name (icon_name, Gtk.IconSize.DIALOG) {
valign = Gtk.Align.START
};
/*
* the indicator does not have a separate item for restart, that's
* why we show both shutdown and restart for the restart action
* (which is sent for shutdown as described above)
*/
if (dialog_type == EndSessionDialogType.RESTART) {
var confirm_restart = (Gtk.Button) add_button (_("Restart"), 1);
confirm_restart.clicked.connect (() => {
set_offline_trigger (REBOOT); // This will just do nothing if no updates are available
reboot ();
destroy ();
});
}
var cancel = (Gtk.Button) add_button (_("Cancel"), Gtk.ResponseType.CANCEL);
var confirm = (Gtk.Button) add_button (button_text, Gtk.ResponseType.ACCEPT);
confirm.get_style_context ().add_class (Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION);
if (dialog_type != LOGOUT) {
bool has_prepared_updates = false;
try {
has_prepared_updates = Pk.offline_get_prepared_ids ().length > 0;
} catch (Error e) {
warning ("Failed to check for prepared updates, assuming no: %s", e.message);
}
if (has_prepared_updates) {
updates_check_button = new Gtk.CheckButton () {
active = true,
label = _("Install pending system updates"),
};
updates_check_button.show ();
custom_bin.add (updates_check_button);
}
}
cancel.grab_focus ();
var cancel_action = new SimpleAction ("cancel", null);
cancel_action.activate.connect (() => {
cancelled ();
destroy ();
});
cancel.clicked.connect (() => {
cancel_action.activate (null);
});
key_controller = new Gtk.EventControllerKey (this);
key_controller.key_released.connect ((keyval, keycode, state) => {
if (keyval == Gdk.Key.Escape) {
cancel_action.activate (null);
}
});
confirm.clicked.connect (() => {
if (dialog_type == EndSessionDialogType.RESTART || dialog_type == EndSessionDialogType.SHUTDOWN) {
if (set_offline_trigger (POWER_OFF)) {
reboot ();
} else {
shutdown ();
}
} else {
logout ();
}
destroy ();
});
realize.connect (() => Idle.add_once (() => init_wl ()));
}
private bool set_offline_trigger (Pk.OfflineAction action) {
if (updates_check_button == null) {
return false;
}
if (updates_check_button.active) {
try {
Pk.offline_trigger (action);
return true;
} catch (Error e) {
critical ("Failed to set offline trigger for updates: %s", e.message);
}
} else {
try {
if (Pk.offline_get_action () != UNSET) {
Pk.offline_cancel ();
}
} catch (Error e) {
critical ("Failed to check/cancel offline trigger for updates: %s", e.message);
}
}
return false;
}
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<PantheonDesktop.Shell> (name, ref PantheonDesktop.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 ();
var extended_behavior = desktop_shell.get_extended_behavior (wl_surface);
extended_behavior.set_keep_above ();
extended_behavior.make_centered ();
extended_behavior.make_modal (1);
}
}
}
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;
}
}
}
}