2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2018-05-05 22:40:24 +03:00
|
|
|
#include "notifications/Manager.h"
|
2018-07-01 00:23:16 +03:00
|
|
|
#include "wintoastlib.h"
|
|
|
|
|
2021-02-20 22:00:13 +03:00
|
|
|
#include <QRegularExpression>
|
2021-03-17 21:17:57 +03:00
|
|
|
#include <QStandardPaths>
|
2021-02-15 15:57:25 +03:00
|
|
|
#include <QTextDocumentFragment>
|
|
|
|
|
2021-02-26 23:33:27 +03:00
|
|
|
#include <variant>
|
|
|
|
|
2021-03-17 21:17:57 +03:00
|
|
|
#include "Cache.h"
|
2021-02-20 21:16:43 +03:00
|
|
|
#include "EventAccessors.h"
|
2021-02-13 20:10:49 +03:00
|
|
|
#include "Utils.h"
|
|
|
|
|
2018-07-01 00:23:16 +03:00
|
|
|
using namespace WinToastLib;
|
|
|
|
|
|
|
|
class CustomHandler : public IWinToastHandler
|
|
|
|
{
|
|
|
|
public:
|
2021-09-18 01:22:33 +03:00
|
|
|
void toastActivated() const {}
|
|
|
|
void toastActivated(int) const {}
|
|
|
|
void toastFailed() const { std::wcout << L"Error showing current toast" << std::endl; }
|
|
|
|
void toastDismissed(WinToastDismissalReason) const {}
|
2018-07-01 00:23:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
bool isInitialized = false;
|
|
|
|
|
|
|
|
void
|
|
|
|
init()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
isInitialized = true;
|
2018-07-01 00:23:16 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
WinToast::instance()->setAppName(L"Nheko");
|
|
|
|
WinToast::instance()->setAppUserModelId(WinToast::configureAUMI(L"nheko", L"nheko"));
|
|
|
|
if (!WinToast::instance()->initialize())
|
|
|
|
std::wcout << "Your system is not compatible with toast notifications\n";
|
2018-07-01 00:23:16 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-05 22:40:24 +03:00
|
|
|
|
2018-07-14 16:27:51 +03:00
|
|
|
NotificationsManager::NotificationsManager(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{}
|
2018-07-11 17:33:02 +03:00
|
|
|
|
2018-05-05 22:40:24 +03:00
|
|
|
void
|
2021-03-17 21:17:57 +03:00
|
|
|
NotificationsManager::postNotification(const mtx::responses::Notification ¬ification,
|
|
|
|
const QImage &icon)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
const auto room_name = QString::fromStdString(cache::singleRoomInfo(notification.room_id).name);
|
|
|
|
const auto sender =
|
|
|
|
cache::displayName(QString::fromStdString(notification.room_id),
|
|
|
|
QString::fromStdString(mtx::accessors::sender(notification.event)));
|
|
|
|
|
|
|
|
const auto isEncrypted = std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
|
|
|
|
¬ification.event) != nullptr;
|
|
|
|
const auto isReply = utils::isReply(notification.event);
|
|
|
|
|
|
|
|
auto formatNotification = [this, notification, sender] {
|
|
|
|
const auto template_ = getMessageTemplate(notification);
|
|
|
|
if (std::holds_alternative<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
|
|
|
|
notification.event)) {
|
|
|
|
return template_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return template_.arg(utils::stripReplyFallbacks(notification.event, {}, {}).quoted_body);
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto line1 =
|
|
|
|
(room_name == sender) ? sender : QString("%1 - %2").arg(sender).arg(room_name);
|
|
|
|
const auto line2 = (isEncrypted ? (isReply ? tr("%1 replied with an encrypted message")
|
|
|
|
: tr("%1 sent an encrypted message"))
|
|
|
|
: formatNotification());
|
|
|
|
|
|
|
|
auto iconPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + room_name +
|
|
|
|
"-room-avatar.png";
|
|
|
|
if (!icon.save(iconPath))
|
|
|
|
iconPath.clear();
|
|
|
|
|
|
|
|
systemPostNotification(line1, line2, iconPath);
|
2021-03-17 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-26 23:38:15 +03:00
|
|
|
NotificationsManager::systemPostNotification(const QString &line1,
|
|
|
|
const QString &line2,
|
|
|
|
const QString &iconPath)
|
2018-05-05 22:40:24 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!isInitialized)
|
|
|
|
init();
|
2018-07-01 00:23:16 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
|
|
|
|
templ.setTextField(line1.toStdWString(), WinToastTemplate::FirstLine);
|
|
|
|
templ.setTextField(line2.toStdWString(), WinToastTemplate::SecondLine);
|
2021-02-26 23:38:15 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!iconPath.isNull())
|
|
|
|
templ.setImagePath(iconPath.toStdWString());
|
2018-07-01 00:23:16 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
WinToast::instance()->showToast(templ, new CustomHandler());
|
2018-05-05 22:40:24 +03:00
|
|
|
}
|
2018-07-11 17:33:02 +03:00
|
|
|
|
2021-10-17 18:33:59 +03:00
|
|
|
void NotificationsManager::actionInvoked(uint, QString) {}
|
|
|
|
void NotificationsManager::notificationReplied(uint, QString) {}
|
2018-07-11 17:33:02 +03:00
|
|
|
|
2021-10-17 18:33:59 +03:00
|
|
|
void NotificationsManager::notificationClosed(uint, uint) {}
|
2020-04-09 21:52:50 +03:00
|
|
|
|
|
|
|
void
|
2020-04-13 18:01:57 +03:00
|
|
|
NotificationsManager::removeNotification(const QString &, const QString &)
|
2020-04-09 21:52:50 +03:00
|
|
|
{}
|