From 32d6742647d5b5df04604bdcc13e07a451bca966 Mon Sep 17 00:00:00 2001 From: Joseph Donofry <joedonofry@gmail.com> Date: Fri, 28 Oct 2022 14:16:47 -0400 Subject: [PATCH] Add implementation of replies via notification on macOS --- CMakeLists.txt | 4 ++-- src/ChatPage.cpp | 24 ++++++++++++-------- src/ChatPage.h | 1 + src/notifications/MacNotificationDelegate.h | 7 +++++- src/notifications/MacNotificationDelegate.mm | 20 ++++++++++++++-- src/notifications/ManagerMac.mm | 9 +++++++- src/notifications/NotificationManagerProxy.h | 19 ++++++++++++++++ 7 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 src/notifications/NotificationManagerProxy.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8818fcaea..7732a7357 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -629,9 +629,9 @@ set(TRANSLATION_DEPS ${LANG_QRC} ${QRC} ${QM_SRC}) if (APPLE) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation -framework Cocoa -framework UserNotifications") - set(SRC_FILES ${SRC_FILES} src/notifications/MacNotificationDelegate.h src/notifications/MacNotificationDelegate.mm src/notifications/ManagerMac.mm src/notifications/ManagerMac.cpp src/emoji/MacHelper.mm src/emoji/MacHelper.h) + set(SRC_FILES ${SRC_FILES} src/notifications/NotificationManagerProxy.h src/notifications/MacNotificationDelegate.h src/notifications/MacNotificationDelegate.mm src/notifications/ManagerMac.mm src/notifications/ManagerMac.cpp src/emoji/MacHelper.mm src/emoji/MacHelper.h) if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0") - set_source_files_properties( src/notifications/MacNotificationDelegate.h src/notifications/MacNotificationDelegate.mm src/notifications/ManagerMac.mm src/emoji/MacHelper.mm src/emoji/MacHelper.h PROPERTIES SKIP_PRECOMPILE_HEADERS ON) + set_source_files_properties( src/notifications/NotificationManagerProxy.h src/notifications/MacNotificationDelegate.h src/notifications/MacNotificationDelegate.mm src/notifications/ManagerMac.mm src/emoji/MacHelper.mm src/emoji/MacHelper.h PROPERTIES SKIP_PRECOMPILE_HEADERS ON) endif() elseif (WIN32) file(DOWNLOAD diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index e40274cb8..f87c27380 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -152,16 +152,7 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QObject *parent) connect(notificationsManager, &NotificationsManager::sendNotificationReply, this, - [this](const QString &roomid, const QString &eventid, const QString &body) { - view_manager_->queueReply(roomid, eventid, body); - auto exWin = MainWindow::instance()->windowForRoom(roomid); - if (exWin) { - exWin->requestActivate(); - } else { - view_manager_->rooms()->setCurrentRoom(roomid); - MainWindow::instance()->requestActivate(); - } - }); + &ChatPage::sendNotificationReply); connect( this, @@ -1583,6 +1574,19 @@ ChatPage::handleMatrixUri(QString uri) return false; } +void +ChatPage::sendNotificationReply(const QString &roomid, const QString &eventid, const QString &body) +{ + view_manager_->queueReply(roomid, eventid, body); + auto exWin = MainWindow::instance()->windowForRoom(roomid); + if (exWin) { + exWin->requestActivate(); + } else { + view_manager_->rooms()->setCurrentRoom(roomid); + MainWindow::instance()->requestActivate(); + } +} + bool ChatPage::handleMatrixUri(const QUrl &uri) { diff --git a/src/ChatPage.h b/src/ChatPage.h index 1bb25dc20..bae4401f8 100644 --- a/src/ChatPage.h +++ b/src/ChatPage.h @@ -105,6 +105,7 @@ public slots: void receivedSessionKey(const std::string &room_id, const std::string &session_id); void decryptDownloadedSecrets(mtx::secret_storage::AesHmacSha2KeyDescription keyDesc, const SecretsToDecrypt &secrets); + void sendNotificationReply(const QString &roomid, const QString &eventid, const QString &body); signals: void connectionLost(); void connectionRestored(); diff --git a/src/notifications/MacNotificationDelegate.h b/src/notifications/MacNotificationDelegate.h index 448ca2cb5..4a5c66d00 100644 --- a/src/notifications/MacNotificationDelegate.h +++ b/src/notifications/MacNotificationDelegate.h @@ -6,10 +6,15 @@ #pragma once #include "notifications/Manager.h" +#include "notifications/NotificationManagerProxy.h" #include <mtx/responses/notifications.hpp> #import <Foundation/Foundation.h> #import <UserNotifications/UserNotifications.h> -@interface MacNotificationDelegate : NSObject <UNUserNotificationCenterDelegate> +@interface MacNotificationDelegate : NSObject <UNUserNotificationCenterDelegate> { + std::unique_ptr<NotificationManagerProxy> mProxy; +} + +- (id)initWithProxy:(std::unique_ptr<NotificationManagerProxy>&&)proxy; @end diff --git a/src/notifications/MacNotificationDelegate.mm b/src/notifications/MacNotificationDelegate.mm index b1969386b..9047efe3f 100644 --- a/src/notifications/MacNotificationDelegate.mm +++ b/src/notifications/MacNotificationDelegate.mm @@ -5,16 +5,32 @@ #import "notifications/MacNotificationDelegate.h" +#include <QString.h> + +#include "ChatPage.h" + @implementation MacNotificationDelegate +- (id)initWithProxy: (std::unique_ptr<NotificationManagerProxy>&&)proxy +{ + if(self = [super init]) { + mProxy = std::move(proxy); + } + + return self; +} + - (void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void (^)())completionHandler { if ([response.actionIdentifier isEqualToString:@"ReplyAction"]) { if ([response respondsToSelector:@selector(userText)]) { - NSString* textValue = [(UNTextInputNotificationResponse*)response userText]; - NSLog(@"%@", textValue); + UNTextInputNotificationResponse* textResponse = (UNTextInputNotificationResponse*)response; + NSString* textValue = [textResponse userText]; + NSString* eventId = [[[textResponse notification] request] identifier]; + NSString* roomId = [[[[textResponse notification] request] content] threadIdentifier]; + mProxy->notificationReplied(QString::fromNSString(roomId), QString::fromNSString(eventId), QString::fromNSString(textValue)); } } completionHandler(); diff --git a/src/notifications/ManagerMac.mm b/src/notifications/ManagerMac.mm index ea910dd02..4865e30d3 100644 --- a/src/notifications/ManagerMac.mm +++ b/src/notifications/ManagerMac.mm @@ -3,9 +3,12 @@ // // SPDX-License-Identifier: GPL-3.0-or-later +#include "notifications/NotificationManagerProxy.h" #include "notifications/MacNotificationDelegate.h" #include "notifications/Manager.h" +#include "ChatPage.h" + #import <AppKit/NSImage.h> #import <Foundation/Foundation.h> #import <UserNotifications/UserNotifications.h> @@ -165,7 +168,11 @@ void NotificationsManager::attachToMacNotifCenter() UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; - MacNotificationDelegate* notifDelegate = [MacNotificationDelegate new]; + std::unique_ptr<NotificationManagerProxy> proxy = std::make_unique<NotificationManagerProxy>(); + + connect(proxy.get(), &NotificationManagerProxy::notificationReplied, ChatPage::instance(), &ChatPage::sendNotificationReply); + + MacNotificationDelegate* notifDelegate = [[MacNotificationDelegate alloc] initWithProxy:std::move(proxy)]; center.delegate = notifDelegate; } diff --git a/src/notifications/NotificationManagerProxy.h b/src/notifications/NotificationManagerProxy.h new file mode 100644 index 000000000..0341519df --- /dev/null +++ b/src/notifications/NotificationManagerProxy.h @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: 2021 Nheko Contributors +// SPDX-FileCopyrightText: 2022 Nheko Contributors +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include <QObject> +#include <QString> + +class NotificationManagerProxy final : public QObject +{ + Q_OBJECT +public: + NotificationManagerProxy(QObject *parent = nullptr): QObject(parent) {} + +signals: + void notificationReplied(const QString &room, const QString &event, const QString &reply); +}; \ No newline at end of file -- GitLab