[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 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 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 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 | QT_BEGIN_NAMESPACE
|
---|
| 53 |
|
---|
| 54 | /*
|
---|
| 55 | Translates a QDesktopServices::StandardLocation into the mac equivalent.
|
---|
| 56 | */
|
---|
| 57 | OSType 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 |
|
---|
| 97 | static bool lsOpen(const QUrl &url)
|
---|
| 98 | {
|
---|
[561] | 99 | if (!url.isValid() || url.scheme().isEmpty())
|
---|
[2] | 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 |
|
---|
| 110 | static bool launchWebBrowser(const QUrl &url)
|
---|
| 111 | {
|
---|
| 112 | return lsOpen(url);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | static 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 | */
|
---|
| 127 | static 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 |
|
---|
| 135 | QString QDesktopServices::storageLocation(StandardLocation type)
|
---|
| 136 | {
|
---|
[561] | 137 | if (type == HomeLocation)
|
---|
[2] | 138 | return QDir::homePath();
|
---|
| 139 |
|
---|
[561] | 140 | if (type == TempLocation)
|
---|
| 141 | return QDir::tempPath();
|
---|
| 142 |
|
---|
[2] | 143 | short domain = kOnAppropriateDisk;
|
---|
| 144 |
|
---|
[561] | 145 | if (type == DataLocation || type == CacheLocation)
|
---|
[2] | 146 | domain = kUserDomain;
|
---|
| 147 |
|
---|
| 148 | // http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/Reference/reference.html
|
---|
| 149 | FSRef ref;
|
---|
| 150 | OSErr err = FSFindFolder(domain, translateLocation(type), false, &ref);
|
---|
| 151 | if (err)
|
---|
| 152 | return QString();
|
---|
| 153 |
|
---|
| 154 | QString path = getFullPath(ref);
|
---|
| 155 |
|
---|
[561] | 156 | if (type == DataLocation || type == CacheLocation) {
|
---|
| 157 | if (QCoreApplication::organizationName().isEmpty() == false)
|
---|
| 158 | path += QLatin1Char('/') + QCoreApplication::organizationName();
|
---|
| 159 | if (QCoreApplication::applicationName().isEmpty() == false)
|
---|
| 160 | path += QLatin1Char('/') + QCoreApplication::applicationName();
|
---|
| 161 | }
|
---|
[2] | 162 |
|
---|
| 163 | return path;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | QString QDesktopServices::displayName(StandardLocation type)
|
---|
| 167 | {
|
---|
| 168 | if (QDesktopServices::HomeLocation == type)
|
---|
| 169 | return QObject::tr("Home");
|
---|
| 170 |
|
---|
| 171 | FSRef ref;
|
---|
| 172 | OSErr err = FSFindFolder(kOnAppropriateDisk, translateLocation(type), false, &ref);
|
---|
| 173 | if (err)
|
---|
| 174 | return QString();
|
---|
| 175 |
|
---|
| 176 | QCFString displayName;
|
---|
| 177 | err = LSCopyDisplayNameForRef(&ref, &displayName);
|
---|
| 178 | if (err)
|
---|
| 179 | return QString();
|
---|
| 180 |
|
---|
| 181 | return static_cast<QString>(displayName);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | QT_END_NAMESPACE
|
---|
| 185 |
|
---|
| 186 | #endif // QT_NO_DESKTOPSERVICES
|
---|