source: trunk/src/gui/util/qdesktopservices_mac.cpp@ 123

Last change on this file since 123 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QT_NO_DESKTOPSERVICES
43
44#include <qprocess.h>
45#include <qstringlist.h>
46#include <qdir.h>
47#include <qurl.h>
48#include <qstringlist.h>
49#include <private/qcore_mac_p.h>
50#include <qcoreapplication.h>
51
52QT_BEGIN_NAMESPACE
53
54/*
55 Translates a QDesktopServices::StandardLocation into the mac equivalent.
56*/
57OSType translateLocation(QDesktopServices::StandardLocation type)
58{
59 switch (type) {
60 case QDesktopServices::DesktopLocation:
61 return kDesktopFolderType; break;
62
63 case QDesktopServices::DocumentsLocation:
64 return kDocumentsFolderType; break;
65
66 case QDesktopServices::FontsLocation:
67 // There are at least two different font directories on the mac: /Library/Fonts and ~/Library/Fonts.
68 // To select a specific one we have to specify a different first parameter when calling FSFindFolder.
69 return kFontsFolderType; break;
70
71 case QDesktopServices::ApplicationsLocation:
72 return kApplicationsFolderType; break;
73
74 case QDesktopServices::MusicLocation:
75 return kMusicDocumentsFolderType; break;
76
77 case QDesktopServices::MoviesLocation:
78 return kMovieDocumentsFolderType; break;
79
80 case QDesktopServices::PicturesLocation:
81 return kPictureDocumentsFolderType; break;
82
83 case QDesktopServices::TempLocation:
84 return kTemporaryFolderType; break;
85
86 case QDesktopServices::DataLocation:
87 return kApplicationSupportFolderType; break;
88
89 case QDesktopServices::CacheLocation:
90 return kCachedDataFolderType; break;
91
92 default:
93 return kDesktopFolderType; break;
94 }
95}
96
97static bool lsOpen(const QUrl &url)
98{
99 if (!url.isValid())
100 return false;
101
102 QCFType<CFURLRef> cfUrl = CFURLCreateWithString(0, QCFString(QString::fromLatin1(url.toEncoded())), 0);
103 if (cfUrl == 0)
104 return false;
105
106 const OSStatus err = LSOpenCFURLRef(cfUrl, 0);
107 return (err == noErr);
108}
109
110static bool launchWebBrowser(const QUrl &url)
111{
112 return lsOpen(url);
113}
114
115static bool openDocument(const QUrl &file)
116{
117 if (!file.isValid())
118 return false;
119
120 // LSOpen does not work in this case, use QProcess open instead.
121 return QProcess::startDetached(QLatin1String("open"), QStringList() << file.toLocalFile());
122}
123
124/*
125 Constructs a full unicode path from a FSRef.
126*/
127static QString getFullPath(const FSRef &ref)
128{
129 QByteArray ba(2048, 0);
130 if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
131 return QString::fromUtf8(ba).normalized(QString::NormalizationForm_C);
132 return QString();
133}
134
135QString QDesktopServices::storageLocation(StandardLocation type)
136{
137 if (QDesktopServices::HomeLocation == type)
138 return QDir::homePath();
139
140 short domain = kOnAppropriateDisk;
141
142 if (QDesktopServices::DataLocation == type
143 || QDesktopServices::CacheLocation == type)
144 domain = kUserDomain;
145
146 // http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
147 FSRef ref;
148 OSErr err = FSFindFolder(domain, translateLocation(type), false, &ref);
149 if (err)
150 return QString();
151
152 QString path = getFullPath(ref);
153
154 QString appName = QCoreApplication::applicationName();
155 if (!appName.isEmpty() &&
156 (QDesktopServices::DataLocation == type || QDesktopServices::CacheLocation == type))
157 path += QLatin1String("/") + appName;
158
159 return path;
160}
161
162QString QDesktopServices::displayName(StandardLocation type)
163{
164 if (QDesktopServices::HomeLocation == type)
165 return QObject::tr("Home");
166
167 FSRef ref;
168 OSErr err = FSFindFolder(kOnAppropriateDisk, translateLocation(type), false, &ref);
169 if (err)
170 return QString();
171
172 QCFString displayName;
173 err = LSCopyDisplayNameForRef(&ref, &displayName);
174 if (err)
175 return QString();
176
177 return static_cast<QString>(displayName);
178}
179
180QT_END_NAMESPACE
181
182#endif // QT_NO_DESKTOPSERVICES
Note: See TracBrowser for help on using the repository browser.