Qt 6.x
The Qt SDK
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;
123 default:
125 break;
126 }
127
128 if (appendOrgAndApp) {
129 switch (type) {
135 break;
136 default:
137 break;
138 }
139 }
140
141 return path;
142}
143
145{
146 QString location = baseWritableLocation(type, NSUserDomainMask, true);
147 if (isTestModeEnabled())
148 location = location.replace(QDir::homePath(), QDir::homePath() + "/.qttest"_L1);
149
150 return location;
151}
152
154{
155 QStringList dirs;
156
157#if defined(QT_PLATFORM_UIKIT)
158 if (type == PicturesLocation)
159 dirs << writableLocation(PicturesLocation) << "assets-library://"_L1;
160#endif
161
166 masks << NSLocalDomainMask;
168 masks << NSSystemDomainMask;
169
171 it != masks.end(); ++it) {
172 const QString path = baseWritableLocation(type, *it, true);
173 if (!path.isEmpty() && !dirs.contains(path))
174 dirs.append(path);
175 }
176 }
177
179 CFBundleRef mainBundle = CFBundleGetMainBundle();
180 if (mainBundle) {
181 if (QCFType<CFURLRef> resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle)) {
182 if (QCFType<CFURLRef> absoluteResouresURL = CFURLCopyAbsoluteURL(resourcesURL)) {
183 if (QCFType<CFStringRef> path = CFURLCopyFileSystemPath(absoluteResouresURL,
184 kCFURLPOSIXPathStyle)) {
185 dirs.append(QString::fromCFString(path));
186 }
187 }
188 }
189 }
190 }
191
192 const QString localDir = writableLocation(type);
193 if (!localDir.isEmpty())
194 dirs.prepend(localDir);
195 return dirs;
196}
197
198#ifndef QT_BOOTSTRAPPED
200{
201 // Use "Home" instead of the user's Unix username
203 return QCoreApplication::translate("QStandardPaths", "Home");
204
205 // The temporary directory returned by the old Carbon APIs is ~/Library/Caches/TemporaryItems,
206 // the display name of which ("TemporaryItems") isn't translated by the system. The standard
207 // temporary directory has no reasonable display name either, so use something more sensible.
209 //: macOS: Temporary directory
210 return QCoreApplication::translate("QStandardPaths", "Temporary Items");
211 }
212
213 // standardLocations() may return an empty list on some platforms
215 return QCoreApplication::translate("QStandardPaths", "Applications");
216
217 const QCFString fsPath(standardLocations(type).constFirst());
218 if (QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
219 fsPath, kCFURLPOSIXPathStyle, true)) {
221 CFURLCopyResourcePropertyForKey(url, kCFURLLocalizedNameKey, &name, NULL);
222 if (name && CFStringGetLength(name))
223 return QString::fromCFString(name);
224 }
225
227}
228#endif
229
231
232#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:2130
static QString homePath()
Returns the absolute path of the user's home directory.
Definition qdir.cpp:2100
\inmodule QtCore \reentrant
Definition qfileinfo.h:22
QString fileName() const
Returns the name of the file, excluding the path.
Definition qlist.h:74
iterator end()
Definition qlist.h:609
iterator begin()
Definition qlist.h:608
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:127
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
QSet< QString >::iterator it
Combined button and popup list for selecting options.
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]