ConsoleProcess: limit code repetition

Change-Id: I3b07e61105b3c2a37658df438ef39bb65968aeea
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2022-01-18 11:53:31 +01:00
parent 7f4342b687
commit 31807ab917
2 changed files with 20 additions and 26 deletions

View File

@@ -411,10 +411,7 @@ bool ConsoleProcess::start()
if (!env.isEmpty()) { if (!env.isEmpty()) {
d->m_tempFile = new QTemporaryFile(); d->m_tempFile = new QTemporaryFile();
if (!d->m_tempFile->open()) { if (!d->m_tempFile->open()) {
stubServerShutdown(); cleanupAfterStartFailure(msgCannotCreateTempFile(d->m_tempFile->errorString()));
emitError(QProcess::FailedToStart, msgCannotCreateTempFile(d->m_tempFile->errorString()));
delete d->m_tempFile;
d->m_tempFile = nullptr;
return false; return false;
} }
QString outString; QString outString;
@@ -444,10 +441,7 @@ bool ConsoleProcess::start()
QTC_CHECK(textCodec); QTC_CHECK(textCodec);
const QByteArray outBytes = textCodec ? textCodec->fromUnicode(outString) : QByteArray(); const QByteArray outBytes = textCodec ? textCodec->fromUnicode(outString) : QByteArray();
if (!textCodec || d->m_tempFile->write(outBytes) < 0) { if (!textCodec || d->m_tempFile->write(outBytes) < 0) {
stubServerShutdown(); cleanupAfterStartFailure(msgCannotWriteTempFile());
emitError(QProcess::FailedToStart, msgCannotWriteTempFile());
delete d->m_tempFile;
d->m_tempFile = nullptr;
return false; return false;
} }
d->m_tempFile->flush(); d->m_tempFile->flush();
@@ -483,10 +477,9 @@ bool ConsoleProcess::start()
if (!success) { if (!success) {
delete d->m_pid; delete d->m_pid;
d->m_pid = nullptr; d->m_pid = nullptr;
delete d->m_tempFile; const QString msg = tr("The process \"%1\" could not be started: %2")
d->m_tempFile = nullptr; .arg(cmdLine, winErrorMessage(GetLastError()));
stubServerShutdown(); cleanupAfterStartFailure(msg);
emitError(QProcess::FailedToStart, tr("The process \"%1\" could not be started: %2").arg(cmdLine, winErrorMessage(GetLastError())));
return false; return false;
} }
@@ -550,22 +543,16 @@ bool ConsoleProcess::start()
if (!env.isEmpty()) { if (!env.isEmpty()) {
d->m_tempFile = new QTemporaryFile(); d->m_tempFile = new QTemporaryFile();
if (!d->m_tempFile->open()) { if (!d->m_tempFile->open()) {
stubServerShutdown(); cleanupAfterStartFailure(msgCannotCreateTempFile(d->m_tempFile->errorString()));
emitError(QProcess::FailedToStart, msgCannotCreateTempFile(d->m_tempFile->errorString()));
delete d->m_tempFile;
d->m_tempFile = nullptr;
return false; return false;
} }
QByteArray contents; QByteArray contents;
for (const QString &var : env) { for (const QString &var : env) {
QByteArray l8b = var.toLocal8Bit(); const QByteArray l8b = var.toLocal8Bit();
contents.append(l8b.constData(), l8b.size() + 1); contents.append(l8b.constData(), l8b.size() + 1);
} }
if (d->m_tempFile->write(contents) != contents.size() || !d->m_tempFile->flush()) { if (d->m_tempFile->write(contents) != contents.size() || !d->m_tempFile->flush()) {
stubServerShutdown(); cleanupAfterStartFailure(msgCannotWriteTempFile());
emitError(QProcess::FailedToStart, msgCannotWriteTempFile());
delete d->m_tempFile;
d->m_tempFile = nullptr;
return false; return false;
} }
} }
@@ -594,11 +581,9 @@ bool ConsoleProcess::start()
d->m_process.setCommand({FilePath::fromString(terminal.command), allArgs}); d->m_process.setCommand({FilePath::fromString(terminal.command), allArgs});
d->m_process.start(); d->m_process.start();
if (!d->m_process.waitForStarted()) { if (!d->m_process.waitForStarted()) {
stubServerShutdown(); const QString msg = tr("Cannot start the terminal emulator \"%1\", change the setting in the "
emitError(QProcess::UnknownError, tr("Cannot start the terminal emulator \"%1\", change the setting in the " "Environment options.").arg(terminal.command);
"Environment options.").arg(terminal.command)); cleanupAfterStartFailure(msg);
delete d->m_tempFile;
d->m_tempFile = nullptr;
return false; return false;
} }
d->m_stubConnectTimer = new QTimer(this); d->m_stubConnectTimer = new QTimer(this);
@@ -611,6 +596,14 @@ bool ConsoleProcess::start()
return true; return true;
} }
void Utils::ConsoleProcess::cleanupAfterStartFailure(const QString &errorMessage)
{
stubServerShutdown();
emitError(QProcess::FailedToStart, errorMessage);
delete d->m_tempFile;
d->m_tempFile = nullptr;
}
void Utils::ConsoleProcess::kickoffProcess() void Utils::ConsoleProcess::kickoffProcess()
{ {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN

View File

@@ -128,6 +128,7 @@ private:
void stubConnectionAvailable(); void stubConnectionAvailable();
void readStubOutput(); void readStubOutput();
void stubExited(); void stubExited();
void cleanupAfterStartFailure(const QString &errorMessage);
static QString modeOption(Mode m); static QString modeOption(Mode m);
static QString msgCommChannelFailed(const QString &error); static QString msgCommChannelFailed(const QString &error);