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

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