File indexing completed on 2024-04-21 03:55:53

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 
0022     FunctionType function = (FunctionType)GetProcAddress(libHandle, functionName.constData());
0023 
0024     if (!function) {
0025         return 0;
0026     }
0027     int result = function((HWND)windowId, libHandle, (LPCWSTR)arguments.utf16(), SW_SHOW);
0028     FreeLibrary(libHandle);
0029     return result;
0030 }
0031 
0032 static int runDll(WId windowId, const QString &libraryName, const QByteArray &functionName, const QByteArray &arguments)
0033 {
0034     HMODULE libHandle = LoadLibraryW((LPCWSTR)libraryName.utf16());
0035     if (!libHandle) {
0036         return 0;
0037     }
0038     typedef int(WINAPI * FunctionType)(HWND, HMODULE, LPCSTR, int);
0039 
0040     FunctionType function = (FunctionType)GetProcAddress(libHandle, functionName.constData());
0041 
0042     if (!function) {
0043         return 0;
0044     }
0045     int result = function((HWND)windowId, libHandle, (LPCSTR)arguments.constData(), SW_SHOW);
0046     FreeLibrary(libHandle);
0047     return result;
0048 }
0049 
0050 // TODO move to a shared lib
0051 static int runDll(QWidget *parent, const QString &libraryName, const QByteArray &functionName, const QString &arguments)
0052 {
0053     return runDll(parent ? parent->winId() : 0, libraryName, functionName, arguments);
0054 }
0055 
0056 // Windows implementation using "OpenAs_RunDLL" entry
0057 static bool displayNativeOpenWithDialog(const QList<QUrl> &lst, QWidget *window)
0058 {
0059     QStringList fnames;
0060     for (const QUrl &url : lst) {
0061         fnames += url.isLocalFile() ? QDir::toNativeSeparators(url.toLocalFile()) : url.toString();
0062     }
0063     int result = runDll(window, QLatin1String("shell32.dll"), "OpenAs_RunDLLW", fnames.join(QLatin1Char(' ')));
0064     return result == 0;
0065 }