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

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 text to search the public room list for.
0039      */
0040     Q_PROPERTY(QString searchText READ searchText WRITE setSearchText NOTIFY searchTextChanged)
0041 
0042     /**
0043      * @brief Whether the model is searching.
0044      */
0045     Q_PROPERTY(bool searching READ searching NOTIFY searchingChanged)
0046 
0047 public:
0048     /**
0049      * @brief Defines the model roles.
0050      */
0051     enum EventRoles {
0052         DisplayNameRole = Qt::DisplayRole, /**< The user's display name. */
0053         AvatarRole, /**< The source URL for the user's avatar. */
0054         UserIDRole, /**< Matrix ID of the user. */
0055         DirectChatExistsRole, /**< Whether there is already a direct chat 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 searchText() const;
0064     void setSearchText(const QString &searchText);
0065 
0066     [[nodiscard]] bool searching() 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 = Qt::DisplayRole) 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 Search the user directory.
0091      *
0092      * @param limit the maximum number of rooms to load.
0093      */
0094     Q_INVOKABLE void search(int limit = 50);
0095 
0096 Q_SIGNALS:
0097     void connectionChanged();
0098     void searchTextChanged();
0099     void searchingChanged();
0100 
0101 private:
0102     Quotient::Connection *m_connection = nullptr;
0103     QString m_searchText;
0104 
0105     bool attempted = false;
0106     QList<Quotient::SearchUserDirectoryJob::User> users;
0107 
0108     Quotient::SearchUserDirectoryJob *m_job = nullptr;
0109 };