File indexing completed on 2024-10-06 07:36:09
0001 // SPDX-FileCopyrightText: 2020 Tobias Fella <tobias.fella@kde.org> 0002 // SPDX-License-Identifier: GPL-2.0-or-later 0003 0004 #pragma once 0005 0006 #include <QImage> 0007 #include <QJsonObject> 0008 #include <QMap> 0009 #include <QObject> 0010 #include <QPointer> 0011 #include <QQmlEngine> 0012 #include <QString> 0013 #include <Quotient/csapi/notifications.h> 0014 #include <Quotient/jobs/basejob.h> 0015 0016 class NeoChatConnection; 0017 class KNotification; 0018 class NeoChatRoom; 0019 0020 /** 0021 * @class NotificationsManager 0022 * 0023 * This class is responsible for managing notifications. 0024 * 0025 * This includes sending native notifications on mobile or desktop if available as 0026 * well as managing the push notification rules for the current active account. 0027 * 0028 * @note Matrix manages push notifications centrally for an account and these are 0029 * stored on the home server. This is to allow a users settings to move between clients. 0030 * See https://spec.matrix.org/v1.3/client-server-api/#push-rules for more 0031 * details. 0032 */ 0033 class NotificationsManager : public QObject 0034 { 0035 Q_OBJECT 0036 QML_ELEMENT 0037 QML_SINGLETON 0038 0039 public: 0040 static NotificationsManager &instance(); 0041 static NotificationsManager *create(QQmlEngine *engine, QJSEngine *) 0042 { 0043 engine->setObjectOwnership(&instance(), QQmlEngine::CppOwnership); 0044 return &instance(); 0045 } 0046 0047 /** 0048 * @brief Display a native notification for an message. 0049 */ 0050 Q_INVOKABLE void 0051 postNotification(NeoChatRoom *room, const QString &sender, const QString &text, const QImage &icon, const QString &replyEventId, bool canReply); 0052 0053 /** 0054 * @brief Display a native notification for an invite. 0055 */ 0056 void postInviteNotification(NeoChatRoom *room, const QString &title, const QString &sender, const QImage &icon); 0057 0058 /** 0059 * @brief Clear an existing invite notification for the given room. 0060 * 0061 * Nothing happens if the given room doesn't have an invite notification. 0062 */ 0063 void clearInvitationNotification(const QString &roomId); 0064 0065 /** 0066 * @brief Display a native notification for the given push notification. 0067 */ 0068 void postPushNotification(const QByteArray &message); 0069 0070 /** 0071 * @brief Handle the notifications for the given connection. 0072 */ 0073 void handleNotifications(QPointer<NeoChatConnection> connection); 0074 0075 private: 0076 explicit NotificationsManager(QObject *parent = nullptr); 0077 0078 QHash<QString, qint64> m_initialTimestamp; 0079 QHash<QString, QStringList> m_oldNotifications; 0080 0081 QStringList m_connActiveJob; 0082 0083 bool shouldPostNotification(QPointer<NeoChatConnection> connection, const QJsonValue ¬ification); 0084 0085 QHash<QString, KNotification *> m_notifications; 0086 QHash<QString, QPointer<KNotification>> m_invitations; 0087 0088 private Q_SLOTS: 0089 void processNotificationJob(QPointer<NeoChatConnection> connection, Quotient::GetNotificationsJob *job, bool initialization); 0090 0091 private: 0092 QPixmap createNotificationImage(const QImage &icon, NeoChatRoom *room); 0093 };