File indexing completed on 2024-05-05 16:58:54

0001 // SPDX-FileCopyrightText: 2022 Snehit Sah <hi@snehit.dev>
0002 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003 
0004 #pragma once
0005 
0006 #include <QHash>
0007 #include <QObject>
0008 #include <QString>
0009 #include <QVector>
0010 
0011 namespace Quotient
0012 {
0013 class Room;
0014 }
0015 
0016 /**
0017  * @class SpaceHierarchyCache
0018  *
0019  * A class to store the child spaces for each space.
0020  *
0021  * Spaces are cached on startup or when the user enters a new space.
0022  */
0023 class SpaceHierarchyCache : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 public:
0028     static SpaceHierarchyCache &instance()
0029     {
0030         static SpaceHierarchyCache _instance;
0031         return _instance;
0032     }
0033 
0034     /**
0035      * @brief Return the list of child rooms for the given space ID.
0036      */
0037     [[nodiscard]] QVector<QString> &getRoomListForSpace(const QString &spaceId, bool updateCache);
0038 
0039     /**
0040      * @brief Returns whether the space is a child space of any other space.
0041      */
0042     [[nodiscard]] bool isChildSpace(const QString &spaceId) const;
0043 
0044 Q_SIGNALS:
0045     void spaceHierarchyChanged();
0046 
0047 private Q_SLOTS:
0048     void addSpaceToHierarchy(Quotient::Room *room);
0049     void removeSpaceFromHierarchy(Quotient::Room *room);
0050 
0051 private:
0052     explicit SpaceHierarchyCache(QObject *parent = nullptr);
0053 
0054     QVector<QString> m_activeSpaceRooms;
0055     QHash<QString, QVector<QString>> m_spaceHierarchy;
0056     void cacheSpaceHierarchy();
0057     void populateSpaceHierarchy(const QString &spaceId);
0058 };