1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtCore module of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
#include "qstandardpaths.h"
44
#include <private/qcore_mac_p.h>
46
#ifndef QT_BOOTSTRAPPED
47
#include <qcoreapplication.h>
50
#include <ApplicationServices/ApplicationServices.h>
55
Translates a QStandardPaths::StandardLocation into the mac equivalent.
57
OSType translateLocation(QStandardPaths::StandardLocation type)
60
case QStandardPaths::ConfigLocation:
61
return kPreferencesFolderType;
62
case QStandardPaths::DesktopLocation:
63
return kDesktopFolderType;
64
case QStandardPaths::DownloadLocation: // needs NSSearchPathForDirectoriesInDomains with NSDownloadsDirectory
65
// which needs an objective-C *.mm file...
66
case QStandardPaths::DocumentsLocation:
67
return kDocumentsFolderType;
68
case QStandardPaths::FontsLocation:
69
// There are at least two different font directories on the mac: /Library/Fonts and ~/Library/Fonts.
70
// To select a specific one we have to specify a different first parameter when calling FSFindFolder.
71
return kFontsFolderType;
72
case QStandardPaths::ApplicationsLocation:
73
return kApplicationsFolderType;
74
case QStandardPaths::MusicLocation:
75
return kMusicDocumentsFolderType;
76
case QStandardPaths::MoviesLocation:
77
return kMovieDocumentsFolderType;
78
case QStandardPaths::PicturesLocation:
79
return kPictureDocumentsFolderType;
80
case QStandardPaths::TempLocation:
81
return kTemporaryFolderType;
82
case QStandardPaths::GenericDataLocation:
83
case QStandardPaths::RuntimeLocation:
84
case QStandardPaths::DataLocation:
85
return kApplicationSupportFolderType;
86
case QStandardPaths::GenericCacheLocation:
87
case QStandardPaths::CacheLocation:
88
return kCachedDataFolderType;
90
return kDesktopFolderType;
95
Constructs a full unicode path from a FSRef.
97
static QString getFullPath(const FSRef &ref)
99
QByteArray ba(2048, 0);
100
if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
101
return QString::fromUtf8(ba.constData()).normalized(QString::NormalizationForm_C);
105
static void appendOrganizationAndApp(QString &path)
107
#ifndef QT_BOOTSTRAPPED
108
const QString org = QCoreApplication::organizationName();
110
path += QLatin1Char('/') + org;
111
const QString appName = QCoreApplication::applicationName();
112
if (!appName.isEmpty())
113
path += QLatin1Char('/') + appName;
119
static QString macLocation(QStandardPaths::StandardLocation type, short domain)
121
// http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
123
OSErr err = FSFindFolder(domain, translateLocation(type), false, &ref);
127
QString path = getFullPath(ref);
129
if (type == QStandardPaths::DataLocation || type == QStandardPaths::CacheLocation)
130
appendOrganizationAndApp(path);
134
QString QStandardPaths::writableLocation(StandardLocation type)
136
if (isTestModeEnabled()) {
137
const QString qttestDir = QDir::homePath() + QLatin1String("/.qttest");
140
case GenericDataLocation:
142
path = qttestDir + QLatin1String("/Application Support");
143
if (type == DataLocation)
144
appendOrganizationAndApp(path);
146
case GenericCacheLocation:
148
path = qttestDir + QLatin1String("/Cache");
149
if (type == CacheLocation)
150
appendOrganizationAndApp(path);
153
return qttestDir + QLatin1String("/Preferences");
161
return QDir::homePath();
163
return QDir::tempPath();
164
case GenericDataLocation:
166
case GenericCacheLocation:
168
case RuntimeLocation:
169
return macLocation(type, kUserDomain);
171
return macLocation(type, kOnAppropriateDisk);
175
QStringList QStandardPaths::standardLocations(StandardLocation type)
179
if (type == GenericDataLocation || type == DataLocation || type == GenericCacheLocation || type == CacheLocation) {
180
const QString path = macLocation(type, kOnAppropriateDisk);
185
const QString localDir = writableLocation(type);
186
dirs.prepend(localDir);
190
#ifndef QT_BOOTSTRAPPED
191
QString QStandardPaths::displayName(StandardLocation type)
193
if (QStandardPaths::HomeLocation == type)
194
return QCoreApplication::translate("QStandardPaths", "Home");
197
OSErr err = FSFindFolder(kOnAppropriateDisk, translateLocation(type), false, &ref);
201
QCFString displayName;
202
err = LSCopyDisplayNameForRef(&ref, &displayName);
206
return static_cast<QString>(displayName);