Plasma-workspace

watchednotificationsmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Shah Bhushan <bshah@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "watchednotificationsmodel.h"
8
9#include <QDBusConnection>
10#include <QDBusConnectionInterface>
11#include <QDBusMetaType>
12#include <QDBusServiceWatcher>
13
14#include <QDebug>
15
16#include "fdonotifications_interface.h"
17
18using namespace Qt::StringLiterals;
19using namespace NotificationManager;
20
21class WatchedNotificationsModel::Private : public QObject
22{
24public:
25 explicit Private(WatchedNotificationsModel *q, QObject *parent = nullptr);
26 ~Private();
27 bool valid = false;
28
29public Q_SLOTS:
30 Q_SCRIPTABLE void Notify(uint id,
31 const QString &app_name,
32 uint replaces_id,
33 const QString &app_icon,
34 const QString &summary,
35 const QString &body,
36 const QStringList &actions,
37 const QVariantMap &hints,
38 int timeout);
39 Q_SCRIPTABLE void CloseNotification(uint id);
40 void NotificationClosed(uint id, uint reason);
41
42private:
43 WatchedNotificationsModel *q;
44 OrgFreedesktopNotificationsInterface *fdoNotificationsInterface;
45};
46
47WatchedNotificationsModel::Private::Private(WatchedNotificationsModel *q, QObject *parent)
48 : QObject(parent)
49 , q(q)
50{
52 fdoNotificationsInterface =
53 new OrgFreedesktopNotificationsInterface(QStringLiteral("org.freedesktop.Notifications"), QStringLiteral("/org/freedesktop/Notifications"), dbus, this);
54 connect(fdoNotificationsInterface,
55 &OrgFreedesktopNotificationsInterface::NotificationClosed,
56 this,
57 &WatchedNotificationsModel::Private::NotificationClosed);
58 dbus.registerObject(u"/NotificationWatcher"_s, QStringLiteral("org.kde.NotificationWatcher"), this, QDBusConnection::ExportScriptableSlots);
59 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"),
60 QStringLiteral("/org/freedesktop/Notifications"),
61 QStringLiteral("org.kde.NotificationManager"),
62 QStringLiteral("RegisterWatcher"));
64 if (reply.type() != QDBusMessage::ErrorMessage) {
65 valid = true;
66 Q_EMIT q->validChanged(valid);
67 }
68}
69
70WatchedNotificationsModel::Private::~Private()
71{
72 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"),
73 QStringLiteral("/org/freedesktop/Notifications"),
74 QStringLiteral("org.kde.NotificationManager"),
75 QStringLiteral("UnRegisterWatcher"));
77}
78
79void WatchedNotificationsModel::Private::Notify(uint id,
80 const QString &app_name,
81 uint replaces_id,
82 const QString &app_icon,
83 const QString &summary,
84 const QString &body,
85 const QStringList &actions,
86 const QVariantMap &hints,
87 int timeout)
88{
89 const bool wasReplaced = replaces_id > 0;
90
91 Notification notification(id);
92 notification.setSummary(summary);
93 notification.setBody(body);
94 notification.setApplicationName(app_name);
95
96 notification.setActions(actions);
97 notification.setTimeout(timeout);
98 notification.setHints(hints);
99 notification.setIcon(app_icon);
100 notification.processHints(hints);
101
102 if (wasReplaced) {
103 q->onNotificationReplaced(replaces_id, notification);
104 } else {
105 q->onNotificationAdded(notification);
106 }
107}
108
109void WatchedNotificationsModel::Private::CloseNotification(uint id)
110{
111 q->onNotificationRemoved(id, Server::CloseReason::Expired);
112}
113
114void WatchedNotificationsModel::Private::NotificationClosed(uint id, uint reason)
115{
116 q->onNotificationRemoved(id, static_cast<Server::CloseReason>(reason));
117}
118
119WatchedNotificationsModel::WatchedNotificationsModel()
120 : AbstractNotificationsModel()
121 , d(new Private(this, nullptr))
122{
123}
124
125WatchedNotificationsModel::~WatchedNotificationsModel()
126{
127}
128
129void WatchedNotificationsModel::close(uint notificationId)
130{
131 onNotificationRemoved(notificationId, Server::CloseReason::DismissedByUser);
132}
133
134void WatchedNotificationsModel::expire(uint notificationId)
135{
136 onNotificationRemoved(notificationId, Server::CloseReason::Expired);
137}
138
139void WatchedNotificationsModel::invokeDefaultAction(uint notificationId, Notifications::InvokeBehavior behavior)
140{
141 this->invokeAction(notificationId, QStringLiteral("default"), behavior);
142}
143
144void WatchedNotificationsModel::invokeAction(uint notificationId, const QString &actionName, Notifications::InvokeBehavior /*behavior*/)
145{
147 dbus.registerObject(u"/NotificationWatcher"_s, this, QDBusConnection::ExportScriptableSlots);
148 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"),
149 QStringLiteral("/org/freedesktop/Notifications"),
150 QStringLiteral("org.kde.NotificationManager"),
151 QStringLiteral("InvokeAction"));
152 msg.setArguments({notificationId, actionName});
154}
155
156void WatchedNotificationsModel::reply(uint notificationId, const QString &text, Notifications::InvokeBehavior behavior)
157{
158 // todo
159 Q_UNUSED(notificationId)
160 Q_UNUSED(text)
161 Q_UNUSED(behavior)
162}
163
164bool WatchedNotificationsModel::valid()
165{
166 return d->valid;
167}
168
169#include "watchednotificationsmodel.moc"
Represents a single notification.
CloseReason
The reason a notification was closed.
Definition server.h:69
@ Expired
The notification timed out.
@ DismissedByUser
The user explicitly closed or acknowledged the notification.
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const const
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
QDBusConnection sessionBus()
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void setArguments(const QList< QVariant > &arguments)
MessageType type() const const
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QObject * parent() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.