| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "registrylayer_win_p.h" |
| 41 | #include <QtCore/qdatastream.h> |
| 42 | |
| 43 | #if defined(Q_OS_WIN) |
| 44 | |
| 45 | QT_BEGIN_NAMESPACE |
| 46 | |
| 47 | Q_GLOBAL_STATIC(NonVolatileRegistryLayer, nonVolatileRegistryLayer) |
| 48 | Q_GLOBAL_STATIC(VolatileRegistryLayer, volatileRegistryLayer) |
| 49 | |
| 50 | void CALLBACK qVolatileRegistryLayerCallback(PVOID lpParameter, BOOLEAN) |
| 51 | { |
| 52 | QMetaObject::invokeMethod(volatileRegistryLayer(), "emitHandleChanged", Qt::QueuedConnection, |
| 53 | Q_ARG(void *, lpParameter)); |
| 54 | } |
| 55 | |
| 56 | void CALLBACK qNonVolatileRegistryLayerCallback(PVOID lpParameter, BOOLEAN) |
| 57 | { |
| 58 | QMetaObject::invokeMethod(nonVolatileRegistryLayer(), "emitHandleChanged", |
| 59 | Qt::QueuedConnection, Q_ARG(void *, lpParameter)); |
| 60 | } |
| 61 | |
| 62 | static QString qConvertPath(const QString &path) |
| 63 | { |
| 64 | QString fixedPath(path); |
| 65 | |
| 66 | while (fixedPath.endsWith(QLatin1Char('/'))) |
| 67 | fixedPath.chop(1); |
| 68 | |
| 69 | fixedPath.replace(QLatin1Char('/'), QLatin1Char('\\')); |
| 70 | |
| 71 | return fixedPath; |
| 72 | } |
| 73 | |
| 74 | VolatileRegistryLayer::VolatileRegistryLayer() |
| 75 | : RegistryLayer(QLatin1String("Software/Nokia/QtMobility/volatileContext"), true, |
| 76 | &qVolatileRegistryLayerCallback) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | VolatileRegistryLayer::~VolatileRegistryLayer() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | QUuid VolatileRegistryLayer::id() |
| 85 | { |
| 86 | return QVALUESPACE_VOLATILEREGISTRY_LAYER; |
| 87 | } |
| 88 | |
| 89 | QValueSpace::LayerOptions VolatileRegistryLayer::layerOptions() const |
| 90 | { |
| 91 | return QValueSpace::TransientLayer | QValueSpace::WritableLayer; |
| 92 | } |
| 93 | |
| 94 | VolatileRegistryLayer *VolatileRegistryLayer::instance() |
| 95 | { |
| 96 | return volatileRegistryLayer(); |
| 97 | } |
| 98 | |
| 99 | NonVolatileRegistryLayer::NonVolatileRegistryLayer() |
| 100 | : RegistryLayer(QLatin1String("Software/Nokia/QtMobility/nonVolatileContext"), false, |
| 101 | &qNonVolatileRegistryLayerCallback) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | NonVolatileRegistryLayer::~NonVolatileRegistryLayer() |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | QUuid NonVolatileRegistryLayer::id() |
| 110 | { |
| 111 | return QVALUESPACE_NONVOLATILEREGISTRY_LAYER; |
| 112 | } |
| 113 | |
| 114 | QValueSpace::LayerOptions NonVolatileRegistryLayer::layerOptions() const |
| 115 | { |
| 116 | return QValueSpace::PermanentLayer | QValueSpace::WritableLayer; |
| 117 | } |
| 118 | |
| 119 | NonVolatileRegistryLayer *NonVolatileRegistryLayer::instance() |
| 120 | { |
| 121 | return nonVolatileRegistryLayer(); |
| 122 | } |
| 123 | |
| 124 | RegistryLayer::RegistryLayer(const QString &basePath, bool volatileKeys, RegistryCallback callback) |
| 125 | : localLock(QMutex::Recursive), m_basePath(basePath), m_volatileKeys(volatileKeys), |
| 126 | m_callback(callback) |
| 127 | { |
| 128 | // Ensure that the m_basePath key exists and is non-volatile. |
| 129 | HKEY key; |
| 130 | RegCreateKeyEx(HKEY_CURRENT_USER, |
| 131 | reinterpret_cast<const wchar_t*>(qConvertPath(m_basePath).utf16()), |
| 132 | 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &key, 0); |
| 133 | |
| 134 | RegCloseKey(key); |
| 135 | } |
| 136 | |
| 137 | RegistryLayer::~RegistryLayer() |
| 138 | { |
| 139 | QMutableHashIterator<QString, RegistryHandle *> i(handles); |
| 140 | while (i.hasNext()) { |
| 141 | i.next(); |
| 142 | |
| 143 | RegistryHandle *handle = i.value(); |
| 144 | |
| 145 | if (handle->valueHandle) |
| 146 | removeHandle(Handle(handle)); |
| 147 | } |
| 148 | |
| 149 | i.toFront(); |
| 150 | while (i.hasNext()) { |
| 151 | i.next(); |
| 152 | |
| 153 | removeHandle(Handle(i.value())); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | bool RegistryLayer::value(Handle handle, QVariant *data) |
| 158 | { |
| 159 | QMutexLocker locker(&localLock); |
| 160 | |
| 161 | RegistryHandle *rh = registryHandle(handle); |
| 162 | if (!rh) |
| 163 | return false; |
| 164 | |
| 165 | return value(InvalidHandle, rh->path, data); |
| 166 | } |
| 167 | |
| 168 | bool RegistryLayer::value(Handle handle, const QString &subPath, QVariant *data) |
| 169 | { |
| 170 | QMutexLocker locker(&localLock); |
| 171 | |
| 172 | if (handle != InvalidHandle && !registryHandle(handle)) |
| 173 | return false; |
| 174 | |
| 175 | QString path(subPath); |
| 176 | while (path.endsWith(QLatin1Char('/'))) |
| 177 | path.chop(1); |
| 178 | if (handle != InvalidHandle) |
| 179 | while (path.startsWith(QLatin1Char('/'))) |
| 180 | path = path.mid(1); |
| 181 | |
| 182 | Handle fullHandle = item(handle, subPath); |
| 183 | if (fullHandle != InvalidHandle) { |
| 184 | RegistryHandle *rh = registryHandle(fullHandle); |
| 185 | if (rh) { |
| 186 | openRegistryKey(rh); |
| 187 | if (!rh->valueHandle) { |
| 188 | handle = fullHandle; |
| 189 | path.clear(); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | int index = path.lastIndexOf(QLatin1Char('/'), -1); |
| 195 | |
| 196 | bool createdHandle = false; |
| 197 | |
| 198 | QString value; |
| 199 | if (index == -1) { |
| 200 | value = path; |
| 201 | } else { |
| 202 | // want a value that is in a sub path under handle |
| 203 | value = path.mid(index + 1); |
| 204 | path.truncate(index); |
| 205 | if (path.isEmpty()) |
| 206 | path = QLatin1String("/"); |
| 207 | |
| 208 | handle = item(handle, path); |
| 209 | createdHandle = true; |
| 210 | } |
| 211 | |
| 212 | RegistryHandle *rh = registryHandle(handle); |
| 213 | |
| 214 | openRegistryKey(rh); |
| 215 | if (!hKeys.contains(rh)) { |
| 216 | if (createdHandle) |
| 217 | removeHandle(handle); |
| 218 | |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | HKEY key = hKeys.value(rh); |
| 223 | |
| 224 | DWORD regSize = 0; |
| 225 | long result = RegQueryValueEx(key, |
| 226 | reinterpret_cast<const wchar_t*>(value.utf16()), 0, 0, 0, ®Size); |
| 227 | if (result == ERROR_FILE_NOT_FOUND) { |
| 228 | *data = QVariant(); |
| 229 | if (createdHandle) |
| 230 | removeHandle(handle); |
| 231 | return false; |
| 232 | } else if (result != ERROR_SUCCESS) { |
| 233 | qDebug() << "RegQueryValueEx failed with error" << result; |
| 234 | *data = QVariant(); |
| 235 | if (createdHandle) |
| 236 | removeHandle(handle); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | BYTE *regData = new BYTE[regSize]; |
| 241 | DWORD regType; |
| 242 | |
| 243 | result = RegQueryValueEx(key, |
| 244 | reinterpret_cast<const wchar_t*>(value.utf16()), |
| 245 | 0, ®Type, regData, ®Size); |
| 246 | if (result != ERROR_SUCCESS) { |
| 247 | qDebug() << "real RegQueryValueEx failed with error" << result; |
| 248 | if (createdHandle) |
| 249 | removeHandle(handle); |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | switch (regType) { |
| 254 | case REG_DWORD: |
| 255 | *data = qVariantFromValue(*reinterpret_cast<uint *>(regData)); |
| 256 | break; |
| 257 | case REG_QWORD: |
| 258 | *data = qVariantFromValue(*reinterpret_cast<quint64 *>(regData)); |
| 259 | break; |
| 260 | case REG_SZ: |
| 261 | *data = qVariantFromValue(QString::fromWCharArray(reinterpret_cast<wchar_t *>(regData))); |
| 262 | break; |
| 263 | case REG_MULTI_SZ: { |
| 264 | QStringList list; |
| 265 | wchar_t *temp = reinterpret_cast<wchar_t *>(regData); |
| 266 | while (*temp != L'\0') { |
| 267 | QString string = QString::fromWCharArray(temp); |
| 268 | list << string; |
| 269 | temp += string.length() + 1; |
| 270 | } |
| 271 | *data = qVariantFromValue(list); |
| 272 | break; |
| 273 | } |
| 274 | case REG_BINARY: |
| 275 | *data = qVariantFromValue(QByteArray(reinterpret_cast<char *>(regData), regSize)); |
| 276 | break; |
| 277 | case REG_NONE: { |
| 278 | QDataStream stream(QByteArray::fromRawData(reinterpret_cast<char *>(regData), regSize)); |
| 279 | stream >> *data; |
| 280 | break; |
| 281 | } |
| 282 | default: |
| 283 | qDebug() << "Unknown REG type" << regType; |
| 284 | delete[] regData; |
| 285 | if (createdHandle) |
| 286 | removeHandle(handle); |
| 287 | return false; |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | delete[] regData; |
| 292 | |
| 293 | if (createdHandle) |
| 294 | removeHandle(handle); |
| 295 | |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | #define MAX_KEY_LENGTH 255 |
| 300 | #define MAX_NAME_LENGTH 16383 |
| 301 | QSet<QString> RegistryLayer::children(Handle handle) |
| 302 | { |
| 303 | QMutexLocker locker(&localLock); |
| 304 | |
| 305 | QSet<QString> foundChildren; |
| 306 | |
| 307 | RegistryHandle *rh = registryHandle(handle); |
| 308 | if (!rh) |
| 309 | return foundChildren; |
| 310 | |
| 311 | openRegistryKey(rh); |
| 312 | if (rh->valueHandle || !hKeys.contains(rh)) |
| 313 | return foundChildren; |
| 314 | |
| 315 | HKEY key = hKeys.value(rh); |
| 316 | int i = 0; |
| 317 | long result; |
| 318 | do { |
| 319 | TCHAR subKey[MAX_KEY_LENGTH]; |
| 320 | DWORD subKeySize = MAX_KEY_LENGTH; |
| 321 | |
| 322 | result = RegEnumKeyEx(key, i, subKey, &subKeySize, 0, 0, 0, 0); |
| 323 | if (result == ERROR_KEY_DELETED) { |
| 324 | QMetaObject::invokeMethod(this, "emitHandleChanged", Qt::QueuedConnection, |
| 325 | Q_ARG(void *, key)); |
| 326 | break; |
| 327 | } |
| 328 | if (result != ERROR_SUCCESS) |
| 329 | break; |
| 330 | |
| 331 | foundChildren << QString::fromWCharArray(subKey, subKeySize); |
| 332 | ++i; |
| 333 | } while (result == ERROR_SUCCESS); |
| 334 | |
| 335 | i = 0; |
| 336 | do { |
| 337 | TCHAR valueName[MAX_NAME_LENGTH]; |
| 338 | DWORD valueNameSize = MAX_NAME_LENGTH; |
| 339 | |
| 340 | result = RegEnumValue(key, i, valueName, &valueNameSize, 0, 0, 0, 0); |
| 341 | if (result == ERROR_KEY_DELETED) { |
| 342 | QMetaObject::invokeMethod(this, "emitHandleChanged", Qt::QueuedConnection, |
| 343 | Q_ARG(void *, key)); |
| 344 | break; |
| 345 | } |
| 346 | if (result != ERROR_SUCCESS) |
| 347 | break; |
| 348 | |
| 349 | QString value = QString::fromWCharArray(valueName, valueNameSize); |
| 350 | if (!value.isEmpty()) |
| 351 | foundChildren << value; |
| 352 | |
| 353 | ++i; |
| 354 | } while (result == ERROR_SUCCESS); |
| 355 | |
| 356 | return foundChildren; |
| 357 | } |
| 358 | |
| 359 | QAbstractValueSpaceLayer::Handle RegistryLayer::item(Handle parent, const QString &path) |
| 360 | { |
| 361 | QMutexLocker locker(&localLock); |
| 362 | |
| 363 | QString fullPath; |
| 364 | |
| 365 | // Fail on invalid path. |
| 366 | if (path.isEmpty() || path.contains(QLatin1String("//"))) |
| 367 | return InvalidHandle; |
| 368 | |
| 369 | if (parent == InvalidHandle) { |
| 370 | fullPath = path; |
| 371 | } else { |
| 372 | RegistryHandle *rh = registryHandle(parent); |
| 373 | if (!rh) |
| 374 | return InvalidHandle; |
| 375 | |
| 376 | if (path == QLatin1String("/")) { |
| 377 | fullPath = rh->path; |
| 378 | } else if (rh->path.endsWith(QLatin1Char('/')) && path.startsWith(QLatin1Char('/'))) |
| 379 | fullPath = rh->path + path.mid(1); |
| 380 | else if (!rh->path.endsWith(QLatin1Char('/')) && !path.startsWith(QLatin1Char('/'))) |
| 381 | fullPath = rh->path + QLatin1Char('/') + path; |
| 382 | else |
| 383 | fullPath = rh->path + path; |
| 384 | } |
| 385 | |
| 386 | if (handles.contains(fullPath)) { |
| 387 | RegistryHandle *rh = handles.value(fullPath); |
| 388 | ++rh->refCount; |
| 389 | return Handle(rh); |
| 390 | } |
| 391 | |
| 392 | // Create a new handle for path |
| 393 | RegistryHandle *rh = new RegistryHandle(fullPath); |
| 394 | handles.insert(fullPath, rh); |
| 395 | |
| 396 | return Handle(rh); |
| 397 | } |
| 398 | |
| 399 | void RegistryLayer::setProperty(Handle handle, Properties properties) |
| 400 | { |
| 401 | QMutexLocker locker(&localLock); |
| 402 | |
| 403 | RegistryHandle *rh = registryHandle(handle); |
| 404 | if (!rh) |
| 405 | return; |
| 406 | |
| 407 | if (properties & QAbstractValueSpaceLayer::Publish) { |
| 408 | // Enable change notification for handle |
| 409 | openRegistryKey(rh); |
| 410 | if (!hKeys.contains(rh)) { |
| 411 | // The key does not exist. Temporarily use the parent key as a proxy. |
| 412 | QString path = rh->path; |
| 413 | while (!path.isEmpty()) { |
| 414 | rh = registryHandle(item(InvalidHandle, path)); |
| 415 | openRegistryKey(rh); |
| 416 | if (hKeys.contains(rh)) { |
| 417 | notifyProxies.insert(rh, registryHandle(handle)); |
| 418 | break; |
| 419 | } |
| 420 | removeHandle(Handle(rh)); |
| 421 | |
| 422 | // root path doesn't exists |
| 423 | if (path.length() == 1) |
| 424 | return; |
| 425 | |
| 426 | int index = path.lastIndexOf(QLatin1Char('/')); |
| 427 | if (index == 0) |
| 428 | path.truncate(1); |
| 429 | else |
| 430 | path.truncate(index); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | HKEY key = hKeys.value(rh); |
| 435 | |
| 436 | if (waitHandles.contains(key)) |
| 437 | return; |
| 438 | |
| 439 | ::HANDLE event = CreateEvent(0, false, false, 0); |
| 440 | if (event == 0) { |
| 441 | qDebug() << "CreateEvent failed with error" << GetLastError(); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | DWORD filter = REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_ATTRIBUTES | REG_NOTIFY_CHANGE_LAST_SET; |
| 446 | long result = RegNotifyChangeKeyValue(key, true, filter, event, true); |
| 447 | |
| 448 | if (result != ERROR_SUCCESS) { |
| 449 | qDebug() << "RegNotifyChangeKeyValue failed with error" << result; |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | ::HANDLE waitHandle; |
| 454 | if (!RegisterWaitForSingleObject(&waitHandle, event, m_callback, key, |
| 455 | INFINITE, WT_EXECUTEDEFAULT)) { |
| 456 | qDebug() << "RegisterWaitForSingleObject failed with error" << GetLastError(); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | //waitHandles.insert(key, QPair<::HANDLE, ::HANDLE>(event, waitHandle)); |
| 461 | waitHandles.insert(key, HandlePair(event, waitHandle)); |
| 462 | } |
| 463 | if (!(properties & QAbstractValueSpaceLayer::Publish)) { |
| 464 | // Disable change notification for handle |
| 465 | if (!hKeys.contains(rh)) |
| 466 | return; |
| 467 | |
| 468 | HKEY key = hKeys.value(rh); |
| 469 | |
| 470 | if (waitHandles.contains(key)) { |
| 471 | //QPair<::HANDLE, ::HANDLE> wait = waitHandles.take(key); |
| 472 | HandlePair wait = waitHandles.take(key); |
| 473 | |
| 474 | UnregisterWait(wait.second); |
| 475 | CloseHandle(wait.first); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | void RegistryLayer::removeHandle(Handle handle) |
| 481 | { |
| 482 | QMutexLocker locker(&localLock); |
| 483 | |
| 484 | RegistryHandle *rh = registryHandle(handle); |
| 485 | if (!rh) |
| 486 | return; |
| 487 | |
| 488 | if (--rh->refCount) |
| 489 | return; |
| 490 | |
| 491 | QList<RegistryHandle *> proxies = notifyProxies.keys(rh); |
| 492 | while (!proxies.isEmpty()) { |
| 493 | notifyProxies.remove(proxies.first(), rh); |
| 494 | removeHandle(Handle(proxies.takeFirst())); |
| 495 | } |
| 496 | |
| 497 | handles.remove(rh->path); |
| 498 | |
| 499 | closeRegistryKey(rh); |
| 500 | |
| 501 | delete rh; |
| 502 | } |
| 503 | |
| 504 | void RegistryLayer::closeRegistryKey(RegistryHandle *handle) |
| 505 | { |
| 506 | QMutexLocker locker(&localLock); |
| 507 | |
| 508 | if (!hKeys.contains(handle)) |
| 509 | return; |
| 510 | |
| 511 | HKEY key = hKeys.take(handle); |
| 512 | |
| 513 | // Check if other handles are using this registry key. |
| 514 | if (!hKeys.keys(key).isEmpty()) |
| 515 | return; |
| 516 | |
| 517 | if (waitHandles.contains(key)) { |
| 518 | //QPair<::HANDLE, ::HANDLE> wait = waitHandles.take(key); |
| 519 | HandlePair wait = waitHandles.take(key); |
| 520 | |
| 521 | UnregisterWait(wait.second); |
| 522 | CloseHandle(wait.first); |
| 523 | } |
| 524 | |
| 525 | RegCloseKey(key); |
| 526 | } |
| 527 | |
| 528 | static LONG qRegDeleteTree(HKEY hKey, LPCTSTR lpSubKey) |
| 529 | { |
| 530 | HKEY key; |
| 531 | long result = RegOpenKeyEx(hKey, lpSubKey, 0, KEY_ALL_ACCESS, &key); |
| 532 | if (result != ERROR_SUCCESS) { |
| 533 | if (result == ERROR_FILE_NOT_FOUND) |
| 534 | return ERROR_SUCCESS; |
| 535 | else |
| 536 | return result; |
| 537 | } |
| 538 | |
| 539 | do { |
| 540 | TCHAR subKey[MAX_KEY_LENGTH]; |
| 541 | DWORD subKeySize = MAX_KEY_LENGTH; |
| 542 | |
| 543 | result = RegEnumKeyEx(key, 0, subKey, &subKeySize, 0, 0, 0, 0); |
| 544 | if (result == ERROR_NO_MORE_ITEMS) |
| 545 | break; |
| 546 | |
| 547 | result = qRegDeleteTree(key, subKey); |
| 548 | } while (result == ERROR_SUCCESS); |
| 549 | |
| 550 | RegCloseKey(key); |
| 551 | |
| 552 | if (result != ERROR_NO_MORE_ITEMS && result != ERROR_SUCCESS) |
| 553 | return result; |
| 554 | |
| 555 | return RegDeleteKey(hKey, lpSubKey); |
| 556 | } |
| 557 | |
| 558 | bool RegistryLayer::removeRegistryValue(RegistryHandle *handle, const QString &subPath) |
| 559 | { |
| 560 | QMutexLocker locker(&localLock); |
| 561 | |
| 562 | QString path(subPath); |
| 563 | while (path.endsWith(QLatin1Char('/'))) |
| 564 | path.chop(1); |
| 565 | |
| 566 | int index = path.lastIndexOf(QLatin1Char('/'), -1); |
| 567 | |
| 568 | bool createdHandle = false; |
| 569 | |
| 570 | RegistryHandle *rh; |
| 571 | QString value; |
| 572 | if (index == -1) { |
| 573 | rh = handle; |
| 574 | value = path; |
| 575 | } else { |
| 576 | // want a value that is in a sub path under handle |
| 577 | value = path.mid(index + 1); |
| 578 | path.truncate(index); |
| 579 | if (path.isEmpty()) |
| 580 | path.append(QLatin1Char('/')); |
| 581 | |
| 582 | rh = registryHandle(item(handle ? Handle(handle) : InvalidHandle, path)); |
| 583 | |
| 584 | createdHandle = true; |
| 585 | } |
| 586 | |
| 587 | openRegistryKey(rh); |
| 588 | if (!hKeys.contains(rh)) { |
| 589 | if (createdHandle) |
| 590 | removeHandle(Handle(rh)); |
| 591 | |
| 592 | return false; |
| 593 | } |
| 594 | |
| 595 | HKEY key = hKeys.value(rh); |
| 596 | |
| 597 | long result = RegDeleteValue(key, reinterpret_cast<const wchar_t*>(value.utf16())); |
| 598 | if (result == ERROR_FILE_NOT_FOUND) { |
| 599 | result = qRegDeleteTree(key, reinterpret_cast<const wchar_t*>(value.utf16())); |
| 600 | if (result == ERROR_SUCCESS) { |
| 601 | const QString rootPath = rh->path; |
| 602 | |
| 603 | QList<QString> paths = handles.keys(); |
| 604 | while (!paths.isEmpty()) { |
| 605 | QString p = paths.takeFirst(); |
| 606 | |
| 607 | if (p.startsWith(rootPath)) |
| 608 | closeRegistryKey(handles.value(p)); |
| 609 | } |
| 610 | } |
| 611 | if (result != ERROR_SUCCESS) |
| 612 | qDebug() << "RegDeleteTree failed with error" << result; |
| 613 | } else if (result != ERROR_SUCCESS) { |
| 614 | qDebug() << "RegDeleteValue failed with error" << result; |
| 615 | } |
| 616 | |
| 617 | if (createdHandle) |
| 618 | removeHandle(Handle(rh)); |
| 619 | |
| 620 | return result == ERROR_SUCCESS; |
| 621 | } |
| 622 | |
| 623 | bool RegistryLayer::setValue(QValueSpacePublisher *creator, Handle handle, const QVariant &data) |
| 624 | { |
| 625 | return setValue(creator, handle, QString(), data); |
| 626 | } |
| 627 | |
| 628 | bool RegistryLayer::setValue(QValueSpacePublisher *creator, Handle handle, const QString &subPath, |
| 629 | const QVariant &data) |
| 630 | { |
| 631 | QMutexLocker locker(&localLock); |
| 632 | |
| 633 | RegistryHandle *rh = registryHandle(handle); |
| 634 | if (!rh) |
| 635 | return false; |
| 636 | |
| 637 | QString path(subPath); |
| 638 | while (path.endsWith(QLatin1Char('/'))) |
| 639 | path.chop(1); |
| 640 | |
| 641 | int index = path.lastIndexOf(QLatin1Char('/'), -1); |
| 642 | |
| 643 | bool createdHandle = false; |
| 644 | |
| 645 | QString value; |
| 646 | if (index == -1) { |
| 647 | value = path; |
| 648 | } else { |
| 649 | // want a value that is in a sub path under handle |
| 650 | value = path.mid(index + 1); |
| 651 | path.truncate(index); |
| 652 | |
| 653 | if (path.isEmpty()) |
| 654 | path.append(QLatin1Char('/')); |
| 655 | |
| 656 | rh = registryHandle(item(Handle(rh), path)); |
| 657 | createdHandle = true; |
| 658 | } |
| 659 | |
| 660 | if (!value.isEmpty()) { |
| 661 | QString fullPath = rh->path; |
| 662 | if (!fullPath.endsWith(QLatin1Char('/'))) |
| 663 | fullPath.append(QLatin1Char('/')); |
| 664 | fullPath.append(value); |
| 665 | |
| 666 | RegistryHandle *fullHandle = registryHandle(item(InvalidHandle, fullPath)); |
| 667 | if (fullHandle) { |
| 668 | fullHandle->valueHandle = true; |
| 669 | removeHandle(Handle(fullHandle)); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | if (createRegistryKey(rh)) { |
| 674 | if (!creators[creator].contains(rh->path)) |
| 675 | creators[creator].append(rh->path); |
| 676 | } |
| 677 | if (!hKeys.contains(rh)) { |
| 678 | if (createdHandle) |
| 679 | removeHandle(Handle(rh)); |
| 680 | |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | HKEY key = hKeys.value(rh); |
| 685 | |
| 686 | long result; |
| 687 | |
| 688 | switch (data.type()) { |
| 689 | case QVariant::UInt: { |
| 690 | DWORD temp = data.toUInt(); |
| 691 | result = RegSetValueEx(key, reinterpret_cast<const wchar_t*>(value.utf16()), 0, |
| 692 | REG_DWORD, reinterpret_cast<const BYTE *>(&temp), sizeof(DWORD)); |
| 693 | break; |
| 694 | } |
| 695 | case QVariant::ULongLong: { |
| 696 | quint64 temp = data.toULongLong(); |
| 697 | result = RegSetValueEx(key, reinterpret_cast<const wchar_t*>(value.utf16()), 0, |
| 698 | REG_QWORD, reinterpret_cast<const BYTE *>(&temp), sizeof(quint64)); |
| 699 | break; |
| 700 | } |
| 701 | case QVariant::String: { |
| 702 | // This may be wrong! |
| 703 | QString tempString = data.toString(); |
| 704 | int length = tempString.length() + 1; |
| 705 | wchar_t *temp = new wchar_t[length]; |
| 706 | tempString.toWCharArray(temp); |
| 707 | temp[length - 1] = L'\0'; |
| 708 | result = RegSetValueEx(key, reinterpret_cast<const wchar_t*>(value.utf16()), 0, |
| 709 | REG_SZ, reinterpret_cast<const BYTE *>(temp), length * sizeof(wchar_t)); |
| 710 | delete[] temp; |
| 711 | break; |
| 712 | } |
| 713 | case QVariant::StringList: { |
| 714 | const QString joined = data.toStringList().join(QString(QLatin1Char('\0'))); |
| 715 | int length = joined.length() + 2; |
| 716 | wchar_t *temp = new wchar_t[length]; |
| 717 | joined.toWCharArray(temp); |
| 718 | temp[length - 2] = L'\0'; |
| 719 | temp[length - 1] = L'\0'; |
| 720 | result = RegSetValueEx(key, reinterpret_cast<const wchar_t*>(value.utf16()), 0, |
| 721 | REG_MULTI_SZ, reinterpret_cast<const BYTE *>(temp), |
| 722 | length * sizeof(wchar_t)); |
| 723 | delete[] temp; |
| 724 | break; |
| 725 | } |
| 726 | case QVariant::ByteArray: { |
| 727 | QByteArray temp = data.toByteArray(); |
| 728 | result = RegSetValueEx(key, reinterpret_cast<const wchar_t*>(value.utf16()), 0, |
| 729 | REG_BINARY, reinterpret_cast<const BYTE *>(temp.constData()), |
| 730 | temp.length()); |
| 731 | break; |
| 732 | } |
| 733 | default: { |
| 734 | QByteArray temp; |
| 735 | QDataStream stream(&temp, QIODevice::WriteOnly | QIODevice::Truncate); |
| 736 | stream << data; |
| 737 | result = RegSetValueEx(key, reinterpret_cast<const wchar_t*>(value.utf16()), 0, |
| 738 | REG_NONE, reinterpret_cast<const BYTE *>(temp.constData()), |
| 739 | temp.length()); |
| 740 | break; |
| 741 | } |
| 742 | }; |
| 743 | |
| 744 | QString fullPath(rh->path); |
| 745 | if (fullPath != QLatin1String("/")) |
| 746 | fullPath.append(QLatin1Char('/')); |
| 747 | |
| 748 | fullPath.append(value); |
| 749 | |
| 750 | if (!creators[creator].contains(fullPath)) |
| 751 | creators[creator].append(fullPath); |
| 752 | |
| 753 | if (createdHandle) |
| 754 | removeHandle(Handle(rh)); |
| 755 | |
| 756 | return result == ERROR_SUCCESS; |
| 757 | } |
| 758 | |
| 759 | void RegistryLayer::sync() |
| 760 | { |
| 761 | QMutexLocker locker(&localLock); |
| 762 | |
| 763 | // Wait for change notification callbacks before returning |
| 764 | QEventLoop loop; |
| 765 | connect(this, SIGNAL(handleChanged(quintptr)), &loop, SLOT(quit())); |
| 766 | bool wait = false; |
| 767 | |
| 768 | QList<HKEY> keys = hKeys.values(); |
| 769 | while (!keys.isEmpty()) { |
| 770 | HKEY key = keys.takeFirst(); |
| 771 | |
| 772 | if (!wait && waitHandles.contains(key)) |
| 773 | wait = true; |
| 774 | |
| 775 | RegFlushKey(key); |
| 776 | } |
| 777 | |
| 778 | if (wait) { |
| 779 | locker.unlock(); |
| 780 | QTimer::singleShot(1000, &loop, SLOT(quit())); |
| 781 | loop.exec(); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | void RegistryLayer::emitHandleChanged(void *k) |
| 786 | { |
| 787 | QMutexLocker locker(&localLock); |
| 788 | |
| 789 | HKEY key = reinterpret_cast<HKEY>(k); |
| 790 | |
| 791 | QList<RegistryHandle *> changedHandles = hKeys.keys(key); |
| 792 | if (changedHandles.isEmpty()) { |
| 793 | if (waitHandles.contains(key)) { |
| 794 | //QPair<::HANDLE, ::HANDLE> wait = waitHandles.take(key); |
| 795 | HandlePair wait = waitHandles.take(key); |
| 796 | |
| 797 | UnregisterWait(wait.second); |
| 798 | CloseHandle(wait.first); |
| 799 | return; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | while (!changedHandles.isEmpty()) { |
| 804 | RegistryHandle *handle = changedHandles.takeFirst(); |
| 805 | emit handleChanged(Handle(handle)); |
| 806 | |
| 807 | // Emit signal for handles that this handle is proxying for. |
| 808 | foreach (RegistryHandle *proxied, notifyProxies.values(handle)) { |
| 809 | openRegistryKey(proxied); |
| 810 | if (hKeys.contains(proxied)) { |
| 811 | notifyProxies.remove(handle, proxied); |
| 812 | removeHandle(Handle(handle)); |
| 813 | setProperty(Handle(proxied), Publish); |
| 814 | |
| 815 | emit handleChanged(Handle(proxied)); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | if (waitHandles.contains(key)) { |
| 821 | //QPair<::HANDLE, ::HANDLE> wait = waitHandles.take(key); |
| 822 | HandlePair wait = waitHandles.take(key); |
| 823 | |
| 824 | ::HANDLE event = wait.first; |
| 825 | |
| 826 | UnregisterWait(wait.second); |
| 827 | |
| 828 | long result = RegNotifyChangeKeyValue(key, true, REG_NOTIFY_CHANGE_NAME | |
| 829 | REG_NOTIFY_CHANGE_ATTRIBUTES | |
| 830 | REG_NOTIFY_CHANGE_LAST_SET, |
| 831 | event, true); |
| 832 | |
| 833 | if (result != ERROR_SUCCESS) { |
| 834 | if (result == ERROR_KEY_DELETED || result == ERROR_INVALID_PARAMETER) { |
| 835 | CloseHandle(event); |
| 836 | |
| 837 | QList<RegistryHandle *> changedHandles = hKeys.keys(key); |
| 838 | |
| 839 | for (int i = 0; i < changedHandles.count(); ++i) |
| 840 | hKeys.remove(changedHandles.at(i)); |
| 841 | |
| 842 | RegCloseKey(key); |
| 843 | |
| 844 | for (int i = 0; i < changedHandles.count(); ++i) |
| 845 | setProperty(Handle(changedHandles.at(i)), Publish); |
| 846 | } else { |
| 847 | qDebug() << "RegNotifyChangeKeyValue failed with error" << result; |
| 848 | } |
| 849 | |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | ::HANDLE waitHandle; |
| 854 | |
| 855 | if (!RegisterWaitForSingleObject(&waitHandle, event, m_callback, key, |
| 856 | INFINITE, WT_EXECUTEDEFAULT)) { |
| 857 | qDebug() << "RegisterWaitForSingleObject failed with error" << GetLastError(); |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | //waitHandles.insert(key, QPair<::HANDLE, ::HANDLE>(event, waitHandle)); |
| 862 | waitHandles.insert(key, HandlePair(event, waitHandle)); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | void RegistryLayer::openRegistryKey(RegistryHandle *handle) |
| 867 | { |
| 868 | QMutexLocker locker(&localLock); |
| 869 | |
| 870 | if (!handle) |
| 871 | return; |
| 872 | |
| 873 | // Check if HKEY for this handle already exists. |
| 874 | if (hKeys.contains(handle)) |
| 875 | return; |
| 876 | |
| 877 | const QString fullPath = qConvertPath(m_basePath + handle->path); |
| 878 | |
| 879 | // Attempt to open registry key path |
| 880 | HKEY key; |
| 881 | long result = RegOpenKeyEx(HKEY_CURRENT_USER, |
| 882 | reinterpret_cast<const wchar_t*>(fullPath.utf16()), |
| 883 | 0, KEY_ALL_ACCESS, &key); |
| 884 | |
| 885 | if (result == ERROR_SUCCESS) { |
| 886 | hKeys.insert(handle, key); |
| 887 | } else if (result == ERROR_FILE_NOT_FOUND) { |
| 888 | // Key for handle does not exist in Registry, check if it is a value handle. |
| 889 | int index = handle->path.lastIndexOf(QLatin1Char('/'), -1); |
| 890 | |
| 891 | const QString parentPath = handle->path.left(index); |
| 892 | const QString valueName = handle->path.mid(index + 1); |
| 893 | |
| 894 | RegistryHandle *parentHandle = registryHandle(item(InvalidHandle, parentPath)); |
| 895 | if (!parentHandle) |
| 896 | return; |
| 897 | |
| 898 | openRegistryKey(parentHandle); |
| 899 | if (!hKeys.contains(parentHandle)) { |
| 900 | removeHandle(Handle(parentHandle)); |
| 901 | return; |
| 902 | } |
| 903 | |
| 904 | // Check if value exists. |
| 905 | if (!children(Handle(parentHandle)).contains(valueName)) { |
| 906 | removeHandle(Handle(parentHandle)); |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | handle->valueHandle = true; |
| 911 | |
| 912 | hKeys.insert(handle, hKeys.value(parentHandle)); |
| 913 | |
| 914 | removeHandle(Handle(parentHandle)); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | bool RegistryLayer::createRegistryKey(RegistryHandle *handle) |
| 919 | { |
| 920 | QMutexLocker locker(&localLock); |
| 921 | |
| 922 | // Check if HKEY for this handle already exists. |
| 923 | if (hKeys.contains(handle)) |
| 924 | return false; |
| 925 | |
| 926 | const QString fullPath = qConvertPath(m_basePath + handle->path); |
| 927 | |
| 928 | // Attempt to open registry key path |
| 929 | HKEY key; |
| 930 | DWORD disposition; |
| 931 | long result = RegCreateKeyEx(HKEY_CURRENT_USER, |
| 932 | reinterpret_cast<const wchar_t*>(fullPath.utf16()), |
| 933 | 0, 0, |
| 934 | (m_volatileKeys ? REG_OPTION_VOLATILE : REG_OPTION_NON_VOLATILE), |
| 935 | KEY_ALL_ACCESS, 0, &key, &disposition); |
| 936 | |
| 937 | if (result == ERROR_SUCCESS) |
| 938 | hKeys.insert(handle, key); |
| 939 | |
| 940 | return disposition == REG_CREATED_NEW_KEY; |
| 941 | } |
| 942 | |
| 943 | bool RegistryLayer::removeSubTree(QValueSpacePublisher *creator, Handle handle) |
| 944 | { |
| 945 | QMutexLocker locker(&localLock); |
| 946 | |
| 947 | RegistryHandle *rh = registryHandle(handle); |
| 948 | if (!rh) |
| 949 | return false; |
| 950 | |
| 951 | QList<QString> paths = creators.value(creator); |
| 952 | |
| 953 | while (!paths.isEmpty()) { |
| 954 | QString item = paths.takeFirst(); |
| 955 | if (!item.startsWith(rh->path)) |
| 956 | continue; |
| 957 | |
| 958 | removeRegistryValue(0, item); |
| 959 | creators[creator].removeOne(item); |
| 960 | |
| 961 | int index = item.lastIndexOf(QLatin1Char('/')); |
| 962 | if (index == -1) |
| 963 | continue; |
| 964 | |
| 965 | item.truncate(index); |
| 966 | if (!paths.contains(item)) |
| 967 | paths.append(item); |
| 968 | } |
| 969 | |
| 970 | pruneEmptyKeys(rh); |
| 971 | |
| 972 | return true; |
| 973 | } |
| 974 | |
| 975 | void RegistryLayer::pruneEmptyKeys(RegistryHandle *handle) |
| 976 | { |
| 977 | QMutexLocker locker(&localLock); |
| 978 | |
| 979 | if (!children(Handle(handle)).isEmpty()) |
| 980 | return; |
| 981 | |
| 982 | QString path = handle->path; |
| 983 | |
| 984 | while (path != QLatin1String("/")) { |
| 985 | int index = path.lastIndexOf(QLatin1Char('/'), -1); |
| 986 | |
| 987 | QString value = path.mid(index + 1); |
| 988 | |
| 989 | path.truncate(index); |
| 990 | if (path.isEmpty()) |
| 991 | path.append(QLatin1Char('/')); |
| 992 | |
| 993 | RegistryHandle *rh = registryHandle(item(InvalidHandle, path)); |
| 994 | |
| 995 | openRegistryKey(rh); |
| 996 | if (!hKeys.contains(rh)) { |
| 997 | removeHandle(Handle(rh)); |
| 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | HKEY key = hKeys.value(rh); |
| 1002 | |
| 1003 | long result = RegDeleteKey(key, reinterpret_cast<const wchar_t*>(value.utf16())); |
| 1004 | if (result == ERROR_SUCCESS) { |
| 1005 | QList<QString> paths = handles.keys(); |
| 1006 | while (!paths.isEmpty()) { |
| 1007 | const QString p = paths.takeFirst(); |
| 1008 | |
| 1009 | if (p.startsWith(path)) |
| 1010 | closeRegistryKey(handles.value(p)); |
| 1011 | } |
| 1012 | } else if (result != ERROR_FILE_NOT_FOUND) { |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | bool hasChildren = !children(Handle(rh)).isEmpty(); |
| 1017 | |
| 1018 | removeHandle(Handle(rh)); |
| 1019 | |
| 1020 | if (hasChildren) |
| 1021 | break; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | bool RegistryLayer::removeValue(QValueSpacePublisher *creator, |
| 1026 | Handle handle, |
| 1027 | const QString &subPath) |
| 1028 | { |
| 1029 | QMutexLocker locker(&localLock); |
| 1030 | |
| 1031 | QString fullPath; |
| 1032 | |
| 1033 | if (handle == InvalidHandle) { |
| 1034 | fullPath = subPath; |
| 1035 | } else { |
| 1036 | RegistryHandle *rh = registryHandle(handle); |
| 1037 | if (!rh) |
| 1038 | return false; |
| 1039 | |
| 1040 | if (subPath == QLatin1String("/")) |
| 1041 | fullPath = rh->path; |
| 1042 | else if (rh->path.endsWith(QLatin1Char('/')) && subPath.startsWith(QLatin1Char('/'))) |
| 1043 | fullPath = rh->path + subPath.mid(1); |
| 1044 | else if (!rh->path.endsWith(QLatin1Char('/')) && !subPath.startsWith(QLatin1Char('/'))) |
| 1045 | fullPath = rh->path + QLatin1Char('/') + subPath; |
| 1046 | else |
| 1047 | fullPath = rh->path + subPath; |
| 1048 | } |
| 1049 | |
| 1050 | // permanent layer always removes items even if our records show that creator does not own it. |
| 1051 | if (!creators[creator].contains(fullPath) && (layerOptions() & QValueSpace::TransientLayer)) |
| 1052 | return false; |
| 1053 | |
| 1054 | removeRegistryValue(0, fullPath); |
| 1055 | creators[creator].removeOne(fullPath); |
| 1056 | |
| 1057 | return true; |
| 1058 | } |
| 1059 | |
| 1060 | void RegistryLayer::addWatch(QValueSpacePublisher *, Handle) |
| 1061 | { |
| 1062 | } |
| 1063 | |
| 1064 | void RegistryLayer::removeWatches(QValueSpacePublisher *, Handle) |
| 1065 | { |
| 1066 | } |
| 1067 | |
| 1068 | bool RegistryLayer::supportsInterestNotification() const |
| 1069 | { |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
| 1073 | bool RegistryLayer::notifyInterest(Handle, bool) |
| 1074 | { |
| 1075 | return false; |
| 1076 | } |
| 1077 | |
| 1078 | QT_END_NAMESPACE |
| 1079 | |
| 1080 | #endif // Q_OS_WIN |