File indexing completed on 2024-12-08 07:33:45

0001 // SPDX-FileCopyrightText: 2023 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include "neochatconnection.h"
0007 #include <QAbstractListModel>
0008 #include <QPointer>
0009 #include <QQmlEngine>
0010 #include <QVariant>
0011 #include <Quotient/csapi/notifications.h>
0012 
0013 class NotificationsModel : public QAbstractListModel
0014 {
0015     Q_OBJECT
0016     QML_ELEMENT
0017 
0018     Q_PROPERTY(NeoChatConnection *connection READ connection WRITE setConnection NOTIFY connectionChanged)
0019     Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
0020     Q_PROPERTY(QString nextToken READ nextToken NOTIFY nextTokenChanged)
0021 
0022 public:
0023     enum Roles {
0024         TextRole = Qt::DisplayRole,
0025         RoomIdRole,
0026         AuthorName,
0027         AuthorAvatar,
0028         RoomRole,
0029         EventIdRole,
0030         RoomDisplayNameRole,
0031         UriRole,
0032     };
0033     Q_ENUM(Roles);
0034 
0035     struct Notification {
0036         QString roomId;
0037         QString text;
0038         QString authorName;
0039         QUrl authorAvatar;
0040         QString eventId;
0041         QString roomDisplayName;
0042     };
0043 
0044     NotificationsModel(QObject *parent = nullptr);
0045 
0046     int rowCount(const QModelIndex &parent) const override;
0047     QVariant data(const QModelIndex &index, int role) const override;
0048     QHash<int, QByteArray> roleNames() const override;
0049     bool canFetchMore(const QModelIndex &parent) const override;
0050     void fetchMore(const QModelIndex &parent) override;
0051 
0052     NeoChatConnection *connection() const;
0053     void setConnection(NeoChatConnection *connection);
0054     bool loading() const;
0055     QString nextToken() const;
0056 
0057 Q_SIGNALS:
0058     void connectionChanged();
0059     void loadingChanged();
0060     void nextTokenChanged();
0061 
0062 private:
0063     QPointer<NeoChatConnection> m_connection;
0064     void loadData();
0065     QList<Notification> m_notifications;
0066     QString m_nextToken;
0067     QPointer<Quotient::GetNotificationsJob> m_job;
0068 };