forked from qt-creator/qt-creator
DeviceUsedPortsGatherer: Simplify internals
Change-Id: I846022c67b8cc815f7fa271614b4baf7628bba3b Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -22,8 +22,6 @@ class DeviceUsedPortsGathererPrivate
|
|||||||
public:
|
public:
|
||||||
std::unique_ptr<QtcProcess> process;
|
std::unique_ptr<QtcProcess> process;
|
||||||
QList<Port> usedPorts;
|
QList<Port> usedPorts;
|
||||||
QByteArray remoteStdout;
|
|
||||||
QByteArray remoteStderr;
|
|
||||||
IDevice::ConstPtr device;
|
IDevice::ConstPtr device;
|
||||||
PortsGatheringMethod portsGatheringMethod;
|
PortsGatheringMethod portsGatheringMethod;
|
||||||
};
|
};
|
||||||
@@ -58,19 +56,11 @@ void DeviceUsedPortsGatherer::start(const IDevice::ConstPtr &device)
|
|||||||
|
|
||||||
connect(d->process.get(), &QtcProcess::done,
|
connect(d->process.get(), &QtcProcess::done,
|
||||||
this, &DeviceUsedPortsGatherer::handleProcessDone);
|
this, &DeviceUsedPortsGatherer::handleProcessDone);
|
||||||
connect(d->process.get(), &QtcProcess::readyReadStandardOutput,
|
|
||||||
this, [this] { d->remoteStdout += d->process->readAllStandardOutput(); });
|
|
||||||
connect(d->process.get(), &QtcProcess::readyReadStandardError,
|
|
||||||
this, [this] { d->remoteStderr += d->process->readAllStandardError(); });
|
|
||||||
|
|
||||||
|
|
||||||
d->process->start();
|
d->process->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeviceUsedPortsGatherer::stop()
|
void DeviceUsedPortsGatherer::stop()
|
||||||
{
|
{
|
||||||
d->remoteStdout.clear();
|
|
||||||
d->remoteStderr.clear();
|
|
||||||
if (d->process) {
|
if (d->process) {
|
||||||
d->process->disconnect();
|
d->process->disconnect();
|
||||||
d->process.release()->deleteLater();
|
d->process.release()->deleteLater();
|
||||||
@@ -85,7 +75,8 @@ QList<Port> DeviceUsedPortsGatherer::usedPorts() const
|
|||||||
void DeviceUsedPortsGatherer::setupUsedPorts()
|
void DeviceUsedPortsGatherer::setupUsedPorts()
|
||||||
{
|
{
|
||||||
d->usedPorts.clear();
|
d->usedPorts.clear();
|
||||||
const QList<Port> usedPorts = d->portsGatheringMethod.parsePorts(d->remoteStdout);
|
const QList<Port> usedPorts = d->portsGatheringMethod.parsePorts(
|
||||||
|
d->process->readAllStandardOutput());
|
||||||
for (const Port port : usedPorts) {
|
for (const Port port : usedPorts) {
|
||||||
if (d->device->freePorts().contains(port))
|
if (d->device->freePorts().contains(port))
|
||||||
d->usedPorts << port;
|
d->usedPorts << port;
|
||||||
@@ -95,32 +86,14 @@ void DeviceUsedPortsGatherer::setupUsedPorts()
|
|||||||
|
|
||||||
void DeviceUsedPortsGatherer::handleProcessDone()
|
void DeviceUsedPortsGatherer::handleProcessDone()
|
||||||
{
|
{
|
||||||
if (d->process->error() != QProcess::UnknownError) {
|
if (d->process->result() == ProcessResult::FinishedWithSuccess) {
|
||||||
emit error(tr("Connection error: %1").arg(d->process->errorString()));
|
setupUsedPorts();
|
||||||
stop();
|
} else {
|
||||||
return;
|
QString errMsg = d->process->errorString();
|
||||||
}
|
const QByteArray stdErr = d->process->readAllStandardError();
|
||||||
|
if (!stdErr.isEmpty()) {
|
||||||
QString errMsg;
|
|
||||||
switch (d->process->exitStatus()) {
|
|
||||||
case QProcess::CrashExit:
|
|
||||||
errMsg = tr("Remote process crashed: %1").arg(d->process->errorString());
|
|
||||||
break;
|
|
||||||
case QProcess::NormalExit:
|
|
||||||
if (d->process->exitCode() == 0)
|
|
||||||
setupUsedPorts();
|
|
||||||
else
|
|
||||||
errMsg = tr("Remote process failed; exit code was %1.").arg(d->process->exitCode());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid exit status");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!errMsg.isEmpty()) {
|
|
||||||
if (!d->remoteStderr.isEmpty()) {
|
|
||||||
errMsg += QLatin1Char('\n');
|
errMsg += QLatin1Char('\n');
|
||||||
errMsg += tr("Remote error output was: %1")
|
errMsg += tr("Remote error output was: %1").arg(QString::fromUtf8(stdErr));
|
||||||
.arg(QString::fromUtf8(d->remoteStderr));
|
|
||||||
}
|
}
|
||||||
emit error(errMsg);
|
emit error(errMsg);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user