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-13 20:10:49 +03:00
|
|
|
#include "Cache.h"
|
|
|
|
#include "EventAccessors.h"
|
|
|
|
#include "MatrixClient.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
#include <mtx/responses.hpp>
|
|
|
|
|
2018-07-01 00:23:16 +03:00
|
|
|
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-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-02-13 20:10:49 +03:00
|
|
|
NotificationsManager::postNotification(const mtx::responses::Notification ¬ification,
|
|
|
|
const QImage &icon)
|
2018-05-05 22:40:24 +03:00
|
|
|
{
|
2018-07-14 16:27:51 +03:00
|
|
|
Q_UNUSED(icon)
|
|
|
|
|
2021-02-13 20:10:49 +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 text = utils::event_body(notification.event);
|
|
|
|
|
2018-07-01 00:23:16 +03:00
|
|
|
if (!isInitialized)
|
|
|
|
init();
|
|
|
|
|
|
|
|
auto templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
|
2018-07-14 16:27:51 +03:00
|
|
|
if (room_name != sender)
|
|
|
|
templ.setTextField(QString("%1 - %2").arg(sender).arg(room_name).toStdWString(),
|
2018-07-01 00:23:16 +03:00
|
|
|
WinToastTemplate::FirstLine);
|
|
|
|
else
|
2018-07-14 16:27:51 +03:00
|
|
|
templ.setTextField(QString("%1").arg(sender).toStdWString(),
|
2018-07-01 00:23:16 +03:00
|
|
|
WinToastTemplate::FirstLine);
|
2021-02-13 20:10:49 +03:00
|
|
|
if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote)
|
2021-02-12 19:28:41 +03:00
|
|
|
templ.setTextField(
|
|
|
|
QString("* ").append(sender).append(" ").append(text).toStdWString(),
|
|
|
|
WinToastTemplate::SecondLine);
|
|
|
|
else
|
|
|
|
templ.setTextField(QString("%1").arg(text).toStdWString(),
|
|
|
|
WinToastTemplate::SecondLine);
|
2018-07-01 00:23:16 +03:00
|
|
|
// TODO: implement room or user avatar
|
|
|
|
// templ.setImagePath(L"C:/example.png");
|
|
|
|
|
|
|
|
WinToast::instance()->showToast(templ, new CustomHandler());
|
2018-05-05 22:40:24 +03:00
|
|
|
}
|
2018-07-11 17:33:02 +03:00
|
|
|
|
2018-07-14 16:27:51 +03:00
|
|
|
void NotificationsManager::actionInvoked(uint, QString) {}
|
2021-01-07 13:21:10 +03:00
|
|
|
void NotificationsManager::notificationReplied(uint, QString) {}
|
2018-07-11 17:33:02 +03:00
|
|
|
|
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
|
|
|
{}
|