ukui-notification/notification-server/server.cpp

156 lines
5.4 KiB
C++
Raw Normal View History

2023-01-31 17:39:14 +08:00
//
// Created by zpf on 23-1-31.
//
#include <QDBusServiceWatcher>
#include <QDBusConnection>
#include "server.h"
#include "server-private.h"
#include "notificationsadaptor.h"
#include "notificationserveradaptor.h"
#include "notification-server-config.h"
#include "utils.h"
2023-01-31 17:39:14 +08:00
2023-02-03 18:01:06 +08:00
using namespace NotificationServer;
2023-01-31 17:39:14 +08:00
ServerPrivate::ServerPrivate(QObject *parent) : QObject(parent), m_notificationWatchers(new QDBusServiceWatcher(this))
{
m_notificationWatchers->setConnection(QDBusConnection::sessionBus());
m_notificationWatchers->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
2023-02-03 18:01:06 +08:00
connect(m_notificationWatchers, &QDBusServiceWatcher::serviceUnregistered,
m_notificationWatchers, &QDBusServiceWatcher::removeWatchedService);
2023-01-31 17:39:14 +08:00
}
ServerPrivate::~ServerPrivate() = default;
QStringList ServerPrivate::GetCapabilities() const
{
return QStringList();
}
uint ServerPrivate::Notify(const QString &app_name, uint replaces_id, const QString &app_icon, const QString &summary,
const QString &body, const QStringList &actions, const QVariantMap &hints, int timeout)
{
2023-02-03 18:01:06 +08:00
uint id = 0;
if(replaces_id > 0) {
id = replaces_id;
} else {
id = m_increasedNotificationId++;
}
uint pid = 0;
QDBusReply<uint> pidReply = connection().interface()->servicePid(message().service());
if(pidReply.isValid()) {
pid = pidReply.value();
}
QVariantMap newHints = hints;
if(newHints.value(QStringLiteral("x-ukui-display")).toString().isEmpty() && pid > 0) {
newHints.insert(QStringLiteral("x-ukui-display"), UkuiNotification::Utils::displayFromPid(pid));
}
if(newHints.value(QStringLiteral("desktop-entry")).toString().isEmpty() && pid > 0) {
newHints.insert(QStringLiteral("desktop-entry"), UkuiNotification::Utils::desktopEntryFromPid(pid));
}
2023-02-03 18:01:06 +08:00
qDebug() << "New message received:" << app_name << id << app_icon << summary << body << actions << hints << timeout;
2023-02-03 18:01:06 +08:00
for(const QString &service : m_notificationWatchers->watchedServices()) {
QDBusMessage msg = QDBusMessage::createMethodCall(service,
QStringLiteral("/NotificationClient"),
QStringLiteral("org.ukui.NotificationClient"),
QStringLiteral("Notify"));
msg.setArguments({app_name, id, app_icon, summary, body, actions, newHints, timeout});
2023-02-03 18:01:06 +08:00
QDBusConnection::sessionBus().call(msg, QDBus::NoBlock);
}
return id;
2023-01-31 17:39:14 +08:00
}
2023-02-06 17:03:12 +08:00
QString ServerPrivate::GetServerInformation(QString &vendor, QString &version, QString &spec_version) const
2023-01-31 17:39:14 +08:00
{
vendor = QStringLiteral("Kylin");
version = QLatin1String(NOTIFICATION_SERVER_VERSION);
spec_version = QStringLiteral("1.2");
return QStringLiteral("UKUI");
2023-01-31 17:39:14 +08:00
}
void ServerPrivate::CloseNotification(uint id)
{
2023-02-03 18:01:06 +08:00
for(const QString &service : m_notificationWatchers->watchedServices()) {
QDBusMessage msg = QDBusMessage::createMethodCall(service,
QStringLiteral("/NotificationClient"),
QStringLiteral("org.ukui.NotificationClient"),
QStringLiteral("CloseNotification"));
msg.setArguments({id, CloseReason::Revoked});
QDBusConnection::sessionBus().call(msg, QDBus::NoBlock);
}
2023-01-31 17:39:14 +08:00
}
bool ServerPrivate::init()
{
new NotificationsAdaptor(this);
new NotificationServerAdaptor(this);
2023-02-01 18:08:11 +08:00
QDBusConnection conn = QDBusConnection::sessionBus();
auto registration = conn.interface()->registerService(notificationServiceName(),
QDBusConnectionInterface::ReplaceExistingService,
QDBusConnectionInterface::DontAllowReplacement);
2023-01-31 17:39:14 +08:00
if (registration.value() != QDBusConnectionInterface::ServiceRegistered) {
qWarning() << "Failed to register Notification service on DBus!";
return false;
}
2023-02-01 18:08:11 +08:00
if(!conn.registerObject(notificationServicePath(), this)) {
qWarning() << "Failed to register Notification DBus object!" << conn.lastError().message();
2023-02-01 18:08:11 +08:00
return false;
}
2023-01-31 17:39:14 +08:00
return true;
}
QString ServerPrivate::notificationServiceName()
{
return QStringLiteral("org.freedesktop.Notifications");
}
QString ServerPrivate::notificationServicePath()
{
return QStringLiteral("/org/freedesktop/Notifications");
}
QString ServerPrivate::notificationServiceInterface()
{
return QStringLiteral("org.freedesktop.Notifications");;
}
void ServerPrivate::RegisterClient()
{
// qDebug() << "Client Register:" << message().service();
2023-01-31 17:39:14 +08:00
m_notificationWatchers->addWatchedService(message().service());
qDebug() << "Watched services:" << m_notificationWatchers->watchedServices();
2023-01-31 17:39:14 +08:00
}
void ServerPrivate::UnRegisterClient()
{
m_notificationWatchers->removeWatchedService(message().service());
}
2023-02-06 17:03:12 +08:00
void ServerPrivate::CloseNotification(uint id, uint reason)
2023-02-03 18:01:06 +08:00
{
2023-02-06 17:03:12 +08:00
Q_EMIT NotificationClosed(id, reason);
}
2023-02-14 17:21:09 +08:00
void ServerPrivate::InvokedAction(uint id, const QString &action_key)
2023-02-06 17:03:12 +08:00
{
Q_EMIT ActionInvoked(id, action_key);
2023-02-03 18:01:06 +08:00
}
2023-01-31 17:39:14 +08:00
Server::Server(QObject *parent): QObject(parent), d(new ServerPrivate(this))
{
}
Server &Server::self()
{
static Server self;
return self;
}
bool Server::init()
{
return d->init();
}
2023-02-03 18:01:06 +08:00
Server::~Server() = default;