Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
JsonBackend.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <phosphorconfig_export.h>
7
9
10#include <QJsonObject>
11#include <QString>
12
13#include <memory>
14
15QT_BEGIN_NAMESPACE
16class QTimer;
17QT_END_NAMESPACE
18
19namespace PhosphorConfig {
20
21class IGroupPathResolver;
22class JsonBackend;
23
31class PHOSPHORCONFIG_EXPORT JsonGroup : public IGroup
32{
33public:
34 JsonGroup(QJsonObject& root, QString groupName, JsonBackend* backend);
35 ~JsonGroup() override;
36
37 QString readString(const QString& key, const QString& defaultValue = {}) const override;
38 int readInt(const QString& key, int defaultValue = 0) const override;
39 bool readBool(const QString& key, bool defaultValue = false) const override;
40 double readDouble(const QString& key, double defaultValue = 0.0) const override;
41 QColor readColor(const QString& key, const QColor& defaultValue = {}) const override;
42
43 void writeString(const QString& key, const QString& value) override;
44 void writeInt(const QString& key, int value) override;
45 void writeBool(const QString& key, bool value) override;
46 void writeDouble(const QString& key, double value) override;
47 void writeColor(const QString& key, const QColor& value) override;
48 void writeJson(const QString& key, const QJsonValue& value) override;
49 QJsonValue readJson(const QString& key, const QJsonValue& defaultValue = {}) const override;
50
51 bool hasKey(const QString& key) const override;
52 void deleteKey(const QString& key) override;
53 QStringList keyList() const override;
54
55private:
56 QJsonObject groupObject() const;
57 void setGroupObject(const QJsonObject& obj);
61 bool refuseWrite(const char* op) const;
62
63 QJsonObject& m_root;
64 QString m_groupName;
65 JsonBackend* m_backend;
70 bool m_disabled = false;
71};
72
87class PHOSPHORCONFIG_EXPORT JsonBackend : public IBackend
88{
89public:
93 explicit JsonBackend(QString filePath);
94 ~JsonBackend() override;
95
96 // IBackend interface
97 std::unique_ptr<IGroup> group(const QString& name) override;
98 void reparseConfiguration() override;
99 bool sync() override;
100 void deleteGroup(const QString& name) override;
101 QString readRootString(const QString& key, const QString& defaultValue = {}) const override;
102 void writeRootString(const QString& key, const QString& value) override;
103 void removeRootKey(const QString& key) override;
104 QStringList groupList() const override;
105
109 void setPathResolver(std::shared_ptr<IGroupPathResolver> resolver) override;
110 std::shared_ptr<IGroupPathResolver> pathResolver() const override;
111
114 void setRootGroupName(const QString& name);
115 QString rootGroupName() const;
116
122 void setVersionStamp(const QString& key, int version) override;
123
127 std::pair<QString, int> versionStamp() const override;
128
136 bool applyMigration(const Schema& schema) override;
137
142 static bool writeJsonAtomically(const QString& filePath, const QJsonObject& root);
143
146 QJsonObject jsonRootSnapshot() const;
147
150 QString filePath() const;
151
155
161 void replaceRoot(QJsonObject root);
162
164 enum class SyncPolicy {
168 Synchronous,
182 Deferred,
183 };
184
193 void setSyncPolicy(SyncPolicy policy, int debounceMs = 500);
195
202
203private:
204 friend class JsonGroup;
205
206 void loadFromDisk();
207 void markDirty();
212 bool flushNow();
213 void incActiveGroupCount();
214 void decActiveGroupCount();
215 int activeGroupCount() const;
216
221 bool shouldWarnOnce(const char* tag, const QString& key);
222
223 struct Data;
224 std::unique_ptr<Data> d;
225};
226
227} // namespace PhosphorConfig
Pluggable configuration backend.
Definition IBackend.h:150
Scoped view into a single configuration group.
Definition IBackend.h:38
Atomic-write JSON configuration backend.
Definition JsonBackend.h:88
SyncPolicy
Sync timing policy. See setSyncPolicy for the full contract.
Definition JsonBackend.h:164
void setRootGroupName(const QString &name)
Change the JSON object that receives ungrouped writeRootString calls.
QStringList groupList() const override
Enumerate every top-level group name.
QString rootGroupName() const
void writeRootString(const QString &key, const QString &value) override
bool applyMigration(const Schema &schema) override
Run schema's migration chain against the in-memory root, and if any step bumps the version stamp,...
SyncPolicy syncPolicy() const
QString filePath() const
Backing file path (useful for migration-chain callers that need to rewrite via writeJsonAtomically du...
void reparseConfiguration() override
Re-read configuration from disk, discarding any pending in-memory changes.
QString readRootString(const QString &key, const QString &defaultValue={}) const override
Read/write ungrouped (root-level) keys.
std::unique_ptr< IGroup > group(const QString &name) override
Return a scoped view into the named group.
void replaceRoot(QJsonObject root)
Replace the in-memory document with root.
void setPathResolver(std::shared_ptr< IGroupPathResolver > resolver) override
Attach a custom path resolver.
void setSyncPolicy(SyncPolicy policy, int debounceMs=500)
Configure when sync() commits to disk.
bool flushPending()
Flush any pending deferred-sync write to disk now.
std::pair< QString, int > versionStamp() const override
Currently-installed version stamp as {key, version}.
void setVersionStamp(const QString &key, int version) override
Stamp-on-sync: if non-empty, every sync() that writes the file will insert key = version when the key...
std::shared_ptr< IGroupPathResolver > pathResolver() const override
Currently-installed resolver, or nullptr when none is set (also the default for backends that ignore ...
void removeRootKey(const QString &key) override
void deleteGroup(const QString &name) override
Delete an entire group and everything inside it.
static bool writeJsonAtomically(const QString &filePath, const QJsonObject &root)
Atomically write root to filePath (temp file + rename via QSaveFile).
JsonBackend(QString filePath)
Construct a backend backed by filePath.
void clearDirty()
Clear the dirty flag without writing.
QJsonObject jsonRootSnapshot() const
Read-only access to the in-memory document.
bool sync() override
Flush pending writes to disk.
Scoped group view backed by a JsonBackend.
Definition JsonBackend.h:32
QStringList keyList() const override
Enumerate scalar leaf keys directly under this group.
QJsonValue readJson(const QString &key, const QJsonValue &defaultValue={}) const override
Read a structured JSON value.
QColor readColor(const QString &key, const QColor &defaultValue={}) const override
bool hasKey(const QString &key) const override
void writeInt(const QString &key, int value) override
bool readBool(const QString &key, bool defaultValue=false) const override
QString readString(const QString &key, const QString &defaultValue={}) const override
void writeString(const QString &key, const QString &value) override
Write a string.
void writeBool(const QString &key, bool value) override
int readInt(const QString &key, int defaultValue=0) const override
void writeJson(const QString &key, const QJsonValue &value) override
Write a structured JSON value (array/object/scalar) natively where supported, or as a compact-JSON st...
void writeDouble(const QString &key, double value) override
double readDouble(const QString &key, double defaultValue=0.0) const override
JsonGroup(QJsonObject &root, QString groupName, JsonBackend *backend)
void deleteKey(const QString &key) override
void writeColor(const QString &key, const QColor &value) override
Definition IBackend.h:19
Declarative description of a configuration store.
Definition Schema.h:89