Qt 4.8
qdesktopservices_mac.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 #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 
52 #include <ApplicationServices/ApplicationServices.h>
53 
55 
56 /*
57  Translates a QDesktopServices::StandardLocation into the mac equivalent.
58 */
60 {
61  switch (type) {
63  return kDesktopFolderType; break;
64 
66  return kDocumentsFolderType; break;
67 
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; break;
72 
74  return kApplicationsFolderType; break;
75 
77  return kMusicDocumentsFolderType; break;
78 
80  return kMovieDocumentsFolderType; break;
81 
83  return kPictureDocumentsFolderType; break;
84 
86  return kTemporaryFolderType; break;
87 
89  return kApplicationSupportFolderType; break;
90 
92  return kCachedDataFolderType; break;
93 
94  default:
95  return kDesktopFolderType; break;
96  }
97 }
98 
99 static bool lsOpen(const QUrl &url)
100 {
101  if (!url.isValid() || url.scheme().isEmpty())
102  return false;
103 
104  QCFType<CFURLRef> cfUrl = CFURLCreateWithString(0, QCFString(QString::fromLatin1(url.toEncoded())), 0);
105  if (cfUrl == 0)
106  return false;
107 
108  const OSStatus err = LSOpenCFURLRef(cfUrl, 0);
109  return (err == noErr);
110 }
111 
112 static bool launchWebBrowser(const QUrl &url)
113 {
114  return lsOpen(url);
115 }
116 
117 static bool openDocument(const QUrl &file)
118 {
119  if (!file.isValid())
120  return false;
121 
122  // LSOpen does not work in this case, use QProcess open instead.
123  return QProcess::startDetached(QLatin1String("open"), QStringList() << file.toLocalFile());
124 }
125 
126 /*
127  Constructs a full unicode path from a FSRef.
128 */
129 static QString getFullPath(const FSRef &ref)
130 {
131  QByteArray ba(2048, 0);
132  if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
134  return QString();
135 }
136 
138 {
139  if (type == HomeLocation)
140  return QDir::homePath();
141 
142  if (type == TempLocation)
143  return QDir::tempPath();
144 
145  short domain = kOnAppropriateDisk;
146 
147  if (type == DataLocation || type == CacheLocation)
148  domain = kUserDomain;
149 
150  // http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
151  FSRef ref;
152  OSErr err = FSFindFolder(domain, translateLocation(type), false, &ref);
153  if (err)
154  return QString();
155 
156  QString path = getFullPath(ref);
157 
158  if (type == DataLocation || type == CacheLocation) {
163  }
164 
165  return path;
166 }
167 
169 {
170  if (QDesktopServices::HomeLocation == type)
171  return QObject::tr("Home");
172 
173  FSRef ref;
174  OSErr err = FSFindFolder(kOnAppropriateDisk, translateLocation(type), false, &ref);
175  if (err)
176  return QString();
177 
179  err = LSCopyDisplayNameForRef(&ref, &displayName);
180  if (err)
181  return QString();
182 
183  return static_cast<QString>(displayName);
184 }
185 
187 
188 #endif // QT_NO_DESKTOPSERVICES
static bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, qint64 *pid=0)
Starts the program program with the arguments arguments in a new process, and detaches from it...
Definition: qprocess.cpp:2332
static bool launchWebBrowser(const QUrl &url)
int type
Definition: qmetatype.cpp:239
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
static QString storageLocation(StandardLocation type)
Returns the default system directory where files of type belong, or an empty string if the location c...
static QString displayName(StandardLocation type)
Returns a localized display name for the given location type or an empty QString if no relevant locat...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
StandardLocation
This enum describes the different locations that can be queried by QDesktopServices::storageLocation ...
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString normalized(NormalizationForm mode) const Q_REQUIRED_RESULT
Returns the string in the given Unicode normalization mode.
Definition: qstring.cpp:6635
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool isEmpty(const char *str)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static bool lsOpen(const QUrl &url)
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
static QString getFullPath(const FSRef &ref)
signed long OSStatus
QString toLocalFile() const
Returns the path of this URL formatted as a local file path.
Definition: qurl.cpp:6412
static QString tempPath()
Returns the absolute path of the system&#39;s temporary directory.
Definition: qdir.cpp:1987
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static bool openDocument(const QUrl &file)
OSType translateLocation(QDesktopServices::StandardLocation type)
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
static QString organizationName()
static QString applicationName()
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static QString homePath()
Returns the absolute path of the user&#39;s home directory.
Definition: qdir.cpp:1942