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

0001 // SPDX-FileCopyrightText: 2018 Black Hat <bhat@encom.eu.org>
0002 // SPDX-License-Identifier: GPL-3.0-only
0003 
0004 #pragma once
0005 
0006 #include <Quotient/room.h>
0007 
0008 #include <QAbstractListModel>
0009 #include <QObject>
0010 #include <QPointer>
0011 
0012 class NeoChatRoom;
0013 
0014 namespace Quotient
0015 {
0016 class User;
0017 }
0018 
0019 /**
0020  * @class UserListModel
0021  *
0022  * This class defines the model for listing the users in a room.
0023  *
0024  * As well as gathering all the users from a room, the model ensures that they are
0025  * sorted in alphabetical order.
0026  *
0027  * @sa NeoChatRoom
0028  */
0029 class UserListModel : public QAbstractListModel
0030 {
0031     Q_OBJECT
0032 
0033     /**
0034      * @brief The room that the model is getting its users from.
0035      */
0036     Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
0037 
0038 public:
0039     /**
0040      * @brief Defines the model roles.
0041      */
0042     enum EventRoles {
0043         DisplayNameRole = Qt::DisplayRole, /**< The user's display name in the current room. */
0044         UserIdRole, /**< Matrix ID of the user. */
0045         AvatarRole, /**< The source URL for the user's avatar in the current room. */
0046         ObjectRole, /**< The QObject for the user. */
0047         PowerLevelRole, /**< The user's power level in the current room. */
0048         PowerLevelStringRole, /**< The name of the user's power level in the current room. */
0049     };
0050     Q_ENUM(EventRoles)
0051 
0052     explicit UserListModel(QObject *parent = nullptr);
0053 
0054     [[nodiscard]] NeoChatRoom *room() const;
0055     void setRoom(NeoChatRoom *room);
0056 
0057     /**
0058      * @brief The user at the given index of the model.
0059      */
0060     [[nodiscard]] Quotient::User *userAt(QModelIndex index) const;
0061 
0062     /**
0063      * @brief Get the given role value at the given index.
0064      *
0065      * @sa QAbstractItemModel::data
0066      */
0067     [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
0068 
0069     /**
0070      * @brief Number of rows in the model.
0071      *
0072      * @sa  QAbstractItemModel::rowCount
0073      */
0074     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0075 
0076     /**
0077      * @brief Returns a mapping from Role enum values to role names.
0078      *
0079      * @sa EventRoles, QAbstractItemModel::roleNames()
0080      */
0081     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0082 
0083 Q_SIGNALS:
0084     void roomChanged();
0085     void usersRefreshed();
0086 
0087 private Q_SLOTS:
0088     void userAdded(Quotient::User *user);
0089     void userRemoved(Quotient::User *user);
0090     void refreshUser(Quotient::User *user, const QVector<int> &roles = {});
0091     void refreshAllUsers();
0092 
0093 private:
0094     QPointer<NeoChatRoom> m_currentRoom;
0095     QList<Quotient::User *> m_users;
0096 
0097     int findUserPos(Quotient::User *user) const;
0098     [[nodiscard]] int findUserPos(const QString &username) const;
0099 };