KConfig

registry_win.cpp
1/*
2 SPDX-FileCopyrightText: 2022 g10 Code GmbH
3 SPDX-FileContributor: Andre Heinecke <aheinecke@gnupg.com>
4 SPDX-FileContributor: Tobias Fella <tobias.fella@gnupg.com>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "registry_win_p.h"
10
11#include <windows.h>
12
13#include <QSettings>
14
15enum Registry {
16 HKLM,
17 HKCU,
18};
19
20static void parseRegValues(const QString &groupName, QSettings &settings, KEntryMap &entryMap, bool groupImmutable)
21{
22 for (auto &key : settings.childKeys()) {
23 KEntryMap::EntryOptions entryOptions = KEntryMap::EntryDefault;
24 const auto value = settings.value(key).toString().toStdWString();
25 const auto requiredSize = ExpandEnvironmentStringsW(value.c_str(), nullptr, 0);
26 std::vector<char16_t> expanded(requiredSize, char16_t(0));
27 ExpandEnvironmentStringsW(value.c_str(), (LPWSTR)expanded.data(), expanded.size());
28 if (key.endsWith(QStringLiteral("[$i]"))) {
29 key.chop(4);
30 entryOptions |= KEntryMap::EntryImmutable;
31 }
32 if (groupImmutable) {
33 entryOptions |= KEntryMap::EntryImmutable;
34 }
35 key = key.replace(QLatin1Char('\\'), QLatin1Char('/'));
36 if (entryMap.getEntryOption(groupName, key.toUtf8(), KEntryMap::SearchDefaults, KEntryMap::EntryImmutable)) {
37 continue;
38 }
39 entryMap.setEntry(groupName, key.toUtf8(), QString::fromUtf16(&expanded[0]), entryOptions);
40 }
41}
42
43static void parseRegSubkeys(const QString &baseGroup, QSettings &settings, KEntryMap &entryMap, bool immutable)
44{
45 parseRegValues(baseGroup, settings, entryMap, immutable);
46
47 for (auto &group : settings.childGroups()) {
48 bool groupImmutable = immutable;
49 QString groupWithoutSuffix = group;
50 if (group.endsWith(QStringLiteral("[$i]"))) {
51 groupWithoutSuffix.chop(4);
52 groupImmutable = true;
53 }
54 settings.beginGroup(group);
55 QString groupName = (baseGroup == QStringLiteral("<default>") ? QString() : (baseGroup + QLatin1Char('\x1d'))) + groupWithoutSuffix;
56 parseRegSubkeys(groupName, settings, entryMap, groupImmutable);
57 settings.endGroup();
58 entryMap.setEntry(groupName, {}, QByteArray(), groupImmutable ? KEntryMap::EntryImmutable : KEntryMap::EntryOptions());
59 }
60}
61
62static void parseRegistry(const QString &regKey, KEntryMap &entryMap, Registry registry)
63{
64 QString registryPath = (registry == HKCU ? QStringLiteral("HKEY_CURRENT_USER\\") : QStringLiteral("HKEY_LOCAL_MACHINE\\")) + regKey;
65 QSettings settings(registryPath, QSettings::NativeFormat);
66 parseRegSubkeys(QStringLiteral("<default>"), settings, entryMap, false);
67}
68
69void WindowsRegistry::parse(const QString &regKey, KEntryMap &entryMap)
70{
71 parseRegistry(regKey, entryMap, HKLM);
72 parseRegistry(regKey, entryMap, HKCU);
73}
void beginGroup(QAnyStringView prefix)
QStringList childGroups() const const
QStringList childKeys() const const
void endGroup()
QVariant value(QAnyStringView key) const const
void chop(qsizetype n)
QString fromUtf16(const char16_t *unicode, qsizetype size)
std::wstring toStdWString() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:55:16 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.