nosignal-shell/plugin/src/Caelestia/Config/rootconfig.hpp
2026-04-12 03:01:58 +10:00

52 lines
1.4 KiB
C++

#pragma once
#include "configobject.hpp"
#include <optional>
#include <qfilesystemwatcher.h>
#include <qtimer.h>
namespace caelestia::config {
// Intermediate base for singleton config roots (GlobalConfig, TokenConfig).
// Provides file-backed persistence, save/reload, and lifecycle signals.
class RootConfig : public ConfigObject {
Q_OBJECT
public:
explicit RootConfig(QObject* parent = nullptr);
void setupFileBackend(const QString& path, const QString& screen = {});
void saveToFile();
// Returns nullopt if retrying, empty string on success, error message on failure.
[[nodiscard]] std::optional<QString> reloadFromFile();
[[nodiscard]] bool recentlySaved() const { return m_recentlySaved; }
Q_INVOKABLE void save();
Q_INVOKABLE void reload();
signals:
void loaded(const QString& screen);
void loadFailed(const QString& error, const QString& screen);
void saved(const QString& screen);
void saveFailed(const QString& error, const QString& screen);
private:
void updateWatch();
void onWatcherEvent();
QString m_filePath;
QString m_screen;
QString m_watchedDir;
bool m_recentlySaved = false;
QFileSystemWatcher* m_watcher = nullptr;
QTimer* m_saveTimer = nullptr;
QTimer* m_cooldownTimer = nullptr;
QTimer* m_retryTimer = nullptr;
QTimer* m_reloadDebounce = nullptr;
int m_parseRetries = 0;
};
} // namespace caelestia::config