| 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 | #include "openurljob.h" |
| 12 | |
| 13 | #include <KConfigGroup> |
| 14 | #include <KJob> |
| 15 | #include <KJobWidgets> |
| 16 | #include <KSharedConfig> |
| 17 | |
| 18 | #include <QApplication> |
| 19 | #include <QMimeDatabase> |
| 20 | |
| 21 | KIO::WidgetsOpenOrExecuteFileHandler::WidgetsOpenOrExecuteFileHandler(QObject *parent) |
| 22 | : KIO::OpenOrExecuteFileInterface(parent) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | KIO::WidgetsOpenOrExecuteFileHandler::~WidgetsOpenOrExecuteFileHandler() = default; |
| 27 | |
| 28 | static ExecutableFileOpenDialog::Mode promptMode(const QMimeType &mime) |
| 29 | { |
| 30 | // Note that ExecutableFileOpenDialog::OpenAsExecute isn't useful here as |
| 31 | // OpenUrlJob treats .exe (application/x-ms-dos-executable) files as executables |
| 32 | // that are only opened using the default application associated with that MIME type |
| 33 | // e.g. WINE |
| 34 | |
| 35 | if (mime.inherits(QStringLiteral("text/plain" ))) { |
| 36 | return ExecutableFileOpenDialog::OpenOrExecute; |
| 37 | } |
| 38 | return ExecutableFileOpenDialog::OnlyExecute; |
| 39 | } |
| 40 | |
| 41 | void KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute(KJob *job, const QString &mimetype) |
| 42 | { |
| 43 | KConfigGroup cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc" )), QStringLiteral("Executable scripts" )); |
| 44 | const QString value = cfgGroup.readEntry(key: "behaviourOnLaunch" , aDefault: "alwaysAsk" ); |
| 45 | |
| 46 | if (value != QLatin1String("alwaysAsk" )) { |
| 47 | Q_EMIT executeFile(enable: value == QLatin1String("execute" )); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | QWidget *parentWidget = nullptr; |
| 52 | |
| 53 | if (job) { |
| 54 | parentWidget = KJobWidgets::window(job); |
| 55 | } |
| 56 | |
| 57 | if (!parentWidget) { |
| 58 | parentWidget = m_parentWidget; |
| 59 | } |
| 60 | |
| 61 | if (!parentWidget) { |
| 62 | parentWidget = qApp->activeWindow(); |
| 63 | } |
| 64 | |
| 65 | QMimeDatabase db; |
| 66 | QMimeType mime = db.mimeTypeForName(nameOrAlias: mimetype); |
| 67 | |
| 68 | // TODO KF7 add url arg to promptUserOpenOrExecute. |
| 69 | QUrl url; |
| 70 | if (auto *openUrlJob = qobject_cast<KIO::OpenUrlJob *>(object: job)) { |
| 71 | url = openUrlJob->url(); |
| 72 | } |
| 73 | ExecutableFileOpenDialog *dialog = new ExecutableFileOpenDialog(url, mime, promptMode(mime), parentWidget); |
| 74 | dialog->setAttribute(Qt::WA_DeleteOnClose); |
| 75 | dialog->setModal(true); |
| 76 | |
| 77 | connect(sender: dialog, signal: &QDialog::finished, context: this, slot: [this, dialog, mime](const int result) { |
| 78 | if (result == ExecutableFileOpenDialog::Rejected) { |
| 79 | Q_EMIT canceled(); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | const bool isExecute = result == ExecutableFileOpenDialog::ExecuteFile; |
| 84 | Q_EMIT executeFile(enable: isExecute); |
| 85 | |
| 86 | if (dialog->isDontAskAgainChecked()) { |
| 87 | KConfigGroup cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc" )), QStringLiteral("Executable scripts" )); |
| 88 | cfgGroup.writeEntry(key: "behaviourOnLaunch" , value: isExecute ? "execute" : "open" ); |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | dialog->show(); |
| 93 | } |
| 94 | |
| 95 | void KIO::WidgetsOpenOrExecuteFileHandler::setWindow(QWidget *window) |
| 96 | { |
| 97 | m_parentWidget = window; |
| 98 | } |
| 99 | |
| 100 | #include "moc_widgetsopenorexecutefilehandler.cpp" |
| 101 | |