aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/boot2qt/qdbstopapplicationstep.cpp
blob: 5a838df1ef251897d374ce559a6498b6e307ef86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "qdbstopapplicationstep.h"

#include "qdbconstants.h"

#include <projectexplorer/devicesupport/idevice.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>

#include <remotelinux/abstractremotelinuxdeployservice.h>
#include <remotelinux/abstractremotelinuxdeploystep.h>

#include <utils/qtcprocess.h>

using namespace ProjectExplorer;
using namespace Utils;
using namespace Utils::Tasking;

namespace Qdb {
namespace Internal {

// QdbStopApplicationService

class QdbStopApplicationService : public RemoteLinux::AbstractRemoteLinuxDeployService
{
    Q_DECLARE_TR_FUNCTIONS(Qdb::Internal::QdbStopApplicationService)

private:
    bool isDeploymentNecessary() const final { return true; }
    Group deployRecipe() final;
};

Group QdbStopApplicationService::deployRecipe()
{
    const auto setupHandler = [this](QtcProcess &process) {
        const auto device = DeviceKitAspect::device(target()->kit());
        QTC_CHECK(device);
        process.setCommand({device->filePath(Constants::AppcontrollerFilepath), {"--stop"}});
        process.setWorkingDirectory("/usr/bin");
        QtcProcess *proc = &process;
        connect(proc, &QtcProcess::readyReadStandardOutput, this, [this, proc] {
            emit stdOutData(QString::fromUtf8(proc->readAllStandardOutput()));
        });
    };
    const auto doneHandler = [this](const QtcProcess &) {
        emit progressMessage(tr("Stopped the running application."));
    };
    const auto errorHandler = [this](const QtcProcess &process) {
        const QString errorOutput = process.cleanedStdErr();
        const QString failureMessage = tr("Could not check and possibly stop running application.");
        if (process.exitStatus() == QProcess::CrashExit) {
            emit errorMessage(failureMessage);
        } else if (process.result() != ProcessResult::FinishedWithSuccess) {
            emit stdErrData(process.errorString());
        } else if (errorOutput.contains("Could not connect: Connection refused")) {
            emit progressMessage(tr("Checked that there is no running application."));
        } else if (!errorOutput.isEmpty()) {
            emit stdErrData(errorOutput);
            emit errorMessage(failureMessage);
        }
    };
    const auto rootSetupHandler = [this] {
        const auto device = DeviceKitAspect::device(target()->kit());
        if (!device) {
            emit errorMessage(tr("No device to stop the application on."));
            return GroupConfig{GroupAction::StopWithError};
        }
        return GroupConfig();
    };
    const Group root {
        DynamicSetup(rootSetupHandler),
        Process(setupHandler, doneHandler, errorHandler)
    };
    return root;
}

// QdbStopApplicationStep

class QdbStopApplicationStep final : public RemoteLinux::AbstractRemoteLinuxDeployStep
{
    Q_DECLARE_TR_FUNCTIONS(Qdb::Internal::QdbStopApplicationStep)

public:
    QdbStopApplicationStep(BuildStepList *bsl, Id id)
        : AbstractRemoteLinuxDeployStep(bsl, id)
    {
        auto service = new QdbStopApplicationService;
        setDeployService(service);

        setWidgetExpandedByDefault(false);

        setInternalInitializer([service] { return service->isDeploymentPossible(); });
    }
};


// QdbStopApplicationStepFactory

QdbStopApplicationStepFactory::QdbStopApplicationStepFactory()
{
    registerStep<QdbStopApplicationStep>(Constants::QdbStopApplicationStepId);
    setDisplayName(QdbStopApplicationStep::tr("Stop already running application"));
    setSupportedDeviceType(Constants::QdbLinuxOsType);
    setSupportedStepList(ProjectExplorer::Constants::BUILDSTEPS_DEPLOY);
}

} // namespace Internal
} // namespace Qdb