KIO

widgetsopenwithhandler.cpp
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);
66 connect(dialog, &QDialog::accepted, this, [=, 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(dialog, &QDialog::rejected, this, [this]() {
74 Q_EMIT canceled();
75 });
76 dialog->show();
77}
78
79#include "moc_widgetsopenwithhandler.cpp"
"Open With" dialog box.
KService::Ptr service() const
QString text() const
QExplicitlySharedDataPointer< KService > Ptr
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
A namespace for KIO globals.
QWidget * window(QObject *job)
void accepted()
void rejected()
WA_DeleteOnClose
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setAttribute(Qt::WidgetAttribute attribute, bool on)
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.