Debian Codegraph @

src/notifications/ManagerWin.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "notifications/Manager.h"
6 #include "wintoastlib.h"
7
8 #include <QRegularExpression>
9 #include <QStandardPaths>
10 #include <QTextDocumentFragment>
11
12 #include <variant>
13
14 #include "Cache.h"
15 #include "EventAccessors.h"
16 #include "MxcImageProvider.h"
17 #include "Utils.h"
18
19 using namespace WinToastLib;
20
21 class CustomHandler : public IWinToastHandler
22 {
23 public:
24 CustomHandler(NotificationsManager *manager_, const QString &roomid_, const QString &eventid_)
25 : manager(manager_)
26 , roomid(roomid_)
27 , eventid(eventid_)
28 {
29 }
30
31 void toastActivated() const { manager->notificationClicked(roomid, eventid); }
32 void toastActivated(int) const {}
33 void toastFailed() const { std::wcout << L"Error showing current toast" << std::endl; }
34 void toastDismissed(WinToastDismissalReason) const {}
35
36 NotificationsManager *manager;
37 QString roomid;
38 QString eventid;
39 };
40
41 namespace {
42 bool isInitialized = false;
43
44 void
45 init()
46 {
47 isInitialized = true;
48
49 WinToast::instance()->setAppUserModelId(
50 WinToast::configureAUMI(L"NhekoReborn", L"in.nheko.Nheko"));
51 WinToast::instance()->setAppName(L"Nheko");
52 if (!WinToast::instance()->initialize())
53 std::wcout << "Your system is not compatible with toast notifications\n";
54 }
55 }
56
57 NotificationsManager::NotificationsManager(QObject *parent)
58 : QObject(parent)
59 {
60 }
61
62 void
63 NotificationsManager::postNotification(const mtx::responses::Notification &notification,
64 const QImage &icon)
65 {
66 const auto room_name = QString::fromStdString(cache::singleRoomInfo(notification.room_id).name);
67 auto roomid = QString::fromStdString(notification.room_id);
68 auto eventid = QString::fromStdString(mtx::accessors::event_id(notification.event));
69
70 auto formatNotification = [this, notification] {
71 const auto template_ = getMessageTemplate(notification);
72 if (std::holds_alternative<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
73 notification.event) ||
74 !template_.contains("%2")) {
75 return template_;
76 }
77
78 return template_.arg(utils::stripReplyFallbacks(notification.event, {}, {}).quoted_body);
79 }();
80
81 auto iconPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + room_name +
82 "-room-avatar.png";
83 if (!icon.save(iconPath))
84 iconPath.clear();
85
86 if (allowShowingImages(notification) &&
87 (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image ||
88 mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image)) {
89 MxcImageProvider::download(
90 QString::fromStdString(mtx::accessors::url(notification.event))
91 .remove(QStringLiteral("mxc://")),
92 QSize(200, 80),
93 [this, roomid, eventid, room_name, formatNotification, iconPath](
94 QString, QSize, QImage, QString imgPath) {
95 if (imgPath.isEmpty())
96 systemPostNotification(
97 roomid, eventid, room_name, formatNotification, iconPath, "");
98 else
99 systemPostNotification(
100 roomid, eventid, room_name, formatNotification, iconPath, imgPath);
101 });
102 } else {
103 systemPostNotification(roomid, eventid, room_name, formatNotification, iconPath, "");
104 }
105 }
106
107 void
108 NotificationsManager::systemPostNotification(const QString &roomid,
109 const QString &eventid,
110 const QString &line1,
111 const QString &line2,
112 const QString &iconPath,
113 const QString &bodyImagePath)
114 {
115 if (!isInitialized)
116 init();
117
118 auto templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
119 templ.setTextField(line1.toStdWString(), WinToastTemplate::FirstLine);
120 templ.setTextField(line2.toStdWString(), WinToastTemplate::SecondLine);
121
122 if (!iconPath.isNull())
123 templ.setImagePath(iconPath.toStdWString());
124 if (!bodyImagePath.isNull())
125 templ.setHeroImagePath(bodyImagePath.toStdWString(), true);
126
127 templ.setAudioPath(WinToastTemplate::IM);
128
129 WinToast::instance()->showToast(templ, new CustomHandler(this, roomid, eventid));
130 }
131
132 // clang-format off
133 // clang-format < 12 is buggy on this
134 void
135 NotificationsManager::actionInvoked(uint, QString)
136 {}
137
138 void
139 NotificationsManager::activationToken(uint, QString)
140 {}
141
142 void
143 NotificationsManager::notificationReplied(uint, QString)
144 {}
145
146 void
147 NotificationsManager::notificationClosed(uint, uint)
148 {}
149
150 void
151 NotificationsManager::removeNotification(const QString &, const QString &)
152 {}