1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSETTINGS_H
5#define QSETTINGS_H
6
7#include <QtCore/qobject.h>
8#include <QtCore/qvariant.h>
9#include <QtCore/qstring.h>
10#include <QtCore/qscopedpointer.h>
11
12QT_REQUIRE_CONFIG(settings);
13
14#include <ctype.h>
15
16QT_BEGIN_NAMESPACE
17
18#ifdef Status // we seem to pick up a macro Status --> int somewhere
19#undef Status
20#endif
21
22class QIODevice;
23class QSettingsPrivate;
24
25#ifndef QT_NO_QOBJECT
26class Q_CORE_EXPORT QSettings : public QObject
27#else
28class Q_CORE_EXPORT QSettings
29#endif
30{
31#ifndef QT_NO_QOBJECT
32 Q_OBJECT
33#else
34 QScopedPointer<QSettingsPrivate> d_ptr;
35#endif
36 Q_DECLARE_PRIVATE(QSettings)
37
38public:
39 enum Status {
40 NoError = 0,
41 AccessError,
42 FormatError
43 };
44#ifndef QT_NO_QOBJECT
45 Q_ENUM(Status)
46#endif
47
48 enum Format {
49 NativeFormat = 0,
50 IniFormat = 1,
51
52#if defined(Q_OS_WIN) || defined(Q_QDOC)
53 Registry32Format = 2,
54 Registry64Format = 3,
55#endif
56
57#if defined(Q_OS_WASM) || defined(Q_QDOC)
58 WebLocalStorageFormat = 4,
59 WebIndexedDBFormat = 5,
60#endif
61
62 InvalidFormat = 16,
63 CustomFormat1,
64 CustomFormat2,
65 CustomFormat3,
66 CustomFormat4,
67 CustomFormat5,
68 CustomFormat6,
69 CustomFormat7,
70 CustomFormat8,
71 CustomFormat9,
72 CustomFormat10,
73 CustomFormat11,
74 CustomFormat12,
75 CustomFormat13,
76 CustomFormat14,
77 CustomFormat15,
78 CustomFormat16
79 };
80#ifndef QT_NO_QOBJECT
81 Q_ENUM(Format)
82#endif
83
84 enum Scope {
85 UserScope,
86 SystemScope
87 };
88#ifndef QT_NO_QOBJECT
89 Q_ENUM(Scope)
90#endif
91
92#ifndef QT_NO_QOBJECT
93 explicit QSettings(const QString &organization,
94 const QString &application = QString(), QObject *parent = nullptr);
95 QSettings(Scope scope, const QString &organization,
96 const QString &application = QString(), QObject *parent = nullptr);
97 QSettings(Format format, Scope scope, const QString &organization,
98 const QString &application = QString(), QObject *parent = nullptr);
99 QSettings(const QString &fileName, Format format, QObject *parent = nullptr);
100 explicit QSettings(QObject *parent = nullptr);
101 explicit QSettings(Scope scope, QObject *parent = nullptr);
102#else
103 explicit QSettings(const QString &organization,
104 const QString &application = QString());
105 QSettings(Scope scope, const QString &organization,
106 const QString &application = QString());
107 QSettings(Format format, Scope scope, const QString &organization,
108 const QString &application = QString());
109 QSettings(const QString &fileName, Format format);
110 explicit QSettings(Scope scope = UserScope);
111#endif
112 ~QSettings();
113
114 void clear();
115 void sync();
116 Status status() const;
117 bool isAtomicSyncRequired() const;
118 void setAtomicSyncRequired(bool enable);
119
120#if QT_CORE_REMOVED_SINCE(6, 4)
121 void beginGroup(const QString &prefix);
122#endif
123 void beginGroup(QAnyStringView prefix);
124 void endGroup();
125 QString group() const;
126
127#if QT_CORE_REMOVED_SINCE(6, 4)
128 int beginReadArray(const QString &prefix);
129 void beginWriteArray(const QString &prefix, int size = -1);
130#endif
131 int beginReadArray(QAnyStringView prefix);
132 void beginWriteArray(QAnyStringView prefix, int size = -1);
133 void endArray();
134 void setArrayIndex(int i);
135
136 QStringList allKeys() const;
137 QStringList childKeys() const;
138 QStringList childGroups() const;
139 bool isWritable() const;
140
141#if QT_CORE_REMOVED_SINCE(6, 4)
142 void setValue(const QString &key, const QVariant &value);
143 QVariant value(const QString &key, const QVariant &defaultValue) const;
144 QVariant value(const QString &key) const;
145#endif
146 void setValue(QAnyStringView key, const QVariant &value);
147 QVariant value(QAnyStringView key, const QVariant &defaultValue) const;
148 QVariant value(QAnyStringView key) const;
149
150#if QT_CORE_REMOVED_SINCE(6, 4)
151 void remove(const QString &key);
152 bool contains(const QString &key) const;
153#endif
154 void remove(QAnyStringView key);
155 bool contains(QAnyStringView key) const;
156
157 void setFallbacksEnabled(bool b);
158 bool fallbacksEnabled() const;
159
160 QString fileName() const;
161 Format format() const;
162 Scope scope() const;
163 QString organizationName() const;
164 QString applicationName() const;
165
166 static void setDefaultFormat(Format format);
167 static Format defaultFormat();
168 static void setPath(Format format, Scope scope, const QString &path);
169
170 typedef QMap<QString, QVariant> SettingsMap;
171 typedef bool (*ReadFunc)(QIODevice &device, SettingsMap &map);
172 typedef bool (*WriteFunc)(QIODevice &device, const SettingsMap &map);
173
174 static Format registerFormat(const QString &extension, ReadFunc readFunc, WriteFunc writeFunc,
175 Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);
176
177protected:
178#ifndef QT_NO_QOBJECT
179 bool event(QEvent *event) override;
180#endif
181
182private:
183 Q_DISABLE_COPY(QSettings)
184};
185
186QT_END_NAMESPACE
187
188#endif // QSETTINGS_H
189