// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "ChatFolders.h" #include "ChatAddress.h" #include "ChatListModel.h" #include #include #include #include namespace { // The two folders that are worked out rather than stored. A chat is a room or // a one-to-one; neither name is a word the user can change, so both carry // themselves as their id and are never written down. constexpr auto kRoomFolderId = "rooms"; constexpr auto kDirectFolderId = "direct"; constexpr auto kFoldersGroup = "ChatFolders"; // which transport rail mode a chat belongs to, the same mapping the // conversation list's transportGroup role uses - a folder's counts and the // rows the list shows must answer the same question the same way, or a // heading lies about what is under it. int transportModeOf(const QString &chatId) { switch (koutnet::chatid::transportOf(chatId)) { case koutnet::chatid::Transport::Matrix: return 1; case koutnet::chatid::Transport::Telegram: return 2; // A chat of a transport the rail does not switch to is shown in no mode, // so it counts into no mode either. case koutnet::chatid::Transport::RocketChat: case koutnet::chatid::Transport::Tox: case koutnet::chatid::Transport::Reserved: return -1; case koutnet::chatid::Transport::Lan: break; } return 0; } QString roomFolderName() { return i18nc("@title:group conversation list section, chats with several people in them", "Group chats"); } QString directFolderName() { return i18nc("@title:group conversation list section, one-to-one chats", "Direct messages"); } QString defaultFolderName() { return i18nc("@item:folder a new conversation folder before it is named", "New folder"); } } ChatFolders::ChatFolders(QObject *parent) : QObject(parent) { } QObject *ChatFolders::chatListObj() const { return m_chatList; } void ChatFolders::setChatListObj(QObject *obj) { auto *list = qobject_cast(obj); if (m_chatList == list) return; if (m_chatList) disconnect(m_chatList, nullptr, this, nullptr); m_chatList = list; if (m_chatList) { // A chat that arrives, or is found to be a direct message after all, // changes what the built-in folders hold, so the headings re-count. connect(m_chatList, &ChatListModel::countsChanged, this, [this] { load(); rebuild(); }); } Q_EMIT chatListChanged(); load(); rebuild(); } void ChatFolders::setConnectionMode(int mode) { if (m_connectionMode == mode) return; m_connectionMode = mode; Q_EMIT connectionModeChanged(); rebuild(); } void ChatFolders::setIncludeAllProtocols(bool include) { if (m_includeAllProtocols == include) return; m_includeAllProtocols = include; Q_EMIT includeAllProtocolsChanged(); rebuild(); } int ChatFolders::indexOf(const QString &folderId) const { for (int i = 0; i < m_folders.size(); ++i) { if (m_folders.at(i).id == folderId) return i; } return -1; } bool ChatFolders::isUserFolder(const QString &folderId) const { const int at = indexOf(folderId); return at >= 0 && m_folders.at(at).id != QLatin1String(kRoomFolderId) && m_folders.at(at).id != QLatin1String(kDirectFolderId); } bool ChatFolders::chatIsRoom(const QString &chatId) const { if (m_chatList) { // The backend's own answer, which is the only one that knows: a Matrix // room and a Matrix direct chat have the same id shape. const QVariantMap info = m_chatList->chatInfo(chatId); if (info.contains(QStringLiteral("isRoom"))) return info.value(QStringLiteral("isRoom")).toBool(); } // Not in the list, so nothing is known about this conversation beyond where it // came from, and the same default the conversation list itself starts from: a // server-backed chat has a room behind it, a LAN one is always two people. // A missing direct message is the rarer case, and folderFor() keeps a filed // chat filed regardless. return koutnet::chatid::transportOf(chatId) != koutnet::chatid::Transport::Lan; } QString ChatFolders::folderFor(const QString &chatId) const { if (chatId.isEmpty()) return QString(); // A filed chat stays filed even while the conversation is missing from the // list, so a folder does not empty itself the moment a room is not synced. // Filing is per protocol like the rows are: in another mode the chat falls // back to the built-in split, unless everything is being shown at once. for (const Folder &f : m_stored) { if (!m_includeAllProtocols && f.mode != m_connectionMode) continue; if (f.chatIds.contains(chatId)) return f.id; } return chatIsRoom(chatId) ? QLatin1String(kRoomFolderId) : QLatin1String(kDirectFolderId); } void ChatFolders::rebuild() { m_folders.clear(); // The built-in pair first: they are where a chat lands when nobody has filed // it anywhere, and a section a user made always comes after them. The // sections a user made belong to the mode they were made in, so a folder of // another protocol is simply not offered here. m_folders.append({QLatin1String(kRoomFolderId), roomFolderName(), {}, m_connectionMode}); m_folders.append({QLatin1String(kDirectFolderId), directFolderName(), {}, m_connectionMode}); for (const Folder &f : m_stored) { if (m_includeAllProtocols || f.mode == m_connectionMode) m_folders.append(f); } // What was written down is the filing, not the size of the folder: this pass // counts what is in the list, and starting from the stored list as well // added every filed chat to its own folder on each rebuild, so the number on // the heading grew every time anything changed. for (Folder &f : m_folders) f.chatIds.clear(); int roomChats = 0; int directChats = 0; if (m_chatList) { for (int row = 0; row < m_chatList->rowCount(); ++row) { const QString chatId = m_chatList->data(m_chatList->index(row), ChatListModel::ChatIdRole).toString(); if (chatId.isEmpty()) continue; // The counts follow the rows: the list hides every protocol but the // rail's current one (or all of them, on request), so a folder that // counted the hidden ones would promise rows it does not draw. if (!m_includeAllProtocols && transportModeOf(chatId) != m_connectionMode) continue; const QString folderId = folderFor(chatId); const int at = indexOf(folderId); if (at < 0) continue; m_folders[at].chatIds.append(chatId); if (folderId == QLatin1String(kRoomFolderId)) ++roomChats; else if (folderId == QLatin1String(kDirectFolderId)) ++directChats; } } QVariantList out; out.reserve(m_folders.size()); for (int i = 0; i < m_folders.size(); ++i) { const Folder &f = m_folders.at(i); QVariantMap m; m[QStringLiteral("id")] = f.id; m[QStringLiteral("name")] = f.name; m[QStringLiteral("editable")] = isUserFolder(f.id); const int count = f.id == QLatin1String(kRoomFolderId) ? roomChats : f.id == QLatin1String(kDirectFolderId) ? directChats : f.chatIds.size(); m[QStringLiteral("count")] = count; out.append(m); } m_foldersCache = out; Q_EMIT foldersChanged(); } QString ChatFolders::addFolder(const QString &name) { load(); Folder f; f.name = name.trimmed().isEmpty() ? defaultFolderName() : name.trimmed(); // An id the user never sees, so two folders named alike stay two folders. f.id = QStringLiteral("f%1").arg(QUuid::createUuid().toString(QUuid::Id128)); f.mode = m_connectionMode; m_stored.append(f); rebuild(); save(); return f.id; } void ChatFolders::renameFolder(const QString &folderId, const QString &name) { if (!isUserFolder(folderId)) return; load(); for (Folder &f : m_stored) { if (f.id != folderId) continue; const QString trimmed = name.trimmed(); // An empty name is not a folder with no name, it is the name it had. if (!trimmed.isEmpty()) f.name = trimmed; break; } rebuild(); save(); } void ChatFolders::removeFolder(const QString &folderId) { if (!isUserFolder(folderId)) return; load(); for (int i = 0; i < m_stored.size(); ++i) { if (m_stored.at(i).id == folderId) { m_stored.remove(i); break; } } rebuild(); save(); } void ChatFolders::assign(const QString &chatId, const QString &folderId) { if (chatId.isEmpty()) return; load(); if (!isUserFolder(folderId)) { // Filing back under the built-in split means forgetting the folder. for (Folder &f : m_stored) { f.chatIds.removeAll(chatId); } rebuild(); save(); return; } // One folder at a time: a chat in two folders appears twice, and the same // conversation listed twice is a bug rather than a feature. for (Folder &f : m_stored) { f.chatIds.removeAll(chatId); if (f.id == folderId) f.chatIds.append(chatId); } rebuild(); save(); } void ChatFolders::load() { if (m_loaded) return; m_loaded = true; QSettings settings; settings.beginGroup(QLatin1String(kFoldersGroup)); const int count = settings.beginReadArray(QStringLiteral("folders")); for (int i = 0; i < count; ++i) { settings.setArrayIndex(i); Folder f; f.id = settings.value(QStringLiteral("id")).toString(); f.name = settings.value(QStringLiteral("name")).toString(); if (f.id.isEmpty() || f.name.isEmpty()) continue; f.chatIds = settings.value(QStringLiteral("chats")).toStringList(); // Folders written before the field existed belong to the mode that was // current when they were first read, which is the only mode their // counts were ever right for. f.mode = settings.value(QStringLiteral("mode"), m_connectionMode).toInt(); m_stored.append(f); } settings.endArray(); settings.endGroup(); } void ChatFolders::save() const { QSettings settings; settings.beginGroup(QLatin1String(kFoldersGroup)); settings.remove(QStringLiteral("folders")); settings.beginWriteArray(QStringLiteral("folders"), m_stored.size()); for (int i = 0; i < m_stored.size(); ++i) { settings.setArrayIndex(i); settings.setValue(QStringLiteral("id"), m_stored.at(i).id); settings.setValue(QStringLiteral("name"), m_stored.at(i).name); settings.setValue(QStringLiteral("chats"), m_stored.at(i).chatIds); settings.setValue(QStringLiteral("mode"), m_stored.at(i).mode); } settings.endArray(); settings.endGroup(); }