File indexing completed on 2024-05-05 05:01:24

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 text to search the public room list for.
0045      */
0046     Q_PROPERTY(QString searchText READ searchText WRITE setSearchText NOTIFY searchTextChanged)
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 is searching.
0055      */
0056     Q_PROPERTY(bool searching READ searching NOTIFY searchingChanged)
0057 
0058 public:
0059     /**
0060      * @brief Defines the model roles.
0061      */
0062     enum EventRoles {
0063         DisplayNameRole = Qt::DisplayRole + 1, /**< The name of the room. */
0064         AvatarUrlRole, /**< The source URL for the room's avatar. */
0065         TopicRole, /**< The room topic. */
0066         RoomIdRole, /**< The room matrix ID. */
0067         AliasRole, /**< The room canonical alias. */
0068         MemberCountRole, /**< The number of members in the room. */
0069         AllowGuestsRole, /**< Whether the room allows guest users. */
0070         WorldReadableRole, /**< Whether the room events can be seen by non-members. */
0071         IsJoinedRole, /**< Whether the local user has joined the room. */
0072     };
0073 
0074     explicit PublicRoomListModel(QObject *parent = nullptr);
0075 
0076     /**
0077      * @brief Get the given role value at the given index.
0078      *
0079      * @sa QAbstractItemModel::data
0080      */
0081     [[nodiscard]] QVariant data(const QModelIndex &index, int role = DisplayNameRole) const override;
0082 
0083     /**
0084      * @brief Number of rows in the model.
0085      *
0086      * @sa  QAbstractItemModel::rowCount
0087      */
0088     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0089 
0090     /**
0091      * @brief Returns a mapping from Role enum values to role names.
0092      *
0093      * @sa EventRoles, QAbstractItemModel::roleNames()
0094      */
0095     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0096 
0097     [[nodiscard]] Quotient::Connection *connection() const;
0098     void setConnection(Quotient::Connection *conn);
0099 
0100     [[nodiscard]] QString server() const;
0101     void setServer(const QString &value);
0102 
0103     [[nodiscard]] QString searchText() const;
0104     void setSearchText(const QString &searchText);
0105 
0106     [[nodiscard]] bool showOnlySpaces() const;
0107     void setShowOnlySpaces(bool showOnlySpaces);
0108 
0109     [[nodiscard]] bool searching() const;
0110 
0111     /**
0112      * @brief Search the room directory.
0113      *
0114      * @param limit the maximum number of rooms to load.
0115      */
0116     Q_INVOKABLE void search(int limit = 50);
0117 
0118 private:
0119     QPointer<Quotient::Connection> m_connection = nullptr;
0120     QString m_server;
0121     QString m_searchText;
0122     bool m_showOnlySpaces = false;
0123 
0124     /**
0125      * @brief Load the next set of rooms.
0126      *
0127      * @param limit the maximum number of rooms to load.
0128      */
0129     void next(int limit = 50);
0130     bool canFetchMore(const QModelIndex &parent) const override;
0131     void fetchMore(const QModelIndex &parent) override;
0132 
0133     bool attempted = false;
0134     bool m_searching = false;
0135     QString nextBatch;
0136 
0137     QList<Quotient::PublicRoomsChunk> rooms;
0138 
0139     Quotient::QueryPublicRoomsJob *job = nullptr;
0140 
0141 Q_SIGNALS:
0142     void connectionChanged();
0143     void serverChanged();
0144     void searchTextChanged();
0145     void showOnlySpacesChanged();
0146     void searchingChanged();
0147 };