File indexing completed on 2024-12-01 07:40:36

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include <Quotient/csapi/space_hierarchy.h>
0005 #include <Quotient/events/stateevent.h>
0006 
0007 class NeoChatConnection;
0008 
0009 /**
0010  * @class SpaceTreeItem
0011  *
0012  * This class defines an item in the space tree hierarchy model.
0013  *
0014  * @note This is separate from Quotient::Room and NeoChatRoom because we don't have
0015  *       full room information for any room/space the user hasn't joined and we
0016  *       don't want to create one for ever possible child in a space as that would
0017  *       be expensive.
0018  *
0019  * @sa Quotient::Room, NeoChatRoom
0020  */
0021 class SpaceTreeItem
0022 {
0023 public:
0024     explicit SpaceTreeItem(NeoChatConnection *connection,
0025                            SpaceTreeItem *parent = nullptr,
0026                            const QString &id = {},
0027                            const QString &name = {},
0028                            const QString &canonicalAlias = {},
0029                            const QString &topic = {},
0030                            int memberCount = {},
0031                            const QUrl &avatarUrl = {},
0032                            bool allowGuests = {},
0033                            bool worldReadable = {},
0034                            bool isSpace = {},
0035                            Quotient::StateEvents childStates = {});
0036     ~SpaceTreeItem();
0037 
0038     /**
0039      * @brief Return the child at the given row number.
0040      *
0041      * Nullptr is returned if there is no child at the given row number.
0042      */
0043     SpaceTreeItem *child(int number);
0044 
0045     /**
0046      * @brief The number of children this item has.
0047      */
0048     int childCount() const;
0049 
0050     /**
0051      * @brief Insert the given child at the given row number.
0052      */
0053     bool insertChild(int row, SpaceTreeItem *newChild);
0054 
0055     /**
0056      * @brief Remove the child at the given row number.
0057      *
0058      * @return True if a child was removed, false if the given row isn't valid.
0059      */
0060     bool removeChild(int row);
0061 
0062     /**
0063      * @brief Return this item's parent.
0064      */
0065     SpaceTreeItem *parentItem() const;
0066 
0067     /**
0068      * @brief Return the row number for this child relative to the parent.
0069      *
0070      * @return The row value if the child has a parent, 0 otherwise.
0071      */
0072     int row() const;
0073 
0074     /**
0075      * @brief The ID of the room.
0076      */
0077     QString id() const;
0078 
0079     /**
0080      * @brief The name of the room, if any.
0081      */
0082     QString name() const;
0083 
0084     /**
0085      * @brief The canonical alias of the room, if any.
0086      */
0087     QString canonicalAlias() const;
0088 
0089     /**
0090      * @brief The topic of the room, if any.
0091      */
0092     QString topic() const;
0093 
0094     /**
0095      * @brief The number of members joined to the room.
0096      */
0097     int memberCount() const;
0098 
0099     /**
0100      * @brief The URL for the room's avatar, if one is set.
0101      *
0102      * @return A CS API QUrl.
0103      */
0104     QUrl avatarUrl() const;
0105 
0106     /**
0107      * @brief Whether guest users may join the room and participate in it.
0108      *
0109      * If they can, they will be subject to ordinary power level rules like any other users.
0110      */
0111     bool allowGuests() const;
0112 
0113     /**
0114      * @brief Whether the room may be viewed by guest users without joining.
0115      */
0116     bool worldReadable() const;
0117 
0118     /**
0119      * @brief Whether the local user is a member of the rooom.
0120      */
0121     bool isJoined() const;
0122 
0123     /**
0124      * @brief Whether the room is a space.
0125      */
0126     bool isSpace() const;
0127 
0128     /**
0129      * @brief Return the m.space.child state event content for the given child.
0130      */
0131     QJsonObject childStateContent(const SpaceTreeItem *child) const;
0132 
0133     /**
0134      * @brief Set the list of m.space.child events.
0135      *
0136      * Overwrites existing states. Calling with no input will clear the existing states.
0137      */
0138     void setChildStates(Quotient::StateEvents childStates = {});
0139 
0140     /**
0141      * @brief Whether the room is suggested in the parent space.
0142      */
0143     bool isSuggested() const;
0144 
0145 private:
0146     NeoChatConnection *m_connection;
0147     QList<SpaceTreeItem *> m_children;
0148     SpaceTreeItem *m_parentItem;
0149 
0150     QString m_id;
0151     QString m_name;
0152     QString m_canonicalAlias;
0153     QString m_topic;
0154     int m_memberCount;
0155     QUrl m_avatarUrl;
0156     bool m_allowGuests;
0157     bool m_worldReadable;
0158     bool m_isSpace;
0159     Quotient::StateEvents m_childStates;
0160 };