File indexing completed on 2024-11-03 10:41:30

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/users.h>
0010 
0011 namespace Quotient
0012 {
0013 class Connection;
0014 }
0015 
0016 /**
0017  * @class UserDirectoryListModel
0018  *
0019  * This class defines the model for visualising the results of a user search.
0020  *
0021  * The model searches for users that have the given keyword in the matrix
0022  * ID or the display name. See
0023  * https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3user_directorysearch
0024  * for more info on matrix user searches.
0025  */
0026 class UserDirectoryListModel : public QAbstractListModel
0027 {
0028     Q_OBJECT
0029 
0030     /**
0031      * @brief The current connection that the model is getting users from.
0032      */
0033     Q_PROPERTY(Quotient::Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged)
0034 
0035     /**
0036      * @brief The keyword to use in the search.
0037      */
0038     Q_PROPERTY(QString keyword READ keyword WRITE setKeyword NOTIFY keywordChanged)
0039 
0040     /**
0041      * @brief Whether the current results have been truncated.
0042      */
0043     Q_PROPERTY(bool limited READ limited NOTIFY limitedChanged)
0044 
0045 public:
0046     /**
0047      * @brief Defines the model roles.
0048      */
0049     enum EventRoles {
0050         NameRole = Qt::DisplayRole + 1, /**< The user's display name. */
0051         AvatarRole, /**< The source URL for the user's avatar. */
0052         UserIDRole, /**< Matrix ID of the user. */
0053         DirectChatsRole, /**< A list of direct chat matrix IDs with the user. */
0054     };
0055 
0056     explicit UserDirectoryListModel(QObject *parent = nullptr);
0057 
0058     [[nodiscard]] Quotient::Connection *connection() const;
0059     void setConnection(Quotient::Connection *conn);
0060 
0061     [[nodiscard]] QString keyword() const;
0062     void setKeyword(const QString &value);
0063 
0064     [[nodiscard]] bool limited() const;
0065 
0066     /**
0067      * @brief Get the given role value at the given index.
0068      *
0069      * @sa QAbstractItemModel::data
0070      */
0071     [[nodiscard]] QVariant data(const QModelIndex &index, int role = NameRole) const override;
0072 
0073     /**
0074      * @brief Number of rows in the model.
0075      *
0076      * @sa QAbstractItemModel::rowCount
0077      */
0078     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0079 
0080     /**
0081      * @brief Returns a mapping from Role enum values to role names.
0082      *
0083      * @sa EventRoles, QAbstractItemModel::roleNames()
0084      */
0085     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0086 
0087     /**
0088      * @brief Start the user search.
0089      */
0090     Q_INVOKABLE void search(int count = 50);
0091 
0092 Q_SIGNALS:
0093     void connectionChanged();
0094     void keywordChanged();
0095     void limitedChanged();
0096 
0097 private:
0098     Quotient::Connection *m_connection = nullptr;
0099     QString m_keyword;
0100     bool m_limited = false;
0101 
0102     bool attempted = false;
0103 
0104     QVector<Quotient::SearchUserDirectoryJob::User> users;
0105 
0106     Quotient::SearchUserDirectoryJob *job = nullptr;
0107 };