This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
572 lines (503 loc) · 23 KB
/
extension.js
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Guillotine: a gnome extension designed for efficiently carrying out executions of commands from a customizable menu
// Copyright (C) 2021 Christian Klaue ([email protected])
'use strict';
const UI = imports.ui;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const St = imports.gi.St;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const DEBUG = 0;
const INFO = 1;
const WARNING = 2;
const ERROR = 3;
const OFF = 4;
var LogLevel = WARNING;
var NotificationLevel = OFF;
/******************************************************************************/
/***** LOGGING *****/
/******************************************************************************/
function debug(message) {
if (LogLevel <= DEBUG) {
log("[guillotine debug] " + message);
}
if (NotificationLevel <= DEBUG) {
notify("[guillotine debug]", message);
}
}
function info(message) {
if (LogLevel <= INFO) {
log("[guillotine info] " + message);
}
if (NotificationLevel <= INFO) {
notify("[guillotine info]", message);
}
}
function warning(message) {
if (LogLevel <= WARNING) {
log("[guillotine WARNING] " + message);
}
if (NotificationLevel <= WARNING) {
notify("[guillotine Warning]", message);
}
}
function error(message, error) {
if (LogLevel <= ERROR) {
if (typeof error === 'undefined') {
log("[guillotine ERROR] " + message);
} else {
log("[guillotine ERROR] " + message + "\n" + error)
}
}
if (NotificationLevel <= WARNING) {
if (typeof error === 'undefined') {
notify("[guillotine ERROR]", message);
} else {
notify("[guillotine ERROR]", message + "\n" + error)
}
}
}
function notify(message, details) {
UI.main.notify(message, details || "");
}
/******************************************************************************/
/***** MENUITEMS *****/
/******************************************************************************/
const commandChecks = [{ name: "title", type: "string", default: "???" },
{ name: "icon", type: "string" },
{ name: "command", type: "string" },
{ name: "instancing", type: "string", values: ["singleinstance", "multipleinstances", "killbeforerestart"], default: "multipleinstances" },
{ name: "killOnDisable", type: "boolean", default: true }];
class Command {
constructor(properties) {
this.canceled = false;
let self = {};
// sanity checks
parseProperties(properties, self, commandChecks);
Object.assign(this, self);
// setup UI
if ("icon" in this) {
if (this.icon.toLowerCase() === "guillotine-symbolic") this.UI = new UI.popupMenu.PopupImageMenuItem(this.title, Gio.icon_new_for_string(Me.path + "/guillotine-symbolic.svg"));
else this.UI = new UI.popupMenu.PopupImageMenuItem(this.title, this.icon);
}
else
this.UI = new UI.popupMenu.PopupMenuItem(this.title);
if (!("command" in this))
this.UI.setSensitive(false);
if (this.instancing === "killbeforerestart") {
notify("Guillotine Warning", "The option 'killBeforeRestart' has been removed. The option no longer has any effect.");
}
// setup callbacks
this.UI.connect('activate', this.execute.bind(this))
this.processes = {};
debug("Menu item '" + this.title + "' initialized.");
}
execute() {
if (this.instancing === "singleinstance") {
this.UI.setSensitive(false);
}
try {
let [_, argv] = GLib.shell_parse_argv(this.command);
let subprocess = new Gio.Subprocess({
argv: argv,
flags: Gio.SubprocessFlags.NONE
});
subprocess.init(null);
// Check the process completion
let pid = subprocess.get_identifier();
subprocess.wait_check_async(null, this.executed.bind(this, pid));
this.processes[pid] = subprocess;
debug("Process for '" + this.title + "' [" + pid + "] started.");
} catch (e) {
this.UI.setSensitive(false);
error("Spawning process for '" + this.title + "' failed.", e);
}
}
executed(pid, process, result) {
process.wait_finish(result);
delete this.processes[pid];
if (process.get_if_signaled()) {
let signal = process.get_term_sig();
warning("Process for '" + this.title + "' [" + pid + "] was terminated by signal: " + signal + ".");
} else {
let result = process.get_exit_status();
if (result) warning("Process for '" + this.title + "' [" + pid + "] finished with return code: " + result + ".");
else info("Process for '" + this.title + "' [" + pid + "] finished without error.");
}
if (!this.canceled) this.UI.setSensitive(true);
}
cancel() {
debug("Cancel called for " + this.title + ".");
this.canceled = true;
if (this.killOnDisable) for (const pid in this.processes) {
info("Process for '" + this.title + "' [" + pid + "] is still running. Termination signal will be issued.");
this.processes[pid].force_exit();
}
}
}
const switchChecks = [{ name: "title", type: "string", default: "???" },
{ name: "icon", type: "string" },
{ name: "start", type: "string" },
{ name: "stop", type: "string" },
{ name: "check", type: "string" },
{ name: "interval", type: "number" },
{ name: "interval_s", type: "number" },
{ name: "interval_ms", type: "number" }];
class Switch {
constructor(properties) {
this.canceled = false;
let self = {};
// sanity checks
parseProperties(properties, self, switchChecks);
Object.assign(this, self);
this.UI = new UI.popupMenu.PopupSwitchMenuItem(this.title, false);
if ("icon" in this) {
this.UI.icon = new St.Icon({ style_class: 'popup-menu-icon' });
if (this.icon.toLowerCase() === "guillotine-symbolic") this.UI.icon.gicon = Gio.icon_new_for_string(Me.path + "/guillotine-symbolic.svg");
else this.UI.icon.icon_name = this.icon;
this.UI.insert_child_at_index(this.UI.icon, 1);
}
this.UI.setSensitive(false);
// setup callbacks
this.UI.connect('activate', this.switch.bind(this));
this.mode = 'interval';
if ("check" in this) {
this.timer = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 0, this.test.bind(this, true));
} else {
error("Switch '" + this.title + "' has no check command defined. Switch is disabled.");
}
if (!("interval" in this) && !("interval_ms" in this) && !("interval_s" in this)) {
this.interval_s = 10;
}
if (("interval_s" in this)) {
delete this.interval_ms;
delete this.interval;
}
if (("interval_ms" in this)) {
delete this.interval;
}
if (("interval" in this)) {
this.interval_ms = this.interval;
delete this.interval;
}
this.processes = {};
debug("Menu item '" + this.title + "' initialized.");
}
switch() {
info("switch fn");
// don't allow another interaction with this item
this.UI.setSensitive(false);
// cancel all automatic interval checks & signal manual switching
if ("timer" in this) GLib.source_remove(this.timer);
delete this.timer;
this.mode = 'switch';
let command;
if (this.UI.state) command = this.start;
else command = this.stop;
try {
let [_, argv] = GLib.shell_parse_argv(command);
let subprocess = new Gio.Subprocess({
argv: argv,
flags: Gio.SubprocessFlags.NONE
});
subprocess.init(null);
let pid = subprocess.get_identifier();
subprocess.wait_check_async(null, this.switched.bind(this, pid, this.UI.state));
this.processes[pid] = subprocess;
if (this.UI.state) debug("Start process for switch '" + this.title + "' started.");
else debug("Stop process for switch '" + this.title + "' started.");
} catch (e) {
if (this.UI.state) error("Spawning start process for switch '" + this.title + "' failed. Switch is disabled.", e);
else error("Spawning stop process for switch '" + this.title + "' failed. Switch is disabled.", e);
}
}
switched(pid, oldState, process, result) {
process.wait_finish(result);
delete this.processes[pid];
if (this.canceled) return;
if (process.get_if_signaled()) {
let signal = process.get_term_sig();
if (oldState) warning("Spawning start process for switch '" + this.title + "' was terminated by signal: " + signal + ".");
else warning("Spawning stop process for switch '" + this.title + "' was terminated by signal: " + signal + ".");
} else {
let result = process.get_exit_status();
if (result) {
if (oldState) warning("Start process for switch '" + this.title + "' [" + pid + "] finished with return code: " + result + ".");
else warning("Stop process for switch '" + this.title + "' [" + pid + "] finished with return code: " + result + ".");
}
else {
if (oldState) debug("Start process for switch '" + this.title + "' [" + pid + "] finished without error.");
else debug("Start process for switch '" + this.title + "' [" + pid + "] finished without error.");
}
}
this.test(false);
}
test(interval) {
// prevent further automatic tests for now
if ("timer" in this) GLib.source_remove(this.timer);
delete this.timer;
// if canceled: abort any further tests
if (this.canceled) return false;
// if this is an automatic triggered interval test (interval is true) but there
// was some manual switching (timer is not defined), then abort and also
// don't allow further automatic tests
if ((interval) && (this.mode === 'switch')) {
return false;
}
try {
let [_, argv] = GLib.shell_parse_argv(this.check);
let subprocess = new Gio.Subprocess({
argv: argv,
flags: Gio.SubprocessFlags.NONE
});
subprocess.init(null);
let pid = subprocess.get_identifier();
subprocess.wait_check_async(null, this.tested.bind(this, pid, interval));
this.processes[pid] = subprocess;
debug("Check process for switch '" + this.title + "' [" + pid + "] started.");
} catch (e) {
error("Spawning the check process for switch '" + this.title + "' failed. Switch is disabled.", e);
return false;
}
return false;
}
tested(pid, interval, process, result) {
process.wait_finish(result);
delete this.processes[pid];
// if canceled: abort any further tests
if (this.canceled) return false;
if (process.get_if_signaled()) {
let signal = process.get_term_sig();
warning("Check process for switch '" + this.title + "' [" + pid + "] was terminated by signal: " + signal + ". No more checks will be scheduled.");
} else if ((this.mode === 'switch') && (interval)) {
// if this is an automatic triggered interval test (interval is true) but there
// was some manual switching (timer is not defined), then abort and also
// don't allow further automatic tests
debug("Check process for switch '" + this.title + "' [" + pid + "] exited without error. Result is ignored due to initiating a manual switching meanwhile");
} else {
let result = process.get_exit_status();
if (result) debug("Check process for switch '" + this.title + "' [" + pid + "] exited with return code: " + result + " --> switch is turned off.");
else debug("Check process for switch '" + this.title + "' [" + pid + "] exited without error --> switch is turned on.");
if (result) this.UI.setToggleState(false);
else this.UI.setToggleState(true);
if ((!this.canceled) && (result) && ("start" in this)) this.UI.setSensitive(true);
if ((!this.canceled) && (!result) && ("stop" in this)) this.UI.setSensitive(true);
if (!this.canceled) {
this.mode = 'interval';
if ("interval_s" in this) this.timer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, this.interval_s, this.test.bind(this, true));
else this.timer = GLib.timeout_add(GLib.PRIORITY_DEFAULT, this.interval_ms, this.test.bind(this, true));
}
}
}
cancel() {
debug("cancel called for " + this.title);
this.canceled = true;
if ("timer" in this) GLib.source_remove(this.timer);
delete this.timer;
this.mode = 'switch';
for (const pid in this.processes) {
info("Process for switch '" + this.title + "' [" + pid + "] is still running. Termination signal will be issued.");
this.processes[pid].force_exit();
}
}
}
const menuChecks = [{ name: "title", type: "string", default: "???" },
{ name: "icon", type: "string" },
{ name: "items", type: "object" }];
class SubMenu {
constructor(properties) {
let self = {};
// sanity checks
parseProperties(properties, self, menuChecks);
Object.assign(this, self);
debug(this.icon);
if ("icon" in this) {
this.UI = new UI.popupMenu.PopupSubMenuMenuItem(this.title, true);
if (this.icon.toLowerCase() === "guillotine-symbolic") this.UI.icon.gicon = Gio.icon_new_for_string(Me.path + "/guillotine-symbolic.svg");
else this.UI.icon.icon_name = this.icon;
} else this.UI = new UI.popupMenu.PopupSubMenuMenuItem(this.title);
this.items = parseMenu(self.items);
for (const item in this.items) {
this.UI.menu.addMenuItem(this.items[item].UI);
}
debug("Menu item '" + this.title + "' initialized.");
}
cancel() {
for (const item in this.items) {
this.items[item].cancel();
}
}
}
class Separator {
constructor(properties) {
debug("Separator initialized.");
this.UI = new UI.popupMenu.PopupSeparatorMenuItem(this.title);
}
cancel() {
}
}
/******************************************************************************/
/***** Guillotine *****/
/******************************************************************************/
const settingsChecks = [
{ name: "icon", type: "string", default: "guillotine-symbolic" },
{ name: "loglevel", type: "string", default: "warning", values: ["debug", "info", "warning", "error"] },
{ name: "notificationlevel", type: "string", values: ["debug", "info", "warning", "error"] }
];
class Guillotine {
constructor(meta) {
info("Initializing " + Me.metadata.name + " version " + Me.metadata.version);
}
enable() {
if (this.button) {
info("Change of config detected: restarting " + Me.metadata.name);
this.disable();
} else {
info("Enabling " + Me.metadata.name + " version " + Me.metadata.version);
}
this.menu = [];
try {
this.loadConfig();
// check config if there are settings included
if (typeof this.config.settings !== "object" || Array.isArray(this.config.settings)) this.config.settings = {};
this.settings = {};
parseProperties(this.config.settings, this.settings, settingsChecks);
this.menu = parseMenu(this.config.menu);
}
catch (e) {
error("Loading config failed.", e);
// icon
this.icon = new St.Icon({
icon_name: "dialog-error",
style_class: "system-status-icon"
});
// button
this.button = new UI.panelMenu.Button();
this.button.add_child(this.icon);
UI.main.panel.addToStatusArea(Me.metadata.uuid, this.button);
return;
}
LogLevel = ["debug", "info", "warning", "error"].indexOf(this.settings.loglevel.toLowerCase());
debug("Log level at: " + this.settings.loglevel);
if ("notificationlevel" in this.settings) {
NotificationLevel = ["debug", "info", "warning", "error"].indexOf(this.settings.notificationlevel.toLowerCase());
debug("Notification level at: " + this.settings.notificationlevel);
} else {
NotificationLevel = OFF;
debug("Notifications are disabled.");
}
// icon
this.icon = new St.Icon({ style_class: "system-status-icon" });
if (this.settings.icon.toLowerCase() == "guillotine-symbolic")
this.icon.gicon = Gio.icon_new_for_string(Me.path + "/guillotine-symbolic.svg");
else
this.icon.icon_name = this.settings.icon;
// button
this.button = new UI.panelMenu.Button(0.0, "guillotine", false);
this.button.add_child(this.icon);
for (const item in this.menu) {
this.button.menu.addMenuItem(this.menu[item].UI);
}
UI.main.panel.addToStatusArea("guillotine", this.button);
}
disable() {
info("Disabling " + Me.metadata.name);
for (const item in this.menu) {
this.menu[item].cancel();
}
if (this.configMonitor) {
this.configMonitor.cancel();
this.configMonitor = null;
}
if (this.button) {
this.button.destroy();
this.button = null;
}
if (this.configFile) {
this.configFile = null;
}
}
loadConfig() {
// determine config location
let configFilename = imports.misc.extensionUtils.getSettings().get_string("config");
configFilename = configFilename || GLib.get_home_dir() + "/.config/guillotine.json";
debug("Config location: " + configFilename);
// TODO: async
// check if custom config exists; restore default config file otherwise
this.configFile = Gio.File.new_for_path(configFilename);
if (!this.configFile.query_exists(null)) {
let defaultConfig = Gio.File.new_for_path(Me.path + "/default.json");
defaultConfig.copy(this.configFile, 0, null, null);
info("Config not found @ location: " + configFilename + ". Default config restored.");
}
// setup monitor for config file
this.configMonitor = this.configFile.monitor(Gio.FileMonitorFlags.NONE, null);
this.configMonitor.connect("changed", this.enable.bind(this));
// TODO: async
// load config file
this.config = {};
let [ok, content] = GLib.file_get_contents(configFilename);
if (ok) {
let contentString = imports.byteArray.toString(content);
this.config = JSON.parse(contentString);
} else throw new Error("Could not load config file.");
}
}
function parseProperties(source, target, checks) {
for (const property in checks) {
debug("checking property " + checks[property].name + ": " + source[checks[property].name]);
if (checks[property].name in source) {
// property is defined
if (typeof source[checks[property].name] !== checks[property].type) {
// property is defined with wrong type
if ("default" in checks[property]) {
// property is defined using wrong type --> default value is used
warning("invalid value for property " + checks[property].name + ": " + source[checks[property].name] + "\nUsing default value: " + checks[property].default);
target[checks[property].name] = checks[property].default;
} else
// property was defined using wrong type --> default value does not exist --> no value used
warning("invalid value for property " + checks[property].name + ": " + source[checks[property].name] + "\nIgnoring the value");
} else
// property is defined with correct type
if ("values" in checks[property]) {
// property is defined with correct type & checks contain a list of valid values
if (!checks[property].values.includes(source[checks[property].name].toLowerCase())) {
// property is defined with correct type & checks contain a list of valid values --> property does not match any of that list --> default value is used
warning("invalid value for property " + checks[property].name + ": " + source[checks[property].name] + "\nUsing default value: " + checks[property].default);
target[checks[property].name] = checks[property].default;
} else
// property is defined with correct type & checks contain a list of valid values --> property does match one of that list
target[checks[property].name] = source[checks[property].name].toLowerCase();
} else
// property is defined with correct type & checks contain a list of valid values --> property does match one of that list
target[checks[property].name] = source[checks[property].name];
} else if ("default" in checks[property]) {
// property is not defined --> use default
debug("no value for property " + checks[property].name + ", using default value: " + checks[property].default);
target[checks[property].name] = checks[property].default;
}
debug("done checking property " + checks[property].name + ": " + target[checks[property].name]);
}
}
function parseMenu(menu) {
let items = [];
let types = [];
types["command"] = Command;
types["switch"] = Switch;
types["submenu"] = SubMenu;
types["separator"] = Separator;
if (typeof menu === "object" && Array.isArray(menu)) {
for (const item in menu) {
if (!("type" in menu[item])) throw new Error("Invalid menu item: missing 'type' property");
if (typeof types[menu[item].type.toLowerCase()] === "undefined") throw new Error("Invalid value for property 'type': " + menu[item].type);
let menuItem = new types[menu[item].type.toLowerCase()](menu[item]);
items.push(menuItem);
}
}
return items;
}
/******************************************************************************/
/***** INIT *****/
/******************************************************************************/
function init(meta) {
return new Guillotine(meta);
}