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
|
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "notifications/Manager.h"
#include "wintoastlib.h"
#include <QRegularExpression>
#include <QStandardPaths>
#include <QTextDocumentFragment>
#include <variant>
#include "Cache.h"
#include "EventAccessors.h"
#include "MxcImageProvider.h"
#include "Utils.h"
using namespace WinToastLib;
class CustomHandler : public IWinToastHandler
{
public:
CustomHandler(NotificationsManager *manager_, const QString &roomid_, const QString &eventid_)
: manager(manager_)
, roomid(roomid_)
, eventid(eventid_)
{
}
void toastActivated() const { manager->notificationClicked(roomid, eventid); }
void toastActivated(int) const {}
void toastFailed() const { std::wcout << L"Error showing current toast" << std::endl; }
void toastDismissed(WinToastDismissalReason) const {}
NotificationsManager *manager;
QString roomid;
QString eventid;
};
namespace {
bool isInitialized = false;
void
init()
{
isInitialized = true;
WinToast::instance()->setAppUserModelId(
WinToast::configureAUMI(L"NhekoReborn", L"in.nheko.Nheko"));
WinToast::instance()->setAppName(L"Nheko");
if (!WinToast::instance()->initialize())
std::wcout << "Your system is not compatible with toast notifications\n";
}
}
NotificationsManager::NotificationsManager(QObject *parent)
: QObject(parent)
{
}
void
NotificationsManager::postNotification(const mtx::responses::Notification ¬ification,
const QImage &icon)
{
const auto room_name = QString::fromStdString(cache::singleRoomInfo(notification.room_id).name);
auto roomid = QString::fromStdString(notification.room_id);
auto eventid = QString::fromStdString(mtx::accessors::event_id(notification.event));
auto formatNotification = [this, notification] {
const auto template_ = getMessageTemplate(notification);
if (std::holds_alternative<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
notification.event) ||
!template_.contains("%2")) {
return template_;
}
return template_.arg(utils::stripReplyFallbacks(notification.event, {}, {}).quoted_body);
}();
auto iconPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + room_name +
"-room-avatar.png";
if (!icon.save(iconPath))
iconPath.clear();
if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image ||
mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image) {
MxcImageProvider::download(
QString::fromStdString(mtx::accessors::url(notification.event))
.remove(QStringLiteral("mxc://")),
QSize(200, 80),
[this, roomid, eventid, room_name, formatNotification, iconPath](
QString, QSize, QImage, QString imgPath) {
if (imgPath.isEmpty())
systemPostNotification(
roomid, eventid, room_name, formatNotification, iconPath, "");
else
systemPostNotification(
roomid, eventid, room_name, formatNotification, iconPath, imgPath);
});
} else {
systemPostNotification(roomid, eventid, room_name, formatNotification, iconPath, "");
}
}
void
NotificationsManager::systemPostNotification(const QString &roomid,
const QString &eventid,
const QString &line1,
const QString &line2,
const QString &iconPath,
const QString &bodyImagePath)
{
if (!isInitialized)
init();
auto templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
templ.setTextField(line1.toStdWString(), WinToastTemplate::FirstLine);
templ.setTextField(line2.toStdWString(), WinToastTemplate::SecondLine);
if (!iconPath.isNull())
templ.setImagePath(iconPath.toStdWString());
if (!bodyImagePath.isNull())
templ.setHeroImagePath(bodyImagePath.toStdWString(), true);
templ.setAudioPath(WinToastTemplate::IM);
WinToast::instance()->showToast(templ, new CustomHandler(this, roomid, eventid));
}
// clang-format off
// clang-format < 12 is buggy on this
void
NotificationsManager::actionInvoked(uint, QString)
{}
void
NotificationsManager::activationToken(uint, QString)
{}
void
NotificationsManager::notificationReplied(uint, QString)
{}
void
NotificationsManager::notificationClosed(uint, uint)
{}
void
NotificationsManager::removeNotification(const QString &, const QString &)
{}
|