From 05a829f923a88e69b2ceaa372820a2dcb8c083cd Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 27 Sep 2019 13:36:38 +0200 Subject: Win32: Consolidate registry code Add a RAII class for registry keys and use it throughout the code base. Change-Id: I666b2fbb790f83436443101d6bc1e3c0525e78df Reviewed-by: Volker Hilsheimer --- src/corelib/global/qglobal.cpp | 29 ++++---- src/corelib/io/qsettings_win.cpp | 2 + src/corelib/kernel/kernel.pri | 6 +- src/corelib/kernel/qwinregistry.cpp | 120 ++++++++++++++++++++++++++++++ src/corelib/kernel/qwinregistry_p.h | 89 ++++++++++++++++++++++ src/corelib/time/qtimezoneprivate_win.cpp | 81 +++++++------------- 6 files changed, 253 insertions(+), 74 deletions(-) create mode 100644 src/corelib/kernel/qwinregistry.cpp create mode 100644 src/corelib/kernel/qwinregistry_p.h (limited to 'src/corelib') diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 17aab17fe4..6ae5bf299a 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -47,12 +47,11 @@ #include "qoperatingsystemversion.h" #include "qoperatingsystemversion_p.h" #if defined(Q_OS_WIN) || defined(Q_OS_CYGWIN) || defined(Q_OS_WINRT) -#include "qoperatingsystemversion_win_p.h" -# if QT_CONFIG(settings) -# include "qsettings.h" -# include "qvariant.h" +# include "qoperatingsystemversion_win_p.h" +# ifndef Q_OS_WINRT +# include "private/qwinregistry_p.h" # endif -#endif +#endif // Q_OS_WIN || Q_OS_CYGWIN #include #include @@ -2190,28 +2189,25 @@ const QSysInfo::WinVersion QSysInfo::WindowsVersion = QSysInfo::windowsVersion() QT_WARNING_POP #endif -static QString readRegistryString(const QString &key, const QString &subKey) +static QString readVersionRegistryString(const wchar_t *subKey) { -#if QT_CONFIG(settings) - QSettings settings(key, QSettings::NativeFormat); - return settings.value(subKey).toString(); +#if !defined(QT_BUILD_QMAKE) && !defined(Q_OS_WINRT) + return QWinRegistryKey(HKEY_LOCAL_MACHINE, LR"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)") + .stringValue(subKey); #else - Q_UNUSED(key); - Q_UNUSED(subKey); - return QString(); + Q_UNUSED(subKey); + return QString(); #endif } -static inline QString windowsVersionKey() { return QStringLiteral(R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion)"); } - static inline QString windows10ReleaseId() { - return readRegistryString(windowsVersionKey(), QStringLiteral("ReleaseId")); + return readVersionRegistryString(L"ReleaseId"); } static inline QString windows7Build() { - return readRegistryString(windowsVersionKey(), QStringLiteral("CurrentBuild")); + return readVersionRegistryString(L"CurrentBuild"); } static QString winSp_helper() @@ -3078,6 +3074,7 @@ QByteArray QSysInfo::machineUniqueId() } #elif defined(Q_OS_WIN) && !defined(Q_OS_WINRT) // Let's poke at the registry + // ### Qt 6: Use new helpers from qwinregistry.cpp (once bootstrap builds are obsolete) HKEY key = NULL; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Cryptography", 0, KEY_READ | KEY_WOW64_64KEY, &key) == ERROR_SUCCESS) { diff --git a/src/corelib/io/qsettings_win.cpp b/src/corelib/io/qsettings_win.cpp index 679212ea21..6eb318006e 100644 --- a/src/corelib/io/qsettings_win.cpp +++ b/src/corelib/io/qsettings_win.cpp @@ -136,6 +136,8 @@ static void mergeKeySets(NameSet *dest, const QStringList &src) ** Wrappers for the insane windows registry API */ +// ### Qt 6: Use new helpers from qwinregistry.cpp (once bootstrap builds are obsolete) + // Open a key with the specified "perms". // "access" is to explicitly use the 32- or 64-bit branch. static HKEY openKey(HKEY parentHandle, REGSAM perms, const QString &rSubKey, REGSAM access = 0) diff --git a/src/corelib/kernel/kernel.pri b/src/corelib/kernel/kernel.pri index 789bcb7927..bd3cabc01a 100644 --- a/src/corelib/kernel/kernel.pri +++ b/src/corelib/kernel/kernel.pri @@ -88,8 +88,10 @@ win32 { SOURCES += kernel/qeventdispatcher_winrt.cpp HEADERS += kernel/qeventdispatcher_winrt_p.h } else { - SOURCES += kernel/qeventdispatcher_win.cpp - HEADERS += kernel/qeventdispatcher_win_p.h + SOURCES += kernel/qeventdispatcher_win.cpp \ + kernel/qwinregistry.cpp + HEADERS += kernel/qeventdispatcher_win_p.h \ + kernel/qwinregistry_p.h } !winrt: LIBS_PRIVATE += -lversion diff --git a/src/corelib/kernel/qwinregistry.cpp b/src/corelib/kernel/qwinregistry.cpp new file mode 100644 index 0000000000..6566dd3c76 --- /dev/null +++ b/src/corelib/kernel/qwinregistry.cpp @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qwinregistry_p.h" + +#include + +#include + +QT_BEGIN_NAMESPACE + +QWinRegistryKey::QWinRegistryKey() : + m_key(nullptr) +{ +} + +// Open a key with the specified permissions (KEY_READ/KEY_WRITE). +// "access" is to explicitly use the 32- or 64-bit branch. +QWinRegistryKey::QWinRegistryKey(HKEY parentHandle, QStringView subKey, + REGSAM permissions, REGSAM access) +{ + if (RegOpenKeyEx(parentHandle, reinterpret_cast(subKey.utf16()), + 0, permissions | access, &m_key) != ERROR_SUCCESS) { + m_key = nullptr; + } +} + +QWinRegistryKey::~QWinRegistryKey() +{ + close(); +} + +void QWinRegistryKey::close() +{ + if (isValid()) { + RegCloseKey(m_key); + m_key = nullptr; + } +} + +QString QWinRegistryKey::stringValue(QStringView subKey) const +{ + QString result; + if (!isValid()) + return result; + DWORD type; + DWORD size; + auto subKeyC = reinterpret_cast(subKey.utf16()); + if (RegQueryValueEx(m_key, subKeyC, nullptr, &type, nullptr, &size) != ERROR_SUCCESS + || (type != REG_SZ && type != REG_EXPAND_SZ) || size <= 2) { + return result; + } + // Reserve more for rare cases where trailing '\0' are missing in registry, + // otherwise chop off the '\0' received. + QString buffer(int(size / sizeof(wchar_t)), Qt::Uninitialized); + if (RegQueryValueEx(m_key, subKeyC, nullptr, &type, + reinterpret_cast(buffer.data()), &size) == ERROR_SUCCESS) { + if (buffer.endsWith(QChar::Null)) + buffer.chop(1); + } else { + buffer.clear(); + } + return buffer; +} + +QPair QWinRegistryKey::dwordValue(QStringView subKey) const +{ + if (!isValid()) + return qMakePair(0, false); + DWORD type; + auto subKeyC = reinterpret_cast(subKey.utf16()); + if (RegQueryValueEx(m_key, subKeyC, nullptr, &type, nullptr, nullptr) != ERROR_SUCCESS + || type != REG_DWORD) { + return qMakePair(0, false); + } + DWORD value = 0; + DWORD size = sizeof(value); + const bool ok = + RegQueryValueEx(m_key, subKeyC, nullptr, nullptr, + reinterpret_cast(&value), &size) == ERROR_SUCCESS; + return qMakePair(value, ok); +} + +QT_END_NAMESPACE diff --git a/src/corelib/kernel/qwinregistry_p.h b/src/corelib/kernel/qwinregistry_p.h new file mode 100644 index 0000000000..d249a97988 --- /dev/null +++ b/src/corelib/kernel/qwinregistry_p.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWINREGISTRY_H +#define QWINREGISTRY_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_CORE_EXPORT QWinRegistryKey +{ +public: + Q_DISABLE_COPY(QWinRegistryKey) + + QWinRegistryKey(); + explicit QWinRegistryKey(HKEY parentHandle, QStringView subKey, + REGSAM permissions = KEY_READ, REGSAM access = 0); + ~QWinRegistryKey(); + + QWinRegistryKey(QWinRegistryKey &&other) noexcept { swap(other); } + QWinRegistryKey &operator=(QWinRegistryKey &&other) noexcept { swap(other); return *this; } + + void swap(QWinRegistryKey &other) noexcept { qSwap(m_key, other.m_key); } + + bool isValid() const { return m_key != nullptr; } + operator HKEY() const { return m_key; } + void close(); + + QString stringValue(QStringView subKey) const; + QPair dwordValue(QStringView subKey) const; + +private: + HKEY m_key; +}; + +QT_END_NAMESPACE + +#endif // QWINREGISTRY_H diff --git a/src/corelib/time/qtimezoneprivate_win.cpp b/src/corelib/time/qtimezoneprivate_win.cpp index 5a480222e0..0fec5355b2 100644 --- a/src/corelib/time/qtimezoneprivate_win.cpp +++ b/src/corelib/time/qtimezoneprivate_win.cpp @@ -46,13 +46,14 @@ #include -QT_BEGIN_NAMESPACE - #ifndef Q_OS_WINRT +#include // The registry-based timezone backend is not available on WinRT, which falls back to equivalent APIs. #define QT_USE_REGISTRY_TIMEZONE 1 #endif +QT_BEGIN_NAMESPACE + /* Private @@ -71,8 +72,8 @@ QT_BEGIN_NAMESPACE // Vista introduced support for historic data, see MSDN docs on DYNAMIC_TIME_ZONE_INFORMATION // http://msdn.microsoft.com/en-gb/library/windows/desktop/ms724253%28v=vs.85%29.aspx #ifdef QT_USE_REGISTRY_TIMEZONE -static const char tzRegPath[] = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones"; -static const char currTzRegPath[] = "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation"; +static const wchar_t tzRegPath[] = LR"(SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones)"; +static const wchar_t currTzRegPath[] = LR"(SYSTEM\CurrentControlSet\Control\TimeZoneInformation)"; #endif enum { @@ -138,27 +139,6 @@ bool equalTzi(const TIME_ZONE_INFORMATION &tzi1, const TIME_ZONE_INFORMATION &tz } #ifdef QT_USE_REGISTRY_TIMEZONE -bool openRegistryKey(const QString &keyPath, HKEY *key) -{ - return RegOpenKeyEx(HKEY_LOCAL_MACHINE, reinterpret_cast(keyPath.utf16()), - 0, KEY_READ, key) == ERROR_SUCCESS; -} - -QString readRegistryString(const HKEY &key, const wchar_t *value) -{ - wchar_t buffer[MAX_PATH] = {0}; - DWORD size = sizeof(wchar_t) * MAX_PATH; - RegQueryValueEx(key, value, nullptr, nullptr, reinterpret_cast(buffer), &size); - return QString::fromWCharArray(buffer); -} - -int readRegistryValue(const HKEY &key, const wchar_t *value) -{ - DWORD buffer; - DWORD size = sizeof(buffer); - RegQueryValueEx(key, value, nullptr, nullptr, reinterpret_cast(&buffer), &size); - return buffer; -} QWinTimeZonePrivate::QWinTransitionRule readRegistryRule(const HKEY &key, const wchar_t *value, bool *ok) @@ -185,12 +165,11 @@ TIME_ZONE_INFORMATION getRegistryTzi(const QByteArray &windowsId, bool *ok) TIME_ZONE_INFORMATION tzi; REG_TZI_FORMAT regTzi; DWORD regTziSize = sizeof(regTzi); - HKEY key = NULL; - const QString tziKeyPath = QString::fromUtf8(tzRegPath) + QLatin1Char('\\') + const QString tziKeyPath = QString::fromWCharArray(tzRegPath) + QLatin1Char('\\') + QString::fromUtf8(windowsId); - if (openRegistryKey(tziKeyPath, &key)) { - + QWinRegistryKey key(HKEY_LOCAL_MACHINE, tziKeyPath); + if (key.isValid()) { DWORD size = sizeof(tzi.DaylightName); RegQueryValueEx(key, L"Dlt", nullptr, nullptr, reinterpret_cast(tzi.DaylightName), &size); @@ -206,8 +185,6 @@ TIME_ZONE_INFORMATION getRegistryTzi(const QByteArray &windowsId, bool *ok) tzi.DaylightDate = regTzi.DaylightDate; *ok = true; } - - RegCloseKey(key); } return tzi; @@ -299,8 +276,8 @@ QList availableWindowsIds() #ifdef QT_USE_REGISTRY_TIMEZONE // TODO Consider caching results in a global static, very unlikely to change. QList list; - HKEY key = NULL; - if (openRegistryKey(QString::fromUtf8(tzRegPath), &key)) { + QWinRegistryKey key(HKEY_LOCAL_MACHINE, tzRegPath); + if (key.isValid()) { DWORD idCount = 0; if (RegQueryInfoKey(key, 0, 0, 0, &idCount, 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS && idCount > 0) { @@ -311,7 +288,6 @@ QList availableWindowsIds() list.append(QString::fromWCharArray(buffer).toUtf8()); } } - RegCloseKey(key); } return list; #else // QT_USE_REGISTRY_TIMEZONE @@ -325,15 +301,10 @@ QByteArray windowsSystemZoneId() { #ifdef QT_USE_REGISTRY_TIMEZONE // On Vista and later is held in the value TimeZoneKeyName in key currTzRegPath - QString id; - HKEY key = NULL; - QString tziKeyPath = QString::fromUtf8(currTzRegPath); - if (openRegistryKey(tziKeyPath, &key)) { - id = readRegistryString(key, L"TimeZoneKeyName"); - RegCloseKey(key); - if (!id.isEmpty()) - return std::move(id).toUtf8(); - } + const QString id = QWinRegistryKey(HKEY_LOCAL_MACHINE, currTzRegPath) + .stringValue(L"TimeZoneKeyName"); + if (!id.isEmpty()) + return id.toUtf8(); // On XP we have to iterate over the zones until we find a match on // names/offsets with the current data @@ -575,22 +546,22 @@ void QWinTimeZonePrivate::init(const QByteArray &ianaId) if (!m_windowsId.isEmpty()) { #ifdef QT_USE_REGISTRY_TIMEZONE // Open the base TZI for the time zone - HKEY baseKey = NULL; - const QString baseKeyPath = QString::fromUtf8(tzRegPath) + QLatin1Char('\\') + const QString baseKeyPath = QString::fromWCharArray(tzRegPath) + QLatin1Char('\\') + QString::fromUtf8(m_windowsId); - if (openRegistryKey(baseKeyPath, &baseKey)) { + QWinRegistryKey baseKey(HKEY_LOCAL_MACHINE, baseKeyPath); + if (baseKey.isValid()) { // Load the localized names - m_displayName = readRegistryString(baseKey, L"Display"); - m_standardName = readRegistryString(baseKey, L"Std"); - m_daylightName = readRegistryString(baseKey, L"Dlt"); + m_displayName = baseKey.stringValue(L"Display"); + m_standardName = baseKey.stringValue(L"Std"); + m_daylightName = baseKey.stringValue(L"Dlt"); // On Vista and later the optional dynamic key holds historic data const QString dynamicKeyPath = baseKeyPath + QLatin1String("\\Dynamic DST"); - HKEY dynamicKey = NULL; - if (openRegistryKey(dynamicKeyPath, &dynamicKey)) { + QWinRegistryKey dynamicKey(HKEY_LOCAL_MACHINE, dynamicKeyPath); + if (dynamicKey.isValid()) { // Find out the start and end years stored, then iterate over them - int startYear = readRegistryValue(dynamicKey, L"FirstEntry"); - int endYear = readRegistryValue(dynamicKey, L"LastEntry"); - for (int year = startYear; year <= endYear; ++year) { + const auto startYear = dynamicKey.dwordValue(L"FirstEntry"); + const auto endYear = dynamicKey.dwordValue(L"LastEntry"); + for (int year = int(startYear.first); year <= int(endYear.first); ++year) { bool ruleOk; QWinTransitionRule rule = readRegistryRule(dynamicKey, reinterpret_cast(QString::number(year).utf16()), @@ -611,7 +582,6 @@ void QWinTimeZonePrivate::init(const QByteArray &ianaId) m_tranRules.append(rule); } } - RegCloseKey(dynamicKey); } else { // No dynamic data so use the base data bool ruleOk; @@ -620,7 +590,6 @@ void QWinTimeZonePrivate::init(const QByteArray &ianaId) if (ruleOk) m_tranRules.append(rule); } - RegCloseKey(baseKey); } #else // QT_USE_REGISTRY_TIMEZONE if (gTimeZones->isEmpty()) -- cgit v1.2.3 From 85ed676dff68b4d5cfa62112d758d80fb050594c Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Mon, 14 Oct 2019 15:12:02 +0200 Subject: Provide a feature for CBOR stream I/O We need to turn it off in bootstrap code. Change-Id: I826e49fbc5f6128e56f84b58d29358dd7b0b9dc5 Reviewed-by: Lars Knoll --- src/corelib/configure.json | 8 ++++++++ src/corelib/global/qconfig-bootstrapped.h | 1 + src/corelib/serialization/qcborstream.h | 2 ++ src/corelib/serialization/qcborvalue.cpp | 9 +++++++++ src/corelib/serialization/qcborvalue.h | 4 ++++ src/corelib/serialization/serialization.pri | 10 ++++++++-- 6 files changed, 32 insertions(+), 2 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/configure.json b/src/corelib/configure.json index ae360239c6..b4b7c4eec3 100644 --- a/src/corelib/configure.json +++ b/src/corelib/configure.json @@ -1086,6 +1086,14 @@ Mozilla License) is included. The data is then also used in QNetworkCookieJar::v "win32_system_libs": { "label": "Windows System Libraries", "condition": "config.win32 && libs.advapi32 && libs.gdi32 && libs.kernel32 && libs.netapi32 && libs.ole32 && libs.shell32 && libs.uuid && libs.user32 && libs.winmm && libs.ws2_32" + }, + "cborstream": { + "label": "CBOR stream I/O", + "purpose": "Provides support for reading and writing the CBOR binary format. + +Note that this is required for plugin loading. Qt GUI needs QPA plugins for basic operation.", + "section": "Utilities", + "output": [ "publicFeature" ] } }, diff --git a/src/corelib/global/qconfig-bootstrapped.h b/src/corelib/global/qconfig-bootstrapped.h index e6ad80525a..e9383ca68b 100644 --- a/src/corelib/global/qconfig-bootstrapped.h +++ b/src/corelib/global/qconfig-bootstrapped.h @@ -74,6 +74,7 @@ #else # define QT_FEATURE_alloca_malloc_h -1 #endif +#define QT_FEATURE_cborstream -1 #define QT_CRYPTOGRAPHICHASH_ONLY_SHA1 #define QT_FEATURE_cxx11_random (QT_HAS_INCLUDE() ? 1 : -1) #define QT_NO_DATASTREAM diff --git a/src/corelib/serialization/qcborstream.h b/src/corelib/serialization/qcborstream.h index 7a451e63ac..08bf680cca 100644 --- a/src/corelib/serialization/qcborstream.h +++ b/src/corelib/serialization/qcborstream.h @@ -47,6 +47,8 @@ #include #include +QT_REQUIRE_CONFIG(cborstream); + // See qcborcommon.h for why we check #if defined(QT_X11_DEFINES_FOUND) # undef True diff --git a/src/corelib/serialization/qcborvalue.cpp b/src/corelib/serialization/qcborvalue.cpp index 1b170739d2..b77cfd5c70 100644 --- a/src/corelib/serialization/qcborvalue.cpp +++ b/src/corelib/serialization/qcborvalue.cpp @@ -42,7 +42,10 @@ #include "qdatastream.h" #include "qcborarray.h" #include "qcbormap.h" + +#if QT_CONFIG(cborstream) #include "qcborstream.h" +#endif #include #include @@ -758,6 +761,7 @@ QT_BEGIN_NAMESPACE using namespace QtCbor; +#if QT_CONFIG(cborstream) // in qcborstream.cpp extern void qt_cbor_stream_set_error(QCborStreamReaderPrivate *d, QCborError error); @@ -799,6 +803,7 @@ static void writeDoubleToCbor(QCborStreamWriter &writer, double d, QCborValue::E writer.append(d); } +#endif // QT_CONFIG(cborstream) static inline int typeOrder(Element e1, Element e2) { @@ -1221,6 +1226,7 @@ int QCborMap::compare(const QCborMap &other) const noexcept return compareContainer(d.data(), other.d.data()); } +#if QT_CONFIG(cborstream) static void encodeToCbor(QCborStreamWriter &writer, const QCborContainerPrivate *d, qsizetype idx, QCborValue::EncodingOptions opt) { @@ -1632,6 +1638,7 @@ void QCborContainerPrivate::decodeFromCbor(QCborStreamReader &reader) if (reader.lastError() == QCborError::NoError) reader.leaveContainer(); } +#endif // QT_CONFIG(cborstream) /*! Creates a QCborValue with byte array value \a ba. The value can later be @@ -2330,6 +2337,7 @@ QCborValueRef QCborValue::operator[](qint64 key) return { container, index }; } +#if QT_CONFIG(cborstream) /*! Decodes one item from the CBOR stream found in \a reader and returns the equivalent representation. This function is recursive: if the item is a map @@ -2567,6 +2575,7 @@ void QCborValueRef::toCbor(QCborStreamWriter &writer, QCborValue::EncodingOption { concrete().toCbor(writer, opt); } +#endif // QT_CONFIG(cborstream) void QCborValueRef::assign(QCborValueRef that, const QCborValue &other) { diff --git a/src/corelib/serialization/qcborvalue.h b/src/corelib/serialization/qcborvalue.h index 3c325b59e7..accd0fae8a 100644 --- a/src/corelib/serialization/qcborvalue.h +++ b/src/corelib/serialization/qcborvalue.h @@ -285,6 +285,7 @@ public: static QCborValue fromJsonValue(const QJsonValue &v); QJsonValue toJsonValue() const; +#if QT_CONFIG(cborstream) static QCborValue fromCbor(QCborStreamReader &reader); static QCborValue fromCbor(const QByteArray &ba, QCborParserError *error = nullptr); static QCborValue fromCbor(const char *data, qsizetype len, QCborParserError *error = nullptr) @@ -293,6 +294,7 @@ public: { return fromCbor(QByteArray(reinterpret_cast(data), int(len)), error); } QByteArray toCbor(EncodingOptions opt = NoTransformation); void toCbor(QCborStreamWriter &writer, EncodingOptions opt = NoTransformation); +#endif QString toDiagnosticNotation(DiagnosticNotationOptions opts = Compact) const; @@ -435,9 +437,11 @@ public: QVariant toVariant() const { return concrete().toVariant(); } QJsonValue toJsonValue() const; +#if QT_CONFIG(cborstream) QByteArray toCbor(QCborValue::EncodingOptions opt = QCborValue::NoTransformation) { return concrete().toCbor(opt); } void toCbor(QCborStreamWriter &writer, QCborValue::EncodingOptions opt = QCborValue::NoTransformation); +#endif QString toDiagnosticNotation(QCborValue::DiagnosticNotationOptions opt = QCborValue::Compact) { return concrete().toDiagnosticNotation(opt); } diff --git a/src/corelib/serialization/serialization.pri b/src/corelib/serialization/serialization.pri index 4f2dc64e4f..5310fddd67 100644 --- a/src/corelib/serialization/serialization.pri +++ b/src/corelib/serialization/serialization.pri @@ -6,7 +6,6 @@ HEADERS += \ serialization/qcbormap.h \ serialization/qcborvalue.h \ serialization/qcborvalue_p.h \ - serialization/qcborstream.h \ serialization/qdatastream.h \ serialization/qdatastream_p.h \ serialization/qjson_p.h \ @@ -23,7 +22,6 @@ HEADERS += \ serialization/qxmlutils_p.h SOURCES += \ - serialization/qcborstream.cpp \ serialization/qcbordiagnostic.cpp \ serialization/qcborvalue.cpp \ serialization/qdatastream.cpp \ @@ -39,6 +37,14 @@ SOURCES += \ serialization/qxmlstream.cpp \ serialization/qxmlutils.cpp +qtConfig(cborstream): { + SOURCES += \ + serialization/qcborstream.cpp + + HEADERS += \ + serialization/qcborstream.h +} + false: SOURCES += \ serialization/qcborarray.cpp \ serialization/qcbormap.cpp -- cgit v1.2.3 From c222a9283d94ad1e29334bb2fba0beef7cc716c7 Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Tue, 18 Jun 2019 00:07:54 +0200 Subject: QAbstractItemModel: implement QRegularExpression support for match This is part of the migration of qtbase from QRexExp to QRegularExpression. [ChangeLog][QtCore][QAbstractItemModel] The match() method now supports the new Qt::RegularExpression match flag value. This will allow users to use either a string or a fully configured QRegularExpression when doing searches. In the second case, the case sensitivity flag will be ignored if passed. Task-number: QTBUG-72587 Change-Id: I07c8d72a661c48b7f4fcf13ef8e95980bcdcb998 Reviewed-by: Giuseppe D'Angelo --- src/corelib/global/qnamespace.h | 5 ++++- src/corelib/global/qnamespace.qdoc | 32 +++++++++++++++++---------- src/corelib/itemmodels/qabstractitemmodel.cpp | 32 +++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 17 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/global/qnamespace.h b/src/corelib/global/qnamespace.h index 810c55709c..047ed8e7b3 100644 --- a/src/corelib/global/qnamespace.h +++ b/src/corelib/global/qnamespace.h @@ -1575,9 +1575,12 @@ public: MatchContains = 1, MatchStartsWith = 2, MatchEndsWith = 3, - MatchRegExp = 4, +#if QT_DEPRECATED_SINCE(5, 15) + MatchRegExp Q_DECL_ENUMERATOR_DEPRECATED_X("MatchRegExp is deprecated. Use MatchRegularExpression instead") = 4, +#endif MatchWildcard = 5, MatchFixedString = 8, + MatchRegularExpression = 9, MatchCaseSensitive = 16, MatchWrap = 32, MatchRecursive = 64 diff --git a/src/corelib/global/qnamespace.qdoc b/src/corelib/global/qnamespace.qdoc index cce88782e9..9896cf6e02 100644 --- a/src/corelib/global/qnamespace.qdoc +++ b/src/corelib/global/qnamespace.qdoc @@ -2840,24 +2840,32 @@ This enum describes the type of matches that can be used when searching for items in a model. - \value MatchExactly Performs QVariant-based matching. - \value MatchFixedString Performs string-based matching. + \value MatchExactly Performs QVariant-based matching. + \value MatchFixedString Performs string-based matching. String-based comparisons are case-insensitive unless the \c MatchCaseSensitive flag is also specified. - \value MatchContains The search term is contained in the item. - \value MatchStartsWith The search term matches the start of the item. - \value MatchEndsWith The search term matches the end of the item. - \value MatchCaseSensitive The search is case sensitive. - \value MatchRegExp Performs string-based matching using a regular - expression as the search term. - \value MatchWildcard Performs string-based matching using a string with + \value MatchContains The search term is contained in the item. + \value MatchStartsWith The search term matches the start of the item. + \value MatchEndsWith The search term matches the end of the item. + \value MatchCaseSensitive The search is case sensitive. + \value MatchRegExp Performs string-based matching using a regular + expression as the search term. Uses the deprecated QRegExp class. + \e{This enum value is deprecated since Qt 5.15.} + \value MatchRegularExpression Performs string-based matching using a regular + expression as the search term. Uses QRegularExpression. + When using this flag, a QRegularExpression object can be passed as + parameter and will directly be used to perform the search. The case + sensitivity flag will be ignored as the QRegularExpression object is + expected to be fully configured. + This enum value was added in Qt 5.15. + \value MatchWildcard Performs string-based matching using a string with wildcards as the search term. - \value MatchWrap Perform a search that wraps around, so that when + \value MatchWrap Perform a search that wraps around, so that when the search reaches the last item in the model, it begins again at the first item and continues until all items have been examined. - \value MatchRecursive Searches the entire hierarchy. + \value MatchRecursive Searches the entire hierarchy. - \sa QString::compare(), QRegExp + \sa QString::compare(), QRegExp, QRegularExpression */ /*! diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index 6e97c2fd39..88555f9572 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -2358,6 +2359,7 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role, bool wrap = flags & Qt::MatchWrap; bool allHits = (hits == -1); QString text; // only convert to a string if it is needed + QRegularExpression rx; // only create it if needed const int column = start.column(); QModelIndex p = parent(start); int from = start.row(); @@ -2374,17 +2376,39 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role, if (matchType == Qt::MatchExactly) { if (value == v) result.append(idx); - } else { // QString based matching - if (text.isEmpty()) // lazy conversion - text = value.toString(); + } else { // QString or regular expression based matching + if (matchType == Qt::MatchRegularExpression) { + if (rx.pattern().isEmpty()) { + if (value.type() == QVariant::RegularExpression) { + rx = value.toRegularExpression(); + } else { + rx.setPattern(value.toString()); + if (cs == Qt::CaseInsensitive) + rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption); + } + } + } else if (matchType == Qt::MatchWildcard) { + if (rx.pattern().isEmpty()) + rx.setPattern(QRegularExpression::wildcardToRegularExpression(value.toString())); + if (cs == Qt::CaseInsensitive) + rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption); + } else { + if (text.isEmpty()) // lazy conversion + text = value.toString(); + } + QString t = v.toString(); switch (matchType) { +#if QT_DEPRECATED_SINCE(5, 15) case Qt::MatchRegExp: if (QRegExp(text, cs).exactMatch(t)) result.append(idx); break; +#endif + case Qt::MatchRegularExpression: + Q_FALLTHROUGH(); case Qt::MatchWildcard: - if (QRegExp(text, cs, QRegExp::Wildcard).exactMatch(t)) + if (t.contains(rx)) result.append(idx); break; case Qt::MatchStartsWith: -- cgit v1.2.3