QStandardPaths/Unix: fix logic in xdgDataDirs() function

This method correctly ignores relative paths (as per the XDG basedir
spec), but checking the list of dirs is empty should be moved to after
splitting the env var, because even if the env var is not empty, if the
paths in it are all relative the resulting list will be empty.

Drive-by change: Split some code to a static helper, which will be
used in xdgConfigDirs() too.

Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Change-Id: If894751ba68b24ccc214f9a4bb2099be3f0e4349
(cherry picked from commit ee515dd842d79fa4543568ed82bd7c949923e438)
(cherry picked from commit 668560f8bd13bd1478c1837ef0dc0eb33f6445f9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ahmad Samir 2023-01-29 21:52:51 +02:00 committed by Qt Cherry-pick Bot
parent 7ddba3c23f
commit 37fcf98d42

View File

@ -326,20 +326,15 @@ QString QStandardPaths::writableLocation(StandardLocation type)
return path;
}
static QStringList xdgDataDirs()
static QStringList dirsList(const QString &xdgEnvVar)
{
QStringList dirs;
// http://standards.freedesktop.org/basedir-spec/latest/
QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
if (xdgDataDirsEnv.isEmpty()) {
dirs.append(QString::fromLatin1("/usr/local/share"));
dirs.append(QString::fromLatin1("/usr/share"));
} else {
// Normalize paths, skip relative paths
for (const auto dir : qTokenize(xdgDataDirsEnv, u':')) {
// Normalize paths, skip relative paths (the spec says relative paths
// should be ignored)
for (const auto dir : qTokenize(xdgEnvVar, u':'))
if (dir.startsWith(u'/'))
dirs.push_back(QDir::cleanPath(dir.toString()));
}
// Remove duplicates from the list, there's no use for duplicated
// paths in XDG_DATA_DIRS - if it's not found in the given
@ -348,7 +343,19 @@ static QStringList xdgDataDirs()
// where duplicate paths here lead to duplicated mime types returned
// for a file, eg "text/plain,text/plain" instead of "text/plain"
dirs.removeDuplicates();
return dirs;
}
static QStringList xdgDataDirs()
{
// http://standards.freedesktop.org/basedir-spec/latest/
QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
QStringList dirs = dirsList(xdgDataDirsEnv);
if (dirs.isEmpty())
dirs = QStringList{u"/usr/local/share"_s, u"/usr/share"_s};
return dirs;
}