aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/boot2qt/qdbplugin.cpp
blob: 3c20669c0e35d1f5d3fafb7a59fa7d0a6e2b2dfd (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "device-detection/devicedetector.h"
#include "qdbconstants.h"
#include "qdbdevice.h"
#include "qdbstopapplicationstep.h"
#include "qdbmakedefaultappstep.h"
#include "qdbdevicedebugsupport.h"
#include "qdbqtversion.h"
#include "qdbrunconfiguration.h"
#include "qdbutils.h"
#include "qdbtr.h"

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/icore.h>

#include <extensionsystem/iplugin.h>

#include <projectexplorer/deployconfiguration.h>
#include <projectexplorer/devicesupport/devicemanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>

#include <qtsupport/qtversionfactory.h>

#include <remotelinux/remotelinux_constants.h>

#include <utils/hostosinfo.h>
#include <utils/qtcprocess.h>

using namespace Core;
using namespace ProjectExplorer;
using namespace Utils;

namespace Qdb::Internal {

static FilePath flashWizardFilePath()
{
    return findTool(QdbTool::FlashingWizard);
}

static void startFlashingWizard()
{
    const FilePath filePath = flashWizardFilePath();
    if (HostOsInfo::isWindowsHost()) {
        if (Process::startDetached({"explorer.exe", {filePath.toUserOutput()}}))
            return;
    } else if (Process::startDetached(CommandLine{filePath})) {
        return;
    }
    const QString message = Tr::tr("Flash wizard \"%1\" failed to start.");
    showMessage(message.arg(filePath.toUserOutput()), true);
}

static bool isFlashActionDisabled()
{
    QtcSettings * const settings = Core::ICore::settings();
    settings->beginGroup(settingsGroupKey());
    bool disabled = settings->value("flashActionDisabled", false).toBool();
    settings->endGroup();
    return disabled;
}

void registerFlashAction(QObject *parentForAction)
{
    if (isFlashActionDisabled())
        return;
    const FilePath fileName = flashWizardFilePath();
    if (!fileName.exists()) {
        const QString message = Tr::tr("Flash wizard executable \"%1\" not found.");
        showMessage(message.arg(fileName.toUserOutput()));
        return;
    }

    const char flashActionId[] = "Qdb.FlashAction";
    if (ActionManager::command(flashActionId))
        return; // The action has already been registered.

    ActionContainer *toolsContainer = ActionManager::actionContainer(Core::Constants::M_TOOLS);
    toolsContainer->insertGroup(Core::Constants::G_TOOLS_DEBUG, flashActionId);

    ActionBuilder flashAction(parentForAction, flashActionId);
    flashAction.setText(Tr::tr("Flash Boot to Qt Device"));
    flashAction.addToContainer(Core::Constants::M_TOOLS, flashActionId);
    flashAction.addOnTriggered(&startFlashingWizard);
}

class QdbDeployStepFactory : public BuildStepFactory
{
public:
    explicit QdbDeployStepFactory(Id existingStepId)
    {
        cloneStepCreator(existingStepId);
        setSupportedStepList(ProjectExplorer::Constants::BUILDSTEPS_DEPLOY);
        setSupportedDeviceType(Constants::QdbLinuxOsType);
    }
};

class QdbDeployConfigurationFactory final : public DeployConfigurationFactory
{
public:
    QdbDeployConfigurationFactory()
    {
        setConfigBaseId(Constants::QdbDeployConfigurationId);
        addSupportedTargetDeviceType(Constants::QdbLinuxOsType);
        setDefaultDisplayName(Tr::tr("Deploy to Boot to Qt target"));
        setUseDeploymentDataView();

        addInitialStep(RemoteLinux::Constants::MakeInstallStepId, [](BuildConfiguration *bc) {
            const Project * const prj = bc->project();
            return prj->deploymentKnowledge() == DeploymentKnowledge::Bad
                   && prj->hasMakeInstallEquivalent();
        });
        addInitialStep(Qdb::Constants::QdbStopApplicationStepId);
#ifdef Q_OS_WIN
        addInitialStep(RemoteLinux::Constants::DirectUploadStepId);
#else
        addInitialStep(RemoteLinux::Constants::GenericDeployStepId);
#endif
    }
};

class QdbPluginPrivate final : public QObject
{
public:
    void setupDeviceDetection() { m_deviceDetector.start(); }

    QdbDeployConfigurationFactory m_deployConfigFactory;
    QdbRunConfigurationFactory m_runConfigFactory;
    QdbStopApplicationStepFactory m_stopApplicationStepFactory;
    QdbMakeDefaultAppStepFactory m_makeDefaultAppStepFactory;

    QdbDeployStepFactory m_directUploadStepFactory{RemoteLinux::Constants::DirectUploadStepId};
    QdbDeployStepFactory m_rsyncDeployStepFactory{RemoteLinux::Constants::GenericDeployStepId};
    QdbDeployStepFactory m_makeInstallStepFactory{RemoteLinux::Constants::MakeInstallStepId};

    DeviceDetector m_deviceDetector;
};

class QdbPlugin final : public ExtensionSystem::IPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "Boot2Qt.json")

public:
    ~QdbPlugin() final { delete d; }

private:
    void initialize() final
    {
        setupQdbLinuxDevice();
        setupQdbQtVersion();
        setupQdbRunWorkers();

        d = new QdbPluginPrivate;

        registerFlashAction(this);
    }

    void extensionsInitialized() final
    {
        if (DeviceManager::isLoaded()) {
            d->setupDeviceDetection();
        } else {
            connect(DeviceManager::instance(), &DeviceManager::devicesLoaded,
                    d, &QdbPluginPrivate::setupDeviceDetection);
        }
    }

    ShutdownFlag aboutToShutdown() final
    {
        d->m_deviceDetector.stop();

        return SynchronousShutdown;
    }

    class QdbPluginPrivate *d = nullptr;
};

} // Qdb::Internal

#include "qdbplugin.moc"