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

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