2023-04-17 11:15:11 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023, KylinSoft Co., Ltd.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors: iaom <zhangpengfei@kylinos.cn>
|
|
|
|
*/
|
2023-02-03 18:01:06 +08:00
|
|
|
|
|
|
|
#include "notification-client.h"
|
|
|
|
#include "notification-client-private.h"
|
|
|
|
#include <QDBusConnection>
|
2023-03-08 16:57:51 +08:00
|
|
|
#include <QQmlEngine>
|
2023-02-03 18:01:06 +08:00
|
|
|
#include "notificationclientadaptor.h"
|
|
|
|
|
|
|
|
using namespace UkuiNotification;
|
|
|
|
|
2023-02-16 09:28:41 +08:00
|
|
|
NotificationClientPrivate::NotificationClientPrivate(NotificationClient *q) : QObject(q), q(q)
|
2023-02-03 18:01:06 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationClientPrivate::~NotificationClientPrivate()
|
|
|
|
{
|
|
|
|
if(m_notificationsInterface) {
|
|
|
|
m_notificationsInterface->call(QDBus::NoBlock, QStringLiteral("UnRegisterClient"));
|
2023-02-09 13:54:53 +08:00
|
|
|
delete m_notificationsInterface;
|
|
|
|
m_notificationsInterface = nullptr;
|
2023-02-03 18:01:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:54:53 +08:00
|
|
|
bool NotificationClientPrivate::init()
|
2023-02-03 18:01:06 +08:00
|
|
|
{
|
2023-02-17 16:54:45 +08:00
|
|
|
if(m_registered) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_registered = true;
|
2023-02-03 18:01:06 +08:00
|
|
|
new NotificationClientAdaptor(this);
|
|
|
|
QDBusConnection conn = QDBusConnection::sessionBus();
|
|
|
|
|
|
|
|
if(!conn.registerObject(clientServicePath(), clientServiceInterface(), this)) {
|
|
|
|
qWarning() << "Failed to register NotificationClient DBus object!" << conn.lastError();
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-17 16:54:45 +08:00
|
|
|
m_watcher = new QDBusServiceWatcher(QStringLiteral("org.freedesktop.Notifications"),
|
2023-02-09 13:54:53 +08:00
|
|
|
conn,
|
2023-03-15 14:42:54 +08:00
|
|
|
QDBusServiceWatcher::WatchForOwnerChange,
|
2023-02-09 13:54:53 +08:00
|
|
|
this);
|
2023-02-17 16:54:45 +08:00
|
|
|
connect(m_watcher, &QDBusServiceWatcher::serviceOwnerChanged, this, &NotificationClientPrivate::serviceChange);
|
2023-02-09 13:54:53 +08:00
|
|
|
return registerClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NotificationClientPrivate::registerClient()
|
|
|
|
{
|
|
|
|
QDBusConnection conn = QDBusConnection::sessionBus();
|
2023-02-17 16:54:45 +08:00
|
|
|
if(!m_notificationsInterface) {
|
|
|
|
m_notificationsInterface = new OrgFreedesktopNotificationsInterface(QStringLiteral("org.freedesktop.Notifications"),
|
|
|
|
QStringLiteral("/org/freedesktop/Notifications"),
|
|
|
|
conn);
|
|
|
|
|
|
|
|
connect(m_notificationsInterface, &OrgFreedesktopNotificationsInterface::NotificationClosed,
|
|
|
|
this, &NotificationClientPrivate::notificationClosed);
|
2023-02-03 18:01:06 +08:00
|
|
|
}
|
2023-02-17 16:54:45 +08:00
|
|
|
|
2023-02-03 18:01:06 +08:00
|
|
|
if(!m_serverInterface) {
|
|
|
|
m_serverInterface = new QDBusInterface(QStringLiteral("org.freedesktop.Notifications"),
|
2023-02-09 13:54:53 +08:00
|
|
|
QStringLiteral("/org/freedesktop/Notifications"),
|
2023-02-03 18:01:06 +08:00
|
|
|
QStringLiteral("org.ukui.NotificationServer"),
|
|
|
|
conn,
|
|
|
|
this);
|
|
|
|
if(!m_serverInterface->isValid()) {
|
|
|
|
qWarning() << "Failed to create notification server interface! " << conn.lastError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-02-09 13:54:53 +08:00
|
|
|
QDBusMessage reply = m_serverInterface->call(QStringLiteral("RegisterClient"));
|
|
|
|
if (reply.type() == QDBusMessage::ErrorMessage || reply.type() == QDBusMessage::InvalidMessage) {
|
|
|
|
qWarning() << "Failed to call RegisterClient!" << conn.lastError() << reply.type();
|
2023-02-03 18:01:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-17 16:54:45 +08:00
|
|
|
void NotificationClientPrivate::unregisterClient()
|
2023-02-09 13:54:53 +08:00
|
|
|
{
|
|
|
|
if(m_notificationsInterface) {
|
2023-02-17 16:54:45 +08:00
|
|
|
disconnect(m_notificationsInterface, &OrgFreedesktopNotificationsInterface::NotificationClosed,
|
|
|
|
this, &NotificationClientPrivate::notificationClosed);
|
2023-02-09 13:54:53 +08:00
|
|
|
delete m_notificationsInterface;
|
|
|
|
m_notificationsInterface = nullptr;
|
|
|
|
}
|
2023-02-17 16:54:45 +08:00
|
|
|
QDBusConnection conn = QDBusConnection::sessionBus();
|
|
|
|
if(m_serverInterface) {
|
|
|
|
QDBusMessage reply = m_serverInterface->call(QStringLiteral("UnRegisterClient"));
|
|
|
|
if (reply.type() == QDBusMessage::ErrorMessage || reply.type() == QDBusMessage::InvalidMessage) {
|
|
|
|
qWarning() << "Failed to call UnRegisterClient!" << conn.lastError() << reply.type();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn.unregisterObject(clientServicePath(), QDBusConnection::UnregisterTree);
|
|
|
|
|
|
|
|
m_registered = false;
|
2023-02-09 13:54:53 +08:00
|
|
|
}
|
|
|
|
|
2023-02-03 18:01:06 +08:00
|
|
|
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)
|
|
|
|
{
|
2023-02-06 17:03:12 +08:00
|
|
|
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);
|
2023-02-03 18:01:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationClientPrivate::notificationClosed(uint id, uint reason)
|
2023-02-06 17:03:12 +08:00
|
|
|
{
|
2023-03-08 16:57:51 +08:00
|
|
|
auto closeReason = NotificationCloseReason::CloseReason::Undefined;
|
2023-02-06 17:03:12 +08:00
|
|
|
switch (reason) {
|
|
|
|
case 1:
|
2023-03-08 16:57:51 +08:00
|
|
|
closeReason = NotificationCloseReason::CloseReason::Expired;
|
2023-02-06 17:03:12 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2023-03-08 16:57:51 +08:00
|
|
|
closeReason = NotificationCloseReason::CloseReason::DismissedByUser;
|
2023-02-06 17:03:12 +08:00
|
|
|
break;
|
|
|
|
case 3:
|
2023-03-08 16:57:51 +08:00
|
|
|
closeReason = NotificationCloseReason::CloseReason::Revoked;
|
2023-02-06 17:03:12 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Q_EMIT q->notificationClosed(id, closeReason);
|
|
|
|
}
|
|
|
|
|
2023-03-08 16:57:51 +08:00
|
|
|
bool NotificationClientPrivate::closeNotification(uint id, NotificationCloseReason::CloseReason reason)
|
2023-02-03 18:01:06 +08:00
|
|
|
{
|
2023-02-17 16:54:45 +08:00
|
|
|
if(!m_serverInterface) {
|
|
|
|
return false;
|
2023-02-03 18:01:06 +08:00
|
|
|
}
|
2023-02-17 16:54:45 +08:00
|
|
|
QDBusMessage reply = m_serverInterface->callWithArgumentList(QDBus::NoBlock, QStringLiteral("CloseNotification"), {id, reason});
|
2023-02-03 18:01:06 +08:00
|
|
|
if (reply.type() == QDBusMessage::ErrorMessage) {
|
|
|
|
qWarning() << "Failed to call CloseNotification!" << QDBusConnection::sessionBus().lastError();
|
2023-02-17 16:54:45 +08:00
|
|
|
return false;
|
2023-02-03 18:01:06 +08:00
|
|
|
}
|
2023-02-17 16:54:45 +08:00
|
|
|
return true;
|
2023-02-03 18:01:06 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:54:53 +08:00
|
|
|
void NotificationClientPrivate::serviceChange(const QString &service, const QString &oldOwner, const QString &newOwner)
|
|
|
|
{
|
|
|
|
qDebug() << "Notification Service" << service << "status change, old owner:" << oldOwner << "new:" << newOwner;
|
2023-02-17 16:54:45 +08:00
|
|
|
if(oldOwner.isEmpty() && m_registered) {
|
2023-02-09 13:54:53 +08:00
|
|
|
registerClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 16:54:45 +08:00
|
|
|
bool NotificationClientPrivate::invokeAction(uint id, const QString &action_key)
|
2023-02-13 17:10:55 +08:00
|
|
|
{
|
2023-02-17 16:54:45 +08:00
|
|
|
if(!m_serverInterface) {
|
|
|
|
return false;
|
2023-02-13 17:10:55 +08:00
|
|
|
}
|
2023-02-17 16:54:45 +08:00
|
|
|
qDebug() << "invokeAction" << id << action_key;
|
|
|
|
QDBusMessage reply = m_serverInterface->callWithArgumentList(QDBus::NoBlock,
|
|
|
|
QStringLiteral("InvokeAction"),
|
2023-02-14 17:21:09 +08:00
|
|
|
{id, action_key});
|
2023-02-13 17:10:55 +08:00
|
|
|
if (reply.type() == QDBusMessage::ErrorMessage) {
|
|
|
|
qWarning() << "Failed to call InvokedAction!" << QDBusConnection::sessionBus().lastError();
|
2023-02-17 16:54:45 +08:00
|
|
|
return false;
|
2023-02-13 17:10:55 +08:00
|
|
|
}
|
2023-02-17 16:54:45 +08:00
|
|
|
return true;
|
2023-02-13 17:10:55 +08:00
|
|
|
}
|
|
|
|
|
2023-02-03 18:01:06 +08:00
|
|
|
NotificationClient::NotificationClient(QObject *parent) : QObject(parent), d(new NotificationClientPrivate(this))
|
|
|
|
{
|
2023-02-09 13:54:53 +08:00
|
|
|
qRegisterMetaType<PopupNotification>("PopupNotification");
|
2023-03-17 15:01:29 +08:00
|
|
|
qRegisterMetaType<UkuiNotification::PopupNotification>("const UkuiNotification::PopupNotification&");
|
|
|
|
qRegisterMetaType<UkuiNotification::ActionList>("ActionList");
|
2023-03-08 16:57:51 +08:00
|
|
|
qmlRegisterUncreatableType<UkuiNotification::NotificationCloseReason>("org.ukui.notification.client", 1, 0, "NotificationCloseReason", "");
|
2023-02-03 18:01:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationClient::~NotificationClient() = default;
|
|
|
|
|
|
|
|
bool NotificationClient::registerClient()
|
|
|
|
{
|
2023-02-09 13:54:53 +08:00
|
|
|
return d->init();
|
2023-02-06 17:03:12 +08:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:57:51 +08:00
|
|
|
bool NotificationClient::closeNotification(uint id, NotificationCloseReason::CloseReason reason)
|
2023-02-17 16:54:45 +08:00
|
|
|
{
|
|
|
|
return d->closeNotification(id, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NotificationClient::invokeAction(uint id, const QString &action_key)
|
2023-02-06 17:03:12 +08:00
|
|
|
{
|
2023-02-17 16:54:45 +08:00
|
|
|
return d->invokeAction(id, action_key);
|
2023-02-13 17:10:55 +08:00
|
|
|
}
|
|
|
|
|
2023-02-17 16:54:45 +08:00
|
|
|
void NotificationClient::unregisterClient()
|
2023-02-13 17:10:55 +08:00
|
|
|
{
|
2023-02-17 16:54:45 +08:00
|
|
|
d->unregisterClient();
|
2023-02-13 17:10:55 +08:00
|
|
|
}
|