io/qstandardpaths.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qstandardpaths.h" -
43 -
44#include <qdir.h> -
45#include <qfileinfo.h> -
46#include <qhash.h> -
47 -
48#ifndef QT_BOOTSTRAPPED -
49#include <qobject.h> -
50#include <qcoreapplication.h> -
51#endif -
52 -
53#ifndef QT_NO_STANDARDPATHS -
54 -
55QT_BEGIN_NAMESPACE -
56 -
57/*! -
58 \class QStandardPaths -
59 \inmodule QtCore -
60 \brief The QStandardPaths class provides methods for accessing standard paths. -
61 \since 5.0 -
62 -
63 This class contains functions to query standard locations on the local -
64 filesystem, for common tasks such as user-specific directories or system-wide -
65 configuration directories. -
66*/ -
67 -
68/*! -
69 \enum QStandardPaths::StandardLocation -
70 -
71 This enum describes the different locations that can be queried using -
72 methods such as QStandardPaths::writableLocation, QStandardPaths::standardLocations, -
73 and QStandardPaths::displayName. -
74 -
75 \value DesktopLocation Returns the user's desktop directory. -
76 \value DocumentsLocation Returns the user's document. -
77 \value FontsLocation Returns the user's fonts. -
78 \value ApplicationsLocation Returns the user's applications. -
79 \value MusicLocation Returns the user's music. -
80 \value MoviesLocation Returns the user's movies. -
81 \value PicturesLocation Returns the user's pictures. -
82 \value TempLocation Returns the system's temporary directory. -
83 \value HomeLocation Returns the user's home directory. -
84 \value DataLocation Returns a directory location where persistent -
85 application data can be stored. QCoreApplication::organizationName -
86 and QCoreApplication::applicationName are appended to the directory location -
87 returned for GenericDataLocation. -
88 \value CacheLocation Returns a directory location where user-specific -
89 non-essential (cached) data should be written. -
90 \value GenericCacheLocation Returns a directory location where user-specific -
91 non-essential (cached) data, shared across applications, should be written. -
92 \value GenericDataLocation Returns a directory location where persistent -
93 data shared across applications can be stored. -
94 \value RuntimeLocation Returns a directory location where runtime communication -
95 files should be written. For instance unix local sockets. -
96 \value ConfigLocation Returns a directory location where user-specific -
97 configuration files should be written. -
98 \value DownloadLocation Returns a directory for user's downloaded files. -
99 -
100 -
101 \sa writableLocation(), standardLocations(), displayName(), locate(), locateAll() -
102*/ -
103 -
104/*! -
105 \fn QString QStandardPaths::writableLocation(StandardLocation type) -
106 -
107 Returns the directory where files of \a type should be written to, or an empty string -
108 if the location cannot be determined. -
109 -
110 \note The storage location returned can be a directory that does not exist; i.e., it -
111 may need to be created by the system or the user. -
112*/ -
113 -
114 -
115/*! -
116 \fn QStringList QStandardPaths::standardLocations(StandardLocation type) -
117 -
118 Returns all the directories where files of \a type belong. -
119 -
120 The list of directories is sorted from high to low priority, starting with -
121 writableLocation() if it can be determined. This list is empty if no locations -
122 for \a type are defined. -
123 -
124 \sa writableLocation() -
125 */ -
126 -
127/*! -
128 \enum QStandardPaths::LocateOption -
129 -
130 This enum describes the different flags that can be used for -
131 controlling the behavior of QStandardPaths::locate and -
132 QStandardPaths::locateAll. -
133 -
134 \value LocateFile return only files -
135 \value LocateDirectory return only directories -
136*/ -
137 -
138static bool existsAsSpecified(const QString &path, QStandardPaths::LocateOptions options) -
139{ -
140 if (options & QStandardPaths::LocateDirectory)
evaluated: options & QStandardPaths::LocateDirectory
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:121
5-121
141 return QDir(path).exists();
executed: return QDir(path).exists();
Execution Count:5
5
142 return QFileInfo(path).isFile();
executed: return QFileInfo(path).isFile();
Execution Count:121
121
143} -
144 -
145/*! -
146 Tries to find a file or directory called \a fileName in the standard locations -
147 for \a type. -
148 -
149 The full path to the first file or directory (depending on \a options) found is returned. -
150 If no such file or directory can be found, an empty string is returned. -
151 */ -
152QString QStandardPaths::locate(StandardLocation type, const QString &fileName, LocateOptions options) -
153{ -
154 const QStringList &dirs = standardLocations(type);
executed (the execution status of this line is deduced): const QStringList &dirs = standardLocations(type);
-
155 for (QStringList::const_iterator dir = dirs.constBegin(); dir != dirs.constEnd(); ++dir) {
evaluated: dir != dirs.constEnd()
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:1
1-4
156 const QString path = *dir + QLatin1Char('/') + fileName;
executed (the execution status of this line is deduced): const QString path = *dir + QLatin1Char('/') + fileName;
-
157 if (existsAsSpecified(path, options))
evaluated: existsAsSpecified(path, options)
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:2
2
158 return path;
executed: return path;
Execution Count:2
2
159 }
executed: }
Execution Count:2
2
160 return QString();
executed: return QString();
Execution Count:1
1
161} -
162 -
163/*! -
164 Tries to find all files or directories called \a fileName in the standard locations -
165 for \a type. -
166 -
167 The \a options flag allows to specify whether to look for files or directories. -
168 -
169 Returns the list of all the files that were found. -
170 */ -
171QStringList QStandardPaths::locateAll(StandardLocation type, const QString &fileName, LocateOptions options) -
172{ -
173 const QStringList &dirs = standardLocations(type);
executed (the execution status of this line is deduced): const QStringList &dirs = standardLocations(type);
-
174 QStringList result;
executed (the execution status of this line is deduced): QStringList result;
-
175 for (QStringList::const_iterator dir = dirs.constBegin(); dir != dirs.constEnd(); ++dir) {
evaluated: dir != dirs.constEnd()
TRUEFALSE
yes
Evaluation Count:122
yes
Evaluation Count:61
61-122
176 const QString path = *dir + QLatin1Char('/') + fileName;
executed (the execution status of this line is deduced): const QString path = *dir + QLatin1Char('/') + fileName;
-
177 if (existsAsSpecified(path, options))
evaluated: existsAsSpecified(path, options)
TRUEFALSE
yes
Evaluation Count:77
yes
Evaluation Count:45
45-77
178 result.append(path);
executed: result.append(path);
Execution Count:77
77
179 }
executed: }
Execution Count:122
122
180 return result;
executed: return result;
Execution Count:61
61
181} -
182 -
183#ifdef Q_OS_WIN -
184static QStringList executableExtensions() -
185{ -
186 // If %PATHEXT% does not contain .exe, it is either empty, malformed, or distorted in ways that we cannot support, anyway. -
187 const QStringList pathExt = QString::fromLocal8Bit(qgetenv("PATHEXT")).toLower().split(QLatin1Char(';')); -
188 return pathExt.contains(QLatin1String(".exe"), Qt::CaseInsensitive) ? -
189 pathExt : -
190 QStringList() << QLatin1String(".exe") << QLatin1String(".com") -
191 << QLatin1String(".bat") << QLatin1String(".cmd"); -
192} -
193#endif -
194 -
195static QString checkExecutable(const QString &path) -
196{ -
197 const QFileInfo info(path);
executed (the execution status of this line is deduced): const QFileInfo info(path);
-
198 if (info.isBundle())
partially evaluated: info.isBundle()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:122
0-122
199 return info.bundleName();
never executed: return info.bundleName();
0
200 if (info.isFile() && info.isExecutable())
evaluated: info.isFile()
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:112
partially evaluated: info.isExecutable()
TRUEFALSE
yes
Evaluation Count:10
no
Evaluation Count:0
0-112
201 return QDir::cleanPath(path);
executed: return QDir::cleanPath(path);
Execution Count:10
10
202 return QString();
executed: return QString();
Execution Count:112
112
203} -
204 -
205static inline QString searchExecutable(const QStringList &searchPaths, -
206 const QString &executableName) -
207{ -
208 const QDir currentDir = QDir::current();
executed (the execution status of this line is deduced): const QDir currentDir = QDir::current();
-
209 foreach (const QString &searchPath, searchPaths) {
executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(searchPaths)> _container_(searchPaths); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &searchPath = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
210 const QString candidate = currentDir.absoluteFilePath(searchPath + QLatin1Char('/') + executableName);
executed (the execution status of this line is deduced): const QString candidate = currentDir.absoluteFilePath(searchPath + QLatin1Char('/') + executableName);
-
211 const QString absPath = checkExecutable(candidate);
executed (the execution status of this line is deduced): const QString absPath = checkExecutable(candidate);
-
212 if (!absPath.isEmpty())
evaluated: !absPath.isEmpty()
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:111
9-111
213 return absPath;
executed: return absPath;
Execution Count:9
9
214 }
executed: }
Execution Count:111
111
215 return QString();
executed: return QString();
Execution Count:2
2
216} -
217 -
218#ifdef Q_OS_WIN -
219 -
220// Find executable appending candidate suffixes, used for suffix-less executables -
221// on Windows. -
222static inline QString -
223 searchExecutableAppendSuffix(const QStringList &searchPaths, -
224 const QString &executableName, -
225 const QStringList &suffixes) -
226{ -
227 const QDir currentDir = QDir::current(); -
228 foreach (const QString &searchPath, searchPaths) { -
229 const QString candidateRoot = currentDir.absoluteFilePath(searchPath + QLatin1Char('/') + executableName); -
230 foreach (const QString &suffix, suffixes) { -
231 const QString absPath = checkExecutable(candidateRoot + suffix); -
232 if (!absPath.isEmpty()) -
233 return absPath; -
234 } -
235 } -
236 return QString(); -
237} -
238 -
239#endif // Q_OS_WIN -
240 -
241/*! -
242 Finds the executable named \a executableName in the paths specified by \a paths, -
243 or the system paths if \a paths is empty. -
244 -
245 On most operating systems the system path is determined by the PATH environment variable. -
246 -
247 The directories where to search for the executable can be set in the \a paths argument. -
248 To search in both your own paths and the system paths, call findExecutable twice, once with -
249 \a paths set and once with \a paths empty. -
250 -
251 Symlinks are not resolved, in order to preserve behavior for the case of executables -
252 whose behavior depends on the name they are invoked with. -
253 -
254 \note On Windows, the usual executable extensions (from the PATHEXT environment variable) -
255 are automatically appended, so that for instance findExecutable("foo") will find foo.exe -
256 or foo.bat if present. -
257 -
258 Returns the absolute file path to the executable, or an empty string if not found. -
259 */ -
260QString QStandardPaths::findExecutable(const QString &executableName, const QStringList &paths) -
261{ -
262 if (QFileInfo(executableName).isAbsolute())
evaluated: QFileInfo(executableName).isAbsolute()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:11
2-11
263 return checkExecutable(executableName);
executed: return checkExecutable(executableName);
Execution Count:2
2
264 -
265 QStringList searchPaths = paths;
executed (the execution status of this line is deduced): QStringList searchPaths = paths;
-
266 if (paths.isEmpty()) {
partially evaluated: paths.isEmpty()
TRUEFALSE
yes
Evaluation Count:11
no
Evaluation Count:0
0-11
267 QByteArray pEnv = qgetenv("PATH");
executed (the execution status of this line is deduced): QByteArray pEnv = qgetenv("PATH");
-
268#if defined(Q_OS_WIN) -
269 const QLatin1Char pathSep(';'); -
270#else -
271 const QLatin1Char pathSep(':');
executed (the execution status of this line is deduced): const QLatin1Char pathSep(':');
-
272#endif -
273 // Remove trailing slashes, which occur on Windows. -
274 const QStringList rawPaths = QString::fromLocal8Bit(pEnv.constData()).split(pathSep, QString::SkipEmptyParts);
executed (the execution status of this line is deduced): const QStringList rawPaths = QString::fromLocal8Bit(pEnv.constData()).split(pathSep, QString::SkipEmptyParts);
-
275 searchPaths.reserve(rawPaths.size());
executed (the execution status of this line is deduced): searchPaths.reserve(rawPaths.size());
-
276 foreach (const QString &rawPath, rawPaths) {
executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(rawPaths)> _container_(rawPaths); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &rawPath = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
277 QString cleanPath = QDir::cleanPath(rawPath);
executed (the execution status of this line is deduced): QString cleanPath = QDir::cleanPath(rawPath);
-
278 if (cleanPath.size() > 1 && cleanPath.endsWith(QLatin1Char('/')))
partially evaluated: cleanPath.size() > 1
TRUEFALSE
yes
Evaluation Count:143
no
Evaluation Count:0
partially evaluated: cleanPath.endsWith(QLatin1Char('/'))
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:143
0-143
279 cleanPath.truncate(cleanPath.size() - 1);
never executed: cleanPath.truncate(cleanPath.size() - 1);
0
280 searchPaths.push_back(cleanPath);
executed (the execution status of this line is deduced): searchPaths.push_back(cleanPath);
-
281 }
executed: }
Execution Count:143
143
282 }
executed: }
Execution Count:11
11
283 -
284#ifdef Q_OS_WIN -
285 // On Windows, if the name does not have a suffix or a suffix not -
286 // in PATHEXT ("xx.foo"), append suffixes from PATHEXT. -
287 static const QStringList executable_extensions = executableExtensions(); -
288 if (executableName.contains(QLatin1Char('.'))) { -
289 const QString suffix = QFileInfo(executableName).suffix(); -
290 if (suffix.isEmpty() || !executable_extensions.contains(QLatin1Char('.') + suffix, Qt::CaseInsensitive)) -
291 return searchExecutableAppendSuffix(searchPaths, executableName, executable_extensions); -
292 } else { -
293 return searchExecutableAppendSuffix(searchPaths, executableName, executable_extensions); -
294 } -
295#endif -
296 return searchExecutable(searchPaths, executableName);
executed: return searchExecutable(searchPaths, executableName);
Execution Count:11
11
297} -
298 -
299/*! -
300 \fn QString QStandardPaths::displayName(StandardLocation type) -
301 -
302 Returns a localized display name for the given location \a type or -
303 an empty QString if no relevant location can be found. -
304*/ -
305 -
306#if !defined(Q_OS_MAC) && !defined(QT_BOOTSTRAPPED) -
307QString QStandardPaths::displayName(StandardLocation type) -
308{ -
309 switch (type) { -
310 case DesktopLocation: -
311 return QCoreApplication::translate("QStandardPaths", "Desktop");
executed: return QCoreApplication::translate("QStandardPaths", "Desktop");
Execution Count:1
1
312 case DocumentsLocation: -
313 return QCoreApplication::translate("QStandardPaths", "Documents");
executed: return QCoreApplication::translate("QStandardPaths", "Documents");
Execution Count:1
1
314 case FontsLocation: -
315 return QCoreApplication::translate("QStandardPaths", "Fonts");
executed: return QCoreApplication::translate("QStandardPaths", "Fonts");
Execution Count:1
1
316 case ApplicationsLocation: -
317 return QCoreApplication::translate("QStandardPaths", "Applications");
executed: return QCoreApplication::translate("QStandardPaths", "Applications");
Execution Count:1
1
318 case MusicLocation: -
319 return QCoreApplication::translate("QStandardPaths", "Music");
executed: return QCoreApplication::translate("QStandardPaths", "Music");
Execution Count:1
1
320 case MoviesLocation: -
321 return QCoreApplication::translate("QStandardPaths", "Movies");
executed: return QCoreApplication::translate("QStandardPaths", "Movies");
Execution Count:1
1
322 case PicturesLocation: -
323 return QCoreApplication::translate("QStandardPaths", "Pictures");
executed: return QCoreApplication::translate("QStandardPaths", "Pictures");
Execution Count:1
1
324 case TempLocation: -
325 return QCoreApplication::translate("QStandardPaths", "Temporary Directory");
executed: return QCoreApplication::translate("QStandardPaths", "Temporary Directory");
Execution Count:1
1
326 case HomeLocation: -
327 return QCoreApplication::translate("QStandardPaths", "Home");
executed: return QCoreApplication::translate("QStandardPaths", "Home");
Execution Count:1
1
328 case DataLocation: -
329 return QCoreApplication::translate("QStandardPaths", "Application Data");
executed: return QCoreApplication::translate("QStandardPaths", "Application Data");
Execution Count:1
1
330 case CacheLocation: -
331 return QCoreApplication::translate("QStandardPaths", "Cache");
never executed: return QCoreApplication::translate("QStandardPaths", "Cache");
0
332 case GenericDataLocation: -
333 return QCoreApplication::translate("QStandardPaths", "Shared Data");
never executed: return QCoreApplication::translate("QStandardPaths", "Shared Data");
0
334 case RuntimeLocation: -
335 return QCoreApplication::translate("QStandardPaths", "Runtime");
never executed: return QCoreApplication::translate("QStandardPaths", "Runtime");
0
336 case ConfigLocation: -
337 return QCoreApplication::translate("QStandardPaths", "Configuration");
never executed: return QCoreApplication::translate("QStandardPaths", "Configuration");
0
338 case GenericCacheLocation: -
339 return QCoreApplication::translate("QStandardPaths", "Shared Cache");
never executed: return QCoreApplication::translate("QStandardPaths", "Shared Cache");
0
340 case DownloadLocation: -
341 return QCoreApplication::translate("QStandardPaths", "Download");
executed: return QCoreApplication::translate("QStandardPaths", "Download");
Execution Count:1
1
342 } -
343 // not reached -
344 return QString();
never executed: return QString();
0
345} -
346#endif -
347 -
348/*! -
349 \fn void QStandardPaths::enableTestMode(bool testMode) -
350 -
351 If \a testMode is true, this enables a special "test mode" in -
352 QStandardPaths, which changes writable locations -
353 to point to test directories, in order to prevent auto tests from reading from -
354 or writing to the current user's configuration. -
355 -
356 This affects the locations into which test programs might write files: -
357 GenericDataLocation, DataLocation, ConfigLocation, -
358 GenericCacheLocation, CacheLocation. -
359 Other locations are not affected. -
360 -
361 On Unix, XDG_DATA_HOME is set to ~/.qttest/share, XDG_CONFIG_HOME is -
362 set to ~/.qttest/config, and XDG_CACHE_HOME is set to ~/.qttest/cache. -
363 -
364 On Mac, data goes to "~/.qttest/Application Support", cache goes to -
365 ~/.qttest/Cache, and config goes to ~/.qttest/Preferences. -
366 -
367 On Windows, everything goes to a "qttest" directory under Application Data. -
368*/ -
369 -
370static bool qsp_testMode = false; -
371 -
372void QStandardPaths::enableTestMode(bool testMode) -
373{ -
374 qsp_testMode = testMode;
executed (the execution status of this line is deduced): qsp_testMode = testMode;
-
375}
executed: }
Execution Count:2
2
376 -
377/*! -
378 \fn void QStandardPaths::isTestModeEnabled() -
379 -
380 \internal -
381 -
382 Returns true if test mode is enabled in QStandardPaths; otherwise returns false. -
383*/ -
384 -
385bool QStandardPaths::isTestModeEnabled() -
386{ -
387 return qsp_testMode;
executed: return qsp_testMode;
Execution Count:125
125
388} -
389 -
390 -
391QT_END_NAMESPACE -
392 -
393#endif // QT_NO_STANDARDPATHS -
394 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial