-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiqnotifications.cpp
329 lines (288 loc) · 8.32 KB
/
iqnotifications.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* 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 "iqnotifications.h"
#include <experimental/optional>
#include <memory>
#include <utility>
#include <QString>
#include <QStringList>
#include <QTimer>
template <class T> using optional = std::experimental::optional<T>;
IQNotifications::IQNotifications(IQDisposition::ptr_t disposition_,
QObject *parent)
: IQNotificationReceiver(parent), IQConfigurable{"popup_notifications"},
disposition{std::move(disposition_)}
{
if (!disposition)
throw std::invalid_argument{
"IQNotifications: provide disposition"};
disposition->setExtraWindowSize(extraWindowSize());
disposition->setSpacing(spacing());
disposition->setMargins(margins());
connect(this, &IQNotifications::dropNotification, disposition.get(),
&IQDisposition::remove);
connect(disposition.get(), &IQDisposition::moveNotification,
[this](IQNotification::id_t id, QPoint pos) {
emit moveNotification(static_cast<int>(id), pos);
checkExtraNotifications();
});
}
gsl::not_null<IQNotifications *>
IQNotifications::get(IQDisposition::ptr_t disposition)
{
static IQNotifications *ptr_{nullptr};
if (!ptr_) {
if (!disposition)
throw std::invalid_argument("IQDisposition: call get "
"with valid disposition "
"first");
ptr_ = new IQNotifications{std::move(disposition)};
}
return {ptr_};
}
void IQNotifications::setFullscreenDetector(
std::unique_ptr<IQFullscreenDetector> detector)
{
fullscreenDetector = std::move(detector);
}
int IQNotifications::extraNotificationsCount() const
{
return static_cast<int>(extraNotifications.size());
}
bool IQNotifications::closeAllByRightClick() const
{
return config
.value(CONFIG_CLOSE_ALL_BY_RIGHT_CLICK,
CONFIG_CLOSE_ALL_BY_RIGHT_CLICK_DEFAULT)
.toBool();
}
bool IQNotifications::closeVisibleByLeftClick() const
{
return config
.value(CONFIG_CLOSE_VISIBLE_BY_MIDDLE_CLICK,
CONFIG_CLOSE_VISIBLE_BY_MIDDLE_CLICK_DEFAULT)
.toBool();
}
bool IQNotifications::closeByLeftClick() const
{
return config
.value(CONFIG_CLOSE_BY_LEFT_CLICK,
CONFIG_CLOSE_BY_LEFT_CLICK_DEFAULT)
.toBool();
}
bool IQNotifications::dontShowWhenFullscreenAny() const
{
return config
.value(CONFIG_DONT_SHOW_WHEN_FULLSCREEN_ANY,
CONFIG_DONT_SHOW_WHEN_FULLSCREEN_ANY_DEFAULT)
.toBool();
}
bool IQNotifications::dontShowWhenFullscreenCurrentDesktop() const
{
return config
.value(CONFIG_DONT_SHOW_WHEN_FULLSCREEN_CURRENT_DESKTOP,
CONFIG_DONT_SHOW_WHEN_FULLSCREEN_CURRENT_DESKTOP_DEFAULT)
.toBool();
}
void IQNotifications::setDontShowWhenFullscreenCurrentDesktop(bool value)
{
config.setValue(CONFIG_DONT_SHOW_WHEN_FULLSCREEN_CURRENT_DESKTOP,
value);
emit dontShowWhenFullscreenCurrentDesktopChanged();
}
void IQNotifications::onCreateNotification(const IQNotification &n)
{
if (!shouldShowPopup())
return;
if (!createNotificationIfSpaceAvailable(n)) {
extraNotifications.push(n);
emit extraNotificationsCountChanged();
}
}
void IQNotifications::onDropNotification(IQNotification::id_t id)
{
emit dropNotification(static_cast<int>(id));
emit notificationDroppedSignal(id,
IQNotification::CR_NOTIFICATION_CLOSED);
}
void IQNotifications::onCloseButtonPressed(int id)
{
emit dropNotification(id);
emit notificationDroppedSignal(
static_cast<IQNotification::id_t>(id),
IQNotification::CR_NOTIFICATION_DISMISSED);
checkExtraNotifications();
}
void IQNotifications::onActionButtonPressed(int id, const QString &action)
{
emit dropNotification(id);
emit actionInvokedSignal(static_cast<IQNotification::id_t>(id), action);
emit notificationDroppedSignal(
static_cast<IQNotification::id_t>(id),
IQNotification::CR_NOTIFICATION_DISMISSED);
}
void IQNotifications::onExpired(int id)
{
emit dropNotification(id);
emit notificationDroppedSignal(static_cast<IQNotification::id_t>(id),
IQNotification::CR_NOTIFICATION_EXPIRED);
}
void IQNotifications::onDropAll()
{
onDropStacked();
onDropVisible();
}
void IQNotifications::onDropStacked()
{
decltype(extraNotifications) empty;
std::swap(extraNotifications, empty);
emit extraNotificationsCountChanged();
}
void IQNotifications::onDropVisible()
{
emit dropAllVisible();
disposition->removeAll();
checkExtraNotifications();
}
int IQNotifications::spacing() const
{
return config.value(CONFIG_SPACING, CONFIG_SPACING_DEFAULT).toInt();
}
QMargins IQNotifications::margins() const
{
static auto string_list_to_margins =
[](QStringList list) -> optional<QMargins> {
enum Sides { LEFT = 0, TOP, RIGHT, BOTTOM, SIZE };
if (list.size() != SIZE)
return {};
auto get = [&list](auto index) {
bool ok{true};
auto value = list[index].toInt(&ok);
if (!ok)
throw std::logic_error{"IQConfig: "
"ui::global_margins "
"wrong value!"};
return value;
};
QMargins ret;
ret.setLeft(get(LEFT));
ret.setTop(get(TOP));
ret.setRight(get(RIGHT));
ret.setBottom(get(BOTTOM));
return {ret};
};
auto config_field = config.value(CONFIG_GLOBAL_MARGINS);
auto config_margin =
string_list_to_margins(config_field.toStringList());
if (config_margin) {
return *config_margin;
} else {
auto screen = disposition->screen()->availableSize();
auto margin = GLOBAL_MARGINS_DEFAULT_FACTOR * screen.height();
auto m = static_cast<int>(margin);
return {m, m, m, m};
}
}
QSize IQNotifications::windowSize() const
{
return windowSize(CONFIG_WIDTH, CONFIG_HEIGHT, WIDTH_DEFAULT_FACTOR,
HEIGHT_DEFAULT_FACTOR);
}
QSize IQNotifications::windowSize(const QString &width_key,
const QString &height_key,
double width_factor,
double height_factor) const
{
// TODO: cache values
auto get_window_size_from_config =
[width_key, height_key](const auto &config) -> optional<QSize> {
auto get = [&config](auto key) {
bool ok{true};
auto value = config.value(key, 0).toInt(&ok);
if (!ok || value < 0)
throw std::logic_error{"IQConfig: window or "
"extra_window size "
"wrong value!"};
return value;
};
auto w = get(width_key);
auto h = get(height_key);
if (!w || !h)
return {};
else
return {QSize{w, h}};
};
auto config_size = get_window_size_from_config(config);
if (config_size) {
return *config_size;
} else {
auto screen = disposition->screen()->availableSize();
auto w = width_factor * screen.width();
auto h = height_factor * screen.height();
return QSize{static_cast<int>(w), static_cast<int>(h)};
}
}
QSize IQNotifications::extraWindowSize() const
{
return windowSize(CONFIG_EXTRA_WINDOW_WIDTH, CONFIG_EXTRA_WINDOW_HEIGHT,
EXTRA_WINDOW_WIDTH_DEFAULT_FACTOR,
EXTRA_WINDOW_HEIGHT_DEFAULT_FACTOR);
}
QPoint IQNotifications::extraWindowPos() const
{
return disposition->externalWindowPos();
}
bool IQNotifications::createNotificationIfSpaceAvailable(
const IQNotification &n)
{
auto size = windowSize();
auto pos = disposition->poses(n.id, size);
if (pos) {
auto id = n.replaces_id ? n.replaces_id : n.id;
emit createNotification(static_cast<int>(id), size, *pos,
n.expire_timeout, n.application, n.body,
n.title, n.icon_url, n.actions);
return true;
} else {
return false;
}
}
void IQNotifications::checkExtraNotifications()
{
while (!extraNotifications.empty() &&
createNotificationIfSpaceAvailable(extraNotifications.front())) {
extraNotifications.pop();
emit extraNotificationsCountChanged();
}
}
bool IQNotifications::shouldShowPopup() const
{
if (fullscreenDetector) {
if (dontShowWhenFullscreenCurrentDesktop()) {
auto cd = fullscreenDetector
->fullscreenWindowsOnCurrentDesktop();
if (cd)
return false;
}
if (dontShowWhenFullscreenAny()) {
auto any = fullscreenDetector->fullscreenWindows();
if (any)
return false;
}
}
return true;
}