File indexing completed on 2024-07-21 12:42:20

0001 /*
0002 
0003  * SPDX-FileCopyrightText: 2016 Riccardo Iaconelli <riccardo@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  *
0007  */
0008 
0009 #pragma once
0010 #include "libruqolacore_export.h"
0011 #include "room.h"
0012 #include "user.h"
0013 #include <QAbstractListModel>
0014 class QObject;
0015 class RocketChatAccount;
0016 class MessagesModel;
0017 
0018 class LIBRUQOLACORE_EXPORT RoomModel : public QAbstractListModel
0019 {
0020     Q_OBJECT
0021 public:
0022     enum RoomRoles {
0023         RoomName = Qt::UserRole + 1,
0024         RoomFName,
0025         RoomSelected,
0026         RoomId,
0027         RoomUnread,
0028         RoomType,
0029         RoomOwnerUserName, // created by UserName
0030         RoomOwnerUserId,
0031         RoomTopic,
0032         RoomMutedUsers,
0033         RoomJitsiTimeout,
0034         RoomReadOnly,
0035         RoomAnnouncement,
0036         RoomOpen,
0037         RoomAlert,
0038         RoomFavorite,
0039         RoomSection,
0040         RoomIcon,
0041         RoomOtr,
0042         RoomUserMentions,
0043         RoomIgnoredUsers,
0044         RoomAutotranslateLanguage,
0045         RoomAutotranslate,
0046         RoomDirectChannelUserId,
0047         RoomAvatarInfo,
0048         RoomTeamId,
0049         RoomTeamIsMain,
0050         RoomLastMessageAt,
0051         UserOffline,
0052         HideBadgeForMention,
0053     };
0054     Q_ENUM(RoomRoles)
0055 
0056     enum class Section {
0057         Unread,
0058         Favorites,
0059         Teams,
0060         Rooms,
0061         PrivateMessages,
0062         Discussions,
0063         Unknown,
0064         NSections,
0065     };
0066 
0067     explicit RoomModel(RocketChatAccount *account = nullptr, QObject *parent = nullptr);
0068     ~RoomModel() override;
0069 
0070     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0071     [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0072 
0073     /**
0074      * @brief Constructs room object from @param roomID and @param roomName and @param selected, then calls @method addRoom
0075      *
0076      * @param roomID The unique room ID
0077      * @param roomName The name of the room
0078      * @param selected True if room if @param roomID is selected, else false
0079      */
0080     void addRoom(const QString &roomID, const QString &roomName, bool selected = false);
0081 
0082     // Clear data and refill it with data in the cache, if there is
0083     void reset();
0084     void clear();
0085 
0086     void updateSubscription(const QJsonArray &array);
0087     void updateRoom(const QJsonObject &array);
0088     void addRoom(const QJsonObject &room);
0089 
0090     [[nodiscard]] Room::TeamRoomInfo roomFromTeamId(const QString &teamId);
0091 
0092     /**
0093      * @brief Adds a room to mRoomsList with @param room
0094      *
0095      * @param room The room to be added
0096      */
0097     [[nodiscard]] bool addRoom(Room *room);
0098     void removeRoom(const QString &roomId);
0099 
0100     void getUnreadAlertFromAccount(bool &hasAlert, int &nbUnread) const;
0101     void userStatusChanged(const User &user);
0102 
0103     UsersForRoomModel *usersModelForRoom(const QString &roomId) const;
0104 
0105     MessagesModel *messageModel(const QString &roomId) const;
0106 
0107     [[nodiscard]] QString inputMessage(const QString &roomId) const;
0108     void setInputMessage(const QString &roomId, const QString &inputMessage);
0109     [[nodiscard]] Room *findRoom(const QString &roomID) const;
0110     void updateSubscriptionRoom(const QJsonObject &room);
0111     [[nodiscard]] QString insertRoom(const QJsonObject &room);
0112 
0113     [[nodiscard]] QModelIndex indexForRoomName(const QString &roomName) const;
0114 
0115     static QString sectionName(RoomModel::Section sectionId);
0116 
0117     [[nodiscard]] QVector<Room *> findRoomNameConstains(const QString &str) const;
0118 Q_SIGNALS:
0119     void needToUpdateNotification();
0120     void roomNeedAttention();
0121     void roomRemoved(const QString &roomId);
0122 
0123 private:
0124     LIBRUQOLACORE_NO_EXPORT Room *createNewRoom();
0125     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT bool userOffline(Room *r) const;
0126     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT QIcon icon(Room *r) const;
0127     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT Section section(Room *r) const;
0128     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT QString generateToolTip(Room *r) const;
0129 
0130     RocketChatAccount *const mRocketChatAccount;
0131     QVector<Room *> mRoomsList;
0132 };
0133 
0134 Q_DECLARE_METATYPE(RoomModel::Section)