增加客户端注册与同步逻辑

This commit is contained in:
iaom 2023-02-03 18:01:06 +08:00
parent d7f0ab0675
commit 572f46019a
14 changed files with 540 additions and 19 deletions

View File

@ -10,9 +10,9 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(GNUInstallDirs) include(GNUInstallDirs)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core LinguistTools DBus Network REQUIRED) find_package(QT NAMES Qt6 Qt5 COMPONENTS Core LinguistTools DBus Network Gui REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core LinguistTools DBus Network REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core LinguistTools DBus Network Gui REQUIRED)
include_directories()
add_subdirectory(notification-server) add_subdirectory(notification-server)
add_subdirectory(libukui-notification) add_subdirectory(libukui-notification)

View File

@ -0,0 +1,16 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.ukui.NotificationClient">
<method name="Notify">
<annotation name="org.qtproject.QtDBus.QtTypeName.In6" value="QVariantMap"/>
<arg name="app_name" type="s" direction="in"/>
<arg name="replaces_id" type="u" direction="in"/>
<arg name="app_icon" type="s" direction="in"/>
<arg name="summary" type="s" direction="in"/>
<arg name="body" type="s" direction="in"/>
<arg name="actions" type="as" direction="in"/>
<arg name="hints" type="a{sv}" direction="in"/>
<arg name="timeout" type="i" direction="in"/>
</method>
</interface>
</node>

View File

@ -3,5 +3,9 @@
<interface name="org.ukui.NotificationServer"> <interface name="org.ukui.NotificationServer">
<method name="RegisterClient"/> <method name="RegisterClient"/>
<method name="UnRegisterClient"/> <method name="UnRegisterClient"/>
<method name="CloseNotification">
<arg name="id" type="u" direction="in"/>
<arg name="reason" type="u" direction="in"/>
</method>
</interface> </interface>
</node> </node>

View File

@ -0,0 +1,20 @@
set(ukui-notification_LIB_SRCS
notification-client.cpp
notification-client.h
popup-notification.h
popup-notification.cpp
ukui-notification_global.h
notification-client-private.h
)
qt_add_dbus_interface(ukui-notification_LIB_SRCS ../dbus/org.freedesktop.Notifications.xml notifications_interface)
qt_add_dbus_adaptor(ukui-notification_LIB_SRCS ../dbus/org.ukui.NotificationClient.xml notification-client-private.h UkuiNotification::NotificationClientPrivate)
add_library(ukui-notification SHARED ${ukui-notification_LIB_SRCS})
target_link_libraries(ukui-notification
PRIVATE
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::DBus
Qt${QT_VERSION_MAJOR}::Gui
)
target_compile_definitions(ukui-notification PRIVATE UKUINOTIFICATION_LIBRARY)

View File

@ -0,0 +1,35 @@
//
// Created by zpf on 23-2-3.
//
#ifndef UKUI_NOTIFICATION_NOTIFICATION_CLIENT_PRIVATE_H
#define UKUI_NOTIFICATION_NOTIFICATION_CLIENT_PRIVATE_H
#include <QObject>
#include <QDBusInterface>
#include "notifications_interface.h"
namespace UkuiNotification {
class NotificationClientPrivate : public QObject
{
Q_OBJECT
public:
explicit NotificationClientPrivate(QObject *parent = nullptr);
~NotificationClientPrivate() override;
bool registerClient();
static QString clientServicePath();
static QString clientServiceInterface();
void 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);
void notificationClosed(uint id, uint reason);
private:
OrgFreedesktopNotificationsInterface* m_notificationsInterface = nullptr;
QDBusInterface *m_serverInterface = nullptr;
};
}
#endif //UKUI_NOTIFICATION_NOTIFICATION_CLIENT_PRIVATE_H

View File

