2019-05-16 15:08:06 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
/* exported NotificationDaemon */
|
|
|
|
|
|
|
|
const { Gio, GLib } = imports.gi;
|
|
|
|
|
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
const { ServiceImplementation } = imports.dbusService;
|
|
|
|
|
|
|
|
const NotificationsIface = loadInterfaceXML('org.freedesktop.Notifications');
|
|
|
|
const NotificationsProxy = Gio.DBusProxy.makeProxyWrapper(NotificationsIface);
|
|
|
|
|
2020-05-15 09:28:17 -04:00
|
|
|
Gio._promisify(Gio.DBusConnection.prototype, 'call', 'call_finish');
|
|
|
|
|
2019-05-16 15:08:06 -04:00
|
|
|
var NotificationDaemon = class extends ServiceImplementation {
|
|
|
|
constructor() {
|
|
|
|
super(NotificationsIface, '/org/freedesktop/Notifications');
|
|
|
|
|
|
|
|
this._autoShutdown = false;
|
|
|
|
|
2022-02-02 19:32:55 -05:00
|
|
|
this._activeNotifications = new Map();
|
|
|
|
|
2019-05-16 15:08:06 -04:00
|
|
|
this._proxy = new NotificationsProxy(Gio.DBus.session,
|
|
|
|
'org.gnome.Shell',
|
|
|
|
'/org/freedesktop/Notifications',
|
|
|
|
(proxy, error) => {
|
|
|
|
if (error)
|
|
|
|
log(error.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
this._proxy.connectSignal('ActionInvoked',
|
|
|
|
(proxy, sender, params) => {
|
2022-02-02 19:32:55 -05:00
|
|
|
const [id] = params;
|
|
|
|
this._emitSignal(
|
|
|
|
this._activeNotifications.get(id),
|
|
|
|
'ActionInvoked',
|
2019-05-16 15:08:06 -04:00
|
|
|
new GLib.Variant('(us)', params));
|
|
|
|
});
|
|
|
|
this._proxy.connectSignal('NotificationClosed',
|
|
|
|
(proxy, sender, params) => {
|
2022-02-02 19:32:55 -05:00
|
|
|
const [id] = params;
|
|
|
|
this._emitSignal(
|
|
|
|
this._activeNotifications.get(id),
|
|
|
|
'NotificationClosed',
|
2019-05-16 15:08:06 -04:00
|
|
|
new GLib.Variant('(uu)', params));
|
2022-02-02 19:32:55 -05:00
|
|
|
this._activeNotifications.delete(id);
|
2019-05-16 15:08:06 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-02 19:32:55 -05:00
|
|
|
_emitSignal(sender, signalName, params) {
|
|
|
|
if (!sender)
|
|
|
|
return;
|
|
|
|
this._dbusImpl.get_connection()?.emit_signal(
|
|
|
|
sender,
|
|
|
|
this._dbusImpl.get_object_path(),
|
|
|
|
'org.freedesktop.Notifications',
|
|
|
|
signalName,
|
|
|
|
params);
|
|
|
|
}
|
|
|
|
|
|
|
|
_untrackSender(sender) {
|
|
|
|
super._untrackSender(sender);
|
|
|
|
|
|
|
|
this._activeNotifications.forEach((value, key) => {
|
|
|
|
if (value === sender)
|
|
|
|
this._activeNotifications.delete(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-16 15:08:06 -04:00
|
|
|
register() {
|
|
|
|
Gio.DBus.session.own_name(
|
|
|
|
'org.freedesktop.Notifications',
|
|
|
|
Gio.BusNameOwnerFlags.REPLACE,
|
|
|
|
null, null);
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:28:17 -04:00
|
|
|
async NotifyAsync(params, invocation) {
|
2022-02-02 19:32:55 -05:00
|
|
|
const sender = invocation.get_sender();
|
|
|
|
const pid = await this._getSenderPid(sender);
|
2020-05-15 09:28:17 -04:00
|
|
|
const hints = params[6];
|
|
|
|
|
|
|
|
params[6] = {
|
|
|
|
...hints,
|
|
|
|
'sender-pid': new GLib.Variant('u', pid),
|
|
|
|
};
|
|
|
|
|
2019-05-16 15:08:06 -04:00
|
|
|
this._proxy.NotifyRemote(...params, (res, error) => {
|
|
|
|
if (this._handleError(invocation, error))
|
|
|
|
return;
|
|
|
|
|
2022-02-02 19:32:55 -05:00
|
|
|
const [id] = res;
|
|
|
|
this._activeNotifications.set(id, sender);
|
2019-05-16 15:08:06 -04:00
|
|
|
invocation.return_value(new GLib.Variant('(u)', res));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseNotificationAsync(params, invocation) {
|
|
|
|
this._proxy.CloseNotificationRemote(...params, (res, error) => {
|
|
|
|
if (this._handleError(invocation, error))
|
|
|
|
return;
|
|
|
|
|
|
|
|
invocation.return_value(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
GetCapabilitiesAsync(params, invocation) {
|
|
|
|
this._proxy.GetCapabilitiesRemote(...params, (res, error) => {
|
|
|
|
if (this._handleError(invocation, error))
|
|
|
|
return;
|
|
|
|
|
|
|
|
invocation.return_value(new GLib.Variant('(as)', res));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
GetServerInformationAsync(params, invocation) {
|
|
|
|
this._proxy.GetServerInformationRemote(...params, (res, error) => {
|
|
|
|
if (this._handleError(invocation, error))
|
|
|
|
return;
|
|
|
|
|
|
|
|
invocation.return_value(new GLib.Variant('(ssss)', res));
|
|
|
|
});
|
|
|
|
}
|
2020-05-15 09:28:17 -04:00
|
|
|
|
|
|
|
async _getSenderPid(sender) {
|
|
|
|
const res = await Gio.DBus.session.call(
|
|
|
|
'org.freedesktop.DBus',
|
|
|
|
'/',
|
|
|
|
'org.freedesktop.DBus',
|
|
|
|
'GetConnectionUnixProcessID',
|
|
|
|
new GLib.Variant('(s)', [sender]),
|
|
|
|
new GLib.VariantType('(u)'),
|
|
|
|
Gio.DBusCallFlags.NONE,
|
|
|
|
-1,
|
|
|
|
null);
|
|
|
|
const [pid] = res.deepUnpack();
|
|
|
|
return pid;
|
|
|
|
}
|
2019-05-16 15:08:06 -04:00
|
|
|
};
|