// 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 #include // the toggle reads AppSettings in main.cpp: start value and change hook. class HistoryManager : public QObject { Q_OBJECT QML_ELEMENT QML_SINGLETON Q_PROPERTY(bool historySavingEnabled READ historySavingEnabled WRITE setHistorySavingEnabled NOTIFY historySavingEnabledChanged) Q_PROPERTY(bool historyEncryptionAvailable READ historyEncryptionAvailable NOTIFY historyEncryptionChanged) Q_PROPERTY(int maxMessagesPerChat READ maxMessagesPerChat WRITE setMaxMessagesPerChat NOTIFY maxMessagesPerChatChanged) public: explicit HistoryManager(QObject *parent = nullptr); static HistoryManager *create(QQmlEngine *, QJSEngine *) { return new HistoryManager; } bool historySavingEnabled() const { return m_savingEnabled; } void setHistorySavingEnabled(bool enabled); // False when the log files cannot be sealed and will not be written at all: // no key store means no on-disk history, never a plaintext one. The banner // in Main.qml reads this on startup, in case the failure predates the window. bool historyEncryptionAvailable() const { return m_historyKeyValid && !m_storeUnavailable; } int maxMessagesPerChat() const { return m_maxMessagesPerChat; } void setMaxMessagesPerChat(int count); // Seals every history file an older build left in plain text, in place and // only over files this run can actually parse. Called once from main.cpp // after the window is up; append()/load() also seal a legacy chat as soon // as it is touched, so nothing depends on this call happening. Q_INVOKABLE void migratePlaintextHistory(); Q_INVOKABLE QVariantList load(const QString &chatId); Q_INVOKABLE void append(const QString &chatId, const QVariantMap &entry); Q_INVOKABLE void replaceAll(const QString &chatId, const QVariantList &entries); Q_INVOKABLE QVariantList loadCallLog(); Q_INVOKABLE void addCall(const QVariantMap &entry); // the conversation list, stored beside the logs rather than derived from // them: a row carries what no message log holds - a switched-off peer's // name and last-seen. Q_INVOKABLE QVariantList loadChatIndex(); Q_INVOKABLE void saveChatIndex(const QVariantList &entries); // the file naming scheme, public so the rule can be checked without a // filesystem. legacyStemFor() is what every build up to now wrote: every // character outside [\w-] replaced by an underscore, which is not injective - // "192.168.1.5" and the id "192_168_1_5" are one file. stemFor() appends a // digest of the id whenever a substitution happened, and returns the id // untouched when none did, so ids already safe as file names keep their names. static QString stemFor(const QString &chatId); static QString legacyStemFor(const QString &chatId); Q_SIGNALS: void historySavingEnabledChanged(); void historyAppended(const QString &chatId, const QVariantMap &entry); void historyEncryptionChanged(); void maxMessagesPerChatChanged(); // the log files cannot be sealed: without a key store they stay off the // disk entirely rather than being written in the clear. void historyStoreUnavailable(const QString &reason); private: // not const: the first call for a chat moves a log written under the old // name to the new one. QString filePathFor(const QString &chatId); QDir historyDir() const; void writeChatFile(const QString &chatId, const QVariantList &msgs); void writeEncryptedTo(const QString &path, const QVariantList &msgs); bool ensureHistoryKey(); bool m_savingEnabled = true; int m_maxMessagesPerChat = 1000; // chats whose file exists but will not parse, so appends stay in memory only QSet m_unreadable; // chats whose legacy file name has already been looked for, so the rename // costs two stat() calls once rather than one per appended message QSet m_namesChecked; QByteArray m_historyKey; bool m_historyKeyValid = false; // Store gone and reported; appends keep happening in memory, nothing new // reaches the disk, and the failure is not re-reported per message. bool m_storeUnavailable = false; bool m_migrationRan = false; };