@ -0,0 +1,99 @@
//
// Created by zpf on 23-2-2.
//
#include "notification-client.h"
#include "notification-client-private.h"
#include <QDBusConnection>
#include "notificationclientadaptor.h"
using namespace UkuiNotification;
NotificationClientPrivate::NotificationClientPrivate(QObject *parent) : QObject(parent)
{
}
NotificationClientPrivate::~NotificationClientPrivate()
{
if(m_notificationsInterface) {
m_notificationsInterface->call(QDBus::NoBlock, QStringLiteral("UnRegisterClient"));
}
}
bool NotificationClientPrivate::registerClient()
{
new NotificationClientAdaptor(this);
QDBusConnection conn = QDBusConnection::sessionBus();
if(!conn.registerObject(clientServicePath(), clientServiceInterface(), this)) {
qWarning() << "Failed to register NotificationClient DBus object!" << conn.lastError();
return false;
}
m_notificationsInterface = new OrgFreedesktopNotificationsInterface(QStringLiteral("org.freedesktop.Notifications"),
QStringLiteral("/org/freedesktop/Notifications"),
conn,
this);
if(!m_notificationsInterface->isValid()) {
qWarning() << "Failed to creat dbus interface for notification server!" << conn.lastError();
return false;
}
connect(m_notificationsInterface, &OrgFreedesktopNotificationsInterface::NotificationClosed,
this, &NotificationClientPrivate::notificationClosed);
if(!m_serverInterface) {
m_serverInterface = new QDBusInterface(QStringLiteral("org.freedesktop.Notifications"),
QStringLiteral("/org/ukui/NotificationServer"),
QStringLiteral("org.ukui.NotificationServer"),
conn,
this);
if(!m_serverInterface->isValid()) {
qWarning() << "Failed to create notification server interface! " << conn.lastError();
return false;
}
}
QDBusMessage reply = m_notificationsInterface->call(QDBus::NoBlock, QStringLiteral("RegisterClient"));
if (reply.type() == QDBusMessage::ErrorMessage) {
qWarning() << "Failed to call RegisterClient!" << conn.lastError();
return false;
}
return true;
}
QString NotificationClientPrivate::clientServicePath()
{
return QStringLiteral("/NotificationClient");
}
QString NotificationClientPrivate::clientServiceInterface()
{
return QStringLiteral("org.ukui.NotificationClient");
}
void NotificationClientPrivate::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)
{
}
void NotificationClientPrivate::notificationClosed(uint id, uint reason)
{
if(!m_notificationsInterface) {
return;
}
QDBusMessage reply = m_notificationsInterface->callWithArgumentList(QDBus::NoBlock, QStringLiteral("CloseNotification"), {id, reason});
if (reply.type() == QDBusMessage::ErrorMessage) {
qWarning() << "Failed to call CloseNotification!" << QDBusConnection::sessionBus().lastError();
}
}
NotificationClient::NotificationClient(QObject *parent) : QObject(parent), d(new NotificationClientPrivate(this))
{
}
NotificationClient::~NotificationClient() = default;
bool NotificationClient::registerClient()
{
return d->registerClient();
}

View File

@ -0,0 +1,24 @@
//
// Created by zpf on 23-2-2.
//
#ifndef UKUI_NOTIFICATION_NOTIFICATION_CLIENT_H
#define UKUI_NOTIFICATION_NOTIFICATION_CLIENT_H
#include "ukui-notification_global.h"
#include <QObject>
namespace UkuiNotification {
class NotificationClientPrivate;
class UKUINOTIFICATION_EXPORT NotificationClient : public QObject
{
Q_OBJECT
public:
NotificationClient(QObject *parent);
~NotificationClient() override;
bool registerClient();
private:
NotificationClientPrivate *d = nullptr;
};
} // UkuiNotification
#endif //UKUI_NOTIFICATION_NOTIFICATION_CLIENT_H

View File

