summaryrefslogtreecommitdiff
path: root/src/plugins/qnx/blackberrydeviceconfigurationwizardpages.cpp
blob: 342a2bd93f39922de5a7a100076ac25481a05582 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**************************************************************************
**
** Copyright (C) 2012, 2013 BlackBerry Limited. All rights reserved.
**
** Contact: BlackBerry (qt@blackberry.com)
** Contact: KDAB (info@kdab.com)
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "blackberrydeviceconfigurationwizardpages.h"
#include "blackberryconfiguration.h"
#include "blackberrydebugtokenrequestdialog.h"
#include "blackberrysshkeysgenerator.h"
#include "blackberrydeviceinformation.h"
#include "ui_blackberrydeviceconfigurationwizardsetuppage.h"
#include "ui_blackberrydeviceconfigurationwizardquerypage.h"
#include "ui_blackberrydeviceconfigurationwizardconfigpage.h"
#include "blackberryconfiguration.h"
#include "blackberrydeviceconnectionmanager.h"
#include "qnxutils.h"

#include <coreplugin/icore.h>
#include <ssh/sshkeygenerator.h>

#include <QDir>
#include <QFormLayout>
#include <QMessageBox>
#include <QFileDialog>
#include <QFileInfo>
#include <QHostInfo>
#include <QAbstractItemModel>

using namespace ProjectExplorer;
using namespace Qnx;
using namespace Qnx::Internal;

namespace {
const char DEVICEHOSTNAME_FIELD_ID[] = "DeviceHostName";
const char DEVICEPASSWORD_FIELD_ID[] = "DevicePassword";
const char CONFIGURATIONNAME_FIELD_ID[] = "ConfigurationName";
const char DEBUGTOKENPATH_FIELD_ID[] = "DebugTokenPath";

enum DeviceListUserRole
{
    ItemKindRole = Qt::UserRole, DeviceNameRole, DeviceIpRole, DeviceTypeRole
};
}

BlackBerryDeviceConfigurationWizardSetupPage::BlackBerryDeviceConfigurationWizardSetupPage(QWidget *parent)
    : QWizardPage(parent)
    , m_ui(new Ui::BlackBerryDeviceConfigurationWizardSetupPage)
    , m_deviceListDetector(new BlackBerryDeviceListDetector(this))

{
    m_ui->setupUi(this);
    setTitle(tr("Connection"));

    connect(m_ui->deviceListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onDeviceSelectionChanged()));
    connect(m_deviceListDetector, SIGNAL(deviceDetected(QString,QString,bool)),
            this, SLOT(onDeviceDetected(QString,QString,bool)));
    connect(m_deviceListDetector, SIGNAL(finished()), this, SLOT(onDeviceListDetectorFinished()));
    connect(m_ui->deviceHostIp, SIGNAL(textChanged(QString)), this, SIGNAL(completeChanged()));

    registerField(QLatin1String(DEVICEHOSTNAME_FIELD_ID), m_ui->deviceHostIp);
    registerField(QLatin1String(DEVICEPASSWORD_FIELD_ID), m_ui->password);
}

BlackBerryDeviceConfigurationWizardSetupPage::~BlackBerryDeviceConfigurationWizardSetupPage()
{
    delete m_ui;
    m_ui = 0;
}

void BlackBerryDeviceConfigurationWizardSetupPage::initializePage()
{
    m_ui->password->clear();
    refreshDeviceList();
}

void BlackBerryDeviceConfigurationWizardSetupPage::refreshDeviceList()
{
    m_ui->deviceListWidget->clear();

    QListWidgetItem *manual = createDeviceListItem(tr("Specify device manually"), SpecifyManually);
    m_ui->deviceListWidget->addItem(manual);
    manual->setSelected(true);

    QListWidgetItem *pleaseWait =
            createDeviceListItem(tr("Auto-detecting devices - please wait..."), PleaseWait);
    m_ui->deviceListWidget->addItem(pleaseWait);

    m_deviceListDetector->detectDeviceList();
}

void BlackBerryDeviceConfigurationWizardSetupPage::onDeviceListDetectorFinished()
{
    QListWidgetItem *pleaseWait = findDeviceListItem(PleaseWait);
    if (pleaseWait) {
        m_ui->deviceListWidget->removeItemWidget(pleaseWait);
        delete pleaseWait;
    }

    if (!findDeviceListItem(Autodetected)) {
        QListWidgetItem *note = createDeviceListItem(tr("No device has been auto-detected."), Note);
        note->setToolTip(tr("Device auto-detection is available in BB NDK 10.2. "
                            "Make sure that your device is in Development Mode."));
        m_ui->deviceListWidget->addItem(note);
    }
}

