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
|
// This file is part of the AppIndicator/KStatusNotifierItem GNOME Shell extension
//
// This program 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 2
// of the License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
const Gio = imports.gi.Gio
const GLib = imports.gi.GLib
const Gtk = imports.gi.Gtk
const Mainloop = imports.mainloop
const ShellConfig = imports.misc.config
const Signals = imports.signals
const Extension = imports.misc.extensionUtils.getCurrentExtension()
const AppIndicator = Extension.imports.appIndicator
const IndicatorStatusIcon = Extension.imports.indicatorStatusIcon
const Interfaces = Extension.imports.interfaces
const Util = Extension.imports.util
// TODO: replace with org.freedesktop and /org/freedesktop when approved
const KDE_PREFIX = 'org.kde';
const WATCHER_BUS_NAME = KDE_PREFIX + '.StatusNotifierWatcher';
const WATCHER_INTERFACE = WATCHER_BUS_NAME;
const WATCHER_OBJECT = '/StatusNotifierWatcher';
const DEFAULT_ITEM_OBJECT_PATH = '/StatusNotifierItem';
const BUS_ADDRESS_REGEX = /([a-zA-Z0-9._-]+\.[a-zA-Z0-9.-]+)|(:[0-9]+\.[0-9]+)$/
/*
* The StatusNotifierWatcher class implements the StatusNotifierWatcher dbus object
*/
var StatusNotifierWatcher = class AppIndicators_StatusNotifierWatcher {
constructor(watchDog) {
this._watchDog = watchDog;
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(Interfaces.StatusNotifierWatcher, this);
this._dbusImpl.export(Gio.DBus.session, WATCHER_OBJECT);
this._cancellable = new Gio.Cancellable;
this._everAcquiredName = false;
this._ownName = Gio.DBus.session.own_name(WATCHER_BUS_NAME,
Gio.BusNameOwnerFlags.NONE,
this._acquiredName.bind(this),
this._lostName.bind(this));
this._items = new Map();
this._nameWatcher = new Map();
this._serviceWatcher = new Map();
this._seekStatusNotifierItems();
}
_acquiredName() {
this._watchDog.nameAcquired = true;
}
_lostName() {
if (this._everAcquiredName)
Util.Logger.debug('Lost name' + WATCHER_BUS_NAME);
else
Util.Logger.warn('Failed to acquire ' + WATCHER_BUS_NAME);
this._watchDog.nameAcquired = false;
}
// create a unique index for the _items dictionary
_getItemId(bus_name, obj_path) {
return bus_name + obj_path;
}
_registerItem(service, bus_name, obj_path) {
let id = this._getItemId(bus_name, obj_path);
if (this._items.has(id)) {
Util.Logger.warn(`Item ${id} is already registered`);
return;
}
Util.Logger.debug(`Registering StatusNotifierItem ${id}`);
let indicator = new AppIndicator.AppIndicator(bus_name, obj_path);
let visual = new IndicatorStatusIcon.IndicatorStatusIcon(indicator);
indicator.connect('destroy', () => visual.destroy());
this._items.set(id, indicator);
this._dbusImpl.emit_signal('StatusNotifierItemRegistered', GLib.Variant.new('(s)', service));
this._nameWatcher.set(id, Gio.DBus.session.watch_name(bus_name,
Gio.BusNameWatcherFlags.NONE, null,
() => this._itemVanished(id)));
if (service != bus_name && service.match(BUS_ADDRESS_REGEX)) {
this._serviceWatcher.set(id, Gio.DBus.session.watch_name(service,
Gio.BusNameWatcherFlags.NONE, null,
() => this._itemVanished(id)));
}
this._dbusImpl.emit_property_changed('RegisteredStatusNotifierItems', GLib.Variant.new('as', this.RegisteredStatusNotifierItems));
}
_ensureItemRegistered(service, bus_name, obj_path) {
let id = this._getItemId(bus_name, obj_path);
let item = this._items.get(id);
if (item) {
//delete the old one and add the new indicator
Util.Logger.debug(`Attempting to re-register ${id}; resetting instead`);
item.reset();
return;
}
this._registerItem(service, bus_name, obj_path)
}
_seekStatusNotifierItems() {
// Some indicators (*coff*, dropbox, *coff*) do not re-register again
// when the plugin is enabled/disabled, thus we need to manually look
// for the objects in the session bus that implements the
// StatusNotifierItem interface...
Util.traverseBusNames(Gio.DBus.session, this._cancellable, (bus, name, cancellable) => {
Util.introspectBusObject(bus, name, cancellable, (node_info) => {
return Util.dbusNodeImplementsInterfaces(node_info, ['org.kde.StatusNotifierItem']);
}, (name, path) => {
let id = this._getItemId(name, path);
if (!this._items.has(id)) {
Util.Logger.debug(`Using Brute-force mode for StatusNotifierItem ${id}`);
this._registerItem(path, name, path);
}
})
});
}
RegisterStatusNotifierItemAsync(params, invocation) {
// it would be too easy if all application behaved the same
// instead, ayatana patched gnome apps to send a path
// while kde apps send a bus name
let [service] = params;
let bus_name = null, obj_path = null;
if (service.charAt(0) == '/') { // looks like a path
bus_name = invocation.get_sender();
obj_path = service;
} else if (service.match(BUS_ADDRESS_REGEX)) {
bus_name = Util.getUniqueBusNameSync(invocation.get_connection(), service);
obj_path = DEFAULT_ITEM_OBJECT_PATH;
}
if (!bus_name || !obj_path) {
let error = "Impossible to register an indicator for parameters '"+
service.toString()+"'";
Util.Logger.warn(error);
invocation.return_dbus_error('org.gnome.gjs.JSError.ValueError',
error);
return;
}
this._ensureItemRegistered(service, bus_name, obj_path);
invocation.return_value(null);
}
_itemVanished(id) {
// FIXME: this is useless if the path name disappears while the bus stays alive (not unheard of)
if (this._items.has(id)) {
this._remove(id);
}
}
_remove(id) {
this._items.get(id).destroy();
this._items.delete(id);
Gio.DBus.session.unwatch_name(this._nameWatcher.get(id));
this._nameWatcher.delete(id);
if (this._serviceWatcher.has(id)) {
Gio.DBus.session.unwatch_name(this._serviceWatcher.get(id));
this._serviceWatcher.delete(id);
}
this._dbusImpl.emit_signal('StatusNotifierItemUnregistered', GLib.Variant.new('(s)', id));
this._dbusImpl.emit_property_changed('RegisteredStatusNotifierItems', GLib.Variant.new('as', this.RegisteredStatusNotifierItems));
}
RegisterStatusNotifierHostAsync(_service, invocation) {
invocation.return_error_literal(
Gio.DBusError,
Gio.DBusError.NOT_SUPPORTED,
'Registering additional notification hosts is not supported');
}
IsNotificationHostRegistered() {
return true;
}
get RegisteredStatusNotifierItems() {
return Array.from(this._items.keys());
}
get IsStatusNotifierHostRegistered() {
return true;
}
get ProtocolVersion() {
return 0;
}
destroy() {
if (!this._isDestroyed) {
// this doesn't do any sync operation and doesn't allow us to hook up the event of being finished
// which results in our unholy debounce hack (see extension.js)
Gio.DBus.session.unown_name(this._ownName);
this._cancellable.cancel();
this._dbusImpl.unexport();
this._nameWatcher.forEach(n => Gio.DBus.session.unwatch_name(n));
delete this._nameWatcher;
this._serviceWatcher.forEach(s => Gio.DBus.session.unwatch_name(s));
delete this._serviceWatcher;
this._items.forEach(i => i.destroy());
delete this._items;
this._isDestroyed = true;
}
}
};
|