From 6339fd78a2bcf641094894c64bd95631c83d1da8 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Sat, 20 Oct 2018 13:28:51 +0800 Subject: [PATCH 1/3] Change structure. --- src/notifications/manager.h | 2 +- src/notifications/managerwin.cpp | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/notifications/manager.h b/src/notifications/manager.h index bdc06d4..653a0e6 100644 --- a/src/notifications/manager.h +++ b/src/notifications/manager.h @@ -39,7 +39,7 @@ class NotificationsManager : public QObject { // these slots are platform specific (D-Bus only) // but Qt slot declarations can not be inside an ifdef! - private slots: + public slots: void actionInvoked(uint id, QString action); void notificationClosed(uint id, uint reason); }; diff --git a/src/notifications/managerwin.cpp b/src/notifications/managerwin.cpp index 87b8bd2..2c98694 100644 --- a/src/notifications/managerwin.cpp +++ b/src/notifications/managerwin.cpp @@ -3,23 +3,26 @@ using namespace WinToastLib; -class CustomHandler : public QObject, public IWinToastHandler { - Q_OBJECT +class CustomHandler : public IWinToastHandler { public: - void toastActivated() { emit activated(notificationID); } - void toastActivated(int) { emit activated(notificationID); } + CustomHandler(NotificationsManager* parent) : notificationsManager(parent) {} + void toastActivated() { + notificationsManager->actionInvoked(notificationID, ""); + } + void toastActivated(int) { + notificationsManager->actionInvoked(notificationID, ""); + } void toastFailed() { std::wcout << L"Error showing current toast" << std::endl; } void toastDismissed(WinToastDismissalReason) { - emit dismissed(notificationID); + notificationsManager->notificationClosed(notificationID, 0); } uint notificationID; - signals: - void activated(uint id); - void dismissed(uint id); +private: + NotificationsManager* notificationsManager; }; namespace { @@ -61,14 +64,10 @@ void NotificationsManager::postNotification( // TODO: implement room or user avatar // templ.setImagePath(L"C:/example.png"); - CustomHandler *customHandler = new CustomHandler(); + CustomHandler *customHandler = new CustomHandler(this); count++; customHandler->notificationID = count; notificationIds[count] = roomEventId{room_id, event_id}; - connect(customHandler, &CustomHandler::activated, this, - [=](uint id) { this->actionInvoked(id, ""); }); - connect(customHandler, &CustomHandler::dismissed, this, - [=](uint id) { this->notificationClosed(id, 0); }); WinToast::instance()->showToast(templ, customHandler); } From 2e4fed72fe6e08a777285730ba0703534320b731 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Sat, 20 Oct 2018 14:59:37 +0800 Subject: [PATCH 2/3] Init Windows notification image. --- imports/Spectral/Page/Room.qml | 2 +- src/controller.cpp | 5 +++-- src/controller.h | 2 +- src/notifications/manager.h | 3 ++- src/notifications/managerlinux.cpp | 2 +- src/notifications/managermac.mm | 4 +++- src/notifications/managerwin.cpp | 24 ++++++++++++------------ src/roomlistmodel.cpp | 28 +++++++++++++++++----------- src/roomlistmodel.h | 2 +- 9 files changed, 41 insertions(+), 31 deletions(-) diff --git a/imports/Spectral/Page/Room.qml b/imports/Spectral/Page/Room.qml index dbcda44..f87ba95 100644 --- a/imports/Spectral/Page/Room.qml +++ b/imports/Spectral/Page/Room.qml @@ -1,5 +1,5 @@ import QtQuick 2.9 RoomForm { - roomListModel.onNewMessage: if (!window.visible) spectralController.postNotification(roomId, eventId, roomName, senderName, text, icon) + roomListModel.onNewMessage: if (!window.visible) spectralController.postNotification(roomId, eventId, roomName, senderName, text, icon, iconPath) } diff --git a/src/controller.cpp b/src/controller.cpp index 8d83a65..3b10d1b 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -244,7 +244,8 @@ void Controller::setColor(QString userId, QColor newColor) { void Controller::postNotification(const QString& roomId, const QString& eventId, const QString& roomName, const QString& senderName, - const QString& text, const QImage& icon) { + const QString& text, const QImage& icon, + const QUrl& iconPath) { notificationsManager.postNotification(roomId, eventId, roomName, senderName, - text, icon); + text, icon, iconPath); } diff --git a/src/controller.h b/src/controller.h index 95ff024..18d2532 100644 --- a/src/controller.h +++ b/src/controller.h @@ -73,7 +73,7 @@ class Controller : public QObject { void playAudio(QUrl localFile); void postNotification(const QString& roomId, const QString& eventId, const QString& roomName, const QString& senderName, - const QString& text, const QImage& icon); + const QString& text, const QImage& icon, const QUrl& iconPath); static QImage safeImage(QImage image); }; diff --git a/src/notifications/manager.h b/src/notifications/manager.h index 653a0e6..b55eac2 100644 --- a/src/notifications/manager.h +++ b/src/notifications/manager.h @@ -4,6 +4,7 @@ #include #include #include +#include #if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) #include @@ -22,7 +23,7 @@ class NotificationsManager : public QObject { void postNotification(const QString &roomId, const QString &eventId, const QString &roomName, const QString &senderName, - const QString &text, const QImage &icon); + const QString &text, const QImage &icon, const QUrl &iconPath); signals: void notificationClicked(const QString roomId, const QString eventId); diff --git a/src/notifications/managerlinux.cpp b/src/notifications/managerlinux.cpp index 368cd02..5312cc4 100644 --- a/src/notifications/managerlinux.cpp +++ b/src/notifications/managerlinux.cpp @@ -25,7 +25,7 @@ NotificationsManager::NotificationsManager(QObject *parent) void NotificationsManager::postNotification( const QString &roomid, const QString &eventid, const QString &roomname, - const QString &sender, const QString &text, const QImage &icon) { + const QString &sender, const QString &text, const QImage &icon, const QUrl &iconPath) { uint id = showNotification(roomname, sender + ": " + text, icon); notificationIds[id] = roomEventId{roomid, eventid}; } diff --git a/src/notifications/managermac.mm b/src/notifications/managermac.mm index 782be1f..2ddc939 100644 --- a/src/notifications/managermac.mm +++ b/src/notifications/managermac.mm @@ -19,11 +19,13 @@ NotificationsManager::postNotification( const QString &roomName, const QString &senderName, const QString &text, - const QImage &icon) + const QImage &icon, + const QUrl &iconPath) { Q_UNUSED(roomId); Q_UNUSED(eventId); Q_UNUSED(icon); + Q_UNUSED(iconPath); NSUserNotification * notif = [[NSUserNotification alloc] init]; diff --git a/src/notifications/managerwin.cpp b/src/notifications/managerwin.cpp index 2c98694..ad1acd9 100644 --- a/src/notifications/managerwin.cpp +++ b/src/notifications/managerwin.cpp @@ -5,24 +5,24 @@ using namespace WinToastLib; class CustomHandler : public IWinToastHandler { public: - CustomHandler(NotificationsManager* parent) : notificationsManager(parent) {} + CustomHandler(uint id, NotificationsManager *parent) + : notificationID(id), notificationsManager(parent) {} void toastActivated() { - notificationsManager->actionInvoked(notificationID, ""); + notificationsManager->actionInvoked(notificationID, ""); } void toastActivated(int) { - notificationsManager->actionInvoked(notificationID, ""); + notificationsManager->actionInvoked(notificationID, ""); } void toastFailed() { std::wcout << L"Error showing current toast" << std::endl; } void toastDismissed(WinToastDismissalReason) { - notificationsManager->notificationClosed(notificationID, 0); + notificationsManager->notificationClosed(notificationID, 0); } + private: uint notificationID; - -private: - NotificationsManager* notificationsManager; + NotificationsManager *notificationsManager; }; namespace { @@ -44,7 +44,8 @@ NotificationsManager::NotificationsManager(QObject *parent) : QObject(parent) {} void NotificationsManager::postNotification( const QString &room_id, const QString &event_id, const QString &room_name, - const QString &sender, const QString &text, const QImage &icon) { + const QString &sender, const QString &text, const QImage &icon, + const QUrl &iconPath) { Q_UNUSED(room_id) Q_UNUSED(event_id) Q_UNUSED(icon) @@ -61,12 +62,11 @@ void NotificationsManager::postNotification( WinToastTemplate::FirstLine); templ.setTextField(QString("%1").arg(text).toStdWString(), WinToastTemplate::SecondLine); - // TODO: implement room or user avatar - // templ.setImagePath(L"C:/example.png"); + templ.setImagePath( + reinterpret_cast(iconPath.toLocalFile().utf16())); - CustomHandler *customHandler = new CustomHandler(this); count++; - customHandler->notificationID = count; + CustomHandler *customHandler = new CustomHandler(count, this); notificationIds[count] = roomEventId{room_id, event_id}; WinToast::instance()->showToast(templ, customHandler); diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index 1c1ec87..dd47894 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -4,6 +4,7 @@ #include "events/roomevent.h" +#include #include #include #include @@ -74,17 +75,22 @@ void RoomListModel::connectRoomSignals(SpectralRoom* room) { [=] { refresh(room, {AvatarRole}); }); connect(room, &Room::addedMessages, this, [=] { refresh(room, {LastEventRole}); }); - connect(room, &Room::aboutToAddNewMessages, this, - [=](QMatrixClient::RoomEventsRange eventsRange) { - RoomEvent* event = (eventsRange.end() - 1)->get(); - if (event->isStateEvent()) return; - User* sender = room->user(event->senderId()); - if (sender == room->localUser()) return; - emit newMessage(room->id(), event->id(), room->displayName(), - sender->displayname(), - event->contentJson().value("body").toString(), - room->avatar(128)); - }); + connect( + room, &Room::aboutToAddNewMessages, this, + [=](QMatrixClient::RoomEventsRange eventsRange) { + RoomEvent* event = (eventsRange.end() - 1)->get(); + if (event->isStateEvent()) return; + User* sender = room->user(event->senderId()); + if (sender == room->localUser()) return; + QUrl _url = room->avatarUrl(); + qDebug() << QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + + "/avatar/" + _url.authority() + '_' + _url.fileName() + ".png"; + emit newMessage( + room->id(), event->id(), room->displayName(), sender->displayname(), + event->contentJson().value("body").toString(), room->avatar(128), + QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + + "/avatar/" + _url.authority() + '_' + _url.fileName() + ".png"); + }); } void RoomListModel::updateRoom(Room* room, Room* prev) { diff --git a/src/roomlistmodel.h b/src/roomlistmodel.h index 9da486d..40b0f04 100644 --- a/src/roomlistmodel.h +++ b/src/roomlistmodel.h @@ -76,7 +76,7 @@ class RoomListModel : public QAbstractListModel { void roomAdded(SpectralRoom* room); void newMessage(const QString& roomId, const QString& eventId, const QString& roomName, const QString& senderName, - const QString& text, const QImage& icon); + const QString& text, const QImage& icon, const QUrl& iconPath); }; #endif // ROOMLISTMODEL_H From f53bdaae89cfca07c23f3cc731da76b1753ae51f Mon Sep 17 00:00:00 2001 From: Black Hat Date: Sat, 20 Oct 2018 15:25:17 +0800 Subject: [PATCH 3/3] Fix separator. --- src/notifications/managerwin.cpp | 5 ++++- src/roomlistmodel.cpp | 6 ++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/notifications/managerwin.cpp b/src/notifications/managerwin.cpp index ad1acd9..70d943c 100644 --- a/src/notifications/managerwin.cpp +++ b/src/notifications/managerwin.cpp @@ -1,6 +1,8 @@ #include "manager.h" #include "wintoastlib.h" +#include + using namespace WinToastLib; class CustomHandler : public IWinToastHandler { @@ -62,8 +64,9 @@ void NotificationsManager::postNotification( WinToastTemplate::FirstLine); templ.setTextField(QString("%1").arg(text).toStdWString(), WinToastTemplate::SecondLine); + templ.setImagePath( - reinterpret_cast(iconPath.toLocalFile().utf16())); + reinterpret_cast(QDir::toNativeSeparators(iconPath.toLocalFile()).utf16())); count++; CustomHandler *customHandler = new CustomHandler(count, this); diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index dd47894..a6f39cd 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -83,13 +83,11 @@ void RoomListModel::connectRoomSignals(SpectralRoom* room) { User* sender = room->user(event->senderId()); if (sender == room->localUser()) return; QUrl _url = room->avatarUrl(); - qDebug() << QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + - "/avatar/" + _url.authority() + '_' + _url.fileName() + ".png"; emit newMessage( room->id(), event->id(), room->displayName(), sender->displayname(), event->contentJson().value("body").toString(), room->avatar(128), - QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + - "/avatar/" + _url.authority() + '_' + _url.fileName() + ".png"); + QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + + "/avatar/" + _url.authority() + '_' + _url.fileName() + ".png")); }); }