QStandardPaths cleanup

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
Ivailo Monev 2019-12-14 22:33:55 +00:00
parent f2806e5133
commit dfab8f5fb3
3 changed files with 350 additions and 369 deletions

View file

@ -222,15 +222,15 @@ QT_BEGIN_NAMESPACE
/*! /*!
\fn QStringList QStandardPaths::standardLocations(StandardLocation type) \fn QStringList QStandardPaths::standardLocations(StandardLocation type)
Returns all the directories where files of \a type belong. Returns all the directories where files of \a type belong.
The list of directories is sorted from high to low priority, starting with The list of directories is sorted from high to low priority, starting with
writableLocation() if it can be determined. This list is empty if no locations writableLocation() if it can be determined. This list is empty if no locations
for \a type are defined. for \a type are defined.
\sa writableLocation() \sa writableLocation()
*/ */
/*! /*!
@ -246,117 +246,111 @@ QT_BEGIN_NAMESPACE
static bool existsAsSpecified(const QString &path, QStandardPaths::LocateOptions options) static bool existsAsSpecified(const QString &path, QStandardPaths::LocateOptions options)
{ {
if (options & QStandardPaths::LocateDirectory) { if (options & QStandardPaths::LocateDirectory) {
return QDir(path).exists(); return QDir(path).exists();
} }
return QFileInfo(path).isFile(); return QFileInfo(path).isFile();
} }
/*! /*!
Tries to find a file or directory called \a fileName in the standard locations Tries to find a file or directory called \a fileName in the standard locations
for \a type. for \a type.
The full path to the first file or directory (depending on \a options) found is returned. The full path to the first file or directory (depending on \a options) found is returned.
If no such file or directory can be found, an empty string is returned. If no such file or directory can be found, an empty string is returned.
*/ */
QString QStandardPaths::locate(StandardLocation type, const QString &fileName, LocateOptions options) QString QStandardPaths::locate(StandardLocation type, const QString &fileName, LocateOptions options)
{ {
const QStringList &dirs = standardLocations(type); foreach (const QString &dir, standardLocations(type)) {
for (QStringList::const_iterator dir = dirs.constBegin(); dir != dirs.constEnd(); ++dir) { const QString path = dir + QLatin1Char('/') + fileName;
const QString path = *dir + QLatin1Char('/') + fileName; if (existsAsSpecified(path, options)) {
if (existsAsSpecified(path, options)) { return path;
return path; }
} }
} return QString();
return QString();
} }
/*! /*!
Tries to find all files or directories called \a fileName in the standard locations Tries to find all files or directories called \a fileName in the standard locations
for \a type. for \a type.
The \a options flag allows to specify whether to look for files or directories. The \a options flag allows to specify whether to look for files or directories.
Returns the list of all the files that were found. Returns the list of all the files that were found.
*/ */
QStringList QStandardPaths::locateAll(StandardLocation type, const QString &fileName, LocateOptions options) QStringList QStandardPaths::locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
{ {
const QStringList &dirs = standardLocations(type); QStringList result;
QStringList result; foreach (const QString &dir, standardLocations(type)) {
for (QStringList::const_iterator dir = dirs.constBegin(); dir != dirs.constEnd(); ++dir) { const QString path = dir + QLatin1Char('/') + fileName;
const QString path = *dir + QLatin1Char('/') + fileName; if (existsAsSpecified(path, options)) {
if (existsAsSpecified(path, options)) { result.append(path);
result.append(path); }
} }
} return result;
return result;
} }
static QString checkExecutable(const QString &path) static QString checkExecutable(const QString &path)
{ {
const QFileInfo info(path); const QFileInfo info(path);
if (info.isFile() && info.isExecutable()) { if (info.isFile() && info.isExecutable()) {
return QDir::cleanPath(path); return QDir::cleanPath(path);
} }
return QString(); return QString();
} }
static inline QString searchExecutable(const QStringList &searchPaths, static inline QString searchExecutable(const QStringList &searchPaths,
const QString &executableName) const QString &executableName)
{ {
const QDir currentDir = QDir::current(); const QDir currentDir = QDir::current();
foreach (const QString & searchPath, searchPaths) { foreach (const QString &searchPath, searchPaths) {
const QString candidate = currentDir.absoluteFilePath(searchPath + QLatin1Char('/') + executableName); const QString candidate = currentDir.absoluteFilePath(searchPath + QLatin1Char('/') + executableName);
const QString absPath = checkExecutable(candidate); const QString absPath = checkExecutable(candidate);
if (!absPath.isEmpty()) { if (!absPath.isEmpty()) {
return absPath; return absPath;
} }
} }
return QString(); return QString();
} }
/*! /*!
Finds the executable named \a executableName in the paths specified by \a paths, Finds the executable named \a executableName in the paths specified by \a paths,
or the system paths if \a paths is empty. or the system paths if \a paths is empty.
On most operating systems the system path is determined by the PATH environment variable. On most operating systems the system path is determined by the PATH environment variable.
The directories where to search for the executable can be set in the \a paths argument. The directories where to search for the executable can be set in the \a paths argument.
To search in both your own paths and the system paths, call findExecutable twice, once with To search in both your own paths and the system paths, call findExecutable twice, once with
\a paths set and once with \a paths empty. \a paths set and once with \a paths empty.
Symlinks are not resolved, in order to preserve behavior for the case of executables Symlinks are not resolved, in order to preserve behavior for the case of executables
whose behavior depends on the name they are invoked with. whose behavior depends on the name they are invoked with.
\note On Windows, the usual executable extensions (from the PATHEXT environment variable) Returns the absolute file path to the executable, or an empty string if not found.
are automatically appended, so that for instance findExecutable("foo") will find foo.exe
or foo.bat if present.
Returns the absolute file path to the executable, or an empty string if not found.
*/ */
QString QStandardPaths::findExecutable(const QString &executableName, const QStringList &paths) QString QStandardPaths::findExecutable(const QString &executableName, const QStringList &paths)
{ {
if (QFileInfo(executableName).isAbsolute()) { if (QFileInfo(executableName).isAbsolute()) {
return checkExecutable(executableName); return checkExecutable(executableName);
} }
QStringList searchPaths = paths; QStringList searchPaths = paths;
if (paths.isEmpty()) { if (paths.isEmpty()) {
const QByteArray pEnv = qgetenv("PATH"); const QByteArray pEnv = qgetenv("PATH");
const QLatin1Char pathSep(':'); const QLatin1Char pathSep(':');
// Remove trailing slashes, which occur on Windows. // Remove trailing slashes, which occur on Windows.
const QStringList rawPaths = QString::fromLocal8Bit(pEnv.constData()).split(pathSep, QString::SkipEmptyParts); const QStringList rawPaths = QString::fromLocal8Bit(pEnv.constData()).split(pathSep, QString::SkipEmptyParts);
searchPaths.reserve(rawPaths.size()); searchPaths.reserve(rawPaths.size());
foreach (const QString & rawPath, rawPaths) { foreach (const QString & rawPath, rawPaths) {
QString cleanPath = QDir::cleanPath(rawPath); QString cleanPath = QDir::cleanPath(rawPath);
if (cleanPath.size() > 1 && cleanPath.endsWith(QLatin1Char('/'))) { if (cleanPath.size() > 1 && cleanPath.endsWith(QLatin1Char('/'))) {
cleanPath.truncate(cleanPath.size() - 1); cleanPath.truncate(cleanPath.size() - 1);
} }
searchPaths.push_back(cleanPath); searchPaths.push_back(cleanPath);
} }
} }
return searchExecutable(searchPaths, executableName); return searchExecutable(searchPaths, executableName);
} }
/*! /*!
@ -368,42 +362,42 @@ QString QStandardPaths::findExecutable(const QString &executableName, const QStr
QString QStandardPaths::displayName(StandardLocation type) QString QStandardPaths::displayName(StandardLocation type)
{ {
switch (type) { switch (type) {
case DesktopLocation: case DesktopLocation:
return QCoreApplication::translate("QStandardPaths", "Desktop"); return QCoreApplication::translate("QStandardPaths", "Desktop");
case DocumentsLocation: case DocumentsLocation:
return QCoreApplication::translate("QStandardPaths", "Documents"); return QCoreApplication::translate("QStandardPaths", "Documents");
case FontsLocation: case FontsLocation:
return QCoreApplication::translate("QStandardPaths", "Fonts"); return QCoreApplication::translate("QStandardPaths", "Fonts");
case ApplicationsLocation: case ApplicationsLocation:
return QCoreApplication::translate("QStandardPaths", "Applications"); return QCoreApplication::translate("QStandardPaths", "Applications");
case MusicLocation: case MusicLocation:
return QCoreApplication::translate("QStandardPaths", "Music"); return QCoreApplication::translate("QStandardPaths", "Music");
case MoviesLocation: case MoviesLocation:
return QCoreApplication::translate("QStandardPaths", "Movies"); return QCoreApplication::translate("QStandardPaths", "Movies");
case PicturesLocation: case PicturesLocation:
return QCoreApplication::translate("QStandardPaths", "Pictures"); return QCoreApplication::translate("QStandardPaths", "Pictures");
case TempLocation: case TempLocation:
return QCoreApplication::translate("QStandardPaths", "Temporary Directory"); return QCoreApplication::translate("QStandardPaths", "Temporary Directory");
case HomeLocation: case HomeLocation:
return QCoreApplication::translate("QStandardPaths", "Home"); return QCoreApplication::translate("QStandardPaths", "Home");
case DataLocation: case DataLocation:
return QCoreApplication::translate("QStandardPaths", "Application Data"); return QCoreApplication::translate("QStandardPaths", "Application Data");
case CacheLocation: case CacheLocation:
return QCoreApplication::translate("QStandardPaths", "Cache"); return QCoreApplication::translate("QStandardPaths", "Cache");
case GenericDataLocation: case GenericDataLocation:
return QCoreApplication::translate("QStandardPaths", "Shared Data"); return QCoreApplication::translate("QStandardPaths", "Shared Data");
case RuntimeLocation: case RuntimeLocation:
return QCoreApplication::translate("QStandardPaths", "Runtime"); return QCoreApplication::translate("QStandardPaths", "Runtime");
case ConfigLocation: case ConfigLocation:
return QCoreApplication::translate("QStandardPaths", "Configuration"); return QCoreApplication::translate("QStandardPaths", "Configuration");
case GenericConfigLocation: case GenericConfigLocation:
return QCoreApplication::translate("QStandardPaths", "Shared Configuration"); return QCoreApplication::translate("QStandardPaths", "Shared Configuration");
case GenericCacheLocation: case GenericCacheLocation:
return QCoreApplication::translate("QStandardPaths", "Shared Cache"); return QCoreApplication::translate("QStandardPaths", "Shared Cache");
case DownloadLocation: case DownloadLocation:
return QCoreApplication::translate("QStandardPaths", "Download"); return QCoreApplication::translate("QStandardPaths", "Download");
} }
// not reached // not reached
return QString(); return QString();
} }

View file

@ -35,46 +35,46 @@ QT_BEGIN_NAMESPACE
class Q_CORE_EXPORT QStandardPaths class Q_CORE_EXPORT QStandardPaths
{ {
public: public:
// Do not re-order, must match QDesktopServices // Do not re-order, must match QDesktopServices
enum StandardLocation { enum StandardLocation {
DesktopLocation, DesktopLocation,
DocumentsLocation, DocumentsLocation,
FontsLocation, FontsLocation,
ApplicationsLocation, ApplicationsLocation,
MusicLocation, MusicLocation,
MoviesLocation, MoviesLocation,
PicturesLocation, PicturesLocation,
TempLocation, TempLocation,
HomeLocation, HomeLocation,
DataLocation, DataLocation,
CacheLocation, CacheLocation,
GenericDataLocation, GenericDataLocation,
RuntimeLocation, RuntimeLocation,
ConfigLocation, ConfigLocation,
DownloadLocation, DownloadLocation,
GenericCacheLocation, GenericCacheLocation,
GenericConfigLocation GenericConfigLocation
}; };
static QString writableLocation(StandardLocation type); static QString writableLocation(StandardLocation type);
static QStringList standardLocations(StandardLocation type); static QStringList standardLocations(StandardLocation type);
enum LocateOption { enum LocateOption {
LocateFile = 0x0, LocateFile = 0x0,
LocateDirectory = 0x1 LocateDirectory = 0x1
}; };
typedef QFlags<LocateOption> LocateOptions; typedef QFlags<LocateOption> LocateOptions;
static QString locate(StandardLocation type, const QString &fileName, LocateOptions options = LocateFile); static QString locate(StandardLocation type, const QString &fileName, LocateOptions options = LocateFile);
static QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options = LocateFile); static QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options = LocateFile);
static QString displayName(StandardLocation type); static QString displayName(StandardLocation type);
static QString findExecutable(const QString &executableName, const QStringList &paths = QStringList()); static QString findExecutable(const QString &executableName, const QStringList &paths = QStringList());
static void enableTestMode(bool testMode); static void enableTestMode(bool testMode);
static void setTestModeEnabled(bool testMode); static void setTestModeEnabled(bool testMode);
static bool isTestModeEnabled(); static bool isTestModeEnabled();
private: private:
Q_DISABLE_COPY(QStandardPaths) Q_DISABLE_COPY(QStandardPaths)

View file

@ -41,243 +41,230 @@ QT_BEGIN_NAMESPACE
static void appendOrganizationAndApp(QString &path) static void appendOrganizationAndApp(QString &path)
{ {
const QString org = QCoreApplication::organizationName(); const QString org = QCoreApplication::organizationName();
if (!org.isEmpty()) { if (!org.isEmpty()) {
path += QLatin1Char('/') + org; path += QLatin1Char('/') + org;
} }
const QString appName = QCoreApplication::applicationName(); const QString appName = QCoreApplication::applicationName();
if (!appName.isEmpty()) { if (!appName.isEmpty()) {
path += QLatin1Char('/') + appName; path += QLatin1Char('/') + appName;
} }
} }
QString QStandardPaths::writableLocation(StandardLocation type) QString QStandardPaths::writableLocation(StandardLocation type)
{ {
switch (type) { switch (type) {
case HomeLocation: case DesktopLocation:
return QDir::homePath(); return QDir::homePath() + QLatin1String("/Desktop");
case TempLocation: case DocumentsLocation:
return QDir::tempPath(); return QDir::homePath() + QLatin1String("/Documents");
case CacheLocation: case PicturesLocation:
case GenericCacheLocation: { return QDir::homePath() + QLatin1String("/Pictures");
// http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html case FontsLocation:
QString xdgCacheHome = QFile::decodeName(qgetenv("XDG_CACHE_HOME")); return QDir::homePath() + QLatin1String("/.fonts");
if (xdgCacheHome.isEmpty()) { case MusicLocation:
xdgCacheHome = QDir::homePath() + QLatin1String("/.cache"); return QDir::homePath() + QLatin1String("/Music");
} case MoviesLocation:
if (type == QStandardPaths::CacheLocation) { return QDir::homePath() + QLatin1String("/Videos");
appendOrganizationAndApp(xdgCacheHome); case DownloadLocation:
} return QDir::homePath() + QLatin1String("/Downloads");
return xdgCacheHome; case ApplicationsLocation:
} return writableLocation(GenericDataLocation) + QLatin1String("/applications");
case DataLocation: case HomeLocation:
case GenericDataLocation: { return QDir::homePath();
QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME")); case TempLocation:
if (xdgDataHome.isEmpty()) { return QDir::tempPath();
xdgDataHome = QDir::homePath() + QLatin1String("/.local/share"); case CacheLocation:
} case GenericCacheLocation: {
if (type == QStandardPaths::DataLocation) { // http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
appendOrganizationAndApp(xdgDataHome); QString xdgCacheHome = QFile::decodeName(qgetenv("XDG_CACHE_HOME"));
} if (xdgCacheHome.isEmpty()) {
return xdgDataHome; xdgCacheHome = QDir::homePath() + QLatin1String("/.cache");
}
case ConfigLocation:
case GenericConfigLocation: {
// http://standards.freedesktop.org/basedir-spec/latest/
QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
if (xdgConfigHome.isEmpty()) {
xdgConfigHome = QDir::homePath() + QLatin1String("/.config");
}
return xdgConfigHome;
}
case RuntimeLocation: {
const uid_t myUid = geteuid();
// http://standards.freedesktop.org/basedir-spec/latest/
QString xdgRuntimeDir = QFile::decodeName(qgetenv("XDG_RUNTIME_DIR"));
if (xdgRuntimeDir.isEmpty()) {
const QString userName = QFileSystemEngine::resolveUserName(myUid);
xdgRuntimeDir = QDir::tempPath() + QLatin1String("/runtime-") + userName;
QDir dir(xdgRuntimeDir);
if (!dir.exists()) {
if (!QDir().mkdir(xdgRuntimeDir)) {
qWarning("QStandardPaths: error creating runtime directory %s: %s", qPrintable(xdgRuntimeDir),
qPrintable(qt_error_string(errno)));
return QString();
}
} }
qWarning("QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '%s'", qPrintable(xdgRuntimeDir)); if (type == QStandardPaths::CacheLocation) {
} appendOrganizationAndApp(xdgCacheHome);
// "The directory MUST be owned by the user"
QFileInfo fileInfo(xdgRuntimeDir);
if (fileInfo.ownerId() != myUid) {
qWarning("QStandardPaths: wrong ownership on runtime directory %s, %d instead of %d", qPrintable(xdgRuntimeDir),
fileInfo.ownerId(), myUid);
return QString();
}
// "and he MUST be the only one having read and write access to it. Its Unix access mode MUST be 0700."
QFile file(xdgRuntimeDir);
const QFile::Permissions wantedPerms = QFile::ReadUser | QFile::WriteUser | QFile::ExeUser;
if (file.permissions() != wantedPerms && !file.setPermissions(wantedPerms)) {
qWarning("QStandardPaths: wrong permissions on runtime directory %s", qPrintable(xdgRuntimeDir));
return QString();
}
return xdgRuntimeDir;
}
default:
break;
}
// http://www.freedesktop.org/wiki/Software/xdg-user-dirs
QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
if (xdgConfigHome.isEmpty()) {
xdgConfigHome = QDir::homePath() + QLatin1String("/.config");
}
QFile file(xdgConfigHome + QLatin1String("/user-dirs.dirs"));
if (file.open(QIODevice::ReadOnly)) {
QHash<QString, QString> lines;
QTextStream stream(&file);
// Only look for lines like: XDG_DESKTOP_DIR="$HOME/Desktop"
QRegExp exp(QLatin1String("^XDG_(.*)_DIR=(.*)$"));
while (!stream.atEnd()) {
const QString &line = stream.readLine();
if (exp.indexIn(line) != -1) {
const QStringList lst = exp.capturedTexts();
const QString key = lst.at(1);
QString value = lst.at(2);
if (value.length() > 2
&& value.startsWith(QLatin1Char('\"'))
&& value.endsWith(QLatin1Char('\"'))) {
value = value.mid(1, value.length() - 2);
} }
// Store the key and value: "DESKTOP", "$HOME/Desktop" return xdgCacheHome;
lines[key] = value; }
} case DataLocation:
} case GenericDataLocation: {
QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
QString key; if (xdgDataHome.isEmpty()) {
switch (type) { xdgDataHome = QDir::homePath() + QLatin1String("/.local/share");
case DesktopLocation:
key = QLatin1String("DESKTOP");
break;
case DocumentsLocation:
key = QLatin1String("DOCUMENTS");
break;
case PicturesLocation:
key = QLatin1String("PICTURES");
break;
case MusicLocation:
key = QLatin1String("MUSIC");
break;
case MoviesLocation:
key = QLatin1String("VIDEOS");
break;
case DownloadLocation:
key = QLatin1String("DOWNLOAD");
break;
default:
break;
}
if (!key.isEmpty()) {
QString value = lines.value(key);
if (!value.isEmpty()) {
// value can start with $HOME
if (value.startsWith(QLatin1String("$HOME"))) {
value = QDir::homePath() + value.mid(5);
} }
return value; if (type == QStandardPaths::DataLocation) {
} appendOrganizationAndApp(xdgDataHome);
} }
} return xdgDataHome;
}
case ConfigLocation:
case GenericConfigLocation: {
// http://standards.freedesktop.org/basedir-spec/latest/
QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
if (xdgConfigHome.isEmpty()) {
xdgConfigHome = QDir::homePath() + QLatin1String("/.config");
}
return xdgConfigHome;
}
case RuntimeLocation: {
const uid_t myUid = geteuid();
// http://standards.freedesktop.org/basedir-spec/latest/
QString xdgRuntimeDir = QFile::decodeName(qgetenv("XDG_RUNTIME_DIR"));
if (xdgRuntimeDir.isEmpty()) {
const QString userName = QFileSystemEngine::resolveUserName(myUid);
xdgRuntimeDir = QDir::tempPath() + QLatin1String("/runtime-") + userName;
QDir dir(xdgRuntimeDir);
if (!dir.exists() && !QDir().mkdir(xdgRuntimeDir)) {
qWarning("QStandardPaths: error creating runtime directory %s: %s", qPrintable(xdgRuntimeDir),
qPrintable(qt_error_string(errno)));
return QString();
}
qWarning("QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '%s'", qPrintable(xdgRuntimeDir));
}
// "The directory MUST be owned by the user"
QFileInfo fileInfo(xdgRuntimeDir);
if (fileInfo.ownerId() != myUid) {
qWarning("QStandardPaths: wrong ownership on runtime directory %s, %d instead of %d", qPrintable(xdgRuntimeDir),
fileInfo.ownerId(), myUid);
return QString();
}
// "and he MUST be the only one having read and write access to it. Its Unix access mode MUST be 0700."
QFile file(xdgRuntimeDir);
const QFile::Permissions wantedPerms = QFile::ReadUser | QFile::WriteUser | QFile::ExeUser;
if (file.permissions() != wantedPerms && !file.setPermissions(wantedPerms)) {
qWarning("QStandardPaths: wrong permissions on runtime directory %s", qPrintable(xdgRuntimeDir));
return QString();
}
return xdgRuntimeDir;
}
default:
break;
}
QString path; // http://www.freedesktop.org/wiki/Software/xdg-user-dirs
switch (type) { QString xdgConfigHome = QFile::decodeName(qgetenv("XDG_CONFIG_HOME"));
case DesktopLocation: if (xdgConfigHome.isEmpty()) {
path = QDir::homePath() + QLatin1String("/Desktop"); xdgConfigHome = QDir::homePath() + QLatin1String("/.config");
break; }
case DocumentsLocation: QFile file(xdgConfigHome + QLatin1String("/user-dirs.dirs"));
path = QDir::homePath() + QLatin1String("/Documents"); if (file.open(QIODevice::ReadOnly)) {
break; QHash<QString, QString> lines;
case PicturesLocation: QTextStream stream(&file);
path = QDir::homePath() + QLatin1String("/Pictures"); // Only look for lines like: XDG_DESKTOP_DIR="$HOME/Desktop"
break; QRegExp exp(QLatin1String("^XDG_(.*)_DIR=(.*)$"));
while (!stream.atEnd()) {
const QString &line = stream.readLine();
if (exp.indexIn(line) != -1) {
const QStringList lst = exp.capturedTexts();
const QString key = lst.at(1);
QString value = lst.at(2);
if (value.length() > 2
&& value.startsWith(QLatin1Char('\"'))
&& value.endsWith(QLatin1Char('\"'))) {
value = value.mid(1, value.length() - 2);
}
// Store the key and value: "DESKTOP", "$HOME/Desktop"
lines[key] = value;
}
}
case FontsLocation: QString key;
path = QDir::homePath() + QLatin1String("/.fonts"); switch (type) {
break; case DesktopLocation: {
key = QLatin1String("DESKTOP");
break;
}
case DocumentsLocation: {
key = QLatin1String("DOCUMENTS");
break;
}
case PicturesLocation: {
key = QLatin1String("PICTURES");
break;
}
case MusicLocation: {
key = QLatin1String("MUSIC");
break;
}
case MoviesLocation: {
key = QLatin1String("VIDEOS");
break;
}
case DownloadLocation: {
key = QLatin1String("DOWNLOAD");
break;
}
default:
break;
}
if (!key.isEmpty()) {
QString value = lines.value(key);
if (!value.isEmpty()) {
// value can start with $HOME
if (value.startsWith(QLatin1String("$HOME"))) {
value = QDir::homePath() + value.mid(5);
}
return value;
}
}
}
case MusicLocation: return QString();
path = QDir::homePath() + QLatin1String("/Music");
break;
case MoviesLocation:
path = QDir::homePath() + QLatin1String("/Videos");
break;
case DownloadLocation:
path = QDir::homePath() + QLatin1String("/Downloads");
break;
case ApplicationsLocation:
path = writableLocation(GenericDataLocation) + QLatin1String("/applications");
break;
default:
break;
}
return path;
} }
static QStringList xdgDataDirs() static QStringList xdgDataDirs()
{ {
QStringList dirs; // http://standards.freedesktop.org/basedir-spec/latest/
// http://standards.freedesktop.org/basedir-spec/latest/ QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS")); if (xdgDataDirsEnv.isEmpty()) {
if (xdgDataDirsEnv.isEmpty()) { QStringList dirs;
dirs.append(QString::fromLatin1("/usr/local/share")); dirs.append(QString::fromLatin1("/usr/local/share"));
dirs.append(QString::fromLatin1("/usr/share")); dirs.append(QString::fromLatin1("/usr/share"));
} else { return dirs;
dirs = xdgDataDirsEnv.split(QLatin1Char(':')); }
} return xdgDataDirsEnv.split(QLatin1Char(':'));
return dirs;
} }
QStringList QStandardPaths::standardLocations(StandardLocation type) QStringList QStandardPaths::standardLocations(StandardLocation type)
{ {
QStringList dirs; QStringList dirs;
switch (type) { switch (type) {
case ConfigLocation: case ConfigLocation:
case GenericConfigLocation: { case GenericConfigLocation: {
// http://standards.freedesktop.org/basedir-spec/latest/ // http://standards.freedesktop.org/basedir-spec/latest/
const QString xdgConfigDirs = QFile::decodeName(qgetenv("XDG_CONFIG_DIRS")); const QString xdgConfigDirs = QFile::decodeName(qgetenv("XDG_CONFIG_DIRS"));
if (xdgConfigDirs.isEmpty()) { if (xdgConfigDirs.isEmpty()) {
dirs.append(QString::fromLatin1("/etc/xdg")); dirs.append(QString::fromLatin1("/etc/xdg"));
} else { } else {
dirs = xdgConfigDirs.split(QLatin1Char(':')); dirs = xdgConfigDirs.split(QLatin1Char(':'));
} }
} break;
break; }
case GenericDataLocation: case GenericDataLocation: {
dirs = xdgDataDirs(); dirs = xdgDataDirs();
break; break;
case ApplicationsLocation: }
dirs = xdgDataDirs(); case ApplicationsLocation: {
for (int i = 0; i < dirs.count(); ++i) { dirs = xdgDataDirs();
dirs[i].append(QLatin1String("/applications")); for (int i = 0; i < dirs.count(); ++i) {
} dirs[i].append(QLatin1String("/applications"));
break; }
case DataLocation: break;
dirs = xdgDataDirs(); }
for (int i = 0; i < dirs.count(); ++i) { case DataLocation: {
appendOrganizationAndApp(dirs[i]); dirs = xdgDataDirs();
} for (int i = 0; i < dirs.count(); ++i) {
break; appendOrganizationAndApp(dirs[i]);
default: }
break; break;
} }
const QString localDir = writableLocation(type); default:
dirs.prepend(localDir); break;
return dirs; }
dirs.prepend(writableLocation(type));
return dirs;
} }
QT_END_NAMESPACE QT_END_NAMESPACE