File indexing completed on 2024-05-05 16:58:39

0001 // SPDX-FileCopyrightText: 2019 Black Hat <bhat@encom.eu.org>
0002 // SPDX-License-Identifier: GPL-3.0-only
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <QObject>
0008 
0009 #include <Quotient/csapi/list_public_rooms.h>
0010 
0011 namespace Quotient
0012 {
0013 class Connection;
0014 }
0015 
0016 /**
0017  * @class PublicRoomListModel
0018  *
0019  * This class defines the model for visualising a list of public rooms.
0020  *
0021  * The model finds the public rooms visible to the given server (which doesn't have
0022  * to be the user's home server) and can also apply a filter if desired.
0023  *
0024  * Due to the fact that the public room list could be huge the model is lazily loaded
0025  * and requires that the next batch of rooms be manually called.
0026  */
0027 class PublicRoomListModel : public QAbstractListModel
0028 {
0029     Q_OBJECT
0030 
0031     /**
0032      * @brief The current connection that the model is getting its rooms from.
0033      */
0034     Q_PROPERTY(Quotient::Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged)
0035 
0036     /**
0037      * @brief The server to get the public room list from.
0038      */
0039     Q_PROPERTY(QString server READ server WRITE setServer NOTIFY serverChanged)
0040 
0041     /**
0042      * @brief The filter keyword for the list of public rooms.
0043      */
0044     Q_PROPERTY(QString keyword READ keyword WRITE setKeyword NOTIFY keywordChanged)
0045 
0046     /**
0047      * @brief Whether the model has more items to load.
0048      */
0049     Q_PROPERTY(bool hasMore READ hasMore NOTIFY hasMoreChanged)
0050 
0051 public:
0052     /**
0053      * @brief Defines the model roles.
0054      */
0055     enum EventRoles {
0056         NameRole = Qt::DisplayRole + 1, /**< The name of the room. */
0057         AvatarRole, /**< The source URL for the room's avatar. */
0058         TopicRole, /**< The room topic. */
0059         RoomIDRole, /**< The room matrix ID. */
0060         AliasRole, /**< The room canonical alias. */
0061         MemberCountRole, /**< The number of members in the room. */
0062         AllowGuestsRole, /**< Whether the room allows guest users. */
0063         WorldReadableRole, /**< Whether the room events can be seen by non-members. */
0064         IsJoinedRole, /**< Whether the local user has joined the room. */
0065     };
0066 
0067     explicit PublicRoomListModel(QObject *parent = nullptr);
0068 
0069     /**
0070      * @brief Get the given role value at the given index.
0071      *
0072      * @sa QAbstractItemModel::data
0073      */
0074     [[nodiscard]] QVariant data(const QModelIndex &index, int role = NameRole) const override;
0075 
0076     /**
0077      * @brief Number of rows in the model.
0078      *
0079      * @sa  QAbstractItemModel::rowCount
0080      */
0081     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0082 
0083     /**
0084      * @brief Returns a mapping from Role enum values to role names.
0085      *
0086      * @sa EventRoles, QAbstractItemModel::roleNames()
0087      */
0088     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0089 
0090     [[nodiscard]] Quotient::Connection *connection() const;
0091     void setConnection(Quotient::Connection *conn);
0092 
0093     [[nodiscard]] QString server() const;
0094     void setServer(const QString &value);
0095 
0096     [[nodiscard]] QString keyword() const;
0097     void setKeyword(const QString &value);
0098 
0099     [[nodiscard]] bool hasMore() const;
0100 
0101     /**
0102      * @brief Load the next set of rooms.
0103      *
0104      * @param count the maximum number of rooms to load.
0105      */
0106     Q_INVOKABLE void next(int count = 50);
0107 
0108 private:
0109     Quotient::Connection *m_connection = nullptr;
0110     QString m_server;
0111     QString m_keyword;
0112 
0113     bool attempted = false;
0114     QString nextBatch;
0115 
0116     QVector<Quotient::PublicRoomsChunk> rooms;
0117 
0118     Quotient::QueryPublicRoomsJob *job = nullptr;
0119 
0120 Q_SIGNALS:
0121     void connectionChanged();
0122     void serverChanged();
0123     void keywordChanged();
0124     void hasMoreChanged();
0125 };