File indexing completed on 2024-04-28 16:54:33

0001 /*
0002     SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@privat.broulik.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractListModel>
0010 #include <QDateTime>
0011 #include <QScopedPointer>
0012 #include <QSharedPointer>
0013 #include <QWindow>
0014 
0015 #include "notification.h"
0016 #include "notifications.h"
0017 #include "server.h"
0018 
0019 namespace NotificationManager
0020 {
0021 class Q_DECL_EXPORT AbstractNotificationsModel : public QAbstractListModel
0022 {
0023     Q_OBJECT
0024     Q_PROPERTY(QWindow *window READ window WRITE setWindow NOTIFY windowChanged)
0025 
0026 public:
0027     ~AbstractNotificationsModel() override;
0028 
0029     QDateTime lastRead() const;
0030     void setLastRead(const QDateTime &lastRead);
0031 
0032     QWindow *window() const;
0033     void setWindow(QWindow *window);
0034 
0035     QVariant data(const QModelIndex &index, int role) const override;
0036     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0037     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0038     QHash<int, QByteArray> roleNames() const override;
0039 
0040     virtual void expire(uint notificationId) = 0;
0041     virtual void close(uint notificationId) = 0;
0042 
0043     // Currently configure actions are not exposed in AbstractNotificationsModel to keep it very minimal
0044     // if usecase for this comes up in future, we can revisit it.
0045 
0046     virtual void invokeDefaultAction(uint notificationId, Notifications::InvokeBehavior behavior) = 0;
0047     virtual void invokeAction(uint notificationId, const QString &actionName, Notifications::InvokeBehavior behavior) = 0;
0048     virtual void reply(uint notificationId, const QString &text, Notifications::InvokeBehavior behavior) = 0;
0049 
0050     void startTimeout(uint notificationId);
0051     void stopTimeout(uint notificationId);
0052 
0053     void clear(Notifications::ClearFlags flags);
0054 
0055 Q_SIGNALS:
0056     void lastReadChanged();
0057     void windowChanged(QWindow *window);
0058 
0059 protected:
0060     AbstractNotificationsModel();
0061     void onNotificationAdded(const Notification &notification);
0062     void onNotificationReplaced(uint replacedId, const Notification &notification);
0063     void onNotificationRemoved(uint notificationId, Server::CloseReason reason);
0064 
0065     void setupNotificationTimeout(const Notification &notification);
0066     const QVector<Notification> &notifications();
0067     int rowOfNotification(uint id) const;
0068 
0069 private:
0070     friend class NotificationTest;
0071 
0072     class Private;
0073     QScopedPointer<Private> d;
0074 
0075     Q_DISABLE_COPY(AbstractNotificationsModel)
0076 };
0077 
0078 } // namespace NotificationManager