File indexing completed on 2024-06-09 07:41:07

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2020 Ahmad Samir <a.samirh78@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "widgetsopenorexecutefilehandler.h"
0009 
0010 #include "executablefileopendialog_p.h"
0011 
0012 #include <KConfigGroup>
0013 #include <KJob>
0014 #include <KJobWidgets>
0015 #include <KSharedConfig>
0016 
0017 #include <QApplication>
0018 #include <QMimeDatabase>
0019 
0020 KIO::WidgetsOpenOrExecuteFileHandler::WidgetsOpenOrExecuteFileHandler(QObject *parent)
0021     : KIO::OpenOrExecuteFileInterface(parent)
0022 {
0023 }
0024 
0025 KIO::WidgetsOpenOrExecuteFileHandler::~WidgetsOpenOrExecuteFileHandler() = default;
0026 
0027 static ExecutableFileOpenDialog::Mode promptMode(const QMimeType &mime)
0028 {
0029     // Note that ExecutableFileOpenDialog::OpenAsExecute isn't useful here as
0030     // OpenUrlJob treats .exe (application/x-ms-dos-executable) files as executables
0031     // that are only opened using the default application associated with that MIME type
0032     // e.g. WINE
0033 
0034     if (mime.inherits(QStringLiteral("text/plain"))) {
0035         return ExecutableFileOpenDialog::OpenOrExecute;
0036     }
0037     return ExecutableFileOpenDialog::OnlyExecute;
0038 }
0039 
0040 void KIO::WidgetsOpenOrExecuteFileHandler::promptUserOpenOrExecute(KJob *job, const QString &mimetype)
0041 {
0042     KConfigGroup cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
0043     const QString value = cfgGroup.readEntry("behaviourOnLaunch", "alwaysAsk");
0044 
0045     if (value != QLatin1String("alwaysAsk")) {
0046         Q_EMIT executeFile(value == QLatin1String("execute"));
0047         return;
0048     }
0049 
0050     QWidget *parentWidget = nullptr;
0051 
0052     if (job) {
0053         parentWidget = KJobWidgets::window(job);
0054     }
0055 
0056     if (!parentWidget) {
0057         parentWidget = m_parentWidget;
0058     }
0059 
0060     if (!parentWidget) {
0061         parentWidget = qApp->activeWindow();
0062     }
0063 
0064     QMimeDatabase db;
0065     QMimeType mime = db.mimeTypeForName(mimetype);
0066 
0067     ExecutableFileOpenDialog *dialog = new ExecutableFileOpenDialog(promptMode(mime), parentWidget);
0068     dialog->setAttribute(Qt::WA_DeleteOnClose);
0069     dialog->setModal(true);
0070 
0071     connect(dialog, &QDialog::finished, this, [this, dialog, mime](const int result) {
0072         if (result == ExecutableFileOpenDialog::Rejected) {
0073             Q_EMIT canceled();
0074             return;
0075         }
0076 
0077         const bool isExecute = result == ExecutableFileOpenDialog::ExecuteFile;
0078         Q_EMIT executeFile(isExecute);
0079 
0080         if (dialog->isDontAskAgainChecked()) {
0081             KConfigGroup cfgGroup(KSharedConfig::openConfig(QStringLiteral("kiorc")), QStringLiteral("Executable scripts"));
0082             cfgGroup.writeEntry("behaviourOnLaunch", isExecute ? "execute" : "open");
0083         }
0084     });
0085 
0086     dialog->show();
0087 }
0088 
0089 void KIO::WidgetsOpenOrExecuteFileHandler::setWindow(QWidget *window)
0090 {
0091     m_parentWidget = window;
0092 }
0093 
0094 #include "moc_widgetsopenorexecutefilehandler.cpp"