Warning: That file was not part of the compilation database. It may have many parsing errors.

1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qstandardpaths.h"
41
42#ifndef QT_NO_STANDARDPATHS
43
44#include <qdir.h>
45#include <qurl.h>
46#include <private/qcore_mac_p.h>
47
48#ifndef QT_BOOTSTRAPPED
49#include <qcoreapplication.h>
50#endif
51
52#import <Foundation/Foundation.h>
53
54QT_BEGIN_NAMESPACE
55
56static QString pathForDirectory(NSSearchPathDirectory directory,
57 NSSearchPathDomainMask mask)
58{
59 return QString::fromNSString(
60 [NSSearchPathForDirectoriesInDomains(directory, mask, YES) lastObject]);
61}
62
63static NSSearchPathDirectory searchPathDirectory(QStandardPaths::StandardLocation type)
64{
65 switch (type) {
66 case QStandardPaths::DesktopLocation:
67 return NSDesktopDirectory;
68 case QStandardPaths::DocumentsLocation:
69 return NSDocumentDirectory;
70 case QStandardPaths::ApplicationsLocation:
71 return NSApplicationDirectory;
72 case QStandardPaths::MusicLocation:
73 return NSMusicDirectory;
74 case QStandardPaths::MoviesLocation:
75 return NSMoviesDirectory;
76 case QStandardPaths::PicturesLocation:
77 return NSPicturesDirectory;
78 case QStandardPaths::GenericDataLocation:
79 case QStandardPaths::RuntimeLocation:
80 case QStandardPaths::AppDataLocation:
81 case QStandardPaths::AppLocalDataLocation:
82 return NSApplicationSupportDirectory;
83 case QStandardPaths::GenericCacheLocation:
84 case QStandardPaths::CacheLocation:
85 return NSCachesDirectory;
86 case QStandardPaths::DownloadLocation:
87 return NSDownloadsDirectory;
88 default:
89 return (NSSearchPathDirectory)0;
90 }
91}
92
93static void appendOrganizationAndApp(QString &path)
94{
95#ifndef QT_BOOTSTRAPPED
96 const QString org = QCoreApplication::organizationName();
97 if (!org.isEmpty())
98 path += QLatin1Char('/') + org;
99 const QString appName = QCoreApplication::applicationName();
100 if (!appName.isEmpty())
101 path += QLatin1Char('/') + appName;
102#else
103 Q_UNUSED(path);
104#endif
105}
106
107static QString baseWritableLocation(QStandardPaths::StandardLocation type,
108 NSSearchPathDomainMask mask = NSUserDomainMask,
109 bool appendOrgAndApp = false)
110{
111 QString path;
112 const NSSearchPathDirectory dir = searchPathDirectory(type);
113 switch (type) {
114 case QStandardPaths::HomeLocation:
115 path = QDir::homePath();
116 break;
117 case QStandardPaths::TempLocation:
118 path = QDir::tempPath();
119 break;
120#if defined(QT_PLATFORM_UIKIT)
121 // These locations point to non-existing write-protected paths. Use sensible fallbacks.
122 case QStandardPaths::MusicLocation:
123 path = pathForDirectory(NSDocumentDirectory, mask) + QLatin1String("/Music");
124 break;
125 case QStandardPaths::MoviesLocation:
126 path = pathForDirectory(NSDocumentDirectory, mask) + QLatin1String("/Movies");
127 break;
128 case QStandardPaths::PicturesLocation:
129 path = pathForDirectory(NSDocumentDirectory, mask) + QLatin1String("/Pictures");
130 break;
131 case QStandardPaths::DownloadLocation:
132 path = pathForDirectory(NSDocumentDirectory, mask) + QLatin1String("/Downloads");
133 break;
134 case QStandardPaths::DesktopLocation:
135 path = pathForDirectory(NSDocumentDirectory, mask) + QLatin1String("/Desktop");
136 break;
137 case QStandardPaths::ApplicationsLocation:
138 break;
139#endif
140 case QStandardPaths::FontsLocation:
141 path = pathForDirectory(NSLibraryDirectory, mask) + QLatin1String("/Fonts");
142 break;
143 case QStandardPaths::ConfigLocation:
144 case QStandardPaths::GenericConfigLocation:
145 case QStandardPaths::AppConfigLocation:
146 path = pathForDirectory(NSLibraryDirectory, mask) + QLatin1String("/Preferences");
147 break;
148 default:
149 path = pathForDirectory(dir, mask);
150 break;
151 }
152
153 if (appendOrgAndApp) {
154 switch (type) {
155 case QStandardPaths::AppDataLocation:
156 case QStandardPaths::AppLocalDataLocation:
157 case QStandardPaths::AppConfigLocation:
158 case QStandardPaths::CacheLocation:
159 appendOrganizationAndApp(path);
160 break;
161 default:
162 break;
163 }
164 }
165
166 return path;
167}
168
169QString QStandardPaths::writableLocation(StandardLocation type)
170{
171 QString location = baseWritableLocation(type, NSUserDomainMask, true);
172 if (isTestModeEnabled())
173 location = location.replace(QDir::homePath(), QDir::homePath() + QLatin1String("/.qttest"));
174
175 return location;
176}
177
178QStringList QStandardPaths::standardLocations(StandardLocation type)
179{
180 QStringList dirs;
181
182#if defined(QT_PLATFORM_UIKIT)
183 if (type == PicturesLocation)
184 dirs << writableLocation(PicturesLocation) << QLatin1String("assets-library://");
185#endif
186
187 if (type == GenericDataLocation || type == FontsLocation || type == ApplicationsLocation
188 || type == AppDataLocation || type == AppLocalDataLocation
189 || type == GenericCacheLocation || type == CacheLocation) {
190 QList<NSSearchPathDomainMask> masks;
191 masks << NSLocalDomainMask;
192 if (type == FontsLocation || type == GenericCacheLocation)
193 masks << NSSystemDomainMask;
194
195 for (QList<NSSearchPathDomainMask>::const_iterator it = masks.begin();
196 it != masks.end(); ++it) {
197 const QString path = baseWritableLocation(type, *it, true);
198 if (!path.isEmpty() && !dirs.contains(path))
199 dirs.append(path);
200 }
201 }
202
203 if (type == AppDataLocation || type == AppLocalDataLocation) {
204 CFBundleRef mainBundle = CFBundleGetMainBundle();
205 if (mainBundle) {
206 if (QCFType<CFURLRef> resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle)) {
207 if (QCFType<CFURLRef> absoluteResouresURL = CFURLCopyAbsoluteURL(resourcesURL)) {
208 if (QCFType<CFStringRef> path = CFURLCopyFileSystemPath(absoluteResouresURL,
209 kCFURLPOSIXPathStyle)) {
210 dirs.append(QString::fromCFString(path));
211 }
212 }
213 }
214 }
215 }
216
217 const QString localDir = writableLocation(type);
218 if (!localDir.isEmpty())
219 dirs.prepend(localDir);
220 return dirs;
221}
222
223#ifndef QT_BOOTSTRAPPED
224QString QStandardPaths::displayName(StandardLocation type)
225{
226 // Use "Home" instead of the user's Unix username
227 if (QStandardPaths::HomeLocation == type)
228 return QCoreApplication::translate("QStandardPaths", "Home");
229
230 // The temporary directory returned by the old Carbon APIs is ~/Library/Caches/TemporaryItems,
231 // the display name of which ("TemporaryItems") isn't translated by the system. The standard
232 // temporary directory has no reasonable display name either, so use something more sensible.
233 if (QStandardPaths::TempLocation == type)
234 return QCoreApplication::translate("QStandardPaths", "Temporary Items");
235
236 // standardLocations() may return an empty list on some platforms
237 if (QStandardPaths::ApplicationsLocation == type)
238 return QCoreApplication::translate("QStandardPaths", "Applications");
239
240 const QCFString fsPath(standardLocations(type).constFirst());
241 if (QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
242 fsPath, kCFURLPOSIXPathStyle, true)) {
243 QCFString name;
244 CFURLCopyResourcePropertyForKey(url, kCFURLLocalizedNameKey, &name, NULL);
245 if (name && CFStringGetLength(name))
246 return QString::fromCFString(name);
247 }
248
249 return QFileInfo(baseWritableLocation(type)).fileName();
250}
251#endif
252
253QT_END_NAMESPACE
254
255#endif // QT_NO_STANDARDPATHS
256

Warning: That file was not part of the compilation database. It may have many parsing errors.