File indexing completed on 2024-06-16 05:08:59

0001 /*
0002     SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ScriptJob.h"
0008 
0009 #include "ScriptConfirmationDialog.h"
0010 #include <KLocalizedString>
0011 #include <KShell>
0012 #include <QDebug>
0013 #include <QDir>
0014 #include <QFileInfoList>
0015 
0016 void ScriptJob::executeOperation(const QFileInfo &fileInfo, const QString & /*mimeType*/, bool install)
0017 {
0018     QString installerPath;
0019     const QFileInfoList archiveEntries = fileInfo.absoluteDir().entryInfoList(QDir::Files, QDir::Name);
0020     const QString scriptPrefix = install ? "install" : "uninstall";
0021     for (const auto &file : archiveEntries) {
0022         if (file.baseName() == scriptPrefix) {
0023             installerPath = file.absoluteFilePath();
0024             // If the name is exactly install/uninstall we immediately take it
0025             break;
0026         } else if (file.baseName().startsWith(scriptPrefix)) {
0027             installerPath = file.absoluteFilePath();
0028         }
0029     }
0030     // We want the user to be exactly aware of what's going on
0031     if (install || installerPath.isEmpty()) {
0032         ScriptConfirmationDialog dlg(installerPath, install, fileInfo.absolutePath());
0033         if (dlg.exec() == QDialog::Accepted) {
0034             if (installerPath.isEmpty()) {
0035                 Q_EMIT finished(); // The "Mark entry as installed" button
0036             } else {
0037                 runScriptInTerminal(formatScriptCommand(install, installerPath), fileInfo.absolutePath());
0038             }
0039         } else {
0040             Q_EMIT error(QString());
0041         }
0042     } else {
0043         runScriptInTerminal(formatScriptCommand(install, installerPath), fileInfo.absolutePath());
0044     }
0045 }
0046 
0047 QString ScriptJob::formatScriptCommand(bool install, const QString &installerPath)
0048 {
0049     const QString bashCommand =
0050         QStringLiteral("echo %1;%1 || $SHELL && echo %2").arg(KShell::quoteArg(installerPath), KShell::quoteArg(terminalCloseMessage(install)));
0051     return QStringLiteral("sh -c %1").arg(KShell::quoteArg(bashCommand));
0052 }