Debian Codegraph @

src/librssguard/gui/notifications/toastnotificationsmanager.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/notifications/toastnotificationsmanager.h"
4
5 #include "3rd-party/boolinq/boolinq.h"
6 #include "gui/notifications/articlelistnotification.h"
7 #include "gui/notifications/basetoastnotification.h"
8 #include "gui/notifications/toastnotification.h"
9 #include "miscellaneous/settings.h"
10
11 #include <QRect>
12 #include <QScreen>
13 #include <QWindow>
14
15 QString ToastNotificationsManager::textForPosition(ToastNotificationsManager::NotificationPosition pos) {
16 switch (pos) {
17 case TopLeft:
18 return QObject::tr("top-left");
19
20 case TopRight:
21 return QObject::tr("top-right");
22
23 case BottomLeft:
24 return QObject::tr("bottom-left");
25
26 case BottomRight:
27 default:
28 return QObject::tr("bottom-right");
29 }
30 }
31
32 ToastNotificationsManager::ToastNotificationsManager(QObject* parent)
33 : QObject(parent), m_position(ToastNotificationsManager::NotificationPosition::TopRight), m_screen(0), m_margins(0),
34 m_width(0), m_opacity(0.0), m_articleListNotification(nullptr) {
35 resetNotifications(false);
36 }
37
38 ToastNotificationsManager::~ToastNotificationsManager() {
39 clear(true);
40 }
41
42 QList<BaseToastNotification*> ToastNotificationsManager::activeNotifications() const {
43 return m_activeNotifications;
44 }
45
46 int ToastNotificationsManager::screen() const {
47 return m_screen;
48 }
49
50 void ToastNotificationsManager::setScreen(int screen) {
51 m_screen = screen;
52 }
53
54 ToastNotificationsManager::NotificationPosition ToastNotificationsManager::position() const {
55 return m_position;
56 }
57
58 void ToastNotificationsManager::setPosition(NotificationPosition position) {
59 m_position = position;
60 }
61
62 void ToastNotificationsManager::resetNotifications(bool reload_existing_notifications) {
63 m_position = qApp->settings()
64 ->value(GROUP(GUI), SETTING(GUI::ToastNotificationsPosition))
65 .value<ToastNotificationsManager::NotificationPosition>();
66 m_screen = qApp->settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsScreen)).toInt();
67 m_margins = qApp->settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsMargin)).toInt();
68 m_opacity = qApp->settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsOpacity)).toDouble();
69 m_width = qApp->settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsWidth)).toInt();
70
71 if (reload_existing_notifications) {
72 auto notif = m_activeNotifications;
73
74 clear(false);
75
76 while (!notif.isEmpty()) {
77 BaseToastNotification* one_notif = notif.takeLast();
78
79 processNotification(one_notif);
80 }
81 }
82 }
83
84 void ToastNotificationsManager::clear(bool delete_from_memory) {
85 for (BaseToastNotification* notif : m_activeNotifications) {
86 closeNotification(notif, delete_from_memory);
87 }
88
89 m_activeNotifications.clear();
90 }
91
92 void ToastNotificationsManager::processNotification(BaseToastNotification* notif) {
93 notif->setWindowOpacity(m_opacity);
94 notif->setFixedWidth(m_width);
95
96 notif->show();
97
98 auto* screen = moveToProperScreen(notif);
99 auto notif_new_pos = cornerForNewNotification(screen->availableGeometry());
100
101 // Make sure notification is finally resized.
102 notif->adjustSize();
103 QCoreApplication::processEvents();
104
105 // Move notification, at this point we already need to know its precise size.
106 moveNotificationToCorner(notif, notif_new_pos);
107
108 // Remove out-of-bounds old notifications and shift existing
109 // ones to make space for new notifications.
110 removeOutOfBoundsNotifications(notif->height());
111 makeSpaceForNotification(notif->height());
112
113 m_activeNotifications.prepend(notif);
114 }
115
116 void ToastNotificationsManager::showNotification(Notification::Event event,
117 const GuiMessage& msg,
118 const GuiAction& action) {
119 BaseToastNotification* notif;
120
121 if (!msg.m_feedFetchResults.updatedFeeds().isEmpty()) {
122 if (m_articleListNotification == nullptr) {
123 initializeArticleListNotification();
124 }
125 else if (m_activeNotifications.contains(m_articleListNotification)) {
126 // Article notification is somewhere in list, clear first to move it to first positon.
127 closeNotification(m_articleListNotification, false);
128 }
129
130 m_articleListNotification->loadResults(msg.m_feedFetchResults.updatedFeeds());
131
132 notif = m_articleListNotification;
133 }
134 else {
135 notif = new ToastNotification(event, msg, action, qApp->mainFormWidget());
136 hookNotification(notif);
137 }
138
139 processNotification(notif);
140 }
141
142 void ToastNotificationsManager::closeNotification(BaseToastNotification* notif, bool delete_from_memory) {
143 auto notif_idx = m_activeNotifications.indexOf(notif);
144
145 if (delete_from_memory) {
146 notif->deleteLater();
147 }
148 else {
149 notif->hide();
150 }
151
152 m_activeNotifications.removeAll(notif);
153
154 // Shift all notifications.
155 if (notif_idx < 0) {
156 return;
157 }
158
159 makeSpaceForNotification(notif->height(), true, notif_idx);
160 }
161
162 QScreen* ToastNotificationsManager::activeScreen() const {
163 if (m_screen >= 0) {
164 auto all_screens = QGuiApplication::screens();
165
166 if (m_screen < all_screens.size()) {
167 return all_screens.at(m_screen);
168 }
169 }
170
171 return QGuiApplication::primaryScreen();
172 }
173
174 QPoint ToastNotificationsManager::cornerForNewNotification(QRect screen_rect) {
175 switch (m_position) {
176 case ToastNotificationsManager::TopLeft:
177 return screen_rect.topLeft() + QPoint(m_margins, m_margins);
178
179 case ToastNotificationsManager::TopRight:
180 return screen_rect.topRight() - QPoint(m_margins, -m_margins);
181
182 case ToastNotificationsManager::BottomLeft:
183 return screen_rect.bottomLeft() - QPoint(-m_margins, m_margins);
184
185 case ToastNotificationsManager::BottomRight:
186 default:
187 return screen_rect.bottomRight() - QPoint(m_margins, m_margins);
188 }
189 }
190
191 void ToastNotificationsManager::initializeArticleListNotification() {
192 m_articleListNotification = new ArticleListNotification();
193 hookNotification(m_articleListNotification);
194
195 connect(m_articleListNotification,
196 &ArticleListNotification::openingArticleInArticleListRequested,
197 this,
198 &ToastNotificationsManager::openingArticleInArticleListRequested);
199
200 connect(m_articleListNotification,
201 &ArticleListNotification::reloadMessageListRequested,
202 this,
203 &ToastNotificationsManager::reloadMessageListRequested);
204 }
205
206 void ToastNotificationsManager::hookNotification(BaseToastNotification* notif) {
207 connect(notif, &BaseToastNotification::closeRequested, this, [this](BaseToastNotification* notif) {
208 closeNotification(notif, notif != m_articleListNotification);
209 });
210 }
211
212 void ToastNotificationsManager::moveNotificationToCorner(BaseToastNotification* notif, QPoint corner) {
213 switch (m_position) {
214 case ToastNotificationsManager::TopLeft:
215 notif->move(corner);
216 break;
217
218 case ToastNotificationsManager::TopRight:
219 notif->move(corner.x() - notif->frameGeometry().width(), corner.y());
220 break;
221
222 case ToastNotificationsManager::BottomLeft:
223 notif->move(corner.x(), corner.y() - notif->frameGeometry().height());
224 break;
225
226 case ToastNotificationsManager::BottomRight:
227 notif->move(corner.x() - notif->frameGeometry().width(), corner.y() - notif->frameGeometry().height());
228 break;
229 }
230 }
231
232 void ToastNotificationsManager::makeSpaceForNotification(int height_to_make_space, bool reverse, int stard_idx) {
233 for (int i = stard_idx; i < m_activeNotifications.size(); i++) {
234 BaseToastNotification* notif = m_activeNotifications.at(i);
235
236 switch (m_position) {
237 case ToastNotificationsManager::TopLeft:
238 case ToastNotificationsManager::TopRight: {
239 std::function<int(int, int)> shift_down;
240
241 if (reverse) {
242 shift_down = [](int x, int y) {
243 return x - y;
244 };
245 }
246 else {
247 shift_down = [](int x, int y) {
248 return x + y;
249 };
250 }
251
252 // Move it all down.
253 notif->move(notif->pos().x(), shift_down(notif->pos().y(), (height_to_make_space + m_margins)));
254 break;
255 }
256
257 case ToastNotificationsManager::BottomLeft:
258 case ToastNotificationsManager::BottomRight: {
259 std::function<int(int, int)> shift_up;
260
261 if (reverse) {
262 shift_up = [](int x, int y) {
263 return x + y;
264 };
265 }
266 else {
267 shift_up = [](int x, int y) {
268 return x - y;
269 };
270 }
271
272 // Move it all up.
273 notif->move(notif->pos().x(), shift_up(notif->pos().y(), height_to_make_space + m_margins));
274 break;
275 }
276 }
277 }
278 }
279
280 void ToastNotificationsManager::removeOutOfBoundsNotifications(int height_to_reserve) {
281 auto* screen = activeScreen();
282
283 int available_height = screen->availableSize().height();
284
285 while (boolinq::from(m_activeNotifications).sum([this](BaseToastNotification* notif) {
286 return notif->height() + m_margins;
287 }) + height_to_reserve >
288 available_height) {
289 if (!m_activeNotifications.isEmpty()) {
290 m_activeNotifications.takeLast()->deleteLater();
291 }
292 else {
293 break;
294 }
295 }
296 }
297
298 QScreen* ToastNotificationsManager::moveToProperScreen(BaseToastNotification* notif) {
299 if (m_screen >= 0) {
300 auto all_screens = QGuiApplication::screens();
301
302 if (m_screen < all_screens.size()) {
303 notif->windowHandle()->setScreen(all_screens.at(m_screen));
304
305 return all_screens.at(m_screen);
306 }
307 }
308
309 return QGuiApplication::primaryScreen();
310 }