增加通知解析部分逻辑

This commit is contained in:
iaom 2023-02-06 17:03:12 +08:00
parent 572f46019a
commit 923dff43f9
10 changed files with 380 additions and 76 deletions

View File

@ -7,5 +7,10 @@
<arg name="id" type="u" direction="in"/>
<arg name="reason" type="u" direction="in"/>
</method>
<method name="InvokedAction">
<arg name="id" type="u" direction="in"/>
<arg name="action_key" type="s" direction="in"/>
<arg name="display" type="s" direction="in"/>
</method>
</interface>
</node>

View File

@ -6,10 +6,13 @@ set(ukui-notification_LIB_SRCS
ukui-notification_global.h
notification-client-private.h
)
if(COMMAND qt_add_dbus_interface)
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)
else()
qt5_add_dbus_interface(ukui-notification_LIB_SRCS ../dbus/org.freedesktop.Notifications.xml notifications_interface)
qt5_add_dbus_adaptor(ukui-notification_LIB_SRCS ../dbus/org.ukui.NotificationClient.xml notification-client-private.h UkuiNotification::NotificationClientPrivate)
endif()
add_library(ukui-notification SHARED ${ukui-notification_LIB_SRCS})
target_link_libraries(ukui-notification
PRIVATE

View File

@ -7,12 +7,13 @@
#include <QObject>
#include <QDBusInterface>
#include "notifications_interface.h"
#include "notification-client.h"
namespace UkuiNotification {
class NotificationClientPrivate : public QObject
{
Q_OBJECT
public:
explicit NotificationClientPrivate(QObject *parent = nullptr);
explicit NotificationClientPrivate(NotificationClient *q, QObject *parent = nullptr);
~NotificationClientPrivate() override;
bool registerClient();
static QString clientServicePath();
@ -26,10 +27,12 @@ public:
const QStringList &actions,
const QVariantMap &hints,
int timeout);
void notificationClosed(uint id, uint reason);
void closeNotification(uint id, NotificationClient::CloseReason reason);
private:
void notificationClosed(uint id, uint reason);
OrgFreedesktopNotificationsInterface* m_notificationsInterface = nullptr;
QDBusInterface *m_serverInterface = nullptr;
NotificationClient *q;
};
}
#endif //UKUI_NOTIFICATION_NOTIFICATION_CLIENT_PRIVATE_H

View File

