1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <qsettings.h>
43 #include <qdir.h>
44 #include <private/qsystemlibrary_p.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 #endif
58 
59 #ifndef CSIDL_MYMUSIC
60 #define CSIDL_MYMUSIC	13
61 #define CSIDL_MYVIDEO	14
62 #endif
63 
64 #ifndef QT_NO_DESKTOPSERVICES
65 
66 QT_BEGIN_NAMESPACE
67 
shellExecute(const QUrl & url)68 static inline bool shellExecute(const QUrl &url)
69 {
70 #ifndef Q_OS_WINCE
71     if (!url.isValid())
72         return false;
73 
74     const QString nativeFilePath =
75             url.isLocalFile() ? QDir::toNativeSeparators(url.toLocalFile()) : url.toString();
76     const quintptr result = (quintptr)ShellExecute(0, 0, (wchar_t*)nativeFilePath.utf16(), 0, 0, SW_SHOWNORMAL);
77     // ShellExecute returns a value greater than 32 if successful
78     if (result <= 32) {
79         qWarning("ShellExecute '%s' failed (error %s).", qPrintable(url.toString()), qPrintable(QString::number(result)));
80         return false;
81     }
82     return true;
83 #else
84     Q_UNUSED(url);
85     return false;
86 #endif
87 }
88 
openDocument(const QUrl & file)89 static bool openDocument(const QUrl &file)
90 {
91     return shellExecute(file);
92 }
93 
expandEnvStrings(const QString & command)94 static QString expandEnvStrings(const QString &command)
95 {
96 #if defined(Q_OS_WINCE)
97     return command;
98 #else
99     wchar_t buffer[MAX_PATH];
100     if (ExpandEnvironmentStrings((wchar_t*)command.utf16(), buffer, MAX_PATH))
101         return QString::fromWCharArray(buffer);
102     else
103         return command;
104 #endif
105 }
106 
launchWebBrowser(const QUrl & url)107 static bool launchWebBrowser(const QUrl &url)
108 {
109     if (url.scheme() == QLatin1String("mailto")) {
110         //Retrieve the commandline for the default mail client
111         //the default key used below is the command line for the mailto: shell command
112         DWORD  bufferSize = sizeof(wchar_t) * MAX_PATH;
113         long  returnValue =  -1;
114         QString command;
115 
116         HKEY handle;
117         LONG res;
118         wchar_t keyValue[MAX_PATH] = {0};
119         QString keyName(QLatin1String("mailto"));
120 
121         //Check if user has set preference, otherwise use default.
122         res = RegOpenKeyEx(HKEY_CURRENT_USER,
123                            L"Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\mailto\\UserChoice",
124                            0, KEY_READ, &handle);
125         if (res == ERROR_SUCCESS) {
126             returnValue = RegQueryValueEx(handle, L"Progid", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize);
127             if (!returnValue)
128                 keyName = QString::fromUtf16((const ushort*)keyValue);
129             RegCloseKey(handle);
130         }
131         keyName += QLatin1String("\\Shell\\Open\\Command");
132         res = RegOpenKeyExW(HKEY_CLASSES_ROOT, (const wchar_t*)keyName.utf16(), 0, KEY_READ, &handle);
133         if (res != ERROR_SUCCESS)
134             return false;
135 
136         bufferSize = sizeof(wchar_t) * MAX_PATH;
137         returnValue = RegQueryValueEx(handle, L"", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize);
138         if (!returnValue)
139             command = QString::fromRawData((QChar*)keyValue, bufferSize);
140         RegCloseKey(handle);
141 
142         if (returnValue)
143             return false;
144 
145         command = expandEnvStrings(command);
146         command = command.trimmed();
147         //Make sure the path for the process is in quotes
148         int index = -1 ;
149         if (command[0]!= QLatin1Char('\"')) {
150             index = command.indexOf(QLatin1String(".exe "), 0, Qt::CaseInsensitive);
151             command.insert(index+4, QLatin1Char('\"'));
152             command.insert(0, QLatin1Char('\"'));
153         }
154         //pass the url as the parameter
155         index =  command.lastIndexOf(QLatin1String("%1"));
156         if (index != -1){
157             command.replace(index, 2, url.toString());
158         }
159         //start the process
160         PROCESS_INFORMATION pi;
161         ZeroMemory(&pi, sizeof(pi));
162         STARTUPINFO si;
163         ZeroMemory(&si, sizeof(si));
164         si.cb = sizeof(si);
165 
166         returnValue = CreateProcess(NULL, (wchar_t*)command.utf16(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
167 
168         if (!returnValue)
169             return false;
170 
171         CloseHandle(pi.hProcess);
172         CloseHandle(pi.hThread);
173         return true;
174     }
175 
176     return shellExecute(url);
177 }
178 
storageLocation(StandardLocation type)179 QString QDesktopServices::storageLocation(StandardLocation type)
180 {
181     QString result;
182 
183 #ifndef Q_OS_WINCE
184         QSystemLibrary library(QLatin1String("shell32"));
185 #else
186         QSystemLibrary library(QLatin1String("coredll"));
187 #endif // Q_OS_WINCE
188     typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPWSTR, int, BOOL);
189     static GetSpecialFolderPath SHGetSpecialFolderPath =
190             (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathW");
191     if (!SHGetSpecialFolderPath)
192         return QString();
193 
194     wchar_t path[MAX_PATH];
195 
196     switch (type) {
197     case DataLocation:
198 #if defined Q_WS_WINCE
199         if (SHGetSpecialFolderPath(0, path, CSIDL_APPDATA, FALSE)) {
200 #else
201         if (SHGetSpecialFolderPath(0, path, CSIDL_LOCAL_APPDATA, FALSE)) {
202 #endif
203             result = QString::fromWCharArray(path);
204             if (!QCoreApplication::organizationName().isEmpty())
205                 result += QLatin1String("\\") + QCoreApplication::organizationName();
206             if (!QCoreApplication::applicationName().isEmpty())
207                 result += QLatin1String("\\") + QCoreApplication::applicationName();
208         }
209         break;
210 
211     case DesktopLocation:
212         if (SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, FALSE))
213             result = QString::fromWCharArray(path);
214         break;
215 
216     case DocumentsLocation:
217         if (SHGetSpecialFolderPath(0, path, CSIDL_PERSONAL, FALSE))
218             result = QString::fromWCharArray(path);
219         break;
220 
221     case FontsLocation:
222         if (SHGetSpecialFolderPath(0, path, CSIDL_FONTS, FALSE))
223             result = QString::fromWCharArray(path);
224         break;
225 
226     case ApplicationsLocation:
227         if (SHGetSpecialFolderPath(0, path, CSIDL_PROGRAMS, FALSE))
228             result = QString::fromWCharArray(path);
229         break;
230 
231     case MusicLocation:
232         if (SHGetSpecialFolderPath(0, path, CSIDL_MYMUSIC, FALSE))
233             result = QString::fromWCharArray(path);
234         break;
235 
236     case MoviesLocation:
237         if (SHGetSpecialFolderPath(0, path, CSIDL_MYVIDEO, FALSE))
238             result = QString::fromWCharArray(path);
239         break;
240 
241     case PicturesLocation:
242         if (SHGetSpecialFolderPath(0, path, CSIDL_MYPICTURES, FALSE))
243             result = QString::fromWCharArray(path);
244         break;
245 
246     case CacheLocation:
247         // Although Microsoft has a Cache key it is a pointer to IE's cache, not a cache
248         // location for everyone.  Most applications seem to be using a
249         // cache directory located in their AppData directory
250         return storageLocation(DataLocation) + QLatin1String("\\cache");
251 
252     case QDesktopServices::HomeLocation:
253         return QDir::homePath(); break;
254 
255     case QDesktopServices::TempLocation:
256         return QDir::tempPath(); break;
257 
258     default:
259         break;
260     }
261     return result;
262 }
263 
264 QString QDesktopServices::displayName(StandardLocation type)
265 {
266     Q_UNUSED(type);
267     return QString();
268 }
269 
270 QT_END_NAMESPACE
271 
272 #endif // QT_NO_DESKTOPSERVICES
273