DeviceUsedPortsGatherer: Introduce done() signal

And get rid of error() and portListReady() signals.
Inline some internals to have a better overview on what's happening.

Change-Id: Ie8f0afc97b3315600a7fb2076208ba057aa041fa
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Jarek Kobus
2024-02-03 22:25:45 +01:00
parent f2dbd28165
commit 554f68b573
2 changed files with 37 additions and 46 deletions

View File

@@ -44,6 +44,11 @@ DeviceUsedPortsGatherer::~DeviceUsedPortsGatherer()
void DeviceUsedPortsGatherer::start() void DeviceUsedPortsGatherer::start()
{ {
const auto emitError = [this](const QString &errorString) {
d->m_errorString = errorString;
emit done(false);
};
d->usedPorts.clear(); d->usedPorts.clear();
d->m_errorString.clear(); d->m_errorString.clear();
QTC_ASSERT(d->device, emitError("No device given"); return); QTC_ASSERT(d->device, emitError("No device given"); return);
@@ -57,7 +62,25 @@ void DeviceUsedPortsGatherer::start()
d->process.reset(new Process); d->process.reset(new Process);
d->process->setCommand(d->portsGatheringMethod.commandLine(protocol)); d->process->setCommand(d->portsGatheringMethod.commandLine(protocol));
connect(d->process.get(), &Process::done, this, &DeviceUsedPortsGatherer::handleProcessDone); connect(d->process.get(), &Process::done, this, [this, emitError] {
if (d->process->result() == ProcessResult::FinishedWithSuccess) {
d->usedPorts.clear();
const QList<Port> usedPorts = d->portsGatheringMethod.parsePorts(
d->process->rawStdOut());
for (const Port port : usedPorts) {
if (d->device->freePorts().contains(port))
d->usedPorts << port;
}
emit done(true);
} else {
const QString errorString = d->process->errorString();
const QString stdErr = d->process->readAllStandardError();
const QString outputString
= stdErr.isEmpty() ? stdErr : Tr::tr("Remote error output was: %1").arg(stdErr);
emitError(Utils::joinStrings({errorString, outputString}, '\n'));
}
stop();
});
d->process->start(); d->process->start();
} }
@@ -84,38 +107,6 @@ QString DeviceUsedPortsGatherer::errorString() const
return d->m_errorString; return d->m_errorString;
} }
void DeviceUsedPortsGatherer::setupUsedPorts()
{
d->usedPorts.clear();
const QList<Port> usedPorts = d->portsGatheringMethod.parsePorts(
d->process->rawStdOut());
for (const Port port : usedPorts) {
if (d->device->freePorts().contains(port))
d->usedPorts << port;
}
emit portListReady();
}
void DeviceUsedPortsGatherer::emitError(const QString &errorString)
{
d->m_errorString = errorString;
emit error(errorString);
}
void DeviceUsedPortsGatherer::handleProcessDone()
{
if (d->process->result() == ProcessResult::FinishedWithSuccess) {
setupUsedPorts();
} else {
const QString errorString = d->process->errorString();
const QString stdErr = d->process->readAllStandardError();
const QString outputString
= stdErr.isEmpty() ? stdErr : Tr::tr("Remote error output was: %1").arg(stdErr);
emitError(Utils::joinStrings({errorString, outputString}, '\n'));
}
stop();
}
// PortGatherer // PortGatherer
PortsGatherer::PortsGatherer(RunControl *runControl) PortsGatherer::PortsGatherer(RunControl *runControl)
@@ -123,11 +114,15 @@ PortsGatherer::PortsGatherer(RunControl *runControl)
{ {
setId("PortGatherer"); setId("PortGatherer");
connect(&m_portsGatherer, &DeviceUsedPortsGatherer::error, this, &PortsGatherer::reportFailure); connect(&m_portsGatherer, &DeviceUsedPortsGatherer::done, this, [this](bool success) {
connect(&m_portsGatherer, &DeviceUsedPortsGatherer::portListReady, this, [this] { if (success) {
m_portList = device()->freePorts(); m_portList = device()->freePorts();
appendMessage(Tr::tr("Found %n free ports.", nullptr, m_portList.count()), NormalMessageFormat); appendMessage(Tr::tr("Found %n free ports.", nullptr, m_portList.count()),
reportStarted(); NormalMessageFormat);
reportStarted();
} else {
reportFailure(m_portsGatherer.errorString());
}
}); });
} }

View File

@@ -35,14 +35,9 @@ public:
QString errorString() const; QString errorString() const;
signals: signals:
void error(const QString &errMsg); void done(bool success);
void portListReady();
private: private:
void handleProcessDone();
void setupUsedPorts();
void emitError(const QString &errorString);
Internal::DeviceUsedPortsGathererPrivate * const d; Internal::DeviceUsedPortsGathererPrivate * const d;
}; };
@@ -51,8 +46,9 @@ class PROJECTEXPLORER_EXPORT DeviceUsedPortsGathererTaskAdapter
{ {
public: public:
DeviceUsedPortsGathererTaskAdapter() { DeviceUsedPortsGathererTaskAdapter() {
connect(task(), &DeviceUsedPortsGatherer::portListReady, this, [this] { emit done(DoneResult::Success); }); connect(task(), &DeviceUsedPortsGatherer::done, this, [this](bool success) {
connect(task(), &DeviceUsedPortsGatherer::error, this, [this] { emit done(DoneResult::Error); }); emit done(toDoneResult(success));
});
} }
void start() final { task()->start(); } void start() final { task()->start(); }
}; };