aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizardpages.cpp
blob: ff2567647d6d9455a8c4b635f464c340da956c2d (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (C) 2016 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 "genericlinuxdeviceconfigurationwizardpages.h"

#include "publickeydeploymentdialog.h"
#include "remotelinuxtr.h"
#include "sshkeycreationdialog.h"

#include <projectexplorer/devicesupport/sshparameters.h>

#include <utils/fileutils.h>
#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h>
#include <utils/utilsicons.h>

#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>

using namespace ProjectExplorer;
using namespace Utils;

namespace RemoteLinux {
namespace Internal {

class GenericLinuxDeviceConfigurationWizardSetupPagePrivate
{
public:
    QLineEdit *nameLineEdit;
    QLineEdit *hostNameLineEdit;
    QSpinBox *sshPortSpinBox;
    QLineEdit *userNameLineEdit;

    LinuxDevice::Ptr device;
};

class GenericLinuxDeviceConfigurationWizardFinalPagePrivate
{
public:
    QLabel infoLabel;
};

} // namespace Internal

GenericLinuxDeviceConfigurationWizardSetupPage::GenericLinuxDeviceConfigurationWizardSetupPage(
        QWidget *parent) :
    QWizardPage(parent), d(new Internal::GenericLinuxDeviceConfigurationWizardSetupPagePrivate)
{
    setTitle(Tr::tr("Connection"));
    setWindowTitle(Tr::tr("WizardPage"));

    d->nameLineEdit = new QLineEdit(this);
    d->hostNameLineEdit = new QLineEdit(this);
    d->sshPortSpinBox = new QSpinBox(this);
    d->userNameLineEdit = new QLineEdit(this);

    using namespace Layouting;
    Form {
        Tr::tr("The name to identify this configuration:"), d->nameLineEdit, br,
        Tr::tr("The device's host name or IP address:"), d->hostNameLineEdit, st, br,
        Tr::tr("The device's SSH port number:"), d->sshPortSpinBox, st, br,
        Tr::tr("The username to log into the device:"), d->userNameLineEdit, st, br
    }.attachTo(this);

    setSubTitle(QLatin1String(" ")); // For Qt bug (background color)
    connect(d->nameLineEdit, &QLineEdit::textChanged, this, &QWizardPage::completeChanged);
    connect(d->hostNameLineEdit, &QLineEdit::textChanged, this, &QWizardPage::completeChanged);
    connect(d->sshPortSpinBox, &QSpinBox::valueChanged, this, &QWizardPage::completeChanged);
    connect(d->userNameLineEdit, &QLineEdit::textChanged, this, &QWizardPage::completeChanged);
}

GenericLinuxDeviceConfigurationWizardSetupPage::~GenericLinuxDeviceConfigurationWizardSetupPage()
{
    delete d;
}

void GenericLinuxDeviceConfigurationWizardSetupPage::initializePage()
{
    d->nameLineEdit->setText(d->device->displayName());
    d->hostNameLineEdit->setText(d->device->sshParameters().host());
    d->sshPortSpinBox->setValue(22);
    d->sshPortSpinBox->setRange(1, 65535);
    d->userNameLineEdit->setText(d->device->sshParameters().userName());
}

bool GenericLinuxDeviceConfigurationWizardSetupPage::isComplete() const
{
    return !configurationName().isEmpty()
            && !d->hostNameLineEdit->text().trimmed().isEmpty()
            && !d->userNameLineEdit->text().trimmed().isEmpty();
}

bool GenericLinuxDeviceConfigurationWizardSetupPage::validatePage()
{
    d->device->setDisplayName(configurationName());
    SshParameters sshParams = d->device->sshParameters();
    sshParams.url = url();
    d->device->setSshParameters(sshParams);
    return true;
}

QString GenericLinuxDeviceConfigurationWizardSetupPage::configurationName() const
{
    return d->nameLineEdit->text().trimmed();
}

QUrl GenericLinuxDeviceConfigurationWizardSetupPage::url() const
{
    QUrl url;
    url.setHost(d->hostNameLineEdit->text().trimmed());
    url.setUserName(d->userNameLineEdit->text().trimmed());
    url.setPort(d->sshPortSpinBox->value());
    return url;
}

void GenericLinuxDeviceConfigurationWizardSetupPage::setDevice(const LinuxDevice::Ptr &device)
{
    d->device = device;
}

GenericLinuxDeviceConfigurationWizardFinalPage::GenericLinuxDeviceConfigurationWizardFinalPage(
        QWidget *parent)
    : QWizardPage(parent), d(new Internal::GenericLinuxDeviceConfigurationWizardFinalPagePrivate)
{
    setTitle(Tr::tr("Summary"));
    setSubTitle(QLatin1String(" ")); // For Qt bug (background color)
    d->infoLabel.setWordWrap(true);
    auto const layout = new QVBoxLayout(this);
    layout->addWidget(&d->infoLabel);
}

GenericLinuxDeviceConfigurationWizardFinalPage::~GenericLinuxDeviceConfigurationWizardFinalPage()
{
    delete d;
}

void GenericLinuxDeviceConfigurationWizardFinalPage::initializePage()
{
    d->infoLabel.setText(infoText());
}

QString GenericLinuxDeviceConfigurationWizardFinalPage::infoText() const
{
    return Tr::tr("The new device configuration will now be created.\n"
                  "In addition, device connectivity will be tested.");
}

struct GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::Private
{
    FilePaths defaultKeys() const
    {
        const FilePath baseDir = FileUtils::homePath() / ".ssh";
        return {baseDir / "id_rsa", baseDir / "id_ecdsa", baseDir / "id_ed25519"};
    }

    PathChooser keyFileChooser;
    QLabel iconLabel;
    LinuxDevice::Ptr device;
};

GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::GenericLinuxDeviceConfigurationWizardKeyDeploymentPage(QWidget *parent)
    : QWizardPage(parent), d(new Private)
{
    setTitle(Tr::tr("Key Deployment"));
    setSubTitle(" ");
    const QString info = Tr::tr("We recommend that you log into your device using public key "
                                "authentication.\n"
                                "If your device is already set up for this, you do not have to do "
                                "anything here.\n"
                                "Otherwise, please deploy the public key for the private key "
                                "with which to connect in the future.\n"
                                "If you do not have a private key yet, you can also "
                                "create one here.");
    d->keyFileChooser.setExpectedKind(PathChooser::File);
    d->keyFileChooser.setHistoryCompleter("Ssh.KeyFile.History");
    d->keyFileChooser.setPromptDialogTitle(Tr::tr("Choose a Private Key File"));
    auto const deployButton = new QPushButton(Tr::tr("Deploy Public Key"), this);
    connect(deployButton, &QPushButton::clicked,
            this, &GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::deployKey);
    auto const createButton = new QPushButton(Tr::tr("Create New Key Pair"), this);
    connect(createButton, &QPushButton::clicked,
            this, &GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::createKey);
    auto const mainLayout = new QVBoxLayout(this);
    auto const keyLayout = new QHBoxLayout;
    auto const deployLayout = new QHBoxLayout;
    mainLayout->addWidget(new QLabel(info));
    keyLayout->addWidget(new QLabel(Tr::tr("Private key file:")));
    keyLayout->addWidget(&d->keyFileChooser);
    keyLayout->addWidget(createButton);
    keyLayout->addStretch();
    mainLayout->addLayout(keyLayout);
    deployLayout->addWidget(deployButton);
    deployLayout->addWidget(&d->iconLabel);
    deployLayout->addStretch();
    mainLayout->addLayout(deployLayout);
    connect(&d->keyFileChooser, &PathChooser::filePathChanged, this, [this, deployButton] {
        deployButton->setEnabled(d->keyFileChooser.filePath().exists());
        d->iconLabel.clear();
        emit completeChanged();
    });
    for (const FilePath &defaultKey : d->defaultKeys()) {
        if (defaultKey.exists()) {
            d->keyFileChooser.setFilePath(defaultKey);
            break;
        }
    }
}

GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::~GenericLinuxDeviceConfigurationWizardKeyDeploymentPage()
{
    delete d;
}

void GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::setDevice(const LinuxDevice::Ptr &device)
{
    d->device = device;
}

void GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::initializePage()
{
    if (!d->device->sshParameters().privateKeyFile.isEmpty())
        d->keyFileChooser.setFilePath(privateKeyFilePath());
    d->iconLabel.clear();
}

bool GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::isComplete() const
{
    return d->keyFileChooser.filePath().toString().isEmpty() || d->keyFileChooser.filePath().exists();
}

bool GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::validatePage()
{
    if (!d->defaultKeys().contains(d->keyFileChooser.filePath())) {
        SshParameters sshParams = d->device->sshParameters();
        sshParams.authenticationType = SshParameters::AuthenticationTypeSpecificKey;
        sshParams.privateKeyFile = d->keyFileChooser.filePath();
        d->device->setSshParameters(sshParams);
    }
    return true;
}

FilePath GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::privateKeyFilePath() const
{
    return d->keyFileChooser.filePath();
}

void GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::createKey()
{
    SshKeyCreationDialog dlg(this);
    if (dlg.exec() == QDialog::Accepted)
        d->keyFileChooser.setFilePath(dlg.privateKeyFilePath());
}

void GenericLinuxDeviceConfigurationWizardKeyDeploymentPage::deployKey()
{
    PublicKeyDeploymentDialog dlg(d->device, privateKeyFilePath() + ".pub", this);
    d->iconLabel.setPixmap((dlg.exec() == QDialog::Accepted ? Icons::OK : Icons::BROKEN).pixmap());
}

} // namespace RemoteLinux