void BlackBerryDeviceConfigurationWizardSetupPage::onDeviceDetected(
        const QString &deviceName, const QString &hostName, bool isSimulator)
{
    QString displayName(deviceName);
    if (displayName != hostName)
        displayName.append(QLatin1String(" (")).append(hostName).append(QLatin1String(")"));

    QListWidgetItem *device = createDeviceListItem(displayName, Autodetected);
    device->setData(DeviceNameRole, displayName);
    device->setData(DeviceIpRole, hostName);
    device->setData(DeviceTypeRole, isSimulator);
    QListWidgetItem *pleaseWait = findDeviceListItem(PleaseWait);
    int row = pleaseWait ? m_ui->deviceListWidget->row(pleaseWait) : m_ui->deviceListWidget->count();
    m_ui->deviceListWidget->insertItem(row, device);
}

void BlackBerryDeviceConfigurationWizardSetupPage::onDeviceSelectionChanged()
{
    QList<QListWidgetItem *> selectedItems = m_ui->deviceListWidget->selectedItems();
    const QListWidgetItem *selected = selectedItems.count() == 1 ? selectedItems[0] : 0;
    const ItemKind itemKind = selected ? selected->data(ItemKindRole).value<ItemKind>() : Note;
    switch (itemKind) {
    case SpecifyManually:
        m_ui->deviceHostIp->setEnabled(true);
        m_ui->deviceHostIp->setText(QLatin1String("169.254.0.1"));
        m_ui->password->setEnabled(true);
        m_ui->deviceHostIp->selectAll();
        m_ui->deviceHostIp->setFocus();
        break;
    case Autodetected:
        m_ui->deviceHostIp->setEnabled(false);
        m_ui->deviceHostIp->setText(selected->data(DeviceIpRole).toString());
        m_ui->password->setEnabled(true);
        m_ui->password->setFocus();
        break;
    case PleaseWait:
    case Note:
        m_ui->deviceHostIp->setEnabled(false);
        m_ui->deviceHostIp->clear();
        m_ui->password->setEnabled(false);
        break;
    }
}

QListWidgetItem *BlackBerryDeviceConfigurationWizardSetupPage::createDeviceListItem(
        const QString &displayName, ItemKind itemKind) const
{
    QListWidgetItem *item = new QListWidgetItem(displayName);
    if (itemKind == PleaseWait || itemKind == Note) {
        item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
        QFont font = item->font();
        font.setItalic(true);
        item->setFont(font);
    }
    item->setData(ItemKindRole, QVariant::fromValue(itemKind));
    return item;
}

QListWidgetItem *BlackBerryDeviceConfigurationWizardSetupPage::findDeviceListItem(ItemKind itemKind) const
{
    int count = m_ui->deviceListWidget->count();
    for (int i = 0; i < count; ++i) {
        QListWidgetItem *item = m_ui->deviceListWidget->item(i);
        if (item->data(ItemKindRole).value<ItemKind>() == itemKind) {
            return item;
        }
    }
    return 0;
}

bool BlackBerryDeviceConfigurationWizardSetupPage::isComplete() const
{
    return !m_ui->deviceHostIp->text().isEmpty();
}

QString BlackBerryDeviceConfigurationWizardSetupPage::hostName() const
{
    return m_ui->deviceHostIp->text();
}

QString BlackBerryDeviceConfigurationWizardSetupPage::password() const
{
    return m_ui->password->text();
}

// ----------------------------------------------------------------------------

BlackBerryDeviceConfigurationWizardQueryPage::BlackBerryDeviceConfigurationWizardQueryPage
        (BlackBerryDeviceConfigurationWizardHolder &holder, QWidget *parent)
    : QWizardPage(parent)
    , m_ui(new Ui::BlackBerryDeviceConfigurationWizardQueryPage)
    , m_holder(holder)
    , m_deviceInformation(new BlackBerryDeviceInformation(this))
{
    m_ui->setupUi(this);
    setTitle(tr("Device Information"));
    m_ui->progressBar->setMaximum(Done);

    connect(m_deviceInformation,SIGNAL(finished(int)),this,SLOT(processQueryFinished(int)));
}

