| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Intel Corporation. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #include "qstandardpaths.h" |
| 42 | |
| 43 | #include <qdir.h> |
| 44 | #include <qfileinfo.h> |
| 45 | |
| 46 | #ifndef QT_BOOTSTRAPPED |
| 47 | #include <qobject.h> |
| 48 | #include <qcoreapplication.h> |
| 49 | #endif |
| 50 | |
| 51 | #if __has_include(<paths.h>) |
| 52 | #include <paths.h> |
| 53 | #endif |
| 54 | |
| 55 | #ifdef Q_OS_UNIX |
| 56 | #include <unistd.h> |
| 57 | #endif |
| 58 | |
| 59 | #ifndef QT_NO_STANDARDPATHS |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | /*! |
| 64 | \class QStandardPaths |
| 65 | \inmodule QtCore |
| 66 | \brief The QStandardPaths class provides methods for accessing standard paths. |
| 67 | \since 5.0 |
| 68 | |
| 69 | This class contains functions to query standard locations on the local |
| 70 | filesystem, for common tasks such as user-specific directories or system-wide |
| 71 | configuration directories. |
| 72 | */ |
| 73 | |
| 74 | /*! |
| 75 | \enum QStandardPaths::StandardLocation |
| 76 | |
| 77 | This enum describes the different locations that can be queried using |
| 78 | methods such as QStandardPaths::writableLocation, QStandardPaths::standardLocations, |
| 79 | and QStandardPaths::displayName. |
| 80 | |
| 81 | Some of the values in this enum represent a user configuration. Such enum |
| 82 | values will return the same paths in different applications, so they could |
| 83 | be used to share data with other applications. Other values are specific to |
| 84 | this application. Each enum value in the table below describes whether it's |
| 85 | application-specific or generic. |
| 86 | |
| 87 | Application-specific directories should be assumed to be unreachable by |
| 88 | other applications. Therefore, files placed there might not be readable by |
| 89 | other applications, even if run by the same user. On the other hand, generic |
| 90 | directories should be assumed to be accessible by all applications run by |
| 91 | this user, but should still be assumed to be unreachable by applications by |
| 92 | other users. |
| 93 | |
| 94 | Data interchange with other users is out of the scope of QStandardPaths. |
| 95 | |
| 96 | \value DesktopLocation Returns the user's desktop directory. This is a generic value. |
| 97 | On systems with no concept of a desktop, this is the same as |
| 98 | QStandardPaths::HomeLocation. |
| 99 | \value DocumentsLocation Returns the directory containing user document files. |
| 100 | This is a generic value. The returned path is never empty. |
| 101 | \value FontsLocation Returns the directory containing user's fonts. This is a generic value. |
| 102 | Note that installing fonts may require additional, platform-specific operations. |
| 103 | \value ApplicationsLocation Returns the directory containing the user applications |
| 104 | (either executables, application bundles, or shortcuts to them). This is a generic value. |
| 105 | Note that installing applications may require additional, platform-specific operations. |
| 106 | Files, folders or shortcuts in this directory are platform-specific. |
| 107 | \value MusicLocation Returns the directory containing the user's music or other audio files. |
| 108 | This is a generic value. If no directory specific for music files exists, a sensible |
| 109 | fallback for storing user documents is returned. |
| 110 | \value MoviesLocation Returns the directory containing the user's movies and videos. |
| 111 | This is a generic value. If no directory specific for movie files exists, a sensible |
| 112 | fallback for storing user documents is returned. |
| 113 | \value PicturesLocation Returns the directory containing the user's pictures or photos. |
| 114 | This is a generic value. If no directory specific for picture files exists, a sensible |
| 115 | fallback for storing user documents is returned. |
| 116 | \value TempLocation Returns a directory where temporary files can be stored. The returned value |
| 117 | might be application-specific, shared among other applications for this user, or even |
| 118 | system-wide. The returned path is never empty. |
| 119 | \value HomeLocation Returns the user's home directory (the same as QDir::homePath()). On Unix |
| 120 | systems, this is equal to the HOME environment variable. This value might be |
| 121 | generic or application-specific, but the returned path is never empty. |
| 122 | \value DataLocation Returns the same value as AppLocalDataLocation. This enumeration value |
| 123 | is deprecated. Using AppDataLocation is preferable since on Windows, the roaming path is |
| 124 | recommended. |
| 125 | \value CacheLocation Returns a directory location where user-specific |
| 126 | non-essential (cached) data should be written. This is an application-specific directory. |
| 127 | The returned path is never empty. |
| 128 | \value GenericCacheLocation Returns a directory location where user-specific non-essential |
| 129 | (cached) data, shared across applications, should be written. This is a generic value. |
| 130 | Note that the returned path may be empty if the system has no concept of shared cache. |
| 131 | \value GenericDataLocation Returns a directory location where persistent |
| 132 | data shared across applications can be stored. This is a generic value. The returned |
| 133 | path is never empty. |
| 134 | \value RuntimeLocation Returns a directory location where runtime communication |
| 135 | files should be written, like Unix local sockets. This is a generic value. |
| 136 | The returned path may be empty on some systems. |
| 137 | \value ConfigLocation Returns a directory location where user-specific |
| 138 | configuration files should be written. This may be either a generic value |
| 139 | or application-specific, and the returned path is never empty. |
| 140 | \value DownloadLocation Returns a directory for user's downloaded files. This is a generic value. |
| 141 | If no directory specific for downloads exists, a sensible fallback for storing user |
| 142 | documents is returned. |
| 143 | \value GenericConfigLocation Returns a directory location where user-specific |
| 144 | configuration files shared between multiple applications should be written. |
| 145 | This is a generic value and the returned path is never empty. |
| 146 | \value AppDataLocation Returns a directory location where persistent |
| 147 | application data can be stored. This is an application-specific directory. |
| 148 | To obtain a path to store data to be shared with other applications, use |
| 149 | QStandardPaths::GenericDataLocation. The returned path is never empty. |
| 150 | On the Windows operating system, this returns the roaming path. |
| 151 | This enum value was added in Qt 5.4. |
| 152 | \value AppLocalDataLocation Returns the local settings path on the Windows operating |
| 153 | system. On all other platforms, it returns the same value as AppDataLocation. |
| 154 | This enum value was added in Qt 5.4. |
| 155 | \value AppConfigLocation Returns a directory location where user-specific |
| 156 | configuration files should be written. This is an application-specific directory, |
| 157 | and the returned path is never empty. |
| 158 | This enum value was added in Qt 5.5. |
| 159 | |
| 160 | The following table gives examples of paths on different operating systems. |
| 161 | The first path is the writable path (unless noted). Other, additional |
| 162 | paths, if any, represent non-writable locations. |
| 163 | |
| 164 | \table |
| 165 | \header \li Path type \li \macos \li Windows |
| 166 | \row \li DesktopLocation |
| 167 | \li "~/Desktop" |
| 168 | \li "C:/Users/<USER>/Desktop" |
| 169 | \row \li DocumentsLocation |
| 170 | \li "~/Documents" |
| 171 | \li "C:/Users/<USER>/Documents" |
| 172 | \row \li FontsLocation |
| 173 | \li "/System/Library/Fonts" (not writable) |
| 174 | \li "C:/Windows/Fonts" (not writable) |
| 175 | \row \li ApplicationsLocation |
| 176 | \li "/Applications" (not writable) |
| 177 | \li "C:/Users/<USER>/AppData/Roaming/Microsoft/Windows/Start Menu/Programs" |
| 178 | \row \li MusicLocation |
| 179 | \li "~/Music" |
| 180 | \li "C:/Users/<USER>/Music" |
| 181 | \row \li MoviesLocation |
| 182 | \li "~/Movies" |
| 183 | \li "C:/Users/<USER>/Videos" |
| 184 | \row \li PicturesLocation |
| 185 | \li "~/Pictures" |
| 186 | \li "C:/Users/<USER>/Pictures" |
| 187 | \row \li TempLocation |
| 188 | \li randomly generated by the OS |
| 189 | \li "C:/Users/<USER>/AppData/Local/Temp" |
| 190 | \row \li HomeLocation |
| 191 | \li "~" |
| 192 | \li "C:/Users/<USER>" |
| 193 | \row \li DataLocation |
| 194 | \li "~/Library/Application Support/<APPNAME>", "/Library/Application Support/<APPNAME>". "<APPDIR>/../Resources" |
| 195 | \li "C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>", "<APPDIR>", "<APPDIR>/data", "<APPDIR>/data/<APPNAME>" |
| 196 | \row \li CacheLocation |
| 197 | \li "~/Library/Caches/<APPNAME>", "/Library/Caches/<APPNAME>" |
| 198 | \li "C:/Users/<USER>/AppData/Local/<APPNAME>/cache" |
| 199 | \row \li GenericDataLocation |
| 200 | \li "~/Library/Application Support", "/Library/Application Support" |
| 201 | \li "C:/Users/<USER>/AppData/Local", "C:/ProgramData", "<APPDIR>", "<APPDIR>/data" |
| 202 | \row \li RuntimeLocation |
| 203 | \li "~/Library/Application Support" |
| 204 | \li "C:/Users/<USER>" |
| 205 | \row \li ConfigLocation |
| 206 | \li "~/Library/Preferences" |
| 207 | \li "C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>" |
| 208 | \row \li GenericConfigLocation |
| 209 | \li "~/Library/Preferences" |
| 210 | \li "C:/Users/<USER>/AppData/Local", "C:/ProgramData" |
| 211 | \row \li DownloadLocation |
| 212 | \li "~/Downloads" |
| 213 | \li "C:/Users/<USER>/Documents" |
| 214 | \row \li GenericCacheLocation |
| 215 | \li "~/Library/Caches", "/Library/Caches" |
| 216 | \li "C:/Users/<USER>/AppData/Local/cache" |
| 217 | \row \li AppDataLocation |
| 218 | \li "~/Library/Application Support/<APPNAME>", "/Library/Application Support/<APPNAME>". "<APPDIR>/../Resources" |
| 219 | \li "C:/Users/<USER>/AppData/Roaming/<APPNAME>", "C:/ProgramData/<APPNAME>", "<APPDIR>", "<APPDIR>/data", "<APPDIR>/data/<APPNAME>" |
| 220 | \row \li AppLocalDataLocation |
| 221 | \li "~/Library/Application Support/<APPNAME>", "/Library/Application Support/<APPNAME>". "<APPDIR>/../Resources" |
| 222 | \li "C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>", "<APPDIR>", "<APPDIR>/data", "<APPDIR>/data/<APPNAME>" |
| 223 | \row \li AppConfigLocation |
| 224 | \li "~/Library/Preferences/<APPNAME>" |
| 225 | \li "C:/Users/<USER>/AppData/Local/<APPNAME>", "C:/ProgramData/<APPNAME>" |
| 226 | \endtable |
| 227 | |
| 228 | \table |
| 229 | \header \li Path type \li Linux |
| 230 | \row \li DesktopLocation |
| 231 | \li "~/Desktop" |
| 232 | \row \li DocumentsLocation |
| 233 | \li "~/Documents" |
| 234 | \row \li FontsLocation |
| 235 | \li "~/.fonts", "~/.local/share/fonts", "/usr/local/share/fonts", "/usr/share/fonts" |
| 236 | \row \li ApplicationsLocation |
| 237 | \li "~/.local/share/applications", "/usr/local/share/applications", "/usr/share/applications" |
| 238 | \row \li MusicLocation |
| 239 | \li "~/Music" |
| 240 | \row \li MoviesLocation |
| 241 | \li "~/Videos" |
| 242 | \row \li PicturesLocation |
| 243 | \li "~/Pictures" |
| 244 | \row \li TempLocation |
| 245 | \li "/tmp" |
| 246 | \row \li HomeLocation |
| 247 | \li "~" |
| 248 | \row \li DataLocation |
| 249 | \li "~/.local/share/<APPNAME>", "/usr/local/share/<APPNAME>", "/usr/share/<APPNAME>" |
| 250 | \row \li CacheLocation |
| 251 | \li "~/.cache/<APPNAME>" |
| 252 | \row \li GenericDataLocation |
| 253 | \li "~/.local/share", "/usr/local/share", "/usr/share" |
| 254 | \row \li RuntimeLocation |
| 255 | \li "/run/user/<USER>" |
| 256 | \row \li ConfigLocation |
| 257 | \li "~/.config", "/etc/xdg" |
| 258 | \row \li GenericConfigLocation |
| 259 | \li "~/.config", "/etc/xdg" |
| 260 | \row \li DownloadLocation |
| 261 | \li "~/Downloads" |
| 262 | \row \li GenericCacheLocation |
| 263 | \li "~/.cache" |
| 264 | \row \li AppDataLocation |
| 265 | \li "~/.local/share/<APPNAME>", "/usr/local/share/<APPNAME>", "/usr/share/<APPNAME>" |
| 266 | \row \li AppLocalDataLocation |
| 267 | \li "~/.local/share/<APPNAME>", "/usr/local/share/<APPNAME>", "/usr/share/<APPNAME>" |
| 268 | \row \li AppConfigLocation |
| 269 | \li "~/.config/<APPNAME>", "/etc/xdg/<APPNAME>" |
| 270 | \endtable |
| 271 | |
| 272 | \table |
| 273 | \header \li Path type \li Android \li iOS |
| 274 | \row \li DesktopLocation |
| 275 | \li "<APPROOT>/files" |
| 276 | \li "<APPROOT>/Documents/Desktop" |
| 277 | \row \li DocumentsLocation |
| 278 | \li "<USER>/Documents", "<USER>/<APPNAME>/Documents" |
| 279 | \li "<APPROOT>/Documents" |
| 280 | \row \li FontsLocation |
| 281 | \li "/system/fonts" (not writable) |
| 282 | \li "<APPROOT>/Library/Fonts" |
| 283 | \row \li ApplicationsLocation |
| 284 | \li not supported (directory not readable) |
| 285 | \li not supported |
| 286 | \row \li MusicLocation |
| 287 | \li "<USER>/Music", "<USER>/<APPNAME>/Music" |
| 288 | \li "<APPROOT>/Documents/Music" |
| 289 | \row \li MoviesLocation |
| 290 | \li "<USER>/Movies", "<USER>/<APPNAME>/Movies" |
| 291 | \li "<APPROOT>/Documents/Movies" |
| 292 | \row \li PicturesLocation |
| 293 | \li "<USER>/Pictures", "<USER>/<APPNAME>/Pictures" |
| 294 | \li "<APPROOT>/Documents/Pictures", "assets-library://" |
| 295 | \row \li TempLocation |
| 296 | \li "<APPROOT>/cache" |
| 297 | \li "<APPROOT>/tmp" |
| 298 | \row \li HomeLocation |
| 299 | \li "<APPROOT>/files" |
| 300 | \li system defined |
| 301 | \row \li DataLocation |
| 302 | \li "<APPROOT>/files", "<USER>/<APPNAME>/files" |
| 303 | \li "<APPROOT>/Library/Application Support" |
| 304 | \row \li CacheLocation |
| 305 | \li "<APPROOT>/cache", "<USER>/<APPNAME>/cache" |
| 306 | \li "<APPROOT>/Library/Caches" |
| 307 | \row \li GenericDataLocation |
| 308 | \li "<USER>" |
| 309 | \li "<APPROOT>/Library/Application Support" |
| 310 | \row \li RuntimeLocation |
| 311 | \li "<APPROOT>/cache" |
| 312 | \li not supported |
| 313 | \row \li ConfigLocation |
| 314 | \li "<APPROOT>/files/settings" |
| 315 | \li "<APPROOT>/Library/Preferences" |
| 316 | \row \li GenericConfigLocation |
| 317 | \li "<APPROOT>/files/settings" (there is no shared settings) |
| 318 | \li "<APPROOT>/Library/Preferences" |
| 319 | \row \li DownloadLocation |
| 320 | \li "<USER>/Downloads", "<USER>/<APPNAME>/Downloads" |
| 321 | \li "<APPROOT>/Documents/Downloads" |
| 322 | \row \li GenericCacheLocation |
| 323 | \li "<APPROOT>/cache" (there is no shared cache) |
| 324 | \li "<APPROOT>/Library/Caches" |
| 325 | \row \li AppDataLocation |
| 326 | \li "<APPROOT>/files", "<USER>/<APPNAME>/files" |
| 327 | \li "<APPROOT>/Library/Application Support" |
| 328 | \row \li AppConfigLocation |
| 329 | \li "<APPROOT>/files/settings" |
| 330 | \li "<APPROOT>/Library/Preferences/<APPNAME>" |
| 331 | \row \li AppLocalDataLocation |
| 332 | \li "<APPROOT>/files", "<USER>/<APPNAME>/files" |
| 333 | \li "<APPROOT>/Library/Application Support" |
| 334 | \endtable |
| 335 | |
| 336 | In the table above, \c <APPNAME> is usually the organization name, the |
| 337 | application name, or both, or a unique name generated at packaging. |
| 338 | Similarly, <APPROOT> is the location where this application is installed |
| 339 | (often a sandbox). <APPDIR> is the directory containing the application |
| 340 | executable. |
| 341 | |
| 342 | The paths above should not be relied upon, as they may change according to |
| 343 | OS configuration, locale, or they may change in future Qt versions. |
| 344 | |
| 345 | \note On Android, applications with open files on the external storage (<USER> locations), |
| 346 | will be killed if the external storage is unmounted. |
| 347 | |
| 348 | \note On Android 6.0 (API 23) or higher, the "WRITE_EXTERNAL_STORAGE" permission must be |
| 349 | requested at runtime when using QStandardPaths::writableLocation or QStandardPaths::standardLocations. |
| 350 | |
| 351 | \note On iOS, if you do pass \c {QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).last()} |
| 352 | as argument to \l{QFileDialog::setDirectory()}, |
| 353 | a native image picker dialog will be used for accessing the user's photo album. |
| 354 | The filename returned can be loaded using QFile and related APIs. |
| 355 | This feature was added in Qt 5.5. |
| 356 | |
| 357 | \sa writableLocation(), standardLocations(), displayName(), locate(), locateAll() |
| 358 | */ |
| 359 | |
| 360 | /*! |
| 361 | \fn QString QStandardPaths::writableLocation(StandardLocation type) |
| 362 | |
| 363 | \include standardpath/functiondocs.qdocinc writableLocation |
| 364 | */ |
| 365 | |
| 366 | |
| 367 | /*! |
| 368 | \fn QStringList QStandardPaths::standardLocations(StandardLocation type) |
| 369 | |
| 370 | \include standardpath/functiondocs.qdocinc standardLocations |
| 371 | |
| 372 | \sa writableLocation() |
| 373 | */ |
| 374 | |
| 375 | /*! |
| 376 | \enum QStandardPaths::LocateOption |
| 377 | |
| 378 | This enum describes the different flags that can be used for |
| 379 | controlling the behavior of QStandardPaths::locate and |
| 380 | QStandardPaths::locateAll. |
| 381 | |
| 382 | \value LocateFile return only files |
| 383 | \value LocateDirectory return only directories |
| 384 | */ |
| 385 | |
| 386 | static bool existsAsSpecified(const QString &path, QStandardPaths::LocateOptions options) |
| 387 | { |
| 388 | if (options & QStandardPaths::LocateDirectory) |
| 389 | return QDir(path).exists(); |
| 390 | return QFileInfo(path).isFile(); |
| 391 | } |
| 392 | |
| 393 | /*! |
| 394 | \include standardpath/functiondocs.qdocinc locate |
| 395 | */ |
| 396 | QString QStandardPaths::locate(StandardLocation type, const QString &fileName, LocateOptions options) |
| 397 | { |
| 398 | const QStringList &dirs = standardLocations(type); |
| 399 | for (QStringList::const_iterator dir = dirs.constBegin(); dir != dirs.constEnd(); ++dir) { |
| 400 | const QString path = *dir + QLatin1Char('/') + fileName; |
| 401 | if (existsAsSpecified(path, options)) |
| 402 | return path; |
| 403 | } |
| 404 | return QString(); |
| 405 | } |
| 406 | |
| 407 | /*! |
| 408 | \include standardpath/functiondocs.qdocinc locateAll |
| 409 | */ |
| 410 | QStringList QStandardPaths::locateAll(StandardLocation type, const QString &fileName, LocateOptions options) |
| 411 | { |
| 412 | const QStringList &dirs = standardLocations(type); |
| 413 | QStringList result; |
| 414 | for (QStringList::const_iterator dir = dirs.constBegin(); dir != dirs.constEnd(); ++dir) { |
| 415 | const QString path = *dir + QLatin1Char('/') + fileName; |
| 416 | if (existsAsSpecified(path, options)) |
| 417 | result.append(t: path); |
| 418 | } |
| 419 | return result; |
| 420 | } |
| 421 | |
| 422 | #ifdef Q_OS_WIN |
| 423 | static QStringList executableExtensions() |
| 424 | { |
| 425 | // If %PATHEXT% does not contain .exe, it is either empty, malformed, or distorted in ways that we cannot support, anyway. |
| 426 | const QStringList pathExt = QString::fromLocal8Bit(qgetenv("PATHEXT" )).toLower().split(QLatin1Char(';')); |
| 427 | return pathExt.contains(QLatin1String(".exe" ), Qt::CaseInsensitive) ? |
| 428 | pathExt : |
| 429 | QStringList() << QLatin1String(".exe" ) << QLatin1String(".com" ) |
| 430 | << QLatin1String(".bat" ) << QLatin1String(".cmd" ); |
| 431 | } |
| 432 | #endif |
| 433 | |
| 434 | static QString checkExecutable(const QString &path) |
| 435 | { |
| 436 | const QFileInfo info(path); |
| 437 | if (info.isBundle()) |
| 438 | return info.bundleName(); |
| 439 | if (info.isFile() && info.isExecutable()) |
| 440 | return QDir::cleanPath(path); |
| 441 | return QString(); |
| 442 | } |
| 443 | |
| 444 | static inline QString searchExecutable(const QStringList &searchPaths, |
| 445 | const QString &executableName) |
| 446 | { |
| 447 | const QDir currentDir = QDir::current(); |
| 448 | for (const QString &searchPath : searchPaths) { |
| 449 | const QString candidate = currentDir.absoluteFilePath(fileName: searchPath + QLatin1Char('/') + executableName); |
| 450 | const QString absPath = checkExecutable(path: candidate); |
| 451 | if (!absPath.isEmpty()) |
| 452 | return absPath; |
| 453 | } |
| 454 | return QString(); |
| 455 | } |
| 456 | |
| 457 | #ifdef Q_OS_WIN |
| 458 | |
| 459 | // Find executable appending candidate suffixes, used for suffix-less executables |
| 460 | // on Windows. |
| 461 | static inline QString |
| 462 | searchExecutableAppendSuffix(const QStringList &searchPaths, |
| 463 | const QString &executableName, |
| 464 | const QStringList &suffixes) |
| 465 | { |
| 466 | const QDir currentDir = QDir::current(); |
| 467 | for (const QString &searchPath : searchPaths) { |
| 468 | const QString candidateRoot = currentDir.absoluteFilePath(searchPath + QLatin1Char('/') + executableName); |
| 469 | for (const QString &suffix : suffixes) { |
| 470 | const QString absPath = checkExecutable(candidateRoot + suffix); |
| 471 | if (!absPath.isEmpty()) |
| 472 | return absPath; |
| 473 | } |
| 474 | } |
| 475 | return QString(); |
| 476 | } |
| 477 | |
| 478 | #endif // Q_OS_WIN |
| 479 | |
| 480 | /*! |
| 481 | \include standardpath/functiondocs.qdocinc findExecutable |
| 482 | */ |
| 483 | QString QStandardPaths::findExecutable(const QString &executableName, const QStringList &paths) |
| 484 | { |
| 485 | if (QFileInfo(executableName).isAbsolute()) |
| 486 | return checkExecutable(path: executableName); |
| 487 | |
| 488 | QStringList searchPaths = paths; |
| 489 | if (paths.isEmpty()) { |
| 490 | QByteArray pEnv = qgetenv(varName: "PATH" ); |
| 491 | if (Q_UNLIKELY(pEnv.isNull())) { |
| 492 | // Get a default path. POSIX.1 does not actually require this, but |
| 493 | // most Unix libc fall back to confstr(_CS_PATH) if the PATH |
| 494 | // environment variable isn't set. Let's try to do the same. |
| 495 | #if defined(_PATH_DEFPATH) |
| 496 | // BSD API. |
| 497 | pEnv = _PATH_DEFPATH; |
| 498 | #elif defined(_CS_PATH) |
| 499 | // POSIX API. |
| 500 | size_t n = confstr(_CS_PATH, nullptr, 0); |
| 501 | if (n) { |
| 502 | pEnv.resize(n); |
| 503 | // size()+1 is ok because QByteArray always has an extra NUL-terminator |
| 504 | confstr(_CS_PATH, pEnv.data(), pEnv.size() + 1); |
| 505 | } |
| 506 | #else |
| 507 | // Windows SDK's execvpe() does not have a fallback, so we won't |
| 508 | // apply one either. |
| 509 | #endif |
| 510 | } |
| 511 | |
| 512 | // Remove trailing slashes, which occur on Windows. |
| 513 | const QStringList rawPaths = QString::fromLocal8Bit(str: pEnv.constData()).split( |
| 514 | sep: QDir::listSeparator(), behavior: Qt::SkipEmptyParts); |
| 515 | searchPaths.reserve(alloc: rawPaths.size()); |
| 516 | for (const QString &rawPath : rawPaths) { |
| 517 | QString cleanPath = QDir::cleanPath(path: rawPath); |
| 518 | if (cleanPath.size() > 1 && cleanPath.endsWith(c: QLatin1Char('/'))) |
| 519 | cleanPath.truncate(pos: cleanPath.size() - 1); |
| 520 | searchPaths.push_back(t: cleanPath); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | #ifdef Q_OS_WIN |
| 525 | // On Windows, if the name does not have a suffix or a suffix not |
| 526 | // in PATHEXT ("xx.foo"), append suffixes from PATHEXT. |
| 527 | static const QStringList executable_extensions = executableExtensions(); |
| 528 | if (executableName.contains(QLatin1Char('.'))) { |
| 529 | const QString suffix = QFileInfo(executableName).suffix(); |
| 530 | if (suffix.isEmpty() || !executable_extensions.contains(QLatin1Char('.') + suffix, Qt::CaseInsensitive)) |
| 531 | return searchExecutableAppendSuffix(searchPaths, executableName, executable_extensions); |
| 532 | } else { |
| 533 | return searchExecutableAppendSuffix(searchPaths, executableName, executable_extensions); |
| 534 | } |
| 535 | #endif |
| 536 | return searchExecutable(searchPaths, executableName); |
| 537 | } |
| 538 | |
| 539 | /*! |
| 540 | \include standardpath/functiondocs.qdocinc displayName |
| 541 | */ |
| 542 | |
| 543 | #if !defined(Q_OS_MAC) && !defined(QT_BOOTSTRAPPED) |
| 544 | QString QStandardPaths::displayName(StandardLocation type) |
| 545 | { |
| 546 | switch (type) { |
| 547 | case DesktopLocation: |
| 548 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Desktop" ); |
| 549 | case DocumentsLocation: |
| 550 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Documents" ); |
| 551 | case FontsLocation: |
| 552 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Fonts" ); |
| 553 | case ApplicationsLocation: |
| 554 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Applications" ); |
| 555 | case MusicLocation: |
| 556 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Music" ); |
| 557 | case MoviesLocation: |
| 558 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Movies" ); |
| 559 | case PicturesLocation: |
| 560 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Pictures" ); |
| 561 | case TempLocation: |
| 562 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Temporary Directory" ); |
| 563 | case HomeLocation: |
| 564 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Home" ); |
| 565 | case CacheLocation: |
| 566 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Cache" ); |
| 567 | case GenericDataLocation: |
| 568 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Shared Data" ); |
| 569 | case RuntimeLocation: |
| 570 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Runtime" ); |
| 571 | case ConfigLocation: |
| 572 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Configuration" ); |
| 573 | case GenericConfigLocation: |
| 574 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Shared Configuration" ); |
| 575 | case GenericCacheLocation: |
| 576 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Shared Cache" ); |
| 577 | case DownloadLocation: |
| 578 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Download" ); |
| 579 | case AppDataLocation: |
| 580 | case AppLocalDataLocation: |
| 581 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Application Data" ); |
| 582 | case AppConfigLocation: |
| 583 | return QCoreApplication::translate(context: "QStandardPaths" , key: "Application Configuration" ); |
| 584 | } |
| 585 | // not reached |
| 586 | return QString(); |
| 587 | } |
| 588 | #endif |
| 589 | |
| 590 | /*! |
| 591 | \fn void QStandardPaths::enableTestMode(bool testMode) |
| 592 | \obsolete Use QStandardPaths::setTestModeEnabled |
| 593 | */ |
| 594 | /*! |
| 595 | \fn void QStandardPaths::setTestModeEnabled(bool testMode) |
| 596 | |
| 597 | \include standardpath/functiondocs.qdocinc setTestModeEnabled |
| 598 | */ |
| 599 | |
| 600 | static bool qsp_testMode = false; |
| 601 | |
| 602 | #if QT_DEPRECATED_SINCE(5, 2) |
| 603 | void QStandardPaths::enableTestMode(bool testMode) |
| 604 | { |
| 605 | qsp_testMode = testMode; |
| 606 | } |
| 607 | #endif |
| 608 | |
| 609 | void QStandardPaths::setTestModeEnabled(bool testMode) |
| 610 | { |
| 611 | qsp_testMode = testMode; |
| 612 | } |
| 613 | |
| 614 | /*! |
| 615 | \fn void QStandardPaths::isTestModeEnabled() |
| 616 | |
| 617 | \internal |
| 618 | |
| 619 | Returns \c true if test mode is enabled in QStandardPaths; otherwise returns \c false. |
| 620 | */ |
| 621 | |
| 622 | bool QStandardPaths::isTestModeEnabled() |
| 623 | { |
| 624 | return qsp_testMode; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | QT_END_NAMESPACE |
| 629 | |
| 630 | #ifndef QT_NO_QOBJECT |
| 631 | #include "moc_qstandardpaths.cpp" |
| 632 | #endif |
| 633 | |
| 634 | #endif // QT_NO_STANDARDPATHS |
| 635 | |