File indexing completed on 2025-01-05 03:53:10

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2015-06-07
0007  * Description : a tool to create panorama by fusion of several images.
0008  *
0009  * SPDX-FileCopyrightText: 2015-2016 by Benjamin Girault <benjamin dot girault at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "commandtask.h"
0016 
0017 // Qt includes
0018 
0019 #include <QThread>
0020 
0021 // KDE includes
0022 
0023 #include <klocalizedstring.h>
0024 
0025 // Local includes
0026 
0027 #include "digikam_debug.h"
0028 #include "digikam_globals.h"
0029 
0030 namespace DigikamGenericPanoramaPlugin
0031 {
0032 
0033 CommandTask::CommandTask(PanoAction action, const QString& workDirPath, const QString& commandPath)
0034     : PanoTask      (action, workDirPath),
0035       process       (nullptr),
0036       commandPath   (commandPath)
0037 {
0038 }
0039 
0040 void CommandTask::requestAbort()
0041 {
0042     PanoTask::requestAbort();
0043 
0044     if (!process.isNull())
0045     {
0046         process->kill();
0047     }
0048 }
0049 
0050 void CommandTask::runProcess(QStringList& args)
0051 {
0052     if (isAbortedFlag)
0053     {
0054         return;
0055     }
0056 
0057     process.reset(new QProcess());
0058     process->setWorkingDirectory(tmpDir.toLocalFile());
0059     process->setProcessChannelMode(QProcess::MergedChannels);
0060 
0061     QProcessEnvironment env = Digikam::adjustedEnvironmentForAppImage();
0062     env.insert(QLatin1String("OMP_NUM_THREADS"),
0063                QString::number(QThread::idealThreadCount()));
0064     process->setProcessEnvironment(env);
0065 
0066     process->setProgram(commandPath);
0067     process->setArguments(args);
0068     process->start();
0069 
0070     successFlag = process->waitForFinished(-1) && (process->exitStatus() == QProcess::NormalExit);
0071     output      = QString::fromLocal8Bit(process->readAll());
0072 
0073     if (!successFlag)
0074     {
0075         errString = getProcessError();
0076     }
0077 }
0078 
0079 QString CommandTask::getProgram()
0080 {
0081     if (process.isNull())
0082     {
0083         return QString();
0084     }
0085 
0086     return process->program();
0087 }
0088 
0089 QString CommandTask::getCommandLine()
0090 {
0091     if (process.isNull())
0092     {
0093         return QString();
0094     }
0095 
0096     return (process->program() + QLatin1Char(' ') + process->arguments().join(QLatin1Char(' ')));
0097 }
0098 
0099 QString CommandTask::getProcessError()
0100 {
0101     if (isAbortedFlag)
0102     {
0103         return i18n("<b>Canceled</b>");
0104     }
0105 
0106     if (process.isNull())
0107     {
0108         return QString();
0109     }
0110 
0111     return (
0112             i18n("<b>Cannot run <i>%1</i>:</b><p>%2</p>",
0113                  getProgram(),
0114                  output.toHtmlEscaped().replace(QLatin1Char('\n'), QLatin1String("<br />")))
0115            );
0116 }
0117 
0118 void CommandTask::printDebug(const QString& binaryName)
0119 {
0120     qCDebug(DIGIKAM_DPLUGIN_GENERIC_LOG) << binaryName << "command line: " << getCommandLine();
0121     qCDebug(DIGIKAM_DPLUGIN_GENERIC_LOG) << binaryName << "output:" << QT_ENDL
0122                                          << qPrintable(QLatin1String(" >>\t") +
0123                                             output.replace(QLatin1Char('\n'), QLatin1String("\n >>\t")));
0124 }
0125 
0126 } // namespace DigikamGenericPanoramaPlugin