@ -0,0 +1,175 @@
//
// Created by zpf on 23-2-2.
//
#include "popup-notification.h"
class PopupNotificationPrvate
{
public:
PopupNotificationPrvate();
~PopupNotificationPrvate();
};
PopupNotificationPrvate::PopupNotificationPrvate()
{
}
PopupNotificationPrvate::~PopupNotificationPrvate()
{
}
PopupNotification::PopupNotification(uint id, QObject *parent) : QObject(parent)
{
}
PopupNotification::PopupNotification(const PopupNotification &other)
{
}
PopupNotification &PopupNotification::operator=( const PopupNotification &other)
{
*d = *other.d;
return *this;
}
PopupNotification &PopupNotification::operator=(PopupNotification &&other) Q_DECL_NOEXCEPT
{
d = other.d;
other.d = nullptr;
return *this;
}
PopupNotification::~PopupNotification()
{
}
QString PopupNotification::applicationName() const
{
return QString();
}
void PopupNotification::setApplicationName(const QString &applicationName)
{
}
QString PopupNotification::icon() const
{
return QString();
}
void PopupNotification::setIcon(const QString &icon)
{
}
QString PopupNotification::summary() const
{
return QString();
}
void PopupNotification::setSummary(const QString &summary)
{
}
QString PopupNotification::body() const
{
return QString();
}
void PopupNotification::setBody(const QString &body)
{
}
bool PopupNotification::hasDefaultAction() const
{
return false;
}
void PopupNotification::setActions(const QStringList &actions)
{
}
QList<QPair<QString, QString>> PopupNotification::getActions()
{
return QList<QPair<QString, QString>>();
}
QVariantMap PopupNotification::hints() const
{
return QVariantMap();
}
void PopupNotification::setHints(const QVariantMap &hints)
{
}
int PopupNotification::timeout() const
{
return 0;
}
void PopupNotification::setTimeout(int timeout)
{
}
QDateTime PopupNotification::created() const
{
return QDateTime();
}
void PopupNotification::setCreated(const QDateTime &created)
{
}
bool PopupNotification::enableActionIcons() const
{
return false;
}
QString PopupNotification::category() const
{
return QString();
}
QString PopupNotification::desktopEntry() const
{
return QString();
}
QImage PopupNotification::image() const
{
return QImage();
}
bool PopupNotification::resident() const
{
return false;
}
QString PopupNotification::soundFile() const
{
return QString();
}
bool PopupNotification::transient() const
{
return false;
}
void PopupNotification::setUrgency(PopupNotification::Urgency urgency)
{
}

View File

@ -0,0 +1,80 @@
//
// Created by zpf on 23-2-2.
//
#ifndef UKUI_NOTIFICATION_POPUP_NOTIFICATION_H
#define UKUI_NOTIFICATION_POPUP_NOTIFICATION_H
#include "ukui-notification_global.h"
#include <QObject>
#include <QDateTime>
#include <QList>
#include <QString>
#include <QUrl>
#include <QVariantMap>
#include <QImage>
class PopupNotificationPrvate;
class UKUINOTIFICATION_EXPORT PopupNotification : public QObject
{
Q_OBJECT
public:
/**
* The notification urgency.
*/
enum Urgency {
LowUrgency = 1 << 0,
NormalUrgency = 1 << 1,
CriticalUrgency = 1 << 2,
};
Q_ENUM(Urgency)
Q_DECLARE_FLAGS(Urgencies, Urgency)
Q_FLAG(Urgencies)
explicit PopupNotification(uint id = 0, QObject *parent = nullptr);
PopupNotification(const PopupNotification &other);
PopupNotification &operator=(const PopupNotification &other);
PopupNotification &operator=(PopupNotification &&other) Q_DECL_NOEXCEPT;
~PopupNotification();
QString applicationName() const;
void setApplicationName(const QString &applicationName);
QString icon() const;
void setIcon(const QString &icon);
QString summary() const;
void setSummary(const QString &summary);
QString body() const;
void setBody(const QString &body);
bool hasDefaultAction() const;
void setActions(const QStringList &actions);
QList<QPair<QString, QString>> getActions();
QVariantMap hints() const;
void setHints(const QVariantMap &hints);
int timeout() const;
void setTimeout(int timeout);
QDateTime created() const;
void setCreated(const QDateTime &created);
//hints
bool enableActionIcons() const;
QString category() const;
QString desktopEntry() const;
QImage image() const;
bool resident() const;
QString soundFile() const;
bool transient() const;
void setUrgency(Urgency urgency);
private:
PopupNotificationPrvate *d = nullptr;
};
#endif //UKUI_NOTIFICATION_POPUP_NOTIFICATION_H

