author | eckhart.koppen@nokia.com |
Wed, 31 Mar 2010 11:06:36 +0300 | |
changeset 7 | f7bc934e204c |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@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 "qsettings.h" |
|
43 |
||
44 |
#ifndef QT_NO_SETTINGS |
|
45 |
||
46 |
#include "qsettings_p.h" |
|
47 |
#include "qvector.h" |
|
48 |
#include "qmap.h" |
|
49 |
#include "qt_windows.h" |
|
50 |
#include "qdebug.h" |
|
51 |
||
52 |
QT_BEGIN_NAMESPACE |
|
53 |
||
54 |
/* Keys are stored in QStrings. If the variable name starts with 'u', this is a "user" |
|
55 |
key, ie. "foo/bar/alpha/beta". If the variable name starts with 'r', this is a "registry" |
|
56 |
key, ie. "\foo\bar\alpha\beta". */ |
|
57 |
||
58 |
/******************************************************************************* |
|
59 |
** Some convenience functions |
|
60 |
*/ |
|
61 |
||
62 |
/* |
|
63 |
We don't use KEY_ALL_ACCESS because it gives more rights than what we |
|
64 |
need. See task 199061. |
|
65 |
*/ |
|
66 |
static const REGSAM registryPermissions = KEY_READ | KEY_WRITE; |
|
67 |
||
68 |
static QString keyPath(const QString &rKey) |
|
69 |
{ |
|
70 |
int idx = rKey.lastIndexOf(QLatin1Char('\\')); |
|
71 |
if (idx == -1) |
|
72 |
return QString(); |
|
73 |
return rKey.left(idx + 1); |
|
74 |
} |
|
75 |
||
76 |
static QString keyName(const QString &rKey) |
|
77 |
{ |
|
78 |
int idx = rKey.lastIndexOf(QLatin1Char('\\')); |
|
79 |
||
80 |
QString res; |
|
81 |
if (idx == -1) |
|
82 |
res = rKey; |
|
83 |
else |
|
84 |
res = rKey.mid(idx + 1); |
|
85 |
||
86 |
if (res == QLatin1String("Default") || res == QLatin1String(".")) |
|
87 |
res = QLatin1String(""); |
|
88 |
||
89 |
return res; |
|
90 |
} |
|
91 |
||
92 |
static QString escapedKey(QString uKey) |
|
93 |
{ |
|
94 |
QChar *data = uKey.data(); |
|
95 |
int l = uKey.length(); |
|
96 |
for (int i = 0; i < l; ++i) { |
|
97 |
ushort &ucs = data[i].unicode(); |
|
98 |
if (ucs == '\\') |
|
99 |
ucs = '/'; |
|
100 |
else if (ucs == '/') |
|
101 |
ucs = '\\'; |
|
102 |
} |
|
103 |
return uKey; |
|
104 |
} |
|
105 |
||
106 |
static QString unescapedKey(QString rKey) |
|
107 |
{ |
|
108 |
return escapedKey(rKey); |
|
109 |
} |
|
110 |
||
111 |
typedef QMap<QString, QString> NameSet; |
|
112 |
||
113 |
static void mergeKeySets(NameSet *dest, const NameSet &src) |
|
114 |
{ |
|
115 |
NameSet::const_iterator it = src.constBegin(); |
|
116 |
for (; it != src.constEnd(); ++it) |
|
117 |
dest->insert(unescapedKey(it.key()), QString()); |
|
118 |
} |
|
119 |
||
120 |
static void mergeKeySets(NameSet *dest, const QStringList &src) |
|
121 |
{ |
|
122 |
QStringList::const_iterator it = src.constBegin(); |
|
123 |
for (; it != src.constEnd(); ++it) |
|
124 |
dest->insert(unescapedKey(*it), QString()); |
|
125 |
} |
|
126 |
||
127 |
/******************************************************************************* |
|
128 |
** Wrappers for the insane windows registry API |
|
129 |
*/ |
|
130 |
||
131 |
static QString errorCodeToString(DWORD errorCode) |
|
132 |
{ |
|
133 |
wchar_t *data = 0; |
|
134 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, data, 0, 0); |
|
135 |
QString result = QString::fromWCharArray(data); |
|
136 |
||
137 |
if (data != 0) |
|
138 |
LocalFree(data); |
|
139 |
||
140 |
if (result.endsWith(QLatin1Char('\n'))) |
|
141 |
result.truncate(result.length() - 1); |
|
142 |
||
143 |
return result; |
|
144 |
} |
|
145 |
||
146 |
// Open a key with the specified perms |
|
147 |
static HKEY openKey(HKEY parentHandle, REGSAM perms, const QString &rSubKey) |
|
148 |
{ |
|
149 |
HKEY resultHandle = 0; |
|
150 |
LONG res = RegOpenKeyEx(parentHandle, reinterpret_cast<const wchar_t *>(rSubKey.utf16()), |
|
151 |
0, perms, &resultHandle); |
|
152 |
||
153 |
if (res == ERROR_SUCCESS) |
|
154 |
return resultHandle; |
|
155 |
||
156 |
return 0; |
|
157 |
} |
|
158 |
||
159 |
// Open a key with the specified perms, create it if it does not exist |
|
160 |
static HKEY createOrOpenKey(HKEY parentHandle, REGSAM perms, const QString &rSubKey) |
|
161 |
{ |
|
162 |
// try to open it |
|
163 |
HKEY resultHandle = openKey(parentHandle, perms, rSubKey); |
|
164 |
if (resultHandle != 0) |
|
165 |
return resultHandle; |
|
166 |
||
167 |
// try to create it |
|
168 |
LONG res = RegCreateKeyEx(parentHandle, reinterpret_cast<const wchar_t *>(rSubKey.utf16()), 0, 0, |
|
169 |
REG_OPTION_NON_VOLATILE, perms, 0, &resultHandle, 0); |
|
170 |
||
171 |
if (res == ERROR_SUCCESS) |
|
172 |
return resultHandle; |
|
173 |
||
174 |
//qWarning("QSettings: Failed to create subkey \"%s\": %s", |
|
175 |
// rSubKey.toLatin1().data(), errorCodeToString(res).toLatin1().data()); |
|
176 |
||
177 |
return 0; |
|
178 |
} |
|
179 |
||
180 |
// Open or create a key in read-write mode if possible, otherwise read-only |
|
181 |
static HKEY createOrOpenKey(HKEY parentHandle, const QString &rSubKey, bool *readOnly) |
|
182 |
{ |
|
183 |
// try to open or create it read/write |
|
184 |
HKEY resultHandle = createOrOpenKey(parentHandle, registryPermissions, rSubKey); |
|
185 |
if (resultHandle != 0) { |
|
186 |
if (readOnly != 0) |
|
187 |
*readOnly = false; |
|
188 |
return resultHandle; |
|
189 |
} |
|
190 |
||
191 |
// try to open or create it read/only |
|
192 |
resultHandle = createOrOpenKey(parentHandle, KEY_READ, rSubKey); |
|
193 |
if (resultHandle != 0) { |
|
194 |
if (readOnly != 0) |
|
195 |
*readOnly = true; |
|
196 |
return resultHandle; |
|
197 |
} |
|
198 |
return 0; |
|
199 |
} |
|
200 |
||
201 |
static QStringList childKeysOrGroups(HKEY parentHandle, QSettingsPrivate::ChildSpec spec) |
|
202 |
{ |
|
203 |
QStringList result; |
|
204 |
DWORD numKeys; |
|
205 |
DWORD maxKeySize; |
|
206 |
DWORD numSubgroups; |
|
207 |
DWORD maxSubgroupSize; |
|
208 |
||
209 |
// Find the number of keys and subgroups, as well as the max of their lengths. |
|
210 |
LONG res = RegQueryInfoKey(parentHandle, 0, 0, 0, &numSubgroups, &maxSubgroupSize, 0, |
|
211 |
&numKeys, &maxKeySize, 0, 0, 0); |
|
212 |
||
213 |
if (res != ERROR_SUCCESS) { |
|
214 |
qWarning("QSettings: RegQueryInfoKey() failed: %s", errorCodeToString(res).toLatin1().data()); |
|
215 |
return result; |
|
216 |
} |
|
217 |
||
218 |
++maxSubgroupSize; |
|
219 |
++maxKeySize; |
|
220 |
||
221 |
int n; |
|
222 |
int m; |
|
223 |
if (spec == QSettingsPrivate::ChildKeys) { |
|
224 |
n = numKeys; |
|
225 |
m = maxKeySize; |
|
226 |
} else { |
|
227 |
n = numSubgroups; |
|
228 |
m = maxSubgroupSize; |
|
229 |
} |
|
230 |
||
231 |
/* The size does not include the terminating null character. */ |
|
232 |
++m; |
|
233 |
||
234 |
// Get the list |
|
235 |
QByteArray buff(m * sizeof(wchar_t), 0); |
|
236 |
for (int i = 0; i < n; ++i) { |
|
237 |
QString item; |
|
238 |
DWORD l = buff.size() / sizeof(wchar_t); |
|
239 |
if (spec == QSettingsPrivate::ChildKeys) { |
|
240 |
res = RegEnumValue(parentHandle, i, reinterpret_cast<wchar_t *>(buff.data()), &l, 0, 0, 0, 0); |
|
241 |
} else { |
|
242 |
res = RegEnumKeyEx(parentHandle, i, reinterpret_cast<wchar_t *>(buff.data()), &l, 0, 0, 0, 0); |
|
243 |
} |
|
244 |
if (res == ERROR_SUCCESS) |
|
245 |
item = QString::fromWCharArray((const wchar_t *)buff.constData(), l); |
|
246 |
||
247 |
if (res != ERROR_SUCCESS) { |
|
248 |
qWarning("QSettings: RegEnumValue failed: %s", errorCodeToString(res).toLatin1().data()); |
|
249 |
continue; |
|
250 |
} |
|
251 |
if (item.isEmpty()) |
|
252 |
item = QLatin1String("."); |
|
253 |
result.append(item); |
|
254 |
} |
|
255 |
return result; |
|
256 |
} |
|
257 |
||
258 |
static void allKeys(HKEY parentHandle, const QString &rSubKey, NameSet *result) |
|
259 |
{ |
|
260 |
HKEY handle = openKey(parentHandle, KEY_READ, rSubKey); |
|
261 |
if (handle == 0) |
|
262 |
return; |
|
263 |
||
264 |
QStringList childKeys = childKeysOrGroups(handle, QSettingsPrivate::ChildKeys); |
|
265 |
QStringList childGroups = childKeysOrGroups(handle, QSettingsPrivate::ChildGroups); |
|
266 |
RegCloseKey(handle); |
|
267 |
||
268 |
for (int i = 0; i < childKeys.size(); ++i) { |
|
269 |
QString s = rSubKey; |
|
270 |
if (!s.isEmpty()) |
|
271 |
s += QLatin1Char('\\'); |
|
272 |
s += childKeys.at(i); |
|
273 |
result->insert(s, QString()); |
|
274 |
} |
|
275 |
||
276 |
for (int i = 0; i < childGroups.size(); ++i) { |
|
277 |
QString s = rSubKey; |
|
278 |
if (!s.isEmpty()) |
|
279 |
s += QLatin1Char('\\'); |
|
280 |
s += childGroups.at(i); |
|
281 |
allKeys(parentHandle, s, result); |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
static void deleteChildGroups(HKEY parentHandle) |
|
286 |
{ |
|
287 |
QStringList childGroups = childKeysOrGroups(parentHandle, QSettingsPrivate::ChildGroups); |
|
288 |
||
289 |
for (int i = 0; i < childGroups.size(); ++i) { |
|
290 |
QString group = childGroups.at(i); |
|
291 |
||
292 |
// delete subgroups in group |
|
293 |
HKEY childGroupHandle = openKey(parentHandle, registryPermissions, group); |
|
294 |
if (childGroupHandle == 0) |
|
295 |
continue; |
|
296 |
deleteChildGroups(childGroupHandle); |
|
297 |
RegCloseKey(childGroupHandle); |
|
298 |
||
299 |
// delete group itself |
|
300 |
LONG res = RegDeleteKey(parentHandle, reinterpret_cast<const wchar_t *>(group.utf16())); |
|
301 |
if (res != ERROR_SUCCESS) { |
|
302 |
qWarning("QSettings: RegDeleteKey failed on subkey \"%s\": %s", |
|
303 |
group.toLatin1().data(), errorCodeToString(res).toLatin1().data()); |
|
304 |
return; |
|
305 |
} |
|
306 |
} |
|
307 |
} |
|
308 |
||
309 |
/******************************************************************************* |
|
310 |
** class RegistryKey |
|
311 |
*/ |
|
312 |
||
313 |
class RegistryKey |
|
314 |
{ |
|
315 |
public: |
|
316 |
RegistryKey(HKEY parent_handle = 0, const QString &key = QString(), bool read_only = true); |
|
317 |
QString key() const; |
|
318 |
HKEY handle() const; |
|
319 |
HKEY parentHandle() const; |
|
320 |
bool readOnly() const; |
|
321 |
void close(); |
|
322 |
private: |
|
323 |
HKEY m_parent_handle; |
|
324 |
mutable HKEY m_handle; |
|
325 |
QString m_key; |
|
326 |
mutable bool m_read_only; |
|
327 |
}; |
|
328 |
||
329 |
RegistryKey::RegistryKey(HKEY parent_handle, const QString &key, bool read_only) |
|
330 |
{ |
|
331 |
m_parent_handle = parent_handle; |
|
332 |
m_handle = 0; |
|
333 |
m_read_only = read_only; |
|
334 |
m_key = key; |
|
335 |
} |
|
336 |
||
337 |
QString RegistryKey::key() const |
|
338 |
{ |
|
339 |
return m_key; |
|
340 |
} |
|
341 |
||
342 |
HKEY RegistryKey::handle() const |
|
343 |
{ |
|
344 |
if (m_handle != 0) |
|
345 |
return m_handle; |
|
346 |
||
347 |
if (m_read_only) |
|
348 |
m_handle = openKey(m_parent_handle, KEY_READ, m_key); |
|
349 |
else |
|
350 |
m_handle = createOrOpenKey(m_parent_handle, m_key, &m_read_only); |
|
351 |
||
352 |
return m_handle; |
|
353 |
} |
|
354 |
||
355 |
HKEY RegistryKey::parentHandle() const |
|
356 |
{ |
|
357 |
return m_parent_handle; |
|
358 |
} |
|
359 |
||
360 |
bool RegistryKey::readOnly() const |
|
361 |
{ |
|
362 |
return m_read_only; |
|
363 |
} |
|
364 |
||
365 |
void RegistryKey::close() |
|
366 |
{ |
|
367 |
if (m_handle != 0) |
|
368 |
RegCloseKey(m_handle); |
|
369 |
m_handle = 0; |
|
370 |
} |
|
371 |
||
372 |
typedef QVector<RegistryKey> RegistryKeyList; |
|
373 |
||
374 |
/******************************************************************************* |
|
375 |
** class QWinSettingsPrivate |
|
376 |
*/ |
|
377 |
||
378 |
class QWinSettingsPrivate : public QSettingsPrivate |
|
379 |
{ |
|
380 |
public: |
|
381 |
QWinSettingsPrivate(QSettings::Scope scope, const QString &organization, |
|
382 |
const QString &application); |
|
383 |
QWinSettingsPrivate(QString rKey); |
|
384 |
~QWinSettingsPrivate(); |
|
385 |
||
386 |
void remove(const QString &uKey); |
|
387 |
void set(const QString &uKey, const QVariant &value); |
|
388 |
bool get(const QString &uKey, QVariant *value) const; |
|
389 |
QStringList children(const QString &uKey, ChildSpec spec) const; |
|
390 |
void clear(); |
|
391 |
void sync(); |
|
392 |
void flush(); |
|
393 |
bool isWritable() const; |
|
394 |
HKEY writeHandle() const; |
|
395 |
bool readKey(HKEY parentHandle, const QString &rSubKey, QVariant *value) const; |
|
396 |
QString fileName() const; |
|
397 |
||
398 |
private: |
|
399 |
RegistryKeyList regList; // list of registry locations to search for keys |
|
400 |
bool deleteWriteHandleOnExit; |
|
401 |
}; |
|
402 |
||
403 |
QWinSettingsPrivate::QWinSettingsPrivate(QSettings::Scope scope, const QString &organization, |
|
404 |
const QString &application) |
|
405 |
: QSettingsPrivate(QSettings::NativeFormat, scope, organization, application) |
|
406 |
{ |
|
407 |
deleteWriteHandleOnExit = false; |
|
408 |
||
409 |
if (!organization.isEmpty()) { |
|
410 |
QString prefix = QLatin1String("Software\\") + organization; |
|
411 |
QString orgPrefix = prefix + QLatin1String("\\OrganizationDefaults"); |
|
412 |
QString appPrefix = prefix + QLatin1Char('\\') + application; |
|
413 |
||
414 |
if (scope == QSettings::UserScope) { |
|
415 |
if (!application.isEmpty()) |
|
416 |
regList.append(RegistryKey(HKEY_CURRENT_USER, appPrefix, !regList.isEmpty())); |
|
417 |
||
418 |
regList.append(RegistryKey(HKEY_CURRENT_USER, orgPrefix, !regList.isEmpty())); |
|
419 |
} |
|
420 |
||
421 |
if (!application.isEmpty()) |
|
422 |
regList.append(RegistryKey(HKEY_LOCAL_MACHINE, appPrefix, !regList.isEmpty())); |
|
423 |
||
424 |
regList.append(RegistryKey(HKEY_LOCAL_MACHINE, orgPrefix, !regList.isEmpty())); |
|
425 |
} |
|
426 |
||
427 |
if (regList.isEmpty()) |
|
428 |
setStatus(QSettings::AccessError); |
|
429 |
} |
|
430 |
||
431 |
QWinSettingsPrivate::QWinSettingsPrivate(QString rPath) |
|
432 |
: QSettingsPrivate(QSettings::NativeFormat) |
|
433 |
{ |
|
434 |
deleteWriteHandleOnExit = false; |
|
435 |
||
436 |
if (rPath.startsWith(QLatin1String("\\"))) |
|
437 |
rPath = rPath.mid(1); |
|
438 |
||
439 |
if (rPath.startsWith(QLatin1String("HKEY_CURRENT_USER\\"))) |
|
440 |
regList.append(RegistryKey(HKEY_CURRENT_USER, rPath.mid(18), false)); |
|
441 |
else if (rPath == QLatin1String("HKEY_CURRENT_USER")) |
|
442 |
regList.append(RegistryKey(HKEY_CURRENT_USER, QString(), false)); |
|
443 |
else if (rPath.startsWith(QLatin1String("HKEY_LOCAL_MACHINE\\"))) |
|
444 |
regList.append(RegistryKey(HKEY_LOCAL_MACHINE, rPath.mid(19), false)); |
|
445 |
else if (rPath == QLatin1String("HKEY_LOCAL_MACHINE")) |
|
446 |
regList.append(RegistryKey(HKEY_LOCAL_MACHINE, QString(), false)); |
|
447 |
else if (rPath.startsWith(QLatin1String("HKEY_CLASSES_ROOT\\"))) |
|
448 |
regList.append(RegistryKey(HKEY_CLASSES_ROOT, rPath.mid(18), false)); |
|
449 |
else if (rPath == QLatin1String("HKEY_CLASSES_ROOT")) |
|
450 |
regList.append(RegistryKey(HKEY_CLASSES_ROOT, QString(), false)); |
|
451 |
else if (rPath.startsWith(QLatin1String("HKEY_USERS\\"))) |
|
452 |
regList.append(RegistryKey(HKEY_USERS, rPath.mid(11), false)); |
|
453 |
else if (rPath == QLatin1String(QLatin1String("HKEY_USERS"))) |
|
454 |
regList.append(RegistryKey(HKEY_USERS, QString(), false)); |
|
455 |
else |
|
456 |
regList.append(RegistryKey(HKEY_LOCAL_MACHINE, rPath, false)); |
|
457 |
} |
|
458 |
||
459 |
bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVariant *value) const |
|
460 |
{ |
|
461 |
QString rSubkeyName = keyName(rSubKey); |
|
462 |
QString rSubkeyPath = keyPath(rSubKey); |
|
463 |
||
464 |
// open a handle on the subkey |
|
465 |
HKEY handle = openKey(parentHandle, KEY_READ, rSubkeyPath); |
|
466 |
if (handle == 0) |
|
467 |
return false; |
|
468 |
||
469 |
// get the size and type of the value |
|
470 |
DWORD dataType; |
|
471 |
DWORD dataSize; |
|
472 |
LONG res = RegQueryValueEx(handle, reinterpret_cast<const wchar_t *>(rSubkeyName.utf16()), 0, &dataType, 0, &dataSize); |
|
473 |
if (res != ERROR_SUCCESS) { |
|
474 |
RegCloseKey(handle); |
|
475 |
return false; |
|
476 |
} |
|
477 |
||
478 |
// get the value |
|
479 |
QByteArray data(dataSize, 0); |
|
480 |
res = RegQueryValueEx(handle, reinterpret_cast<const wchar_t *>(rSubkeyName.utf16()), 0, 0, |
|
481 |
reinterpret_cast<unsigned char*>(data.data()), &dataSize); |
|
482 |
if (res != ERROR_SUCCESS) { |
|
483 |
RegCloseKey(handle); |
|
484 |
return false; |
|
485 |
} |
|
486 |
||
487 |
switch (dataType) { |
|
488 |
case REG_EXPAND_SZ: |
|
489 |
case REG_SZ: { |
|
490 |
QString s; |
|
491 |
if (dataSize) { |
|
492 |
s = QString::fromWCharArray(((const wchar_t *)data.constData())); |
|
493 |
} |
|
494 |
if (value != 0) |
|
495 |
*value = stringToVariant(s); |
|
496 |
break; |
|
497 |
} |
|
498 |
||
499 |
case REG_MULTI_SZ: { |
|
500 |
QStringList l; |
|
501 |
if (dataSize) { |
|
502 |
int i = 0; |
|
503 |
for (;;) { |
|
504 |
QString s = QString::fromWCharArray((const wchar_t *)data.constData() + i); |
|
505 |
i += s.length() + 1; |
|
506 |
||
507 |
if (s.isEmpty()) |
|
508 |
break; |
|
509 |
l.append(s); |
|
510 |
} |
|
511 |
} |
|
512 |
if (value != 0) |
|
513 |
*value = stringListToVariantList(l); |
|
514 |
break; |
|
515 |
} |
|
516 |
||
517 |
case REG_NONE: |
|
518 |
case REG_BINARY: { |
|
519 |
QString s; |
|
520 |
if (dataSize) { |
|
521 |
s = QString::fromWCharArray((const wchar_t *)data.constData(), data.size() / 2); |
|
522 |
} |
|
523 |
if (value != 0) |
|
524 |
*value = stringToVariant(s); |
|
525 |
break; |
|
526 |
} |
|
527 |
||
528 |
case REG_DWORD_BIG_ENDIAN: |
|
529 |
case REG_DWORD: { |
|
530 |
Q_ASSERT(data.size() == sizeof(int)); |
|
531 |
int i; |
|
532 |
memcpy((char*)&i, data.constData(), sizeof(int)); |
|
533 |
if (value != 0) |
|
534 |
*value = i; |
|
535 |
break; |
|
536 |
} |
|
537 |
||
538 |
default: |
|
539 |
qWarning("QSettings: Unknown data %d type in Windows registry", static_cast<int>(dataType)); |
|
540 |
if (value != 0) |
|
541 |
*value = QVariant(); |
|
542 |
break; |
|
543 |
} |
|
544 |
||
545 |
RegCloseKey(handle); |
|
546 |
return true; |
|
547 |
} |
|
548 |
||
549 |
HKEY QWinSettingsPrivate::writeHandle() const |
|
550 |
{ |
|
551 |
if (regList.isEmpty()) |
|
552 |
return 0; |
|
553 |
const RegistryKey &key = regList.at(0); |
|
554 |
if (key.handle() == 0 || key.readOnly()) |
|
555 |
return 0; |
|
556 |
return key.handle(); |
|
557 |
} |
|
558 |
||
559 |
QWinSettingsPrivate::~QWinSettingsPrivate() |
|
560 |
{ |
|
561 |
if (deleteWriteHandleOnExit && writeHandle() != 0) { |
|
562 |
#if defined(Q_OS_WINCE) |
|
563 |
remove(regList.at(0).key()); |
|
564 |
#else |
|
565 |
QString emptyKey; |
|
566 |
DWORD res = RegDeleteKey(writeHandle(), reinterpret_cast<const wchar_t *>(emptyKey.utf16())); |
|
567 |
if (res != ERROR_SUCCESS) { |
|
568 |
qWarning("QSettings: Failed to delete key \"%s\": %s", |
|
569 |
regList.at(0).key().toLatin1().data(), errorCodeToString(res).toLatin1().data()); |
|
570 |
} |
|
571 |
#endif |
|
572 |
} |
|
573 |
||
574 |
for (int i = 0; i < regList.size(); ++i) |
|
575 |
regList[i].close(); |
|
576 |
} |
|
577 |
||
578 |
void QWinSettingsPrivate::remove(const QString &uKey) |
|
579 |
{ |
|
580 |
if (writeHandle() == 0) { |
|
581 |
setStatus(QSettings::AccessError); |
|
582 |
return; |
|
583 |
} |
|
584 |
||
585 |
QString rKey = escapedKey(uKey); |
|
586 |
||
587 |
// try to delete value bar in key foo |
|
588 |
LONG res; |
|
589 |
HKEY handle = openKey(writeHandle(), registryPermissions, keyPath(rKey)); |
|
590 |
if (handle != 0) { |
|
591 |
res = RegDeleteValue(handle, reinterpret_cast<const wchar_t *>(keyName(rKey).utf16())); |
|
592 |
RegCloseKey(handle); |
|
593 |
} |
|
594 |
||
595 |
// try to delete key foo/bar and all subkeys |
|
596 |
handle = openKey(writeHandle(), registryPermissions, rKey); |
|
597 |
if (handle != 0) { |
|
598 |
deleteChildGroups(handle); |
|
599 |
||
600 |
if (rKey.isEmpty()) { |
|
601 |
QStringList childKeys = childKeysOrGroups(handle, QSettingsPrivate::ChildKeys); |
|
602 |
||
603 |
for (int i = 0; i < childKeys.size(); ++i) { |
|
604 |
QString group = childKeys.at(i); |
|
605 |
||
606 |
LONG res = RegDeleteValue(handle, reinterpret_cast<const wchar_t *>(group.utf16())); |
|
607 |
if (res != ERROR_SUCCESS) { |
|
608 |
qWarning("QSettings: RegDeleteValue failed on subkey \"%s\": %s", |
|
609 |
group.toLatin1().data(), errorCodeToString(res).toLatin1().data()); |
|
610 |
} |
|
611 |
} |
|
612 |
} else { |
|
613 |
#if defined(Q_OS_WINCE) |
|
614 |
// For WinCE always Close the handle first. |
|
615 |
RegCloseKey(handle); |
|
616 |
#endif |
|
617 |
res = RegDeleteKey(writeHandle(), reinterpret_cast<const wchar_t *>(rKey.utf16())); |
|
618 |
||
619 |
if (res != ERROR_SUCCESS) { |
|
620 |
qWarning("QSettings: RegDeleteKey failed on key \"%s\": %s", |
|
621 |
rKey.toLatin1().data(), errorCodeToString(res).toLatin1().data()); |
|
622 |
} |
|
623 |
} |
|
624 |
RegCloseKey(handle); |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
static bool stringContainsNullChar(const QString &s) |
|
629 |
{ |
|
630 |
for (int i = 0; i < s.length(); ++i) { |
|
631 |
if (s.at(i).unicode() == 0) |
|
632 |
return true; |
|
633 |
} |
|
634 |
return false; |
|
635 |
} |
|
636 |
||
637 |
void QWinSettingsPrivate::set(const QString &uKey, const QVariant &value) |
|
638 |
{ |
|
639 |
if (writeHandle() == 0) { |
|
640 |
setStatus(QSettings::AccessError); |
|
641 |
return; |
|
642 |
} |
|
643 |
||
644 |
QString rKey = escapedKey(uKey); |
|
645 |
||
646 |
HKEY handle = createOrOpenKey(writeHandle(), registryPermissions, keyPath(rKey)); |
|
647 |
if (handle == 0) { |
|
648 |
setStatus(QSettings::AccessError); |
|
649 |
return; |
|
650 |
} |
|
651 |
||
652 |
DWORD type; |
|
653 |
QByteArray regValueBuff; |
|
654 |
||
655 |
// Determine the type |
|
656 |
switch (value.type()) { |
|
657 |
case QVariant::List: |
|
658 |
case QVariant::StringList: { |
|
659 |
// If none of the elements contains '\0', we can use REG_MULTI_SZ, the |
|
660 |
// native registry string list type. Otherwise we use REG_BINARY. |
|
661 |
type = REG_MULTI_SZ; |
|
662 |
QStringList l = variantListToStringList(value.toList()); |
|
663 |
QStringList::const_iterator it = l.constBegin(); |
|
664 |
for (; it != l.constEnd(); ++it) { |
|
665 |
if ((*it).length() == 0 || stringContainsNullChar(*it)) { |
|
666 |
type = REG_BINARY; |
|
667 |
break; |
|
668 |
} |
|
669 |
} |
|
670 |
||
671 |
if (type == REG_BINARY) { |
|
672 |
QString s = variantToString(value); |
|
673 |
regValueBuff = QByteArray((const char*)s.utf16(), s.length() * 2); |
|
674 |
} else { |
|
675 |
QStringList::const_iterator it = l.constBegin(); |
|
676 |
for (; it != l.constEnd(); ++it) { |
|
677 |
const QString &s = *it; |
|
678 |
regValueBuff += QByteArray((const char*)s.utf16(), (s.length() + 1) * 2); |
|
679 |
} |
|
680 |
regValueBuff.append((char)0); |
|
681 |
regValueBuff.append((char)0); |
|
682 |
} |
|
683 |
break; |
|
684 |
} |
|
685 |
||
686 |
case QVariant::Int: { |
|
687 |
type = REG_DWORD; |
|
688 |
int i = value.toInt(); |
|
689 |
regValueBuff = QByteArray((const char*)&i, sizeof(int)); |
|
690 |
break; |
|
691 |
} |
|
692 |
||
693 |
case QVariant::ByteArray: |
|
694 |
// fallthrough intended |
|
695 |
||
696 |
default: { |
|
697 |
// If the string does not contain '\0', we can use REG_SZ, the native registry |
|
698 |
// string type. Otherwise we use REG_BINARY. |
|
699 |
QString s = variantToString(value); |
|
700 |
type = stringContainsNullChar(s) ? REG_BINARY : REG_SZ; |
|
701 |
if (type == REG_BINARY) { |
|
702 |
regValueBuff = QByteArray((const char*)s.utf16(), s.length() * 2); |
|
703 |
} else { |
|
704 |
regValueBuff = QByteArray((const char*)s.utf16(), (s.length() + 1) * 2); |
|
705 |
} |
|
706 |
break; |
|
707 |
} |
|
708 |
} |
|
709 |
||
710 |
// set the value |
|
711 |
LONG res = RegSetValueEx(handle, reinterpret_cast<const wchar_t *>(keyName(rKey).utf16()), 0, type, |
|
712 |
reinterpret_cast<const unsigned char*>(regValueBuff.constData()), |
|
713 |
regValueBuff.size()); |
|
714 |
||
715 |
if (res == ERROR_SUCCESS) { |
|
716 |
deleteWriteHandleOnExit = false; |
|
717 |
} else { |
|
718 |
qWarning("QSettings: failed to set subkey \"%s\": %s", |
|
719 |
rKey.toLatin1().data(), errorCodeToString(res).toLatin1().data()); |
|
720 |
setStatus(QSettings::AccessError); |
|
721 |
} |
|
722 |
||
723 |
RegCloseKey(handle); |
|
724 |
} |
|
725 |
||
726 |
bool QWinSettingsPrivate::get(const QString &uKey, QVariant *value) const |
|
727 |
{ |
|
728 |
QString rKey = escapedKey(uKey); |
|
729 |
||
730 |
for (int i = 0; i < regList.size(); ++i) { |
|
731 |
HKEY handle = regList.at(i).handle(); |
|
732 |
if (handle != 0 && readKey(handle, rKey, value)) |
|
733 |
return true; |
|
734 |
||
735 |
if (!fallbacks) |
|
736 |
return false; |
|
737 |
} |
|
738 |
||
739 |
return false; |
|
740 |
} |
|
741 |
||
742 |
QStringList QWinSettingsPrivate::children(const QString &uKey, ChildSpec spec) const |
|
743 |
{ |
|
744 |
NameSet result; |
|
745 |
QString rKey = escapedKey(uKey); |
|
746 |
||
747 |
for (int i = 0; i < regList.size(); ++i) { |
|
748 |
HKEY parent_handle = regList.at(i).handle(); |
|
749 |
if (parent_handle == 0) |
|
750 |
continue; |
|
751 |
HKEY handle = openKey(parent_handle, KEY_READ, rKey); |
|
752 |
if (handle == 0) |
|
753 |
continue; |
|
754 |
||
755 |
if (spec == AllKeys) { |
|
756 |
NameSet keys; |
|
757 |
allKeys(handle, QLatin1String(""), &keys); |
|
758 |
mergeKeySets(&result, keys); |
|
759 |
} else { // ChildGroups or ChildKeys |
|
760 |
QStringList names = childKeysOrGroups(handle, spec); |
|
761 |
mergeKeySets(&result, names); |
|
762 |
} |
|
763 |
||
764 |
RegCloseKey(handle); |
|
765 |
||
766 |
if (!fallbacks) |
|
767 |
return result.keys(); |
|
768 |
} |
|
769 |
||
770 |
return result.keys(); |
|
771 |
} |
|
772 |
||
773 |
void QWinSettingsPrivate::clear() |
|
774 |
{ |
|
775 |
remove(QString()); |
|
776 |
deleteWriteHandleOnExit = true; |
|
777 |
} |
|
778 |
||
779 |
void QWinSettingsPrivate::sync() |
|
780 |
{ |
|
781 |
RegFlushKey(writeHandle()); |
|
782 |
} |
|
783 |
||
784 |
void QWinSettingsPrivate::flush() |
|
785 |
{ |
|
786 |
// Windows does this for us. |
|
787 |
} |
|
788 |
||
789 |
QString QWinSettingsPrivate::fileName() const |
|
790 |
{ |
|
791 |
if (regList.isEmpty()) |
|
792 |
return QString(); |
|
793 |
||
794 |
const RegistryKey &key = regList.at(0); |
|
795 |
QString result; |
|
796 |
if (key.parentHandle() == HKEY_CURRENT_USER) |
|
797 |
result = QLatin1String("\\HKEY_CURRENT_USER\\"); |
|
798 |
else |
|
799 |
result = QLatin1String("\\HKEY_LOCAL_MACHINE\\"); |
|
800 |
||
801 |
return result + regList.at(0).key(); |
|
802 |
} |
|
803 |
||
804 |
bool QWinSettingsPrivate::isWritable() const |
|
805 |
{ |
|
806 |
return writeHandle() != 0; |
|
807 |
} |
|
808 |
||
809 |
QSettingsPrivate *QSettingsPrivate::create(QSettings::Format format, QSettings::Scope scope, |
|
810 |
const QString &organization, const QString &application) |
|
811 |
{ |
|
812 |
if (format == QSettings::NativeFormat) { |
|
813 |
return new QWinSettingsPrivate(scope, organization, application); |
|
814 |
} else { |
|
815 |
return new QConfFileSettingsPrivate(format, scope, organization, application); |
|
816 |
} |
|
817 |
} |
|
818 |
||
819 |
QSettingsPrivate *QSettingsPrivate::create(const QString &fileName, QSettings::Format format) |
|
820 |
{ |
|
821 |
if (format == QSettings::NativeFormat) { |
|
822 |
return new QWinSettingsPrivate(fileName); |
|
823 |
} else { |
|
824 |
return new QConfFileSettingsPrivate(fileName, format); |
|
825 |
} |
|
826 |
} |
|
827 |
||
828 |
QT_END_NAMESPACE |
|
829 |
#endif // QT_NO_SETTINGS |