Qt 4.8
Classes | Typedefs | Functions | Variables
qsettings_win.cpp File Reference
#include "qsettings.h"
#include "qsettings_p.h"
#include "qvector.h"
#include "qmap.h"
#include "qt_windows.h"
#include "qdebug.h"

Go to the source code of this file.

Classes

class  QWinSettingsPrivate
 
class  RegistryKey
 

Typedefs

typedef QMap< QString, QStringNameSet
 
typedef QVector< RegistryKeyRegistryKeyList
 

Functions

static void allKeys (HKEY parentHandle, const QString &rSubKey, NameSet *result)
 
static QStringList childKeysOrGroups (HKEY parentHandle, QSettingsPrivate::ChildSpec spec)
 
static HKEY createOrOpenKey (HKEY parentHandle, REGSAM perms, const QString &rSubKey)
 
static HKEY createOrOpenKey (HKEY parentHandle, const QString &rSubKey, bool *readOnly)
 
static void deleteChildGroups (HKEY parentHandle)
 
static QString errorCodeToString (DWORD errorCode)
 
static QString escapedKey (QString uKey)
 
static QString keyName (const QString &rKey)
 
static QString keyPath (const QString &rKey)
 
static void mergeKeySets (NameSet *dest, const NameSet &src)
 
static void mergeKeySets (NameSet *dest, const QStringList &src)
 
static HKEY openKey (HKEY parentHandle, REGSAM perms, const QString &rSubKey)
 
static bool stringContainsNullChar (const QString &s)
 
static QString unescapedKey (QString rKey)
 

Variables

static const REGSAM registryPermissions = KEY_READ | KEY_WRITE
 

Typedef Documentation

◆ NameSet

Definition at line 111 of file qsettings_win.cpp.

◆ RegistryKeyList

Definition at line 372 of file qsettings_win.cpp.

Function Documentation

◆ allKeys()

static void allKeys ( HKEY  parentHandle,
const QString rSubKey,
NameSet result 
)
static

Definition at line 258 of file qsettings_win.cpp.

Referenced by QWinSettingsPrivate::children(), QCoreTextFontDatabase::populateFontDatabase(), QShortcutMap::removeShortcut(), QShortcutMap::setShortcutAutoRepeat(), and QShortcutMap::setShortcutEnabled().

259 {
260  HKEY handle = openKey(parentHandle, KEY_READ, rSubKey);
261  if (handle == 0)
262  return;
263 
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 }
static void allKeys(HKEY parentHandle, const QString &rSubKey, NameSet *result)
static QStringList childKeysOrGroups(HKEY parentHandle, QSettingsPrivate::ChildSpec spec)
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static HKEY openKey(HKEY parentHandle, REGSAM perms, const QString &rSubKey)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ childKeysOrGroups()

static QStringList childKeysOrGroups ( HKEY  parentHandle,
QSettingsPrivate::ChildSpec  spec 
)
static

Definition at line 201 of file qsettings_win.cpp.

Referenced by allKeys(), QWinSettingsPrivate::children(), deleteChildGroups(), and QWinSettingsPrivate::remove().

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 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString errorCodeToString(DWORD errorCode)
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
QFactoryLoader * l

◆ createOrOpenKey() [1/2]

static HKEY createOrOpenKey ( HKEY  parentHandle,
REGSAM  perms,
const QString rSubKey 
)
static

Definition at line 160 of file qsettings_win.cpp.

Referenced by createOrOpenKey(), RegistryKey::handle(), and QWinSettingsPrivate::set().

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 }
static HKEY openKey(HKEY parentHandle, REGSAM perms, const QString &rSubKey)
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

◆ createOrOpenKey() [2/2]

static HKEY createOrOpenKey ( HKEY  parentHandle,
const QString rSubKey,
bool *  readOnly 
)
static

Definition at line 181 of file qsettings_win.cpp.

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 }
static HKEY createOrOpenKey(HKEY parentHandle, REGSAM perms, const QString &rSubKey)
static const REGSAM registryPermissions

◆ deleteChildGroups()

static void deleteChildGroups ( HKEY  parentHandle)
static

Definition at line 285 of file qsettings_win.cpp.

Referenced by QWinSettingsPrivate::remove().

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 }
static QStringList childKeysOrGroups(HKEY parentHandle, QSettingsPrivate::ChildSpec spec)
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
static const REGSAM registryPermissions
The QString class provides a Unicode character string.
Definition: qstring.h:83
static void deleteChildGroups(HKEY parentHandle)
static QString errorCodeToString(DWORD errorCode)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT void qWarning(const char *,...)
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static HKEY openKey(HKEY parentHandle, REGSAM perms, const QString &rSubKey)
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

