diff options
Diffstat (limited to 'src/corelib/io/qsettings.cpp')
-rw-r--r-- | src/corelib/io/qsettings.cpp | 177 |
1 files changed, 80 insertions, 97 deletions
diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index 05f6c22fc2..480a777457 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -81,7 +81,6 @@ #include <stdlib.h> #ifdef Q_OS_WIN // for homedirpath reading from registry -# include <private/qsystemlibrary_p.h> # include <qt_windows.h> # ifndef Q_OS_WINRT # include <shlobj.h> @@ -98,19 +97,11 @@ using namespace ABI::Windows::Foundation; using namespace ABI::Windows::Storage; #endif -#ifndef CSIDL_COMMON_APPDATA -#define CSIDL_COMMON_APPDATA 0x0023 // All Users\Application Data -#endif - -#ifndef CSIDL_APPDATA -#define CSIDL_APPDATA 0x001a // <username>\Application Data -#endif - #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_ANDROID) #define Q_XDG_PLATFORM #endif -#if !defined(QT_NO_STANDARDPATHS) && (defined(Q_XDG_PLATFORM) || defined(Q_OS_IOS)) +#if !defined(QT_NO_STANDARDPATHS) && (defined(Q_XDG_PLATFORM) || defined(QT_PLATFORM_UIKIT)) #define QSETTINGS_USE_QSTANDARDPATHS #endif @@ -818,7 +809,7 @@ StNormal: ++i; goto StSkipSpaces; } - // fallthrough + Q_FALLTHROUGH(); default: { int j = i + 1; while (j < to) { @@ -960,40 +951,34 @@ void QConfFileSettingsPrivate::initAccess() } #if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) -static QString windowsConfigPath(int type) +static QString windowsConfigPath(const KNOWNFOLDERID &type) { QString result; - wchar_t path[MAX_PATH]; - if (SHGetSpecialFolderPath(0, path, type, false)) + PWSTR path = nullptr; + if (SHGetKnownFolderPath(type, KF_FLAG_DONT_VERIFY, NULL, &path) == S_OK) { result = QString::fromWCharArray(path); + CoTaskMemFree(path); + } if (result.isEmpty()) { - switch (type) { -#ifndef Q_OS_WINCE - case CSIDL_COMMON_APPDATA: + if (type == FOLDERID_ProgramData) { result = QLatin1String("C:\\temp\\qt-common"); - break; - case CSIDL_APPDATA: + } else if (type == FOLDERID_RoamingAppData) { result = QLatin1String("C:\\temp\\qt-user"); - break; -#else - case CSIDL_COMMON_APPDATA: - result = QLatin1String("\\Temp\\qt-common"); - break; - case CSIDL_APPDATA: - result = QLatin1String("\\Temp\\qt-user"); - break; -#endif - default: - ; } } return result; } #elif defined(Q_OS_WINRT) // Q_OS_WIN && !Q_OS_WINRT -static QString windowsConfigPath(int type) + +enum ConfigPathType { + ConfigPath_CommonAppData, + ConfigPath_UserAppData +}; + +static QString windowsConfigPath(ConfigPathType type) { static QString result; while (result.isEmpty()) { @@ -1016,12 +1001,10 @@ static QString windowsConfigPath(int type) } switch (type) { - case CSIDL_COMMON_APPDATA: + case ConfigPath_CommonAppData: return result + QLatin1String("\\qt-common"); - case CSIDL_APPDATA: + case ConfigPath_UserAppData: return result + QLatin1String("\\qt-user"); - default: - break; } return result; } @@ -1078,10 +1061,18 @@ static void initDefaultPaths(QMutexLocker *locker) Windows registry and the Mac CFPreferences.) */ #ifdef Q_OS_WIN + +# ifdef Q_OS_WINRT + const QString roamingAppDataFolder = windowsConfigPath(ConfigPath_UserAppData); + const QString programDataFolder = windowsConfigPath(ConfigPath_CommonAppData); +# else + const QString roamingAppDataFolder = windowsConfigPath(FOLDERID_RoamingAppData); + const QString programDataFolder = windowsConfigPath(FOLDERID_ProgramData); +# endif pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope), - windowsConfigPath(CSIDL_APPDATA) + QDir::separator()); + roamingAppDataFolder + QDir::separator()); pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), - windowsConfigPath(CSIDL_COMMON_APPDATA) + QDir::separator()); + programDataFolder + QDir::separator()); #else const QString userPath = make_user_path(); pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope), userPath); @@ -1369,7 +1360,6 @@ void QConfFileSettingsPrivate::syncConfFile(int confFileNo) { QConfFile *confFile = confFiles[confFileNo].data(); bool readOnly = confFile->addedKeys.isEmpty() && confFile->removedKeys.isEmpty(); - bool ok; /* We can often optimize the read-only case, if the file on disk @@ -1429,31 +1419,26 @@ void QConfFileSettingsPrivate::syncConfFile(int confFileNo) because they don't exist) are treated as empty files. */ if (file.isReadable() && fileInfo.size() != 0) { + bool ok = false; #ifdef Q_OS_MAC if (format == QSettings::NativeFormat) { - ok = readPlistFile(confFile->name, &confFile->originalKeys); + QByteArray data = file.readAll(); + ok = readPlistFile(data, &confFile->originalKeys); } else #endif - { - if (format <= QSettings::IniFormat) { - QByteArray data = file.readAll(); - ok = readIniFile(data, &confFile->unparsedIniSections); - } else { - if (readFunc) { - QSettings::SettingsMap tempNewKeys; - ok = readFunc(file, tempNewKeys); - - if (ok) { - QSettings::SettingsMap::const_iterator i = tempNewKeys.constBegin(); - while (i != tempNewKeys.constEnd()) { - confFile->originalKeys.insert(QSettingsKey(i.key(), - caseSensitivity), - i.value()); - ++i; - } - } - } else { - ok = false; + if (format <= QSettings::IniFormat) { + QByteArray data = file.readAll(); + ok = readIniFile(data, &confFile->unparsedIniSections); + } else if (readFunc) { + QSettings::SettingsMap tempNewKeys; + ok = readFunc(file, tempNewKeys); + + if (ok) { + QSettings::SettingsMap::const_iterator i = tempNewKeys.constBegin(); + while (i != tempNewKeys.constEnd()) { + confFile->originalKeys.insert(QSettingsKey(i.key(), caseSensitivity), + i.value()); + ++i; } } } @@ -1471,44 +1456,42 @@ void QConfFileSettingsPrivate::syncConfFile(int confFileNo) so everything is under control. */ if (!readOnly) { + bool ok = false; ensureAllSectionsParsed(confFile); ParsedSettingsMap mergedKeys = confFile->mergedKeyMap(); -#ifdef Q_OS_MAC - if (format == QSettings::NativeFormat) { - ok = writePlistFile(confFile->name, mergedKeys); - } else -#endif - { #ifndef QT_BOOTSTRAPPED - QSaveFile sf(confFile->name); + QSaveFile sf(confFile->name); #else - QFile sf(confFile->name); + QFile sf(confFile->name); #endif - if (!sf.open(QIODevice::WriteOnly)) { - setStatus(QSettings::AccessError); - ok = false; - } else if (format <= QSettings::IniFormat) { - ok = writeIniFile(sf, mergedKeys); - } else { - if (writeFunc) { - QSettings::SettingsMap tempOriginalKeys; + if (!sf.open(QIODevice::WriteOnly)) { + setStatus(QSettings::AccessError); + return; + } - ParsedSettingsMap::const_iterator i = mergedKeys.constBegin(); - while (i != mergedKeys.constEnd()) { - tempOriginalKeys.insert(i.key(), i.value()); - ++i; - } - ok = writeFunc(sf, tempOriginalKeys); - } else { - ok = false; - } +#ifdef Q_OS_MAC + if (format == QSettings::NativeFormat) { + ok = writePlistFile(sf, mergedKeys); + } else +#endif + if (format <= QSettings::IniFormat) { + ok = writeIniFile(sf, mergedKeys); + } else if (writeFunc) { + QSettings::SettingsMap tempOriginalKeys; + + ParsedSettingsMap::const_iterator i = mergedKeys.constBegin(); + while (i != mergedKeys.constEnd()) { + tempOriginalKeys.insert(i.key(), i.value()); + ++i; } + ok = writeFunc(sf, tempOriginalKeys); + } + #ifndef QT_BOOTSTRAPPED - if (ok) - ok = sf.commit(); + if (ok) + ok = sf.commit(); #endif - } if (ok) { confFile->unparsedIniSections.clear(); @@ -2249,20 +2232,20 @@ void QConfFileSettingsPrivate::ensureSectionParsed(QConfFile *confFile, On Windows, the following files are used: \list 1 - \li \c{CSIDL_APPDATA\MySoft\Star Runner.ini} - \li \c{CSIDL_APPDATA\MySoft.ini} - \li \c{CSIDL_COMMON_APPDATA\MySoft\Star Runner.ini} - \li \c{CSIDL_COMMON_APPDATA\MySoft.ini} + \li \c{FOLDERID_RoamingAppData\MySoft\Star Runner.ini} + \li \c{FOLDERID_RoamingAppData\MySoft.ini} + \li \c{FOLDERID_ProgramData\MySoft\Star Runner.ini} + \li \c{FOLDERID_ProgramData\MySoft.ini} \endlist - The identifiers prefixed by \c{CSIDL_} are special item ID lists to be passed - to the Win32 API function \c{SHGetSpecialFolderPath()} to obtain the + The identifiers prefixed by \c{FOLDERID_} are special item ID lists to be passed + to the Win32 API function \c{SHGetKnownFolderPath()} to obtain the corresponding path. - \c{CSIDL_APPDATA} usually points to \tt{C:\\Users\\\e{User Name}\\AppData\\Roaming}, + \c{FOLDERID_RoamingAppData} usually points to \tt{C:\\Users\\\e{User Name}\\AppData\\Roaming}, also shown by the environment variable \c{%APPDATA%}. - \c{CSIDL_COMMON_APPDATA} usually points to \tt{C:\\ProgramData}. + \c{FOLDERID_ProgramData} usually points to \tt{C:\\ProgramData}. If the file format is IniFormat, this is "Settings/MySoft/Star Runner.ini" in the application's home directory. @@ -3366,8 +3349,8 @@ void QSettings::setUserIniPath(const QString &dir) \table \header \li Platform \li Format \li Scope \li Path - \row \li{1,2} Windows \li{1,2} IniFormat \li UserScope \li \c CSIDL_APPDATA - \row \li SystemScope \li \c CSIDL_COMMON_APPDATA + \row \li{1,2} Windows \li{1,2} IniFormat \li UserScope \li \c FOLDERID_RoamingAppData + \row \li SystemScope \li \c FOLDERID_ProgramData \row \li{1,2} Unix \li{1,2} NativeFormat, IniFormat \li UserScope \li \c $HOME/.config \row \li SystemScope \li \c /etc/xdg \row \li{1,2} Qt for Embedded Linux \li{1,2} NativeFormat, IniFormat \li UserScope \li \c $HOME/Settings |