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 | |
20 | KIO::WidgetsOpenOrExecuteFileHandler::WidgetsOpenOrExecuteFileHandler(QObject *parent) |
21 | : KIO::OpenOrExecuteFileInterface(parent) |
22 | { |
23 | } |
24 | |
25 | KIO::WidgetsOpenOrExecuteFileHandler::~WidgetsOpenOrExecuteFileHandler() = default; |
26 | |
27 | static 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 | |
40 | void 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(key: "behaviourOnLaunch" , aDefault: "alwaysAsk" ); |
44 | |
45 | if (value != QLatin1String("alwaysAsk" )) { |
46 | Q_EMIT executeFile(enable: 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 | |
64 | QMimeDatabase db; |
65 | QMimeType mime = db.mimeTypeForName(nameOrAlias: mimetype); |
66 | |
67 | ExecutableFileOpenDialog *dialog = new ExecutableFileOpenDialog(promptMode(mime), parentWidget); |
68 | dialog->setAttribute(Qt::WA_DeleteOnClose); |
69 | dialog->setModal(true); |
70 | |
71 | connect(sender: dialog, signal: &QDialog::finished, context: this, slot: [this, dialog, mime](const int result) { |
72 | if (result == ExecutableFileOpenDialog::Rejected) { |
73 | Q_EMIT canceled(); |
74 | return; |
75 | } |
76 | |
77 | const bool isExecute = result == ExecutableFileOpenDialog::ExecuteFile; |
78 | Q_EMIT executeFile(enable: isExecute); |
79 | |
80 | if (dialog->isDontAskAgainChecked()) { |
81 | KConfigGroup cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc" )), QStringLiteral("Executable scripts" )); |
82 | cfgGroup.writeEntry(key: "behaviourOnLaunch" , value: isExecute ? "execute" : "open" ); |
83 | } |
84 | }); |
85 | |
86 | dialog->show(); |
87 | } |
88 | |
89 | void KIO::WidgetsOpenOrExecuteFileHandler::setWindow(QWidget *window) |
90 | { |
91 | m_parentWidget = window; |
92 | } |
93 | |
94 | #include "moc_widgetsopenorexecutefilehandler.cpp" |
95 | |