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

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