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
|
// SPDX-FileCopyrightText: 2023 Deminder <tremminder@gmail.com>
// SPDX-License-Identifier: GPL-3.0-or-later
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import {
gettext as _,
ngettext as _n,
pgettext as C_,
} from '../modules/translation.js';
import { logDebug, throttleTimeout } from '../modules/util.js';
import * as Control from './control.js';
import {
longDurationString,
ScheduleInfo,
getShutdownScheduleFromSettings,
getSliderMinutesFromSettings,
} from '../modules/schedule-info.js';
import {
actionLabel,
Action,
mapLegacyAction,
UnsupportedActionError,
} from './action.js';
const Signals = imports.signals;
export class Timer {
_timerCancellable = null;
_updateScheduleCancel = null;
_action = new Action();
info = {
externalShutdown: new ScheduleInfo({
external: true,
}),
externalWake: new ScheduleInfo({
external: false,
mode: 'wake',
}),
internalShutdown: new ScheduleInfo({ mode: 'PowerOff' }),
};
constructor({ settings }) {
this._settings = settings;
const [updateScheduleThrottled, updateScheduleCancel] = throttleTimeout(
() => this.updateSchedule(),
20
);
this._updateScheduleCancel = updateScheduleCancel;
this._settingsIds = [
'root-mode-value',
'shutdown-timestamp-value',
'shutdown-mode-value',
].map(settingName =>
settings.connect(`changed::${settingName}`, updateScheduleThrottled)
);
this.updateSchedule();
}
destroy() {
if (this._settings === null) {
throw new Error('should not destroy twice');
}
// Disconnect settings
this._settingsIds.forEach(id => this._settings.disconnect(id));
this._settingsIds = [];
this._settings = null;
// Cancel schedule updates
if (this._updateScheduleCancel !== null) {
this._updateScheduleCancel();
this._updateScheduleCancel = null;
}
// Cancel internal timer
if (this._timerCancellable !== null) {
this._timerCancellable.cancel();
this._timerCancellable = null;
}
// External schedules (for 'shutdown' and 'wake') are not stopped
}
updateSchedule() {
const oldInternal = this.info.internalShutdown;
const internal = getShutdownScheduleFromSettings(this._settings);
this.info.internalShutdown = internal;
logDebug(
`[updateSchedule] internal schedule: ${internal.label} (old internal schedule: ${oldInternal.label})`
);
this._updateRootModeProtection(oldInternal);
if (
internal.mode !== oldInternal.mode ||
internal.deadline !== oldInternal.deadline
) {
this.emit('change');
if (internal.scheduled) {
if (internal.minutes > 0) {
// Show schedule info
this.emit(
'message',
C_('StartSchedulePopup', '%s in %s').format(
actionLabel(internal.mode),
longDurationString(
internal.minutes,
h => _n('%s hour', '%s hours', h),
m => _n('%s minute', '%s minutes', m)
)
)
);
} else {
logDebug(`[updateSchedule] hidden message for '< 1 minute' schedule`);
}
if (this._timerCancellable !== null) {
this._timerCancellable.cancel();
this._timerCancellable = null;
}
this.executeActionDelayed()
.then(() => {
logDebug('[executeActionDelayed] done');
})
.catch(err => {
console.error('executeActionDelayed', err);
});
} else {
if (this._timerCancellable !== null) {
this._timerCancellable.cancel();
this._timerCancellable = null;
}
if (oldInternal.scheduled) {
this.emit('message', _('Shutdown Timer stopped'));
}
}
}
}
async executeAction() {
const internal = this.info.internalShutdown;
if (!internal.scheduled) {
logDebug(`Refusing to exectute non scheduled action! '${internal.mode}'`);
return;
}
logDebug(`Running '${internal.mode}' timer action...`);
try {
this.emit('change');
// Refresh root mode protection
await Promise.all([
this._updateRootModeProtection(),
// Do shutdown
this._action.shutdownAction(
internal.mode,
this._settings.get_boolean('show-end-session-dialog-value')
),
]);
} catch (err) {
if (/* destroyed */ this._settings === null) {
throw err;
}
const newInternal = this.info.internalShutdown;
if (newInternal.scheduled && newInternal.secondsLeft > 0) {
logDebug('[timer] Replaced by new schedule.');
return;
}
if (err instanceof UnsupportedActionError) {
this.emit(
'message',
_('%s is not supported!').format(actionLabel(internal.mode))
);
}
}
await this.toggleShutdown(false, '');
}
async executeActionDelayed() {
const internal = this.info.internalShutdown;
const secs = internal.secondsLeft;
if (secs > 0) {
logDebug(`Started delayed action: ${internal.minutes}min remaining`);
try {
this._timerCancellable = new Gio.Cancellable();
await Control.sleepUntilDeadline(
internal.deadline,
this._timerCancellable
);
this._timerCancellable = null;
} catch {
logDebug(`Canceled delayed action: ${internal.minutes}min remaining`);
return;
}
}
await this.executeAction();
}
async toggleWake(wake) {
try {
logDebug('[toggleWake] wake', wake);
await this._action.wakeAction(
wake,
getSliderMinutesFromSettings(this._settings, 'wake')
);
this.emit('change-external');
} catch (err) {
this.emit(
'message',
C_('Error', '%s\n%s').format(_('Wake action failed!'), err)
);
this._settings.set_int('shutdown-timestamp-value', -1);
}
}
async toggleShutdown(shutdown, legacyAction) {
// Update shutdown action
const action = mapLegacyAction(legacyAction);
if (action === undefined) {
this.emit(
'message',
_('Unknown shutdown action: "%s"!').format(legacyAction)
);
return;
}
logDebug('[toggleShutdown] shutdown', shutdown, 'action', action);
if (action !== '') {
this._settings.set_string('shutdown-mode-value', action);
}
// Update shutdown timestamp
this._settings.set_int(
'shutdown-timestamp-value',
shutdown
? GLib.DateTime.new_now_utc().to_unix() +
Math.max(
1,
getSliderMinutesFromSettings(this._settings, 'shutdown') * 60
)
: -1
);
if (shutdown) {
await this._action.inhibitSuspend();
} else {
await this._action.uninhibitSuspend();
}
if (this._settings.get_boolean('auto-wake-value')) {
await this.toggleWake(shutdown);
}
}
get state() {
return this.info.internalShutdown.scheduled
? this.info.internalShutdown.secondsLeft > 0
? 'active'
: 'action'
: 'inactive';
}
/**
* Ensure that shutdown/reboot is executed even if the Timer fails by running
* the `shutdown` command delayed by 1 minute.
*/
async _updateRootModeProtection(oldInternal) {
if (this._settings.get_boolean('root-mode-value')) {
const internal = this.info.internalShutdown;
logDebug('[updateRootModeProtection] mode ', internal.mode);
try {
if (oldInternal?.scheduled && oldInternal.mode !== internal.mode) {
await Control.stopRootModeProtection(oldInternal);
}
if (internal.scheduled) {
await Control.startRootModeProtection(internal);
} else {
await Control.stopRootModeProtection(internal);
}
} catch (err) {
this.emit(
'message',
C_('Error', '%s\n%s').format(_('Root mode protection failed!'), err)
);
console.error('[updateRootModeProtection]', err);
}
this.emit('change-external');
}
}
}
Signals.addSignalMethods(Timer.prototype);
|