diff options
Diffstat (limited to 'src/gui/util/qdesktopservices_win.cpp')
-rw-r--r-- | src/gui/util/qdesktopservices_win.cpp | 170 |
1 files changed, 94 insertions, 76 deletions
diff --git a/src/gui/util/qdesktopservices_win.cpp b/src/gui/util/qdesktopservices_win.cpp index 50eda42..c0bd5e7 100644 --- a/src/gui/util/qdesktopservices_win.cpp +++ b/src/gui/util/qdesktopservices_win.cpp @@ -41,13 +41,14 @@ #include <qsettings.h> #include <qdir.h> +#include <qlibrary.h> #include <qurl.h> #include <qstringlist.h> #include <qprocess.h> #include <qtemporaryfile.h> #include <qcoreapplication.h> -#include <windows.h> +#include <qt_windows.h> #include <shlobj.h> #if !defined(Q_OS_WINCE) # include <intshcut.h> @@ -58,37 +59,34 @@ # endif #endif +#if defined(Q_CC_MINGW) && !defined(CSIDL_MYMUSIC) +#define CSIDL_MYMUSIC 13 +#define CSIDL_MYVIDEO 14 +#endif + #ifndef QT_NO_DESKTOPSERVICES QT_BEGIN_NAMESPACE -//#undef UNICODE - static bool openDocument(const QUrl &file) { if (!file.isValid()) return false; - - quintptr returnValue; - QT_WA({ - returnValue = (quintptr)ShellExecute(0, 0, (TCHAR *)file.toString().utf16(), 0, 0, SW_SHOWNORMAL); - } , { - returnValue = (quintptr)ShellExecuteA(0, 0, file.toString().toLocal8Bit().constData(), 0, 0, SW_SHOWNORMAL); - }); + QString filePath = file.toLocalFile(); + if (filePath.isEmpty()) + filePath = file.toString(); + quintptr returnValue = (quintptr)ShellExecute(0, 0, (wchar_t*)filePath.utf16(), 0, 0, SW_SHOWNORMAL); return (returnValue > 32); //ShellExecute returns a value greater than 32 if successful } static QString expandEnvStrings(const QString &command) { - #if defined(Q_OS_WINCE) return command; #else - QByteArray path = command.toLocal8Bit(); - char commandValue[2 * MAX_PATH] = {0}; - DWORD returnValue = ExpandEnvironmentStringsA(path.data(), commandValue, MAX_PATH); - if (returnValue) - return QString::fromLocal8Bit(commandValue); + wchar_t buffer[MAX_PATH]; + if (ExpandEnvironmentStrings((wchar_t*)command.utf16(), buffer, MAX_PATH)) + return QString::fromWCharArray(buffer); else return command; #endif @@ -98,36 +96,40 @@ static bool launchWebBrowser(const QUrl &url) { if (url.scheme() == QLatin1String("mailto")) { //Retrieve the commandline for the default mail client - //the key used below is the command line for the mailto: shell command + //the default key used below is the command line for the mailto: shell command DWORD bufferSize = 2 * MAX_PATH; long returnValue = -1; QString command; HKEY handle; LONG res; - QT_WA ({ - res = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"mailto\\Shell\\Open\\Command", 0, KEY_READ, &handle); - if (res != ERROR_SUCCESS) - return false; - - wchar_t keyValue[2 * MAX_PATH] = {0}; - returnValue = RegQueryValueExW(handle, L"", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize); - if (!returnValue) - command = QString::fromRawData((QChar*)keyValue, bufferSize); - }, { - res = RegOpenKeyExA(HKEY_CLASSES_ROOT, "mailto\\Shell\\Open\\Command", 0, KEY_READ, &handle); - if (res != ERROR_SUCCESS) - return false; - - char keyValue[2 * MAX_PATH] = {0}; - returnValue = RegQueryValueExA(handle, "", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize); + wchar_t keyValue[2 * MAX_PATH] = {0}; + QString keyName(QLatin1String("mailto")); + + //Check if user has set preference, otherwise use default. + res = RegOpenKeyExW(HKEY_CURRENT_USER, + L"Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\mailto\\UserChoice", + 0, KEY_READ, &handle); + if (res == ERROR_SUCCESS) { + returnValue = RegQueryValueEx(handle, L"Progid", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize); if (!returnValue) - command = QString::fromLocal8Bit(keyValue); - }); + keyName = QString::fromUtf16((const ushort*)keyValue); + RegCloseKey(handle); + } + keyName += QLatin1String("\\Shell\\Open\\Command"); + res = RegOpenKeyExW(HKEY_CLASSES_ROOT, (const wchar_t*)keyName.utf16(), 0, KEY_READ, &handle); + if (res != ERROR_SUCCESS) + return false; + + bufferSize = 2 * MAX_PATH; + returnValue = RegQueryValueExW(handle, L"", 0, 0, reinterpret_cast<unsigned char*>(keyValue), &bufferSize); + if (!returnValue) + command = QString::fromRawData((QChar*)keyValue, bufferSize); RegCloseKey(handle); - if(returnValue) + if (returnValue) return false; + command = expandEnvStrings(command); command = command.trimmed(); //Make sure the path for the process is in quotes @@ -145,19 +147,11 @@ static bool launchWebBrowser(const QUrl &url) //start the process PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); - QT_WA ({ - STARTUPINFO si; - ZeroMemory(&si, sizeof(si)); - si.cb = sizeof(si); - - returnValue = CreateProcess(NULL, (TCHAR*)command.utf16(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); - }, { - STARTUPINFOA si; - ZeroMemory(&si, sizeof(si)); - si.cb = sizeof(si); + STARTUPINFO si; + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); - returnValue = CreateProcessA(NULL, command.toLocal8Bit().data(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); - }); + returnValue = CreateProcess(NULL, (wchar_t*)command.utf16(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); if (!returnValue) return false; @@ -170,61 +164,86 @@ static bool launchWebBrowser(const QUrl &url) if (!url.isValid()) return false; - quintptr returnValue; - QT_WA ({ - returnValue = (quintptr)ShellExecute(0, 0, (TCHAR *) QString::fromUtf8(url.toEncoded().constData()).utf16(), 0, 0, SW_SHOWNORMAL); - } , { - returnValue = (quintptr)ShellExecuteA(0, 0, url.toEncoded().constData(), 0, 0, SW_SHOWNORMAL); - }); + if (url.scheme().isEmpty()) + return openDocument(url); + + quintptr returnValue = (quintptr)ShellExecute(0, 0, (wchar_t *)QString::fromUtf8(url.toEncoded().constData()).utf16(), + 0, 0, SW_SHOWNORMAL); return (returnValue > 32); } QString QDesktopServices::storageLocation(StandardLocation type) { -#if !defined(QT_NO_SETTINGS) - QSettings settings(QSettings::UserScope, QLatin1String("Microsoft"), QLatin1String("Windows")); - settings.beginGroup(QLatin1String("CurrentVersion/Explorer/Shell Folders")); + QString result; + +#ifndef Q_OS_WINCE + QLibrary library(QLatin1String("shell32")); +#else + QLibrary library(QLatin1String("coredll")); +#endif // Q_OS_WINCE + typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPWSTR, int, BOOL); + static GetSpecialFolderPath SHGetSpecialFolderPath = + (GetSpecialFolderPath)library.resolve("SHGetSpecialFolderPathW"); + if (!SHGetSpecialFolderPath) + return QString(); + + wchar_t path[MAX_PATH]; + switch (type) { - case CacheLocation: - // Although Microsoft has a Cache key it is a pointer to IE's cache, not a cache - // location for everyone. Most applications seem to be using a - // cache directory located in their AppData directory - return storageLocation(DataLocation) + QLatin1String("\\cache"); case DataLocation: - if (!settings.contains(QLatin1String("Local AppData"))) - break; - return settings.value(QLatin1String("Local AppData")).toString() - + QLatin1String("\\") + QCoreApplication::organizationName() - + QLatin1String("\\") + QCoreApplication::applicationName(); +#if defined Q_WS_WINCE + if (SHGetSpecialFolderPath(0, path, CSIDL_APPDATA, FALSE)) +#else + if (SHGetSpecialFolderPath(0, path, CSIDL_LOCAL_APPDATA, FALSE)) +#endif + result = QString::fromWCharArray(path); + if (!QCoreApplication::organizationName().isEmpty()) + result = result + QLatin1String("\\") + QCoreApplication::organizationName(); + if (!QCoreApplication::applicationName().isEmpty()) + result = result + QLatin1String("\\") + QCoreApplication::applicationName(); break; + case DesktopLocation: - return settings.value(QLatin1String("Desktop")).toString(); + if (SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, FALSE)) + result = QString::fromWCharArray(path); break; case DocumentsLocation: - return settings.value(QLatin1String("Personal")).toString(); + if (SHGetSpecialFolderPath(0, path, CSIDL_PERSONAL, FALSE)) + result = QString::fromWCharArray(path); break; case FontsLocation: - return settings.value(QLatin1String("Fonts")).toString(); + if (SHGetSpecialFolderPath(0, path, CSIDL_FONTS, FALSE)) + result = QString::fromWCharArray(path); break; case ApplicationsLocation: - return settings.value(QLatin1String("Programs")).toString(); + if (SHGetSpecialFolderPath(0, path, CSIDL_PROGRAMS, FALSE)) + result = QString::fromWCharArray(path); break; case MusicLocation: - return settings.value(QLatin1String("My Music")).toString(); + if (SHGetSpecialFolderPath(0, path, CSIDL_MYMUSIC, FALSE)) + result = QString::fromWCharArray(path); break; case MoviesLocation: - return settings.value(QLatin1String("My Video")).toString(); + if (SHGetSpecialFolderPath(0, path, CSIDL_MYVIDEO, FALSE)) + result = QString::fromWCharArray(path); break; case PicturesLocation: - return settings.value(QLatin1String("My Pictures")).toString(); + if (SHGetSpecialFolderPath(0, path, CSIDL_MYPICTURES, FALSE)) + result = QString::fromWCharArray(path); break; + case CacheLocation: + // Although Microsoft has a Cache key it is a pointer to IE's cache, not a cache + // location for everyone. Most applications seem to be using a + // cache directory located in their AppData directory + return storageLocation(DataLocation) + QLatin1String("\\cache"); + case QDesktopServices::HomeLocation: return QDir::homePath(); break; @@ -234,8 +253,7 @@ QString QDesktopServices::storageLocation(StandardLocation type) default: break; } -#endif - return QString(); + return result; } QString QDesktopServices::displayName(StandardLocation type) |