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

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