View File

@ -0,0 +1,16 @@
//
// Created by zpf on 23-2-2.
//
#ifndef UKUINOTIFICATION_GLOBAL_H
#define UKUINOTIFICATION_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(UKUINOTIFICATION_LIBRARY)
# define UKUINOTIFICATION_EXPORT Q_DECL_EXPORT
#else
# define UKUINOTIFICATION_EXPORT Q_DECL_IMPORT
#endif
#endif // UKUINOTIFICATION_GLOBAL_H

View File

@ -13,9 +13,14 @@ set(notificationServer_SRCS
${3rdParties_DIR}qtsinglecoreapplication.cpp ${3rdParties_DIR}qtsinglecoreapplication.cpp
${3rdParties_DIR}qtlocalpeer.h ${3rdParties_DIR}qtlocalpeer.h
${3rdParties_DIR}qtlocalpeer.cpp ${3rdParties_DIR}qtlocalpeer.cpp
notification-server-application.cpp notification-server-application.h notification.cpp notification.h) notification-server-application.cpp
qt_add_dbus_adaptor(notificationServer_SRCS dbus/org.freedesktop.Notifications.xml server-private.h NotificationServer::ServerPrivate) notification-server-application.h
qt_add_dbus_adaptor(notificationServer_SRCS dbus/org.ukui.notificationServer.xml server-private.h NotificationServer::ServerPrivate) notification.cpp
notification.h
)
qt_add_dbus_adaptor(notificationServer_SRCS ../dbus/org.freedesktop.Notifications.xml server-private.h NotificationServer::ServerPrivate)
qt_add_dbus_adaptor(notificationServer_SRCS ../dbus/org.ukui.NotificationServer.xml server-private.h NotificationServer::ServerPrivate)
add_executable(ukui-notification-server ${notificationServer_SRCS}) add_executable(ukui-notification-server ${notificationServer_SRCS})
target_include_directories(ukui-notification-server PUBLIC ${3rdParties_DIR}) target_include_directories(ukui-notification-server PUBLIC ${3rdParties_DIR})

View File

