Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qstandardpaths.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qstandardpaths.h"
6
7#include <qdir.h>
8#include <qfileinfo.h>
9
10#ifndef QT_BOOTSTRAPPED
11#include <qobject.h>
12#include <qcoreapplication.h>
13#endif
14
15#if __has_include(<paths.h>)
16#include <paths.h>
17#endif
18
19#ifdef Q_OS_UNIX
20#include <unistd.h>
21#endif
22
23#ifndef QT_NO_STANDARDPATHS
24
26
27using namespace Qt::StringLiterals;
391static bool existsAsSpecified(const QString &path, QStandardPaths::LocateOptions options)
392{
394 return QDir(path).exists();
395 return QFileInfo(path).isFile();
396}
397
402{
403 const QStringList &dirs = standardLocations(type);
404 for (QStringList::const_iterator dir = dirs.constBegin(); dir != dirs.constEnd(); ++dir) {
405 const QString path = *dir + u'/' + fileName;
406 if (existsAsSpecified(path, options))
407 return path;
408 }
409 return QString();
410}
411
416{
417 const QStringList &dirs = standardLocations(type);
419 for (QStringList::const_iterator dir = dirs.constBegin(); dir != dirs.constEnd(); ++dir) {
420 const QString path = *dir + u'/' + fileName;
421 if (existsAsSpecified(path, options))
423 }
424 return result;
425}
426
427#ifdef Q_OS_WIN
428static QStringList executableExtensions()
429{
430 // If %PATHEXT% does not contain .exe, it is either empty, malformed, or distorted in ways that we cannot support, anyway.
431 const QStringList pathExt = QString::fromLocal8Bit(qgetenv("PATHEXT")).toLower().split(u';');
432 return pathExt.contains(".exe"_L1, Qt::CaseInsensitive) ?
433 pathExt : QStringList{".exe"_L1, ".com"_L1, ".bat"_L1, ".cmd"_L1};
434}
435#endif
436
438{
439 const QFileInfo info(path);
440 if (info.isBundle())
441 return info.bundleName();
442 if (info.isFile() && info.isExecutable())
443 return QDir::cleanPath(path);
444 return QString();
445}
446
447static inline QString searchExecutable(const QStringList &searchPaths,
448 const QString &executableName)
449{
450 const QDir currentDir = QDir::current();
451 for (const QString &searchPath : searchPaths) {
452 const QString candidate = currentDir.absoluteFilePath(searchPath + u'/' + executableName);
453 const QString absPath = checkExecutable(candidate);
454 if (!absPath.isEmpty())
455 return absPath;
456 }
457 return QString();
458}
459
460#ifdef Q_OS_WIN
461
462// Find executable appending candidate suffixes, used for suffix-less executables
463// on Windows.
464static inline QString
465 searchExecutableAppendSuffix(const QStringList &searchPaths,
466 const QString &executableName,
467 const QStringList &suffixes)
468{
469 const QDir currentDir = QDir::current();
470 for (const QString &searchPath : searchPaths) {
471 const QString candidateRoot = currentDir.absoluteFilePath(searchPath + u'/' + executableName);
472 for (const QString &suffix : suffixes) {
473 const QString absPath = checkExecutable(candidateRoot + suffix);
474 if (!absPath.isEmpty())
475 return absPath;
476 }
477 }
478 return QString();
479}
480
481#endif // Q_OS_WIN
482
487{
488 if (QFileInfo(executableName).isAbsolute())
489 return checkExecutable(executableName);
490
491 QStringList searchPaths = paths;
492 if (paths.isEmpty()) {
493 QByteArray pEnv = qgetenv("PATH");
494 if (Q_UNLIKELY(pEnv.isNull())) {
495 // Get a default path. POSIX.1 does not actually require this, but
496 // most Unix libc fall back to confstr(_CS_PATH) if the PATH
497 // environment variable isn't set. Let's try to do the same.
498#if defined(_PATH_DEFPATH)
499 // BSD API.
500 pEnv = _PATH_DEFPATH;
501#elif defined(_CS_PATH)
502 // POSIX API.
503 size_t n = confstr(_CS_PATH, nullptr, 0);
504 if (n) {
505 pEnv.resize(n);
506 // size()+1 is ok because QByteArray always has an extra NUL-terminator
507 confstr(_CS_PATH, pEnv.data(), pEnv.size() + 1);
508 }
509#else
510 // Windows SDK's execvpe() does not have a fallback, so we won't
511 // apply one either.
512#endif
513 }
514
515 // Remove trailing slashes, which occur on Windows.
516 const QStringList rawPaths = QString::fromLocal8Bit(pEnv.constData()).split(
518 searchPaths.reserve(rawPaths.size());
519 for (const QString &rawPath : rawPaths) {
520 QString cleanPath = QDir::cleanPath(rawPath);
521 if (cleanPath.size() > 1 && cleanPath.endsWith(u'/'))
522 cleanPath.truncate(cleanPath.size() - 1);
523 searchPaths.push_back(cleanPath);
524 }
525 }
526
527#ifdef Q_OS_WIN
528 // On Windows, if the name does not have a suffix or a suffix not
529 // in PATHEXT ("xx.foo"), append suffixes from PATHEXT.
530 static const QStringList executable_extensions = executableExtensions();
531 if (executableName.contains(u'.')) {
532 const QString suffix = QFileInfo(executableName).suffix();
533 if (suffix.isEmpty() || !executable_extensions.contains(u'.' + suffix, Qt::CaseInsensitive))
534 return searchExecutableAppendSuffix(searchPaths, executableName, executable_extensions);
535 } else {
536 return searchExecutableAppendSuffix(searchPaths, executableName, executable_extensions);
537 }
538#endif
539 return searchExecutable(searchPaths, executableName);
540}
541
548#if !defined(Q_OS_DARWIN) && !defined(QT_BOOTSTRAPPED)
550{
551 switch (type) {
552 case DesktopLocation:
553 return QCoreApplication::translate("QStandardPaths", "Desktop");
555 return QCoreApplication::translate("QStandardPaths", "Documents");
556 case FontsLocation:
557 return QCoreApplication::translate("QStandardPaths", "Fonts");
559 return QCoreApplication::translate("QStandardPaths", "Applications");
560 case MusicLocation:
561 return QCoreApplication::translate("QStandardPaths", "Music");
562 case MoviesLocation:
563 return QCoreApplication::translate("QStandardPaths", "Movies");
564 case PicturesLocation:
565 return QCoreApplication::translate("QStandardPaths", "Pictures");
566 case TempLocation:
567 return QCoreApplication::translate("QStandardPaths", "Temporary Directory");
568 case HomeLocation:
569 return QCoreApplication::translate("QStandardPaths", "Home");
571 return QCoreApplication::translate("QStandardPaths", "Application Data");
572 case CacheLocation:
573 return QCoreApplication::translate("QStandardPaths", "Cache");
574 case StateLocation:
575 return QCoreApplication::translate("QStandardPaths", "State");
577 return QCoreApplication::translate("QStandardPaths", "Shared Data");
578 case RuntimeLocation:
579 return QCoreApplication::translate("QStandardPaths", "Runtime");
580 case ConfigLocation:
581 return QCoreApplication::translate("QStandardPaths", "Configuration");
583 return QCoreApplication::translate("QStandardPaths", "Shared Configuration");
585 return QCoreApplication::translate("QStandardPaths", "Shared Cache");
587 return QCoreApplication::translate("QStandardPaths", "Shared State");
588 case DownloadLocation:
589 return QCoreApplication::translate("QStandardPaths", "Download");
590 case AppDataLocation:
592 return QCoreApplication::translate("QStandardPaths", "Application Configuration");
594 return QCoreApplication::translate("QStandardPaths", "Public");
596 return QCoreApplication::translate("QStandardPaths", "Templates");
597 }
598 // not reached
599 return QString();
600}
601#endif
602
609Q_CONSTINIT static bool qsp_testMode = false;
610
612{
613 qsp_testMode = testMode;
614}
615
628
629
631
632#ifndef QT_NO_QOBJECT
633#include "moc_qstandardpaths.cpp"
634#endif
635
636#endif // QT_NO_STANDARDPATHS
\inmodule QtCore
Definition qbytearray.h:57
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
\inmodule QtCore
Definition qdir.h:20
static QDir current()
Returns the application's current directory.
Definition qdir.h:219
bool exists() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qdir.cpp:1715
static QString cleanPath(const QString &path)
Returns path with directory separators normalized (that is, platform-native separators converted to "...
Definition qdir.cpp:2398
QString absoluteFilePath(const QString &fileName) const
Returns the absolute path name of a file in the directory.
Definition qdir.cpp:809
static constexpr QChar listSeparator() noexcept
Definition qdir.h:200
QString suffix() const
Returns the suffix (extension) of the file.
bool isFile() const
Returns true if this object points to a file or to a symbolic link to a file.
static QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options=LocateFile)
[0]
static bool isTestModeEnabled()
static void setTestModeEnabled(bool testMode)
static QStringList standardLocations(StandardLocation type)
static QString displayName(StandardLocation type)
static QString findExecutable(const QString &executableName, const QStringList &paths=QStringList())
static QString locate(StandardLocation type, const QString &fileName, LocateOptions options=LocateFile)
StandardLocation
This enum describes the different locations that can be queried using methods such as QStandardPaths:...
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
const_iterator constEnd() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing just after the last character in...
Definition qstring.h:1363
void truncate(qsizetype pos)
Truncates the string at the given position index.
Definition qstring.cpp:6319
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5949
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5506
QString & append(QChar c)
Definition qstring.cpp:3252
const_iterator constBegin() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in the st...
Definition qstring.h:1355
Combined button and popup list for selecting options.
@ CaseInsensitive
@ SkipEmptyParts
Definition qnamespace.h:128
#define Q_UNLIKELY(x)
GLenum type
GLsizei const GLuint * paths
GLfloat n
GLsizei const GLchar *const * path
GLuint64EXT * result
[6]
static bool existsAsSpecified(const QString &path, QStandardPaths::LocateOptions options)
static QString searchExecutable(const QStringList &searchPaths, const QString &executableName)
static Q_CONSTINIT bool qsp_testMode
static QString checkExecutable(const QString &path)
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
QString dir
[11]
QHostInfo info
[0]