KIO

widgetsopenorexecutefilehandler.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2020 Ahmad Samir <a.samirh78@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "widgetsopenorexecutefilehandler.h"
9
10#include "executablefileopendialog_p.h"
11
12#include <KConfigGroup>
13#include <KJob>
14#include <KJobWidgets>
15#include <KSharedConfig>
16
17#include <QApplication>
18#include <QMimeDatabase>
19
20KIO::WidgetsOpenOrExecuteFileHandler::WidgetsOpenOrExecuteFileHandler(QObject *parent)
21 : KIO::OpenOrExecuteFileInterface(parent)
22{
23}
24
25KIO::WidgetsOpenOrExecuteFileHandler::~WidgetsOpenOrExecuteFileHandler() = default;
26
27static ExecutableFileOpenDialog::Mode promptMode(const QMimeType &mime)
28{
29 // Note that ExecutableFileOpenDialog::OpenAsExecute isn't useful here as
30 // OpenUrlJob treats .exe (application/x-ms-dos-executable) files as executables
31 // that are only opened using the default application associated with that MIME type
32 // e.g. WINE
33
34 if (mime.inherits(QStringLiteral("text/plain"))) {
35 return ExecutableFileOpenDialog::OpenOrExecute;
36 }
37 return ExecutableFileOpenDialog::OnlyExecute;
38}
39
40void KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute(KJob *job, const QString &mimetype)
41{
42 KConfigGroup cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
43 const QString value = cfgGroup.readEntry("behaviourOnLaunch", "alwaysAsk");
44
45 if (value != QLatin1String("alwaysAsk")) {
46 Q_EMIT executeFile(value == QLatin1String("execute"));
47 return;
48 }
49
50 QWidget *parentWidget = nullptr;
51
52 if (job) {
53 parentWidget = KJobWidgets::window(job);
54 }
55
56 if (!parentWidget) {
57 parentWidget = m_parentWidget;
58 }
59
60 if (!parentWidget) {
61 parentWidget = qApp->activeWindow();
62 }
63
66
67 ExecutableFileOpenDialog *dialog = new ExecutableFileOpenDialog(promptMode(mime), parentWidget);
68 dialog->setAttribute(Qt::WA_DeleteOnClose);
69 dialog->setModal(true);
70
71 connect(dialog, &QDialog::finished, this, [this, dialog, mime](const int result) {
72 if (result == ExecutableFileOpenDialog::Rejected) {
74 return;
75 }
76
77 const bool isExecute = result == ExecutableFileOpenDialog::ExecuteFile;
78 Q_EMIT executeFile(isExecute);
79
80 if (dialog->isDontAskAgainChecked()) {
81 KConfigGroup cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
82 cfgGroup.writeEntry("behaviourOnLaunch", isExecute ? "execute" : "open");
83 }
84 });
85
86 dialog->show();
87}
88
89void KIO::WidgetsOpenOrExecuteFileHandler::setWindow(QWidget *window)
90{
91 m_parentWidget = window;
92}
93
94#include "moc_widgetsopenorexecutefilehandler.cpp"
void executeFile(bool enable)
Emitted by promptUserOpenOrExecute() once the user chooses an action.
void canceled()
Emitted by promptUserOpenOrExecute() if user selects cancel.
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
A namespace for KIO globals.
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
Find MIME type for one file or directory.
QWidget * window(QObject *job)
void finished(int result)
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
bool inherits(const QString &mimeTypeName) const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
WA_DeleteOnClose
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:48:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.