forked from qt-creator/qt-creator
Boot2Qt: Simplify DeviceApplicationObserver further
- Use Utils::CommandLine - Indirect member with same lifetime can be normal members ... Change-Id: I56f09574942cbd26e5d456d7b1def5a4b2cc34eb Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -42,100 +42,55 @@
|
|||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/qtcprocess.h>
|
#include <utils/qtcprocess.h>
|
||||||
|
|
||||||
#include <QStringList>
|
|
||||||
|
|
||||||
using namespace ProjectExplorer;
|
using namespace ProjectExplorer;
|
||||||
|
using namespace Utils;
|
||||||
|
|
||||||
namespace Qdb {
|
namespace Qdb {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
class Command {
|
class DeviceApplicationObserver : public ApplicationLauncher
|
||||||
public:
|
|
||||||
QString binary;
|
|
||||||
QStringList arguments;
|
|
||||||
};
|
|
||||||
|
|
||||||
class DeviceApplicationObserver : public QObject
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit DeviceApplicationObserver(QObject *parent = 0);
|
DeviceApplicationObserver(const IDevice::ConstPtr &device, const CommandLine &command)
|
||||||
|
|
||||||
// Once all commands have finished, this object will delete itself.
|
|
||||||
void start(const ProjectExplorer::IDevice::ConstPtr &device, const Command &command);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void handleStdout(const QString &data);
|
|
||||||
void handleStderr(const QString &data);
|
|
||||||
void handleError(const QString &message);
|
|
||||||
void handleFinished(bool success);
|
|
||||||
|
|
||||||
QString m_stdout;
|
|
||||||
QString m_stderr;
|
|
||||||
Command m_command;
|
|
||||||
ProjectExplorer::ApplicationLauncher * const m_appRunner;
|
|
||||||
ProjectExplorer::IDevice::ConstPtr m_device;
|
|
||||||
QString m_error;
|
|
||||||
};
|
|
||||||
|
|
||||||
DeviceApplicationObserver::DeviceApplicationObserver(QObject *parent)
|
|
||||||
: QObject(parent), m_appRunner(new ApplicationLauncher(this))
|
|
||||||
{
|
{
|
||||||
connect(m_appRunner, &ApplicationLauncher::remoteStdout, this,
|
connect(&m_appRunner, &ApplicationLauncher::remoteStdout, this,
|
||||||
&DeviceApplicationObserver::handleStdout);
|
&DeviceApplicationObserver::handleStdout);
|
||||||
connect(m_appRunner, &ApplicationLauncher::remoteStderr, this,
|
connect(&m_appRunner, &ApplicationLauncher::remoteStderr, this,
|
||||||
&DeviceApplicationObserver::handleStderr);
|
&DeviceApplicationObserver::handleStderr);
|
||||||
connect(m_appRunner, &ApplicationLauncher::reportError, this,
|
connect(&m_appRunner, &ApplicationLauncher::reportError, this,
|
||||||
&DeviceApplicationObserver::handleError);
|
&DeviceApplicationObserver::handleError);
|
||||||
connect(m_appRunner, &ApplicationLauncher::finished, this,
|
connect(&m_appRunner, &ApplicationLauncher::finished, this,
|
||||||
&DeviceApplicationObserver::handleFinished);
|
&DeviceApplicationObserver::handleFinished);
|
||||||
}
|
|
||||||
|
|
||||||
void DeviceApplicationObserver::start(const IDevice::ConstPtr &device,
|
|
||||||
const Command &command)
|
|
||||||
{
|
|
||||||
QTC_ASSERT(device, return);
|
QTC_ASSERT(device, return);
|
||||||
m_device = device;
|
m_deviceName = device->displayName();
|
||||||
m_command = command;
|
|
||||||
|
|
||||||
m_stdout.clear();
|
|
||||||
m_stderr.clear();
|
|
||||||
|
|
||||||
Runnable r;
|
Runnable r;
|
||||||
r.executable = m_command.binary;
|
r.setCommandLine(command);
|
||||||
r.commandLineArguments = Utils::QtcProcess::joinArgs(m_command.arguments);
|
m_appRunner.start(r, device);
|
||||||
m_appRunner->start(r, m_device);
|
showMessage(QdbDevice::tr("Starting command '%1' on device '%2'.")
|
||||||
showMessage(QdbDevice::tr("Starting command '%1 %2' on device '%3'.")
|
.arg(command.toUserOutput(), m_deviceName));
|
||||||
.arg(r.executable, r.commandLineArguments, m_device->displayName()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeviceApplicationObserver::handleStdout(const QString &data)
|
private:
|
||||||
{
|
void handleStdout(const QString &data) { m_stdout += data; }
|
||||||
m_stdout += data;
|
void handleStderr(const QString &data) { m_stderr += data; }
|
||||||
}
|
void handleError(const QString &message) { m_error = message; }
|
||||||
|
|
||||||
void DeviceApplicationObserver::handleStderr(const QString &data)
|
void handleFinished(bool success)
|
||||||
{
|
|
||||||
m_stderr += data;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DeviceApplicationObserver::handleError(const QString &message)
|
|
||||||
{
|
|
||||||
m_error = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DeviceApplicationObserver::handleFinished(bool success)
|
|
||||||
{
|
{
|
||||||
if (success && (m_stdout.contains("fail") || m_stdout.contains("error")
|
if (success && (m_stdout.contains("fail") || m_stdout.contains("error")
|
||||||
|| m_stdout.contains("not found"))) {
|
|| m_stdout.contains("not found"))) {
|
||||||
|
// FIXME: Needed in a post-adb world?
|
||||||
success = false; // adb does not forward exit codes and all stderr goes to stdout.
|
success = false; // adb does not forward exit codes and all stderr goes to stdout.
|
||||||
}
|
}
|
||||||
if (!success) {
|
if (!success) {
|
||||||
QString errorString;
|
QString errorString;
|
||||||
if (!m_error.isEmpty()) {
|
if (!m_error.isEmpty()) {
|
||||||
errorString = QdbDevice::tr("Command failed on device '%1': %2").arg(m_device->displayName(),
|
errorString = QdbDevice::tr("Command failed on device '%1': %2")
|
||||||
m_error);
|
.arg(m_deviceName, m_error);
|
||||||
} else {
|
} else {
|
||||||
errorString = QdbDevice::tr("Command failed on device '%1'.").arg(m_device->displayName());
|
errorString = QdbDevice::tr("Command failed on device '%1'.").arg(m_deviceName);
|
||||||
}
|
}
|
||||||
showMessage(errorString, true);
|
showMessage(errorString, true);
|
||||||
if (!m_stdout.isEmpty())
|
if (!m_stdout.isEmpty())
|
||||||
@@ -144,21 +99,31 @@ void DeviceApplicationObserver::handleFinished(bool success)
|
|||||||
showMessage(QdbDevice::tr("stderr was: '%1'").arg(m_stderr));
|
showMessage(QdbDevice::tr("stderr was: '%1'").arg(m_stderr));
|
||||||
} else {
|
} else {
|
||||||
showMessage(QdbDevice::tr("Commands on device '%1' finished successfully.")
|
showMessage(QdbDevice::tr("Commands on device '%1' finished successfully.")
|
||||||
.arg(m_device->displayName()));
|
.arg(m_deviceName));
|
||||||
}
|
}
|
||||||
deleteLater();
|
deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString m_stdout;
|
||||||
|
QString m_stderr;
|
||||||
|
ProjectExplorer::ApplicationLauncher m_appRunner;
|
||||||
|
QString m_deviceName;
|
||||||
|
QString m_error;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// QdbDevice
|
||||||
|
|
||||||
QdbDevice::QdbDevice()
|
QdbDevice::QdbDevice()
|
||||||
{
|
{
|
||||||
addDeviceAction({tr("Reboot Device"), [](const IDevice::Ptr &device, QWidget *) {
|
addDeviceAction({tr("Reboot Device"), [](const IDevice::Ptr &device, QWidget *) {
|
||||||
Command cmd{QStringLiteral("reboot"), {}};
|
CommandLine cmd{FilePath::fromString("reboot")};
|
||||||
(new DeviceApplicationObserver)->start(device, cmd);
|
(void) new DeviceApplicationObserver(device, cmd);
|
||||||
}});
|
}});
|
||||||
|
|
||||||
addDeviceAction({tr("Restore Default App"), [](const IDevice::Ptr &device, QWidget *) {
|
addDeviceAction({tr("Restore Default App"), [](const IDevice::Ptr &device, QWidget *) {
|
||||||
Command cmd{appControllerFilePath(), {"--remove-default"}};
|
CommandLine cmd{FilePath::fromString(appControllerFilePath()), {"--remove-default"}};
|
||||||
(new DeviceApplicationObserver)->start(device, cmd);
|
(void) new DeviceApplicationObserver(device, cmd);
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user