Repository navigation
Expand file tree
/
Copy pathextension.js
More file actions
332 lines (277 loc) · 12.1 KB
/
Copy pathextension.js
File metadata and controls
332 lines (277 loc) · 12.1 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
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import GTop from 'gi://GTop';
import St from 'gi://St';
import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as MessageTray from 'resource:///org/gnome/shell/ui/messageTray.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
const INTERVAL_MINUTES = 60;
class Indicator extends PanelMenu.Button {
static {
GObject.registerClass(this);
}
constructor(settings, refreshCallback, prefsCallback) {
GTop.glibtop_init();
super(0.5, _('Brew Updates'), false);
this._refreshCallback = refreshCallback;
this._prefsCallback = prefsCallback;
this._settings = settings;
this._showNotification = this._settings.get_boolean('update-auto-notify');
// connect destroy event
this.connect('destroy', () => {
this._refreshCallback = null;
this._prefsCallback = null;
this._settings = null;
this._gioIdleIcon = null;
this._gioLoadingIcon = null;
this._gioUpdatesIcon = null;
this._notificationSource = null;
});
// build a new panel box to align icon and label
this._box = new St.BoxLayout({ style_class: 'brew-update-section' });
// load icon from the ext path
const ext = Extension.lookupByURL(import.meta.url);
const idleIconFile = ext.dir.resolve_relative_path("icons/pub-symbolic.svg");
const loadingIconFile = ext.dir.resolve_relative_path("icons/spinner-symbolic.svg");
const updatesIconFile = ext.dir.resolve_relative_path("icons/shield-warning-symbolic.svg");
// create two gio file instances
this._gioIdleIcon = new Gio.FileIcon({file: idleIconFile});
this._gioLoadingIcon = new Gio.FileIcon({file: loadingIconFile});
this._gioUpdatesIcon = new Gio.FileIcon({file: updatesIconFile});
// create a new icon with the file loaded from the ext path
this._icon = new St.Icon({
style_class: 'brew-update-icon',
gicon: this._gioIdleIcon,
});
// create empty label for displaying the number of updates
this._label = new St.Label({
text: '',
style_class: 'brew-update-label',
y_expand: true,
y_align: Clutter.ActorAlign.CENTER,
});
// add label actor for the label
this.label_actor = this._label
// add icon and label to the box
this._box.add_child(this._icon);
this._box.add_child(this._label);
// add box to the panel
this.add_child(this._box);
// build the default Menu
this.buildDefaultMenu();
}
getNotificationSource() {
if(!this._notificationSource) {
// get a new notification policy
const notificationPolicy = new MessageTray.NotificationGenericPolicy();
// build the extensions notification source
this._notificationSource = new MessageTray.Source({
// The source name (e.g. application name)
title: _('Brew Updates'),
// An icon for the notification (defaults to the source's icon)
icon: this._gioIdleIcon,
// The notification policy
policy: notificationPolicy,
});
// Reset the notification source if it's destroyed
this._notificationSource.connect('destroy', _source => {
this._notificationSource = null;
});
// add notification source to the shell
Main.messageTray.add(this._notificationSource);
}
// return notification source
return this._notificationSource;
}
buildDefaultMenu() {
// add action for updates
this.menu.addAction(_('Check for Update'), this._refreshCallback);
// add separator between update action and preferences
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// Add a menu item to open the preferences window
this.menu.addAction(_('Preferences'), () => this._prefsCallback());
}
setState(count) {
// check for updates
const hasUpdates = Number.isFinite(count) && count > 0;
// set the icon and the text
this._icon.gicon = hasUpdates ? this._gioUpdatesIcon : this._gioIdleIcon;
this._label.text = hasUpdates ? String(count) : '';
// check if there are updates and notify user
if(hasUpdates && this._showNotification){
// build notification
const notification = new MessageTray.Notification({
// The source of the notification
source: this.getNotificationSource(),
// A title for the notification
title: _('New brew updates'),
// The content of the notification
body: _('There are brew updates avaliable'),
// An icon for the notification (defaults to the source's icon)
gicon: this._gioUpdatesIcon,
// The urgency of the notification
urgency: MessageTray.Urgency.NORMAL,
});
// fire notification
this._notificationSource.addNotification(notification);
}
}
setLoadingState() {
// set the icon and the text
this._icon.gicon = this._gioLoadingIcon;
this._label.text = '';
}
setIdleState() {
// set the icon and the text
this._icon.gicon = this._gioIdleIcon;
this._label.text = '';
}
toggleShowNotification() {
this._showNotification = this._settings.get_boolean('update-auto-notify');
}
}
export default class BrewUpdatesExtension extends Extension {
enable() {
// create timout id property
this._timeoutId = 0;
// local save of the settings object
this._settings = this.getSettings();
console.log("[BREW UPDATES]");
console.log(this._settings.get_boolean('update-auto-check'));
console.log(this._settings.get_boolean('update-auto-notify'));
console.log(this._settings.get_uint('update-auto-check-interval'));
console.log("[BREW UPDATES]");
// get the current interval value
this._interval = this._settings.get_uint('update-auto-check-interval');
// get the current autoUpdate value
this._autoUpate = this._settings.get_boolean('update-auto-check');
// create indicatior instance
this._indicator = new Indicator(this.getSettings(), () => this._refresh(), () => this.openPreferences());
// add the indicator to the panel
Main.panel.addToStatusArea(this.uuid, this._indicator);
// run the refresh function and the schedule function
if(this._autoUpate) {
this._refresh();
this._schedule();
}
// Watch for changes to a specific setting
this._settings.connect('changed', (settings, key) => {
// check if the key is `update-auto-check` and start the schedule
if(key == 'update-auto-check') {
// get the current autoUpdate value
this._autoUpate = this._settings.get_boolean('update-auto-check');
// check auto update value
if(this._autoUpate) {
// run the refresh function and the schedule function
this._refresh();
this._schedule();
} else {
// check if the timout object is set, if yes we destroy it
if (this._timeoutId) {
GLib.Source.remove(this._timeoutId);
this._timeoutId = 0;
}
// set idle state
this._indicator.setIdleState();
}
}
// check if the key is `update-auto-notify` and update the showNotification value of the indicator
if(key == 'update-auto-notify') {
// toggle show notification
this._indicator.toggleShowNotification();
}
// check if the key is `update-auto-check-interval` and update the interval
if(key == 'update-auto-check-interval') {
// get the new value
this._interval = this._settings.get_uint('update-auto-check-interval');
console.log(this._interval);
// restart the schedule
this._schedule();
}
});
}
disable() {
// destroy timeout object
if (this._timeoutId) {
GLib.Source.remove(this._timeoutId);
this._timeoutId = 0;
}
// destroy indicator instance
this._indicator?.destroy();
this._indicator = null;
}
_schedule() {
// log that the auto scheduler is started
console.log("[BREW UPDATES]: Auto update scheduler started");
// check if the timout object is set, if yes we destroy it
if (this._timeoutId) {
GLib.Source.remove(this._timeoutId);
this._timeoutId = 0;
}
// start a timeout and save it locally
this._timeoutId = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
INTERVAL_MINUTES * this._interval,
() => {
// run refresh
this._refresh();
// notify the timeout can continue
return GLib.SOURCE_CONTINUE;
}
);
}
async _refresh() {
// log that the auto update has started
console.log("[BREW UPDATES]: Auto Update started");
// set loading state
this._indicator.setLoadingState();
// run bash programm to determine the update count and set the update count
try {
const { stdout, status } = await this._spawnCapture(['bash', '-lc', 'brew outdated']);
// check if status is not zero, therefore the result is no update
if(status !== 0) {
// set idle state
this._indicator.setIdleState();
// return early
return;
}
// extract the number of packages for update
const lines = (stdout || '')
.split('\n')
.map(l => l.trim())
.filter(l => l.length > 0);
// send the number of upates to the indicator
this._indicator.setState(lines.length);
} catch (e) {
console.error(e);
// set idle state
this._indicator.setIdleState();
}
}
_spawnCapture(argv) {
// Return promise with a subprocess
return new Promise((resolve, reject) => {
try {
// create the subprocess via GIO lib
const proc = Gio.Subprocess.new(argv,Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE);
// try to run the given command
proc.communicate_utf8_async(null, null, (p, res) => {
try {
// waiting for response
const [, stdout, stderr] = p.communicate_utf8_finish(res);
// go back to the code, where the promise is waiting for the return
resolve({ stdout, stderr, status: p.get_exit_status() });
} catch (err) {
reject(err);
}
});
} catch (err) {
reject(err);
}
});
}
}