src/gui/util/qdesktopservices_mac.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 **
       
    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.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 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 {
       
    99     if (!url.isValid() || url.scheme().isEmpty())
       
   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 {
       
   137      if (type == HomeLocation)
       
   138         return QDir::homePath();
       
   139 
       
   140      if (type == TempLocation)
       
   141          return QDir::tempPath();
       
   142 
       
   143     short domain = kOnAppropriateDisk;
       
   144 
       
   145     if (type == DataLocation || type == CacheLocation)
       
   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 
       
   156     QString appName = QCoreApplication::applicationName();
       
   157     if (!appName.isEmpty() && (type == DataLocation || type == CacheLocation))
       
   158         path += QLatin1Char('/') + appName;
       
   159 
       
   160     return path;
       
   161 }
       
   162 
       
   163 QString QDesktopServices::displayName(StandardLocation type)
       
   164 {
       
   165     if (QDesktopServices::HomeLocation == type)
       
   166         return QObject::tr("Home");
       
   167 
       
   168     FSRef ref;
       
   169     OSErr err = FSFindFolder(kOnAppropriateDisk, translateLocation(type), false, &ref);
       
   170     if (err)
       
   171         return QString();
       
   172 
       
   173     QCFString displayName;
       
   174     err = LSCopyDisplayNameForRef(&ref, &displayName);
       
   175     if (err)
       
   176         return QString();
       
   177 
       
   178     return static_cast<QString>(displayName);
       
   179 }
       
   180 
       
   181 QT_END_NAMESPACE
       
   182 
       
   183 #endif // QT_NO_DESKTOPSERVICES