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