File indexing completed on 2024-05-05 09:14:02

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