52 lines
1.1 KiB
C++
52 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <qfilesystemwatcher.h>
|
|
#include <qjsonvalue.h>
|
|
#include <qobject.h>
|
|
#include <qtimer.h>
|
|
|
|
namespace ZShell::settings {
|
|
|
|
class SettingsFile : public QObject {
|
|
Q_OBJECT
|
|
|
|
public:
|
|
enum class LoadResult : quint8 {
|
|
Unchanged,
|
|
Changed,
|
|
ParseError,
|
|
Error
|
|
};
|
|
|
|
explicit SettingsFile(QString path, QObject* parent = nullptr);
|
|
|
|
[[nodiscard]] std::optional<QJsonValue> read() const;
|
|
void write(const QJsonValue& json);
|
|
|
|
LoadResult load(bool reportErrors = true);
|
|
|
|
signals:
|
|
void changed(); // Data changed, not file watcher event
|
|
void readFailed(const QString& error);
|
|
void writeFailed(const QString& error);
|
|
|
|
private:
|
|
QString m_path;
|
|
QFileSystemWatcher* m_watcher;
|
|
QTimer* m_saveDebounce;
|
|
QTimer* m_loadDebounce;
|
|
int m_loadRetries;
|
|
std::optional<QJsonValue> m_lastData;
|
|
std::optional<QJsonValue> m_pendingWrite;
|
|
|
|
void onFileChanged();
|
|
void onDirChanged();
|
|
|
|
void initWatcher();
|
|
void scheduleLoad();
|
|
void onLoadDebounced();
|
|
void save();
|
|
};
|
|
|
|
} // namespace ZShell::settings
|