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

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "widgetsopenwithhandler.h"
0009 #include "kopenwithdialog.h"
0010 #include "openurljob.h"
0011 
0012 #include <KConfigGroup>
0013 #include <KJobWidgets>
0014 #include <KLocalizedString>
0015 #include <KSharedConfig>
0016 
0017 #include <QApplication>
0018 
0019 #ifdef Q_OS_WIN
0020 #include "widgetsopenwithhandler_win.cpp" // displayNativeOpenWithDialog
0021 #endif
0022 
0023 KIO::WidgetsOpenWithHandler::WidgetsOpenWithHandler(QObject *parent)
0024     : KIO::OpenWithHandlerInterface(parent)
0025 {
0026 }
0027 
0028 KIO::WidgetsOpenWithHandler::~WidgetsOpenWithHandler() = default;
0029 
0030 void KIO::WidgetsOpenWithHandler::setWindow(QWidget *widget)
0031 {
0032     m_parentWidget = widget;
0033 }
0034 
0035 void KIO::WidgetsOpenWithHandler::promptUserForApplication(KJob *job, const QList<QUrl> &urls, const QString &mimeType)
0036 {
0037     QWidget *parentWidget = nullptr;
0038 
0039     if (job) {
0040         parentWidget = KJobWidgets::window(job);
0041     }
0042 
0043     if (!parentWidget) {
0044         parentWidget = m_parentWidget;
0045     }
0046 
0047     if (!parentWidget) {
0048         parentWidget = qApp->activeWindow();
0049     }
0050 
0051 #ifdef Q_OS_WIN
0052     KConfigGroup cfgGroup(KSharedConfig::openConfig(), QStringLiteral("KOpenWithDialog Settings"));
0053     if (cfgGroup.readEntry("Native", true)) {
0054         // Implemented in applicationlauncherjob_win.cpp
0055         if (displayNativeOpenWithDialog(urls, parentWidget)) {
0056             Q_EMIT handled();
0057             return;
0058         } else {
0059             // Some error happened with the Windows-specific code. Fallback to the KDE one...
0060         }
0061     }
0062 #endif
0063 
0064     KOpenWithDialog *dialog = new KOpenWithDialog(urls, mimeType, QString(), QString(), parentWidget);
0065     dialog->setAttribute(Qt::WA_DeleteOnClose);
0066     connect(dialog, &QDialog::accepted, this, [=]() {
0067         KService::Ptr service = dialog->service();
0068         if (!service) {
0069             service = KService::Ptr(new KService(QString() /*name*/, dialog->text(), QString() /*icon*/));
0070         }
0071         Q_EMIT serviceSelected(service);
0072     });
0073     connect(dialog, &QDialog::rejected, this, [this]() {
0074         Q_EMIT canceled();
0075     });
0076     dialog->show();
0077 }
0078 
0079 #include "moc_widgetsopenwithhandler.cpp"