// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #pragma once #include #include #include #include #include #include #include class ChatListModel; // The folders the conversation list is split into. // // Two of them are not stored at all: group chats and one-to-one chats are told // apart by the chat itself, so a new conversation lands in the right folder // without anybody filing it and a chat that turns from a room into a direct // message moves by itself. Everything the user creates sits on top of those and // owns its membership, because a folder somebody named is a statement about // where a conversation belongs and the app has no way to guess it. // // The transport is deliberately not a folder. Which protocol a conversation // runs over is the transport rail's business, and a list of chats that hides // everything but the current protocol is a list you have to switch protocols to // read. class ChatFolders : public QObject { Q_OBJECT QML_ELEMENT QML_SINGLETON // The conversation list, so a chat can be asked which folder it is in and // the two built-in folders can count themselves. Set from QML. Q_PROPERTY(QObject *chatList READ chatListObj WRITE setChatListObj NOTIFY chatListChanged) // The transport rail's current mode, bound from QML: a folder belongs to the // protocol it was made in, and the counts follow the rows the list actually // shows - which hide every other protocol's chats. Q_PROPERTY(int connectionMode READ connectionMode WRITE setConnectionMode NOTIFY connectionModeChanged) // "Show chats from all protocols": when set, the counts count everything // the list holds, and filing stops caring which mode a folder was made in. Q_PROPERTY(bool includeAllProtocols READ includeAllProtocols WRITE setIncludeAllProtocols NOTIFY includeAllProtocolsChanged) // One map per folder, in the order they are drawn: id, name, editable, // count. A Repeater over this is the list, so adding a folder is adding a // section and nothing else has to learn about it. Q_PROPERTY(QVariantList folders READ folders NOTIFY foldersChanged) public: explicit ChatFolders(QObject *parent = nullptr); static ChatFolders *create(QQmlEngine *, QJSEngine *) { return new ChatFolders; } QObject *chatListObj() const; void setChatListObj(QObject *obj); int connectionMode() const { return m_connectionMode; } void setConnectionMode(int mode); bool includeAllProtocols() const { return m_includeAllProtocols; } void setIncludeAllProtocols(bool include); QVariantList folders() const { return m_foldersCache; } // The folder a conversation belongs to. User folders win over the built-in // split, because filing a chat somewhere is the whole point of having them. Q_INVOKABLE QString folderFor(const QString &chatId) const; // The folders a user can put a chat into: the built-in pair plus the ones // that exist, which is what a context menu offers. Q_INVOKABLE QVariantList assignableFolders() const { return m_foldersCache; } Q_INVOKABLE QString addFolder(const QString &name); Q_INVOKABLE void renameFolder(const QString &folderId, const QString &name); Q_INVOKABLE void removeFolder(const QString &folderId); // Empty folderId files the chat back under the built-in split. Q_INVOKABLE void assign(const QString &chatId, const QString &folderId); // The folders that hold something, for the menu that offers a destination. Q_INVOKABLE bool isUserFolder(const QString &folderId) const; Q_SIGNALS: void chatListChanged(); void connectionModeChanged(); void includeAllProtocolsChanged(); void foldersChanged(); private: struct Folder { QString id; QString name; // Empty for the two built-in folders: their membership is worked out // from the chat list rather than kept, so a new chat needs no filing. QStringList chatIds; // The transport mode the folder was made in. Filing is per protocol, // like the rows are: a folder counts the chats of its own mode only, // and a chat filed in one mode falls back to the built-in split in // another. Legacy folders written before this field existed load with // the mode that was current when they were first read. int mode = 0; }; int indexOf(const QString &folderId) const; void rebuild(); void save() const; void load(); bool chatIsRoom(const QString &chatId) const; QPointer m_chatList; QVector m_folders; // Name and membership as written down, so a restart does not ask again and // a chat that is currently missing from the list does not lose its folder. QVector m_stored; QVariantList m_foldersCache; bool m_loaded = false; int m_connectionMode = 0; bool m_includeAllProtocols = false; };