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:
0
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 |
#include <qdebug.h> |
|
43 |
#include "qplatformdefs.h" |
|
44 |
#include "qsettings.h" |
|
45 |
||
46 |
#ifndef QT_NO_SETTINGS |
|
47 |
||
48 |
#include "qsettings_p.h" |
|
49 |
#include "qcache.h" |
|
50 |
#include "qfile.h" |
|
51 |
#include "qdir.h" |
|
52 |
#include "qfileinfo.h" |
|
53 |
#include "qmutex.h" |
|
54 |
#include "qlibraryinfo.h" |
|
55 |
#include "qtemporaryfile.h" |
|
56 |
||
57 |
#ifndef QT_NO_TEXTCODEC |
|
58 |
# include "qtextcodec.h" |
|
59 |
#endif |
|
60 |
||
61 |
#ifndef QT_NO_GEOM_VARIANT |
|
62 |
#include "qsize.h" |
|
63 |
#include "qpoint.h" |
|
64 |
#include "qrect.h" |
|
65 |
#endif // !QT_NO_GEOM_VARIANT |
|
66 |
||
67 |
#ifndef QT_NO_QOBJECT |
|
68 |
#include "qcoreapplication.h" |
|
69 |
||
70 |
#ifdef Q_OS_WIN // for homedirpath reading from registry |
|
71 |
#include "qt_windows.h" |
|
72 |
#include "qlibrary.h" |
|
73 |
||
74 |
#endif // Q_OS_WIN |
|
75 |
#endif // QT_NO_QOBJECT |
|
76 |
||
77 |
#ifdef Q_OS_VXWORKS |
|
78 |
# include <ioLib.h> |
|
79 |
#endif |
|
80 |
||
81 |
#include <stdlib.h> |
|
82 |
||
83 |
#ifndef CSIDL_COMMON_APPDATA |
|
84 |
#define CSIDL_COMMON_APPDATA 0x0023 // All Users\Application Data |
|
85 |
#endif |
|
86 |
||
87 |
#ifndef CSIDL_APPDATA |
|
88 |
#define CSIDL_APPDATA 0x001a // <username>\Application Data |
|
89 |
#endif |
|
90 |
||
91 |
// ************************************************************************ |
|
92 |
// QConfFile |
|
93 |
||
94 |
/* |
|
95 |
QConfFile objects are explicitly shared within the application. |
|
96 |
This ensures that modification to the settings done through one |
|
97 |
QSettings object are immediately reflected in other setting |
|
98 |
objects of the same application. |
|
99 |
*/ |
|
100 |
||
101 |
QT_BEGIN_NAMESPACE |
|
102 |
||
103 |
struct QConfFileCustomFormat |
|
104 |
{ |
|
105 |
QString extension; |
|
106 |
QSettings::ReadFunc readFunc; |
|
107 |
QSettings::WriteFunc writeFunc; |
|
108 |
Qt::CaseSensitivity caseSensitivity; |
|
109 |
}; |
|
110 |
||
111 |
typedef QHash<QString, QConfFile *> ConfFileHash; |
|
112 |
typedef QCache<QString, QConfFile> ConfFileCache; |
|
113 |
typedef QHash<int, QString> PathHash; |
|
114 |
typedef QVector<QConfFileCustomFormat> CustomFormatVector; |
|
115 |
||
116 |
Q_GLOBAL_STATIC(ConfFileHash, usedHashFunc) |
|
117 |
Q_GLOBAL_STATIC(ConfFileCache, unusedCacheFunc) |
|
118 |
Q_GLOBAL_STATIC(PathHash, pathHashFunc) |
|
119 |
Q_GLOBAL_STATIC(CustomFormatVector, customFormatVectorFunc) |
|
120 |
Q_GLOBAL_STATIC(QMutex, globalMutex) |
|
121 |
static QSettings::Format globalDefaultFormat = QSettings::NativeFormat; |
|
122 |
||
123 |
#ifndef Q_OS_WIN |
|
124 |
inline bool qt_isEvilFsTypeName(const char *name) |
|
125 |
{ |
|
126 |
return (qstrncmp(name, "nfs", 3) == 0 |
|
127 |
|| qstrncmp(name, "autofs", 6) == 0 |
|
128 |
|| qstrncmp(name, "cachefs", 7) == 0); |
|
129 |
} |
|
130 |
||
131 |
#if defined(Q_OS_BSD4) && !defined(Q_OS_NETBSD) |
|
132 |
QT_BEGIN_INCLUDE_NAMESPACE |
|
133 |
# include <sys/param.h> |
|
134 |
# include <sys/mount.h> |
|
135 |
QT_END_INCLUDE_NAMESPACE |
|
136 |
||
137 |
static bool isLikelyToBeNfs(int handle) |
|
138 |
{ |
|
139 |
struct statfs buf; |
|
140 |
if (fstatfs(handle, &buf) != 0) |
|
141 |
return false; |
|
142 |
return qt_isEvilFsTypeName(buf.f_fstypename); |
|
143 |
} |
|
144 |
||
145 |
#elif defined(Q_OS_LINUX) || defined(Q_OS_HURD) |
|
146 |
QT_BEGIN_INCLUDE_NAMESPACE |
|
147 |
# include <sys/vfs.h> |
|
148 |
# ifdef QT_LINUXBASE |
|
149 |
// LSB 3.2 has fstatfs in sys/statfs.h, sys/vfs.h is just an empty dummy header |
|
150 |
# include <sys/statfs.h> |
|
151 |
# endif |
|
152 |
QT_END_INCLUDE_NAMESPACE |
|
153 |
# ifndef NFS_SUPER_MAGIC |
|
154 |
# define NFS_SUPER_MAGIC 0x00006969 |
|
155 |
# endif |
|
156 |
# ifndef AUTOFS_SUPER_MAGIC |
|
157 |
# define AUTOFS_SUPER_MAGIC 0x00000187 |
|
158 |
# endif |
|
159 |
# ifndef AUTOFSNG_SUPER_MAGIC |
|
160 |
# define AUTOFSNG_SUPER_MAGIC 0x7d92b1a0 |
|
161 |
# endif |
|
162 |
||
163 |
static bool isLikelyToBeNfs(int handle) |
|
164 |
{ |
|
165 |
struct statfs buf; |
|
166 |
if (fstatfs(handle, &buf) != 0) |
|
167 |
return false; |
|
168 |
return buf.f_type == NFS_SUPER_MAGIC |
|
169 |
|| buf.f_type == AUTOFS_SUPER_MAGIC |
|
170 |
|| buf.f_type == AUTOFSNG_SUPER_MAGIC; |
|
171 |
} |
|
172 |
||
173 |
#elif defined(Q_OS_SOLARIS) || defined(Q_OS_IRIX) || defined(Q_OS_AIX) || defined(Q_OS_HPUX) \ |
|
174 |
|| defined(Q_OS_OSF) || defined(Q_OS_QNX) || defined(Q_OS_SCO) \ |
|
175 |
|| defined(Q_OS_UNIXWARE) || defined(Q_OS_RELIANT) || defined(Q_OS_NETBSD) |
|
176 |
QT_BEGIN_INCLUDE_NAMESPACE |
|
177 |
# include <sys/statvfs.h> |
|
178 |
QT_END_INCLUDE_NAMESPACE |
|
179 |
||
180 |
static bool isLikelyToBeNfs(int handle) |
|
181 |
{ |
|
182 |
struct statvfs buf; |
|
183 |
if (fstatvfs(handle, &buf) != 0) |
|
184 |
return false; |
|
185 |
#if defined(Q_OS_NETBSD) |
|
186 |
return qt_isEvilFsTypeName(buf.f_fstypename); |
|
187 |
#else |
|
188 |
return qt_isEvilFsTypeName(buf.f_basetype); |
|
189 |
#endif |
|
190 |
} |
|
191 |
#else |
|
192 |
static inline bool isLikelyToBeNfs(int /* handle */) |
|
193 |
{ |
|
194 |
return true; |
|
195 |
} |
|
196 |
#endif |
|
197 |
||
198 |
static bool unixLock(int handle, int lockType) |
|
199 |
{ |
|
200 |
/* |
|
201 |
NFS hangs on the fcntl() call below when statd or lockd isn't |
|
202 |
running. There's no way to detect this. Our work-around for |
|
203 |
now is to disable locking when we detect NFS (or AutoFS or |
|
204 |
CacheFS, which are probably wrapping NFS). |
|
205 |
*/ |
|
206 |
if (isLikelyToBeNfs(handle)) |
|
207 |
return false; |
|
208 |
||
209 |
struct flock fl; |
|
210 |
fl.l_whence = SEEK_SET; |
|
211 |
fl.l_start = 0; |
|
212 |
fl.l_len = 0; |
|
213 |
fl.l_type = lockType; |
|
214 |
return fcntl(handle, F_SETLKW, &fl) == 0; |
|
215 |
} |
|
216 |
#endif |
|
217 |
||
218 |
QConfFile::QConfFile(const QString &fileName, bool _userPerms) |
|
219 |
: name(fileName), size(0), ref(1), userPerms(_userPerms) |
|
220 |
{ |
|
221 |
usedHashFunc()->insert(name, this); |
|
222 |
} |
|
223 |
||
224 |
QConfFile::~QConfFile() |
|
225 |
{ |
|
226 |
if (usedHashFunc()) |
|
227 |
usedHashFunc()->remove(name); |
|
228 |
} |
|
229 |
||
230 |
ParsedSettingsMap QConfFile::mergedKeyMap() const |
|
231 |
{ |
|
232 |
ParsedSettingsMap result = originalKeys; |
|
233 |
ParsedSettingsMap::const_iterator i; |
|
234 |
||
235 |
for (i = removedKeys.begin(); i != removedKeys.end(); ++i) |
|
236 |
result.remove(i.key()); |
|
237 |
for (i = addedKeys.begin(); i != addedKeys.end(); ++i) |
|
238 |
result.insert(i.key(), i.value()); |
|
239 |
return result; |
|
240 |
} |
|
241 |
||
242 |
bool QConfFile::isWritable() const |
|
243 |
{ |
|
244 |
QFileInfo fileInfo(name); |
|
245 |
||
246 |
#ifndef QT_NO_TEMPORARYFILE |
|
247 |
if (fileInfo.exists()) { |
|
248 |
#endif |
|
249 |
QFile file(name); |
|
250 |
return file.open(QFile::ReadWrite); |
|
251 |
#ifndef QT_NO_TEMPORARYFILE |
|
252 |
} else { |
|
253 |
// Create the directories to the file. |
|
254 |
QDir dir(fileInfo.absolutePath()); |
|
255 |
if (dir.exists() && dir.isReadable()) { |
|
256 |
return true; |
|
257 |
} else { |
|
258 |
if (!dir.mkpath(dir.absolutePath())) |
|
259 |
return false; |
|
260 |
} |
|
261 |
||
262 |
// we use a temporary file to avoid race conditions |
|
263 |
QTemporaryFile file(name); |
|
264 |
return file.open(); |
|
265 |
} |
|
266 |
#endif |
|
267 |
} |
|
268 |
||
269 |
QConfFile *QConfFile::fromName(const QString &fileName, bool _userPerms) |
|
270 |
{ |
|
271 |
QString absPath = QFileInfo(fileName).absoluteFilePath(); |
|
272 |
||
273 |
ConfFileHash *usedHash = usedHashFunc(); |
|
274 |
ConfFileCache *unusedCache = unusedCacheFunc(); |
|
275 |
||
276 |
QConfFile *confFile = 0; |
|
277 |
QMutexLocker locker(globalMutex()); |
|
278 |
||
279 |
if (!(confFile = usedHash->value(absPath))) { |
|
280 |
if ((confFile = unusedCache->take(absPath))) |
|
281 |
usedHash->insert(absPath, confFile); |
|
282 |
} |
|
283 |
if (confFile) { |
|
284 |
confFile->ref.ref(); |
|
285 |
return confFile; |
|
286 |
} |
|
287 |
return new QConfFile(absPath, _userPerms); |
|
288 |
} |
|
289 |
||
290 |
void QConfFile::clearCache() |
|
291 |
{ |
|
292 |
QMutexLocker locker(globalMutex()); |
|
293 |
unusedCacheFunc()->clear(); |
|
294 |
} |
|
295 |
||
296 |
// ************************************************************************ |
|
297 |
// QSettingsPrivate |
|
298 |
||
299 |
QSettingsPrivate::QSettingsPrivate(QSettings::Format format) |
|
300 |
: format(format), scope(QSettings::UserScope /* nothing better to put */), iniCodec(0), spec(0), fallbacks(true), |
|
301 |
pendingChanges(false), status(QSettings::NoError) |
|
302 |
{ |
|
303 |
} |
|
304 |
||
305 |
QSettingsPrivate::QSettingsPrivate(QSettings::Format format, QSettings::Scope scope, |
|
306 |
const QString &organization, const QString &application) |
|
307 |
: format(format), scope(scope), organizationName(organization), applicationName(application), |
|
308 |
iniCodec(0), spec(0), fallbacks(true), pendingChanges(false), status(QSettings::NoError) |
|
309 |
{ |
|
310 |
} |
|
311 |
||
312 |
QSettingsPrivate::~QSettingsPrivate() |
|
313 |
{ |
|
314 |
} |
|
315 |
||
316 |
QString QSettingsPrivate::actualKey(const QString &key) const |
|
317 |
{ |
|
318 |
QString n = normalizedKey(key); |
|
319 |
Q_ASSERT_X(!n.isEmpty(), "QSettings", "empty key"); |
|
320 |
n.prepend(groupPrefix); |
|
321 |
return n; |
|
322 |
} |
|
323 |
||
324 |
/* |
|
325 |
Returns a string that never starts nor ends with a slash (or an |
|
326 |
empty string). Examples: |
|
327 |
||
328 |
"foo" becomes "foo" |
|
329 |
"/foo//bar///" becomes "foo/bar" |
|
330 |
"///" becomes "" |
|
331 |
||
332 |
This function is optimized to avoid a QString deep copy in the |
|
333 |
common case where the key is already normalized. |
|
334 |
*/ |
|
335 |
QString QSettingsPrivate::normalizedKey(const QString &key) |
|
336 |
{ |
|
337 |
QString result = key; |
|
338 |
||
339 |
int i = 0; |
|
340 |
while (i < result.size()) { |
|
341 |
while (result.at(i) == QLatin1Char('/')) { |
|
342 |
result.remove(i, 1); |
|
343 |
if (i == result.size()) |
|
344 |
goto after_loop; |
|
345 |
} |
|
346 |
while (result.at(i) != QLatin1Char('/')) { |
|
347 |
++i; |
|
348 |
if (i == result.size()) |
|
349 |
return result; |
|
350 |
} |
|
351 |
++i; // leave the slash alone |
|
352 |
} |
|
353 |
||
354 |
after_loop: |
|
355 |
if (!result.isEmpty()) |
|
356 |
result.truncate(i - 1); // remove the trailing slash |
|
357 |
return result; |
|
358 |
} |
|
359 |
||
360 |
// see also qsettings_win.cpp and qsettings_mac.cpp |
|
361 |
||
362 |
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC) |
|
363 |
QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format, QSettings::Scope scope, |
|
364 |
const QString &organization, const QString &application) |
|
365 |
{ |
|
366 |
return new QConfFileSettingsPrivate(format, scope, organization, application); |
|
367 |
} |
|
368 |
#endif |
|
369 |
||
370 |
#if !defined(Q_OS_WIN) |
|
371 |
QSettingsPrivate *QSettingsPrivate::create(const QString &fileName, QSettings::Format format) |
|
372 |
{ |
|
373 |
return new QConfFileSettingsPrivate(fileName, format); |
|
374 |
} |
|
375 |
#endif |
|
376 |
||
377 |
void QSettingsPrivate::processChild(QString key, ChildSpec spec, QMap<QString, QString> &result) |
|
378 |
{ |
|
379 |
if (spec != AllKeys) { |
|
380 |
int slashPos = key.indexOf(QLatin1Char('/')); |
|
381 |
if (slashPos == -1) { |
|
382 |
if (spec != ChildKeys) |
|
383 |
return; |
|
384 |
} else { |
|
385 |
if (spec != ChildGroups) |
|
386 |
return; |
|
387 |
key.truncate(slashPos); |
|
388 |
} |
|
389 |
} |
|
390 |
result.insert(key, QString()); |
|
391 |
} |
|
392 |
||
393 |
void QSettingsPrivate::beginGroupOrArray(const QSettingsGroup &group) |
|
394 |
{ |
|
395 |
groupStack.push(group); |
|
396 |
if (!group.name().isEmpty()) { |
|
397 |
groupPrefix += group.name(); |
|
398 |
groupPrefix += QLatin1Char('/'); |
|
399 |
} |
|
400 |
} |
|
401 |
||
402 |
/* |
|
403 |
We only set an error if there isn't one set already. This way the user always gets the |
|
404 |
first error that occurred. We always allow clearing errors. |
|
405 |
*/ |
|
406 |
||
407 |
void QSettingsPrivate::setStatus(QSettings::Status status) const |
|
408 |
{ |
|
409 |
if (status == QSettings::NoError || this->status == QSettings::NoError) |
|
410 |
this->status = status; |
|
411 |
} |
|
412 |
||
413 |
void QSettingsPrivate::update() |
|
414 |
{ |
|
415 |
flush(); |
|
416 |
pendingChanges = false; |
|
417 |
} |
|
418 |
||
419 |
void QSettingsPrivate::requestUpdate() |
|
420 |
{ |
|
421 |
if (!pendingChanges) { |
|
422 |
pendingChanges = true; |
|
423 |
#ifndef QT_NO_QOBJECT |
|
424 |
Q_Q(QSettings); |
|
425 |
QCoreApplication::postEvent(q, new QEvent(QEvent::UpdateRequest)); |
|
426 |
#else |
|
427 |
update(); |
|
428 |
#endif |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
QStringList QSettingsPrivate::variantListToStringList(const QVariantList &l) |
|
433 |
{ |
|
434 |
QStringList result; |
|
435 |
QVariantList::const_iterator it = l.constBegin(); |
|
436 |
for (; it != l.constEnd(); ++it) |
|
437 |
result.append(variantToString(*it)); |
|
438 |
return result; |
|
439 |
} |
|
440 |
||
441 |
QVariant QSettingsPrivate::stringListToVariantList(const QStringList &l) |
|
442 |
{ |
|
443 |
QStringList outStringList = l; |
|
444 |
for (int i = 0; i < outStringList.count(); ++i) { |
|
445 |
const QString &str = outStringList.at(i); |
|
446 |
||
447 |
if (str.startsWith(QLatin1Char('@'))) { |
|
448 |
if (str.length() >= 2 && str.at(1) == QLatin1Char('@')) { |
|
449 |
outStringList[i].remove(0, 1); |
|
450 |
} else { |
|
451 |
QVariantList variantList; |
|
452 |
for (int j = 0; j < l.count(); ++j) |
|
453 |
variantList.append(stringToVariant(l.at(j))); |
|
454 |
return variantList; |
|
455 |
} |
|
456 |
} |
|
457 |
} |
|
458 |
return outStringList; |
|
459 |
} |
|
460 |
||
461 |
QString QSettingsPrivate::variantToString(const QVariant &v) |
|
462 |
{ |
|
463 |
QString result; |
|
464 |
||
465 |
switch (v.type()) { |
|
466 |
case QVariant::Invalid: |
|
467 |
result = QLatin1String("@Invalid()"); |
|
468 |
break; |
|
469 |
||
470 |
case QVariant::ByteArray: { |
|
471 |
QByteArray a = v.toByteArray(); |
|
472 |
result = QLatin1String("@ByteArray("); |
|
473 |
result += QString::fromLatin1(a.constData(), a.size()); |
|
474 |
result += QLatin1Char(')'); |
|
475 |
break; |
|
476 |
} |
|
477 |
||
478 |
case QVariant::String: |
|
479 |
case QVariant::LongLong: |
|
480 |
case QVariant::ULongLong: |
|
481 |
case QVariant::Int: |
|
482 |
case QVariant::UInt: |
|
483 |
case QVariant::Bool: |
|
484 |
case QVariant::Double: |
|
485 |
case QVariant::KeySequence: { |
|
486 |
result = v.toString(); |
|
487 |
if (result.startsWith(QLatin1Char('@'))) |
|
488 |
result.prepend(QLatin1Char('@')); |
|
489 |
break; |
|
490 |
} |
|
491 |
#ifndef QT_NO_GEOM_VARIANT |
|
492 |
case QVariant::Rect: { |
|
493 |
QRect r = qvariant_cast<QRect>(v); |
|
494 |
result += QLatin1String("@Rect("); |
|
495 |
result += QString::number(r.x()); |
|
496 |
result += QLatin1Char(' '); |
|
497 |
result += QString::number(r.y()); |
|
498 |
result += QLatin1Char(' '); |
|
499 |
result += QString::number(r.width()); |
|
500 |
result += QLatin1Char(' '); |
|
501 |
result += QString::number(r.height()); |
|
502 |
result += QLatin1Char(')'); |
|
503 |
break; |
|
504 |
} |
|
505 |
case QVariant::Size: { |
|
506 |
QSize s = qvariant_cast<QSize>(v); |
|
507 |
result += QLatin1String("@Size("); |
|
508 |
result += QString::number(s.width()); |
|
509 |
result += QLatin1Char(' '); |
|
510 |
result += QString::number(s.height()); |
|
511 |
result += QLatin1Char(')'); |
|
512 |
break; |
|
513 |
} |
|
514 |
case QVariant::Point: { |
|
515 |
QPoint p = qvariant_cast<QPoint>(v); |
|
516 |
result += QLatin1String("@Point("); |
|
517 |
result += QString::number(p.x()); |
|
518 |
result += QLatin1Char(' '); |
|
519 |
result += QString::number(p.y()); |
|
520 |
result += QLatin1Char(')'); |
|
521 |
break; |
|
522 |
} |
|
523 |
#endif // !QT_NO_GEOM_VARIANT |
|
524 |
||
525 |
default: { |
|
526 |
#ifndef QT_NO_DATASTREAM |
|
527 |
QByteArray a; |
|
528 |
{ |
|
529 |
QDataStream s(&a, QIODevice::WriteOnly); |
|
530 |
s.setVersion(QDataStream::Qt_4_0); |
|
531 |
s << v; |
|
532 |
} |
|
533 |
||
534 |
result = QLatin1String("@Variant("); |
|
535 |
result += QString::fromLatin1(a.constData(), a.size()); |
|
536 |
result += QLatin1Char(')'); |
|
537 |
#else |
|
538 |
Q_ASSERT(!"QSettings: Cannot save custom types without QDataStream support"); |
|
539 |
#endif |
|
540 |
break; |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
return result; |
|
545 |
} |
|
546 |
||
547 |
||
548 |
QVariant QSettingsPrivate::stringToVariant(const QString &s) |
|
549 |
{ |
|
550 |
if (s.startsWith(QLatin1Char('@'))) { |
|
551 |
if (s.endsWith(QLatin1Char(')'))) { |
|
552 |
if (s.startsWith(QLatin1String("@ByteArray("))) { |
|
553 |
return QVariant(s.toLatin1().mid(11, s.size() - 12)); |
|
554 |
} else if (s.startsWith(QLatin1String("@Variant("))) { |
|
555 |
#ifndef QT_NO_DATASTREAM |
|
556 |
QByteArray a(s.toLatin1().mid(9)); |
|
557 |
QDataStream stream(&a, QIODevice::ReadOnly); |
|
558 |
stream.setVersion(QDataStream::Qt_4_0); |
|
559 |
QVariant result; |
|
560 |
stream >> result; |
|
561 |
return result; |
|
562 |
#else |
|
563 |
Q_ASSERT(!"QSettings: Cannot load custom types without QDataStream support"); |
|
564 |
#endif |
|
565 |
#ifndef QT_NO_GEOM_VARIANT |
|
566 |
} else if (s.startsWith(QLatin1String("@Rect("))) { |
|
567 |
QStringList args = QSettingsPrivate::splitArgs(s, 5); |
|
568 |
if (args.size() == 4) |
|
569 |
return QVariant(QRect(args[0].toInt(), args[1].toInt(), args[2].toInt(), args[3].toInt())); |
|
570 |
} else if (s.startsWith(QLatin1String("@Size("))) { |
|
571 |
QStringList args = QSettingsPrivate::splitArgs(s, 5); |
|
572 |
if (args.size() == 2) |
|
573 |
return QVariant(QSize(args[0].toInt(), args[1].toInt())); |
|
574 |
} else if (s.startsWith(QLatin1String("@Point("))) { |
|
575 |
QStringList args = QSettingsPrivate::splitArgs(s, 6); |
|
576 |
if (args.size() == 2) |
|
577 |
return QVariant(QPoint(args[0].toInt(), args[1].toInt())); |
|
578 |
#endif |
|
579 |
} else if (s == QLatin1String("@Invalid()")) { |
|
580 |
return QVariant(); |
|
581 |
} |
|
582 |
||
583 |
} |
|
584 |
if (s.startsWith(QLatin1String("@@"))) |
|
585 |
return QVariant(s.mid(1)); |
|
586 |
} |
|
587 |
||
588 |
return QVariant(s); |
|
589 |
} |
|
590 |
||
591 |
static const char hexDigits[] = "0123456789ABCDEF"; |
|
592 |
||
593 |
void QSettingsPrivate::iniEscapedKey(const QString &key, QByteArray &result) |
|
594 |
{ |
|
595 |
result.reserve(result.length() + key.length() * 3 / 2); |
|
596 |
for (int i = 0; i < key.size(); ++i) { |
|
597 |
uint ch = key.at(i).unicode(); |
|
598 |
||
599 |
if (ch == '/') { |
|
600 |
result += '\\'; |
|
601 |
} else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') |
|
602 |
|| ch == '_' || ch == '-' || ch == '.') { |
|
603 |
result += (char)ch; |
|
604 |
} else if (ch <= 0xFF) { |
|
605 |
result += '%'; |
|
606 |
result += hexDigits[ch / 16]; |
|
607 |
result += hexDigits[ch % 16]; |
|
608 |
} else { |
|
609 |
result += "%U"; |
|
610 |
QByteArray hexCode; |
|
611 |
for (int i = 0; i < 4; ++i) { |
|
612 |
hexCode.prepend(hexDigits[ch % 16]); |
|
613 |
ch >>= 4; |
|
614 |
} |
|
615 |
result += hexCode; |
|
616 |
} |
|
617 |
} |
|
618 |
} |
|
619 |
||
620 |
bool QSettingsPrivate::iniUnescapedKey(const QByteArray &key, int from, int to, QString &result) |
|
621 |
{ |
|
622 |
bool lowercaseOnly = true; |
|
623 |
int i = from; |
|
624 |
result.reserve(result.length() + (to - from)); |
|
625 |
while (i < to) { |
|
626 |
int ch = (uchar)key.at(i); |
|
627 |
||
628 |
if (ch == '\\') { |
|
629 |
result += QLatin1Char('/'); |
|
630 |
++i; |
|
631 |
continue; |
|
632 |
} |
|
633 |
||
634 |
if (ch != '%' || i == to - 1) { |
|
635 |
if (uint(ch - 'A') <= 'Z' - 'A') // only for ASCII |
|
636 |
lowercaseOnly = false; |
|
637 |
result += QLatin1Char(ch); |
|
638 |
++i; |
|
639 |
continue; |
|
640 |
} |
|
641 |
||
642 |
int numDigits = 2; |
|
643 |
int firstDigitPos = i + 1; |
|
644 |
||
645 |
ch = key.at(i + 1); |
|
646 |
if (ch == 'U') { |
|
647 |
++firstDigitPos; |
|
648 |
numDigits = 4; |
|
649 |
} |
|
650 |
||
651 |
if (firstDigitPos + numDigits > to) { |
|
652 |
result += QLatin1Char('%'); |
|
653 |
// ### missing U |
|
654 |
++i; |
|
655 |
continue; |
|
656 |
} |
|
657 |
||
658 |
bool ok; |
|
659 |
ch = key.mid(firstDigitPos, numDigits).toInt(&ok, 16); |
|
660 |
if (!ok) { |
|
661 |
result += QLatin1Char('%'); |
|
662 |
// ### missing U |
|
663 |
++i; |
|
664 |
continue; |
|
665 |
} |
|
666 |
||
667 |
QChar qch(ch); |
|
668 |
if (qch.isUpper()) |
|
669 |
lowercaseOnly = false; |
|
670 |
result += qch; |
|
671 |
i = firstDigitPos + numDigits; |
|
672 |
} |
|
673 |
return lowercaseOnly; |
|
674 |
} |
|
675 |
||
676 |
void QSettingsPrivate::iniEscapedString(const QString &str, QByteArray &result, QTextCodec *codec) |
|
677 |
{ |
|
678 |
bool needsQuotes = false; |
|
679 |
bool escapeNextIfDigit = false; |
|
680 |
int i; |
|
681 |
int startPos = result.size(); |
|
682 |
||
683 |
result.reserve(startPos + str.size() * 3 / 2); |
|
684 |
for (i = 0; i < str.size(); ++i) { |
|
685 |
uint ch = str.at(i).unicode(); |
|
686 |
if (ch == ';' || ch == ',' || ch == '=') |
|
687 |
needsQuotes = true; |
|
688 |
||
689 |
if (escapeNextIfDigit |
|
690 |
&& ((ch >= '0' && ch <= '9') |
|
691 |
|| (ch >= 'a' && ch <= 'f') |
|
692 |
|| (ch >= 'A' && ch <= 'F'))) { |
|
693 |
result += "\\x"; |
|
694 |
result += QByteArray::number(ch, 16); |
|
695 |
continue; |
|
696 |
} |
|
697 |
||
698 |
escapeNextIfDigit = false; |
|
699 |
||
700 |
switch (ch) { |
|
701 |
case '\0': |
|
702 |
result += "\\0"; |
|
703 |
escapeNextIfDigit = true; |
|
704 |
break; |
|
705 |
case '\a': |
|
706 |
result += "\\a"; |
|
707 |
break; |
|
708 |
case '\b': |
|
709 |
result += "\\b"; |
|
710 |
break; |
|
711 |
case '\f': |
|
712 |
result += "\\f"; |
|
713 |
break; |
|
714 |
case '\n': |
|
715 |
result += "\\n"; |
|
716 |
break; |
|
717 |
case '\r': |
|
718 |
result += "\\r"; |
|
719 |
break; |
|
720 |
case '\t': |
|
721 |
result += "\\t"; |
|
722 |
break; |
|
723 |
case '\v': |
|
724 |
result += "\\v"; |
|
725 |
break; |
|
726 |
case '"': |
|
727 |
case '\\': |
|
728 |
result += '\\'; |
|
729 |
result += (char)ch; |
|
730 |
break; |
|
731 |
default: |
|
732 |
if (ch <= 0x1F || (ch >= 0x7F && !codec)) { |
|
733 |
result += "\\x"; |
|
734 |
result += QByteArray::number(ch, 16); |
|
735 |
escapeNextIfDigit = true; |
|
736 |
#ifndef QT_NO_TEXTCODEC |
|
737 |
} else if (codec) { |
|
738 |
// slow |
|
739 |
result += codec->fromUnicode(str.at(i)); |
|
740 |
#endif |
|
741 |
} else { |
|
742 |
result += (char)ch; |
|
743 |
} |
|
744 |
} |
|
745 |
} |
|
746 |
||
747 |
if (needsQuotes |
|
748 |
|| (startPos < result.size() && (result.at(startPos) == ' ' |
|
749 |
|| result.at(result.size() - 1) == ' '))) { |
|
750 |
result.insert(startPos, '"'); |
|
751 |
result += '"'; |
|
752 |
} |
|
753 |
} |
|
754 |
||
755 |
inline static void iniChopTrailingSpaces(QString &str) |
|
756 |
{ |
|
757 |
int n = str.size() - 1; |
|
758 |
QChar ch; |
|
759 |
while (n >= 0 && ((ch = str.at(n)) == QLatin1Char(' ') || ch == QLatin1Char('\t'))) |
|
760 |
str.truncate(n--); |
|
761 |
} |
|
762 |
||
763 |
void QSettingsPrivate::iniEscapedStringList(const QStringList &strs, QByteArray &result, QTextCodec *codec) |
|
764 |
{ |
|
765 |
if (strs.isEmpty()) { |
|
766 |
/* |
|
767 |
We need to distinguish between empty lists and one-item |
|
768 |
lists that contain an empty string. Ideally, we'd have a |
|
769 |
@EmptyList() symbol but that would break compatibility |
|
770 |
with Qt 4.0. @Invalid() stands for QVariant(), and |
|
771 |
QVariant().toStringList() returns an empty QStringList, |
|
772 |
so we're in good shape. |
|
773 |
||
774 |
### Qt 5: Use a nicer syntax, e.g. @List, for variant lists |
|
775 |
*/ |
|
776 |
result += "@Invalid()"; |
|
777 |
} else { |
|
778 |
for (int i = 0; i < strs.size(); ++i) { |
|
779 |
if (i != 0) |
|
780 |
result += ", "; |
|
781 |
iniEscapedString(strs.at(i), result, codec); |
|
782 |
} |
|
783 |
} |
|
784 |
} |
|
785 |
||
786 |
bool QSettingsPrivate::iniUnescapedStringList(const QByteArray &str, int from, int to, |
|
787 |
QString &stringResult, QStringList &stringListResult, |
|
788 |
QTextCodec *codec) |
|
789 |
{ |
|
790 |
static const char escapeCodes[][2] = |
|
791 |
{ |
|
792 |
{ 'a', '\a' }, |
|
793 |
{ 'b', '\b' }, |
|
794 |
{ 'f', '\f' }, |
|
795 |
{ 'n', '\n' }, |
|
796 |
{ 'r', '\r' }, |
|
797 |
{ 't', '\t' }, |
|
798 |
{ 'v', '\v' }, |
|
799 |
{ '"', '"' }, |
|
800 |
{ '?', '?' }, |
|
801 |
{ '\'', '\'' }, |
|
802 |
{ '\\', '\\' } |
|
803 |
}; |
|
804 |
static const int numEscapeCodes = sizeof(escapeCodes) / sizeof(escapeCodes[0]); |
|
805 |
||
806 |
bool isStringList = false; |
|
807 |
bool inQuotedString = false; |
|
808 |
bool currentValueIsQuoted = false; |
|
809 |
int escapeVal = 0; |
|
810 |
int i = from; |
|
811 |
char ch; |
|
812 |
||
813 |
StSkipSpaces: |
|
814 |
while (i < to && ((ch = str.at(i)) == ' ' || ch == '\t')) |
|
815 |
++i; |
|
816 |
// fallthrough |
|
817 |
||
818 |
StNormal: |
|
819 |
while (i < to) { |
|
820 |
switch (str.at(i)) { |
|
821 |
case '\\': |
|
822 |
++i; |
|
823 |
if (i >= to) |
|
824 |
goto end; |
|
825 |
||
826 |
ch = str.at(i++); |
|
827 |
for (int j = 0; j < numEscapeCodes; ++j) { |
|
828 |
if (ch == escapeCodes[j][0]) { |
|
829 |
stringResult += QLatin1Char(escapeCodes[j][1]); |
|
830 |
goto StNormal; |
|
831 |
} |
|
832 |
} |
|
833 |
||
834 |
if (ch == 'x') { |
|
835 |
escapeVal = 0; |
|
836 |
||
837 |
if (i >= to) |
|
838 |
goto end; |
|
839 |
||
840 |
ch = str.at(i); |
|
841 |
if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) |
|
842 |
goto StHexEscape; |
|
843 |
} else if (ch >= '0' && ch <= '7') { |
|
844 |
escapeVal = ch - '0'; |
|
845 |
goto StOctEscape; |
|
846 |
} else if (ch == '\n' || ch == '\r') { |
|
847 |
if (i < to) { |
|
848 |
char ch2 = str.at(i); |
|
849 |
// \n, \r, \r\n, and \n\r are legitimate line terminators in INI files |
|
850 |
if ((ch2 == '\n' || ch2 == '\r') && ch2 != ch) |
|
851 |
++i; |
|
852 |
} |
|
853 |
} else { |
|
854 |
// the character is skipped |
|
855 |
} |
|
856 |
break; |
|
857 |
case '"': |
|
858 |
++i; |
|
859 |
currentValueIsQuoted = true; |
|
860 |
inQuotedString = !inQuotedString; |
|
861 |
if (!inQuotedString) |
|
862 |
goto StSkipSpaces; |
|
863 |
break; |
|
864 |
case ',': |
|
865 |
if (!inQuotedString) { |
|
866 |
if (!currentValueIsQuoted) |
|
867 |
iniChopTrailingSpaces(stringResult); |
|
868 |
if (!isStringList) { |
|
869 |
isStringList = true; |
|
870 |
stringListResult.clear(); |
|
871 |
stringResult.squeeze(); |
|
872 |
} |
|
873 |
stringListResult.append(stringResult); |
|
874 |
stringResult.clear(); |
|
875 |
currentValueIsQuoted = false; |
|
876 |
++i; |
|
877 |
goto StSkipSpaces; |
|
878 |
} |
|
879 |
// fallthrough |
|
880 |
default: { |
|
881 |
int j = i + 1; |
|
882 |
while (j < to) { |
|
883 |
ch = str.at(j); |
|
884 |
if (ch == '\\' || ch == '"' || ch == ',') |
|
885 |
break; |
|
886 |
++j; |
|
887 |
} |
|
888 |
||
889 |
#ifndef QT_NO_TEXTCODEC |
|
890 |
if (codec) { |
|
891 |
stringResult += codec->toUnicode(str.constData() + i, j - i); |
|
892 |
} else |
|
893 |
#endif |
|
894 |
{ |
|
895 |
int n = stringResult.size(); |
|
896 |
stringResult.resize(n + (j - i)); |
|
897 |
QChar *resultData = stringResult.data() + n; |
|
898 |
for (int k = i; k < j; ++k) |
|
899 |
*resultData++ = QLatin1Char(str.at(k)); |
|
900 |
} |
|
901 |
i = j; |
|
902 |
} |
|
903 |
} |
|
904 |
} |
|
905 |
goto end; |
|
906 |
||
907 |
StHexEscape: |
|
908 |
if (i >= to) { |
|
909 |
stringResult += QChar(escapeVal); |
|
910 |
goto end; |
|
911 |
} |
|
912 |
||
913 |
ch = str.at(i); |
|
914 |
if (ch >= 'a') |
|
915 |
ch -= 'a' - 'A'; |
|
916 |
if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) { |
|
917 |
escapeVal <<= 4; |
|
918 |
escapeVal += strchr(hexDigits, ch) - hexDigits; |
|
919 |
++i; |
|
920 |
goto StHexEscape; |
|
921 |
} else { |
|
922 |
stringResult += QChar(escapeVal); |
|
923 |
goto StNormal; |
|
924 |
} |
|
925 |
||
926 |
StOctEscape: |
|
927 |
if (i >= to) { |
|
928 |
stringResult += QChar(escapeVal); |
|
929 |
goto end; |
|
930 |
} |
|
931 |
||
932 |
ch = str.at(i); |
|
933 |
if (ch >= '0' && ch <= '7') { |
|
934 |
escapeVal <<= 3; |
|
935 |
escapeVal += ch - '0'; |
|
936 |
++i; |
|
937 |
goto StOctEscape; |
|
938 |
} else { |
|
939 |
stringResult += QChar(escapeVal); |
|
940 |
goto StNormal; |
|
941 |
} |
|
942 |
||
943 |
end: |
|
944 |
if (!currentValueIsQuoted) |
|
945 |
iniChopTrailingSpaces(stringResult); |
|
946 |
if (isStringList) |
|
947 |
stringListResult.append(stringResult); |
|
948 |
return isStringList; |
|
949 |
} |
|
950 |
||
951 |
QStringList QSettingsPrivate::splitArgs(const QString &s, int idx) |
|
952 |
{ |
|
953 |
int l = s.length(); |
|
954 |
Q_ASSERT(l > 0); |
|
955 |
Q_ASSERT(s.at(idx) == QLatin1Char('(')); |
|
956 |
Q_ASSERT(s.at(l - 1) == QLatin1Char(')')); |
|
957 |
||
958 |
QStringList result; |
|
959 |
QString item; |
|
960 |
||
961 |
for (++idx; idx < l; ++idx) { |
|
962 |
QChar c = s.at(idx); |
|
963 |
if (c == QLatin1Char(')')) { |
|
964 |
Q_ASSERT(idx == l - 1); |
|
965 |
result.append(item); |
|
966 |
} else if (c == QLatin1Char(' ')) { |
|
967 |
result.append(item); |
|
968 |
item.clear(); |
|
969 |
} else { |
|
970 |
item.append(c); |
|
971 |
} |
|
972 |
} |
|
973 |
||
974 |
return result; |
|
975 |
} |
|
976 |
||
977 |
// ************************************************************************ |
|
978 |
// QConfFileSettingsPrivate |
|
979 |
||
980 |
/* |
|
981 |
If we don't have the permission to read the file, returns false. |
|
982 |
If the file doesn't exist, returns true. |
|
983 |
*/ |
|
984 |
static bool checkAccess(const QString &name) |
|
985 |
{ |
|
986 |
QFileInfo fileInfo(name); |
|
987 |
||
988 |
if (fileInfo.exists()) { |
|
989 |
QFile file(name); |
|
990 |
// if the file exists but we can't open it, report an error |
|
991 |
return file.open(QFile::ReadOnly); |
|
992 |
} else { |
|
993 |
return true; |
|
994 |
} |
|
995 |
} |
|
996 |
||
997 |
void QConfFileSettingsPrivate::initFormat() |
|
998 |
{ |
|
999 |
extension = (format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"); |
|
1000 |
readFunc = 0; |
|
1001 |
writeFunc = 0; |
|
1002 |
#if defined(Q_OS_MAC) |
|
1003 |
caseSensitivity = (format == QSettings::NativeFormat) ? Qt::CaseSensitive : Qt::CaseInsensitive; |
|
1004 |
#else |
|
1005 |
caseSensitivity = IniCaseSensitivity; |
|
1006 |
#endif |
|
1007 |
||
1008 |
if (format > QSettings::IniFormat) { |
|
1009 |
QMutexLocker locker(globalMutex()); |
|
1010 |
const CustomFormatVector *customFormatVector = customFormatVectorFunc(); |
|
1011 |
||
1012 |
int i = (int)format - (int)QSettings::CustomFormat1; |
|
1013 |
if (i >= 0 && i < customFormatVector->size()) { |
|
1014 |
QConfFileCustomFormat info = customFormatVector->at(i); |
|
1015 |
extension = info.extension; |
|
1016 |
readFunc = info.readFunc; |
|
1017 |
writeFunc = info.writeFunc; |
|
1018 |
caseSensitivity = info.caseSensitivity; |
|
1019 |
} |
|
1020 |
} |
|
1021 |
} |
|
1022 |
||
1023 |
void QConfFileSettingsPrivate::initAccess() |
|
1024 |
{ |
|
1025 |
bool readAccess = false; |
|
1026 |
if (confFiles[spec]) { |
|
1027 |
readAccess = checkAccess(confFiles[spec]->name); |
|
1028 |
if (format > QSettings::IniFormat) { |
|
1029 |
if (!readFunc) |
|
1030 |
readAccess = false; |
|
1031 |
} |
|
1032 |
} |
|
1033 |
||
1034 |
if (!readAccess) |
|
1035 |
setStatus(QSettings::AccessError); |
|
1036 |
||
1037 |
sync(); // loads the files the first time |
|
1038 |
} |
|
1039 |
||
1040 |
#ifdef Q_OS_WIN |
|
1041 |
static QString windowsConfigPath(int type) |
|
1042 |
{ |
|
1043 |
QString result; |
|
1044 |
||
1045 |
#ifndef QT_NO_QOBJECT |
|
1046 |
// We can't use QLibrary if there is QT_NO_QOBJECT is defined |
|
1047 |
// This only happens when bootstrapping qmake. |
|
1048 |
#ifndef Q_OS_WINCE |
|
1049 |
QLibrary library(QLatin1String("shell32")); |
|
1050 |
#else |
|
1051 |
QLibrary library(QLatin1String("coredll")); |
|
1052 |
#endif // Q_OS_WINCE |
|
1053 |
typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPWSTR, int, BOOL); |
|
1054 |
GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathW"); |
|
1055 |
if (SHGetSpecialFolderPath) { |
|
1056 |
wchar_t path[MAX_PATH]; |
|
1057 |
SHGetSpecialFolderPath(0, path, type, FALSE); |
|
1058 |
result = QString::fromWCharArray(path); |
|
1059 |
} |
|
1060 |
||
1061 |
#endif // QT_NO_QOBJECT |
|
1062 |
||
1063 |
if (result.isEmpty()) { |
|
1064 |
switch (type) { |
|
1065 |
#ifndef Q_OS_WINCE |
|
1066 |
case CSIDL_COMMON_APPDATA: |
|
1067 |
result = QLatin1String("C:\\temp\\qt-common"); |
|
1068 |
break; |
|
1069 |
case CSIDL_APPDATA: |
|
1070 |
result = QLatin1String("C:\\temp\\qt-user"); |
|
1071 |
break; |
|
1072 |
#else |
|
1073 |
case CSIDL_COMMON_APPDATA: |
|
1074 |
result = QLatin1String("\\Temp\\qt-common"); |
|
1075 |
break; |
|
1076 |
case CSIDL_APPDATA: |
|
1077 |
result = QLatin1String("\\Temp\\qt-user"); |
|
1078 |
break; |
|
1079 |
#endif |
|
1080 |
default: |
|
1081 |
; |
|
1082 |
} |
|
1083 |
} |
|
1084 |
||
1085 |
return result; |
|
1086 |
} |
|
1087 |
#endif // Q_OS_WIN |
|
1088 |
||
1089 |
static inline int pathHashKey(QSettings::Format format, QSettings::Scope scope) |
|
1090 |
{ |
|
1091 |
return int((uint(format) << 1) | uint(scope == QSettings::SystemScope)); |
|
1092 |
} |
|
1093 |
||
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1094 |
static void initDefaultPaths(QMutexLocker *locker) |
0 | 1095 |
{ |
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1096 |
PathHash *pathHash = pathHashFunc(); |
0 | 1097 |
QString homePath = QDir::homePath(); |
1098 |
QString systemPath; |
|
1099 |
||
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1100 |
locker->unlock(); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1101 |
|
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1102 |
/* |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1103 |
QLibraryInfo::location() uses QSettings, so in order to |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1104 |
avoid a dead-lock, we can't hold the global mutex while |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1105 |
calling it. |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1106 |
*/ |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1107 |
systemPath = QLibraryInfo::location(QLibraryInfo::SettingsPath); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1108 |
systemPath += QLatin1Char('/'); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1109 |
|
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1110 |
locker->relock(); |
0 | 1111 |
if (pathHash->isEmpty()) { |
1112 |
/* |
|
1113 |
Lazy initialization of pathHash. We initialize the |
|
1114 |
IniFormat paths and (on Unix) the NativeFormat paths. |
|
1115 |
(The NativeFormat paths are not configurable for the |
|
1116 |
Windows registry and the Mac CFPreferences.) |
|
1117 |
*/ |
|
1118 |
#ifdef Q_OS_WIN |
|
1119 |
pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope), |
|
1120 |
windowsConfigPath(CSIDL_APPDATA) + QDir::separator()); |
|
1121 |
pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), |
|
1122 |
windowsConfigPath(CSIDL_COMMON_APPDATA) + QDir::separator()); |
|
1123 |
#else |
|
1124 |
QString userPath; |
|
1125 |
char *env = getenv("XDG_CONFIG_HOME"); |
|
1126 |
if (env == 0) { |
|
1127 |
userPath = homePath; |
|
1128 |
userPath += QLatin1Char('/'); |
|
1129 |
#ifdef Q_WS_QWS |
|
1130 |
userPath += QLatin1String("Settings"); |
|
1131 |
#else |
|
1132 |
userPath += QLatin1String(".config"); |
|
1133 |
#endif |
|
1134 |
} else if (*env == '/') { |
|
1135 |
userPath = QLatin1String(env); |
|
1136 |
} else { |
|
1137 |
userPath = homePath; |
|
1138 |
userPath += QLatin1Char('/'); |
|
1139 |
userPath += QLatin1String(env); |
|
1140 |
} |
|
1141 |
userPath += QLatin1Char('/'); |
|
1142 |
||
1143 |
pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope), userPath); |
|
1144 |
pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), systemPath); |
|
1145 |
#ifndef Q_OS_MAC |
|
1146 |
pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::UserScope), userPath); |
|
1147 |
pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::SystemScope), systemPath); |
|
1148 |
#endif |
|
1149 |
#endif |
|
1150 |
} |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1151 |
} |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1152 |
|
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1153 |
static QString getPath(QSettings::Format format, QSettings::Scope scope) |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1154 |
{ |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1155 |
Q_ASSERT((int)QSettings::NativeFormat == 0); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1156 |
Q_ASSERT((int)QSettings::IniFormat == 1); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1157 |
|
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1158 |
QMutexLocker locker(globalMutex()); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1159 |
PathHash *pathHash = pathHashFunc(); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1160 |
if (pathHash->isEmpty()) |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
1161 |
initDefaultPaths(&locker); |
0 | 1162 |
|
1163 |
QString result = pathHash->value(pathHashKey(format, scope)); |
|
1164 |
if (!result.isEmpty()) |
|
1165 |
return result; |
|
1166 |
||
1167 |
// fall back on INI path |
|
1168 |
return pathHash->value(pathHashKey(QSettings::IniFormat, scope)); |
|
1169 |
} |
|
1170 |
||
1171 |
QConfFileSettingsPrivate::QConfFileSettingsPrivate(QSettings::Format format, |
|
1172 |
QSettings::Scope scope, |
|
1173 |
const QString &organization, |
|
1174 |
const QString &application) |
|
1175 |
: QSettingsPrivate(format, scope, organization, application), |
|
1176 |
nextPosition(0x40000000) // big positive number |
|
1177 |
{ |
|
1178 |
int i; |
|
1179 |
initFormat(); |
|
1180 |
||
1181 |
QString org = organization; |
|
1182 |
if (org.isEmpty()) { |
|
1183 |
setStatus(QSettings::AccessError); |
|
1184 |
org = QLatin1String("Unknown Organization"); |
|
1185 |
} |
|
1186 |
||
1187 |
QString appFile = org + QDir::separator() + application + extension; |
|
1188 |
QString orgFile = org + extension; |
|
1189 |
||
1190 |
if (scope == QSettings::UserScope) { |
|
1191 |
QString userPath = getPath(format, QSettings::UserScope); |
|
1192 |
if (!application.isEmpty()) |
|
1193 |
confFiles[F_User | F_Application].reset(QConfFile::fromName(userPath + appFile, true)); |
|
1194 |
confFiles[F_User | F_Organization].reset(QConfFile::fromName(userPath + orgFile, true)); |
|
1195 |
} |
|
1196 |
||
1197 |
QString systemPath = getPath(format, QSettings::SystemScope); |
|
1198 |
if (!application.isEmpty()) |
|
1199 |
confFiles[F_System | F_Application].reset(QConfFile::fromName(systemPath + appFile, false)); |
|
1200 |
confFiles[F_System | F_Organization].reset(QConfFile::fromName(systemPath + orgFile, false)); |
|
1201 |
||
1202 |
for (i = 0; i < NumConfFiles; ++i) { |
|
1203 |
if (confFiles[i]) { |
|
1204 |
spec = i; |
|
1205 |
break; |
|
1206 |
} |
|
1207 |
} |
|
1208 |
||
1209 |
initAccess(); |
|
1210 |
} |
|
1211 |
||
1212 |
QConfFileSettingsPrivate::QConfFileSettingsPrivate(const QString &fileName, |
|
1213 |
QSettings::Format format) |
|
1214 |
: QSettingsPrivate(format), |
|
1215 |
nextPosition(0x40000000) // big positive number |
|
1216 |
{ |
|
1217 |
initFormat(); |
|
1218 |
||
1219 |
confFiles[0].reset(QConfFile::fromName(fileName, true)); |
|
1220 |
||
1221 |
initAccess(); |
|
1222 |
} |
|
1223 |
||
1224 |
QConfFileSettingsPrivate::~QConfFileSettingsPrivate() |
|
1225 |
{ |
|
1226 |
QMutexLocker locker(globalMutex()); |
|
1227 |
ConfFileHash *usedHash = usedHashFunc(); |
|
1228 |
ConfFileCache *unusedCache = unusedCacheFunc(); |
|
1229 |
||
1230 |
for (int i = 0; i < NumConfFiles; ++i) { |
|
1231 |
if (confFiles[i] && !confFiles[i]->ref.deref()) { |
|
1232 |
if (confFiles[i]->size == 0) { |
|
1233 |
delete confFiles[i].take(); |
|
1234 |
} else if (unusedCache) { |
|
1235 |
if (usedHash) |
|
1236 |
usedHash->remove(confFiles[i]->name); |
|
1237 |
QT_TRY { |
|
1238 |
// compute a better size? |
|
1239 |
unusedCache->insert(confFiles[i]->name, confFiles[i].data(), |
|
1240 |
10 + (confFiles[i]->originalKeys.size() / 4)); |
|
1241 |
confFiles[i].take(); |
|
1242 |
} QT_CATCH(...) { |
|
1243 |
// out of memory. Do not cache the file. |
|
1244 |
delete confFiles[i].take(); |
|
1245 |
} |
|
1246 |
} |
|
1247 |
} |
|
1248 |
// prevent the ScopedPointer to deref it again. |
|
1249 |
confFiles[i].take(); |
|
1250 |
} |
|
1251 |
} |
|
1252 |
||
1253 |
void QConfFileSettingsPrivate::remove(const QString &key) |
|
1254 |
{ |
|
1255 |
QConfFile *confFile = confFiles[spec].data(); |
|
1256 |
if (!confFile) |
|
1257 |
return; |
|
1258 |
||
1259 |
QSettingsKey theKey(key, caseSensitivity); |
|
1260 |
QSettingsKey prefix(key + QLatin1Char('/'), caseSensitivity); |
|
1261 |
QMutexLocker locker(&confFile->mutex); |
|
1262 |
||
1263 |
ensureSectionParsed(confFile, theKey); |
|
1264 |
ensureSectionParsed(confFile, prefix); |
|
1265 |
||
1266 |
ParsedSettingsMap::iterator i = confFile->addedKeys.lowerBound(prefix); |
|
1267 |
while (i != confFile->addedKeys.end() && i.key().startsWith(prefix)) |
|
1268 |
i = confFile->addedKeys.erase(i); |
|
1269 |
confFile->addedKeys.remove(theKey); |
|
1270 |
||
1271 |
ParsedSettingsMap::const_iterator j = const_cast<const ParsedSettingsMap *>(&confFile->originalKeys)->lowerBound(prefix); |
|
1272 |
while (j != confFile->originalKeys.constEnd() && j.key().startsWith(prefix)) { |
|
1273 |
confFile->removedKeys.insert(j.key(), QVariant()); |
|
1274 |
++j; |
|
1275 |
} |
|
1276 |
if (confFile->originalKeys.contains(theKey)) |
|
1277 |
confFile->removedKeys.insert(theKey, QVariant()); |
|
1278 |
} |
|
1279 |
||
1280 |
void QConfFileSettingsPrivate::set(const QString &key, const QVariant &value) |
|
1281 |
{ |
|
1282 |
QConfFile *confFile = confFiles[spec].data(); |
|
1283 |
if (!confFile) |
|
1284 |
return; |
|
1285 |
||
1286 |
QSettingsKey theKey(key, caseSensitivity, nextPosition++); |
|
1287 |
QMutexLocker locker(&confFile->mutex); |
|
1288 |
confFile->removedKeys.remove(theKey); |
|
1289 |
confFile->addedKeys.insert(theKey, value); |
|
1290 |
} |
|
1291 |
||
1292 |
bool QConfFileSettingsPrivate::get(const QString &key, QVariant *value) const |
|
1293 |
{ |
|
1294 |
QSettingsKey theKey(key, caseSensitivity); |
|
1295 |
ParsedSettingsMap::const_iterator j; |
|
1296 |
bool found = false; |
|
1297 |
||
1298 |
for (int i = 0; i < NumConfFiles; ++i) { |
|
1299 |
if (QConfFile *confFile = confFiles[i].data()) { |
|
1300 |
QMutexLocker locker(&confFile->mutex); |
|
1301 |
||
1302 |
if (!confFile->addedKeys.isEmpty()) { |
|
1303 |
j = confFile->addedKeys.constFind(theKey); |
|
1304 |
found = (j != confFile->addedKeys.constEnd()); |
|
1305 |
} |
|
1306 |
if (!found) { |
|
1307 |
ensureSectionParsed(confFile, theKey); |
|
1308 |
j = confFile->originalKeys.constFind(theKey); |
|
1309 |
found = (j != confFile->originalKeys.constEnd() |
|
1310 |
&& !confFile->removedKeys.contains(theKey)); |
|
1311 |
} |
|
1312 |
||
1313 |
if (found && value) |
|
1314 |
*value = *j; |
|
1315 |
||
1316 |
if (found) |
|
1317 |
return true; |
|
1318 |
if (!fallbacks) |
|
1319 |
break; |
|
1320 |
} |
|
1321 |
} |
|
1322 |
return false; |
|
1323 |
} |
|
1324 |
||
1325 |
QStringList QConfFileSettingsPrivate::children(const QString &prefix, ChildSpec spec) const |
|
1326 |
{ |
|
1327 |
QMap<QString, QString> result; |
|
1328 |
ParsedSettingsMap::const_iterator j; |
|
1329 |
||
1330 |
QSettingsKey thePrefix(prefix, caseSensitivity); |
|
1331 |
int startPos = prefix.size(); |
|
1332 |
||
1333 |
for (int i = 0; i < NumConfFiles; ++i) { |
|
1334 |
if (QConfFile *confFile = confFiles[i].data()) { |
|
1335 |
QMutexLocker locker(&confFile->mutex); |
|
1336 |
||
1337 |
if (thePrefix.isEmpty()) { |
|
1338 |
ensureAllSectionsParsed(confFile); |
|
1339 |
} else { |
|
1340 |
ensureSectionParsed(confFile, thePrefix); |
|
1341 |
} |
|
1342 |
||
1343 |
j = const_cast<const ParsedSettingsMap *>( |
|
1344 |
&confFile->originalKeys)->lowerBound( thePrefix); |
|
1345 |
while (j != confFile->originalKeys.constEnd() && j.key().startsWith(thePrefix)) { |
|
1346 |
if (!confFile->removedKeys.contains(j.key())) |
|
1347 |
processChild(j.key().originalCaseKey().mid(startPos), spec, result); |
|
1348 |
++j; |
|
1349 |
} |
|
1350 |
||
1351 |
j = const_cast<const ParsedSettingsMap *>( |
|
1352 |
&confFile->addedKeys)->lowerBound(thePrefix); |
|
1353 |
while (j != confFile->addedKeys.constEnd() && j.key().startsWith(thePrefix)) { |
|
1354 |
processChild(j.key().originalCaseKey().mid(startPos), spec, result); |
|
1355 |
++j; |
|
1356 |
} |
|
1357 |
||
1358 |
if (!fallbacks) |
|
1359 |
break; |
|
1360 |
} |
|
1361 |
} |
|
1362 |
return result.keys(); |
|
1363 |
} |
|
1364 |
||
1365 |
void QConfFileSettingsPrivate::clear() |
|
1366 |
{ |
|
1367 |
QConfFile *confFile = confFiles[spec].data(); |
|
1368 |
if (!confFile) |
|
1369 |
return; |
|
1370 |
||
1371 |
QMutexLocker locker(&confFile->mutex); |
|
1372 |
ensureAllSectionsParsed(confFile); |
|
1373 |
confFile->addedKeys.clear(); |
|
1374 |
confFile->removedKeys = confFile->originalKeys; |
|
1375 |
} |
|
1376 |
||
1377 |
void QConfFileSettingsPrivate::sync() |
|
1378 |
{ |
|
1379 |
// people probably won't be checking the status a whole lot, so in case of |
|
1380 |
// error we just try to go on and make the best of it |
|
1381 |
||
1382 |
for (int i = 0; i < NumConfFiles; ++i) { |
|
1383 |
QConfFile *confFile = confFiles[i].data(); |
|
1384 |
if (confFile) { |
|
1385 |
QMutexLocker locker(&confFile->mutex); |
|
1386 |
syncConfFile(i); |
|
1387 |
} |
|
1388 |
} |
|
1389 |
} |
|
1390 |
||
1391 |
void QConfFileSettingsPrivate::flush() |
|
1392 |
{ |
|
1393 |
sync(); |
|
1394 |
} |
|
1395 |
||
1396 |
QString QConfFileSettingsPrivate::fileName() const |
|
1397 |
{ |
|
1398 |
QConfFile *confFile = confFiles[spec].data(); |
|
1399 |
if (!confFile) |
|
1400 |
return QString(); |
|
1401 |
return confFile->name; |
|
1402 |
} |
|
1403 |
||
1404 |
bool QConfFileSettingsPrivate::isWritable() const |
|
1405 |
{ |
|
1406 |
if (format > QSettings::IniFormat && !writeFunc) |
|
1407 |
return false; |
|
1408 |
||
1409 |
QConfFile *confFile = confFiles[spec].data(); |
|
1410 |
if (!confFile) |
|
1411 |
return false; |
|
1412 |
||
1413 |
return confFile->isWritable(); |
|
1414 |
} |
|
1415 |
||
1416 |
void QConfFileSettingsPrivate::syncConfFile(int confFileNo) |
|
1417 |
{ |
|
1418 |
QConfFile *confFile = confFiles[confFileNo].data(); |
|
1419 |
bool readOnly = confFile->addedKeys.isEmpty() && confFile->removedKeys.isEmpty(); |
|
1420 |
bool ok; |
|
1421 |
||
1422 |
/* |
|
1423 |
We can often optimize the read-only case, if the file on disk |
|
1424 |
hasn't changed. |
|
1425 |
*/ |
|
1426 |
if (readOnly) { |
|
1427 |
QFileInfo fileInfo(confFile->name); |
|
1428 |
if (confFile->size == fileInfo.size() && confFile->timeStamp == fileInfo.lastModified()) |
|
1429 |
return; |
|
1430 |
} |
|
1431 |
||
1432 |
/* |
|
1433 |
Open the configuration file and try to use it using a named |
|
1434 |
semaphore on Windows and an advisory lock on Unix-based |
|
1435 |
systems. This protect us against other QSettings instances |
|
1436 |
trying to access the same file from other threads or |
|
1437 |
processes. |
|
1438 |
||
1439 |
As it stands now, the locking mechanism doesn't work for |
|
1440 |
.plist files. |
|
1441 |
*/ |
|
1442 |
QFile file(confFile->name); |
|
1443 |
bool createFile = !file.exists(); |
|
1444 |
if (!readOnly && confFile->isWritable()) |
|
1445 |
file.open(QFile::ReadWrite); |
|
1446 |
if (!file.isOpen()) |
|
1447 |
file.open(QFile::ReadOnly); |
|
1448 |
||
1449 |
#ifdef Q_OS_WIN |
|
1450 |
HANDLE readSemaphore = 0; |
|
1451 |
HANDLE writeSemaphore = 0; |
|
1452 |
static const int FileLockSemMax = 50; |
|
1453 |
int numReadLocks = readOnly ? 1 : FileLockSemMax; |
|
1454 |
||
1455 |
if (file.isOpen()) { |
|
1456 |
// Acquire the write lock if we will be writing |
|
1457 |
if (!readOnly) { |
|
1458 |
QString writeSemName = QLatin1String("QSettingsWriteSem "); |
|
1459 |
writeSemName.append(file.fileName()); |
|
1460 |
||
1461 |
writeSemaphore = CreateSemaphore(0, 1, 1, reinterpret_cast<const wchar_t *>(writeSemName.utf16())); |
|
1462 |
||
1463 |
if (writeSemaphore) { |
|
1464 |
WaitForSingleObject(writeSemaphore, INFINITE); |
|
1465 |
} else { |
|
1466 |
setStatus(QSettings::AccessError); |
|
1467 |
return; |
|
1468 |
} |
|
1469 |
} |
|
1470 |
||
1471 |
// Acquire all the read locks if we will be writing, to make sure nobody |
|
1472 |
// reads while we're writing. If we are only reading, acquire a single |
|
1473 |
// read lock. |
|
1474 |
QString readSemName(QLatin1String("QSettingsReadSem ")); |
|
1475 |
readSemName.append(file.fileName()); |
|
1476 |
||
1477 |
readSemaphore = CreateSemaphore(0, FileLockSemMax, FileLockSemMax, reinterpret_cast<const wchar_t *>(readSemName.utf16())); |
|
1478 |
||
1479 |
if (readSemaphore) { |
|
1480 |
for (int i = 0; i < numReadLocks; ++i) |
|
1481 |
WaitForSingleObject(readSemaphore, INFINITE); |
|
1482 |
} else { |
|
1483 |
setStatus(QSettings::AccessError); |
|
1484 |
if (writeSemaphore != 0) { |
|
1485 |
ReleaseSemaphore(writeSemaphore, 1, 0); |
|
1486 |
CloseHandle(writeSemaphore); |
|
1487 |
} |
|
1488 |
return; |
|
1489 |
} |
|
1490 |
} |
|
1491 |
#else |
|
1492 |
if (file.isOpen()) |
|
1493 |
unixLock(file.handle(), readOnly ? F_RDLCK : F_WRLCK); |
|
1494 |
#endif |
|
1495 |
||
1496 |
// If we have created the file, apply the file perms |
|
1497 |
if (file.isOpen()) { |
|
1498 |
if (createFile) { |
|
1499 |
QFile::Permissions perms = file.permissions() | QFile::ReadOwner | QFile::WriteOwner; |
|
1500 |
if (!confFile->userPerms) |
|
1501 |
perms |= QFile::ReadGroup | QFile::ReadOther; |
|
1502 |
file.setPermissions(perms); |
|
1503 |
} |
|
1504 |
} |
|
1505 |
||
1506 |
/* |
|
1507 |
We hold the lock. Let's reread the file if it has changed |
|
1508 |
since last time we read it. |
|
1509 |
*/ |
|
1510 |
QFileInfo fileInfo(confFile->name); |
|
1511 |
bool mustReadFile = true; |
|
1512 |
||
1513 |
if (!readOnly) |
|
1514 |
mustReadFile = (confFile->size != fileInfo.size() |
|
1515 |
|| (confFile->size != 0 && confFile->timeStamp != fileInfo.lastModified())); |
|
1516 |
||
1517 |
if (mustReadFile) { |
|
1518 |
confFile->unparsedIniSections.clear(); |
|
1519 |
confFile->originalKeys.clear(); |
|
1520 |
||
1521 |
/* |
|
1522 |
Files that we can't read (because of permissions or |
|
1523 |
because they don't exist) are treated as empty files. |
|
1524 |
*/ |
|
1525 |
if (file.isReadable() && fileInfo.size() != 0) { |
|
1526 |
#ifdef Q_OS_MAC |
|
1527 |
if (format == QSettings::NativeFormat) { |
|
1528 |
ok = readPlistFile(confFile->name, &confFile->originalKeys); |
|
1529 |
} else |
|
1530 |
#endif |
|
1531 |
{ |
|
1532 |
if (format <= QSettings::IniFormat) { |
|
1533 |
QByteArray data = file.readAll(); |
|
1534 |
ok = readIniFile(data, &confFile->unparsedIniSections); |
|
1535 |
} else { |
|
1536 |
if (readFunc) { |
|
1537 |
QSettings::SettingsMap tempNewKeys; |
|
1538 |
ok = readFunc(file, tempNewKeys); |
|
1539 |
||
1540 |
if (ok) { |
|
1541 |
QSettings::SettingsMap::const_iterator i = tempNewKeys.constBegin(); |
|
1542 |
while (i != tempNewKeys.constEnd()) { |
|
1543 |
confFile->originalKeys.insert(QSettingsKey(i.key(), |
|
1544 |
caseSensitivity), |
|
1545 |
i.value()); |
|
1546 |
++i; |
|
1547 |
} |
|
1548 |
} |
|
1549 |
} else { |
|
1550 |
ok = false; |
|
1551 |
} |
|
1552 |
} |
|
1553 |
} |
|
1554 |
||
1555 |
if (!ok) |
|
1556 |
setStatus(QSettings::FormatError); |
|
1557 |
} |
|
1558 |
||
1559 |
confFile->size = fileInfo.size(); |
|
1560 |
confFile->timeStamp = fileInfo.lastModified(); |
|
1561 |
} |
|
1562 |
||
1563 |
/* |
|
1564 |
We also need to save the file. We still hold the file lock, |
|
1565 |
so everything is under control. |
|
1566 |
*/ |
|
1567 |
if (!readOnly) { |
|
1568 |
ensureAllSectionsParsed(confFile); |
|
1569 |
ParsedSettingsMap mergedKeys = confFile->mergedKeyMap(); |
|
1570 |
||
1571 |
if (file.isWritable()) { |
|
1572 |
#ifdef Q_OS_MAC |
|
1573 |
if (format == QSettings::NativeFormat) { |
|
1574 |
ok = writePlistFile(confFile->name, mergedKeys); |
|
1575 |
} else |
|
1576 |
#endif |
|
1577 |
{ |
|
1578 |
file.seek(0); |
|
1579 |
file.resize(0); |
|
1580 |
||
1581 |
if (format <= QSettings::IniFormat) { |
|
1582 |
ok = writeIniFile(file, mergedKeys); |
|
1583 |
if (!ok) { |
|
1584 |
// try to restore old data; might work if the disk was full and the new data |
|
1585 |
// was larger than the old data |
|
1586 |
file.seek(0); |
|
1587 |
file.resize(0); |
|
1588 |
writeIniFile(file, confFile->originalKeys); |
|
1589 |
} |
|
1590 |
} else { |
|
1591 |
if (writeFunc) { |
|
1592 |
QSettings::SettingsMap tempOriginalKeys; |
|
1593 |
||
1594 |
ParsedSettingsMap::const_iterator i = mergedKeys.constBegin(); |
|
1595 |
while (i != mergedKeys.constEnd()) { |
|
1596 |
tempOriginalKeys.insert(i.key(), i.value()); |
|
1597 |
++i; |
|
1598 |
} |
|
1599 |
ok = writeFunc(file, tempOriginalKeys); |
|
1600 |
} else { |
|
1601 |
ok = false; |
|
1602 |
} |
|
1603 |
} |
|
1604 |
} |
|
1605 |
} else { |
|
1606 |
ok = false; |
|
1607 |
} |
|
1608 |
||
1609 |
if (ok) { |
|
1610 |
confFile->unparsedIniSections.clear(); |
|
1611 |
confFile->originalKeys = mergedKeys; |
|
1612 |
confFile->addedKeys.clear(); |
|
1613 |
confFile->removedKeys.clear(); |
|
1614 |
||
1615 |
QFileInfo fileInfo(confFile->name); |
|
1616 |
confFile->size = fileInfo.size(); |
|
1617 |
confFile->timeStamp = fileInfo.lastModified(); |
|
1618 |
} else { |
|
1619 |
setStatus(QSettings::AccessError); |
|
1620 |
} |
|
1621 |
} |
|
1622 |
||
1623 |
/* |
|
1624 |
Release the file lock. |
|
1625 |
*/ |
|
1626 |
#ifdef Q_OS_WIN |
|
1627 |
if (readSemaphore != 0) { |
|
1628 |
ReleaseSemaphore(readSemaphore, numReadLocks, 0); |
|
1629 |
CloseHandle(readSemaphore); |
|
1630 |
} |
|
1631 |
if (writeSemaphore != 0) { |
|
1632 |
ReleaseSemaphore(writeSemaphore, 1, 0); |
|
1633 |
CloseHandle(writeSemaphore); |
|
1634 |
} |
|
1635 |
#endif |
|
1636 |
} |
|
1637 |
||
1638 |
enum { Space = 0x1, Special = 0x2 }; |
|
1639 |
||
1640 |
static const char charTraits[256] = |
|
1641 |
{ |
|
1642 |
// Space: '\t', '\n', '\r', ' ' |
|
1643 |
// Special: '\n', '\r', '"', ';', '=', '\\' |
|
1644 |
||
1645 |
0, 0, 0, 0, 0, 0, 0, 0, 0, Space, Space | Special, 0, 0, Space | Special, 0, 0, |
|
1646 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1647 |
Space, 0, Special, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1648 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Special, 0, Special, 0, 0, |
|
1649 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1650 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Special, 0, 0, 0, |
|
1651 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1652 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1653 |
||
1654 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1655 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1656 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1657 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1658 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1659 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1660 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1661 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
|
1662 |
}; |
|
1663 |
||
1664 |
bool QConfFileSettingsPrivate::readIniLine(const QByteArray &data, int &dataPos, |
|
1665 |
int &lineStart, int &lineLen, int &equalsPos) |
|
1666 |
{ |
|
1667 |
int dataLen = data.length(); |
|
1668 |
bool inQuotes = false; |
|
1669 |
||
1670 |
equalsPos = -1; |
|
1671 |
||
1672 |
lineStart = dataPos; |
|
1673 |
while (lineStart < dataLen && (charTraits[uint(uchar(data.at(lineStart)))] & Space)) |
|
1674 |
++lineStart; |
|
1675 |
||
1676 |
int i = lineStart; |
|
1677 |
while (i < dataLen) { |
|
1678 |
while (!(charTraits[uint(uchar(data.at(i)))] & Special)) { |
|
1679 |
if (++i == dataLen) |
|
1680 |
goto break_out_of_outer_loop; |
|
1681 |
} |
|
1682 |
||
1683 |
char ch = data.at(i++); |
|
1684 |
if (ch == '=') { |
|
1685 |
if (!inQuotes && equalsPos == -1) |
|
1686 |
equalsPos = i - 1; |
|
1687 |
} else if (ch == '\n' || ch == '\r') { |
|
1688 |
if (i == lineStart + 1) { |
|
1689 |
++lineStart; |
|
1690 |
} else if (!inQuotes) { |
|
1691 |
--i; |
|
1692 |
goto break_out_of_outer_loop; |
|
1693 |
} |
|
1694 |
} else if (ch == '\\') { |
|
1695 |
if (i < dataLen) { |
|
1696 |
char ch = data.at(i++); |
|
1697 |
if (i < dataLen) { |
|
1698 |
char ch2 = data.at(i); |
|
1699 |
// \n, \r, \r\n, and \n\r are legitimate line terminators in INI files |
|
1700 |
if ((ch == '\n' && ch2 == '\r') || (ch == '\r' && ch2 == '\n')) |
|
1701 |
++i; |
|
1702 |
} |
|
1703 |
} |
|
1704 |
} else if (ch == '"') { |
|
1705 |
inQuotes = !inQuotes; |
|
1706 |
} else { |
|
1707 |
Q_ASSERT(ch == ';'); |
|
1708 |
||
1709 |
if (i == lineStart + 1) { |
|
1710 |
char ch; |
|
1711 |
while (i < dataLen && ((ch = data.at(i) != '\n') && ch != '\r')) |
|
1712 |
++i; |
|
1713 |
lineStart = i; |
|
1714 |
} else if (!inQuotes) { |
|
1715 |
--i; |
|
1716 |
goto break_out_of_outer_loop; |
|
1717 |
} |
|
1718 |
} |
|
1719 |
} |
|
1720 |
||
1721 |
break_out_of_outer_loop: |
|
1722 |
dataPos = i; |
|
1723 |
lineLen = i - lineStart; |
|
1724 |
return lineLen > 0; |
|
1725 |
} |
|
1726 |
||
1727 |
/* |
|
1728 |
Returns false on parse error. However, as many keys are read as |
|
1729 |
possible, so if the user doesn't check the status he will get the |
|
1730 |
most out of the file anyway. |
|
1731 |
*/ |
|
1732 |
bool QConfFileSettingsPrivate::readIniFile(const QByteArray &data, |
|
1733 |
UnparsedSettingsMap *unparsedIniSections) |
|
1734 |
{ |
|
1735 |
#define FLUSH_CURRENT_SECTION() \ |
|
1736 |
{ \ |
|
1737 |
QByteArray §ionData = (*unparsedIniSections)[QSettingsKey(currentSection, \ |
|
1738 |
IniCaseSensitivity, \ |
|
1739 |
sectionPosition)]; \ |
|
1740 |
if (!sectionData.isEmpty()) \ |
|
1741 |
sectionData.append('\n'); \ |
|
1742 |
sectionData += data.mid(currentSectionStart, lineStart - currentSectionStart); \ |
|
1743 |
sectionPosition = ++position; \ |
|
1744 |
} |
|
1745 |
||
1746 |
QString currentSection; |
|
1747 |
int currentSectionStart = 0; |
|
1748 |
int dataPos = 0; |
|
1749 |
int lineStart; |
|
1750 |
int lineLen; |
|
1751 |
int equalsPos; |
|
1752 |
int position = 0; |
|
1753 |
int sectionPosition = 0; |
|
1754 |
bool ok = true; |
|
1755 |
||
1756 |
while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) { |
|
1757 |
char ch = data.at(lineStart); |
|
1758 |
if (ch == '[') { |
|
1759 |
FLUSH_CURRENT_SECTION(); |
|
1760 |
||
1761 |
// this is a section |
|
1762 |
QByteArray iniSection; |
|
1763 |
int idx = data.indexOf(']', lineStart); |
|
1764 |
if (idx == -1 || idx >= lineStart + lineLen) { |
|
1765 |
ok = false; |
|
1766 |
iniSection = data.mid(lineStart + 1, lineLen - 1); |
|
1767 |
} else { |
|
1768 |
iniSection = data.mid(lineStart + 1, idx - lineStart - 1); |
|
1769 |
} |
|
1770 |
||
1771 |
iniSection = iniSection.trimmed(); |
|
1772 |
||
1773 |
if (qstricmp(iniSection, "general") == 0) { |
|
1774 |
currentSection.clear(); |
|
1775 |
} else { |
|
1776 |
if (qstricmp(iniSection, "%general") == 0) { |
|
1777 |
currentSection = QLatin1String(iniSection.constData() + 1); |
|
1778 |
} else { |
|
1779 |
currentSection.clear(); |
|
1780 |
iniUnescapedKey(iniSection, 0, iniSection.size(), currentSection); |
|
1781 |
} |
|
1782 |
currentSection += QLatin1Char('/'); |
|
1783 |
} |
|
1784 |
currentSectionStart = dataPos; |
|
1785 |
} |
|
1786 |
++position; |
|
1787 |
} |
|
1788 |
||
1789 |
Q_ASSERT(lineStart == data.length()); |
|
1790 |
FLUSH_CURRENT_SECTION(); |
|
1791 |
||
1792 |
return ok; |
|
1793 |
||
1794 |
#undef FLUSH_CURRENT_SECTION |
|
1795 |
} |
|
1796 |
||
1797 |
bool QConfFileSettingsPrivate::readIniSection(const QSettingsKey §ion, const QByteArray &data, |
|
1798 |
ParsedSettingsMap *settingsMap, QTextCodec *codec) |
|
1799 |
{ |
|
1800 |
QStringList strListValue; |
|
1801 |
bool sectionIsLowercase = (section == section.originalCaseKey()); |
|
1802 |
int equalsPos; |
|
1803 |
||
1804 |
bool ok = true; |
|
1805 |
int dataPos = 0; |
|
1806 |
int lineStart; |
|
1807 |
int lineLen; |
|
1808 |
int position = section.originalKeyPosition(); |
|
1809 |
||
1810 |
while (readIniLine(data, dataPos, lineStart, lineLen, equalsPos)) { |
|
1811 |
char ch = data.at(lineStart); |
|
1812 |
Q_ASSERT(ch != '['); |
|
1813 |
||
1814 |
if (equalsPos == -1) { |
|
1815 |
if (ch != ';') |
|
1816 |
ok = false; |
|
1817 |
continue; |
|
1818 |
} |
|
1819 |
||
1820 |
int keyEnd = equalsPos; |
|
1821 |
while (keyEnd > lineStart && ((ch = data.at(keyEnd - 1)) == ' ' || ch == '\t')) |
|
1822 |
--keyEnd; |
|
1823 |
int valueStart = equalsPos + 1; |
|
1824 |
||
1825 |
QString key = section.originalCaseKey(); |
|
1826 |
bool keyIsLowercase = (iniUnescapedKey(data, lineStart, keyEnd, key) && sectionIsLowercase); |
|
1827 |
||
1828 |
QString strValue; |
|
1829 |
strValue.reserve(lineLen - (valueStart - lineStart)); |
|
1830 |
bool isStringList = iniUnescapedStringList(data, valueStart, lineStart + lineLen, |
|
1831 |
strValue, strListValue, codec); |
|
1832 |
QVariant variant; |
|
1833 |
if (isStringList) { |
|
1834 |
variant = stringListToVariantList(strListValue); |
|
1835 |
} else { |
|
1836 |
variant = stringToVariant(strValue); |
|
1837 |
} |
|
1838 |
||
1839 |
/* |
|
1840 |
We try to avoid the expensive toLower() call in |
|
1841 |
QSettingsKey by passing Qt::CaseSensitive when the |
|
1842 |
key is already in lowercase. |
|
1843 |
*/ |
|
1844 |
settingsMap->insert(QSettingsKey(key, keyIsLowercase ? Qt::CaseSensitive |
|
1845 |
: IniCaseSensitivity, |
|
1846 |
position), |
|
1847 |
variant); |
|
1848 |
++position; |
|
1849 |
} |
|
1850 |
||
1851 |
return ok; |
|
1852 |
} |
|
1853 |
||
1854 |
class QSettingsIniKey : public QString |
|
1855 |
{ |
|
1856 |
public: |
|
1857 |
inline QSettingsIniKey() : position(-1) {} |
|
1858 |
inline QSettingsIniKey(const QString &str, int pos = -1) : QString(str), position(pos) {} |
|
1859 |
||
1860 |
int position; |
|
1861 |
}; |
|
1862 |
||
1863 |
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2) |
|
1864 |
{ |
|
1865 |
if (k1.position != k2.position) |
|
1866 |
return k1.position < k2.position; |
|
1867 |
return static_cast<const QString &>(k1) < static_cast<const QString &>(k2); |
|
1868 |
} |
|
1869 |
||
1870 |
typedef QMap<QSettingsIniKey, QVariant> IniKeyMap; |
|
1871 |
||
1872 |
struct QSettingsIniSection |
|
1873 |
{ |
|
1874 |
int position; |
|
1875 |
IniKeyMap keyMap; |
|
1876 |
||
1877 |
inline QSettingsIniSection() : position(-1) {} |
|
1878 |
}; |
|
1879 |
||
1880 |
typedef QMap<QString, QSettingsIniSection> IniMap; |
|
1881 |
||
1882 |
/* |
|
1883 |
This would be more straightforward if we didn't try to remember the original |
|
1884 |
key order in the .ini file, but we do. |
|
1885 |
*/ |
|
1886 |
bool QConfFileSettingsPrivate::writeIniFile(QIODevice &device, const ParsedSettingsMap &map) |
|
1887 |
{ |
|
1888 |
IniMap iniMap; |
|
1889 |
IniMap::const_iterator i; |
|
1890 |
||
1891 |
#ifdef Q_OS_WIN |
|
1892 |
const char * const eol = "\r\n"; |
|
1893 |
#else |
|
1894 |
const char eol = '\n'; |
|
1895 |
#endif |
|
1896 |
||
1897 |
for (ParsedSettingsMap::const_iterator j = map.constBegin(); j != map.constEnd(); ++j) { |
|
1898 |
QString section; |
|
1899 |
QSettingsIniKey key(j.key().originalCaseKey(), j.key().originalKeyPosition()); |
|
1900 |
int slashPos; |
|
1901 |
||
1902 |
if ((slashPos = key.indexOf(QLatin1Char('/'))) != -1) { |
|
1903 |
section = key.left(slashPos); |
|
1904 |
key.remove(0, slashPos + 1); |
|
1905 |
} |
|
1906 |
||
1907 |
QSettingsIniSection &iniSection = iniMap[section]; |
|
1908 |
||
1909 |
// -1 means infinity |
|
1910 |
if (uint(key.position) < uint(iniSection.position)) |
|
1911 |
iniSection.position = key.position; |
|
1912 |
iniSection.keyMap[key] = j.value(); |
|
1913 |
} |
|
1914 |
||
1915 |
const int sectionCount = iniMap.size(); |
|
1916 |
QVector<QSettingsIniKey> sections; |
|
1917 |
sections.reserve(sectionCount); |
|
1918 |
for (i = iniMap.constBegin(); i != iniMap.constEnd(); ++i) |
|
1919 |
sections.append(QSettingsIniKey(i.key(), i.value().position)); |
|
1920 |
qSort(sections); |
|
1921 |
||
1922 |
bool writeError = false; |
|
1923 |
for (int j = 0; !writeError && j < sectionCount; ++j) { |
|
1924 |
i = iniMap.constFind(sections.at(j)); |
|
1925 |
Q_ASSERT(i != iniMap.constEnd()); |
|
1926 |
||
1927 |
QByteArray realSection; |
|
1928 |
||
1929 |
iniEscapedKey(i.key(), realSection); |
|
1930 |
||
1931 |
if (realSection.isEmpty()) { |
|
1932 |
realSection = "[General]"; |
|
1933 |
} else if (qstricmp(realSection, "general") == 0) { |
|
1934 |
realSection = "[%General]"; |
|
1935 |
} else { |
|
1936 |
realSection.prepend('['); |
|
1937 |
realSection.append(']'); |
|
1938 |
} |
|
1939 |
||
1940 |
if (j != 0) |
|
1941 |
realSection.prepend(eol); |
|
1942 |
realSection += eol; |
|
1943 |
||
1944 |
device.write(realSection); |
|
1945 |
||
1946 |
const IniKeyMap &ents = i.value().keyMap; |
|
1947 |
for (IniKeyMap::const_iterator j = ents.constBegin(); j != ents.constEnd(); ++j) { |
|
1948 |
QByteArray block; |
|
1949 |
iniEscapedKey(j.key(), block); |
|
1950 |
block += '='; |
|
1951 |
||
1952 |
const QVariant &value = j.value(); |
|
1953 |
||
1954 |
/* |
|
1955 |
The size() != 1 trick is necessary because |
|
1956 |
QVariant(QString("foo")).toList() returns an empty |
|
1957 |
list, not a list containing "foo". |
|
1958 |
*/ |
|
1959 |
if (value.type() == QVariant::StringList |
|
1960 |
|| (value.type() == QVariant::List && value.toList().size() != 1)) { |
|
1961 |
iniEscapedStringList(variantListToStringList(value.toList()), block, iniCodec); |
|
1962 |
} else { |
|
1963 |
iniEscapedString(variantToString(value), block, iniCodec); |
|
1964 |
} |
|
1965 |
block += eol; |
|
1966 |
if (device.write(block) == -1) { |
|
1967 |
writeError = true; |
|
1968 |
break; |
|
1969 |
} |
|
1970 |
} |
|
1971 |
} |
|
1972 |
return !writeError; |
|
1973 |
} |
|
1974 |
||
1975 |
void QConfFileSettingsPrivate::ensureAllSectionsParsed(QConfFile *confFile) const |
|
1976 |
{ |
|
1977 |
UnparsedSettingsMap::const_iterator i = confFile->unparsedIniSections.constBegin(); |
|
1978 |
const UnparsedSettingsMap::const_iterator end = confFile->unparsedIniSections.constEnd(); |
|
1979 |
||
1980 |
for (; i != end; ++i) { |
|
1981 |
if (!QConfFileSettingsPrivate::readIniSection(i.key(), i.value(), &confFile->originalKeys, iniCodec)) |
|
1982 |
setStatus(QSettings::FormatError); |
|
1983 |
} |
|
1984 |
confFile->unparsedIniSections.clear(); |
|
1985 |
} |
|
1986 |
||
1987 |
void QConfFileSettingsPrivate::ensureSectionParsed(QConfFile *confFile, |
|
1988 |
const QSettingsKey &key) const |
|
1989 |
{ |
|
1990 |
if (confFile->unparsedIniSections.isEmpty()) |
|
1991 |
return; |
|
1992 |
||
1993 |
UnparsedSettingsMap::iterator i; |
|
1994 |
||
1995 |
int indexOfSlash = key.indexOf(QLatin1Char('/')); |
|
1996 |
if (indexOfSlash != -1) { |
|
1997 |
i = confFile->unparsedIniSections.upperBound(key); |
|
1998 |
if (i == confFile->unparsedIniSections.begin()) |
|
1999 |
return; |
|
2000 |
--i; |
|
2001 |
if (i.key().isEmpty() || !key.startsWith(i.key())) |
|
2002 |
return; |
|
2003 |
} else { |
|
2004 |
i = confFile->unparsedIniSections.begin(); |
|
2005 |
if (i == confFile->unparsedIniSections.end() || !i.key().isEmpty()) |
|
2006 |
return; |
|
2007 |
} |
|
2008 |
||
2009 |
if (!QConfFileSettingsPrivate::readIniSection(i.key(), i.value(), &confFile->originalKeys, iniCodec)) |
|
2010 |
setStatus(QSettings::FormatError); |
|
2011 |
confFile->unparsedIniSections.erase(i); |
|
2012 |
} |
|
2013 |
||
2014 |
/*! |
|
2015 |
\class QSettings |
|
2016 |
\brief The QSettings class provides persistent platform-independent application settings. |
|
2017 |
||
2018 |
\ingroup io |
|
2019 |
||
2020 |
\reentrant |
|
2021 |
||
2022 |
Users normally expect an application to remember its settings |
|
2023 |
(window sizes and positions, options, etc.) across sessions. This |
|
2024 |
information is often stored in the system registry on Windows, |
|
2025 |
and in XML preferences files on Mac OS X. On Unix systems, in the |
|
2026 |
absence of a standard, many applications (including the KDE |
|
2027 |
applications) use INI text files. |
|
2028 |
||
2029 |
QSettings is an abstraction around these technologies, enabling |
|
2030 |
you to save and restore application settings in a portable |
|
2031 |
manner. It also supports \l{registerFormat()}{custom storage |
|
2032 |
formats}. |
|
2033 |
||
2034 |
QSettings's API is based on QVariant, allowing you to save |
|
2035 |
most value-based types, such as QString, QRect, and QImage, |
|
2036 |
with the minimum of effort. |
|
2037 |
||
2038 |
If all you need is a non-persistent memory-based structure, |
|
2039 |
consider using QMap<QString, QVariant> instead. |
|
2040 |
||
2041 |
\tableofcontents section1 |
|
2042 |
||
2043 |
\section1 Basic Usage |
|
2044 |
||
2045 |
When creating a QSettings object, you must pass the name of your |
|
2046 |
company or organization as well as the name of your application. |
|
2047 |
For example, if your product is called Star Runner and your |
|
2048 |
company is called MySoft, you would construct the QSettings |
|
2049 |
object as follows: |
|
2050 |
||
2051 |
\snippet doc/src/snippets/settings/settings.cpp 0 |
|
2052 |
||
2053 |
QSettings objects can be created either on the stack or on |
|
2054 |
the heap (i.e. using \c new). Constructing and destroying a |
|
2055 |
QSettings object is very fast. |
|
2056 |
||
2057 |
If you use QSettings from many places in your application, you |
|
2058 |
might want to specify the organization name and the application |
|
2059 |
name using QCoreApplication::setOrganizationName() and |
|
2060 |
QCoreApplication::setApplicationName(), and then use the default |
|
2061 |
QSettings constructor: |
|
2062 |
||
2063 |
\snippet doc/src/snippets/settings/settings.cpp 1 |
|
2064 |
\snippet doc/src/snippets/settings/settings.cpp 2 |
|
2065 |
\snippet doc/src/snippets/settings/settings.cpp 3 |
|
2066 |
\dots |
|
2067 |
\snippet doc/src/snippets/settings/settings.cpp 4 |
|
2068 |
||
2069 |
(Here, we also specify the organization's Internet domain. When |
|
2070 |
the Internet domain is set, it is used on Mac OS X instead of the |
|
2071 |
organization name, since Mac OS X applications conventionally use |
|
2072 |
Internet domains to identify themselves. If no domain is set, a |
|
2073 |
fake domain is derived from the organization name. See the |
|
2074 |
\l{Platform-Specific Notes} below for details.) |
|
2075 |
||
2076 |
QSettings stores settings. Each setting consists of a QString |
|
2077 |
that specifies the setting's name (the \e key) and a QVariant |
|
2078 |
that stores the data associated with the key. To write a setting, |
|
2079 |
use setValue(). For example: |
|
2080 |
||
2081 |
\snippet doc/src/snippets/settings/settings.cpp 5 |
|
2082 |
||
2083 |
If there already exists a setting with the same key, the existing |
|
2084 |
value is overwritten by the new value. For efficiency, the |
|
2085 |
changes may not be saved to permanent storage immediately. (You |
|
2086 |
can always call sync() to commit your changes.) |
|
2087 |
||
2088 |
You can get a setting's value back using value(): |
|
2089 |
||
2090 |
\snippet doc/src/snippets/settings/settings.cpp 6 |
|
2091 |
||
2092 |
If there is no setting with the specified name, QSettings |
|
2093 |
returns a null QVariant (which can be converted to the integer 0). |
|
2094 |
You can specify another default value by passing a second |
|
2095 |
argument to value(): |
|
2096 |
||
2097 |
\snippet doc/src/snippets/settings/settings.cpp 7 |
|
2098 |
||
2099 |
To test whether a given key exists, call contains(). To remove |
|
2100 |
the setting associated with a key, call remove(). To obtain the |
|
2101 |
list of all keys, call allKeys(). To remove all keys, call |
|
2102 |
clear(). |
|
2103 |
||
2104 |
\section1 QVariant and GUI Types |
|
2105 |
||
2106 |
Because QVariant is part of the \l QtCore library, it cannot provide |
|
2107 |
conversion functions to data types such as QColor, QImage, and |
|
2108 |
QPixmap, which are part of \l QtGui. In other words, there is no |
|
2109 |
\c toColor(), \c toImage(), or \c toPixmap() functions in QVariant. |
|
2110 |
||
2111 |
Instead, you can use the QVariant::value() or the qVariantValue() |
|
2112 |
template function. For example: |
|
2113 |
||
2114 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 0 |
|
2115 |
||
2116 |
The inverse conversion (e.g., from QColor to QVariant) is |
|
2117 |
automatic for all data types supported by QVariant, including |
|
2118 |
GUI-related types: |
|
2119 |
||
2120 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 1 |
|
2121 |
||
2122 |
Custom types registered using qRegisterMetaType() and |
|
2123 |
qRegisterMetaTypeStreamOperators() can be stored using QSettings. |
|
2124 |
||
2125 |
\section1 Section and Key Syntax |
|
2126 |
||
2127 |
Setting keys can contain any Unicode characters. The Windows |
|
2128 |
registry and INI files use case-insensitive keys, whereas the |
|
2129 |
Carbon Preferences API on Mac OS X uses case-sensitive keys. To |
|
2130 |
avoid portability problems, follow these simple rules: |
|
2131 |
||
2132 |
\list 1 |
|
2133 |
\o Always refer to the same key using the same case. For example, |
|
2134 |
if you refer to a key as "text fonts" in one place in your |
|
2135 |
code, don't refer to it as "Text Fonts" somewhere else. |
|
2136 |
||
2137 |
\o Avoid key names that are identical except for the case. For |
|
2138 |
example, if you have a key called "MainWindow", don't try to |
|
2139 |
save another key as "mainwindow". |
|
2140 |
||
2141 |
\o Do not use slashes ('/' and '\\') in section or key names; the |
|
2142 |
backslash character is used to separate sub keys (see below). On |
|
2143 |
windows '\\' are converted by QSettings to '/', which makes |
|
2144 |
them identical. |
|
2145 |
\endlist |
|
2146 |
||
2147 |
You can form hierarchical keys using the '/' character as a |
|
2148 |
separator, similar to Unix file paths. For example: |
|
2149 |
||
2150 |
\snippet doc/src/snippets/settings/settings.cpp 8 |
|
2151 |
\snippet doc/src/snippets/settings/settings.cpp 9 |
|
2152 |
\snippet doc/src/snippets/settings/settings.cpp 10 |
|
2153 |
||
2154 |
If you want to save or restore many settings with the same |
|
2155 |
prefix, you can specify the prefix using beginGroup() and call |
|
2156 |
endGroup() at the end. Here's the same example again, but this |
|
2157 |
time using the group mechanism: |
|
2158 |
||
2159 |
\snippet doc/src/snippets/settings/settings.cpp 11 |
|
2160 |
\codeline |
|
2161 |
\snippet doc/src/snippets/settings/settings.cpp 12 |
|
2162 |
||
2163 |
If a group is set using beginGroup(), the behavior of most |
|
2164 |
functions changes consequently. Groups can be set recursively. |
|
2165 |
||
2166 |
In addition to groups, QSettings also supports an "array" |
|
2167 |
concept. See beginReadArray() and beginWriteArray() for details. |
|
2168 |
||
2169 |
\section1 Fallback Mechanism |
|
2170 |
||
2171 |
Let's assume that you have created a QSettings object with the |
|
2172 |
organization name MySoft and the application name Star Runner. |
|
2173 |
When you look up a value, up to four locations are searched in |
|
2174 |
that order: |
|
2175 |
||
2176 |
\list 1 |
|
2177 |
\o a user-specific location for the Star Runner application |
|
2178 |
\o a user-specific location for all applications by MySoft |
|
2179 |
\o a system-wide location for the Star Runner application |
|
2180 |
\o a system-wide location for all applications by MySoft |
|
2181 |
\endlist |
|
2182 |
||
2183 |
(See \l{Platform-Specific Notes} below for information on what |
|
2184 |
these locations are on the different platforms supported by Qt.) |
|
2185 |
||
2186 |
If a key cannot be found in the first location, the search goes |
|
2187 |
on in the second location, and so on. This enables you to store |
|
2188 |
system-wide or organization-wide settings and to override them on |
|
2189 |
a per-user or per-application basis. To turn off this mechanism, |
|
2190 |
call setFallbacksEnabled(false). |
|
2191 |
||
2192 |
Although keys from all four locations are available for reading, |
|
2193 |
only the first file (the user-specific location for the |
|
2194 |
application at hand) is accessible for writing. To write to any |
|
2195 |
of the other files, omit the application name and/or specify |
|
2196 |
QSettings::SystemScope (as opposed to QSettings::UserScope, the |
|
2197 |
default). |
|
2198 |
||
2199 |
Let's see with an example: |
|
2200 |
||
2201 |
\snippet doc/src/snippets/settings/settings.cpp 13 |
|
2202 |
\snippet doc/src/snippets/settings/settings.cpp 14 |
|
2203 |
||
2204 |
The table below summarizes which QSettings objects access |
|
2205 |
which location. "\bold{X}" means that the location is the main |
|
2206 |
location associated to the QSettings object and is used both |
|
2207 |
for reading and for writing; "o" means that the location is used |
|
2208 |
as a fallback when reading. |
|
2209 |
||
2210 |
\table |
|
2211 |
\header \o Locations \o \c{obj1} \o \c{obj2} \o \c{obj3} \o \c{obj4} |
|
2212 |
\row \o 1. User, Application \o \bold{X} \o \o \o |
|
2213 |
\row \o 2. User, Organization \o o \o \bold{X} \o \o |
|
2214 |
\row \o 3. System, Application \o o \o \o \bold{X} \o |
|
2215 |
\row \o 4. System, Organization \o o \o o \o o \o \bold{X} |
|
2216 |
\endtable |
|
2217 |
||
2218 |
The beauty of this mechanism is that it works on all platforms |
|
2219 |
supported by Qt and that it still gives you a lot of flexibility, |
|
2220 |
without requiring you to specify any file names or registry |
|
2221 |
paths. |
|
2222 |
||
2223 |
If you want to use INI files on all platforms instead of the |
|
2224 |
native API, you can pass QSettings::IniFormat as the first |
|
2225 |
argument to the QSettings constructor, followed by the scope, the |
|
2226 |
organization name, and the application name: |
|
2227 |
||
2228 |
\snippet doc/src/snippets/settings/settings.cpp 15 |
|
2229 |
||
2230 |
The \l{tools/settingseditor}{Settings Editor} example lets you |
|
2231 |
experiment with different settings location and with fallbacks |
|
2232 |
turned on or off. |
|
2233 |
||
2234 |
\section1 Restoring the State of a GUI Application |
|
2235 |
||
2236 |
QSettings is often used to store the state of a GUI |
|
2237 |
application. The following example illustrates how to use QSettings |
|
2238 |
to save and restore the geometry of an application's main window. |
|
2239 |
||
2240 |
\snippet doc/src/snippets/settings/settings.cpp 16 |
|
2241 |
\codeline |
|
2242 |
\snippet doc/src/snippets/settings/settings.cpp 17 |
|
2243 |
||
2244 |
See \l{Window Geometry} for a discussion on why it is better to |
|
2245 |
call QWidget::resize() and QWidget::move() rather than QWidget::setGeometry() |
|
2246 |
to restore a window's geometry. |
|
2247 |
||
2248 |
The \c readSettings() and \c writeSettings() functions must be |
|
2249 |
called from the main window's constructor and close event handler |
|
2250 |
as follows: |
|
2251 |
||
2252 |
\snippet doc/src/snippets/settings/settings.cpp 18 |
|
2253 |
\dots |
|
2254 |
\snippet doc/src/snippets/settings/settings.cpp 19 |
|
2255 |
\snippet doc/src/snippets/settings/settings.cpp 20 |
|
2256 |
\codeline |
|
2257 |
\snippet doc/src/snippets/settings/settings.cpp 21 |
|
2258 |
||
2259 |
See the \l{mainwindows/application}{Application} example for a |
|
2260 |
self-contained example that uses QSettings. |
|
2261 |
||
2262 |
\section1 Accessing Settings from Multiple Threads or Processes Simultaneously |
|
2263 |
||
2264 |
QSettings is \l{reentrant}. This means that you can use |
|
2265 |
distinct QSettings object in different threads |
|
2266 |
simultaneously. This guarantee stands even when the QSettings |
|
2267 |
objects refer to the same files on disk (or to the same entries |
|
2268 |
in the system registry). If a setting is modified through one |
|
2269 |
QSettings object, the change will immediately be visible in |
|
2270 |
any other QSettings objects that operate on the same location |
|
2271 |
and that live in the same process. |
|
2272 |
||
2273 |
QSettings can safely be used from different processes (which can |
|
2274 |
be different instances of your application running at the same |
|
2275 |
time or different applications altogether) to read and write to |
|
2276 |
the same system locations. It uses advisory file locking and a |
|
2277 |
smart merging algorithm to ensure data integrity. Changes |
|
2278 |
performed by another process aren't visible in the current |
|
2279 |
process until sync() is called. |
|
2280 |
||
2281 |
\section1 Platform-Specific Notes |
|
2282 |
||
2283 |
\section2 Locations Where Application Settings Are Stored |
|
2284 |
||
2285 |
As mentioned in the \l{Fallback Mechanism} section, QSettings |
|
2286 |
stores settings for an application in up to four locations, |
|
2287 |
depending on whether the settings are user-specific or |
|
2288 |
system-wide and whether the settings are application-specific |
|
2289 |
or organization-wide. For simplicity, we're assuming the |
|
2290 |
organization is called MySoft and the application is called Star |
|
2291 |
Runner. |
|
2292 |
||
2293 |
On Unix systems, if the file format is NativeFormat, the |
|
2294 |
following files are used by default: |
|
2295 |
||
2296 |
\list 1 |
|
2297 |
\o \c{$HOME/.config/MySoft/Star Runner.conf} (Qt for Embedded Linux: \c{$HOME/Settings/MySoft/Star Runner.conf}) |
|
2298 |
\o \c{$HOME/.config/MySoft.conf} (Qt for Embedded Linux: \c{$HOME/Settings/MySoft.conf}) |
|
2299 |
\o \c{/etc/xdg/MySoft/Star Runner.conf} |
|
2300 |
\o \c{/etc/xdg/MySoft.conf} |
|
2301 |
\endlist |
|
2302 |
||
2303 |
On Mac OS X versions 10.2 and 10.3, these files are used by |
|
2304 |
default: |
|
2305 |
||
2306 |
\list 1 |
|
2307 |
\o \c{$HOME/Library/Preferences/com.MySoft.Star Runner.plist} |
|
2308 |
\o \c{$HOME/Library/Preferences/com.MySoft.plist} |
|
2309 |
\o \c{/Library/Preferences/com.MySoft.Star Runner.plist} |
|
2310 |
\o \c{/Library/Preferences/com.MySoft.plist} |
|
2311 |
\endlist |
|
2312 |
||
2313 |
On Windows, NativeFormat settings are stored in the following |
|
2314 |
registry paths: |
|
2315 |
||
2316 |
\list 1 |
|
2317 |
\o \c{HKEY_CURRENT_USER\Software\MySoft\Star Runner} |
|
2318 |
\o \c{HKEY_CURRENT_USER\Software\MySoft} |
|
2319 |
\o \c{HKEY_LOCAL_MACHINE\Software\MySoft\Star Runner} |
|
2320 |
\o \c{HKEY_LOCAL_MACHINE\Software\MySoft} |
|
2321 |
\endlist |
|
2322 |
||
2323 |
\note On Windows, for 32-bit programs running in WOW64 mode, settings are |
|
2324 |
stored in the following registry path: |
|
2325 |
\c{HKEY_LOCAL_MACHINE\Software\WOW6432node}. |
|
2326 |
||
2327 |
If the file format is IniFormat, the following files are |
|
2328 |
used on Unix and Mac OS X: |
|
2329 |
||
2330 |
\list 1 |
|
2331 |
\o \c{$HOME/.config/MySoft/Star Runner.ini} (Qt for Embedded Linux: \c{$HOME/Settings/MySoft/Star Runner.ini}) |
|
2332 |
\o \c{$HOME/.config/MySoft.ini} (Qt for Embedded Linux: \c{$HOME/Settings/MySoft.ini}) |
|
2333 |
\o \c{/etc/xdg/MySoft/Star Runner.ini} |
|
2334 |
\o \c{/etc/xdg/MySoft.ini} |
|
2335 |
\endlist |
|
2336 |
||
2337 |
On Windows, the following files are used: |
|
2338 |
||
2339 |
\list 1 |
|
2340 |
\o \c{%APPDATA%\MySoft\Star Runner.ini} |
|
2341 |
\o \c{%APPDATA%\MySoft.ini} |
|
2342 |
\o \c{%COMMON_APPDATA%\MySoft\Star Runner.ini} |
|
2343 |
\o \c{%COMMON_APPDATA%\MySoft.ini} |
|
2344 |
\endlist |
|
2345 |
||
2346 |
The \c %APPDATA% path is usually \tt{C:\\Documents and |
|
2347 |
Settings\\\e{User Name}\\Application Data}; the \c |
|
2348 |
%COMMON_APPDATA% path is usually \tt{C:\\Documents and |
|
2349 |
Settings\\All Users\\Application Data}. |
|
2350 |
||
2351 |
The paths for the \c .ini and \c .conf files can be changed using |
|
2352 |
setPath(). On Unix and Mac OS X, the user can override them by by |
|
2353 |
setting the \c XDG_CONFIG_HOME environment variable; see |
|
2354 |
setPath() for details. |
|
2355 |
||
2356 |
\section2 Accessing INI and .plist Files Directly |
|
2357 |
||
2358 |
Sometimes you do want to access settings stored in a specific |
|
2359 |
file or registry path. On all platforms, if you want to read an |
|
2360 |
INI file directly, you can use the QSettings constructor that |
|
2361 |
takes a file name as first argument and pass QSettings::IniFormat |
|
2362 |
as second argument. For example: |
|
2363 |
||
2364 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 2 |
|
2365 |
||
2366 |
You can then use the QSettings object to read and write settings |
|
2367 |
in the file. |
|
2368 |
||
2369 |
On Mac OS X, you can access XML-based \c .plist files by passing |
|
2370 |
QSettings::NativeFormat as second argument. For example: |
|
2371 |
||
2372 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 3 |
|
2373 |
||
2374 |
\section2 Accessing the Windows Registry Directly |
|
2375 |
||
2376 |
On Windows, QSettings lets you access settings that have been |
|
2377 |
written with QSettings (or settings in a supported format, e.g., string |
|
2378 |
data) in the system registry. This is done by constructing a QSettings |
|
2379 |
object with a path in the registry and QSettings::NativeFormat. |
|
2380 |
||
2381 |
For example: |
|
2382 |
||
2383 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 4 |
|
2384 |
||
2385 |
All the registry entries that appear under the specified path can |
|
2386 |
be read or written through the QSettings object as usual (using |
|
2387 |
forward slashes instead of backslashes). For example: |
|
2388 |
||
2389 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 5 |
|
2390 |
||
2391 |
Note that the backslash character is, as mentioned, used by |
|
2392 |
QSettings to separate subkeys. As a result, you cannot read or |
|
2393 |
write windows registry entries that contain slashes or |
|
2394 |
backslashes; you should use a native windows API if you need to do |
|
2395 |
so. |
|
2396 |
||
2397 |
\section2 Accessing Common Registry Settings on Windows |
|
2398 |
||
2399 |
On Windows, it is possible for a key to have both a value and subkeys. |
|
2400 |
Its default value is accessed by using "Default" or "." in |
|
2401 |
place of a subkey: |
|
2402 |
||
2403 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 6 |
|
2404 |
||
2405 |
On other platforms than Windows, "Default" and "." would be |
|
2406 |
treated as regular subkeys. |
|
2407 |
||
2408 |
\section2 Platform Limitations |
|
2409 |
||
2410 |
While QSettings attempts to smooth over the differences between |
|
2411 |
the different supported platforms, there are still a few |
|
2412 |
differences that you should be aware of when porting your |
|
2413 |
application: |
|
2414 |
||
2415 |
\list |
|
2416 |
\o The Windows system registry has the following limitations: A |
|
2417 |
subkey may not exceed 255 characters, an entry's value may |
|
2418 |
not exceed 16,383 characters, and all the values of a key may |
|
2419 |
not exceed 65,535 characters. One way to work around these |
|
2420 |
limitations is to store the settings using the IniFormat |
|
2421 |
instead of the NativeFormat. |
|
2422 |
||
2423 |
\o On Mac OS X, allKeys() will return some extra keys for global |
|
2424 |
settings that apply to all applications. These keys can be |
|
2425 |
read using value() but cannot be changed, only shadowed. |
|
2426 |
Calling setFallbacksEnabled(false) will hide these global |
|
2427 |
settings. |
|
2428 |
||
2429 |
\o On Mac OS X, the CFPreferences API used by QSettings expects |
|
2430 |
Internet domain names rather than organization names. To |
|
2431 |
provide a uniform API, QSettings derives a fake domain name |
|
2432 |
from the organization name (unless the organization name |
|
2433 |
already is a domain name, e.g. OpenOffice.org). The algorithm |
|
2434 |
appends ".com" to the company name and replaces spaces and |
|
2435 |
other illegal characters with hyphens. If you want to specify |
|
2436 |
a different domain name, call |
|
2437 |
QCoreApplication::setOrganizationDomain(), |
|
2438 |
QCoreApplication::setOrganizationName(), and |
|
2439 |
QCoreApplication::setApplicationName() in your \c main() |
|
2440 |
function and then use the default QSettings constructor. |
|
2441 |
Another solution is to use preprocessor directives, for |
|
2442 |
example: |
|
2443 |
||
2444 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 7 |
|
2445 |
||
2446 |
\o On Unix and Mac OS X systems, the advisory file locking is disabled |
|
2447 |
if NFS (or AutoFS or CacheFS) is detected to work around a bug in the |
|
2448 |
NFS fcntl() implementation, which hangs forever if statd or lockd aren't |
|
2449 |
running. Also, the locking isn't performed when accessing \c .plist |
|
2450 |
files. |
|
2451 |
||
2452 |
\endlist |
|
2453 |
||
2454 |
\sa QVariant, QSessionManager, {Settings Editor Example}, {Application Example} |
|
2455 |
*/ |
|
2456 |
||
2457 |
/*! \enum QSettings::Status |
|
2458 |
||
2459 |
The following status values are possible: |
|
2460 |
||
2461 |
\value NoError No error occurred. |
|
2462 |
\value AccessError An access error occurred (e.g. trying to write to a read-only file). |
|
2463 |
\value FormatError A format error occurred (e.g. loading a malformed INI file). |
|
2464 |
||
2465 |
\sa status() |
|
2466 |
*/ |
|
2467 |
||
2468 |
/*! \enum QSettings::Format |
|
2469 |
||
2470 |
This enum type specifies the storage format used by QSettings. |
|
2471 |
||
2472 |
\value NativeFormat Store the settings using the most |
|
2473 |
appropriate storage format for the platform. |
|
2474 |
On Windows, this means the system registry; |
|
2475 |
on Mac OS X, this means the CFPreferences |
|
2476 |
API; on Unix, this means textual |
|
2477 |
configuration files in INI format. |
|
2478 |
\value IniFormat Store the settings in INI files. |
|
2479 |
\value InvalidFormat Special value returned by registerFormat(). |
|
2480 |
\omitvalue CustomFormat1 |
|
2481 |
\omitvalue CustomFormat2 |
|
2482 |
\omitvalue CustomFormat3 |
|
2483 |
\omitvalue CustomFormat4 |
|
2484 |
\omitvalue CustomFormat5 |
|
2485 |
\omitvalue CustomFormat6 |
|
2486 |
\omitvalue CustomFormat7 |
|
2487 |
\omitvalue CustomFormat8 |
|
2488 |
\omitvalue CustomFormat9 |
|
2489 |
\omitvalue CustomFormat10 |
|
2490 |
\omitvalue CustomFormat11 |
|
2491 |
\omitvalue CustomFormat12 |
|
2492 |
\omitvalue CustomFormat13 |
|
2493 |
\omitvalue CustomFormat14 |
|
2494 |
\omitvalue CustomFormat15 |
|
2495 |
\omitvalue CustomFormat16 |
|
2496 |
||
2497 |
On Unix, NativeFormat and IniFormat mean the same thing, except |
|
2498 |
that the file extension is different (\c .conf for NativeFormat, |
|
2499 |
\c .ini for IniFormat). |
|
2500 |
||
2501 |
The INI file format is a Windows file format that Qt supports on |
|
2502 |
all platforms. In the absence of an INI standard, we try to |
|
2503 |
follow what Microsoft does, with the following exceptions: |
|
2504 |
||
2505 |
\list |
|
2506 |
\o If you store types that QVariant can't convert to QString |
|
2507 |
(e.g., QPoint, QRect, and QSize), Qt uses an \c{@}-based |
|
2508 |
syntax to encode the type. For example: |
|
2509 |
||
2510 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 8 |
|
2511 |
||
2512 |
To minimize compatibility issues, any \c @ that doesn't |
|
2513 |
appear at the first position in the value or that isn't |
|
2514 |
followed by a Qt type (\c Point, \c Rect, \c Size, etc.) is |
|
2515 |
treated as a normal character. |
|
2516 |
||
2517 |
\o Although backslash is a special character in INI files, most |
|
2518 |
Windows applications don't escape backslashes (\c{\}) in file |
|
2519 |
paths: |
|
2520 |
||
2521 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 9 |
|
2522 |
||
2523 |
QSettings always treats backslash as a special character and |
|
2524 |
provides no API for reading or writing such entries. |
|
2525 |
||
2526 |
\o The INI file format has severe restrictions on the syntax of |
|
2527 |
a key. Qt works around this by using \c % as an escape |
|
2528 |
character in keys. In addition, if you save a top-level |
|
2529 |
setting (a key with no slashes in it, e.g., "someKey"), it |
|
2530 |
will appear in the INI file's "General" section. To avoid |
|
2531 |
overwriting other keys, if you save something using the a key |
|
2532 |
such as "General/someKey", the key will be located in the |
|
2533 |
"%General" section, \e not in the "General" section. |
|
2534 |
||
2535 |
\o Following the philosophy that we should be liberal in what |
|
2536 |
we accept and conservative in what we generate, QSettings |
|
2537 |
will accept Latin-1 encoded INI files, but generate pure |
|
2538 |
ASCII files, where non-ASCII values are encoded using standard |
|
2539 |
INI escape sequences. To make the INI files more readable (but |
|
2540 |
potentially less compatible), call setIniCodec(). |
|
2541 |
\endlist |
|
2542 |
||
2543 |
\sa registerFormat(), setPath() |
|
2544 |
*/ |
|
2545 |
||
2546 |
/*! \enum QSettings::Scope |
|
2547 |
||
2548 |
This enum specifies whether settings are user-specific or shared |
|
2549 |
by all users of the same system. |
|
2550 |
||
2551 |
\value UserScope Store settings in a location specific to the |
|
2552 |
current user (e.g., in the user's home |
|
2553 |
directory). |
|
2554 |
\value SystemScope Store settings in a global location, so that |
|
2555 |
all users on the same machine access the same |
|
2556 |
set of settings. |
|
2557 |
\omitvalue User |
|
2558 |
\omitvalue Global |
|
2559 |
||
2560 |
\sa setPath() |
|
2561 |
*/ |
|
2562 |
||
2563 |
#ifndef QT_NO_QOBJECT |
|
2564 |
/*! |
|
2565 |
Constructs a QSettings object for accessing settings of the |
|
2566 |
application called \a application from the organization called \a |
|
2567 |
organization, and with parent \a parent. |
|
2568 |
||
2569 |
Example: |
|
2570 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 10 |
|
2571 |
||
2572 |
The scope is set to QSettings::UserScope, and the format is |
|
2573 |
set to QSettings::NativeFormat (i.e. calling setDefaultFormat() |
|
2574 |
before calling this constructor has no effect). |
|
2575 |
||
2576 |
\sa setDefaultFormat(), {Fallback Mechanism} |
|
2577 |
*/ |
|
2578 |
QSettings::QSettings(const QString &organization, const QString &application, QObject *parent) |
|
2579 |
: QObject(*QSettingsPrivate::create(NativeFormat, UserScope, organization, application), |
|
2580 |
parent) |
|
2581 |
{ |
|
2582 |
} |
|
2583 |
||
2584 |
/*! |
|
2585 |
Constructs a QSettings object for accessing settings of the |
|
2586 |
application called \a application from the organization called \a |
|
2587 |
organization, and with parent \a parent. |
|
2588 |
||
2589 |
If \a scope is QSettings::UserScope, the QSettings object searches |
|
2590 |
user-specific settings first, before it searches system-wide |
|
2591 |
settings as a fallback. If \a scope is QSettings::SystemScope, the |
|
2592 |
QSettings object ignores user-specific settings and provides |
|
2593 |
access to system-wide settings. |
|
2594 |
||
2595 |
The storage format is set to QSettings::NativeFormat (i.e. calling |
|
2596 |
setDefaultFormat() before calling this constructor has no effect). |
|
2597 |
||
2598 |
If no application name is given, the QSettings object will |
|
2599 |
only access the organization-wide \l{Fallback Mechanism}{locations}. |
|
2600 |
||
2601 |
\sa setDefaultFormat() |
|
2602 |
*/ |
|
2603 |
QSettings::QSettings(Scope scope, const QString &organization, const QString &application, |
|
2604 |
QObject *parent) |
|
2605 |
: QObject(*QSettingsPrivate::create(NativeFormat, scope, organization, application), parent) |
|
2606 |
{ |
|
2607 |
} |
|
2608 |
||
2609 |
/*! |
|
2610 |
Constructs a QSettings object for accessing settings of the |
|
2611 |
application called \a application from the organization called |
|
2612 |
\a organization, and with parent \a parent. |
|
2613 |
||
2614 |
If \a scope is QSettings::UserScope, the QSettings object searches |
|
2615 |
user-specific settings first, before it searches system-wide |
|
2616 |
settings as a fallback. If \a scope is |
|
2617 |
QSettings::SystemScope, the QSettings object ignores user-specific |
|
2618 |
settings and provides access to system-wide settings. |
|
2619 |
||
2620 |
If \a format is QSettings::NativeFormat, the native API is used for |
|
2621 |
storing settings. If \a format is QSettings::IniFormat, the INI format |
|
2622 |
is used. |
|
2623 |
||
2624 |
If no application name is given, the QSettings object will |
|
2625 |
only access the organization-wide \l{Fallback Mechanism}{locations}. |
|
2626 |
*/ |
|
2627 |
QSettings::QSettings(Format format, Scope scope, const QString &organization, |
|
2628 |
const QString &application, QObject *parent) |
|
2629 |
: QObject(*QSettingsPrivate::create(format, scope, organization, application), parent) |
|
2630 |
{ |
|
2631 |
} |
|
2632 |
||
2633 |
/*! |
|
2634 |
Constructs a QSettings object for accessing the settings |
|
2635 |
stored in the file called \a fileName, with parent \a parent. If |
|
2636 |
the file doesn't already exist, it is created. |
|
2637 |
||
2638 |
If \a format is QSettings::NativeFormat, the meaning of \a |
|
2639 |
fileName depends on the platform. On Unix, \a fileName is the |
|
2640 |
name of an INI file. On Mac OS X, \a fileName is the name of a |
|
2641 |
\c .plist file. On Windows, \a fileName is a path in the system |
|
2642 |
registry. |
|
2643 |
||
2644 |
If \a format is QSettings::IniFormat, \a fileName is the name of an INI |
|
2645 |
file. |
|
2646 |
||
2647 |
\warning This function is provided for convenience. It works well for |
|
2648 |
accessing INI or \c .plist files generated by Qt, but might fail on some |
|
2649 |
syntaxes found in such files originated by other programs. In particular, |
|
2650 |
be aware of the following limitations: |
|
2651 |
||
2652 |
\list |
|
2653 |
\o QSettings provides no way of reading INI "path" entries, i.e., entries |
|
2654 |
with unescaped slash characters. (This is because these entries are |
|
2655 |
ambiguous and cannot be resolved automatically.) |
|
2656 |
\o In INI files, QSettings uses the \c @ character as a metacharacter in some |
|
2657 |
contexts, to encode Qt-specific data types (e.g., \c @Rect), and might |
|
2658 |
therefore misinterpret it when it occurs in pure INI files. |
|
2659 |
\endlist |
|
2660 |
||
2661 |
\sa fileName() |
|
2662 |
*/ |
|
2663 |
QSettings::QSettings(const QString &fileName, Format format, QObject *parent) |
|
2664 |
: QObject(*QSettingsPrivate::create(fileName, format), parent) |
|
2665 |
{ |
|
2666 |
} |
|
2667 |
||
2668 |
/*! |
|
2669 |
Constructs a QSettings object for accessing settings of the |
|
2670 |
application and organization set previously with a call to |
|
2671 |
QCoreApplication::setOrganizationName(), |
|
2672 |
QCoreApplication::setOrganizationDomain(), and |
|
2673 |
QCoreApplication::setApplicationName(). |
|
2674 |
||
2675 |
The scope is QSettings::UserScope and the format is |
|
2676 |
defaultFormat() (QSettings::NativeFormat by default). |
|
2677 |
Use setDefaultFormat() before calling this constructor |
|
2678 |
to change the default format used by this constructor. |
|
2679 |
||
2680 |
The code |
|
2681 |
||
2682 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 11 |
|
2683 |
||
2684 |
is equivalent to |
|
2685 |
||
2686 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 12 |
|
2687 |
||
2688 |
If QCoreApplication::setOrganizationName() and |
|
2689 |
QCoreApplication::setApplicationName() has not been previously |
|
2690 |
called, the QSettings object will not be able to read or write |
|
2691 |
any settings, and status() will return AccessError. |
|
2692 |
||
2693 |
On Mac OS X, if both a name and an Internet domain are specified |
|
2694 |
for the organization, the domain is preferred over the name. On |
|
2695 |
other platforms, the name is preferred over the domain. |
|
2696 |
||
2697 |
\sa QCoreApplication::setOrganizationName(), |
|
2698 |
QCoreApplication::setOrganizationDomain(), |
|
2699 |
QCoreApplication::setApplicationName(), |
|
2700 |
setDefaultFormat() |
|
2701 |
*/ |
|
2702 |
QSettings::QSettings(QObject *parent) |
|
2703 |
: QObject(*QSettingsPrivate::create(globalDefaultFormat, UserScope, |
|
2704 |
#ifdef Q_OS_MAC |
|
2705 |
QCoreApplication::organizationDomain().isEmpty() |
|
2706 |
? QCoreApplication::organizationName() |
|
2707 |
: QCoreApplication::organizationDomain() |
|
2708 |
#else |
|
2709 |
QCoreApplication::organizationName().isEmpty() |
|
2710 |
? QCoreApplication::organizationDomain() |
|
2711 |
: QCoreApplication::organizationName() |
|
2712 |
#endif |
|
2713 |
, QCoreApplication::applicationName()), |
|
2714 |
parent) |
|
2715 |
{ |
|
2716 |
} |
|
2717 |
||
2718 |
#else |
|
2719 |
QSettings::QSettings(const QString &organization, const QString &application) |
|
2720 |
: d_ptr(QSettingsPrivate::create(globalDefaultFormat, QSettings::UserScope, organization, application)) |
|
2721 |
{ |
|
2722 |
d_ptr->q_ptr = this; |
|
2723 |
} |
|
2724 |
||
2725 |
QSettings::QSettings(Scope scope, const QString &organization, const QString &application) |
|
2726 |
: d_ptr(QSettingsPrivate::create(globalDefaultFormat, scope, organization, application)) |
|
2727 |
{ |
|
2728 |
d_ptr->q_ptr = this; |
|
2729 |
} |
|
2730 |
||
2731 |
QSettings::QSettings(Format format, Scope scope, const QString &organization, |
|
2732 |
const QString &application) |
|
2733 |
: d_ptr(QSettingsPrivate::create(format, scope, organization, application)) |
|
2734 |
{ |
|
2735 |
d_ptr->q_ptr = this; |
|
2736 |
} |
|
2737 |
||
2738 |
QSettings::QSettings(const QString &fileName, Format format) |
|
2739 |
: d_ptr(QSettingsPrivate::create(fileName, format)) |
|
2740 |
{ |
|
2741 |
d_ptr->q_ptr = this; |
|
2742 |
} |
|
2743 |
#endif |
|
2744 |
||
2745 |
/*! |
|
2746 |
Destroys the QSettings object. |
|
2747 |
||
2748 |
Any unsaved changes will eventually be written to permanent |
|
2749 |
storage. |
|
2750 |
||
2751 |
\sa sync() |
|
2752 |
*/ |
|
2753 |
QSettings::~QSettings() |
|
2754 |
{ |
|
2755 |
Q_D(QSettings); |
|
2756 |
if (d->pendingChanges) { |
|
2757 |
QT_TRY { |
|
2758 |
d->flush(); |
|
2759 |
} QT_CATCH(...) { |
|
2760 |
; // ok. then don't flush but at least don't throw in the destructor |
|
2761 |
} |
|
2762 |
} |
|
2763 |
} |
|
2764 |
||
2765 |
/*! |
|
2766 |
Removes all entries in the primary location associated to this |
|
2767 |
QSettings object. |
|
2768 |
||
2769 |
Entries in fallback locations are not removed. |
|
2770 |
||
2771 |
If you only want to remove the entries in the current group(), |
|
2772 |
use remove("") instead. |
|
2773 |
||
2774 |
\sa remove(), setFallbacksEnabled() |
|
2775 |
*/ |
|
2776 |
void QSettings::clear() |
|
2777 |
{ |
|
2778 |
Q_D(QSettings); |
|
2779 |
d->clear(); |
|
2780 |
d->requestUpdate(); |
|
2781 |
} |
|
2782 |
||
2783 |
/*! |
|
2784 |
Writes any unsaved changes to permanent storage, and reloads any |
|
2785 |
settings that have been changed in the meantime by another |
|
2786 |
application. |
|
2787 |
||
2788 |
This function is called automatically from QSettings's destructor and |
|
2789 |
by the event loop at regular intervals, so you normally don't need to |
|
2790 |
call it yourself. |
|
2791 |
||
2792 |
\sa status() |
|
2793 |
*/ |
|
2794 |
void QSettings::sync() |
|
2795 |
{ |
|
2796 |
Q_D(QSettings); |
|
2797 |
d->sync(); |
|
2798 |
} |
|
2799 |
||
2800 |
/*! |
|
2801 |
Returns the path where settings written using this QSettings |
|
2802 |
object are stored. |
|
2803 |
||
2804 |
On Windows, if the format is QSettings::NativeFormat, the return value |
|
2805 |
is a system registry path, not a file path. |
|
2806 |
||
2807 |
\sa isWritable(), format() |
|
2808 |
*/ |
|
2809 |
QString QSettings::fileName() const |
|
2810 |
{ |
|
2811 |
Q_D(const QSettings); |
|
2812 |
return d->fileName(); |
|
2813 |
} |
|
2814 |
||
2815 |
/*! |
|
2816 |
\since 4.4 |
|
2817 |
||
2818 |
Returns the format used for storing the settings. |
|
2819 |
||
2820 |
\sa defaultFormat(), fileName(), scope(), organizationName(), applicationName() |
|
2821 |
*/ |
|
2822 |
QSettings::Format QSettings::format() const |
|
2823 |
{ |
|
2824 |
Q_D(const QSettings); |
|
2825 |
return d->format; |
|
2826 |
} |
|
2827 |
||
2828 |
/*! |
|
2829 |
\since 4.4 |
|
2830 |
||
2831 |
Returns the scope used for storing the settings. |
|
2832 |
||
2833 |
\sa format(), organizationName(), applicationName() |
|
2834 |
*/ |
|
2835 |
QSettings::Scope QSettings::scope() const |
|
2836 |
{ |
|
2837 |
Q_D(const QSettings); |
|
2838 |
return d->scope; |
|
2839 |
} |
|
2840 |
||
2841 |
/*! |
|
2842 |
\since 4.4 |
|
2843 |
||
2844 |
Returns the organization name used for storing the settings. |
|
2845 |
||
2846 |
\sa QCoreApplication::organizationName(), format(), scope(), applicationName() |
|
2847 |
*/ |
|
2848 |
QString QSettings::organizationName() const |
|
2849 |
{ |
|
2850 |
Q_D(const QSettings); |
|
2851 |
return d->organizationName; |
|
2852 |
} |
|
2853 |
||
2854 |
/*! |
|
2855 |
\since 4.4 |
|
2856 |
||
2857 |
Returns the application name used for storing the settings. |
|
2858 |
||
2859 |
\sa QCoreApplication::applicationName(), format(), scope(), organizationName() |
|
2860 |
*/ |
|
2861 |
QString QSettings::applicationName() const |
|
2862 |
{ |
|
2863 |
Q_D(const QSettings); |
|
2864 |
return d->applicationName; |
|
2865 |
} |
|
2866 |
||
2867 |
#ifndef QT_NO_TEXTCODEC |
|
2868 |
||
2869 |
/*! |
|
2870 |
\since 4.5 |
|
2871 |
||
2872 |
Sets the codec for accessing INI files (including \c .conf files on Unix) |
|
2873 |
to \a codec. The codec is used for decoding any data that is read from |
|
2874 |
the INI file, and for encoding any data that is written to the file. By |
|
2875 |
default, no codec is used, and non-ASCII characters are encoded using |
|
2876 |
standard INI escape sequences. |
|
2877 |
||
2878 |
\warning The codec must be set immediately after creating the QSettings |
|
2879 |
object, before accessing any data. |
|
2880 |
||
2881 |
\sa iniCodec() |
|
2882 |
*/ |
|
2883 |
void QSettings::setIniCodec(QTextCodec *codec) |
|
2884 |
{ |
|
2885 |
Q_D(QSettings); |
|
2886 |
d->iniCodec = codec; |
|
2887 |
} |
|
2888 |
||
2889 |
/*! |
|
2890 |
\since 4.5 |
|
2891 |
\overload |
|
2892 |
||
2893 |
Sets the codec for accessing INI files (including \c .conf files on Unix) |
|
2894 |
to the QTextCodec for the encoding specified by \a codecName. Common |
|
2895 |
values for \c codecName include "ISO 8859-1", "UTF-8", and "UTF-16". |
|
2896 |
If the encoding isn't recognized, nothing happens. |
|
2897 |
||
2898 |
\sa QTextCodec::codecForName() |
|
2899 |
*/ |
|
2900 |
void QSettings::setIniCodec(const char *codecName) |
|
2901 |
{ |
|
2902 |
Q_D(QSettings); |
|
2903 |
if (QTextCodec *codec = QTextCodec::codecForName(codecName)) |
|
2904 |
d->iniCodec = codec; |
|
2905 |
} |
|
2906 |
||
2907 |
/*! |
|
2908 |
\since 4.5 |
|
2909 |
||
2910 |
Returns the codec that is used for accessing INI files. By default, |
|
2911 |
no codec is used, so a null pointer is returned. |
|
2912 |
*/ |
|
2913 |
||
2914 |
QTextCodec *QSettings::iniCodec() const |
|
2915 |
{ |
|
2916 |
Q_D(const QSettings); |
|
2917 |
return d->iniCodec; |
|
2918 |
} |
|
2919 |
||
2920 |
#endif // QT_NO_TEXTCODEC |
|
2921 |
||
2922 |
/*! |
|
2923 |
Returns a status code indicating the first error that was met by |
|
2924 |
QSettings, or QSettings::NoError if no error occurred. |
|
2925 |
||
2926 |
Be aware that QSettings delays performing some operations. For this |
|
2927 |
reason, you might want to call sync() to ensure that the data stored |
|
2928 |
in QSettings is written to disk before calling status(). |
|
2929 |
||
2930 |
\sa sync() |
|
2931 |
*/ |
|
2932 |
QSettings::Status QSettings::status() const |
|
2933 |
{ |
|
2934 |
Q_D(const QSettings); |
|
2935 |
return d->status; |
|
2936 |
} |
|
2937 |
||
2938 |
/*! |
|
2939 |
Appends \a prefix to the current group. |
|
2940 |
||
2941 |
The current group is automatically prepended to all keys |
|
2942 |
specified to QSettings. In addition, query functions such as |
|
2943 |
childGroups(), childKeys(), and allKeys() are based on the group. |
|
2944 |
By default, no group is set. |
|
2945 |
||
2946 |
Groups are useful to avoid typing in the same setting paths over |
|
2947 |
and over. For example: |
|
2948 |
||
2949 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 13 |
|
2950 |
||
2951 |
This will set the value of three settings: |
|
2952 |
||
2953 |
\list |
|
2954 |
\o \c mainwindow/size |
|
2955 |
\o \c mainwindow/fullScreen |
|
2956 |
\o \c outputpanel/visible |
|
2957 |
\endlist |
|
2958 |
||
2959 |
Call endGroup() to reset the current group to what it was before |
|
2960 |
the corresponding beginGroup() call. Groups can be nested. |
|
2961 |
||
2962 |
\sa endGroup(), group() |
|
2963 |
*/ |
|
2964 |
void QSettings::beginGroup(const QString &prefix) |
|
2965 |
{ |
|
2966 |
Q_D(QSettings); |
|
2967 |
d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix))); |
|
2968 |
} |
|
2969 |
||
2970 |
/*! |
|
2971 |
Resets the group to what it was before the corresponding |
|
2972 |
beginGroup() call. |
|
2973 |
||
2974 |
Example: |
|
2975 |
||
2976 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 14 |
|
2977 |
||
2978 |
\sa beginGroup(), group() |
|
2979 |
*/ |
|
2980 |
void QSettings::endGroup() |
|
2981 |
{ |
|
2982 |
Q_D(QSettings); |
|
2983 |
if (d->groupStack.isEmpty()) { |
|
2984 |
qWarning("QSettings::endGroup: No matching beginGroup()"); |
|
2985 |
return; |
|
2986 |
} |
|
2987 |
||
2988 |
QSettingsGroup group = d->groupStack.pop(); |
|
2989 |
int len = group.toString().size(); |
|
2990 |
if (len > 0) |
|
2991 |
d->groupPrefix.truncate(d->groupPrefix.size() - (len + 1)); |
|
2992 |
||
2993 |
if (group.isArray()) |
|
2994 |
qWarning("QSettings::endGroup: Expected endArray() instead"); |
|
2995 |
} |
|
2996 |
||
2997 |
/*! |
|
2998 |
Returns the current group. |
|
2999 |
||
3000 |
\sa beginGroup(), endGroup() |
|
3001 |
*/ |
|
3002 |
QString QSettings::group() const |
|
3003 |
{ |
|
3004 |
Q_D(const QSettings); |
|
3005 |
return d->groupPrefix.left(d->groupPrefix.size() - 1); |
|
3006 |
} |
|
3007 |
||
3008 |
/*! |
|
3009 |
Adds \a prefix to the current group and starts reading from an |
|
3010 |
array. Returns the size of the array. |
|
3011 |
||
3012 |
Example: |
|
3013 |
||
3014 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 15 |
|
3015 |
||
3016 |
Use beginWriteArray() to write the array in the first place. |
|
3017 |
||
3018 |
\sa beginWriteArray(), endArray(), setArrayIndex() |
|
3019 |
*/ |
|
3020 |
int QSettings::beginReadArray(const QString &prefix) |
|
3021 |
{ |
|
3022 |
Q_D(QSettings); |
|
3023 |
d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix), false)); |
|
3024 |
return value(QLatin1String("size")).toInt(); |
|
3025 |
} |
|
3026 |
||
3027 |
/*! |
|
3028 |
Adds \a prefix to the current group and starts writing an array |
|
3029 |
of size \a size. If \a size is -1 (the default), it is automatically |
|
3030 |
determined based on the indexes of the entries written. |
|
3031 |
||
3032 |
If you have many occurrences of a certain set of keys, you can |
|
3033 |
use arrays to make your life easier. For example, let's suppose |
|
3034 |
that you want to save a variable-length list of user names and |
|
3035 |
passwords. You could then write: |
|
3036 |
||
3037 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 16 |
|
3038 |
||
3039 |
The generated keys will have the form |
|
3040 |
||
3041 |
\list |
|
3042 |
\o \c logins/size |
|
3043 |
\o \c logins/1/userName |
|
3044 |
\o \c logins/1/password |
|
3045 |
\o \c logins/2/userName |
|
3046 |
\o \c logins/2/password |
|
3047 |
\o \c logins/3/userName |
|
3048 |
\o \c logins/3/password |
|
3049 |
\o ... |
|
3050 |
\endlist |
|
3051 |
||
3052 |
To read back an array, use beginReadArray(). |
|
3053 |
||
3054 |
\sa beginReadArray(), endArray(), setArrayIndex() |
|
3055 |
*/ |
|
3056 |
void QSettings::beginWriteArray(const QString &prefix, int size) |
|
3057 |
{ |
|
3058 |
Q_D(QSettings); |
|
3059 |
d->beginGroupOrArray(QSettingsGroup(d->normalizedKey(prefix), size < 0)); |
|
3060 |
||
3061 |
if (size < 0) |
|
3062 |
remove(QLatin1String("size")); |
|
3063 |
else |
|
3064 |
setValue(QLatin1String("size"), size); |
|
3065 |
} |
|
3066 |
||
3067 |
/*! |
|
3068 |
Closes the array that was started using beginReadArray() or |
|
3069 |
beginWriteArray(). |
|
3070 |
||
3071 |
\sa beginReadArray(), beginWriteArray() |
|
3072 |
*/ |
|
3073 |
void QSettings::endArray() |
|
3074 |
{ |
|
3075 |
Q_D(QSettings); |
|
3076 |
if (d->groupStack.isEmpty()) { |
|
3077 |
qWarning("QSettings::endArray: No matching beginArray()"); |
|
3078 |
return; |
|
3079 |
} |
|
3080 |
||
3081 |
QSettingsGroup group = d->groupStack.top(); |
|
3082 |
int len = group.toString().size(); |
|
3083 |
d->groupStack.pop(); |
|
3084 |
if (len > 0) |
|
3085 |
d->groupPrefix.truncate(d->groupPrefix.size() - (len + 1)); |
|
3086 |
||
3087 |
if (group.arraySizeGuess() != -1) |
|
3088 |
setValue(group.name() + QLatin1String("/size"), group.arraySizeGuess()); |
|
3089 |
||
3090 |
if (!group.isArray()) |
|
3091 |
qWarning("QSettings::endArray: Expected endGroup() instead"); |
|
3092 |
} |
|
3093 |
||
3094 |
/*! |
|
3095 |
Sets the current array index to \a i. Calls to functions such as |
|
3096 |
setValue(), value(), remove(), and contains() will operate on the |
|
3097 |
array entry at that index. |
|
3098 |
||
3099 |
You must call beginReadArray() or beginWriteArray() before you |
|
3100 |
can call this function. |
|
3101 |
*/ |
|
3102 |
void QSettings::setArrayIndex(int i) |
|
3103 |
{ |
|
3104 |
Q_D(QSettings); |
|
3105 |
if (d->groupStack.isEmpty() || !d->groupStack.top().isArray()) { |
|
3106 |
qWarning("QSettings::setArrayIndex: Missing beginArray()"); |
|
3107 |
return; |
|
3108 |
} |
|
3109 |
||
3110 |
QSettingsGroup &top = d->groupStack.top(); |
|
3111 |
int len = top.toString().size(); |
|
3112 |
top.setArrayIndex(qMax(i, 0)); |
|
3113 |
d->groupPrefix.replace(d->groupPrefix.size() - len - 1, len, top.toString()); |
|
3114 |
} |
|
3115 |
||
3116 |
/*! |
|
3117 |
Returns a list of all keys, including subkeys, that can be read |
|
3118 |
using the QSettings object. |
|
3119 |
||
3120 |
Example: |
|
3121 |
||
3122 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 17 |
|
3123 |
||
3124 |
If a group is set using beginGroup(), only the keys in the group |
|
3125 |
are returned, without the group prefix: |
|
3126 |
||
3127 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 18 |
|
3128 |
||
3129 |
\sa childGroups(), childKeys() |
|
3130 |
*/ |
|
3131 |
QStringList QSettings::allKeys() const |
|
3132 |
{ |
|
3133 |
Q_D(const QSettings); |
|
3134 |
return d->children(d->groupPrefix, QSettingsPrivate::AllKeys); |
|
3135 |
} |
|
3136 |
||
3137 |
/*! |
|
3138 |
Returns a list of all top-level keys that can be read using the |
|
3139 |
QSettings object. |
|
3140 |
||
3141 |
Example: |
|
3142 |
||
3143 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 19 |
|
3144 |
||
3145 |
If a group is set using beginGroup(), the top-level keys in that |
|
3146 |
group are returned, without the group prefix: |
|
3147 |
||
3148 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 20 |
|
3149 |
||
3150 |
You can navigate through the entire setting hierarchy using |
|
3151 |
childKeys() and childGroups() recursively. |
|
3152 |
||
3153 |
\sa childGroups(), allKeys() |
|
3154 |
*/ |
|
3155 |
QStringList QSettings::childKeys() const |
|
3156 |
{ |
|
3157 |
Q_D(const QSettings); |
|
3158 |
return d->children(d->groupPrefix, QSettingsPrivate::ChildKeys); |
|
3159 |
} |
|
3160 |
||
3161 |
/*! |
|
3162 |
Returns a list of all key top-level groups that contain keys that |
|
3163 |
can be read using the QSettings object. |
|
3164 |
||
3165 |
Example: |
|
3166 |
||
3167 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 21 |
|
3168 |
||
3169 |
If a group is set using beginGroup(), the first-level keys in |
|
3170 |
that group are returned, without the group prefix. |
|
3171 |
||
3172 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 22 |
|
3173 |
||
3174 |
You can navigate through the entire setting hierarchy using |
|
3175 |
childKeys() and childGroups() recursively. |
|
3176 |
||
3177 |
\sa childKeys(), allKeys() |
|
3178 |
*/ |
|
3179 |
QStringList QSettings::childGroups() const |
|
3180 |
{ |
|
3181 |
Q_D(const QSettings); |
|
3182 |
return d->children(d->groupPrefix, QSettingsPrivate::ChildGroups); |
|
3183 |
} |
|
3184 |
||
3185 |
/*! |
|
3186 |
Returns true if settings can be written using this QSettings |
|
3187 |
object; returns false otherwise. |
|
3188 |
||
3189 |
One reason why isWritable() might return false is if |
|
3190 |
QSettings operates on a read-only file. |
|
3191 |
||
3192 |
\warning This function is not perfectly reliable, because the |
|
3193 |
file permissions can change at any time. |
|
3194 |
||
3195 |
\sa fileName(), status(), sync() |
|
3196 |
*/ |
|
3197 |
bool QSettings::isWritable() const |
|
3198 |
{ |
|
3199 |
Q_D(const QSettings); |
|
3200 |
return d->isWritable(); |
|
3201 |
} |
|
3202 |
||
3203 |
/*! |
|
3204 |
||
3205 |
Sets the value of setting \a key to \a value. If the \a key already |
|
3206 |
exists, the previous value is overwritten. |
|
3207 |
||
3208 |
Note that the Windows registry and INI files use case-insensitive |
|
3209 |
keys, whereas the Carbon Preferences API on Mac OS X uses |
|
3210 |
case-sensitive keys. To avoid portability problems, see the |
|
3211 |
\l{Section and Key Syntax} rules. |
|
3212 |
||
3213 |
Example: |
|
3214 |
||
3215 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 23 |
|
3216 |
||
3217 |
\sa value(), remove(), contains() |
|
3218 |
*/ |
|
3219 |
void QSettings::setValue(const QString &key, const QVariant &value) |
|
3220 |
{ |
|
3221 |
Q_D(QSettings); |
|
3222 |
QString k = d->actualKey(key); |
|
3223 |
d->set(k, value); |
|
3224 |
d->requestUpdate(); |
|
3225 |
} |
|
3226 |
||
3227 |
/*! |
|
3228 |
Removes the setting \a key and any sub-settings of \a key. |
|
3229 |
||
3230 |
Example: |
|
3231 |
||
3232 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 24 |
|
3233 |
||
3234 |
Be aware that if one of the fallback locations contains a setting |
|
3235 |
with the same key, that setting will be visible after calling |
|
3236 |
remove(). |
|
3237 |
||
3238 |
If \a key is an empty string, all keys in the current group() are |
|
3239 |
removed. For example: |
|
3240 |
||
3241 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 25 |
|
3242 |
||
3243 |
Note that the Windows registry and INI files use case-insensitive |
|
3244 |
keys, whereas the Carbon Preferences API on Mac OS X uses |
|
3245 |
case-sensitive keys. To avoid portability problems, see the |
|
3246 |
\l{Section and Key Syntax} rules. |
|
3247 |
||
3248 |
\sa setValue(), value(), contains() |
|
3249 |
*/ |
|
3250 |
void QSettings::remove(const QString &key) |
|
3251 |
{ |
|
3252 |
Q_D(QSettings); |
|
3253 |
/* |
|
3254 |
We cannot use actualKey(), because remove() supports empty |
|
3255 |
keys. The code is also tricky because of slash handling. |
|
3256 |
*/ |
|
3257 |
QString theKey = d->normalizedKey(key); |
|
3258 |
if (theKey.isEmpty()) |
|
3259 |
theKey = group(); |
|
3260 |
else |
|
3261 |
theKey.prepend(d->groupPrefix); |
|
3262 |
||
3263 |
if (theKey.isEmpty()) { |
|
3264 |
d->clear(); |
|
3265 |
} else { |
|
3266 |
d->remove(theKey); |
|
3267 |
} |
|
3268 |
d->requestUpdate(); |
|
3269 |
} |
|
3270 |
||
3271 |
/*! |
|
3272 |
Returns true if there exists a setting called \a key; returns |
|
3273 |
false otherwise. |
|
3274 |
||
3275 |
If a group is set using beginGroup(), \a key is taken to be |
|
3276 |
relative to that group. |
|
3277 |
||
3278 |
Note that the Windows registry and INI files use case-insensitive |
|
3279 |
keys, whereas the Carbon Preferences API on Mac OS X uses |
|
3280 |
case-sensitive keys. To avoid portability problems, see the |
|
3281 |
\l{Section and Key Syntax} rules. |
|
3282 |
||
3283 |
\sa value(), setValue() |
|
3284 |
*/ |
|
3285 |
bool QSettings::contains(const QString &key) const |
|
3286 |
{ |
|
3287 |
Q_D(const QSettings); |
|
3288 |
QString k = d->actualKey(key); |
|
3289 |
return d->get(k, 0); |
|
3290 |
} |
|
3291 |
||
3292 |
/*! |
|
3293 |
Sets whether fallbacks are enabled to \a b. |
|
3294 |
||
3295 |
By default, fallbacks are enabled. |
|
3296 |
||
3297 |
\sa fallbacksEnabled() |
|
3298 |
*/ |
|
3299 |
void QSettings::setFallbacksEnabled(bool b) |
|
3300 |
{ |
|
3301 |
Q_D(QSettings); |
|
3302 |
d->fallbacks = !!b; |
|
3303 |
} |
|
3304 |
||
3305 |
/*! |
|
3306 |
Returns true if fallbacks are enabled; returns false otherwise. |
|
3307 |
||
3308 |
By default, fallbacks are enabled. |
|
3309 |
||
3310 |
\sa setFallbacksEnabled() |
|
3311 |
*/ |
|
3312 |
bool QSettings::fallbacksEnabled() const |
|
3313 |
{ |
|
3314 |
Q_D(const QSettings); |
|
3315 |
return d->fallbacks; |
|
3316 |
} |
|
3317 |
||
3318 |
#ifndef QT_NO_QOBJECT |
|
3319 |
/*! |
|
3320 |
\reimp |
|
3321 |
*/ |
|
3322 |
bool QSettings::event(QEvent *event) |
|
3323 |
{ |
|
3324 |
Q_D(QSettings); |
|
3325 |
if (event->type() == QEvent::UpdateRequest) { |
|
3326 |
d->update(); |
|
3327 |
return true; |
|
3328 |
} |
|
3329 |
return QObject::event(event); |
|
3330 |
} |
|
3331 |
#endif |
|
3332 |
||
3333 |
/*! |
|
3334 |
Returns the value for setting \a key. If the setting doesn't |
|
3335 |
exist, returns \a defaultValue. |
|
3336 |
||
3337 |
If no default value is specified, a default QVariant is |
|
3338 |
returned. |
|
3339 |
||
3340 |
Note that the Windows registry and INI files use case-insensitive |
|
3341 |
keys, whereas the Carbon Preferences API on Mac OS X uses |
|
3342 |
case-sensitive keys. To avoid portability problems, see the |
|
3343 |
\l{Section and Key Syntax} rules. |
|
3344 |
||
3345 |
Example: |
|
3346 |
||
3347 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 26 |
|
3348 |
||
3349 |
\sa setValue(), contains(), remove() |
|
3350 |
*/ |
|
3351 |
QVariant QSettings::value(const QString &key, const QVariant &defaultValue) const |
|
3352 |
{ |
|
3353 |
Q_D(const QSettings); |
|
3354 |
QVariant result = defaultValue; |
|
3355 |
QString k = d->actualKey(key); |
|
3356 |
d->get(k, &result); |
|
3357 |
return result; |
|
3358 |
} |
|
3359 |
||
3360 |
/*! |
|
3361 |
\since 4.4 |
|
3362 |
||
3363 |
Sets the default file format to the given \a format, which is used |
|
3364 |
for storing settings for the QSettings(QObject *) constructor. |
|
3365 |
||
3366 |
If no default format is set, QSettings::NativeFormat is used. See |
|
3367 |
the documentation for the QSettings constructor you are using to |
|
3368 |
see if that constructor will ignore this function. |
|
3369 |
||
3370 |
\sa format() |
|
3371 |
*/ |
|
3372 |
void QSettings::setDefaultFormat(Format format) |
|
3373 |
{ |
|
3374 |
globalDefaultFormat = format; |
|
3375 |
} |
|
3376 |
||
3377 |
/*! |
|
3378 |
\since 4.4 |
|
3379 |
||
3380 |
Returns default file format used for storing settings for the QSettings(QObject *) constructor. |
|
3381 |
If no default format is set, QSettings::NativeFormat is used. |
|
3382 |
||
3383 |
\sa format() |
|
3384 |
*/ |
|
3385 |
QSettings::Format QSettings::defaultFormat() |
|
3386 |
{ |
|
3387 |
return globalDefaultFormat; |
|
3388 |
} |
|
3389 |
||
3390 |
/*! |
|
3391 |
\obsolete |
|
3392 |
||
3393 |
Use setPath() instead. |
|
3394 |
||
3395 |
\oldcode |
|
3396 |
setSystemIniPath(path); |
|
3397 |
\newcode |
|
3398 |
setPath(QSettings::NativeFormat, QSettings::SystemScope, path); |
|
3399 |
setPath(QSettings::IniFormat, QSettings::SystemScope, path); |
|
3400 |
\endcode |
|
3401 |
*/ |
|
3402 |
void QSettings::setSystemIniPath(const QString &dir) |
|
3403 |
{ |
|
3404 |
setPath(IniFormat, SystemScope, dir); |
|
3405 |
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC) |
|
3406 |
setPath(NativeFormat, SystemScope, dir); |
|
3407 |
#endif |
|
3408 |
} |
|
3409 |
||
3410 |
/*! |
|
3411 |
\obsolete |
|
3412 |
||
3413 |
Use setPath() instead. |
|
3414 |
*/ |
|
3415 |
||
3416 |
void QSettings::setUserIniPath(const QString &dir) |
|
3417 |
{ |
|
3418 |
setPath(IniFormat, UserScope, dir); |
|
3419 |
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC) |
|
3420 |
setPath(NativeFormat, UserScope, dir); |
|
3421 |
#endif |
|
3422 |
} |
|
3423 |
||
3424 |
/*! |
|
3425 |
\since 4.1 |
|
3426 |
||
3427 |
Sets the path used for storing settings for the given \a format |
|
3428 |
and \a scope, to \a path. The \a format can be a custom format. |
|
3429 |
||
3430 |
The table below summarizes the default values: |
|
3431 |
||
3432 |
\table |
|
3433 |
\header \o Platform \o Format \o Scope \o Path |
|
3434 |
\row \o{1,2} Windows \o{1,2} IniFormat \o UserScope \o \c %APPDATA% |
|
3435 |
\row \o SystemScope \o \c %COMMON_APPDATA% |
|
3436 |
\row \o{1,2} Unix \o{1,2} NativeFormat, IniFormat \o UserScope \o \c $HOME/.config |
|
3437 |
\row \o SystemScope \o \c /etc/xdg |
|
3438 |
\row \o{1,2} Qt for Embedded Linux \o{1,2} NativeFormat, IniFormat \o UserScope \o \c $HOME/Settings |
|
3439 |
\row \o SystemScope \o \c /etc/xdg |
|
3440 |
\row \o{1,2} Mac OS X \o{1,2} IniFormat \o UserScope \o \c $HOME/.config |
|
3441 |
\row \o SystemScope \o \c /etc/xdg |
|
3442 |
\endtable |
|
3443 |
||
3444 |
The default UserScope paths on Unix and Mac OS X (\c |
|
3445 |
$HOME/.config or $HOME/Settings) can be overridden by the user by setting the |
|
3446 |
\c XDG_CONFIG_HOME environment variable. The default SystemScope |
|
3447 |
paths on Unix and Mac OS X (\c /etc/xdg) can be overridden when |
|
3448 |
building the Qt library using the \c configure script's \c |
|
3449 |
--sysconfdir flag (see QLibraryInfo for details). |
|
3450 |
||
3451 |
Setting the NativeFormat paths on Windows and Mac OS X has no |
|
3452 |
effect. |
|
3453 |
||
3454 |
\warning This function doesn't affect existing QSettings objects. |
|
3455 |
||
3456 |
\sa registerFormat() |
|
3457 |
*/ |
|
3458 |
void QSettings::setPath(Format format, Scope scope, const QString &path) |
|
3459 |
{ |
|
3460 |
QMutexLocker locker(globalMutex()); |
|
3461 |
PathHash *pathHash = pathHashFunc(); |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
3462 |
if (pathHash->isEmpty()) |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
3463 |
initDefaultPaths(&locker); |
0 | 3464 |
pathHash->insert(pathHashKey(format, scope), path + QDir::separator()); |
3465 |
} |
|
3466 |
||
3467 |
/*! |
|
3468 |
\typedef QSettings::SettingsMap |
|
3469 |
||
3470 |
Typedef for QMap<QString, QVariant>. |
|
3471 |
||
3472 |
\sa registerFormat() |
|
3473 |
*/ |
|
3474 |
||
3475 |
/*! |
|
3476 |
\typedef QSettings::ReadFunc |
|
3477 |
||
3478 |
Typedef for a pointer to a function with the following signature: |
|
3479 |
||
3480 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 27 |
|
3481 |
||
3482 |
\c ReadFunc is used in \c registerFormat() as a pointer to a function |
|
3483 |
that reads a set of key/value pairs. \c ReadFunc should read all the |
|
3484 |
options in one pass, and return all the settings in the \c SettingsMap |
|
3485 |
container, which is initially empty. |
|
3486 |
||
3487 |
\sa WriteFunc, registerFormat() |
|
3488 |
*/ |
|
3489 |
||
3490 |
/*! |
|
3491 |
\typedef QSettings::WriteFunc |
|
3492 |
||
3493 |
Typedef for a pointer to a function with the following signature: |
|
3494 |
||
3495 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 28 |
|
3496 |
||
3497 |
\c WriteFunc is used in \c registerFormat() as a pointer to a function |
|
3498 |
that writes a set of key/value pairs. \c WriteFunc is only called once, |
|
3499 |
so you need to output the settings in one go. |
|
3500 |
||
3501 |
\sa ReadFunc, registerFormat() |
|
3502 |
*/ |
|
3503 |
||
3504 |
/*! |
|
3505 |
\since 4.1 |
|
3506 |
\threadsafe |
|
3507 |
||
3508 |
Registers a custom storage format. On success, returns a special |
|
3509 |
Format value that can then be passed to the QSettings constuctor. |
|
3510 |
On failure, returns InvalidFormat. |
|
3511 |
||
3512 |
The \a extension is the file |
|
3513 |
extension associated to the format (without the '.'). |
|
3514 |
||
3515 |
The \a readFunc and \a writeFunc parameters are pointers to |
|
3516 |
functions that read and write a set of key/value pairs. The |
|
3517 |
QIODevice parameter to the read and write functions is always |
|
3518 |
opened in binary mode (i.e., without the QIODevice::Text flag). |
|
3519 |
||
3520 |
The \a caseSensitivity parameter specifies whether keys are case |
|
3521 |
sensitive or not. This makes a difference when looking up values |
|
3522 |
using QSettings. The default is case sensitive. |
|
3523 |
||
3524 |
By default, if you use one of the constructors that work in terms |
|
3525 |
of an organization name and an application name, the file system |
|
3526 |
locations used are the same as for IniFormat. Use setPath() to |
|
3527 |
specify other locations. |
|
3528 |
||
3529 |
Example: |
|
3530 |
||
3531 |
\snippet doc/src/snippets/code/src_corelib_io_qsettings.cpp 29 |
|
3532 |
||
3533 |
\sa setPath() |
|
3534 |
*/ |
|
3535 |
QSettings::Format QSettings::registerFormat(const QString &extension, ReadFunc readFunc, |
|
3536 |
WriteFunc writeFunc, |
|
3537 |
Qt::CaseSensitivity caseSensitivity) |
|
3538 |
{ |
|
3539 |
#ifdef QT_QSETTINGS_ALWAYS_CASE_SENSITIVE_AND_FORGET_ORIGINAL_KEY_ORDER |
|
3540 |
Q_ASSERT(caseSensitivity == Qt::CaseSensitive); |
|
3541 |
#endif |
|
3542 |
||
3543 |
QMutexLocker locker(globalMutex()); |
|
3544 |
CustomFormatVector *customFormatVector = customFormatVectorFunc(); |
|
3545 |
int index = customFormatVector->size(); |
|
3546 |
if (index == 16) // the QSettings::Format enum has room for 16 custom formats |
|
3547 |
return QSettings::InvalidFormat; |
|
3548 |
||
3549 |
QConfFileCustomFormat info; |
|
3550 |
info.extension = QLatin1Char('.'); |
|
3551 |
info.extension += extension; |
|
3552 |
info.readFunc = readFunc; |
|
3553 |
info.writeFunc = writeFunc; |
|
3554 |
info.caseSensitivity = caseSensitivity; |
|
3555 |
customFormatVector->append(info); |
|
3556 |
||
3557 |
return QSettings::Format((int)QSettings::CustomFormat1 + index); |
|
3558 |
} |
|
3559 |
||
3560 |
#ifdef QT3_SUPPORT |
|
3561 |
void QSettings::setPath_helper(Scope scope, const QString &organization, const QString &application) |
|
3562 |
{ |
|
3563 |
Q_D(QSettings); |
|
3564 |
if (d->pendingChanges) |
|
3565 |
d->flush(); |
|
3566 |
QSettingsPrivate *oldPriv = d; |
|
3567 |
QSettingsPrivate *newPriv = QSettingsPrivate::create(oldPriv->format, scope, organization, application); |
|
3568 |
static_cast<QObjectPrivate &>(*newPriv) = static_cast<QObjectPrivate &>(*oldPriv); // copy the QObject stuff over (hack) |
|
3569 |
d_ptr.reset(newPriv); |
|
3570 |
} |
|
3571 |
||
3572 |
/*! \fn bool QSettings::writeEntry(const QString &key, bool value) |
|
3573 |
||
3574 |
Sets the value of setting \a key to \a value. |
|
3575 |
||
3576 |
Use setValue() instead. |
|
3577 |
*/ |
|
3578 |
||
3579 |
/*! \fn bool QSettings::writeEntry(const QString &key, double value) |
|
3580 |
||
3581 |
\overload |
|
3582 |
*/ |
|
3583 |
||
3584 |
/*! \fn bool QSettings::writeEntry(const QString &key, int value) |
|
3585 |
||
3586 |
\overload |
|
3587 |
*/ |
|
3588 |
||
3589 |
/*! \fn bool QSettings::writeEntry(const QString &key, const char *value) |
|
3590 |
||
3591 |
\overload |
|
3592 |
*/ |
|
3593 |
||
3594 |
/*! \fn bool QSettings::writeEntry(const QString &key, const QString &value) |
|
3595 |
||
3596 |
\overload |
|
3597 |
*/ |
|
3598 |
||
3599 |
/*! \fn bool QSettings::writeEntry(const QString &key, const QStringList &value) |
|
3600 |
||
3601 |
\overload |
|
3602 |
*/ |
|
3603 |
||
3604 |
/*! \fn bool QSettings::writeEntry(const QString &key, const QStringList &value, QChar separator) |
|
3605 |
||
3606 |
\overload |
|
3607 |
||
3608 |
Use setValue(\a key, \a value) instead. You don't need \a separator. |
|
3609 |
*/ |
|
3610 |
||
3611 |
/*! \fn QStringList QSettings::readListEntry(const QString &key, bool *ok = 0) |
|
3612 |
||
3613 |
Returns the value of setting \a key converted to a QStringList. |
|
3614 |
||
3615 |
If \a ok is not 0, *\a{ok} is set to true if the key exists, |
|
3616 |
otherwise *\a{ok} is set to false. |
|
3617 |
||
3618 |
Use value() instead. |
|
3619 |
||
3620 |
\oldcode |
|
3621 |
bool ok; |
|
3622 |
QStringList list = settings.readListEntry("recentFiles", &ok); |
|
3623 |
\newcode |
|
3624 |
bool ok = settings.contains("recentFiles"); |
|
3625 |
QStringList list = settings.value("recentFiles").toStringList(); |
|
3626 |
\endcode |
|
3627 |
*/ |
|
3628 |
||
3629 |
/*! \fn QStringList QSettings::readListEntry(const QString &key, QChar separator, bool *ok) |
|
3630 |
||
3631 |
Returns the value of setting \a key converted to a QStringList. |
|
3632 |
\a separator is ignored. |
|
3633 |
||
3634 |
If \a ok is not 0, *\a{ok} is set to true if the key exists, |
|
3635 |
otherwise *\a{ok} is set to false. |
|
3636 |
||
3637 |
Use value() instead. |
|
3638 |
||
3639 |
\oldcode |
|
3640 |
bool ok; |
|
3641 |
QStringList list = settings.readListEntry("recentFiles", ":", &ok); |
|
3642 |
\newcode |
|
3643 |
bool ok = settings.contains("recentFiles"); |
|
3644 |
QStringList list = settings.value("recentFiles").toStringList(); |
|
3645 |
\endcode |
|
3646 |
*/ |
|
3647 |
||
3648 |
/*! \fn QString QSettings::readEntry(const QString &key, const QString &defaultValue, bool *ok) |
|
3649 |
||
3650 |
Returns the value for setting \a key converted to a QString. If |
|
3651 |
the setting doesn't exist, returns \a defaultValue. |
|
3652 |
||
3653 |
If \a ok is not 0, *\a{ok} is set to true if the key exists, |
|
3654 |
otherwise *\a{ok} is set to false. |
|
3655 |
||
3656 |
Use value() instead. |
|
3657 |
||
3658 |
\oldcode |
|
3659 |
bool ok; |
|
3660 |
QString str = settings.readEntry("userName", "administrator", &ok); |
|
3661 |
\newcode |
|
3662 |
bool ok = settings.contains("userName"); |
|
3663 |
QString str = settings.value("userName", "administrator").toString(); |
|
3664 |
\endcode |
|
3665 |
*/ |
|
3666 |
||
3667 |
/*! \fn int QSettings::readNumEntry(const QString &key, int defaultValue, bool *ok) |
|
3668 |
||
3669 |
Returns the value for setting \a key converted to an \c int. If |
|
3670 |
the setting doesn't exist, returns \a defaultValue. |
|
3671 |
||
3672 |
If \a ok is not 0, *\a{ok} is set to true if the key exists, |
|
3673 |
otherwise *\a{ok} is set to false. |
|
3674 |
||
3675 |
Use value() instead. |
|
3676 |
||
3677 |
\oldcode |
|
3678 |
bool ok; |
|
3679 |
int max = settings.readNumEntry("maxConnections", 30, &ok); |
|
3680 |
\newcode |
|
3681 |
bool ok = settings.contains("maxConnections"); |
|
3682 |
int max = settings.value("maxConnections", 30).toInt(); |
|
3683 |
\endcode |
|
3684 |
*/ |
|
3685 |
||
3686 |
/*! \fn double QSettings::readDoubleEntry(const QString &key, double defaultValue, bool *ok) |
|
3687 |
||
3688 |
Returns the value for setting \a key converted to a \c double. If |
|
3689 |
the setting doesn't exist, returns \a defaultValue. |
|
3690 |
||
3691 |
If \a ok is not 0, *\a{ok} is set to true if the key exists, |
|
3692 |
otherwise *\a{ok} is set to false. |
|
3693 |
||
3694 |
Use value() instead. |
|
3695 |
||
3696 |
\oldcode |
|
3697 |
bool ok; |
|
3698 |
double pi = settings.readDoubleEntry("pi", 3.141592, &ok); |
|
3699 |
\newcode |
|
3700 |
bool ok = settings.contains("pi"); |
|
3701 |
double pi = settings.value("pi", 3.141592).toDouble(); |
|
3702 |
\endcode |
|
3703 |
*/ |
|
3704 |
||
3705 |
/*! \fn bool QSettings::readBoolEntry(const QString &key, bool defaultValue, bool *ok) |
|
3706 |
||
3707 |
Returns the value for setting \a key converted to a \c bool. If |
|
3708 |
the setting doesn't exist, returns \a defaultValue. |
|
3709 |
||
3710 |
If \a ok is not 0, *\a{ok} is set to true if the key exists, |
|
3711 |
otherwise *\a{ok} is set to false. |
|
3712 |
||
3713 |
Use value() instead. |
|
3714 |
||
3715 |
\oldcode |
|
3716 |
bool ok; |
|
3717 |
bool grid = settings.readBoolEntry("showGrid", true, &ok); |
|
3718 |
\newcode |
|
3719 |
bool ok = settings.contains("showGrid"); |
|
3720 |
bool grid = settings.value("showGrid", true).toBool(); |
|
3721 |
\endcode |
|
3722 |
*/ |
|
3723 |
||
3724 |
/*! \fn bool QSettings::removeEntry(const QString &key) |
|
3725 |
||
3726 |
Use remove() instead. |
|
3727 |
*/ |
|
3728 |
||
3729 |
/*! \enum QSettings::System |
|
3730 |
\compat |
|
3731 |
||
3732 |
\value Unix Unix systems (X11 and Embedded Linux) |
|
3733 |
\value Windows Microsoft Windows systems |
|
3734 |
\value Mac Mac OS X systems |
|
3735 |
||
3736 |
\sa insertSearchPath(), removeSearchPath() |
|
3737 |
*/ |
|
3738 |
||
3739 |
/*! \fn void QSettings::insertSearchPath(System system, const QString &path) |
|
3740 |
||
3741 |
This function is implemented as a no-op. It is provided for |
|
3742 |
source compatibility with Qt 3. The new QSettings class has no |
|
3743 |
concept of "search path". |
|
3744 |
*/ |
|
3745 |
||
3746 |
/*! \fn void QSettings::removeSearchPath(System system, const QString &path) |
|
3747 |
||
3748 |
This function is implemented as a no-op. It is provided for |
|
3749 |
source compatibility with Qt 3. The new QSettings class has no |
|
3750 |
concept of "search path". |
|
3751 |
*/ |
|
3752 |
||
3753 |
/*! \fn void QSettings::setPath(const QString &organization, const QString &application, \ |
|
3754 |
Scope scope) |
|
3755 |
||
3756 |
Specifies the \a organization, \a application, and \a scope to |
|
3757 |
use by the QSettings object. |
|
3758 |
||
3759 |
Use the appropriate constructor instead, with QSettings::UserScope |
|
3760 |
instead of QSettings::User and QSettings::SystemScope instead of |
|
3761 |
QSettings::Global. |
|
3762 |
||
3763 |
\oldcode |
|
3764 |
QSettings settings; |
|
3765 |
settings.setPath("twikimaster.com", "Kanooth", QSettings::Global); |
|
3766 |
\newcode |
|
3767 |
QSettings settings(QSettings::SystemScope, "twikimaster.com", "Kanooth"); |
|
3768 |
\endcode |
|
3769 |
*/ |
|
3770 |
||
3771 |
/*! \fn void QSettings::resetGroup() |
|
3772 |
||
3773 |
Sets the current group to be the empty string. |
|
3774 |
||
3775 |
Use endGroup() instead (possibly multiple times). |
|
3776 |
||
3777 |
\oldcode |
|
3778 |
QSettings settings; |
|
3779 |
settings.beginGroup("mainWindow"); |
|
3780 |
settings.beginGroup("leftPanel"); |
|
3781 |
... |
|
3782 |
settings.resetGroup(); |
|
3783 |
\newcode |
|
3784 |
QSettings settings; |
|
3785 |
settings.beginGroup("mainWindow"); |
|
3786 |
settings.beginGroup("leftPanel"); |
|
3787 |
... |
|
3788 |
settings.endGroup(); |
|
3789 |
settings.endGroup(); |
|
3790 |
\endcode |
|
3791 |
*/ |
|
3792 |
||
3793 |
/*! \fn QStringList QSettings::entryList(const QString &key) const |
|
3794 |
||
3795 |
Returns a list of all sub-keys of \a key. |
|
3796 |
||
3797 |
Use childKeys() instead. |
|
3798 |
||
3799 |
\oldcode |
|
3800 |
QSettings settings; |
|
3801 |
QStringList keys = settings.entryList("cities"); |
|
3802 |
... |
|
3803 |
\newcode |
|
3804 |
QSettings settings; |
|
3805 |
settings.beginGroup("cities"); |
|
3806 |
QStringList keys = settings.childKeys(); |
|
3807 |
... |
|
3808 |
settings.endGroup(); |
|
3809 |
\endcode |
|
3810 |
*/ |
|
3811 |
||
3812 |
/*! \fn QStringList QSettings::subkeyList(const QString &key) const |
|
3813 |
||
3814 |
Returns a list of all sub-keys of \a key. |
|
3815 |
||
3816 |
Use childGroups() instead. |
|
3817 |
||
3818 |
\oldcode |
|
3819 |
QSettings settings; |
|
3820 |
QStringList groups = settings.entryList("cities"); |
|
3821 |
... |
|
3822 |
\newcode |
|
3823 |
QSettings settings; |
|
3824 |
settings.beginGroup("cities"); |
|
3825 |
QStringList groups = settings.childKeys(); |
|
3826 |
... |
|
3827 |
settings.endGroup(); |
|
3828 |
\endcode |
|
3829 |
*/ |
|
3830 |
#endif |
|
3831 |
||
3832 |
QT_END_NAMESPACE |
|
3833 |
||
3834 |
#endif // QT_NO_SETTINGS |