author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifndef QSETTINGS_P_H |
|
43 |
#define QSETTINGS_P_H |
|
44 |
||
45 |
// |
|
46 |
// W A R N I N G |
|
47 |
// ------------- |
|
48 |
// |
|
49 |
// This file is not part of the Qt API. It exists purely as an |
|
50 |
// implementation detail. This header file may change from version to |
|
51 |
// version without notice, or even be removed. |
|
52 |
// |
|
53 |
// We mean it. |
|
54 |
// |
|
55 |
||
56 |
#include "QtCore/qdatetime.h" |
|
57 |
#include "QtCore/qmap.h" |
|
58 |
#include "QtCore/qmutex.h" |
|
59 |
#include "QtCore/qiodevice.h" |
|
60 |
#include "QtCore/qstack.h" |
|
61 |
#include "QtCore/qstringlist.h" |
|
62 |
#ifndef QT_NO_QOBJECT |
|
63 |
#include "private/qobject_p.h" |
|
64 |
#endif |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
#include "private/qscopedpointer_p.h" |
0 | 66 |
|
67 |
#ifdef Q_OS_WIN |
|
68 |
#include "QtCore/qt_windows.h" |
|
69 |
#endif |
|
70 |
||
71 |
QT_BEGIN_NAMESPACE |
|
72 |
||
73 |
#if defined(Q_WS_QWS) |
|
74 |
#define QT_QSETTINGS_ALWAYS_CASE_SENSITIVE_AND_FORGET_ORIGINAL_KEY_ORDER |
|
75 |
#endif |
|
76 |
||
77 |
// used in testing framework |
|
78 |
#define QSETTINGS_P_H_VERSION 3 |
|
79 |
||
80 |
#ifdef QT_QSETTINGS_ALWAYS_CASE_SENSITIVE_AND_FORGET_ORIGINAL_KEY_ORDER |
|
81 |
static const Qt::CaseSensitivity IniCaseSensitivity = Qt::CaseSensitive; |
|
82 |
||
83 |
class QSettingsKey : public QString |
|
84 |
{ |
|
85 |
public: |
|
86 |
inline QSettingsKey(const QString &key, Qt::CaseSensitivity cs, int /* position */ = -1) |
|
87 |
: QString(key) { Q_ASSERT(cs == Qt::CaseSensitive); Q_UNUSED(cs); } |
|
88 |
||
89 |
inline QString originalCaseKey() const { return *this; } |
|
90 |
inline int originalKeyPosition() const { return -1; } |
|
91 |
}; |
|
92 |
#else |
|
93 |
static const Qt::CaseSensitivity IniCaseSensitivity = Qt::CaseInsensitive; |
|
94 |
||
95 |
class QSettingsKey : public QString |
|
96 |
{ |
|
97 |
public: |
|
98 |
inline QSettingsKey(const QString &key, Qt::CaseSensitivity cs, int position = -1) |
|
99 |
: QString(key), theOriginalKey(key), theOriginalKeyPosition(position) |
|
100 |
{ |
|
101 |
if (cs == Qt::CaseInsensitive) |
|
102 |
QString::operator=(toLower()); |
|
103 |
} |
|
104 |
||
105 |
inline QString originalCaseKey() const { return theOriginalKey; } |
|
106 |
inline int originalKeyPosition() const { return theOriginalKeyPosition; } |
|
107 |
||
108 |
private: |
|
109 |
QString theOriginalKey; |
|
110 |
int theOriginalKeyPosition; |
|
111 |
}; |
|
112 |
#endif |
|
113 |
||
114 |
typedef QMap<QSettingsKey, QByteArray> UnparsedSettingsMap; |
|
115 |
typedef QMap<QSettingsKey, QVariant> ParsedSettingsMap; |
|
116 |
||
117 |
class QSettingsGroup |
|
118 |
{ |
|
119 |
public: |
|
120 |
inline QSettingsGroup() |
|
121 |
: num(-1), maxNum(-1) {} |
|
122 |
inline QSettingsGroup(const QString &s) |
|
123 |
: str(s), num(-1), maxNum(-1) {} |
|
124 |
inline QSettingsGroup(const QString &s, bool guessArraySize) |
|
125 |
: str(s), num(0), maxNum(guessArraySize ? 0 : -1) {} |
|
126 |
||
127 |
inline QString name() const { return str; } |
|
128 |
inline QString toString() const; |
|
129 |
inline bool isArray() const { return num != -1; } |
|
130 |
inline int arraySizeGuess() const { return maxNum; } |
|
131 |
inline void setArrayIndex(int i) |
|
132 |
{ num = i + 1; if (maxNum != -1 && num > maxNum) maxNum = num; } |
|
133 |
||
134 |
QString str; |
|
135 |
int num; |
|
136 |
int maxNum; |
|
137 |
}; |
|
138 |
||
139 |
inline QString QSettingsGroup::toString() const |
|
140 |
{ |
|
141 |
QString result; |
|
142 |
result = str; |
|
143 |
if (num > 0) { |
|
144 |
result += QLatin1Char('/'); |
|
145 |
result += QString::number(num); |
|
146 |
} |
|
147 |
return result; |
|
148 |
} |
|
149 |
||
150 |
class Q_AUTOTEST_EXPORT QConfFile |
|
151 |
{ |
|
152 |
public: |
|
153 |
~QConfFile(); |
|
154 |
||
155 |
ParsedSettingsMap mergedKeyMap() const; |
|
156 |
bool isWritable() const; |
|
157 |
||
158 |
static QConfFile *fromName(const QString &name, bool _userPerms); |
|
159 |
static void clearCache(); |
|
160 |
||
161 |
QString name; |
|
162 |
QDateTime timeStamp; |
|
163 |
qint64 size; |
|
164 |
UnparsedSettingsMap unparsedIniSections; |
|
165 |
ParsedSettingsMap originalKeys; |
|
166 |
ParsedSettingsMap addedKeys; |
|
167 |
ParsedSettingsMap removedKeys; |
|
168 |
QAtomicInt ref; |
|
169 |
QMutex mutex; |
|
170 |
bool userPerms; |
|
171 |
||
172 |
private: |
|
173 |
#ifdef Q_DISABLE_COPY |
|
174 |
QConfFile(const QConfFile &); |
|
175 |
QConfFile &operator=(const QConfFile &); |
|
176 |
#endif |
|
177 |
QConfFile(const QString &name, bool _userPerms); |
|
178 |
||
179 |
friend class QConfFile_createsItself; // silences compiler warning |
|
180 |
}; |
|
181 |
||
182 |
class Q_AUTOTEST_EXPORT QSettingsPrivate |
|
183 |
#ifndef QT_NO_QOBJECT |
|
184 |
: public QObjectPrivate |
|
185 |
#endif |
|
186 |
{ |
|
187 |
#ifdef QT_NO_QOBJECT |
|
188 |
QSettings *q_ptr; |
|
189 |
#endif |
|
190 |
Q_DECLARE_PUBLIC(QSettings) |
|
191 |
||
192 |
public: |
|
193 |
QSettingsPrivate(QSettings::Format format); |
|
194 |
QSettingsPrivate(QSettings::Format format, QSettings::Scope scope, |
|
195 |
const QString &organization, const QString &application); |
|
196 |
virtual ~QSettingsPrivate(); |
|
197 |
||
198 |
virtual void remove(const QString &key) = 0; |
|
199 |
virtual void set(const QString &key, const QVariant &value) = 0; |
|
200 |
virtual bool get(const QString &key, QVariant *value) const = 0; |
|
201 |
||
202 |
enum ChildSpec { AllKeys, ChildKeys, ChildGroups }; |
|
203 |
virtual QStringList children(const QString &prefix, ChildSpec spec) const = 0; |
|
204 |
||
205 |
virtual void clear() = 0; |
|
206 |
virtual void sync() = 0; |
|
207 |
virtual void flush() = 0; |
|
208 |
virtual bool isWritable() const = 0; |
|
209 |
virtual QString fileName() const = 0; |
|
210 |
||
211 |
QString actualKey(const QString &key) const; |
|
212 |
void beginGroupOrArray(const QSettingsGroup &group); |
|
213 |
void setStatus(QSettings::Status status) const; |
|
214 |
void requestUpdate(); |
|
215 |
void update(); |
|
216 |
||
217 |
static QString normalizedKey(const QString &key); |
|
218 |
static QSettingsPrivate *create(QSettings::Format format, QSettings::Scope scope, |
|
219 |
const QString &organization, const QString &application); |
|
220 |
static QSettingsPrivate *create(const QString &fileName, QSettings::Format format); |
|
221 |
||
222 |
static void processChild(QString key, ChildSpec spec, QMap<QString, QString> &result); |
|
223 |
||
224 |
// Variant streaming functions |
|
225 |
static QStringList variantListToStringList(const QVariantList &l); |
|
226 |
static QVariant stringListToVariantList(const QStringList &l); |
|
227 |
||
228 |
// parser functions |
|
229 |
static QString variantToString(const QVariant &v); |
|
230 |
static QVariant stringToVariant(const QString &s); |
|
231 |
static void iniEscapedKey(const QString &key, QByteArray &result); |
|
232 |
static bool iniUnescapedKey(const QByteArray &key, int from, int to, QString &result); |
|
233 |
static void iniEscapedString(const QString &str, QByteArray &result, QTextCodec *codec); |
|
234 |
static void iniEscapedStringList(const QStringList &strs, QByteArray &result, QTextCodec *codec); |
|
235 |
static bool iniUnescapedStringList(const QByteArray &str, int from, int to, |
|
236 |
QString &stringResult, QStringList &stringListResult, |
|
237 |
QTextCodec *codec); |
|
238 |
static QStringList splitArgs(const QString &s, int idx); |
|
239 |
||
240 |
/* |
|
241 |
The numeric values of these enums define their search order. For example, |
|
242 |
F_User | F_Organization is searched before F_System | F_Application, |
|
243 |
because their values are respectively 1 and 2. |
|
244 |
*/ |
|
245 |
enum { |
|
246 |
F_Application = 0x0, |
|
247 |
F_Organization = 0x1, |
|
248 |
F_User = 0x0, |
|
249 |
F_System = 0x2, |
|
250 |
NumConfFiles = 4 |
|
251 |
}; |
|
252 |
||
253 |
QSettings::Format format; |
|
254 |
QSettings::Scope scope; |
|
255 |
QString organizationName; |
|
256 |
QString applicationName; |
|
257 |
QTextCodec *iniCodec; |
|
258 |
||
259 |
protected: |
|
260 |
QStack<QSettingsGroup> groupStack; |
|
261 |
QString groupPrefix; |
|
262 |
int spec; |
|
263 |
bool fallbacks; |
|
264 |
bool pendingChanges; |
|
265 |
mutable QSettings::Status status; |
|
266 |
}; |
|
267 |
||
268 |
class QConfFileSettingsPrivate : public QSettingsPrivate |
|
269 |
{ |
|
270 |
public: |
|
271 |
QConfFileSettingsPrivate(QSettings::Format format, QSettings::Scope scope, |
|
272 |
const QString &organization, const QString &application); |
|
273 |
QConfFileSettingsPrivate(const QString &fileName, QSettings::Format format); |
|
274 |
~QConfFileSettingsPrivate(); |
|
275 |
||
276 |
void remove(const QString &key); |
|
277 |
void set(const QString &key, const QVariant &value); |
|
278 |
bool get(const QString &key, QVariant *value) const; |
|
279 |
||
280 |
QStringList children(const QString &prefix, ChildSpec spec) const; |
|
281 |
||
282 |
void clear(); |
|
283 |
void sync(); |
|
284 |
void flush(); |
|
285 |
bool isWritable() const; |
|
286 |
QString fileName() const; |
|
287 |
||
288 |
static bool readIniFile(const QByteArray &data, UnparsedSettingsMap *unparsedIniSections); |
|
289 |
static bool readIniSection(const QSettingsKey §ion, const QByteArray &data, |
|
290 |
ParsedSettingsMap *settingsMap, QTextCodec *codec); |
|
291 |
static bool readIniLine(const QByteArray &data, int &dataPos, int &lineStart, int &lineLen, |
|
292 |
int &equalsPos); |
|
293 |
||
294 |
private: |
|
295 |
void initFormat(); |
|
296 |
void initAccess(); |
|
297 |
void syncConfFile(int confFileNo); |
|
298 |
bool writeIniFile(QIODevice &device, const ParsedSettingsMap &map); |
|
299 |
#ifdef Q_OS_MAC |
|
300 |
bool readPlistFile(const QString &fileName, ParsedSettingsMap *map) const; |
|
301 |
bool writePlistFile(const QString &fileName, const ParsedSettingsMap &map) const; |
|
302 |
#endif |
|
303 |
void ensureAllSectionsParsed(QConfFile *confFile) const; |
|
304 |
void ensureSectionParsed(QConfFile *confFile, const QSettingsKey &key) const; |
|
305 |
||
306 |
QScopedSharedPointer<QConfFile> confFiles[NumConfFiles]; |
|
307 |
QSettings::ReadFunc readFunc; |
|
308 |
QSettings::WriteFunc writeFunc; |
|
309 |
QString extension; |
|
310 |
Qt::CaseSensitivity caseSensitivity; |
|
311 |
int nextPosition; |
|
312 |
}; |
|
313 |
||
314 |
QT_END_NAMESPACE |
|
315 |
||
316 |
#endif // QSETTINGS_P_H |