◆ errorCodeToString()

static QString errorCodeToString ( DWORD  errorCode)
static

Definition at line 131 of file qsettings_win.cpp.

Referenced by childKeysOrGroups(), deleteChildGroups(), QWinSettingsPrivate::remove(), QWinSettingsPrivate::set(), and QWinSettingsPrivate::~QWinSettingsPrivate().

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 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
The QString class provides a Unicode character string.
Definition: qstring.h:83
void truncate(int pos)
Truncates the string at the given position index.
Definition: qstring.cpp:4603
static const char * data(const QByteArray &arr)
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ escapedKey()

static QString escapedKey ( QString  uKey)
static

Definition at line 92 of file qsettings_win.cpp.

Referenced by QWinSettingsPrivate::children(), QWinSettingsPrivate::get(), QWinSettingsPrivate::remove(), QWinSettingsPrivate::set(), and unescapedKey().

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 }
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
static const char * data(const QByteArray &arr)
unsigned short ushort
Definition: qglobal.h:995
QFactoryLoader * l

◆ keyName()

static QString keyName ( const QString rKey)
static

Definition at line 76 of file qsettings_win.cpp.

Referenced by QCoreWlanEngine::connectToId(), QKeySequencePrivate::decodeString(), launchWebBrowser(), QWinSettingsPrivate::readKey(), QWinSettingsPrivate::remove(), and QWinSettingsPrivate::set().

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 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
int lastIndexOf(QChar c, int from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:3000
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ keyPath()

static QString keyPath ( const QString rKey)
static

Definition at line 68 of file qsettings_win.cpp.

Referenced by QWinSettingsPrivate::readKey(), QWinSettingsPrivate::remove(), QWinSettingsPrivate::set(), and UpdateRegistry().

69 {
70  int idx = rKey.lastIndexOf(QLatin1Char('\\'));
71  if (idx == -1)
72  return QString();
73  return rKey.left(idx + 1);
74 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int lastIndexOf(QChar c, int from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:3000
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ mergeKeySets() [1/2]

static void mergeKeySets ( NameSet dest,
const NameSet src 
)
static

Definition at line 113 of file qsettings_win.cpp.

Referenced by QWinSettingsPrivate::children().

114 {
116  for (; it != src.constEnd(); ++it)
117  dest->insert(unescapedKey(it.key()), QString());
118 }
static QString unescapedKey(QString rKey)
#define it(className, varName)
The QString class provides a Unicode character string.
Definition: qstring.h:83
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
friend class const_iterator
Definition: qmap.h:369

◆ mergeKeySets() [2/2]

static void mergeKeySets ( NameSet dest,
const QStringList src 
)
static

Definition at line 120 of file qsettings_win.cpp.

121 {
123  for (; it != src.constEnd(); ++it)
124  dest->insert(unescapedKey(*it), QString());
125 }
static QString unescapedKey(QString rKey)
#define it(className, varName)
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
The QString class provides a Unicode character string.
Definition: qstring.h:83
friend class const_iterator
Definition: qlist.h:264
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272

◆ openKey()

static HKEY openKey ( HKEY  parentHandle,
REGSAM  perms,
const QString rSubKey 
)
static

Definition at line 147 of file qsettings_win.cpp.

Referenced by allKeys(), QWinSettingsPrivate::children(), createOrOpenKey(), deleteChildGroups(), RegistryKey::handle(), QWinSettingsPrivate::readKey(), and QWinSettingsPrivate::remove().

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 }
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

◆ stringContainsNullChar()

static bool stringContainsNullChar ( const QString s)
static

Definition at line 637 of file qsettings_win.cpp.

Referenced by QWinSettingsPrivate::set().

638 {
639  for (int i = 0; i < s.length(); ++i) {
640  if (s.at(i).unicode() == 0)
641  return true;
642  }
643  return false;
644 }
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696

◆ unescapedKey()

static QString unescapedKey ( QString  rKey)
static

Definition at line 106 of file qsettings_win.cpp.

Referenced by mergeKeySets().

107 {
108  return escapedKey(rKey);
109 }
static QString escapedKey(QString uKey)

Variable Documentation

◆ registryPermissions

const REGSAM registryPermissions = KEY_READ | KEY_WRITE
static