BlackBerryDeviceConfigurationWizardQueryPage::~BlackBerryDeviceConfigurationWizardQueryPage()
{
    delete m_ui;
    m_ui = 0;
}

void BlackBerryDeviceConfigurationWizardQueryPage::initializePage()
{
    m_holder.deviceInfoRetrieved = false;

    setState(Querying, tr("Querying device information. Please wait..."));

    m_deviceInformation->setDeviceTarget(
                field(QLatin1String(DEVICEHOSTNAME_FIELD_ID)).toString(),
                field(QLatin1String(DEVICEPASSWORD_FIELD_ID)).toString());
}

void BlackBerryDeviceConfigurationWizardQueryPage::processQueryFinished(int status)
{
    m_holder.deviceInfoRetrieved = status == BlackBerryDeviceInformation::Success;
    m_holder.devicePin = m_deviceInformation->devicePin();
    m_holder.scmBundle = m_deviceInformation->scmBundle();
    m_holder.deviceName = m_deviceInformation->hostName();
    if (m_holder.deviceName.isEmpty())
        m_holder.deviceName = QLatin1String("BlackBerry at ")
                + field(QLatin1String(DEVICEHOSTNAME_FIELD_ID)).toString();
    m_holder.debugTokenAuthor = m_deviceInformation->debugTokenAuthor();
    m_holder.debugTokenValid = m_deviceInformation->debugTokenValid();
    m_holder.isSimulator = m_deviceInformation->isSimulator();
    m_holder.isProductionDevice = m_deviceInformation->isProductionDevice();

    if (m_holder.deviceInfoRetrieved)
        checkAndGenerateSSHKeys();
    else
        setState(Done, tr("Cannot connect to the device. "
                "Check if the device is in development mode and has matching host name and password."));
}

void BlackBerryDeviceConfigurationWizardQueryPage::checkAndGenerateSSHKeys()
{
    if (! BlackBerryDeviceConnectionManager::instance()->hasValidSSHKeys()) {
        setState(GeneratingSshKey, tr("Generating SSH keys. Please wait..."));

        BlackBerrySshKeysGenerator *sshKeysGenerator = new BlackBerrySshKeysGenerator();
        connect(sshKeysGenerator, SIGNAL(sshKeysGenerationFailed(QString)),
                this, SLOT(sshKeysGenerationFailed(QString)), Qt::QueuedConnection);
        connect(sshKeysGenerator, SIGNAL(sshKeysGenerationFinished(QByteArray,QByteArray)),
                this, SLOT(processSshKeys(QByteArray,QByteArray)), Qt::QueuedConnection);
        sshKeysGenerator->start();
    } else
        queryDone();
}

void BlackBerryDeviceConfigurationWizardQueryPage::sshKeysGenerationFailed(const QString &error)
{
    // this slot can be called asynchronously - processing it only in GeneratingSshKey state
    if (m_state != GeneratingSshKey)
        return;

    QString message = tr("Failed generating SSH key needed for securing connection to a device. Error: ");
    message.append(error);
    setState(Done, message);
}

void BlackBerryDeviceConfigurationWizardQueryPage::processSshKeys(const QByteArray &privateKey,
                                                                  const QByteArray &publicKey)
{
    // this slot can be called asynchronously - processing it only in GeneratingSshKey state
    if (m_state != GeneratingSshKey)
        return;

    // condition prevents overriding already generated SSH keys
    // this may happens when an user enter the QueryPage several times before
    // the first SSH keys are generated i.e. when multiple calls of checkAndGenerateSSHKeys()
    // before processSshKeys() is called, multiple processSshKeys() calls are triggered later.
    // only the first one is allowed to write the SSH keys.
    if (! BlackBerryDeviceConnectionManager::instance()->hasValidSSHKeys()) {
        QString error;
        if (!BlackBerryDeviceConnectionManager::instance()->setSSHKeys(privateKey, publicKey, &error)) {
            QString message = tr("Failed saving SSH key needed for securing connection to a device. Error: ");
            message.append(error);
            setState(Done, message);
            return;
        }
    }

    queryDone();
}

void BlackBerryDeviceConfigurationWizardQueryPage::queryDone()
{
    setState(Done, tr("Device information retrieved successfully."));
}

