File indexing completed on 2024-04-28 04:38:39

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andreas Pakulat <apaku@gmx.de>
0003     SPDX-FileCopyrightText: 2009 Niko Sams <niko.sams@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "plasmoidexecutionjob.h"
0009 #include "executeplasmoidplugin.h"
0010 
0011 #include <QFileInfo>
0012 #include <QDir>
0013 
0014 #include <KLocalizedString>
0015 #include <KConfigGroup>
0016 
0017 #include <interfaces/ilaunchconfiguration.h>
0018 #include <outputview/outputmodel.h>
0019 #include <outputview/outputdelegate.h>
0020 #include <util/commandexecutor.h>
0021 #include <util/path.h>
0022 
0023 #include <interfaces/iproject.h>
0024 #include <project/projectmodel.h>
0025 #include <QStandardPaths>
0026 
0027 using namespace KDevelop;
0028 
0029 PlasmoidExecutionJob::PlasmoidExecutionJob(ExecutePlasmoidPlugin* iface, ILaunchConfiguration* cfg)
0030     : OutputJob( iface )
0031 {
0032     QString identifier = cfg->config().readEntry("PlasmoidIdentifier", "");
0033 
0034     Q_ASSERT(!identifier.isEmpty());
0035     setToolTitle(i18n("Plasmoid Viewer"));
0036     setCapabilities(Killable);
0037     setStandardToolView( IOutputView::RunView );
0038     setBehaviours(IOutputView::AllowUserClose | IOutputView::AutoScroll );
0039     setObjectName(QLatin1String("plasmoidviewer ")+identifier);
0040     setDelegate(new OutputDelegate);
0041 
0042     m_process = new CommandExecutor(executable(cfg), this);
0043     m_process->setArguments( arguments(cfg) );
0044     m_process->setWorkingDirectory(workingDirectory(cfg));
0045 
0046     auto* model = new OutputModel(QUrl::fromLocalFile(m_process->workingDirectory()), this);
0047     model->setFilteringStrategy(OutputModel::CompilerFilter);
0048     setModel( model );
0049 
0050     connect(m_process, &CommandExecutor::receivedStandardError, model,
0051             &OutputModel::appendLines );
0052     connect(m_process, &CommandExecutor::receivedStandardOutput, model,
0053             &OutputModel::appendLines );
0054 
0055     connect( m_process, &CommandExecutor::failed, this, &PlasmoidExecutionJob::slotFailed );
0056     connect( m_process, &CommandExecutor::completed, this, &PlasmoidExecutionJob::slotCompleted );
0057 }
0058 
0059 
0060 void PlasmoidExecutionJob::start()
0061 {
0062     startOutput();
0063     model()->appendLine(m_process->workingDirectory() + QLatin1String("> ") + m_process->command() + QLatin1Char(' ') + m_process->arguments().join(QLatin1Char(' ')));
0064     m_process->start();
0065 }
0066 
0067 bool PlasmoidExecutionJob::doKill()
0068 {
0069     m_process->kill();
0070     model()->appendLine( i18n("** Killed **") );
0071     return true;
0072 }
0073 
0074 OutputModel* PlasmoidExecutionJob::model()
0075 {
0076     return qobject_cast<OutputModel*>( OutputJob::model() );
0077 }
0078 
0079 void PlasmoidExecutionJob::slotCompleted(int code)
0080 {
0081     if( code != 0 ) {
0082         setError( FailedShownError );
0083         model()->appendLine( i18n("*** Failed ***") );
0084     } else {
0085         model()->appendLine( i18n("*** Finished ***") );
0086     }
0087     emitResult();
0088 }
0089 
0090 void PlasmoidExecutionJob::slotFailed(QProcess::ProcessError error)
0091 {
0092     setError(error);
0093     // FIXME need more detail
0094     setErrorText(i18n("Plasmoid failed to execute on %1", m_process->workingDirectory()));
0095     model()->appendLine( i18n("*** Failed ***") );
0096     emitResult();
0097 }
0098 
0099 QString PlasmoidExecutionJob::executable(ILaunchConfiguration*)
0100 {
0101     return QStandardPaths::findExecutable(QStringLiteral("plasmoidviewer"));
0102 }
0103 
0104 QStringList PlasmoidExecutionJob::arguments(ILaunchConfiguration* cfg)
0105 {
0106     QStringList arguments = cfg->config().readEntry("Arguments", QStringList());
0107     if(workingDirectory(cfg) == QDir::tempPath()) {
0108         QString identifier = cfg->config().readEntry("PlasmoidIdentifier", "");
0109         arguments += QStringLiteral("-a");
0110         arguments += identifier;
0111     } else {
0112         arguments += { QStringLiteral("-a"), QStringLiteral(".") };
0113     }
0114     return arguments;
0115 }
0116 
0117 QString PlasmoidExecutionJob::workingDirectory(ILaunchConfiguration* cfg)
0118 {
0119     QString workingDirectory;
0120     IProject* p = cfg->project();
0121     if(p) {
0122         QString identifier = cfg->config().readEntry("PlasmoidIdentifier", "");
0123         QString possiblePath = Path(p->path(), identifier).toLocalFile();
0124         if(QFileInfo(possiblePath).isDir()) {
0125             workingDirectory = possiblePath;
0126         }
0127     }
0128 
0129     if(workingDirectory.isEmpty())
0130     {
0131         workingDirectory = QDir::tempPath();
0132     }
0133     return workingDirectory;
0134 }
0135 
0136 #include "moc_plasmoidexecutionjob.cpp"