File indexing completed on 2024-05-26 05:37:12

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 "ZypperRPMJob.h"
0008 #include <KLocalizedString>
0009 #include <KShell>
0010 #include <QDir>
0011 #include <QFileInfo>
0012 #include <QProcess>
0013 #include <QRegularExpression>
0014 
0015 void ZypperRPMJob::executeOperation(const QFileInfo &fileInfo, const QString & /*mimeType*/, bool install)
0016 {
0017     if (install) {
0018         const QString command = QStringLiteral("sudo zypper install %1").arg(KShell::quoteArg(fileInfo.absoluteFilePath()));
0019         const QString bashCommand = QStringLiteral("echo %1;%1").arg(command, KShell::quoteArg(terminalCloseMessage(install)));
0020         runScriptInTerminal(QStringLiteral("sh -c %1").arg(KShell::quoteArg(bashCommand)), QDir::homePath());
0021     } else {
0022         QProcess rpmInfoProcess;
0023         rpmInfoProcess.start(QStringLiteral("rpm"), {"-qi", fileInfo.absoluteFilePath()});
0024         rpmInfoProcess.waitForFinished();
0025         const QString rpmInfo = rpmInfoProcess.readAll();
0026         const auto infoMatch = QRegularExpression(QStringLiteral("Name *: (.+)")).match(rpmInfo);
0027         if (!infoMatch.hasMatch()) {
0028             Q_EMIT error(i18nc("@info", "Could not resolve package name of %1", fileInfo.baseName()));
0029         }
0030         const QString rpmPackageName = KShell::quoteArg(infoMatch.captured(1));
0031         QProcess *process = new QProcess(this);
0032         process->start(QStringLiteral("pkexec"), {"zypper", "remove", "--no-confirm", rpmPackageName});
0033 
0034         auto reportProcessError = [this, process]() {
0035             Q_EMIT error(xi18nc("@info", "Failed to run install command: <message>%1</message>", process->errorString()));
0036         };
0037 
0038         connect(process,
0039                 qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
0040                 this,
0041                 [this, reportProcessError](int exitCode, QProcess::ExitStatus exitStatus) {
0042                     if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
0043                         Q_EMIT finished();
0044                     } else {
0045                         reportProcessError();
0046                     }
0047                 });
0048         connect(process, &QProcess::errorOccurred, this, reportProcessError);
0049     }
0050 }