@ -9,7 +9,7 @@
using namespace UkuiNotification;
NotificationClientPrivate::NotificationClientPrivate(QObject *parent) : QObject(parent)
NotificationClientPrivate::NotificationClientPrivate(NotificationClient *q, QObject *parent) : QObject(parent), q(q)
{
}
@ -25,7 +25,6 @@ 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;
@ -74,9 +73,35 @@ void NotificationClientPrivate::Notify(const QString &app_name, uint replaces_id
const QString &summary, const QString &body, const QStringList &actions,
const QVariantMap &hints, int timeout)
{
PopupNotification notification(replaces_id);
notification.setSummary(summary);
notification.setBody(body);
notification.setApplicationName(app_name);
notification.setAplicationIconName(app_icon);
notification.setActions(actions);
notification.setTimeout(timeout);
notification.setHints(hints);
Q_EMIT q->newNotification(notification);
}
void NotificationClientPrivate::notificationClosed(uint id, uint reason)
{
NotificationClient::CloseReason closeReason = NotificationClient::Undefined;
switch (reason) {
case 1:
closeReason = NotificationClient::Expired;
break;
case 2:
closeReason = NotificationClient::DismissedByUser;
break;
case 3:
closeReason = NotificationClient::Revoked;
break;
}
Q_EMIT q->notificationClosed(id, closeReason);
}
void NotificationClientPrivate::closeNotification(uint id, NotificationClient::CloseReason reason)
{
if(!m_notificationsInterface) {
return;
@ -97,3 +122,8 @@ bool NotificationClient::registerClient()
{
return d->registerClient();
}
void NotificationClient::closeNotification(uint id, NotificationClient::CloseReason reason)
{
d->closeNotification(id, reason);
}

View File

@ -6,15 +6,34 @@
#define UKUI_NOTIFICATION_NOTIFICATION_CLIENT_H
#include "ukui-notification_global.h"
#include <QObject>
#include "popup-notification.h"
namespace UkuiNotification {
class NotificationClientPrivate;
class UKUINOTIFICATION_EXPORT NotificationClient : public QObject
{
Q_OBJECT
public:
NotificationClient(QObject *parent);
~NotificationClient() override;
/**
* The reason a notification was closed
*/
enum CloseReason {
Expired = 1, // The notification expired(timed out).
DismissedByUser = 2, // The notification was dismissed by the user.
Revoked = 3, //< The notification was closed by sender by a call to CloseNotification.
Undefined = 4, //Undefined/reserved reasons.
};
Q_ENUM(CloseReason)
explicit NotificationClient(QObject *parent);
~NotificationClient();
/**
*
* @return ture- false
*/
bool registerClient();
void closeNotification(uint id, CloseReason reason);
Q_SIGNALS:
void newNotification(const PopupNotification &notification);
void notificationClosed(uint id, CloseReason closeReason);
private:
NotificationClientPrivate *d = nullptr;
};

View File

@ -3,31 +3,153 @@
//
#include "popup-notification.h"
class PopupNotificationPrvate
#include <QDBusArgument>
#include <QDebug>
#include <QImageReader>
namespace UkuiNotification {
class PopupNotificationPrivate
{
public:
PopupNotificationPrvate();
~PopupNotificationPrvate();
PopupNotificationPrivate();
~PopupNotificationPrivate();
QImage parseImageHint(const QDBusArgument &arg);
void loadImageFromPath(const QString &path);
QSize maximumImageSize();
uint m_id = 0;
QString m_applicationName;
QString m_applicationIconName;
QString m_summary;
QString m_body;
QList<QPair<QString, QString>> m_actions;
bool m_hasDefaultAction = false;
QString m_defaultActionInfo;
QVariantMap m_hints = QVariantMap();
QString m_category;
QImage m_image;
QString m_icon;
QDateTime m_createdTime;
QString m_desktopEntry;
PopupNotification::Urgency m_urgency = PopupNotification::NormalUrgency;
int m_timeout = -1;
bool m_resident = false;
bool m_transient = false;
QString m_soundFile;
bool m_suppressSound = false;
QString m_display;
};
PopupNotificationPrvate::PopupNotificationPrvate()
{
}
using namespace UkuiNotification;
PopupNotificationPrivate::PopupNotificationPrivate()
{
}
PopupNotificationPrvate::~PopupNotificationPrvate()
{
PopupNotificationPrivate::~PopupNotificationPrivate() = default;
QImage PopupNotificationPrivate::parseImageHint(const QDBusArgument &arg)
{
//copy from plasma-workspace
int width, height, rowStride, hasAlpha, bitsPerSample, channels;
QByteArray pixels;
char *ptr;
char *end;
arg.beginStructure();
arg >> width >> height >> rowStride >> hasAlpha >> bitsPerSample >> channels >> pixels;
arg.endStructure();
auto copyLineRGB32 = [](QRgb *dst, const char *src, int width) {
const char *end = src + width * 3;
for (; src != end; ++dst, src += 3) {
*dst = qRgb(src[0], src[1], src[2]);
}
};
auto copyLineARGB32 = [](QRgb *dst, const char *src, int width) {
const char *end = src + width * 4;
for (; src != end; ++dst, src += 4) {
*dst = qRgba(src[0], src[1], src[2], src[3]);
}
};
QImage::Format format = QImage::Format_Invalid;
void (*fcn)(QRgb *, const char *, int) = nullptr;
if (bitsPerSample == 8) {
if (channels == 4) {
format = QImage::Format_ARGB32;
fcn = copyLineARGB32;
} else if (channels == 3) {
format = QImage::Format_RGB32;
fcn = copyLineRGB32;
}
}
if (format == QImage::Format_Invalid) {
qWarning() << "Unsupported image format (hasAlpha:" << hasAlpha << "bitsPerSample:" << bitsPerSample
<< "channels:" << channels << ")";
return QImage();
}
PopupNotification::PopupNotification(uint id, QObject *parent) : QObject(parent)
{
QImage image(width, height, format);
ptr = pixels.data();
end = ptr + pixels.length();
for (int y = 0; y < height; ++y, ptr += rowStride) {
if (ptr + channels * width > end) {
qWarning() << "Image data is incomplete. y:" << y << "height:" << height;
break;
}
fcn((QRgb *)image.scanLine(y), ptr, width);
}
PopupNotification::PopupNotification(const PopupNotification &other)
{
return image;
}
void PopupNotificationPrivate::loadImageFromPath(const QString &path)
{
//copy from plasma-workspace
QUrl imageUrl;
if (path.startsWith(QLatin1Char('/'))) {
imageUrl = QUrl::fromLocalFile(path);
} else if (path.contains(QLatin1Char('/'))) {
imageUrl = QUrl(path);
if (!imageUrl.isLocalFile()) {
qDebug() << "Refused to load image from" << path << "which isn't a valid local location.";
return;
}
}
if (!imageUrl.isValid()) {
m_icon = path;
return;
}
QImageReader reader(imageUrl.toLocalFile());
reader.setAutoTransform(true);
const QSize imageSize = reader.size();
if (imageSize.isValid() && (imageSize.width() > maximumImageSize().width() || imageSize.height() > maximumImageSize().height())) {
const QSize thumbnailSize = imageSize.scaled(maximumImageSize(), Qt::KeepAspectRatio);
reader.setScaledSize(thumbnailSize);
}
m_image = reader.read();
}
QSize PopupNotificationPrivate::maximumImageSize()
{
return QSize(256, 256);
}
PopupNotification::PopupNotification(uint id, QObject *parent) : QObject(parent), d(new PopupNotificationPrivate())
{
d->m_id = id;
d->m_createdTime = QDateTime::currentDateTimeUtc();
}
PopupNotification::PopupNotification(const PopupNotification &other) : d(new PopupNotificationPrivate(*other.d))
{
}
PopupNotification &PopupNotification::operator=( const PopupNotification &other)
@ -44,132 +166,209 @@ PopupNotification &PopupNotification::operator=(PopupNotification &&other) Q_DEC
}
PopupNotification::~PopupNotification()
{
if(d) {
delete d;
d = nullptr;
}
}
QString PopupNotification::applicationName() const
{
return QString();
return d->m_applicationName;
}
void PopupNotification::setApplicationName(const QString &applicationName)
{
d->m_applicationName = applicationName;
}
QString PopupNotification::icon() const
QString PopupNotification::applicationIconName() const
{
return QString();
return d->m_applicationIconName;
}
void PopupNotification::setIcon(const QString &icon)
void PopupNotification::setAplicationIconName(const QString &applicationIconName)
{
d->m_applicationIconName = applicationIconName;
}
QString PopupNotification::summary() const
{
return QString();
return d->m_summary;
}
void PopupNotification::setSummary(const QString &summary)
{
d->m_summary = summary;
}
QString PopupNotification::body() const
{
return QString();
return d->m_body;
}
void PopupNotification::setBody(const QString &body)
{
d->m_body = body;
}
bool PopupNotification::hasDefaultAction() const
{
return false;
return d->m_hasDefaultAction;
}
QString PopupNotification::defauleActionLable()
{
return d->m_defaultActionInfo;
}
void PopupNotification::setActions(const QStringList &actions)
{
if (actions.count() % 2 != 0) {
qWarning() << "List of actions must contain an even number of items, tried to set actions to" << actions;
return;
}
d->m_hasDefaultAction = false;
for (int i = 0; i < actions.count(); i += 2) {
const QString &name = actions.at(i);
const QString &info = actions.at(i + 1);
if (!d->m_hasDefaultAction && name == QLatin1String("default")) {
d->m_hasDefaultAction = true;
d->m_defaultActionInfo = info;
continue;
}
d->m_actions.append({name, info});
}
}
QList<QPair<QString, QString>> PopupNotification::getActions()
{
return QList<QPair<QString, QString>>();
return d->m_actions;
}
QVariantMap PopupNotification::hints() const
{
return QVariantMap();
return d->m_hints;
}
void PopupNotification::setHints(const QVariantMap &hints)
{
d->m_hints = hints;
d->m_desktopEntry = hints.value(QStringLiteral("desktop-entry")).toString();
bool ok;
int urgency = hints.value(QStringLiteral("urgency")).toInt(&ok);
if(ok) {
switch (urgency) {
case 0:
setUrgency(Urgency::LowUrgency);
break;
case 1:
setUrgency(Urgency::NormalUrgency);
break;
case 2:
setUrgency(Urgency::CriticalUrgency);
break;
}
}
d->m_resident = hints.value(QStringLiteral("resident")).toBool();
d->m_resident = hints.value(QStringLiteral("transient")).toBool();
d->m_category = hints.value(QStringLiteral("category")).toString();
auto end = hints.end();
auto it = hints.find(QStringLiteral("image-data"));
if (it != end) {
d->m_image = d->parseImageHint(it->value<QDBusArgument>());
if (d->m_image.isNull()) {
return;
}
const QSize max = d->maximumImageSize();
if (d->m_image.size().width() > max.width() || d->m_image.size().height() > max.height()) {
d->m_image = d->m_image.scaled(max, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
}
if (d->m_image.isNull()) {
it = hints.find(QStringLiteral("image-path"));
if (it != end) {
d->loadImageFromPath(it->toString());
}
}
//暂不支持声音主题
d->m_soundFile = hints.value(QStringLiteral("sound-file")).toString();
d->m_suppressSound = hints.value(QStringLiteral("suppress-sound")).toBool();
d->m_display = hints.value(QStringLiteral("x-ukui-display")).toString();
}
int PopupNotification::timeout() const
{
return 0;
return d->m_timeout;
}
void PopupNotification::setTimeout(int timeout)
{
d->m_timeout = timeout;
}
QDateTime PopupNotification::created() const
QDateTime PopupNotification::createdTime() const
{
return QDateTime();
}
void PopupNotification::setCreated(const QDateTime &created)
{
return d->m_createdTime;
}
bool PopupNotification::enableActionIcons() const
{
//TODO 可以支持action-icon
return false;
}
QString PopupNotification::category() const
{
return QString();
return d->m_category;
}
QString PopupNotification::desktopEntry() const
{
return QString();
return d->m_desktopEntry;
}
QImage PopupNotification::image() const
{
return QImage();
return d->m_image;
}
QString PopupNotification::icon() const
{
return d->m_icon;
}
bool PopupNotification::resident() const
{
return false;
return d->m_resident;
}
QString PopupNotification::soundFile() const
{
return QString();
return d->m_soundFile;
}
bool PopupNotification::suppressSound() const
{
return d->m_suppressSound;
}
bool PopupNotification::transient() const
{
return false;
return d->m_transient;
}
QString PopupNotification::display() const
{
return d->m_display;
}
void PopupNotification::setUrgency(PopupNotification::Urgency urgency)
{
d->m_urgency = urgency;
}

View File

@ -12,7 +12,11 @@
#include <QUrl>
#include <QVariantMap>
#include <QImage>
class PopupNotificationPrvate;
#include <QList>
#include <QPair>
namespace UkuiNotification {
class PopupNotificationPrivate;
//TODO 可以增加通知基类,实现多种类型通知
class UKUINOTIFICATION_EXPORT PopupNotification : public QObject
{
Q_OBJECT
@ -21,13 +25,11 @@ public:
* The notification urgency.
*/
enum Urgency {
LowUrgency = 1 << 0,
NormalUrgency = 1 << 1,
CriticalUrgency = 1 << 2,
LowUrgency = 0,
NormalUrgency = 1,
CriticalUrgency = 2,
};
Q_ENUM(Urgency)
Q_DECLARE_FLAGS(Urgencies, Urgency)
Q_FLAG(Urgencies)
explicit PopupNotification(uint id = 0, QObject *parent = nullptr);
PopupNotification(const PopupNotification &other);
@ -38,8 +40,8 @@ public:
QString applicationName() const;
void setApplicationName(const QString &applicationName);
QString icon() const;
void setIcon(const QString &icon);
QString applicationIconName() const;
void setAplicationIconName(const QString &applicationIconName);
QString summary() const;
void setSummary(const QString &summary);
@ -48,6 +50,7 @@ public:
void setBody(const QString &body);
bool hasDefaultAction() const;
QString defauleActionLable();
void setActions(const QStringList &actions);
QList<QPair<QString, QString>> getActions();
@ -57,24 +60,55 @@ public:
int timeout() const;
void setTimeout(int timeout);
QDateTime created() const;
void setCreated(const QDateTime &created);
//hints
QDateTime createdTime() const;
//m_hints
/**
* 使action
* @return
*/
bool enableActionIcons() const;
/**
*
* @return
*/
QString category() const;
/**
* desktop名称.desktop后缀
* @return
*/
QString desktopEntry() const;
/**
* image-data字段
* @return
*/
QImage image() const;
/**
* image-data失败image-path加载image失败时image-path不是URL,icon会被设置为image-path数据
* @return
*/
QString icon() const;
/**
*
* @return
*/
bool resident() const;
/**
*
* @return
*/
QString soundFile() const;
/**
*
* @return
*/
bool suppressSound() const;
bool transient() const;
void setUrgency(Urgency urgency);
QString display() const;
private:
PopupNotificationPrvate *d = nullptr;
PopupNotificationPrivate *d = nullptr;
};
}
#endif //UKUI_NOTIFICATION_POPUP_NOTIFICATION_H

View File

@ -18,10 +18,13 @@ set(notificationServer_SRCS
notification.cpp
notification.h
)
if(COMMAND qt_add_dbus_adaptor)
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)
else()
qt5_add_dbus_adaptor(notificationServer_SRCS ../dbus/org.freedesktop.Notifications.xml server-private.h NotificationServer::ServerPrivate)
qt5_add_dbus_adaptor(notificationServer_SRCS ../dbus/org.ukui.NotificationServer.xml server-private.h NotificationServer::ServerPrivate)
endif()
add_executable(ukui-notification-server ${notificationServer_SRCS})
target_include_directories(ukui-notification-server PUBLIC ${3rdParties_DIR})

View File

@ -35,7 +35,7 @@ public:
const QVariantMap &hints,
int timeout);
void CloseNotification(uint id);
QString GetServerInformation(QString &vendor, QString &version, QString &specVersion) const;
QString GetServerInformation(QString &vendor, QString &version, QString &spec_version) const;
/**
*
@ -51,6 +51,7 @@ public:
* @param reason
*/
void CloseNotification(uint id, uint);
void InvokedAction(uint id, const QString &action_key, const QString &display);
Q_SIGNALS:
void NotificationClosed(uint id, uint reason);
void ActionInvoked(uint id, const QString &actionKey);

View File

@ -33,7 +33,7 @@ uint ServerPrivate::Notify(const QString &app_name, uint replaces_id, const QStr
id = m_increasedNotificationId++;
}
//TODO 在hint中增加DISPLAY信息
//TODO 在hint中增加DISPLAY信息,注意区分X和waylandDISPLAY信息决定了通知弹窗显示和action执行所在的DISPLAY
for(const QString &service : m_notificationWatchers->watchedServices()) {
QDBusMessage msg = QDBusMessage::createMethodCall(service,
@ -46,7 +46,7 @@ uint ServerPrivate::Notify(const QString &app_name, uint replaces_id, const QStr
return id;
}
QString ServerPrivate::GetServerInformation(QString &vendor, QString &version, QString &specVersion) const
QString ServerPrivate::GetServerInformation(QString &vendor, QString &version, QString &spec_version) const
{
return QString();
}
@ -106,8 +106,15 @@ void ServerPrivate::UnRegisterClient()
m_notificationWatchers->removeWatchedService(message().service());
}
void ServerPrivate::CloseNotification(uint id, uint)
void ServerPrivate::CloseNotification(uint id, uint reason)
{
Q_EMIT NotificationClosed(id, reason);
}
void ServerPrivate::InvokedAction(uint id, const QString &action_key, const QString &display)
{
//TODO: 异步执行action
Q_EMIT ActionInvoked(id, action_key);
}
Server::Server(QObject *parent): QObject(parent), d(new ServerPrivate(this))