void BlackBerryDeviceConfigurationWizardQueryPage::setState(QueryState state, const QString &message)
{
    m_state = state;
    m_ui->statusLabel->setText(message);
    m_ui->progressBar->setVisible(state != Done);
    m_ui->progressBar->setValue(state);
    emit completeChanged();

    if (isComplete())
        if (wizard()->currentPage() == this)
            wizard()->next();
}

bool BlackBerryDeviceConfigurationWizardQueryPage::isComplete() const
{
    return m_state == Done
            && m_holder.deviceInfoRetrieved
            && BlackBerryDeviceConnectionManager::instance()->hasValidSSHKeys();
}

// ----------------------------------------------------------------------------

BlackBerryDeviceConfigurationWizardConfigPage::BlackBerryDeviceConfigurationWizardConfigPage
        (BlackBerryDeviceConfigurationWizardHolder &holder, QWidget *parent)
    : QWizardPage(parent)
    , m_ui(new Ui::BlackBerryDeviceConfigurationWizardConfigPage)
    , m_holder(holder)
{
    m_ui->setupUi(this);
    setTitle(tr("Configuration"));

    m_ui->debugTokenField->setExpectedKind(Utils::PathChooser::File);
    m_ui->debugTokenField->setPromptDialogFilter(QLatin1String("*.bar"));

    QString debugTokenBrowsePath = QnxUtils::dataDirPath();
    if (!QFileInfo(debugTokenBrowsePath).exists())
        debugTokenBrowsePath = QDir::homePath();
    m_ui->debugTokenField->setInitialBrowsePathBackup(debugTokenBrowsePath);

    connect(m_ui->configurationNameField, SIGNAL(textChanged(QString)), this, SIGNAL(completeChanged()));
    connect(m_ui->debugTokenField, SIGNAL(changed(QString)), this, SIGNAL(completeChanged()));
    connect(m_ui->generateButton, SIGNAL(clicked()), this, SLOT(generateDebugToken()));

    registerField(QLatin1String(CONFIGURATIONNAME_FIELD_ID), m_ui->configurationNameField);
    registerField(QLatin1String(DEBUGTOKENPATH_FIELD_ID), m_ui->debugTokenField);
}

BlackBerryDeviceConfigurationWizardConfigPage::~BlackBerryDeviceConfigurationWizardConfigPage()
{
    delete m_ui;
    m_ui = 0;
}

void BlackBerryDeviceConfigurationWizardConfigPage::initializePage()
{
    QString deviceHostName = field(QLatin1String(DEVICEHOSTNAME_FIELD_ID)).toString();
    m_ui->configurationNameField->setText(m_holder.deviceName);
    m_ui->deviceHostNameField->setText(deviceHostName);
    m_ui->deviceTypeField->setText(QLatin1String (m_holder.isSimulator ? "Simulator" : "Device"));
    m_ui->debugTokenField->setEnabled(!m_holder.isSimulator);
    m_ui->generateButton->setEnabled(!m_holder.isSimulator);
}

bool BlackBerryDeviceConfigurationWizardConfigPage::isComplete() const
{
    bool configurationNameComplete = !m_ui->configurationNameField->text().isEmpty();
    Utils::FileName fileName = m_ui->debugTokenField->fileName();
    bool debugTokenComplete = m_holder.isSimulator || !m_holder.isProductionDevice
            || (!fileName.isEmpty() && QFileInfo(fileName.toString()).exists());

    return configurationNameComplete  &&  debugTokenComplete;
}

void BlackBerryDeviceConfigurationWizardConfigPage::generateDebugToken()
{
    BlackBerryDebugTokenRequestDialog dialog;
    dialog.setDevicePin(m_holder.devicePin);

    const int result = dialog.exec();

    if (result != QDialog::Accepted)
        return;

    m_ui->debugTokenField->setPath(dialog.debugToken());
}

QString BlackBerryDeviceConfigurationWizardConfigPage::configurationName() const
{
    return m_ui->configurationNameField->text();
}

QString BlackBerryDeviceConfigurationWizardConfigPage::debugToken() const
{
    return m_ui->debugTokenField->fileName().toString();
}

// ----------------------------------------------------------------------------

BlackBerryDeviceConfigurationWizardFinalPage::BlackBerryDeviceConfigurationWizardFinalPage(QWidget *parent)
    : QWizardPage(parent)
{
    setTitle(tr("Summary"));

    QVBoxLayout *layout = new QVBoxLayout(this);
    QLabel *label = new QLabel(tr("The new device configuration will be created now."), this);
    layout->addWidget(label);
}