QStandardPaths/Unix: minor cleanup

- Better readability by returning early
- Make a QRegularExpression static const so that the regex pattern is
  compiled only once

Pick-to: 6.4 6.2
Change-Id: Ic0d6bd4b068624c4ff60c6e71d09f20393adaa64
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 482a75fef9643220366029862dbf636a88bccb82)
Reviewed-by: Daniel Smith <Daniel.Smith@qt.io>
This commit is contained in:
Ahmad Samir 2023-01-29 21:04:52 +02:00 committed by Mårten Nordheim
parent 27761744b6
commit f4eacea72b

View File

@ -180,10 +180,11 @@ QString QStandardPaths::writableLocation(StandardLocation type)
case CacheLocation: case CacheLocation:
case GenericCacheLocation: case GenericCacheLocation:
{ {
if (isTestModeEnabled())
return QDir::homePath() + "/.qttest/cache"_L1;
// http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html // http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
QString xdgCacheHome = QFile::decodeName(qgetenv("XDG_CACHE_HOME")); QString xdgCacheHome = QFile::decodeName(qgetenv("XDG_CACHE_HOME"));
if (isTestModeEnabled())
xdgCacheHome = QDir::homePath() + "/.qttest/cache"_L1;
if (xdgCacheHome.isEmpty()) if (xdgCacheHome.isEmpty())
xdgCacheHome = QDir::homePath() + "/.cache"_L1; xdgCacheHome = QDir::homePath() + "/.cache"_L1;
if (type == QStandardPaths::CacheLocation) if (type == QStandardPaths::CacheLocation)
@ -194,9 +195,10 @@ QString QStandardPaths::writableLocation(StandardLocation type)
case AppLocalDataLocation: case AppLocalDataLocation:
case GenericDataLocation: case GenericDataLocation:
{ {
QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
if (isTestModeEnabled()) if (isTestModeEnabled())
xdgDataHome = QDir::homePath() + "/.qttest/share"_L1; return QDir::homePath() + "/.qttest/share"_L1;
QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
if (xdgDataHome.isEmpty()) if (xdgDataHome.isEmpty())
xdgDataHome = QDir::homePath() + "/.local/share"_L1; xdgDataHome = QDir::homePath() + "/.local/share"_L1;
if (type == AppDataLocation || type == AppLocalDataLocation) if (type == AppDataLocation || type == AppLocalDataLocation)
@ -207,10 +209,11 @@ QString QStandardPaths::writableLocation(StandardLocation type)
case GenericConfigLocation: case GenericConfigLocation:
case AppConfigLocation: case AppConfigLocation:
{ {
if (isTestModeEnabled())
return QDir::homePath() + "/.qttest/config"_L1;
// http://standards.freedesktop.org/basedir-spec/latest/ // http://standards.freedesktop.org/basedir-spec/latest/
QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME")); QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
if (isTestModeEnabled())
xdgConfigHome = QDir::homePath() + "/.qttest/config"_L1;
if (xdgConfigHome.isEmpty()) if (xdgConfigHome.isEmpty())
xdgConfigHome = QDir::homePath() + "/.config"_L1; xdgConfigHome = QDir::homePath() + "/.config"_L1;
if (type == AppConfigLocation) if (type == AppConfigLocation)
@ -253,7 +256,7 @@ QString QStandardPaths::writableLocation(StandardLocation type)
if (!key.isEmpty() && !isTestModeEnabled() && file.open(QIODevice::ReadOnly)) { if (!key.isEmpty() && !isTestModeEnabled() && file.open(QIODevice::ReadOnly)) {
QTextStream stream(&file); QTextStream stream(&file);
// Only look for lines like: XDG_DESKTOP_DIR="$HOME/Desktop" // Only look for lines like: XDG_DESKTOP_DIR="$HOME/Desktop"
QRegularExpression exp("^XDG_(.*)_DIR=(.*)$"_L1); static const QRegularExpression exp(u"^XDG_(.*)_DIR=(.*)$"_s);
QString result; QString result;
while (!stream.atEnd()) { while (!stream.atEnd()) {
const QString &line = stream.readLine(); const QString &line = stream.readLine();