2019-05-16 19:08:06 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2023-05-25 18:31:22 +00:00
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GLib from 'gi://GLib';
|
2019-05-16 19:08:06 +00:00
|
|
|
|
2023-05-25 18:31:22 +00:00
|
|
|
import {ServiceImplementation} from './dbusService.js';
|
2019-05-16 19:08:06 +00:00
|
|
|
|
2023-08-07 15:13:48 +00:00
|
|
|
import {loadInterfaceXML} from './misc/dbusUtils.js';
|
2019-05-16 19:08:06 +00:00
|
|
|
|
|
|
|
const NotificationsIface = loadInterfaceXML('org.freedesktop.Notifications');
|
|
|
|
const NotificationsProxy = Gio.DBusProxy.makeProxyWrapper(NotificationsIface);
|
|
|
|
|
2022-02-10 23:09:54 +00:00
|
|
|
Gio._promisify(Gio.DBusConnection.prototype, 'call');
|
2020-05-15 13:28:17 +00:00
|
|
|
|
2023-05-25 18:31:22 +00:00
|
|
|
export const NotificationDaemon = class extends ServiceImplementation {
|
2019-05-16 19:08:06 +00:00
|
|
|
constructor() {
|
|
|
|
super(NotificationsIface, '/org/freedesktop/Notifications');
|
|
|
|
|
|
|
|
this._autoShutdown = false;
|
|
|
|
|
2022-02-03 00:32:55 +00:00
|
|
|
this._activeNotifications = new Map();
|
|
|
|
|
2019-05-16 19:08:06 +00: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-03 00:32:55 +00:00
|
|
|
const [id] = params;
|
|
|
|
this._emitSignal(
|
|
|
|
this._activeNotifications.get(id),
|
|
|
|
'ActionInvoked',
|
2019-05-16 19:08:06 +00:00
|
|
|
new GLib.Variant('(us)', params));
|
|
|
|
});
|
|
|
|
this._proxy.connectSignal('NotificationClosed',
|
|
|
|
(proxy, sender, params) => {
|
2022-02-03 00:32:55 +00:00
|
|
|
const [id] = params;
|
|
|
|
this._emitSignal(
|
|
|
|
this._activeNotifications.get(id),
|
|
|
|
'NotificationClosed',
|
2019-05-16 19:08:06 +00:00
|
|
|
new GLib.Variant('(uu)', params));
|
2022-02-03 00:32:55 +00:00
|
|
|
this._activeNotifications.delete(id);
|
2019-05-16 19:08:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-03 00:32:55 +00: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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-03 01:34:37 +00:00
|
|
|
_checkNotificationId(invocation, id) {
|
|
|
|
if (id === 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!this._activeNotifications.has(id))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (this._activeNotifications.get(id) === invocation.get_sender())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const error = new GLib.Error(Gio.DBusError,
|
|
|
|
Gio.DBusError.INVALID_ARGS, 'Invalid notification ID');
|
|
|
|
this._handleError(invocation, error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-16 19:08:06 +00:00
|
|
|
register() {
|
|
|
|
Gio.DBus.session.own_name(
|
|
|
|
'org.freedesktop.Notifications',
|
|
|
|
Gio.BusNameOwnerFlags.REPLACE,
|
|
|
|
null, null);
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:28:17 +00:00
|
|
|
async NotifyAsync(params, invocation) {
|
2022-02-03 00:32:55 +00:00
|
|
|
const sender = invocation.get_sender();
|
|
|
|
const pid = await this._getSenderPid(sender);
|
2022-02-03 01:34:37 +00:00
|
|
|
const replaceId = params[1];
|
2020-05-15 13:28:17 +00:00
|
|
|
const hints = params[6];
|
|
|
|
|
2022-02-03 01:34:37 +00:00
|
|
|
if (!this._checkNotificationId(invocation, replaceId))
|
|
|
|
return;
|
|
|
|
|
2020-05-15 13:28:17 +00:00
|
|
|
params[6] = {
|
|
|
|
...hints,
|
|
|
|
'sender-pid': new GLib.Variant('u', pid),
|
|
|
|
};
|
|
|
|
|
2022-06-23 12:53:29 +00:00
|
|
|
try {
|
|
|
|
const [id] = await this._proxy.NotifyAsync(...params);
|
2022-02-03 00:32:55 +00:00
|
|
|
this._activeNotifications.set(id, sender);
|
2022-06-23 12:53:29 +00:00
|
|
|
invocation.return_value(new GLib.Variant('(u)', [id]));
|
|
|
|
} catch (error) {
|
|
|
|
this._handleError(invocation, error);
|
|
|
|
}
|
2019-05-16 19:08:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 12:53:29 +00:00
|
|
|
async CloseNotificationAsync(params, invocation) {
|
2022-02-03 01:34:37 +00:00
|
|
|
const [id] = params;
|
|
|
|
if (!this._checkNotificationId(invocation, id))
|
|
|
|
return;
|
|
|
|
|
2022-06-23 12:53:29 +00:00
|
|
|
try {
|
|
|
|
await this._proxy.CloseNotificationAsync(...params);
|
2019-05-16 19:08:06 +00:00
|
|
|
invocation.return_value(null);
|
2022-06-23 12:53:29 +00:00
|
|
|
} catch (error) {
|
|
|
|
this._handleError(invocation, error);
|
|
|
|
}
|
2019-05-16 19:08:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 12:53:29 +00:00
|
|
|
async GetCapabilitiesAsync(params, invocation) {
|
|
|
|
try {
|
|
|
|
const res = await this._proxy.GetCapabilitiesAsync(...params);
|
2019-05-16 19:08:06 +00:00
|
|
|
invocation.return_value(new GLib.Variant('(as)', res));
|
2022-06-23 12:53:29 +00:00
|
|
|
} catch (error) {
|
|
|
|
this._handleError(invocation, error);
|
|
|
|
}
|
2019-05-16 19:08:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 12:53:29 +00:00
|
|
|
async GetServerInformationAsync(params, invocation) {
|
|
|
|
try {
|
|
|
|
const res = await this._proxy.GetServerInformationAsync(...params);
|
2019-05-16 19:08:06 +00:00
|
|
|
invocation.return_value(new GLib.Variant('(ssss)', res));
|
2022-06-23 12:53:29 +00:00
|
|
|
} catch (error) {
|
|
|
|
this._handleError(invocation, error);
|
|
|
|
}
|
2019-05-16 19:08:06 +00:00
|
|
|
}
|
2020-05-15 13:28:17 +00: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 19:08:06 +00:00
|
|
|
};
|