1 /*
2 SPDX-FileCopyrightText: 2020 Shah Bhushan <bshah@kde.org>
3 SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@privat.broulik.de>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #include "notificationsmodel.h"
9 #include "abstractnotificationsmodel_p.h"
10 #include "notification_p.h"
11 #include "server.h"
12
13 #include "debug.h"
14
15 #include <QProcess>
16
17 #include <KShell>
18
19 using namespace NotificationManager;
20
createNotificationsModel()21 NotificationsModel::Ptr NotificationsModel::createNotificationsModel()
22 {
23 static QWeakPointer<NotificationsModel> s_instance;
24 if (!s_instance) {
25 QSharedPointer<NotificationsModel> ptr(new NotificationsModel());
26 s_instance = ptr.toWeakRef();
27 return ptr;
28 }
29 return s_instance.toStrongRef();
30 }
31
NotificationsModel()32 NotificationsModel::NotificationsModel()
33 {
34 connect(&Server::self(), &Server::notificationAdded, this, [this](const Notification ¬ification) {
35 onNotificationAdded(notification);
36 });
37 connect(&Server::self(), &Server::notificationReplaced, this, [this](uint replacedId, const Notification ¬ification) {
38 onNotificationReplaced(replacedId, notification);
39 });
40 connect(&Server::self(), &Server::notificationRemoved, this, [this](uint removedId, Server::CloseReason reason) {
41 onNotificationRemoved(removedId, reason);
42 });
43 connect(&Server::self(), &Server::serviceOwnershipLost, this, [this] {
44 // Expire all notifications as we're defunct now
45 const auto notificationList = notifications();
46 for (const Notification ¬ification : notificationList) {
47 if (!notification.expired()) {
48 onNotificationRemoved(notification.id(), Server::CloseReason::Expired);
49 }
50 }
51 });
52 Server::self().init();
53 }
54
expire(uint notificationId)55 void NotificationsModel::expire(uint notificationId)
56 {
57 if (rowOfNotification(notificationId) > -1) {
58 Server::self().closeNotification(notificationId, Server::CloseReason::Expired);
59 }
60 }
61
close(uint notificationId)62 void NotificationsModel::close(uint notificationId)
63 {
64 if (rowOfNotification(notificationId) > -1) {
65 Server::self().closeNotification(notificationId, Server::CloseReason::DismissedByUser);
66 }
67 }
68
invokeDefaultAction(uint notificationId)69 void NotificationsModel::invokeDefaultAction(uint notificationId)
70 {
71 const int row = rowOfNotification(notificationId);
72 if (row == -1) {
73 return;
74 }
75
76 const Notification ¬ification = notifications().at(row);
77 if (!notification.hasDefaultAction()) {
78 qCWarning(NOTIFICATIONMANAGER) << "Trying to invoke default action on notification" << notificationId << "which doesn't have one";
79 return;
80 }
81
82 Server::self().invokeAction(notificationId, QStringLiteral("default")); // FIXME make a static Notification::defaultActionName() or something
83 }
84
invokeAction(uint notificationId,const QString & actionName)85 void NotificationsModel::invokeAction(uint notificationId, const QString &actionName)
86 {
87 const int row = rowOfNotification(notificationId);
88 if (row == -1) {
89 return;
90 }
91
92 const Notification ¬ification = notifications().at(row);
93 if (!notification.actionNames().contains(actionName)) {
94 qCWarning(NOTIFICATIONMANAGER) << "Trying to invoke action" << actionName << "on notification" << notificationId << "which it doesn't have";
95 return;
96 }
97
98 Server::self().invokeAction(notificationId, actionName);
99 }
100
reply(uint notificationId,const QString & text)101 void NotificationsModel::reply(uint notificationId, const QString &text)
102 {
103 const int row = rowOfNotification(notificationId);
104 if (row == -1) {
105 return;
106 }
107
108 const Notification ¬ification = notifications().at(row);
109 if (!notification.hasReplyAction()) {
110 qCWarning(NOTIFICATIONMANAGER) << "Trying to reply to a notification which doesn't have a reply action";
111 return;
112 }
113
114 Server::self().reply(notification.dBusService(), notificationId, text);
115 }
116
configure(uint notificationId)117 void NotificationsModel::configure(uint notificationId)
118 {
119 const int row = rowOfNotification(notificationId);
120 if (row == -1) {
121 return;
122 }
123
124 const Notification ¬ification = notifications().at(row);
125
126 if (notification.d->hasConfigureAction) {
127 Server::self().invokeAction(notificationId, QStringLiteral("settings")); // FIXME make a static Notification::configureActionName() or something
128 return;
129 }
130
131 if (!notification.desktopEntry().isEmpty() || !notification.notifyRcName().isEmpty()) {
132 configure(notification.desktopEntry(), notification.notifyRcName(), notification.eventId());
133 return;
134 }
135
136 qCWarning(NOTIFICATIONMANAGER) << "Trying to configure notification" << notificationId << "which isn't configurable";
137 }
138
configure(const QString & desktopEntry,const QString & notifyRcName,const QString & eventId)139 void NotificationsModel::configure(const QString &desktopEntry, const QString ¬ifyRcName, const QString &eventId)
140 {
141 // TODO would be nice to just have a signal but since NotificationsModel is shared,
142 // if we connect to this from Notifications you would get a signal in every instance
143 // and potentially open the config dialog multiple times.
144
145 QStringList args;
146 if (!desktopEntry.isEmpty()) {
147 args.append(QStringLiteral("--desktop-entry"));
148 args.append(desktopEntry);
149 }
150 if (!notifyRcName.isEmpty()) {
151 args.append(QStringLiteral("--notifyrc"));
152 args.append(notifyRcName);
153 }
154 if (!eventId.isEmpty()) {
155 args.append(QStringLiteral("--event-id"));
156 args.append(eventId);
157 }
158
159 QProcess::startDetached(QStringLiteral("kcmshell5"), {QStringLiteral("notifications"), QStringLiteral("--args"), KShell::joinArgs(args)});
160 }
161