| 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 | #include <qsettings.h>
|
|---|
| 43 | #include <qdir.h>
|
|---|
| 44 | #include <qurl.h>
|
|---|
| 45 | #include <qstringlist.h>
|
|---|
| 46 | #include <qprocess.h>
|
|---|
| 47 | #include <qtemporaryfile.h>
|
|---|
| 48 | #include <qcoreapplication.h>
|
|---|
| 49 |
|
|---|
| 50 | #include <windows.h>
|
|---|
| 51 | #include <shlobj.h>
|
|---|
| 52 | #if !defined(Q_OS_WINCE)
|
|---|
| 53 | # include <intshcut.h>
|
|---|
| 54 | #else
|
|---|
| 55 | # include <qguifunctions_wince.h>
|
|---|
| 56 | # if !defined(STANDARDSHELL_UI_MODEL)
|
|---|
| 57 | # include <winx.h>
|
|---|
| 58 | # endif
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| 61 | #ifndef QT_NO_DESKTOPSERVICES
|
|---|
| 62 |
|
|---|
| 63 | QT_BEGIN_NAMESPACE
|
|---|
| 64 |
|
|---|
| 65 | //#undef UNICODE
|
|---|
| 66 |
|
|---|
| 67 | static bool openDocument(const QUrl &file)
|
|---|
| 68 | {
|
|---|
| 69 | if (!file.isValid())
|
|---|
| 70 | return false;
|
|---|
| 71 |
|
|---|
| 72 | quintptr returnValue;
|
|---|
| 73 | QT_WA({
|
|---|
| 74 | returnValue = (quintptr)ShellExecute(0, 0, (TCHAR *)file.toString().utf16(), 0, 0, SW_SHOWNORMAL);
|
|---|
| 75 | } , {
|
|---|
| 76 | returnValue = (quintptr)ShellExecuteA(0, 0, file.toString().toLocal8Bit().constData(), 0, 0, SW_SHOWNORMAL);
|
|---|
| 77 | });
|
|---|
| 78 | return (returnValue > 32); //ShellExecute returns a value greater than 32 if successful
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | static QString expandEnvStrings(const QString &command)
|
|---|
| 82 | {
|
|---|
| 83 |
|
|---|
| 84 | #if defined(Q_OS_WINCE)
|
|---|
| 85 | return command;
|
|---|
| 86 | #else
|
|---|
| 87 | QByteArray path = command.toLocal8Bit();
|
|---|
| 88 | char commandValue[2 * MAX_PATH] = {0};
|
|---|
| 89 | DWORD returnValue = ExpandEnvironmentStringsA(path.data(), commandValue, MAX_PATH);
|
|---|
| 90 | if (returnValue)
|
|---|
| 91 | return QString::fromLocal8Bit(commandValue);
|
|---|
| 92 | else
|
|---|
| 93 | return command;
|
|---|
| 94 | #endif
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static bool launchWebBrowser(const QUrl &url)
|
|---|
| 98 | {
|
|---|
| 99 | if (url.scheme() == QLatin1String("mailto")) {
|
|---|
| 100 | //Retrieve the commandline for the default mail client
|
|---|
| 101 | //the key used below is the command line for the mailto: shell command
|
|---|
| 102 | DWORD bufferSize = 2 * MAX_PATH;
|
|---|
| 103 | long returnValue = -1;
|
|---|
| 104 | QString command;
|
|---|
| 105 |
|
|---|
| 106 | HKEY handle;
|
|---|
| 107 | LONG res;
|
|---|
| 108 | QT_WA ({
|
|---|
| 109 | res = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"mailto\\Shell\\Open\\Command", 0, KEY_READ, &handle);
|
|---|
| 110 | if (res != ERROR_SUCCESS)
|
|---|
| 111 | return false;
|
|---|
| 112 |
|
|---|
| 113 | wchar_t keyValue[2 * MAX_PATH] = {0};
|
|---|
| 114 | returnValue = RegQueryValueExW(handle, L"", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize);
|
|---|
| 115 | if (!returnValue)
|
|---|
| 116 | command = QString::fromRawData((QChar*)keyValue, bufferSize);
|
|---|
| 117 | }, {
|
|---|
| 118 | res = RegOpenKeyExA(HKEY_CLASSES_ROOT, "mailto\\Shell\\Open\\Command", 0, KEY_READ, &handle);
|
|---|
| 119 | if (res != ERROR_SUCCESS)
|
|---|
| 120 | return false;
|
|---|
| 121 |
|
|---|
| 122 | char keyValue[2 * MAX_PATH] = {0};
|
|---|
| 123 | returnValue = RegQueryValueExA(handle, "", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize);
|
|---|
| 124 | if (!returnValue)
|
|---|
| 125 | command = QString::fromLocal8Bit(keyValue);
|
|---|
| 126 | });
|
|---|
| 127 | RegCloseKey(handle);
|
|---|
| 128 |
|
|---|
| 129 | if(returnValue)
|
|---|
| 130 | return false;
|
|---|
| 131 | command = expandEnvStrings(command);
|
|---|
| 132 | command = command.trimmed();
|
|---|
| 133 | //Make sure the path for the process is in quotes
|
|---|
| 134 | int index = -1 ;
|
|---|
| 135 | if (command[0]!= QLatin1Char('\"')) {
|
|---|
| 136 | index = command.indexOf(QLatin1String(".exe "), 0, Qt::CaseInsensitive);
|
|---|
| 137 | command.insert(index+4, QLatin1Char('\"'));
|
|---|
| 138 | command.insert(0, QLatin1Char('\"'));
|
|---|
| 139 | }
|
|---|
| 140 | //pass the url as the parameter
|
|---|
| 141 | index = command.lastIndexOf(QLatin1String("%1"));
|
|---|
| 142 | if (index != -1){
|
|---|
| 143 | command.replace(index, 2, url.toString());
|
|---|
| 144 | }
|
|---|
| 145 | //start the process
|
|---|
| 146 | PROCESS_INFORMATION pi;
|
|---|
| 147 | ZeroMemory(&pi, sizeof(pi));
|
|---|
| 148 | QT_WA ({
|
|---|
| 149 | STARTUPINFO si;
|
|---|
| 150 | ZeroMemory(&si, sizeof(si));
|
|---|
| 151 | si.cb = sizeof(si);
|
|---|
| 152 |
|
|---|
| 153 | returnValue = CreateProcess(NULL, (TCHAR*)command.utf16(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
|
|---|
| 154 | }, {
|
|---|
| 155 | STARTUPINFOA si;
|
|---|
| 156 | ZeroMemory(&si, sizeof(si));
|
|---|
| 157 | si.cb = sizeof(si);
|
|---|
| 158 |
|
|---|
| 159 | returnValue = CreateProcessA(NULL, command.toLocal8Bit().data(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
|
|---|
| 160 | });
|
|---|
| 161 |
|
|---|
| 162 | if (!returnValue)
|
|---|
| 163 | return false;
|
|---|
| 164 |
|
|---|
| 165 | CloseHandle(pi.hProcess);
|
|---|
| 166 | CloseHandle(pi.hThread);
|
|---|
| 167 | return true;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | if (!url.isValid())
|
|---|
| 171 | return false;
|
|---|
| 172 |
|
|---|
| 173 | quintptr returnValue;
|
|---|
| 174 | QT_WA ({
|
|---|
| 175 | returnValue = (quintptr)ShellExecute(0, 0, (TCHAR *) QString::fromUtf8(url.toEncoded().constData()).utf16(), 0, 0, SW_SHOWNORMAL);
|
|---|
| 176 | } , {
|
|---|
| 177 | returnValue = (quintptr)ShellExecuteA(0, 0, url.toEncoded().constData(), 0, 0, SW_SHOWNORMAL);
|
|---|
| 178 | });
|
|---|
| 179 | return (returnValue > 32);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | QString QDesktopServices::storageLocation(StandardLocation type)
|
|---|
| 183 | {
|
|---|
| 184 | #if !defined(QT_NO_SETTINGS)
|
|---|
| 185 | QSettings settings(QSettings::UserScope, QLatin1String("Microsoft"), QLatin1String("Windows"));
|
|---|
| 186 | settings.beginGroup(QLatin1String("CurrentVersion/Explorer/Shell Folders"));
|
|---|
| 187 | switch (type) {
|
|---|
| 188 | case CacheLocation:
|
|---|
| 189 | // Although Microsoft has a Cache key it is a pointer to IE's cache, not a cache
|
|---|
| 190 | // location for everyone. Most applications seem to be using a
|
|---|
| 191 | // cache directory located in their AppData directory
|
|---|
| 192 | return storageLocation(DataLocation) + QLatin1String("\\cache");
|
|---|
| 193 | case DataLocation:
|
|---|
| 194 | if (!settings.contains(QLatin1String("Local AppData")))
|
|---|
| 195 | break;
|
|---|
| 196 | return settings.value(QLatin1String("Local AppData")).toString()
|
|---|
| 197 | + QLatin1String("\\") + QCoreApplication::organizationName()
|
|---|
| 198 | + QLatin1String("\\") + QCoreApplication::applicationName();
|
|---|
| 199 | break;
|
|---|
| 200 | case DesktopLocation:
|
|---|
| 201 | return settings.value(QLatin1String("Desktop")).toString();
|
|---|
| 202 | break;
|
|---|
| 203 |
|
|---|
| 204 | case DocumentsLocation:
|
|---|
| 205 | return settings.value(QLatin1String("Personal")).toString();
|
|---|
| 206 | break;
|
|---|
| 207 |
|
|---|
| 208 | case FontsLocation:
|
|---|
| 209 | return settings.value(QLatin1String("Fonts")).toString();
|
|---|
| 210 | break;
|
|---|
| 211 |
|
|---|
| 212 | case ApplicationsLocation:
|
|---|
| 213 | return settings.value(QLatin1String("Programs")).toString();
|
|---|
| 214 | break;
|
|---|
| 215 |
|
|---|
| 216 | case MusicLocation:
|
|---|
| 217 | return settings.value(QLatin1String("My Music")).toString();
|
|---|
| 218 | break;
|
|---|
| 219 |
|
|---|
| 220 | case MoviesLocation:
|
|---|
| 221 | return settings.value(QLatin1String("My Video")).toString();
|
|---|
| 222 | break;
|
|---|
| 223 |
|
|---|
| 224 | case PicturesLocation:
|
|---|
| 225 | return settings.value(QLatin1String("My Pictures")).toString();
|
|---|
| 226 | break;
|
|---|
| 227 |
|
|---|
| 228 | case QDesktopServices::HomeLocation:
|
|---|
| 229 | return QDir::homePath(); break;
|
|---|
| 230 |
|
|---|
| 231 | case QDesktopServices::TempLocation:
|
|---|
| 232 | return QDir::tempPath(); break;
|
|---|
| 233 |
|
|---|
| 234 | default:
|
|---|
| 235 | break;
|
|---|
| 236 | }
|
|---|
| 237 | #endif
|
|---|
| 238 | return QString();
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | QString QDesktopServices::displayName(StandardLocation type)
|
|---|
| 242 | {
|
|---|
| 243 | Q_UNUSED(type);
|
|---|
| 244 | return QString();
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | QT_END_NAMESPACE
|
|---|
| 248 |
|
|---|
| 249 | #endif // QT_NO_DESKTOPSERVICES
|
|---|