File indexing completed on 2024-06-16 10:06:24

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2008 Jarosław Staniek <staniek@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QDir>
0009 #include <QWidget>
0010 
0011 #include <qt_windows.h>
0012 
0013 // TODO move to a shared lib
0014 static int runDll(WId windowId, const QString &libraryName, const QByteArray &functionName, const QString &arguments)
0015 {
0016     HMODULE libHandle = LoadLibraryW((LPCWSTR)libraryName.utf16());
0017     if (!libHandle) {
0018         return 0;
0019     }
0020     typedef int(WINAPI * FunctionType)(HWND, HMODULE, LPCWSTR, int);
0021 #ifdef _WIN32_WCE
0022     QString functionNamestr = QString(functionName);
0023     FunctionType function = (FunctionType)GetProcAddressW(libHandle, functionNamestr.utf16());
0024 #else
0025     FunctionType function = (FunctionType)GetProcAddress(libHandle, functionName.constData());
0026 #endif
0027     if (!function) {
0028         return 0;
0029     }
0030     int result = function((HWND)windowId, libHandle, (LPCWSTR)arguments.utf16(), SW_SHOW);
0031     FreeLibrary(libHandle);
0032     return result;
0033 }
0034 
0035 static int runDll(WId windowId, const QString &libraryName, const QByteArray &functionName, const QByteArray &arguments)
0036 {
0037     HMODULE libHandle = LoadLibraryW((LPCWSTR)libraryName.utf16());
0038     if (!libHandle) {
0039         return 0;
0040     }
0041     typedef int(WINAPI * FunctionType)(HWND, HMODULE, LPCSTR, int);
0042 #ifdef _WIN32_WCE
0043     QString functionNamestr = QString(functionName);
0044     FunctionType function = (FunctionType)GetProcAddressW(libHandle, functionNamestr.utf16());
0045 #else
0046     FunctionType function = (FunctionType)GetProcAddress(libHandle, functionName.constData());
0047 #endif
0048     if (!function) {
0049         return 0;
0050     }
0051     int result = function((HWND)windowId, libHandle, (LPCSTR)arguments.constData(), SW_SHOW);
0052     FreeLibrary(libHandle);
0053     return result;
0054 }
0055 
0056 // TODO move to a shared lib
0057 static int runDll(QWidget *parent, const QString &libraryName, const QByteArray &functionName, const QString &arguments)
0058 {
0059     return runDll(parent ? parent->winId() : 0, libraryName, functionName, arguments);
0060 }
0061 
0062 // Windows implementation using "OpenAs_RunDLL" entry
0063 static bool displayNativeOpenWithDialog(const QList<QUrl> &lst, QWidget *window)
0064 {
0065     QStringList fnames;
0066     for (const QUrl &url : lst) {
0067         fnames += url.isLocalFile() ? QDir::toNativeSeparators(url.toLocalFile()) : url.toString();
0068     }
0069     int result = runDll(window, QLatin1String("shell32.dll"), "OpenAs_RunDLLW", fnames.join(QLatin1Char(' ')));
0070     return result == 0;
0071 }