matrixion/src/notifications/ManagerWin.cpp

128 lines
4.4 KiB
C++
Raw Normal View History

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"
#include "wintoastlib.h"
#include <QRegularExpression>
#include <QStandardPaths>
#include <QTextDocumentFragment>
2021-02-26 23:33:27 +03:00
#include <variant>
#include "Cache.h"
#include "EventAccessors.h"
#include "Utils.h"
using namespace WinToastLib;
class CustomHandler : public IWinToastHandler
{
public:
void toastActivated() const {}
void toastActivated(int) const {}
void toastFailed() const { std::wcout << L"Error showing current toast" << std::endl; }
void toastDismissed(WinToastDismissalReason) const {}
};
namespace {
bool isInitialized = false;
void
init()
{
isInitialized = true;
WinToast::instance()->setAppName(L"Nheko");
WinToast::instance()->setAppUserModelId(WinToast::configureAUMI(L"nheko", L"nheko"));
if (!WinToast::instance()->initialize())
2021-02-13 18:58:09 +03:00
std::wcout << "Your system is not compatible with toast notifications\n";
}
}
2018-05-05 22:40:24 +03:00
2018-07-14 16:27:51 +03:00
NotificationsManager::NotificationsManager(QObject *parent)
: QObject(parent)
{}
2018-05-05 22:40:24 +03:00
void
NotificationsManager::postNotification(const mtx::responses::Notification &notification,
const QImage &icon)
{
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)));
2021-02-26 23:33:27 +03:00
const auto isEncrypted =
std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
&notification.event) != nullptr;
const auto isReply = utils::isReply(notification.event);
2021-02-26 23:38:15 +03:00
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(notification));
auto iconPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) +
room_name + "-room-avatar.png";
if (!icon.save(iconPath))
iconPath.clear();
systemPostNotification(line1, line2, iconPath);
}
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
{
if (!isInitialized)
init();
auto templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
2021-02-26 23:38:15 +03:00
templ.setTextField(line1.toStdWString(), WinToastTemplate::FirstLine);
templ.setTextField(line2.toStdWString(), WinToastTemplate::SecondLine);
if (!iconPath.isNull())
templ.setImagePath(iconPath.toStdWString());
WinToast::instance()->showToast(templ, new CustomHandler());
2018-05-05 22:40:24 +03:00
}
2018-07-14 16:27:51 +03:00
void NotificationsManager::actionInvoked(uint, QString) {}
void NotificationsManager::notificationReplied(uint, QString) {}
2018-07-14 16:27:51 +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
{}
QString
NotificationsManager::formatNotification(const mtx::responses::Notification &notification)
{
const auto sender =
cache::displayName(QString::fromStdString(notification.room_id),
QString::fromStdString(mtx::accessors::sender(notification.event)));
2021-02-26 23:33:27 +03:00
const auto messageLeadIn =
((mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
? "* " + sender + " "
: sender +
(utils::isReply(notification.event)
? tr(" replied",
"Used to denote that this message is a reply to another "
"message. Displayed as 'foo replied: message'.")
: "") +
": ");
return QTextDocumentFragment::fromHtml(
mtx::accessors::formattedBodyWithFallback(notification.event)
.replace(QRegularExpression("(<mx-reply>.+\\<\\/mx-reply\\>)"), ""))
.toPlainText()
2021-02-26 23:33:27 +03:00
.prepend(messageLeadIn);
}