diff options
Diffstat (limited to 'src/corelib/io/qsettings.cpp')
-rw-r--r-- | src/corelib/io/qsettings.cpp | 192 |
1 files changed, 82 insertions, 110 deletions
diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index d1ae14490c..05f6c22fc2 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -1,31 +1,37 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 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:LGPL21$ +** $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 http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** 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 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** 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. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** 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$ ** @@ -100,7 +106,7 @@ using namespace ABI::Windows::Storage; #define CSIDL_APPDATA 0x001a // <username>\Application Data #endif -#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_BLACKBERRY) && !defined(Q_OS_ANDROID) +#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_ANDROID) #define Q_XDG_PLATFORM #endif @@ -243,8 +249,7 @@ QString QSettingsPrivate::actualKey(const QString &key) const { QString n = normalizedKey(key); Q_ASSERT_X(!n.isEmpty(), "QSettings", "empty key"); - n.prepend(groupPrefix); - return n; + return groupPrefix + n; } /* @@ -319,10 +324,9 @@ void QSettingsPrivate::processChild(QStringRef key, ChildSpec spec, QStringList void QSettingsPrivate::beginGroupOrArray(const QSettingsGroup &group) { groupStack.push(group); - if (!group.name().isEmpty()) { - groupPrefix += group.name(); - groupPrefix += QLatin1Char('/'); - } + const QString name = group.name(); + if (!name.isEmpty()) + groupPrefix += name + QLatin1Char('/'); } /* @@ -398,9 +402,9 @@ QString QSettingsPrivate::variantToString(const QVariant &v) case QVariant::ByteArray: { QByteArray a = v.toByteArray(); - result = QLatin1String("@ByteArray("); - result += QString::fromLatin1(a.constData(), a.size()); - result += QLatin1Char(')'); + result = QLatin1String("@ByteArray(") + + QLatin1String(a.constData(), a.size()) + + QLatin1Char(')'); break; } @@ -422,33 +426,17 @@ QString QSettingsPrivate::variantToString(const QVariant &v) #ifndef QT_NO_GEOM_VARIANT case QVariant::Rect: { QRect r = qvariant_cast<QRect>(v); - result += QLatin1String("@Rect("); - result += QString::number(r.x()); - result += QLatin1Char(' '); - result += QString::number(r.y()); - result += QLatin1Char(' '); - result += QString::number(r.width()); - result += QLatin1Char(' '); - result += QString::number(r.height()); - result += QLatin1Char(')'); + result = QString::asprintf("@Rect(%d %d %d %d)", r.x(), r.y(), r.width(), r.height()); break; } case QVariant::Size: { QSize s = qvariant_cast<QSize>(v); - result += QLatin1String("@Size("); - result += QString::number(s.width()); - result += QLatin1Char(' '); - result += QString::number(s.height()); - result += QLatin1Char(')'); + result = QString::asprintf("@Size(%d %d)", s.width(), s.height()); break; } case QVariant::Point: { QPoint p = qvariant_cast<QPoint>(v); - result += QLatin1String("@Point("); - result += QString::number(p.x()); - result += QLatin1Char(' '); - result += QString::number(p.y()); - result += QLatin1Char(')'); + result = QString::asprintf("@Point(%d %d)", p.x(), p.y()); break; } #endif // !QT_NO_GEOM_VARIANT @@ -471,9 +459,9 @@ QString QSettingsPrivate::variantToString(const QVariant &v) s << v; } - result = QLatin1String(typeSpec); - result += QString::fromLatin1(a.constData(), a.size()); - result += QLatin1Char(')'); + result = QLatin1String(typeSpec) + + QLatin1String(a.constData(), a.size()) + + QLatin1Char(')'); #else Q_ASSERT(!"QSettings: Cannot save custom types without QDataStream support"); #endif @@ -646,8 +634,7 @@ void QSettingsPrivate::iniEscapedString(const QString &str, QByteArray &result, && ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) { - result += "\\x"; - result += QByteArray::number(ch, 16); + result += "\\x" + QByteArray::number(ch, 16); continue; } @@ -686,8 +673,7 @@ void QSettingsPrivate::iniEscapedString(const QString &str, QByteArray &result, break; default: if (ch <= 0x1F || (ch >= 0x7F && !useCodec)) { - result += "\\x"; - result += QByteArray::number(ch, 16); + result += "\\x" + QByteArray::number(ch, 16); escapeNextIfDigit = true; #ifndef QT_NO_TEXTCODEC } else if (useCodec) { @@ -1046,10 +1032,33 @@ static inline int pathHashKey(QSettings::Format format, QSettings::Scope scope) return int((uint(format) << 1) | uint(scope == QSettings::SystemScope)); } +#ifndef Q_OS_WIN +static QString make_user_path() +{ + static Q_CONSTEXPR QChar sep = QLatin1Char('/'); +#ifndef QSETTINGS_USE_QSTANDARDPATHS + // Non XDG platforms (OS X, iOS, Android...) have used this code path erroneously + // for some time now. Moving away from that would require migrating existing settings. + QByteArray env = qgetenv("XDG_CONFIG_HOME"); + if (env.isEmpty()) { + return QDir::homePath() + QLatin1String("/.config/"); + } else if (env.startsWith('/')) { + return QFile::decodeName(env) + sep; + } else { + return QDir::homePath() + sep + QFile::decodeName(env) + sep; + } +#else + // When using a proper XDG platform, use QStandardPaths rather than the above hand-written code; + // it makes the use of test mode from unit tests possible. + // Ideally all platforms should use this, but see above for the migration issue. + return QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + sep; +#endif +} +#endif // !Q_OS_WIN + static void initDefaultPaths(QMutexLocker *locker) { PathHash *pathHash = pathHashFunc(); - QString systemPath; locker->unlock(); @@ -1058,8 +1067,7 @@ static void initDefaultPaths(QMutexLocker *locker) avoid a dead-lock, we can't hold the global mutex while calling it. */ - systemPath = QLibraryInfo::location(QLibraryInfo::SettingsPath); - systemPath += QLatin1Char('/'); + QString systemPath = QLibraryInfo::location(QLibraryInfo::SettingsPath) + QLatin1Char('/'); locker->relock(); if (pathHash->isEmpty()) { @@ -1075,38 +1083,14 @@ static void initDefaultPaths(QMutexLocker *locker) pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), windowsConfigPath(CSIDL_COMMON_APPDATA) + QDir::separator()); #else - -#ifndef QSETTINGS_USE_QSTANDARDPATHS - // Non XDG platforms (OS X, iOS, Blackberry, Android...) have used this code path erroneously - // for some time now. Moving away from that would require migrating existing settings. - QString userPath; - QByteArray env = qgetenv("XDG_CONFIG_HOME"); - if (env.isEmpty()) { - userPath = QDir::homePath(); - userPath += QLatin1Char('/'); - userPath += QLatin1String(".config"); - } else if (env.startsWith('/')) { - userPath = QFile::decodeName(env); - } else { - userPath = QDir::homePath(); - userPath += QLatin1Char('/'); - userPath += QFile::decodeName(env); - } -#else - // When using a proper XDG platform, use QStandardPaths rather than the above hand-written code; - // it makes the use of test mode from unit tests possible. - // Ideally all platforms should use this, but see above for the migration issue. - QString userPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); -#endif - userPath += QLatin1Char('/'); - + const QString userPath = make_user_path(); pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope), userPath); pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), systemPath); #ifndef Q_OS_MAC pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::UserScope), userPath); pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::SystemScope), systemPath); #endif -#endif +#endif // Q_OS_WIN } } @@ -1144,7 +1128,6 @@ QConfFileSettingsPrivate::QConfFileSettingsPrivate(QSettings::Format format, org = QLatin1String("Unknown Organization"); } -#if !defined(Q_OS_BLACKBERRY) QString appFile = org + QDir::separator() + application + extension; QString orgFile = org + extension; @@ -1159,13 +1142,6 @@ QConfFileSettingsPrivate::QConfFileSettingsPrivate(QSettings::Format format, if (!application.isEmpty()) confFiles[F_System | F_Application].reset(QConfFile::fromName(systemPath + appFile, false)); confFiles[F_System | F_Organization].reset(QConfFile::fromName(systemPath + orgFile, false)); -#else - QString confName = getPath(format, QSettings::UserScope) + org; - if (!application.isEmpty()) - confName += QDir::separator() + application; - confName += extension; - confFiles[SandboxConfFile].reset(QConfFile::fromName(confName, true)); -#endif for (i = 0; i < NumConfFiles; ++i) { if (confFiles[i]) { @@ -2257,7 +2233,6 @@ void QConfFileSettingsPrivate::ensureSectionParsed(QConfFile *confFile, stored in the following registry path: \c{HKEY_LOCAL_MACHINE\Software\WOW6432node}. - On BlackBerry only a single file is used (see \l{Platform Limitations}). If the file format is NativeFormat, this is "Settings/MySoft/Star Runner.conf" in the application's home directory. @@ -2289,7 +2264,6 @@ void QConfFileSettingsPrivate::ensureSectionParsed(QConfFile *confFile, \c{CSIDL_COMMON_APPDATA} usually points to \tt{C:\\ProgramData}. - On BlackBerry only a single file is used (see \l{Platform Limitations}). If the file format is IniFormat, this is "Settings/MySoft/Star Runner.ini" in the application's home directory. @@ -2394,17 +2368,6 @@ void QConfFileSettingsPrivate::ensureSectionParsed(QConfFile *confFile, 10.8 (Mountain Lion), only root can. However, 10.9 (Mavericks) changes that rule again but only for the native format (plist files). - \li On the BlackBerry platform, applications run in a sandbox. They are not - allowed to read or write outside of this sandbox. This involves the - following limitations: - \list - \li As there is only a single scope the scope is simply ignored, - i.e. there is no difference between SystemScope and UserScope. - \li The \l{Fallback Mechanism} is not applied, i.e. only a single - location is considered. - \li It is advised against setting and using custom file paths. - \endlist - \endlist \sa QVariant, QSessionManager, {Settings Editor Example}, {Application Example} @@ -2425,14 +2388,24 @@ void QConfFileSettingsPrivate::ensureSectionParsed(QConfFile *confFile, This enum type specifies the storage format used by QSettings. - \value NativeFormat Store the settings using the most - appropriate storage format for the platform. - On Windows, this means the system registry; - on \macos and iOS, this means the CFPreferences - API; on Unix, this means textual - configuration files in INI format. - \value IniFormat Store the settings in INI files. - \value InvalidFormat Special value returned by registerFormat(). + \value NativeFormat Store the settings using the most + appropriate storage format for the platform. + On Windows, this means the system registry; + on \macos and iOS, this means the CFPreferences + API; on Unix, this means textual + configuration files in INI format. + \value Registry32Format Windows only: Explicitly access the 32-bit system registry + from a 64-bit application running on 64-bit Windows. + On 32-bit Windows or from a 32-bit application on 64-bit Windows, + this works the same as specifying NativeFormat. + This enum value was added in Qt 5.7. + \value Registry64Format Windows only: Explicitly access the 64-bit system registry + from a 32-bit application running on 64-bit Windows. + On 32-bit Windows or from a 64-bit application on 64-bit Windows, + this works the same as specifying NativeFormat. + This enum value was added in Qt 5.7. + \value IniFormat Store the settings in INI files. + \value InvalidFormat Special value returned by registerFormat(). \omitvalue CustomFormat1 \omitvalue CustomFormat2 \omitvalue CustomFormat3 @@ -3509,8 +3482,7 @@ QSettings::Format QSettings::registerFormat(const QString &extension, ReadFunc r return QSettings::InvalidFormat; QConfFileCustomFormat info; - info.extension = QLatin1Char('.'); - info.extension += extension; + info.extension = QLatin1Char('.') + extension; info.readFunc = readFunc; info.writeFunc = writeFunc; info.caseSensitivity = caseSensitivity; |