@ -12,14 +12,18 @@ namespace NotificationServer {
class ServerPrivate : public QObject, protected QDBusContext class ServerPrivate : public QObject, protected QDBusContext
{ {
Q_OBJECT Q_OBJECT
//dbus interface
public: public:
ServerPrivate(QObject *parent); /**
~ServerPrivate(); * The reason a notification was closed
bool init(); */
enum CloseReason {
static QString notificationServiceName(); Expired = 1, // The notification expired(timed out).
static QString notificationServicePath(); DismissedByUser = 2, // The notification was dismissed by the user.
static QString notificationServiceInterface(); Revoked = 3, //< The notification was closed by sender by a call to CloseNotification.
Undefined = 4, //Undefined/reserved reasons.
};
Q_ENUM(CloseReason)
QStringList GetCapabilities() const; QStringList GetCapabilities() const;
uint Notify(const QString &app_name, uint Notify(const QString &app_name,
@ -41,12 +45,27 @@ public:
* *
*/ */
void UnRegisterClient(); void UnRegisterClient();
/**
*
* @param id id
* @param reason
*/
void CloseNotification(uint id, uint);
Q_SIGNALS: Q_SIGNALS:
void NotificationClosed(uint id, uint reason); void NotificationClosed(uint id, uint reason);
void ActionInvoked(uint id, const QString &actionKey); void ActionInvoked(uint id, const QString &actionKey);
void ActivationToken(uint id, const QString &ActivationToken); void ActivationToken(uint id, const QString &ActivationToken);
public:
explicit ServerPrivate(QObject *parent = nullptr);
~ServerPrivate() override;
bool init();
static QString notificationServiceName();
static QString notificationServicePath();
static QString notificationServiceInterface();
private: private:
QDBusServiceWatcher *m_notificationWatchers = nullptr; QDBusServiceWatcher *m_notificationWatchers = nullptr;
uint m_increasedNotificationId = 1;
}; };

View File

@ -7,12 +7,13 @@
#include "server-private.h" #include "server-private.h"
#include "notificationsadaptor.h" #include "notificationsadaptor.h"
namespace NotificationServer { using namespace NotificationServer;
ServerPrivate::ServerPrivate(QObject *parent) : QObject(parent), m_notificationWatchers(new QDBusServiceWatcher(this)) ServerPrivate::ServerPrivate(QObject *parent) : QObject(parent), m_notificationWatchers(new QDBusServiceWatcher(this))
{ {
m_notificationWatchers->setConnection(QDBusConnection::sessionBus()); m_notificationWatchers->setConnection(QDBusConnection::sessionBus());
m_notificationWatchers->setWatchMode(QDBusServiceWatcher::WatchForUnregistration); m_notificationWatchers->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
connect(m_notificationWatchers, &QDBusServiceWatcher::serviceUnregistered, m_notificationWatchers, &QDBusServiceWatcher::removeWatchedService); connect(m_notificationWatchers, &QDBusServiceWatcher::serviceUnregistered,
m_notificationWatchers, &QDBusServiceWatcher::removeWatchedService);
} }
ServerPrivate::~ServerPrivate() = default; ServerPrivate::~ServerPrivate() = default;
@ -25,7 +26,24 @@ QStringList ServerPrivate::GetCapabilities() const
uint ServerPrivate::Notify(const QString &app_name, uint replaces_id, const QString &app_icon, const QString &summary, 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) const QString &body, const QStringList &actions, const QVariantMap &hints, int timeout)
{ {
return 0; uint id = 0;
if(replaces_id > 0) {
id = replaces_id;
} else {
id = m_increasedNotificationId++;
}
//TODO 在hint中增加DISPLAY信息。
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, hints, timeout});
QDBusConnection::sessionBus().call(msg, QDBus::NoBlock);
}
return id;
} }
QString ServerPrivate::GetServerInformation(QString &vendor, QString &version, QString &specVersion) const QString ServerPrivate::GetServerInformation(QString &vendor, QString &version, QString &specVersion) const
@ -35,7 +53,14 @@ QString ServerPrivate::GetServerInformation(QString &vendor, QString &version, Q
void ServerPrivate::CloseNotification(uint id) void ServerPrivate::CloseNotification(uint id)
{ {
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);
}
} }
bool ServerPrivate::init() bool ServerPrivate::init()
@ -81,6 +106,10 @@ void ServerPrivate::UnRegisterClient()
m_notificationWatchers->removeWatchedService(message().service()); m_notificationWatchers->removeWatchedService(message().service());
} }
void ServerPrivate::CloseNotification(uint id, uint)
{
}
Server::Server(QObject *parent): QObject(parent), d(new ServerPrivate(this)) Server::Server(QObject *parent): QObject(parent), d(new ServerPrivate(this))
{ {
} }
@ -96,5 +125,4 @@ bool Server::init()
return d->init(); return d->init();
} }
Server::~Server() = default; Server::~Server() = default;
} // NotificationServer