~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/corelib/io/qstandardpaths_mac.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 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
#include <qdir.h>
 
44
#include <private/qcore_mac_p.h>
 
45
 
 
46
#ifndef QT_BOOTSTRAPPED
 
47
#include <qcoreapplication.h>
 
48
#endif
 
49
 
 
50
#include <ApplicationServices/ApplicationServices.h>
 
51
 
 
52
QT_BEGIN_NAMESPACE
 
53
 
 
54
/*
 
55
    Translates a QStandardPaths::StandardLocation into the mac equivalent.
 
56
*/
 
57
OSType translateLocation(QStandardPaths::StandardLocation type)
 
58
{
 
59
    switch (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;
 
89
    default:
 
90
        return kDesktopFolderType;
 
91
    }
 
92
}
 
93
 
 
94
/*
 
95
    Constructs a full unicode path from a FSRef.
 
96
*/
 
97
static QString getFullPath(const FSRef &ref)
 
98
{
 
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);
 
102
    return QString();
 
103
}
 
104
 
 
105
static void appendOrganizationAndApp(QString &path)
 
106
{
 
107
#ifndef QT_BOOTSTRAPPED
 
108
    const QString org = QCoreApplication::organizationName();
 
109
    if (!org.isEmpty())
 
110
        path += QLatin1Char('/') + org;
 
111
    const QString appName = QCoreApplication::applicationName();
 
112
    if (!appName.isEmpty())
 
113
        path += QLatin1Char('/') + appName;
 
114
#else
 
115
    Q_UNUSED(path);
 
116
#endif
 
117
}
 
118
 
 
119
static QString macLocation(QStandardPaths::StandardLocation type, short domain)
 
120
{
 
121
    // http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
 
122
    FSRef ref;
 
123
    OSErr err = FSFindFolder(domain, translateLocation(type), false, &ref);
 
124
    if (err)
 
125
       return QString();
 
126
 
 
127
   QString path = getFullPath(ref);
 
128
 
 
129
    if (type == QStandardPaths::DataLocation || type == QStandardPaths::CacheLocation)
 
130
        appendOrganizationAndApp(path);
 
131
    return path;
 
132
}
 
133
 
 
134
QString QStandardPaths::writableLocation(StandardLocation type)
 
135
{
 
136
    if (isTestModeEnabled()) {
 
137
        const QString qttestDir = QDir::homePath() + QLatin1String("/.qttest");
 
138
        QString path;
 
139
        switch (type) {
 
140
        case GenericDataLocation:
 
141
        case DataLocation:
 
142
            path = qttestDir + QLatin1String("/Application Support");
 
143
            if (type == DataLocation)
 
144
                appendOrganizationAndApp(path);
 
145
            return path;
 
146
        case GenericCacheLocation:
 
147
        case CacheLocation:
 
148
            path = qttestDir + QLatin1String("/Cache");
 
149
            if (type == CacheLocation)
 
150
                appendOrganizationAndApp(path);
 
151
            return path;
 
152
        case ConfigLocation:
 
153
            return qttestDir + QLatin1String("/Preferences");
 
154
        default:
 
155
            break;
 
156
        }
 
157
    }
 
158
 
 
159
    switch (type) {
 
160
    case HomeLocation:
 
161
        return QDir::homePath();
 
162
    case TempLocation:
 
163
        return QDir::tempPath();
 
164
    case GenericDataLocation:
 
165
    case DataLocation:
 
166
    case GenericCacheLocation:
 
167
    case CacheLocation:
 
168
    case RuntimeLocation:
 
169
        return macLocation(type, kUserDomain);
 
170
    default:
 
171
        return macLocation(type, kOnAppropriateDisk);
 
172
    }
 
173
}
 
174
 
 
175
QStringList QStandardPaths::standardLocations(StandardLocation type)
 
176
{
 
177
    QStringList dirs;
 
178
 
 
179
    if (type == GenericDataLocation || type == DataLocation || type == GenericCacheLocation || type == CacheLocation) {
 
180
        const QString path = macLocation(type, kOnAppropriateDisk);
 
181
        if (!path.isEmpty())
 
182
            dirs.append(path);
 
183
    }
 
184
 
 
185
    const QString localDir = writableLocation(type);
 
186
    dirs.prepend(localDir);
 
187
    return dirs;
 
188
}
 
189
 
 
190
#ifndef QT_BOOTSTRAPPED
 
191
QString QStandardPaths::displayName(StandardLocation type)
 
192
{
 
193
    if (QStandardPaths::HomeLocation == type)
 
194
        return QCoreApplication::translate("QStandardPaths", "Home");
 
195
 
 
196
    FSRef ref;
 
197
    OSErr err = FSFindFolder(kOnAppropriateDisk, translateLocation(type), false, &ref);
 
198
    if (err)
 
199
        return QString();
 
200
 
 
201
    QCFString displayName;
 
202
    err = LSCopyDisplayNameForRef(&ref, &displayName);
 
203
    if (err)
 
204
        return QString();
 
205
 
 
206
    return static_cast<QString>(displayName);
 
207
}
 
208
#endif
 
209
 
 
210
QT_END_NAMESPACE