2020-09-02 23:49:15 +03:00
|
|
|
/*
|
|
|
|
Copyright (C) 2020 Sebastian J. Wolf
|
|
|
|
|
|
|
|
This file is part of Fernschreiber.
|
|
|
|
|
|
|
|
Fernschreiber is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Fernschreiber is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notificationmanager.h"
|
2020-10-03 23:58:45 +03:00
|
|
|
#include "fernschreiberutils.h"
|
2020-09-04 00:20:46 +03:00
|
|
|
#include <nemonotifications-qt5/notification.h>
|
|
|
|
#include <sailfishapp.h>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QListIterator>
|
|
|
|
#include <QUrl>
|
2020-09-13 00:22:33 +03:00
|
|
|
#include <QDateTime>
|
2020-09-13 22:54:36 +03:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusInterface>
|
2020-09-02 23:49:15 +03:00
|
|
|
|
2020-10-10 16:48:12 +03:00
|
|
|
#define LOG(x) qDebug() << "[NotificationManager]" << x
|
|
|
|
|
2020-09-02 23:49:15 +03:00
|
|
|
NotificationManager::NotificationManager(TDLibWrapper *tdLibWrapper, QObject *parent) : QObject(parent)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Initializing...");
|
2020-09-02 23:49:15 +03:00
|
|
|
this->tdLibWrapper = tdLibWrapper;
|
2020-09-13 22:54:36 +03:00
|
|
|
this->ngfClient = new Ngf::Client(this);
|
2020-09-02 23:49:15 +03:00
|
|
|
|
|
|
|
connect(this->tdLibWrapper, SIGNAL(activeNotificationsUpdated(QVariantList)), this, SLOT(handleUpdateActiveNotifications(QVariantList)));
|
|
|
|
connect(this->tdLibWrapper, SIGNAL(notificationGroupUpdated(QVariantMap)), this, SLOT(handleUpdateNotificationGroup(QVariantMap)));
|
|
|
|
connect(this->tdLibWrapper, SIGNAL(notificationUpdated(QVariantMap)), this, SLOT(handleUpdateNotification(QVariantMap)));
|
2020-09-03 00:05:09 +03:00
|
|
|
connect(this->tdLibWrapper, SIGNAL(newChatDiscovered(QString, QVariantMap)), this, SLOT(handleChatDiscovered(QString, QVariantMap)));
|
2020-09-13 22:54:36 +03:00
|
|
|
connect(this->ngfClient, SIGNAL(connectionStatus(bool)), this, SLOT(handleNgfConnectionStatus(bool)));
|
|
|
|
connect(this->ngfClient, SIGNAL(eventCompleted(quint32)), this, SLOT(handleNgfEventCompleted(quint32)));
|
|
|
|
connect(this->ngfClient, SIGNAL(eventFailed(quint32)), this, SLOT(handleNgfEventFailed(quint32)));
|
|
|
|
connect(this->ngfClient, SIGNAL(eventPlaying(quint32)), this, SLOT(handleNgfEventPlaying(quint32)));
|
|
|
|
|
|
|
|
if (this->ngfClient->connect()) {
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("NGF Client successfully initialized...");
|
2020-09-13 22:54:36 +03:00
|
|
|
} else {
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Failed to initialize NGF Client...");
|
2020-09-13 22:54:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this->controlLedNotification(false);
|
2020-09-02 23:49:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationManager::~NotificationManager()
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Destroying myself...");
|
2020-09-02 23:49:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationManager::handleUpdateActiveNotifications(const QVariantList notificationGroups)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Received active notifications, number of groups:" << notificationGroups.size());
|
2020-09-02 23:49:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationManager::handleUpdateNotificationGroup(const QVariantMap notificationGroupUpdate)
|
|
|
|
{
|
2020-09-04 00:20:46 +03:00
|
|
|
QString notificationGroupId = notificationGroupUpdate.value("notification_group_id").toString();
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Received notification group update, group ID:" << notificationGroupId);
|
2020-09-04 00:20:46 +03:00
|
|
|
QVariantMap notificationGroup = this->notificationGroups.value(notificationGroupId).toMap();
|
|
|
|
|
2020-09-09 00:44:57 +03:00
|
|
|
QString chatId = notificationGroupUpdate.value("chat_id").toString();
|
2020-09-04 00:20:46 +03:00
|
|
|
notificationGroup.insert("type", notificationGroupUpdate.value("type"));
|
2020-09-09 00:44:57 +03:00
|
|
|
notificationGroup.insert("chat_id", chatId);
|
|
|
|
notificationGroup.insert("notification_group_id", notificationGroupId);
|
2020-09-04 00:20:46 +03:00
|
|
|
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"));
|
|
|
|
|
2020-09-09 00:44:57 +03:00
|
|
|
QVariantMap activeNotifications = notificationGroup.value("notifications").toMap();
|
|
|
|
|
|
|
|
QVariantList removedNotificationIds = notificationGroupUpdate.value("removed_notification_ids").toList();
|
|
|
|
QListIterator<QVariant> 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 00:44:02 +03:00
|
|
|
// If we have deleted notifications, we need to update possibly existing ones
|
|
|
|
if (!removedNotificationIds.isEmpty() && !activeNotifications.isEmpty()) {
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Some removals happend, but we have" << activeNotifications.size() << "existing notifications.");
|
2020-09-10 00:44:02 +03:00
|
|
|
QVariantMap firstActiveNotification = activeNotifications.first().toMap();
|
|
|
|
activeNotifications.remove(firstActiveNotification.value("id").toString());
|
|
|
|
QVariantMap newFirstActiveNotification = this->sendNotification(chatId, firstActiveNotification, activeNotifications);
|
|
|
|
QVariantMap newActiveNotifications;
|
|
|
|
QListIterator<QVariant> activeNotificationsIterator(activeNotifications.values());
|
|
|
|
while (activeNotificationsIterator.hasNext()) {
|
|
|
|
QVariantMap newActiveNotification = activeNotificationsIterator.next().toMap();
|
|
|
|
newActiveNotification.insert("replaces_id", newFirstActiveNotification.value("replaces_id"));
|
|
|
|
newActiveNotifications.insert(newActiveNotification.value("id").toString(), newActiveNotification);
|
|
|
|
}
|
|
|
|
newActiveNotifications.insert(newFirstActiveNotification.value("id").toString(), newFirstActiveNotification);
|
|
|
|
activeNotifications = newActiveNotifications;
|
|
|
|
}
|
|
|
|
|
2020-09-13 22:54:36 +03:00
|
|
|
if (activeNotifications.isEmpty()) {
|
|
|
|
this->controlLedNotification(false);
|
|
|
|
}
|
|
|
|
|
2020-09-04 00:20:46 +03:00
|
|
|
QVariantList addedNotifications = notificationGroupUpdate.value("added_notifications").toList();
|
|
|
|
QListIterator<QVariant> addedNotificationIterator(addedNotifications);
|
|
|
|
while (addedNotificationIterator.hasNext()) {
|
|
|
|
QVariantMap addedNotification = addedNotificationIterator.next().toMap();
|
2020-09-10 00:44:02 +03:00
|
|
|
QVariantMap activeNotification = this->sendNotification(chatId, addedNotification, activeNotifications);
|
2020-09-09 00:44:57 +03:00
|
|
|
activeNotifications.insert(activeNotification.value("id").toString(), activeNotification);
|
2020-09-04 00:20:46 +03:00
|
|
|
}
|
2020-09-09 00:44:57 +03:00
|
|
|
|
|
|
|
notificationGroup.insert("notifications", activeNotifications);
|
2020-09-04 00:20:46 +03:00
|
|
|
this->notificationGroups.insert(notificationGroupId, notificationGroup);
|
2020-09-02 23:49:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationManager::handleUpdateNotification(const QVariantMap updatedNotification)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Received notification update, group ID:" << updatedNotification.value("notification_group_id").toInt());
|
2020-09-02 23:49:15 +03:00
|
|
|
}
|
2020-09-03 00:05:09 +03:00
|
|
|
|
|
|
|
void NotificationManager::handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation)
|
|
|
|
{
|
|
|
|
this->chatListMutex.lock();
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Adding chat to internal map" << chatId);
|
2020-09-03 00:05:09 +03:00
|
|
|
this->chatMap.insert(chatId, chatInformation);
|
|
|
|
this->chatListMutex.unlock();
|
|
|
|
}
|
2020-09-04 00:20:46 +03:00
|
|
|
|
2020-09-13 22:54:36 +03:00
|
|
|
void NotificationManager::handleNgfConnectionStatus(const bool &connected)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("NGF Daemon connection status changed" << connected);
|
2020-09-13 22:54:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationManager::handleNgfEventFailed(const quint32 &eventId)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("NGF event failed, id:" << eventId);
|
2020-09-13 22:54:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationManager::handleNgfEventCompleted(const quint32 &eventId)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("NGF event completed, id:" << eventId);
|
2020-09-13 22:54:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationManager::handleNgfEventPlaying(const quint32 &eventId)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("NGF event playing, id:" << eventId);
|
2020-09-13 22:54:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationManager::handleNgfEventPaused(const quint32 &eventId)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("NGF event paused, id:" << eventId);
|
2020-09-13 22:54:36 +03:00
|
|
|
}
|
|
|
|
|
2020-09-10 00:44:02 +03:00
|
|
|
QVariantMap NotificationManager::sendNotification(const QString &chatId, const QVariantMap ¬ificationInformation, const QVariantMap &activeNotifications)
|
2020-09-04 00:20:46 +03:00
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Sending notification" << notificationInformation.value("id").toString());
|
2020-09-09 00:44:57 +03:00
|
|
|
|
|
|
|
QVariantMap chatInformation = this->chatMap.value(chatId).toMap();
|
2020-09-13 00:22:33 +03:00
|
|
|
QString chatType = chatInformation.value("type").toMap().value("@type").toString();
|
|
|
|
bool addAuthor = false;
|
2020-09-30 22:41:49 +03:00
|
|
|
if (chatType == "chatTypeBasicGroup" || ( chatType == "chatTypeSupergroup" && !chatInformation.value("type").toMap().value("is_channel").toBool() )) {
|
2020-09-13 00:22:33 +03:00
|
|
|
addAuthor = true;
|
|
|
|
}
|
2020-09-09 00:44:57 +03:00
|
|
|
|
|
|
|
QVariantMap updatedNotificationInformation = notificationInformation;
|
2020-09-04 00:20:46 +03:00
|
|
|
QUrl appIconUrl = SailfishApp::pathTo("images/fernschreiber-notification.png");
|
2020-09-13 00:22:33 +03:00
|
|
|
QVariantMap messageMap = notificationInformation.value("type").toMap().value("message").toMap();
|
2020-09-09 00:44:57 +03:00
|
|
|
Notification nemoNotification;
|
|
|
|
nemoNotification.setAppName("Fernschreiber");
|
|
|
|
nemoNotification.setAppIcon(appIconUrl.toLocalFile());
|
|
|
|
nemoNotification.setSummary(chatInformation.value("title").toString());
|
|
|
|
nemoNotification.setCategory("x-nemo.messaging.im");
|
2020-09-13 00:22:33 +03:00
|
|
|
nemoNotification.setTimestamp(QDateTime::fromMSecsSinceEpoch(messageMap.value("date").toLongLong() * 1000));
|
2020-09-15 00:43:21 +03:00
|
|
|
QVariantList remoteActionArguments;
|
|
|
|
remoteActionArguments.append(chatId);
|
|
|
|
remoteActionArguments.append(messageMap.value("id").toString());
|
|
|
|
nemoNotification.setRemoteAction(Notification::remoteAction("default", "openMessage", "de.ygriega.fernschreiber", "/de/ygriega/fernschreiber", "de.ygriega.fernschreiber", "openMessage", remoteActionArguments));
|
2020-09-10 00:44:02 +03:00
|
|
|
if (activeNotifications.isEmpty()) {
|
2020-09-13 00:22:33 +03:00
|
|
|
QString notificationBody;
|
|
|
|
if (addAuthor) {
|
|
|
|
QVariantMap authorInformation = tdLibWrapper->getUserInformation(messageMap.value("sender_user_id").toString());
|
|
|
|
QString firstName = authorInformation.value("first_name").toString();
|
|
|
|
QString lastName = authorInformation.value("last_name").toString();
|
|
|
|
QString fullName = firstName + " " + lastName;
|
|
|
|
notificationBody = notificationBody + fullName.trimmed() + ": ";
|
|
|
|
}
|
|
|
|
notificationBody = notificationBody + this->getNotificationText(messageMap.value("content").toMap());
|
|
|
|
nemoNotification.setBody(notificationBody);
|
2020-09-10 00:44:02 +03:00
|
|
|
} else {
|
|
|
|
nemoNotification.setReplacesId(activeNotifications.first().toMap().value("replaces_id").toUInt());
|
|
|
|
nemoNotification.setBody(tr("%1 unread messages").arg(activeNotifications.size() + 1));
|
|
|
|
}
|
2020-09-09 00:44:57 +03:00
|
|
|
|
|
|
|
nemoNotification.publish();
|
2020-09-13 22:54:36 +03:00
|
|
|
this->ngfClient->play("chat");
|
|
|
|
this->controlLedNotification(true);
|
2020-09-09 00:44:57 +03:00
|
|
|
updatedNotificationInformation.insert("replaces_id", nemoNotification.replacesId());
|
|
|
|
return updatedNotificationInformation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationManager::removeNotification(const QVariantMap ¬ificationInformation)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Removing notification" << notificationInformation.value("id").toString());
|
2020-09-09 00:44:57 +03:00
|
|
|
Notification nemoNotification;
|
|
|
|
nemoNotification.setReplacesId(notificationInformation.value("replaces_id").toUInt());
|
|
|
|
nemoNotification.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString NotificationManager::getNotificationText(const QVariantMap ¬ificationContent)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Getting notification text from content" << notificationContent);
|
2020-09-13 00:22:33 +03:00
|
|
|
|
2020-10-03 23:58:45 +03:00
|
|
|
return FernschreiberUtils::getMessageShortText(notificationContent, false);
|
2020-09-04 00:20:46 +03:00
|
|
|
}
|
2020-09-13 22:54:36 +03:00
|
|
|
|
|
|
|
void NotificationManager::controlLedNotification(const bool &enabled)
|
|
|
|
{
|
2020-10-10 16:48:12 +03:00
|
|
|
LOG("Controlling notification LED" << enabled;
|
2020-09-13 22:54:36 +03:00
|
|
|
QDBusConnection dbusConnection = QDBusConnection::connectToBus(QDBusConnection::SystemBus, "system");
|
|
|
|
QDBusInterface dbusInterface("com.nokia.mce", "/com/nokia/mce/request", "com.nokia.mce.request", dbusConnection);
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
dbusInterface.call("req_led_pattern_activate", "PatternCommunicationIM");
|
|
|
|
} else {
|
|
|
|
dbusInterface.call("req_led_pattern_deactivate", "PatternCommunicationIM");
|
|
|
|
}
|
|
|
|
}
|