This repository has been archived by the owner on Apr 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.js
463 lines (413 loc) · 16 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
/*
This file is part of Arch Linux Updates Indicator
Arch Linux Updates Indicator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Arch Linux Updates Indicator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Arch Linux Updates Indicator. If not, see <http://www.gnu.org/licenses/>.
Copyright 2016 Raphaël Rochet
*/
const Clutter = imports.gi.Clutter;
const Lang = imports.lang;
const St = imports.gi.St;
const GObject = imports.gi.GObject;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const Gio = imports.gi.Gio;
const Main = imports.ui.main;
const Panel = imports.ui.panel;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const MessageTray = imports.ui.messageTray;
const Util = imports.misc.util;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const Format = imports.format;
const Gettext = imports.gettext.domain('arch-update');
const _ = Gettext.gettext;
/* Options */
let ALWAYS_VISIBLE = true;
let SHOW_COUNT = true;
let BOOT_WAIT = 30; // 30s
let CHECK_INTERVAL = 360*360; // 1h
let NOTIFY = false;
let HOWMUCH = 0;
let TRANSIENT = true;
let UPDATE_CMD = "update-command";
let CHECK_CMD = "/usr/bin/update-check";
let MANAGER_CMD = "pamac-manager";
let PACMAN_DIR = "/var/lib/pacman/local";
let STRIP_VERSIONS = true;
let AUTO_EXPAND_LIST = 0;
/* Variables we want to keep when extension is disabled (eg during screen lock) */
let FIRST_BOOT = 1;
let UPDATES_PENDING = -1;
let UPDATES_LIST = [];
function init() {
String.prototype.format = Format.format;
Utils.initTranslations("arch-update");
}
const ArchUpdateIndicator = new Lang.Class({
Name: 'ArchUpdateIndicator',
Extends: PanelMenu.Button,
_TimeoutId: null,
_FirstTimeoutId: null,
_updateProcess_sourceId: null,
_updateProcess_stream: null,
_updateProcess_pid: null,
_updateList: [],
_init: function() {
this.parent(0.0, "ArchUpdateIndicator");
Gtk.IconTheme.get_default().append_search_path(Me.dir.get_child('icons').get_path());
this.updateIcon = new St.Icon({icon_name: "arch-unknown-symbolic", style_class: 'system-status-icon'});
let box = new St.BoxLayout({ vertical: false, style_class: 'panel-status-menu-box' });
this.label = new St.Label({ text: '',
y_expand: true,
y_align: Clutter.ActorAlign.CENTER });
box.add_child(this.updateIcon);
box.add_child(this.label);
this.actor.add_child(box);
// Prepare the special menu : a submenu for updates list that will look like a regular menu item when disabled
// Scrollability will also be taken care of by the popupmenu
this.menuExpander = new PopupMenu.PopupSubMenuMenuItem('');
this.updatesListMenuLabel = new St.Label();
this.menuExpander.menu.box.add(this.updatesListMenuLabel);
this.menuExpander.menu.box.style_class = 'arch-updates-list';
// Other standard menu items
let settingsMenuItem = new PopupMenu.PopupMenuItem(_('Settings'));
this.updateNowMenuItem = new PopupMenu.PopupMenuItem(_('Update now'));
this.managerMenuItem = new PopupMenu.PopupMenuItem(_('Open packager manager'));
// A special "Checking" menu item with a stop button
this.checkingMenuItem = new PopupMenu.PopupBaseMenuItem( {reactive:false} );
let checkingLabel = new St.Label({ text: _('Checking') + " …" });
let cancelButton = new St.Button({
child: new St.Icon({ icon_name: 'process-stop-symbolic' }),
style_class: 'system-menu-action arch-updates-menubutton',
x_expand: true
});
cancelButton.set_x_align(Clutter.ActorAlign.END);
this.checkingMenuItem.actor.add_actor( checkingLabel );
this.checkingMenuItem.actor.add_actor( cancelButton );
// A little trick on "check now" menuitem to keep menu opened
this.checkNowMenuItem = new PopupMenu.PopupMenuItem( _('Check now') );
this.checkNowMenuContainer = new PopupMenu.PopupMenuSection();
this.checkNowMenuContainer.actor.add_actor(this.checkNowMenuItem.actor);
// Assemble all menu items into the popup menu
this.menu.addMenuItem(this.menuExpander);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this.menu.addMenuItem(this.updateNowMenuItem);
this.menu.addMenuItem(this.checkingMenuItem);
this.menu.addMenuItem(this.checkNowMenuContainer);
this.menu.addMenuItem(this.managerMenuItem);
this.menu.addMenuItem(settingsMenuItem);
// Bind some events
this.menu.connect('open-state-changed', Lang.bind(this, this._onMenuOpened));
this.checkNowMenuItem.connect('activate', Lang.bind(this, this._checkUpdates));
cancelButton.connect('clicked', Lang.bind(this, this._cancelCheck));
settingsMenuItem.connect('activate', Lang.bind(this, this._openSettings));
this.updateNowMenuItem.connect('activate', Lang.bind(this, this._updateNow));
this.managerMenuItem.connect('activate', Lang.bind(this, this._openManager));
// Load settings
this._settings = Utils.getSettings();
this._settingsChangedId = this._settings.connect('changed', Lang.bind(this, this._applySettings));
this._applySettings();
this._showChecking(false);
this._updateMenuExpander(false, _('Waiting first check'));
if (FIRST_BOOT) {
// Schedule first check only if this is the first extension load
// This won't be run again if extension is disabled/enabled (like when screen is locked)
let that = this;
this._FirstTimeoutId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, BOOT_WAIT, function () {
that._checkUpdates();
that._FirstTimeoutId = null;
FIRST_BOOT = 0;
that._startFolderMonitor();
return false; // Run once
});
} else {
// Restore previous state
this._updateList = UPDATES_LIST;
this._updateStatus(UPDATES_PENDING);
this._startFolderMonitor();
}
},
_openSettings: function () {
Util.spawn([ "gnome-shell-extension-prefs", Me.uuid ]);
},
_openManager: function () {
Util.spawnCommandLine(MANAGER_CMD);
},
_updateNow: function () {
Util.spawnCommandLine(UPDATE_CMD);
},
_applySettings: function() {
ALWAYS_VISIBLE = this._settings.get_boolean('always-visible');
SHOW_COUNT = this._settings.get_boolean('show-count');
BOOT_WAIT = this._settings.get_int('boot-wait');
CHECK_INTERVAL = 60 * this._settings.get_int('check-interval');
NOTIFY = this._settings.get_boolean('notify');
HOWMUCH = this._settings.get_int('howmuch');
TRANSIENT = this._settings.get_boolean('transient');
UPDATE_CMD = this._settings.get_string('update-cmd');
CHECK_CMD = this._settings.get_string('check-cmd');
MANAGER_CMD = this._settings.get_string('package-manager');
PACMAN_DIR = this._settings.get_string('pacman-dir');
STRIP_VERSIONS = this._settings.get_boolean('strip-versions');
AUTO_EXPAND_LIST = this._settings.get_int('auto-expand-list');
this.managerMenuItem.actor.visible = ( MANAGER_CMD != "" );
this._checkShowHide();
let that = this;
if (this._TimeoutId) GLib.source_remove(this._TimeoutId);
this._TimeoutId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, CHECK_INTERVAL, function () {
that._checkUpdates();
return true;
});
},
destroy: function() {
this._settings.disconnect( this._settingsChangedId );
if (this._notifSource) {
// Delete the notification source, which lay still have a notification shown
this._notifSource.destroy();
this._notifSource = null;
};
if (this.monitor) {
// Stop spying on pacman local dir
this.monitor.cancel();
this.monitor = null;
}
if (this._updateProcess_sourceId) {
// We leave the checkupdate process end by itself but undef handles to avoid zombies
GLib.source_remove(this._updateProcess_sourceId);
this._updateProcess_sourceId = null;
this._updateProcess_stream = null;
}
if (this._FirstTimeoutId) {
GLib.source_remove(this._FirstTimeoutId);
this._FirstTimeoutId = null;
}
if (this._TimeoutId) {
GLib.source_remove(this._TimeoutId);
this._TimeoutId = null;
}
this.parent();
},
_checkShowHide: function() {
if ( UPDATES_PENDING == -3 ) {
// Do not apply visibility change while checking for updates
return;
}
if (!ALWAYS_VISIBLE && UPDATES_PENDING < 1) {
this.actor.visible = false;
} else {
this.actor.visible = true;
}
this.label.visible = SHOW_COUNT && UPDATES_PENDING > 0;
},
_onMenuOpened: function() {
// This event is fired when menu is shown or hidden
// Only open the submenu if the menu is being opened and there is something to show
this._checkAutoExpandList();
},
_checkAutoExpandList: function() {
if (this.menu.isOpen && UPDATES_PENDING > 0 && UPDATES_PENDING <= AUTO_EXPAND_LIST) {
this.menuExpander.setSubmenuShown(true);
} else {
this.menuExpander.setSubmenuShown(false);
}
},
_startFolderMonitor: function() {
if (PACMAN_DIR) {
this.pacman_dir = Gio.file_new_for_path(PACMAN_DIR);
this.monitor = this.pacman_dir.monitor_directory(0, null);
this.monitor.connect('changed', Lang.bind(this, this._onFolderChanged));
}
},
_onFolderChanged: function() {
// Folder have changed ! Let's schedule a check in a few seconds
let that = this;
if (this._FirstTimeoutId) GLib.source_remove(this._FirstTimeoutId);
this._FirstTimeoutId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 5, function () {
that._checkUpdates();
that._FirstTimeoutId = null;
return false;
});
},
_showChecking: function(isChecking) {
if (isChecking == true) {
this.updateIcon.set_icon_name('arch-unknown-symbolic');
this.checkNowMenuContainer.actor.visible = false;
this.checkingMenuItem.actor.visible = true;;
} else {
this.checkNowMenuContainer.actor.visible = true;;
this.checkingMenuItem.actor.visible = false;;
}
},
_updateStatus: function(updatesCount) {
updatesCount = typeof updatesCount === 'number' ? updatesCount : UPDATES_PENDING;
if (updatesCount > 0) {
// Updates pending
this.updateIcon.set_icon_name('arch-updates-symbolic');
this._updateMenuExpander( true, Gettext.ngettext( "%d update pending", "%d updates pending", updatesCount ).format(updatesCount) );
this.updatesListMenuLabel.set_text( this._updateList.join("\n") );
this.label.set_text(updatesCount.toString());
if (NOTIFY && UPDATES_PENDING < updatesCount) {
if (HOWMUCH > 0) {
let updateList = [];
if (HOWMUCH > 1) {
updateList = this._updateList;
} else {
// Keep only packets that was not in the previous notification
updateList = this._updateList.filter(function(pkg) { return UPDATES_LIST.indexOf(pkg) < 0 });
}
if (updateList.length > 0) {
// Show notification only if there's new updates
this._showNotification(
Gettext.ngettext( "New Arch Linux Update", "New Arch Linux Updates", updateList.length ),
updateList.join(', ')
);
}
} else {
this._showNotification(
Gettext.ngettext( "New Arch Linux Update", "New Arch Linux Updates", updatesCount ),
Gettext.ngettext( "There is %d update pending", "There are %d updates pending", updatesCount ).format(updatesCount)
);
}
}
// Store the new list
UPDATES_LIST = this._updateList;
} else {
this.updatesListMenuLabel.set_text("");
this.label.set_text('');
if (updatesCount == -1) {
// Unknown
this.updateIcon.set_icon_name('arch-unknown-symbolic');
this._updateMenuExpander( false, '' );
} else if (updatesCount == -2) {
// Error
this.updateIcon.set_icon_name('arch-error-symbolic');
this._updateMenuExpander( false, _('Error') );
} else {
// Up to date
this.updateIcon.set_icon_name('arch-uptodate-symbolic');
this._updateMenuExpander( false, _('Up to date :)') );
UPDATES_LIST = []; // Reset stored list
}
}
UPDATES_PENDING = updatesCount;
this._checkAutoExpandList();
this._checkShowHide();
},
_updateMenuExpander: function(enabled, label) {
if (label == "") {
// No text, hide the menuitem
this.menuExpander.actor.visible = false;
} else {
// We make our expander look like a regular menu label if disabled
this.menuExpander.actor.reactive = enabled;
this.menuExpander._triangle.visible = enabled;
this.menuExpander.label.set_text(label);
this.menuExpander.actor.visible = true;
}
// 'Update now' visibility is linked so let's save a few lines and set it here
this.updateNowMenuItem.actor.reactive = enabled;
},
_checkUpdates: function() {
if(this._updateProcess_sourceId) {
// A check is already running ! Maybe we should kill it and run another one ?
return;
}
// Run asynchronously, to avoid shell freeze - even for a 1s check
this._showChecking(true);
try {
// Parse check command line
let [parseok, argvp] = GLib.shell_parse_argv( CHECK_CMD );
if (!parseok) { throw 'Parse error' };
let [res, pid, in_fd, out_fd, err_fd] = GLib.spawn_async_with_pipes(null, argvp, null, GLib.SpawnFlags.DO_NOT_REAP_CHILD, null);
// Let's buffer the command's output - that's a input for us !
this._updateProcess_stream = new Gio.DataInputStream({
base_stream: new Gio.UnixInputStream({fd: out_fd})
});
// We will process the output at once when it's done
this._updateProcess_sourceId = GLib.child_watch_add(0, pid, Lang.bind(this, function() {this._checkUpdatesRead()}));
this._updateProcess_pid = pid;
} catch (err) {
this._showChecking(false);
this.lastUnknowErrorString = err.message.toString();
this._updateStatus(-2);
}
},
_cancelCheck: function() {
if (this._updateProcess_pid == null) { return; };
Util.spawnCommandLine( "kill " + this._updateProcess_pid );
this._updateProcess_pid = null; // Prevent double kill
this._checkUpdatesEnd();
},
_checkUpdatesRead: function() {
// Read the buffered output
let updateList = [];
let out, size;
do {
[out, size] = this._updateProcess_stream.read_line_utf8(null);
if (out) updateList.push(out);
} while (out);
// If version numbers should be stripped, do it
if (STRIP_VERSIONS == true) {
updateList = updateList.map(function(p) {
// Try to keep only what's before the first space
var chunks = p.split(" ",2);
return chunks[0];
});
}
this._updateList = updateList;
this._checkUpdatesEnd();
},
_checkUpdatesEnd: function() {
// Free resources
this._updateProcess_stream.close(null);
this._updateProcess_stream = null;
GLib.source_remove(this._updateProcess_sourceId);
this._updateProcess_sourceId = null;
this._updateProcess_pid = null;
// Update indicator
this._showChecking(false);
this._updateStatus(this._updateList.length);
},
_showNotification: function(title, message) {
if (this._notifSource == null) {
// We have to prepare this only once
this._notifSource = new MessageTray.SystemNotificationSource();
this._notifSource.createIcon = function() {
return new St.Icon({ icon_name: 'arch-lit-symbolic' });
};
// Take care of note leaving unneeded sources
this._notifSource.connect('destroy', Lang.bind(this, function() {this._notifSource = null;}));
Main.messageTray.add(this._notifSource);
}
let notification = null;
// We do not want to have multiple notifications stacked
// instead we will update previous
if (this._notifSource.notifications.length == 0) {
notification = new MessageTray.Notification(this._notifSource, title, message);
notification.addAction( _('Update now') , Lang.bind(this, function() {this._updateNow()}) );
} else {
notification = this._notifSource.notifications[0];
notification.update( title, message, { clear: true });
}
notification.setTransient(TRANSIENT);
this._notifSource.notify(notification);
},
});
let archupdateindicator;
function enable() {
archupdateindicator = new ArchUpdateIndicator();
Main.panel.addToStatusArea('ArchUpdateIndicator', archupdateindicator);
}
function disable() {
archupdateindicator.destroy();
}