File indexing completed on 2024-07-14 14:34:09

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