1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "widgetsopenwithhandler.h"
9#include "kopenwithdialog.h"
10#include "openurljob.h"
11
12#include <KConfigGroup>
13#include <KJobWidgets>
14#include <KLocalizedString>
15#include <KSharedConfig>
16
17#include <QApplication>
18
19#ifdef Q_OS_WIN
20#include "widgetsopenwithhandler_win.cpp" // displayNativeOpenWithDialog
21#endif
22
23KIO::WidgetsOpenWithHandler::WidgetsOpenWithHandler(QObject *parent)
24 : KIO::OpenWithHandlerInterface(parent)
25{
26}
27
28KIO::WidgetsOpenWithHandler::~WidgetsOpenWithHandler() = default;
29
30void KIO::WidgetsOpenWithHandler::setWindow(QWidget *widget)
31{
32 m_parentWidget = widget;
33}
34
35void KIO::WidgetsOpenWithHandler::promptUserForApplication(KJob *job, const QList<QUrl> &urls, const QString &mimeType)
36{
37 QWidget *parentWidget = nullptr;
38
39 if (job) {
40 parentWidget = KJobWidgets::window(job);
41 }
42
43 if (!parentWidget) {
44 parentWidget = m_parentWidget;
45 }
46
47 if (!parentWidget) {
48 parentWidget = qApp->activeWindow();
49 }
50
51#ifdef Q_OS_WIN
52 KConfigGroup cfgGroup(KSharedConfig::openConfig(), QStringLiteral("KOpenWithDialog Settings"));
53 if (cfgGroup.readEntry("Native", true)) {
54 // Implemented in applicationlauncherjob_win.cpp
55 if (displayNativeOpenWithDialog(urls, parentWidget)) {
56 Q_EMIT handled();
57 return;
58 } else {
59 // Some error happened with the Windows-specific code. Fallback to the KDE one...
60 }
61 }
62#endif
63
64 KOpenWithDialog *dialog = new KOpenWithDialog(urls, mimeType, QString(), QString(), parentWidget);
65 dialog->setAttribute(Qt::WA_DeleteOnClose);
66 connect(sender: dialog, signal: &QDialog::accepted, context: this, slot: [=, this]() {
67 KService::Ptr service = dialog->service();
68 if (!service) {
69 service = KService::Ptr(new KService(QString() /*name*/, dialog->text(), QString() /*icon*/));
70 }
71 Q_EMIT serviceSelected(service);
72 });
73 connect(sender: dialog, signal: &QDialog::rejected, context: this, slot: [this]() {
74 Q_EMIT canceled();
75 });
76 dialog->show();
77}
78
79#include "moc_widgetsopenwithhandler.cpp"
80

source code of kio/src/widgets/widgetsopenwithhandler.cpp