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

0001 // SPDX-FileCopyrightText: 2022 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #pragma once
0005 
0006 #include <Quotient/csapi/list_public_rooms.h>
0007 
0008 #include <QAbstractListModel>
0009 #include <QPointer>
0010 #include <QUrl>
0011 
0012 /**
0013  * @class ServerListModel
0014  *
0015  * This class defines the model for visualising a list of matrix servers.
0016  *
0017  * The list of servers is retrieved from the local cache. Any additions are also
0018  * stored locally so that they are retrieved on subsequent instantiations.
0019  *
0020  * The model also automatically adds the local user's home server and matrix.org to
0021  * the model. Finally the model also adds an entry to create a space in the model
0022  * for an "add new server" delegate.
0023  */
0024 class ServerListModel : public QAbstractListModel
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     /**
0030      * @brief Define the data required to represent a server.
0031      */
0032     struct Server {
0033         QString url; /**< Server URL. */
0034         bool isHomeServer; /**< Whether the server is the local user's home server. */
0035         bool isAddServerDelegate; /**< Wether the item is the "add new server" delegate. */
0036         bool isDeletable; /**< Whether the item can be deleted from the model. */
0037     };
0038 
0039     /**
0040      * @brief Defines the model roles.
0041      */
0042     enum EventRoles {
0043         UrlRole = Qt::UserRole + 1, /**< Server URL. */
0044         IsHomeServerRole, /**< Whether the server is the local user's home server. */
0045         IsAddServerDelegateRole, /**< Whether the item is the add new server delegate. */
0046         IsDeletableRole, /**< Whether the item can be deleted from the model. */
0047     };
0048 
0049     explicit ServerListModel(QObject *parent = nullptr);
0050 
0051     /**
0052      * @brief Get the given role value at the given index.
0053      *
0054      * @sa QAbstractItemModel::data
0055      */
0056     [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0057 
0058     /**
0059      * @brief Number of rows in the model.
0060      *
0061      * @sa QAbstractItemModel::rowCount
0062      */
0063     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0064 
0065     /**
0066      * @brief Returns a mapping from Role enum values to role names.
0067      *
0068      * @sa EventRoles, QAbstractItemModel::roleNames()
0069      */
0070     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0071 
0072     /**
0073      * @brief Start a check to see if the given URL is a valid matrix server.
0074      *
0075      * This function starts the check but due to the requests being asynchronous
0076      * the caller will need to watch the serverCheckComplete signal for confirmation.
0077      * The server URL should be treated as invalid until the signal is emitted true.
0078      *
0079      * @sa serverCheckComplete()
0080      */
0081     Q_INVOKABLE void checkServer(const QString &url);
0082 
0083     /**
0084      * @brief Add a new server to the model.
0085      *
0086      * The server will also be stored in local cache.
0087      */
0088     Q_INVOKABLE void addServer(const QString &url);
0089 
0090     /**
0091      * @brief Remove the server at the given index.
0092      *
0093      * The server will also be removed from local cache.
0094      */
0095     Q_INVOKABLE void removeServerAtIndex(int index);
0096 
0097 Q_SIGNALS:
0098     void serverCheckComplete(QString url, bool valid);
0099 
0100 private:
0101     QList<Server> m_servers;
0102     QPointer<Quotient::QueryPublicRoomsJob> m_checkServerJob = nullptr;
0103 };