From 239af6e6e741eb35c5f416c745eac91ed67138e7 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Tue, 8 Sep 2020 23:44:57 +0200 Subject: [PATCH] Sending simple text notifications, yeah! --- src/notificationmanager.cpp | 75 ++++++++++++++++++++++++------------- src/notificationmanager.h | 4 +- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/notificationmanager.cpp b/src/notificationmanager.cpp index 812ce68..75fa6a9 100644 --- a/src/notificationmanager.cpp +++ b/src/notificationmanager.cpp @@ -51,28 +51,37 @@ void NotificationManager::handleUpdateNotificationGroup(const QVariantMap notifi qDebug() << "[NotificationManager] Received notification group update, group ID:" << notificationGroupId; QVariantMap notificationGroup = this->notificationGroups.value(notificationGroupId).toMap(); + QString chatId = notificationGroupUpdate.value("chat_id").toString(); notificationGroup.insert("type", notificationGroupUpdate.value("type")); - notificationGroup.insert("chat_id", notificationGroupUpdate.value("chat_id")); + notificationGroup.insert("chat_id", chatId); + notificationGroup.insert("notification_group_id", notificationGroupId); notificationGroup.insert("notification_settings_chat_id", notificationGroupUpdate.value("notification_settings_chat_id")); notificationGroup.insert("is_silent", notificationGroupUpdate.value("is_silent")); notificationGroup.insert("total_count", notificationGroupUpdate.value("total_count")); - QVariantMap notifications = notificationGroup.value("notifications").toMap(); + QVariantMap activeNotifications = notificationGroup.value("notifications").toMap(); + + QVariantList removedNotificationIds = notificationGroupUpdate.value("removed_notification_ids").toList(); + QListIterator removedNotificationsIterator(removedNotificationIds); + while (removedNotificationsIterator.hasNext()) { + QString removedNotificationId = removedNotificationsIterator.next().toString(); + QVariantMap notificationInformation = activeNotifications.value(removedNotificationId).toMap(); + if (!notificationInformation.isEmpty()) { + this->removeNotification(notificationInformation); + activeNotifications.remove(removedNotificationId); + } + } + QVariantList addedNotifications = notificationGroupUpdate.value("added_notifications").toList(); QListIterator addedNotificationIterator(addedNotifications); while (addedNotificationIterator.hasNext()) { QVariantMap addedNotification = addedNotificationIterator.next().toMap(); - notifications.insert(addedNotification.value("id").toString(), addedNotification); + QVariantMap activeNotification = this->sendNotification(chatId, addedNotification); + activeNotifications.insert(activeNotification.value("id").toString(), activeNotification); } - QVariantList removedNotifications = notificationGroupUpdate.value("removed_notification_ids").toList(); - QListIterator removedNotificationIterator(removedNotifications); - while (removedNotificationIterator.hasNext()) { - QString removedNotificationId = removedNotificationIterator.next().toString(); - notifications.remove(removedNotificationId); - } - notificationGroup.insert("notifications", notifications); + + notificationGroup.insert("notifications", activeNotifications); this->notificationGroups.insert(notificationGroupId, notificationGroup); - // this->sendNotifications(); } void NotificationManager::handleUpdateNotification(const QVariantMap updatedNotification) @@ -88,20 +97,36 @@ void NotificationManager::handleChatDiscovered(const QString &chatId, const QVar this->chatListMutex.unlock(); } -void NotificationManager::sendNotifications() +QVariantMap NotificationManager::sendNotification(const QString &chatId, const QVariantMap ¬ificationInformation) { - QVariantList notificationGroupList = this->notificationGroups.values(); - QListIterator notificationGroupIterator(notificationGroupList); + qDebug() << "[NotificationManager] Sending notification" << notificationInformation.value("id").toString(); + + QVariantMap chatInformation = this->chatMap.value(chatId).toMap(); + + QVariantMap updatedNotificationInformation = notificationInformation; QUrl appIconUrl = SailfishApp::pathTo("images/fernschreiber-notification.png"); - while (notificationGroupIterator.hasNext()) { - QVariantMap notificationGroup = notificationGroupIterator.next().toMap(); - Notification nemoNotification; - nemoNotification.setAppName("Fernschreiber"); - nemoNotification.setAppIcon(appIconUrl.toLocalFile()); - nemoNotification.setBody("This is the body"); - nemoNotification.setSummary("This is the summary"); - nemoNotification.setPreviewSummary("This is the preview summary"); - nemoNotification.setPreviewBody("This is the preview body"); - nemoNotification.publish(); - } + Notification nemoNotification; + nemoNotification.setAppName("Fernschreiber"); + nemoNotification.setAppIcon(appIconUrl.toLocalFile()); + nemoNotification.setSummary(chatInformation.value("title").toString()); + nemoNotification.setCategory("x-nemo.messaging.im"); + nemoNotification.setBody(this->getNotificationText(notificationInformation.value("type").toMap().value("message").toMap().value("content").toMap())); + + nemoNotification.publish(); + updatedNotificationInformation.insert("replaces_id", nemoNotification.replacesId()); + return updatedNotificationInformation; +} + +void NotificationManager::removeNotification(const QVariantMap ¬ificationInformation) +{ + qDebug() << "[NotificationManager] Removing notification" << notificationInformation.value("id").toString(); + Notification nemoNotification; + nemoNotification.setReplacesId(notificationInformation.value("replaces_id").toUInt()); + nemoNotification.close(); +} + +QString NotificationManager::getNotificationText(const QVariantMap ¬ificationContent) +{ + qDebug() << "[NotificationManager] Getting notification text from content" << notificationContent; + return notificationContent.value("text").toMap().value("text").toString(); } diff --git a/src/notificationmanager.h b/src/notificationmanager.h index 3117372..59ece2b 100644 --- a/src/notificationmanager.h +++ b/src/notificationmanager.h @@ -48,7 +48,9 @@ private: QVariantMap notificationGroups; QMutex chatListMutex; - void sendNotifications(); + QVariantMap sendNotification(const QString &chatId, const QVariantMap ¬ificationInformation); + void removeNotification(const QVariantMap ¬ificationInformation); + QString getNotificationText(const QVariantMap ¬ificationContent); };