src/gui/util/qdesktopservices_win.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 #include <qsettings.h>
       
    43 #include <qdir.h>
       
    44 #include <qlibrary.h>
       
    45 #include <qurl.h>
       
    46 #include <qstringlist.h>
       
    47 #include <qprocess.h>
       
    48 #include <qtemporaryfile.h>
       
    49 #include <qcoreapplication.h>
       
    50 
       
    51 #include <qt_windows.h>
       
    52 #include <shlobj.h>
       
    53 #if !defined(Q_OS_WINCE)
       
    54 #  include <intshcut.h>
       
    55 #else
       
    56 #  include <qguifunctions_wince.h>
       
    57 #  if !defined(STANDARDSHELL_UI_MODEL)
       
    58 #    include <winx.h>
       
    59 #  endif
       
    60 #endif
       
    61 
       
    62 #if defined(Q_CC_MINGW) && !defined(CSIDL_MYMUSIC)
       
    63 #define CSIDL_MYMUSIC	13
       
    64 #define CSIDL_MYVIDEO	14
       
    65 #endif
       
    66 
       
    67 #ifndef QT_NO_DESKTOPSERVICES
       
    68 
       
    69 QT_BEGIN_NAMESPACE
       
    70 
       
    71 static bool openDocument(const QUrl &file)
       
    72 {
       
    73     if (!file.isValid())
       
    74         return false;
       
    75     QString filePath = file.toLocalFile();
       
    76     if (filePath.isEmpty())
       
    77         filePath = file.toString();
       
    78     quintptr returnValue = (quintptr)ShellExecute(0, 0, (wchar_t*)filePath.utf16(), 0, 0, SW_SHOWNORMAL);
       
    79     return (returnValue > 32); //ShellExecute returns a value greater than 32 if successful
       
    80 }
       
    81 
       
    82 static QString expandEnvStrings(const QString &command)
       
    83 {
       
    84 #if defined(Q_OS_WINCE)
       
    85     return command;
       
    86 #else
       
    87     wchar_t buffer[MAX_PATH];
       
    88     if (ExpandEnvironmentStrings((wchar_t*)command.utf16(), buffer, MAX_PATH))
       
    89         return QString::fromWCharArray(buffer);
       
    90     else
       
    91         return command;
       
    92 #endif
       
    93 }
       
    94 
       
    95 static bool launchWebBrowser(const QUrl &url)
       
    96 {
       
    97     if (url.scheme() == QLatin1String("mailto")) {
       
    98         //Retrieve the commandline for the default mail client
       
    99         //the default key used below is the command line for the mailto: shell command
       
   100         DWORD  bufferSize = 2 * MAX_PATH;
       
   101         long  returnValue =  -1;
       
   102         QString command;
       
   103 
       
   104         HKEY handle;
       
   105         LONG res;
       
   106         wchar_t keyValue[2 * MAX_PATH] = {0};
       
   107         QString keyName(QLatin1String("mailto"));
       
   108 
       
   109         //Check if user has set preference, otherwise use default.
       
   110         res = RegOpenKeyExW(HKEY_CURRENT_USER,
       
   111                             L"Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\mailto\\UserChoice",
       
   112                             0, KEY_READ, &handle);
       
   113         if (res == ERROR_SUCCESS) {
       
   114             returnValue = RegQueryValueEx(handle, L"Progid", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize);
       
   115             if (!returnValue)
       
   116                 keyName = QString::fromUtf16((const ushort*)keyValue);
       
   117             RegCloseKey(handle);
       
   118         }
       
   119         keyName += QLatin1String("\\Shell\\Open\\Command");
       
   120         res = RegOpenKeyExW(HKEY_CLASSES_ROOT, (const wchar_t*)keyName.utf16(), 0, KEY_READ, &handle);
       
   121         if (res != ERROR_SUCCESS)
       
   122             return false;
       
   123 
       
   124         bufferSize = 2 * MAX_PATH;
       
   125         returnValue = RegQueryValueExW(handle, L"", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize);
       
   126         if (!returnValue)
       
   127             command = QString::fromRawData((QChar*)keyValue, bufferSize);
       
   128         RegCloseKey(handle);
       
   129 
       
   130         if (returnValue)
       
   131             return false;
       
   132 
       
   133         command = expandEnvStrings(command);
       
   134         command = command.trimmed();
       
   135         //Make sure the path for the process is in quotes
       
   136         int index = -1 ;
       
   137         if (command[0]!= QLatin1Char('\"')) {
       
   138             index = command.indexOf(QLatin1String(".exe "), 0, Qt::CaseInsensitive);
       
   139             command.insert(index+4, QLatin1Char('\"'));
       
   140             command.insert(0, QLatin1Char('\"'));
       
   141         }
       
   142         //pass the url as the parameter
       
   143         index =  command.lastIndexOf(QLatin1String("%1"));
       
   144         if (index != -1){
       
   145             command.replace(index, 2, url.toString());
       
   146         }
       
   147         //start the process
       
   148         PROCESS_INFORMATION pi;
       
   149         ZeroMemory(&pi, sizeof(pi));
       
   150         STARTUPINFO si;
       
   151         ZeroMemory(&si, sizeof(si));
       
   152         si.cb = sizeof(si);
       
   153 
       
   154         returnValue = CreateProcess(NULL, (wchar_t*)command.utf16(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
       
   155 
       
   156         if (!returnValue)
       
   157             return false;
       
   158 
       
   159         CloseHandle(pi.hProcess);
       
   160         CloseHandle(pi.hThread);
       
   161         return true;
       
   162     }
       
   163 
       
   164     if (!url.isValid())
       
   165         return false;
       
   166 
       
   167     if (url.scheme().isEmpty())
       
   168         return openDocument(url);
       
   169 
       
   170     quintptr returnValue = (quintptr)ShellExecute(0, 0, (wchar_t *)QString::fromUtf8(url.toEncoded().constData()).utf16(),
       
   171                                                   0, 0, SW_SHOWNORMAL);
       
   172     return (returnValue > 32);
       
   173 }
       
   174 
       
   175 QString QDesktopServices::storageLocation(StandardLocation type)
       
   176 {
       
   177     QString result;
       
   178 
       
   179 #ifndef Q_OS_WINCE
       
   180         QLibrary library(QLatin1String("shell32"));
       
   181 #else
       
   182         QLibrary library(QLatin1String("coredll"));
       
   183 #endif // Q_OS_WINCE
       
   184     typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPWSTR, int, BOOL);
       
   185     static GetSpecialFolderPath SHGetSpecialFolderPath =
       
   186             (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathW");
       
   187     if (!SHGetSpecialFolderPath)
       
   188         return QString();
       
   189 
       
   190     wchar_t path[MAX_PATH];
       
   191 
       
   192     switch (type) {
       
   193     case DataLocation:
       
   194 #if defined Q_WS_WINCE
       
   195         if (SHGetSpecialFolderPath(0, path, CSIDL_APPDATA, FALSE))
       
   196 #else
       
   197         if (SHGetSpecialFolderPath(0, path, CSIDL_LOCAL_APPDATA, FALSE))
       
   198 #endif
       
   199             result = QString::fromWCharArray(path);
       
   200         if (!QCoreApplication::organizationName().isEmpty())
       
   201             result = result + QLatin1String("\\") + QCoreApplication::organizationName();
       
   202         if (!QCoreApplication::applicationName().isEmpty())
       
   203             result = result + QLatin1String("\\") + QCoreApplication::applicationName();
       
   204         break;
       
   205 
       
   206     case DesktopLocation:
       
   207         if (SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, FALSE))
       
   208             result = QString::fromWCharArray(path);
       
   209         break;
       
   210 
       
   211     case DocumentsLocation:
       
   212         if (SHGetSpecialFolderPath(0, path, CSIDL_PERSONAL, FALSE))
       
   213             result = QString::fromWCharArray(path);
       
   214         break;
       
   215 
       
   216     case FontsLocation:
       
   217         if (SHGetSpecialFolderPath(0, path, CSIDL_FONTS, FALSE))
       
   218             result = QString::fromWCharArray(path);
       
   219         break;
       
   220 
       
   221     case ApplicationsLocation:
       
   222         if (SHGetSpecialFolderPath(0, path, CSIDL_PROGRAMS, FALSE))
       
   223             result = QString::fromWCharArray(path);
       
   224         break;
       
   225 
       
   226     case MusicLocation:
       
   227         if (SHGetSpecialFolderPath(0, path, CSIDL_MYMUSIC, FALSE))
       
   228             result = QString::fromWCharArray(path);
       
   229         break;
       
   230 
       
   231     case MoviesLocation:
       
   232         if (SHGetSpecialFolderPath(0, path, CSIDL_MYVIDEO, FALSE))
       
   233             result = QString::fromWCharArray(path);
       
   234         break;
       
   235 
       
   236     case PicturesLocation:
       
   237         if (SHGetSpecialFolderPath(0, path, CSIDL_MYPICTURES, FALSE))
       
   238             result = QString::fromWCharArray(path);
       
   239         break;
       
   240 
       
   241     case CacheLocation:
       
   242         // Although Microsoft has a Cache key it is a pointer to IE's cache, not a cache
       
   243         // location for everyone.  Most applications seem to be using a
       
   244         // cache directory located in their AppData directory
       
   245         return storageLocation(DataLocation) + QLatin1String("\\cache");
       
   246 
       
   247     case QDesktopServices::HomeLocation:
       
   248         return QDir::homePath(); break;
       
   249 
       
   250     case QDesktopServices::TempLocation:
       
   251         return QDir::tempPath(); break;
       
   252 
       
   253     default:
       
   254         break;
       
   255     }
       
   256     return result;
       
   257 }
       
   258 
       
   259 QString QDesktopServices::displayName(StandardLocation type)
       
   260 {
       
   261     Q_UNUSED(type);
       
   262     return QString();
       
   263 }
       
   264 
       
   265 QT_END_NAMESPACE
       
   266 
       
   267 #endif // QT_NO_DESKTOPSERVICES