ProjectExplorer: Dissolve ApplicationLauncher

Make its members part of SimpleTargetRunnerPrivate.

Change-Id: Ib6f700f7b849db761a1bd0b5ea401bbe010c010f
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2022-05-23 12:51:11 +02:00
parent b9e53428b5
commit bf4b94e4b8

View File

@@ -1196,19 +1196,17 @@ void RunControlPrivate::debugMessage(const QString &msg)
} }
// ApplicationLauncher // SimpleTargetRunnerPrivate
class ApplicationLauncher : public QObject namespace Internal {
class SimpleTargetRunnerPrivate : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
ApplicationLauncher(); SimpleTargetRunnerPrivate();
~ApplicationLauncher() override; ~SimpleTargetRunnerPrivate() override;
void setUseTerminal(bool on);
void setRunAsRoot(bool on);
void setRunnable(const Runnable &runnable);
void start(); void start();
void stop(); void stop();
@@ -1245,33 +1243,40 @@ public:
ProcessResultData m_resultData; ProcessResultData m_resultData;
std::function<void()> m_starter;
bool m_stopReported = false;
bool m_stopForced = false;
signals: signals:
void appendMessage(const QString &message, Utils::OutputFormat format, bool appendNewLine = true); void appendMessage(const QString &message, Utils::OutputFormat format, bool appendNewLine = true);
void started(); void started();
void done(); void done();
}; };
} // Internal
static QProcess::ProcessChannelMode defaultProcessChannelMode() static QProcess::ProcessChannelMode defaultProcessChannelMode()
{ {
return ProjectExplorerPlugin::appOutputSettings().mergeChannels return ProjectExplorerPlugin::appOutputSettings().mergeChannels
? QProcess::MergedChannels : QProcess::SeparateChannels; ? QProcess::MergedChannels : QProcess::SeparateChannels;
} }
ApplicationLauncher::ApplicationLauncher() SimpleTargetRunnerPrivate::SimpleTargetRunnerPrivate()
{ {
m_process.setProcessChannelMode(defaultProcessChannelMode()); m_process.setProcessChannelMode(defaultProcessChannelMode());
connect(&m_process, &QtcProcess::started, this, &ApplicationLauncher::started); connect(&m_process, &QtcProcess::started, this, &SimpleTargetRunnerPrivate::started);
connect(&m_process, &QtcProcess::done, this, &ApplicationLauncher::handleDone); connect(&m_process, &QtcProcess::done, this, &SimpleTargetRunnerPrivate::handleDone);
connect(&m_process, &QtcProcess::readyReadStandardError, connect(&m_process, &QtcProcess::readyReadStandardError,
this, &ApplicationLauncher::handleStandardError); this, &SimpleTargetRunnerPrivate::handleStandardError);
connect(&m_process, &QtcProcess::readyReadStandardOutput, connect(&m_process, &QtcProcess::readyReadStandardOutput,
this, &ApplicationLauncher::handleStandardOutput); this, &SimpleTargetRunnerPrivate::handleStandardOutput);
if (WinDebugInterface::instance()) { if (WinDebugInterface::instance()) {
connect(WinDebugInterface::instance(), &WinDebugInterface::cannotRetrieveDebugOutput, connect(WinDebugInterface::instance(), &WinDebugInterface::cannotRetrieveDebugOutput,
this, [this] { this, [this] {
disconnect(WinDebugInterface::instance(), nullptr, this, nullptr); disconnect(WinDebugInterface::instance(), nullptr, this, nullptr);
emit appendMessage(ApplicationLauncher::tr("Cannot retrieve debugging output.") emit appendMessage(tr("Cannot retrieve debugging output.")
+ QLatin1Char('\n'), ErrorMessageFormat); + QLatin1Char('\n'), ErrorMessageFormat);
}); });
@@ -1283,28 +1288,13 @@ ApplicationLauncher::ApplicationLauncher()
} }
} }
ApplicationLauncher::~ApplicationLauncher() SimpleTargetRunnerPrivate::~SimpleTargetRunnerPrivate()
{ {
if (m_state == Run) if (m_state == Run)
emit done(); emit done();
} }
void ApplicationLauncher::setUseTerminal(bool on) void SimpleTargetRunnerPrivate::stop()
{
m_process.setTerminalMode(on ? Utils::TerminalMode::On : Utils::TerminalMode::Off);
}
void ApplicationLauncher::setRunAsRoot(bool on)
{
m_runAsRoot = on;
}
void ApplicationLauncher::setRunnable(const Runnable &runnable)
{
m_runnable = runnable;
}
void ApplicationLauncher::stop()
{ {
m_resultData.m_exitStatus = QProcess::CrashExit; m_resultData.m_exitStatus = QProcess::CrashExit;
if (m_isLocal) { if (m_isLocal) {
@@ -1316,8 +1306,7 @@ void ApplicationLauncher::stop()
if (m_stopRequested) if (m_stopRequested)
return; return;
m_stopRequested = true; m_stopRequested = true;
emit appendMessage(ApplicationLauncher::tr("User requested stop. Shutting down..."), emit appendMessage(tr("User requested stop. Shutting down..."), NormalMessageFormat);
NormalMessageFormat);
switch (m_state) { switch (m_state) {
case Run: case Run:
m_process.terminate(); m_process.terminate();
@@ -1328,17 +1317,12 @@ void ApplicationLauncher::stop()
} }
} }
bool ApplicationLauncher::isRunning() const bool SimpleTargetRunnerPrivate::isRunning() const
{ {
return m_process.state() != QProcess::NotRunning; return m_process.state() != QProcess::NotRunning;
} }
ProcessHandle ApplicationLauncher::applicationPID() const qint64 SimpleTargetRunnerPrivate::privateApplicationPID() const
{
return ProcessHandle(privateApplicationPID());
}
qint64 ApplicationLauncher::privateApplicationPID() const
{ {
if (!isRunning()) if (!isRunning())
return 0; return 0;
@@ -1346,7 +1330,7 @@ qint64 ApplicationLauncher::privateApplicationPID() const
return m_process.processId(); return m_process.processId();
} }
void ApplicationLauncher::handleDone() void SimpleTargetRunnerPrivate::handleDone()
{ {
m_resultData = m_process.resultData(); m_resultData = m_process.resultData();
@@ -1366,13 +1350,13 @@ void ApplicationLauncher::handleDone()
QString errorString; QString errorString;
switch (m_resultData.m_error) { switch (m_resultData.m_error) {
case QProcess::FailedToStart: case QProcess::FailedToStart:
errorString = ApplicationLauncher::tr("Failed to start program. Path or permissions wrong?"); errorString = tr("Failed to start program. Path or permissions wrong?");
break; break;
case QProcess::Crashed: case QProcess::Crashed:
m_resultData.m_exitStatus = QProcess::CrashExit; m_resultData.m_exitStatus = QProcess::CrashExit;
break; break;
default: default:
errorString = ApplicationLauncher::tr("Some error has occurred while running the program."); errorString = tr("Some error has occurred while running the program.");
} }
if (!errorString.isEmpty()) if (!errorString.isEmpty())
emit appendMessage(errorString, ErrorMessageFormat); emit appendMessage(errorString, ErrorMessageFormat);
@@ -1394,7 +1378,7 @@ void ApplicationLauncher::handleDone()
emit done(); emit done();
} }
void ApplicationLauncher::handleStandardOutput() void SimpleTargetRunnerPrivate::handleStandardOutput()
{ {
const QByteArray data = m_process.readAllStandardOutput(); const QByteArray data = m_process.readAllStandardOutput();
const QString msg = m_outputCodec->toUnicode( const QString msg = m_outputCodec->toUnicode(
@@ -1402,7 +1386,7 @@ void ApplicationLauncher::handleStandardOutput()
emit appendMessage(msg, StdOutFormat, false); emit appendMessage(msg, StdOutFormat, false);
} }
void ApplicationLauncher::handleStandardError() void SimpleTargetRunnerPrivate::handleStandardError()
{ {
const QByteArray data = m_process.readAllStandardError(); const QByteArray data = m_process.readAllStandardError();
const QString msg = m_outputCodec->toUnicode( const QString msg = m_outputCodec->toUnicode(
@@ -1410,7 +1394,7 @@ void ApplicationLauncher::handleStandardError()
emit appendMessage(msg, StdErrFormat, false); emit appendMessage(msg, StdErrFormat, false);
} }
void ApplicationLauncher::start() void SimpleTargetRunnerPrivate::start()
{ {
m_isLocal = m_runnable.device.isNull() || m_runnable.device.dynamicCast<const DesktopDevice>(); m_isLocal = m_runnable.device.isNull() || m_runnable.device.dynamicCast<const DesktopDevice>();
@@ -1445,7 +1429,7 @@ void ApplicationLauncher::start()
QTC_ASSERT(m_state == Inactive, return); QTC_ASSERT(m_state == Inactive, return);
if (!m_runnable.device) { if (!m_runnable.device) {
m_resultData.m_errorString = ApplicationLauncher::tr("Cannot run: No device."); m_resultData.m_errorString = tr("Cannot run: No device.");
m_resultData.m_error = QProcess::FailedToStart; m_resultData.m_error = QProcess::FailedToStart;
m_resultData.m_exitStatus = QProcess::CrashExit; m_resultData.m_exitStatus = QProcess::CrashExit;
emit done(); emit done();
@@ -1453,7 +1437,7 @@ void ApplicationLauncher::start()
} }
if (!m_runnable.device->isEmptyCommandAllowed() && m_runnable.command.isEmpty()) { if (!m_runnable.device->isEmptyCommandAllowed() && m_runnable.command.isEmpty()) {
m_resultData.m_errorString = ApplicationLauncher::tr("Cannot run: No command given."); m_resultData.m_errorString = tr("Cannot run: No command given.");
m_resultData.m_error = QProcess::FailedToStart; m_resultData.m_error = QProcess::FailedToStart;
m_resultData.m_exitStatus = QProcess::CrashExit; m_resultData.m_exitStatus = QProcess::CrashExit;
emit done(); emit done();
@@ -1490,20 +1474,6 @@ void ApplicationLauncher::start()
\sa Utils::QtcProcess \sa Utils::QtcProcess
*/ */
namespace Internal {
class SimpleTargetRunnerPrivate
{
public:
ApplicationLauncher m_launcher;
std::function<void()> m_starter;
bool m_stopReported = false;
bool m_stopForced = false;
};
} // Internal
SimpleTargetRunner::SimpleTargetRunner(RunControl *runControl) SimpleTargetRunner::SimpleTargetRunner(RunControl *runControl)
: RunWorker(runControl), d(new Internal::SimpleTargetRunnerPrivate) : RunWorker(runControl), d(new Internal::SimpleTargetRunnerPrivate)
{ {
@@ -1536,18 +1506,18 @@ void SimpleTargetRunner::doStart(const Runnable &runnable)
d->m_stopForced = false; d->m_stopForced = false;
d->m_stopReported = false; d->m_stopReported = false;
d->m_launcher.disconnect(this); d->disconnect(this);
d->m_launcher.setUseTerminal(useTerminal); d->m_process.setTerminalMode(useTerminal ? Utils::TerminalMode::On : Utils::TerminalMode::Off);
d->m_launcher.setRunAsRoot(runAsRoot); d->m_runAsRoot = runAsRoot;
const QString msg = RunControl::tr("Starting %1...").arg(runnable.command.toUserOutput()); const QString msg = RunControl::tr("Starting %1...").arg(runnable.command.toUserOutput());
appendMessage(msg, Utils::NormalMessageFormat); appendMessage(msg, Utils::NormalMessageFormat);
connect(&d->m_launcher, &ApplicationLauncher::done, this, [this, runnable] { connect(d.get(), &SimpleTargetRunnerPrivate::done, this, [this, runnable] {
if (d->m_stopReported) if (d->m_stopReported)
return; return;
const QString executable = runnable.command.executable().toUserOutput(); const QString executable = runnable.command.executable().toUserOutput();
const ProcessResultData &resultData = d->m_launcher.m_resultData; const ProcessResultData &resultData = d->m_resultData;
QString msg = tr("%2 exited with code %1").arg(resultData.m_exitCode).arg(executable); QString msg = tr("%2 exited with code %1").arg(resultData.m_exitCode).arg(executable);
if (resultData.m_exitStatus == QProcess::CrashExit) if (resultData.m_exitStatus == QProcess::CrashExit)
msg = tr("%1 crashed.").arg(executable); msg = tr("%1 crashed.").arg(executable);
@@ -1560,14 +1530,14 @@ void SimpleTargetRunner::doStart(const Runnable &runnable)
reportStopped(); reportStopped();
}); });
connect(&d->m_launcher, &ApplicationLauncher::appendMessage, this, &RunWorker::appendMessage); connect(d.get(), &SimpleTargetRunnerPrivate::appendMessage, this, &RunWorker::appendMessage);
const bool isDesktop = runnable.device.isNull() const bool isDesktop = runnable.device.isNull()
|| runnable.device.dynamicCast<const DesktopDevice>(); || runnable.device.dynamicCast<const DesktopDevice>();
if (isDesktop) { if (isDesktop) {
connect(&d->m_launcher, &ApplicationLauncher::started, this, [this] { connect(d.get(), &SimpleTargetRunnerPrivate::started, this, [this] {
// Console processes only know their pid after being started // Console processes only know their pid after being started
ProcessHandle pid = d->m_launcher.applicationPID(); ProcessHandle pid{d->privateApplicationPID()};
runControl()->setApplicationProcessHandle(pid); runControl()->setApplicationProcessHandle(pid);
pid.activate(); pid.activate();
reportStarted(); reportStarted();
@@ -1578,16 +1548,16 @@ void SimpleTargetRunner::doStart(const Runnable &runnable)
return; return;
} }
} else { } else {
connect(&d->m_launcher, &ApplicationLauncher::started, this, &RunWorker::reportStarted); connect(d.get(), &SimpleTargetRunnerPrivate::started, this, &RunWorker::reportStarted);
} }
d->m_launcher.setRunnable(runnable); d->m_runnable = runnable;
d->m_launcher.start(); d->start();
} }
void SimpleTargetRunner::stop() void SimpleTargetRunner::stop()
{ {
d->m_stopForced = true; d->m_stopForced = true;
d->m_launcher.stop(); d->stop();
} }
void SimpleTargetRunner::setStarter(const std::function<void ()> &starter) void SimpleTargetRunner::setStarter(const std::function<void ()> &starter)