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

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/events/roomevent.h>
0007 
0008 #include <QAbstractListModel>
0009 
0010 class NeoChatRoom;
0011 
0012 namespace Quotient
0013 {
0014 class Connection;
0015 class Room;
0016 }
0017 
0018 class NeoChatRoomType : public QObject
0019 {
0020     Q_OBJECT
0021 
0022 public:
0023     /**
0024      * @brief Defines the room list categories a room can be assigned.
0025      */
0026     enum Types {
0027         Invited = 1, /**< The user has been invited to the room. */
0028         Favorite, /**< The room is set as a favourite. */
0029         Direct, /**< The room is a direct chat. */
0030         Normal, /**< The default category for a joined room. */
0031         Deprioritized, /**< The room is set as low priority. */
0032         Space, /**< The room is a space. */
0033     };
0034     Q_ENUM(Types)
0035 };
0036 
0037 /**
0038  * @class RoomListModel
0039  *
0040  * This class defines the model for visualising the user's list of joined rooms.
0041  */
0042 class RoomListModel : public QAbstractListModel
0043 {
0044     Q_OBJECT
0045 
0046     /**
0047      * @brief The current connection that the model is getting its rooms from.
0048      */
0049     Q_PROPERTY(Quotient::Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged)
0050 
0051     /**
0052      * @brief The total number of notifications for all the rooms.
0053      */
0054     Q_PROPERTY(int notificationCount READ notificationCount NOTIFY notificationCountChanged)
0055 
0056 public:
0057     /**
0058      * @brief Defines the model roles.
0059      */
0060     enum EventRoles {
0061         DisplayNameRole = Qt::DisplayRole, /**< The display name of the room. */
0062         AvatarRole, /**< The source URL for the room's avatar. */
0063         CanonicalAliasRole, /**< The room canonical alias. */
0064         TopicRole, /**< The room topic. */
0065         CategoryRole, /**< The room category, e.g favourite. */
0066         NotificationCountRole, /**< The number of notifications in the room. */
0067         HighlightCountRole, /**< The number of highlighted messages in the room. */
0068         LastEventRole, /**< Text for the last event in the room. */
0069         LastActiveTimeRole, /**< The timestamp of the last event sent in the room. */
0070         JoinStateRole, /**< The local user's join state in the room. */
0071         CurrentRoomRole, /**< The room object for the room. */
0072         CategoryVisibleRole, /**< If the room's category is visible. */
0073         SubtitleTextRole, /**< The text to show as the room subtitle. */
0074         AvatarImageRole, /**< The room avatar as an image. */
0075         RoomIdRole, /**< The room matrix ID. */
0076         IsSpaceRole, /**< Whether the room is a space. */
0077         IsChildSpaceRole, /**< Whether this space is a child of a different space. */
0078     };
0079     Q_ENUM(EventRoles)
0080 
0081     explicit RoomListModel(QObject *parent = nullptr);
0082     ~RoomListModel() override;
0083 
0084     [[nodiscard]] Quotient::Connection *connection() const;
0085     void setConnection(Quotient::Connection *connection);
0086 
0087     [[nodiscard]] int notificationCount() const;
0088 
0089     /**
0090      * @brief Get the given role value at the given index.
0091      *
0092      * @sa QAbstractItemModel::data
0093      */
0094     [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0095 
0096     /**
0097      * @brief Number of rows in the model.
0098      *
0099      * @sa QAbstractItemModel::rowCount
0100      */
0101     Q_INVOKABLE [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0102 
0103     /**
0104      * @brief Returns a mapping from Role enum values to role names.
0105      *
0106      * @sa EventRoles, QAbstractItemModel::roleNames()
0107      */
0108     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0109 
0110     /**
0111      * @brief Return the room at the given row.
0112      */
0113     Q_INVOKABLE [[nodiscard]] NeoChatRoom *roomAt(int row) const;
0114 
0115     /**
0116      * @brief Return a string to represent the given room category.
0117      */
0118     Q_INVOKABLE [[nodiscard]] static QString categoryName(int category);
0119 
0120     /**
0121      * @brief Return a string with the name of the given room category icon.
0122      */
0123     Q_INVOKABLE [[nodiscard]] static QString categoryIconName(int category);
0124 
0125     /**
0126      * @brief Set whether a given category should be visible or not.
0127      *
0128      * @param category the NeoChatRoomType::Types value for the category (it's an
0129      *                 int due to the pain of Q_INVOKABLES and cpp enums).
0130      * @param visible true if the category should be visible, false if not.
0131      */
0132     Q_INVOKABLE void setCategoryVisible(int category, bool visible);
0133 
0134     /**
0135      * @brief Return whether a room category is set to be visible.
0136      */
0137     Q_INVOKABLE [[nodiscard]] bool categoryVisible(int category) const;
0138 
0139     /**
0140      * @brief Return the model row for the given room.
0141      */
0142     Q_INVOKABLE [[nodiscard]] int rowForRoom(NeoChatRoom *room) const;
0143 
0144     /**
0145      * @brief Return a room for the given room alias or room matrix ID.
0146      *
0147      * The room must be in the model.
0148      */
0149     Q_INVOKABLE NeoChatRoom *roomByAliasOrId(const QString &aliasOrId);
0150 
0151 private Q_SLOTS:
0152     void doResetModel();
0153     void doAddRoom(Quotient::Room *room);
0154     void updateRoom(Quotient::Room *room, Quotient::Room *prev);
0155     void deleteRoom(Quotient::Room *room);
0156     void refresh(NeoChatRoom *room, const QVector<int> &roles = {});
0157     void refreshNotificationCount();
0158 
0159 private:
0160     Quotient::Connection *m_connection = nullptr;
0161     QList<NeoChatRoom *> m_rooms;
0162 
0163     QMap<int, bool> m_categoryVisibility;
0164 
0165     int m_notificationCount = 0;
0166     QString m_activeSpaceId = "";
0167 
0168     void connectRoomSignals(NeoChatRoom *room);
0169 
0170 Q_SIGNALS:
0171     void connectionChanged();
0172     void notificationCountChanged();
0173 
0174     void roomAdded(NeoChatRoom *_t1);
0175 };