File indexing completed on 2024-04-28 16:54:38
0001 /* 0002 SPDX-FileCopyrightText: 2020 Shah Bhushan <bshah@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "watchednotificationsmodel.h" 0008 0009 #include <QDBusConnection> 0010 #include <QDBusConnectionInterface> 0011 #include <QDBusMetaType> 0012 #include <QDBusServiceWatcher> 0013 0014 #include <QDebug> 0015 0016 #include "fdonotifications_interface.h" 0017 0018 using namespace NotificationManager; 0019 0020 class WatchedNotificationsModel::Private : public QObject 0021 { 0022 Q_OBJECT 0023 public: 0024 explicit Private(WatchedNotificationsModel *q, QObject *parent = nullptr); 0025 ~Private(); 0026 bool valid = false; 0027 0028 public Q_SLOTS: 0029 Q_SCRIPTABLE void Notify(uint id, 0030 const QString &app_name, 0031 uint replaces_id, 0032 const QString &app_icon, 0033 const QString &summary, 0034 const QString &body, 0035 const QStringList &actions, 0036 const QVariantMap &hints, 0037 int timeout); 0038 Q_SCRIPTABLE void CloseNotification(uint id); 0039 void NotificationClosed(uint id, uint reason); 0040 0041 private: 0042 WatchedNotificationsModel *q; 0043 OrgFreedesktopNotificationsInterface *fdoNotificationsInterface; 0044 }; 0045 0046 WatchedNotificationsModel::Private::Private(WatchedNotificationsModel *q, QObject *parent) 0047 : QObject(parent) 0048 , q(q) 0049 { 0050 QDBusConnection dbus = QDBusConnection::sessionBus(); 0051 fdoNotificationsInterface = 0052 new OrgFreedesktopNotificationsInterface(QStringLiteral("org.freedesktop.Notifications"), QStringLiteral("/org/freedesktop/Notifications"), dbus, this); 0053 connect(fdoNotificationsInterface, 0054 &OrgFreedesktopNotificationsInterface::NotificationClosed, 0055 this, 0056 &WatchedNotificationsModel::Private::NotificationClosed); 0057 dbus.registerObject("/NotificationWatcher", QStringLiteral("org.kde.NotificationWatcher"), this, QDBusConnection::ExportScriptableSlots); 0058 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"), 0059 QStringLiteral("/org/freedesktop/Notifications"), 0060 QStringLiteral("org.kde.NotificationManager"), 0061 QStringLiteral("RegisterWatcher")); 0062 QDBusMessage reply = QDBusConnection::sessionBus().call(msg, QDBus::NoBlock); 0063 if (reply.type() != QDBusMessage::ErrorMessage) { 0064 valid = true; 0065 Q_EMIT q->validChanged(valid); 0066 } 0067 } 0068 0069 WatchedNotificationsModel::Private::~Private() 0070 { 0071 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"), 0072 QStringLiteral("/org/freedesktop/Notifications"), 0073 QStringLiteral("org.kde.NotificationManager"), 0074 QStringLiteral("UnRegisterWatcher")); 0075 QDBusConnection::sessionBus().call(msg, QDBus::NoBlock); 0076 } 0077 0078 void WatchedNotificationsModel::Private::Notify(uint id, 0079 const QString &app_name, 0080 uint replaces_id, 0081 const QString &app_icon, 0082 const QString &summary, 0083 const QString &body, 0084 const QStringList &actions, 0085 const QVariantMap &hints, 0086 int timeout) 0087 { 0088 const bool wasReplaced = replaces_id > 0; 0089 0090 Notification notification(id); 0091 notification.setSummary(summary); 0092 notification.setBody(body); 0093 notification.setApplicationName(app_name); 0094 0095 notification.setActions(actions); 0096 notification.setTimeout(timeout); 0097 notification.setHints(hints); 0098 notification.setIcon(app_icon); 0099 notification.processHints(hints); 0100 0101 if (wasReplaced) { 0102 q->onNotificationReplaced(replaces_id, notification); 0103 } else { 0104 q->onNotificationAdded(notification); 0105 } 0106 } 0107 0108 void WatchedNotificationsModel::Private::CloseNotification(uint id) 0109 { 0110 q->onNotificationRemoved(id, Server::CloseReason::Expired); 0111 } 0112 0113 void WatchedNotificationsModel::Private::NotificationClosed(uint id, uint reason) 0114 { 0115 q->onNotificationRemoved(id, static_cast<Server::CloseReason>(reason)); 0116 } 0117 0118 WatchedNotificationsModel::WatchedNotificationsModel() 0119 : AbstractNotificationsModel() 0120 , d(new Private(this, nullptr)) 0121 { 0122 } 0123 0124 WatchedNotificationsModel::~WatchedNotificationsModel() 0125 { 0126 } 0127 0128 void WatchedNotificationsModel::close(uint notificationId) 0129 { 0130 onNotificationRemoved(notificationId, Server::CloseReason::DismissedByUser); 0131 } 0132 0133 void WatchedNotificationsModel::expire(uint notificationId) 0134 { 0135 onNotificationRemoved(notificationId, Server::CloseReason::Expired); 0136 } 0137 0138 void WatchedNotificationsModel::invokeDefaultAction(uint notificationId, Notifications::InvokeBehavior behavior) 0139 { 0140 this->invokeAction(notificationId, QStringLiteral("default"), behavior); 0141 } 0142 0143 void WatchedNotificationsModel::invokeAction(uint notificationId, const QString &actionName, Notifications::InvokeBehavior /*behavior*/) 0144 { 0145 QDBusConnection dbus = QDBusConnection::sessionBus(); 0146 dbus.registerObject("/NotificationWatcher", this, QDBusConnection::ExportScriptableSlots); 0147 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"), 0148 QStringLiteral("/org/freedesktop/Notifications"), 0149 QStringLiteral("org.kde.NotificationManager"), 0150 QStringLiteral("InvokeAction")); 0151 msg.setArguments({notificationId, actionName}); 0152 QDBusConnection::sessionBus().call(msg, QDBus::NoBlock); 0153 } 0154 0155 void WatchedNotificationsModel::reply(uint notificationId, const QString &text, Notifications::InvokeBehavior behavior) 0156 { 0157 // todo 0158 Q_UNUSED(notificationId) 0159 Q_UNUSED(text) 0160 Q_UNUSED(behavior) 0161 } 0162 0163 bool WatchedNotificationsModel::valid() 0164 { 0165 return d->valid; 0166 } 0167 0168 #include "watchednotificationsmodel.moc"