Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
QSettingsBackend.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 <QMap>
11#include <QSettings>
12#include <QString>
13#include <QVariant>
14
15#include <memory>
16
17namespace PhosphorConfig {
18
19class QSettingsBackend;
20
27class PHOSPHORCONFIG_EXPORT QSettingsGroup : public IGroup
28{
29public:
30 QSettingsGroup(QSettings* settings, QString groupName, QSettingsBackend* backend);
31 ~QSettingsGroup() override;
32
33 QString readString(const QString& key, const QString& defaultValue = {}) const override;
34 int readInt(const QString& key, int defaultValue = 0) const override;
35 bool readBool(const QString& key, bool defaultValue = false) const override;
36 double readDouble(const QString& key, double defaultValue = 0.0) const override;
37 QColor readColor(const QString& key, const QColor& defaultValue = {}) const override;
38
39 void writeString(const QString& key, const QString& value) override;
40 void writeInt(const QString& key, int value) override;
41 void writeBool(const QString& key, bool value) override;
42 void writeDouble(const QString& key, double value) override;
43 void writeColor(const QString& key, const QColor& value) override;
44
45 bool hasKey(const QString& key) const override;
46 void deleteKey(const QString& key) override;
47 QStringList keyList() const override;
48
49private:
53 bool refuseWrite(const char* op) const;
54
55 QSettings* m_settings;
56 QString m_group;
57 QSettingsBackend* m_backend;
61 bool m_disabled = false;
62};
63
68class PHOSPHORCONFIG_EXPORT QSettingsBackend : public IBackend
69{
70public:
71 explicit QSettingsBackend(const QString& filePath);
73
74 // IBackend interface
75 std::unique_ptr<IGroup> group(const QString& name) override;
76 void reparseConfiguration() override;
77 bool sync() override;
78 void deleteGroup(const QString& name) override;
79 QString readRootString(const QString& key, const QString& defaultValue = {}) const override;
80 void writeRootString(const QString& key, const QString& value) override;
81 void removeRootKey(const QString& key) override;
82 QStringList groupList() const override;
83
88 static QMap<QString, QVariant> readConfigFromDisk(const QString& filePath);
89
90private:
91 friend class QSettingsGroup;
92
93 void incActiveGroupCount();
94 void decActiveGroupCount();
95 int activeGroupCount() const;
96
97 QString m_filePath;
98 std::unique_ptr<QSettings> m_settings;
99 int m_activeGroupCount = 0;
100};
101
102} // namespace PhosphorConfig
Pluggable configuration backend.
Definition IBackend.h:150
Scoped view into a single configuration group.
Definition IBackend.h:38
QSettings-backed configuration backend (INI format).
Definition QSettingsBackend.h:69
void removeRootKey(const QString &key) override
QSettingsBackend(const QString &filePath)
static QMap< QString, QVariant > readConfigFromDisk(const QString &filePath)
Read an INI config from an arbitrary file path, bypassing Qt's QConfFile cache.
QStringList groupList() const override
Enumerate every top-level group name.
bool sync() override
Flush pending writes to disk.
QString readRootString(const QString &key, const QString &defaultValue={}) const override
Read/write ungrouped (root-level) keys.
void deleteGroup(const QString &name) override
Delete an entire group and everything inside it.
void writeRootString(const QString &key, const QString &value) override
std::unique_ptr< IGroup > group(const QString &name) override
Return a scoped view into the named group.
void reparseConfiguration() override
Re-read configuration from disk, discarding any pending in-memory changes.
Scoped group view backed by QSettings.
Definition QSettingsBackend.h:28
void writeDouble(const QString &key, double value) override
QString readString(const QString &key, const QString &defaultValue={}) const override
void writeColor(const QString &key, const QColor &value) override
void writeBool(const QString &key, bool value) override
QStringList keyList() const override
Enumerate scalar leaf keys directly under this group.
int readInt(const QString &key, int defaultValue=0) const override
void writeInt(const QString &key, int value) override
QSettingsGroup(QSettings *settings, QString groupName, QSettingsBackend *backend)
void writeString(const QString &key, const QString &value) override
Write a string.
bool hasKey(const QString &key) const override
double readDouble(const QString &key, double defaultValue=0.0) const override
QColor readColor(const QString &key, const QColor &defaultValue={}) const override
bool readBool(const QString &key, bool defaultValue=false) const override
void deleteKey(const QString &key) override
Definition IBackend.h:19