forked from lxqt/liblxqt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxqtnotification.cpp
254 lines (217 loc) · 6.78 KB
/
lxqtnotification.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright (C) 2012 Alec Moskvin <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "lxqtnotification.h"
#include "lxqtnotification_p.h"
#include <QMessageBox>
#include <QDebug>
using namespace LXQt;
Notification::Notification(const QString& summary, QObject* parent) :
QObject(parent),
d_ptr(new NotificationPrivate(summary, this))
{
}
Notification::~Notification()
{
Q_D(Notification);
delete d;
}
void Notification::update()
{
Q_D(Notification);
d->update();
}
void Notification::close()
{
Q_D(Notification);
d->close();
}
void Notification::setSummary(const QString& summary)
{
Q_D(Notification);
d->mSummary = summary;
}
void Notification::setBody(const QString& body)
{
Q_D(Notification);
d->mBody = body;
}
void Notification::setIcon(const QString& iconName)
{
Q_D(Notification);
d->mIconName = iconName;
}
void Notification::setActions(const QStringList& actions, int defaultAction)
{
Q_D(Notification);
d->setActions(actions, defaultAction);
}
void Notification::setTimeout(int timeout)
{
Q_D(Notification);
d->mTimeout = timeout;
}
void Notification::setHint(const QString& hintName, const QVariant& value)
{
Q_D(Notification);
d->mHints.insert(hintName, value);
}
void Notification::setUrgencyHint(Urgency urgency)
{
Q_D(Notification);
d->mHints.insert(QL1S("urgency"), qvariant_cast<uchar>(urgency));
}
void Notification::clearHints()
{
Q_D(Notification);
d->mHints.clear();
}
QStringList Notification::getCapabilities()
{
Q_D(Notification);
return d->mInterface->GetCapabilities().value();
}
const Notification::ServerInfo Notification::serverInfo()
{
Q_D(Notification);
return d->serverInfo();
}
void Notification::queryServerInfo()
{
Q_D(Notification);
d->queryServerInfo(/*async=*/true);
}
void Notification::notify(const QString& summary, const QString& body, const QString& iconName)
{
Notification notification(summary);
notification.setBody(body);
notification.setIcon(iconName);
notification.update();
}
bool NotificationPrivate::sIsServerInfoQueried = 0;
Notification::ServerInfo NotificationPrivate::sServerInfo;
NotificationPrivate::NotificationPrivate(const QString& summary, Notification* parent) :
mId(0),
mSummary(summary),
mTimeout(-1),
q_ptr(parent)
{
mInterface = new OrgFreedesktopNotificationsInterface(QL1S("org.freedesktop.Notifications"),
QL1S("/org/freedesktop/Notifications"),
QDBusConnection::sessionBus(), this);
connect(mInterface, &OrgFreedesktopNotificationsInterface::NotificationClosed,
this, &NotificationPrivate::notificationClosed);
connect(mInterface, &OrgFreedesktopNotificationsInterface::ActionInvoked,
this, &NotificationPrivate::handleAction);
}
NotificationPrivate::~NotificationPrivate() = default;
void NotificationPrivate::update()
{
QDBusPendingReply<uint> reply = mInterface->Notify(qAppName(), mId, mIconName, mSummary, mBody, mActions, mHints, mTimeout);
reply.waitForFinished();
if (!reply.isError())
{
mId = reply.value();
}
else
{
if (mHints.contains(QL1S("urgency")) && mHints.value(QL1S("urgency")).toInt() != Notification::UrgencyLow)
QMessageBox::information(nullptr, tr("Notifications Fallback"), mSummary + QL1S(" \n\n ") + mBody);
}
}
void NotificationPrivate::setActions(QStringList actions, int defaultAction)
{
mActions.clear();
mDefaultAction = defaultAction;
const int N = actions.size();
for (int ix = 0; ix < N; ix++)
{
if (ix == defaultAction)
mActions.append(QL1S("default"));
else
mActions.append(QString::number(ix));
mActions.append(actions[ix]);
}
}
const Notification::ServerInfo NotificationPrivate::serverInfo()
{
if (!sIsServerInfoQueried) {
queryServerInfo (/*async=*/false);
}
return sServerInfo;
}
void NotificationPrivate::queryServerInfo(bool async)
{
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(mInterface->GetServerInformation(), this);
connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher* callWatcher) {
Q_Q(Notification);
QDBusPendingReply<QString, QString, QString, QString> reply = *callWatcher;
if (!reply.isError()) {
sServerInfo.name = reply.argumentAt<0>();
sServerInfo.vendor = reply.argumentAt<1>();
sServerInfo.version = reply.argumentAt<2>();
sServerInfo.specVersion = reply.argumentAt<3>();
} else {
// server either responds something strange or nothing; assume it's malfunctioning
sServerInfo.name.clear();
sServerInfo.vendor.clear();
sServerInfo.version.clear();
sServerInfo.specVersion.clear();
}
sIsServerInfoQueried = true;
Q_EMIT q->serverInfoReady();
sender()->deleteLater();
});
if (!async) {
QEventLoop loop;
connect(watcher, &QDBusPendingCallWatcher::finished, &loop, &QEventLoop::quit);
loop.exec();
}
}
void NotificationPrivate::handleAction(uint id, const QString& key)
{
if (id != mId)
return;
Q_Q(Notification);
qDebug() << "action invoked:" << key;
bool ok = true;
int keyId;
if (key == QL1S("default"))
keyId = mDefaultAction;
else
keyId = key.toInt(&ok);
if (ok && keyId >= 0)
Q_EMIT q->actionActivated(keyId);
}
void NotificationPrivate::close()
{
mInterface->CloseNotification(mId);
mId = 0;
}
void NotificationPrivate::notificationClosed(uint id, uint reason)
{
Q_Q(Notification);
if (id != 0 && id == mId)
{
mId = 0;
}
Q_EMIT q->notificationClosed(Notification::CloseReason(reason));
}