-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiqnotificationmodifiers.cpp
270 lines (234 loc) · 7.22 KB
/
iqnotificationmodifiers.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* This file is part of IQ Notifier.
*
* IQ Notifier 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
* any later version.
*
* IQ Notifier 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 IQ Notifier. If not, see <http://www.gnu.org/licenses/>.
*/
#include "iqnotificationmodifiers.h"
#include <map>
#include <QDBusArgument>
#include <QFile>
#include <QIcon>
#include <QUrl>
#include <qt5xdg/XdgDirs>
#include <qt5xdg/XdgIcon>
/*
* Best way found
* Using private pointers for notification fields
* in base class lead to sex with '(*ptr)'
*/
#define N_TO_REFS(N__) \
id_t &id = N__.id; \
Q_UNUSED(id); \
QString &application = N__.application; \
Q_UNUSED(application); \
QString &body = N__.body; \
Q_UNUSED(body); \
QString &title = N__.title; \
Q_UNUSED(title); \
QString &icon_url = N__.icon_url; \
Q_UNUSED(icon_url); \
QStringList &actions = N__.actions; \
Q_UNUSED(actions); \
QVariantMap &hints = N__.hints; \
Q_UNUSED(hints); \
IQNotification::ExpireTimeout &expire_timeout = N__.expire_timeout; \
Q_UNUSED(expire_timeout); \
id_t &replaces_id = N__.replaces_id; \
Q_UNUSED(replaces_id);
namespace
{
template <class K, class V> using map_t = std::map<K, V>;
static map_t<uintmax_t, QString> cached_images;
bool isCached(uintmax_t hash)
{
return cached_images.find(hash) != cached_images.end();
}
QString imageFilenameFromHash(uintmax_t hash)
{
auto ret = XdgDirs::cacheHome() + "/iq-cached_" +
QString::number(hash) + ".png";
return ret;
}
/*
* No ret due to we want to reuse var
*/
void toQmlAbsolutePath(QString &path)
{
if (path.isEmpty())
return;
if (path[0] == '/')
path.insert(0, "file://");
}
template <class T> bool cacheImage(const T &img, uintmax_t hash)
{
auto fname = imageFilenameFromHash(hash);
if (img.save(fname))
cached_images[hash] = std::move(fname);
else
return false;
return true;
}
QString getImageUrlFromHint(const QVariant &argument)
{
int width, height, rowstride, bitsPerSample, channels;
bool hasAlpha;
QByteArray data;
const QDBusArgument arg = argument.value<QDBusArgument>();
arg.beginStructure();
arg >> width;
arg >> height;
arg >> rowstride;
arg >> hasAlpha;
arg >> bitsPerSample;
arg >> channels;
arg >> data;
arg.endStructure();
auto hash = qHash(data);
if (!isCached(hash)) {
bool rgb = !hasAlpha && channels == 3 && bitsPerSample == 8;
QImage::Format imageFormat =
rgb ? QImage::Format_RGB888 : QImage::Format_ARGB32;
QImage img =
QImage(reinterpret_cast<const uchar *>(data.constData()),
width, height, imageFormat);
if (!rgb)
img = img.rgbSwapped();
if (!cacheImage(img, hash)) {
return {};
}
}
return cached_images[hash];
}
QString getImageUrlFromString(const QString &str)
{
static constexpr auto PIXMAP_SIZE = 256;
QUrl url(str);
if (url.isValid() && QFile::exists(url.toLocalFile())) {
return url.toLocalFile();
} else {
// TODO: OMFG????????
auto icon = XdgIcon::fromTheme(str);
auto hash = static_cast<uintmax_t>(icon.cacheKey());
if (!isCached(hash)) {
auto pixmap = icon.pixmap({PIXMAP_SIZE, PIXMAP_SIZE});
if (!cacheImage(pixmap, hash)) {
return {};
}
}
return cached_images[hash];
}
}
} // anonymouse namespace
void IQNotificationModifiers::IDGenerator::modify(IQNotification ¬ification)
{
N_TO_REFS(notification);
if (replaces_id == 0)
id = ++last_id;
}
/*
* Based on lxqt notification daemon
*/
void IQNotificationModifiers::IconHandler::modify(IQNotification ¬ification)
{
N_TO_REFS(notification);
if (!hints["image_data"].isNull()) {
icon_url = getImageUrlFromHint(hints["image_data"]);
} else if (!hints["image_path"].isNull()) {
icon_url =
getImageUrlFromString(hints["image_path"].toString());
} else if (!icon_url.isEmpty()) {
/*
* Check, is it web URL
*/
{
static const QString http{"http://"};
static const QString https{"https://"};
if (icon_url.startsWith(http, Qt::CaseInsensitive) ||
icon_url.startsWith(https, Qt::CaseInsensitive))
return;
}
icon_url = getImageUrlFromString(icon_url);
} else if (!hints["icon_data"].isNull()) {
icon_url = getImageUrlFromHint(hints["icon_data"]);
}
toQmlAbsolutePath(icon_url);
}
IQNotificationModifiers::DefaultTimeout::DefaultTimeout()
: IQConfigurable{"default_timeout"}
{
static constexpr auto real_default{3500};
defaultTimeout = static_cast<uint16_t>(
config.value("default_timeout", real_default).toUInt());
if (defaultTimeout == 0)
defaultTimeout = real_default;
}
void IQNotificationModifiers::DefaultTimeout::modify(
IQNotification ¬ification)
{
N_TO_REFS(notification);
if (expire_timeout < 0)
expire_timeout = static_cast<
std::remove_reference_t<decltype(expire_timeout)>>(
defaultTimeout);
}
IQNotificationModifiers::TitleToIcon::TitleToIcon()
: IQConfigurable{"title_to_icon"}
{
}
void IQNotificationModifiers::TitleToIcon::modify(IQNotification ¬ification)
{
N_TO_REFS(notification);
// Do nothing if icon presented
if (!icon_url.isEmpty())
return;
if (application.compare(title, Qt::CaseInsensitive) == 0) {
icon_url = application.toLower().replace(' ', '-');
}
}
IQNotificationModifiers::ReplaceMinusToDash::ReplaceMinusToDash()
: IQConfigurable{"replace_minus_to_dash"}
{
fixTitle = config.value("title", true).toBool();
fixBody = config.value("body", true).toBool();
}
void IQNotificationModifiers::ReplaceMinusToDash::modify(
IQNotification ¬ification)
{
N_TO_REFS(notification);
if (fixTitle)
replaceMinusToDash(title);
if (fixBody)
replaceMinusToDash(body);
}
void IQNotificationModifiers::ReplaceMinusToDash::replaceMinusToDash(
QString &str)
{
static QString minus{minusPattern}, dash{replaceTo};
str.replace(minus, dash);
}
IQNotificationModifiers::BodyToTitleWhenTitleIsAppName::
BodyToTitleWhenTitleIsAppName()
: IQConfigurable{"body_to_title_when_title_is_app_name"}
{
}
void IQNotificationModifiers::BodyToTitleWhenTitleIsAppName::modify(
IQNotification ¬ification)
{
N_TO_REFS(notification);
if (application.compare(title, Qt::CaseInsensitive) == 0) {
title = body;
body = QString{};
}
}
#undef N_TO_REFS