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_mac.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qstandardpaths.h"
5
6#ifndef QT_NO_STANDARDPATHS
7
8#include <qdir.h>
9#include <qurl.h>
10#include <private/qcore_mac_p.h>
11
12#ifndef QT_BOOTSTRAPPED
13#include <qcoreapplication.h>
14#endif
15
16#import <Foundation/Foundation.h>
17
19
20using namespace Qt::StringLiterals;
21
22static QString pathForDirectory(NSSearchPathDirectory directory,
23 NSSearchPathDomainMask mask)
24{
25 return QString::fromNSString(
26 [NSSearchPathForDirectoriesInDomains(directory, mask, YES) lastObject]);
27}
28
30{
31 switch (type) {
33 return NSDesktopDirectory;
35 return NSDocumentDirectory;
37 return NSApplicationDirectory;
39 return NSMusicDirectory;
41 return NSMoviesDirectory;
43 return NSPicturesDirectory;
48 return NSApplicationSupportDirectory;
51 return NSCachesDirectory;
53 return NSDownloadsDirectory;
55 return NSSharedPublicDirectory;
57 default:
58 return (NSSearchPathDirectory)0;
59 }
60}
61
63{
64#ifndef QT_BOOTSTRAPPED
66 if (!org.isEmpty())
67 path += u'/' + org;
69 if (!appName.isEmpty())
70 path += u'/' + appName;
71#else
73#endif
74}
75
77 NSSearchPathDomainMask mask = NSUserDomainMask,
78 bool appendOrgAndApp = false)
79{
81 const NSSearchPathDirectory dir = searchPathDirectory(type);
82 switch (type) {
85 break;
88 break;
89#if defined(QT_PLATFORM_UIKIT)
90 // These locations point to non-existing write-protected paths. Use sensible fallbacks.
92 path = pathForDirectory(NSDocumentDirectory, mask) + "/Music"_L1;
93 break;
95 path = pathForDirectory(NSDocumentDirectory, mask) + "/Movies"_L1;
96 break;
98 path = pathForDirectory(NSDocumentDirectory, mask) + "/Pictures"_L1;
99 break;
101 path = pathForDirectory(NSDocumentDirectory, mask) + "/Downloads"_L1;
102 break;
104 path = pathForDirectory(NSDocumentDirectory, mask) + "/Desktop"_L1;
105 break;
107 break;
109 path = pathForDirectory(NSDocumentDirectory, mask) + "/Public"_L1;
110 break;
112 path = pathForDirectory(NSDocumentDirectory, mask) + "/Templates"_L1;
113 break;
114#endif
116 path = pathForDirectory(NSLibraryDirectory, mask) + "/Fonts"_L1;
117 break;
121 path = pathForDirectory(NSLibraryDirectory, mask) + "/Preferences"_L1;
122 break;
124 if (appendOrgAndApp) { break; }
127 path = pathForDirectory(NSLibraryDirectory, mask) + "/Preferences/State"_L1;
128 break;
129 default:
131 break;
132 }
133
134 if (appendOrgAndApp) {
135 switch (type) {
141 break;
143 path = pathForDirectory(NSLibraryDirectory, mask) + "/Preferences"_L1;
145 path += "/State"_L1;
146 break;
147 default:
148 break;
149 }
150 }
151
152 return path;
153}
154
156{
157 QString location = baseWritableLocation(type, NSUserDomainMask, true);
158 if (isTestModeEnabled())
159 location = location.replace(QDir::homePath(), QDir::homePath() + "/.qttest"_L1);
160
161 return location;
162}
163
165{
166 QStringList dirs;
167
168#if defined(QT_PLATFORM_UIKIT)
169 if (type == PicturesLocation)
170 dirs << writableLocation(PicturesLocation) << "assets-library://"_L1;
171#endif
172
176 QList<NSSearchPathDomainMask> masks;
177 masks << NSLocalDomainMask;
179 masks << NSSystemDomainMask;
180
182 it != masks.end(); ++it) {
183 const QString path = baseWritableLocation(type, *it, true);
184 if (!path.isEmpty() && !dirs.contains(path))
185 dirs.append(path);
186 }
187 }
188
190 CFBundleRef mainBundle = CFBundleGetMainBundle();
191 if (mainBundle) {
192 if (QCFType<CFURLRef> resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle)) {
193 if (QCFType<CFURLRef> absoluteResouresURL = CFURLCopyAbsoluteURL(resourcesURL)) {
194 if (QCFType<CFStringRef> path = CFURLCopyFileSystemPath(absoluteResouresURL,
195 kCFURLPOSIXPathStyle)) {
196 dirs.append(QString::fromCFString(path));
197 }
198 }
199 }
200 }
201 }
202
203 const QString localDir = writableLocation(type);
204 if (!localDir.isEmpty())
205 dirs.prepend(localDir);
206 return dirs;
207}
208
209#ifndef QT_BOOTSTRAPPED
211{
212 // Use "Home" instead of the user's Unix username
214 return QCoreApplication::translate("QStandardPaths", "Home");
215
216 // The temporary directory returned by the old Carbon APIs is ~/Library/Caches/TemporaryItems,
217 // the display name of which ("TemporaryItems") isn't translated by the system. The standard
218 // temporary directory has no reasonable display name either, so use something more sensible.
220 //: macOS: Temporary directory
221 return QCoreApplication::translate("QStandardPaths", "Temporary Items");
222 }
223
224 // standardLocations() may return an empty list on some platforms
226 return QCoreApplication::translate("QStandardPaths", "Applications");
227
228 const QCFString fsPath(standardLocations(type).constFirst());
229 if (QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
230 fsPath, kCFURLPOSIXPathStyle, true)) {
232 CFURLCopyResourcePropertyForKey(url, kCFURLLocalizedNameKey, &name, NULL);
233 if (name && CFStringGetLength(name))
234 return QString::fromCFString(name);
235 }
236
238}
239#endif
240
242
243#endif // QT_NO_STANDARDPATHS
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
QString organizationName
the name of the organization that wrote this application
QString applicationName
the name of this application
static QString tempPath()
Returns the absolute canonical path of the system's temporary directory.
Definition qdir.cpp:2133
static QString homePath()
Returns the absolute path of the user's home directory.
Definition qdir.cpp:2103
QString fileName() const
iterator begin()
Definition qset.h:136
iterator end()
Definition qset.h:140
static bool isTestModeEnabled()
static QStringList standardLocations(StandardLocation type)
static QString writableLocation(StandardLocation type)
static QString displayName(StandardLocation type)
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
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define Q_FALLTHROUGH()
GLint location
GLenum type
GLuint name
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLsizei const GLchar *const * path
static QString baseWritableLocation(QStandardPaths::StandardLocation type, NSSearchPathDomainMask mask=NSUserDomainMask, bool appendOrgAndApp=false)
static NSSearchPathDirectory searchPathDirectory(QStandardPaths::StandardLocation type)
static void appendOrganizationAndApp(QString &path)
static QString pathForDirectory(NSSearchPathDirectory directory, NSSearchPathDomainMask mask)
#define Q_UNUSED(x)
QUrl url("example.com")
[constructor-url-reference]
QString dir
[11]