1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
SPDX-FileCopyrightText: 2009 Andreas Pakulat <apaku@gmx.de>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "nativeappjob.h"
#include <QAbstractButton>
#include <QFileInfo>
#include <QMessageBox>
#include <QPointer>
#include <QCheckBox>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KShell>
#include <KSharedConfig>
#include <interfaces/ilaunchconfiguration.h>
#include <interfaces/iruncontroller.h>
#include <outputview/outputmodel.h>
#include <util/environmentprofilelist.h>
#include <interfaces/icore.h>
#include <interfaces/iplugincontroller.h>
#include <project/projectmodel.h>
#include "executeplugin.h"
#include "debug.h"
using namespace KDevelop;
NativeAppJob::NativeAppJob(QObject* parent, KDevelop::ILaunchConfiguration* cfg)
: KDevelop::OutputExecuteJob( parent )
, m_name(cfg->name())
{
{
auto cfgGroup = cfg->config();
if (cfgGroup.readEntry(ExecutePlugin::isExecutableEntry, false)) {
m_name = cfgGroup.readEntry(ExecutePlugin::executableEntry, cfg->name()).section(QLatin1Char('/'), -1);
}
if (!cfgGroup.readEntry<bool>(ExecutePlugin::configuredByCTest, false)) {
m_killBeforeExecutingAgain = cfgGroup.readEntry<int>(ExecutePlugin::killBeforeExecutingAgain, askIfRunning);
}
}
setCapabilities(Killable);
auto* iface = KDevelop::ICore::self()->pluginController()->pluginForExtension(QStringLiteral("org.kdevelop.IExecutePlugin"), QStringLiteral("kdevexecute"))->extension<IExecutePlugin>();
Q_ASSERT(iface);
const KDevelop::EnvironmentProfileList environmentProfiles(KSharedConfig::openConfig());
QString envProfileName = iface->environmentProfileName(cfg);
QString err;
QUrl executable = iface->executable( cfg, err );
if( !err.isEmpty() )
{
setError( -1 );
setErrorText( err );
return;
}
if (envProfileName.isEmpty()) {
qCWarning(PLUGIN_EXECUTE) << "Launch Configuration:" << cfg->name() << i18n("No environment profile specified, looks like a broken "
"configuration, please check run configuration '%1'. "
"Using default environment profile.", cfg->name() );
envProfileName = environmentProfiles.defaultProfileName();
}
setEnvironmentProfile(envProfileName);
QStringList arguments = iface->arguments( cfg, err );
if( !err.isEmpty() )
{
setError( -2 );
setErrorText( err );
}
if( error() != 0 )
{
qCWarning(PLUGIN_EXECUTE) << "Launch Configuration:" << cfg->name() << "oops, problem" << errorText();
return;
}
setStandardToolView(KDevelop::IOutputView::RunView);
setBehaviours(KDevelop::IOutputView::AllowUserClose | KDevelop::IOutputView::AutoScroll);
setFilteringStrategy(OutputModel::NativeAppErrorFilter);
setProperties(DisplayStdout | DisplayStderr);
// Now setup the process parameters
QUrl wc = iface->workingDirectory( cfg );
if( !wc.isValid() || wc.isEmpty() ) {
wc = QUrl::fromLocalFile( QFileInfo( executable.toLocalFile() ).absolutePath() );
}
setWorkingDirectory( wc );
qCDebug(PLUGIN_EXECUTE) << "setting app:" << executable << arguments;
if (iface->useTerminal(cfg)) {
QString terminalCommand = iface->terminal(cfg);
terminalCommand.replace(QLatin1String("%exe"), KShell::quoteArg( executable.toLocalFile()) );
terminalCommand.replace(QLatin1String("%workdir"), KShell::quoteArg( wc.toLocalFile()) );
QStringList args = KShell::splitArgs(terminalCommand);
args.append( arguments );
*this << args;
} else {
*this << executable.toLocalFile();
*this << arguments;
}
setJobName(m_name);
}
NativeAppJob* findNativeJob(KJob* j)
{
auto* job = qobject_cast<NativeAppJob*>(j);
if (!job) {
const QList<NativeAppJob*> jobs = j->findChildren<NativeAppJob*>();
if (!jobs.isEmpty())
job = jobs.first();
}
return job;
}
void NativeAppJob::start()
{
QVector<QPointer<NativeAppJob> > currentJobs;
// collect running instances of the same type
const auto& allCurrentJobs = ICore::self()->runController()->currentJobs();
for (auto j : allCurrentJobs) {
NativeAppJob* njob = findNativeJob(j);
if (njob && njob != this && njob->m_name == m_name)
currentJobs << njob;
}
if (!currentJobs.isEmpty()) {
int oldJobAction = m_killBeforeExecutingAgain;
if (oldJobAction == askIfRunning) {
QMessageBox msgBox(QMessageBox::Question,
i18nc("@title:window", "Job Already Running"),
i18n("'%1' is already being executed.", m_name),
startAnother | killAllInstances | QMessageBox::Cancel /* aka askIfRunning */);
msgBox.button(killAllInstances)->setText(i18nc("@action:button", "Kill All Instances"));
msgBox.button(startAnother)->setText(i18nc("@action:button", "Start Another"));
msgBox.setDefaultButton(QMessageBox::Cancel);
QCheckBox* remember = new QCheckBox(i18nc("@option:check", "Remember choice"));
msgBox.setCheckBox(remember);
oldJobAction = msgBox.exec();
if (remember->isChecked() && oldJobAction != QMessageBox::Cancel) {
Q_EMIT killBeforeExecutingAgainChanged(oldJobAction);
}
}
switch (oldJobAction) {
case startAnother:
break;
case killAllInstances:
for (auto & job : currentJobs) {
if (job)
job->kill();
}
break;
default: // cancel starting a new job
kill();
return;
}
}
OutputExecuteJob::start();
}
#include "moc_nativeappjob.cpp"
|