summaryrefslogtreecommitdiff
path: root/src/plugins/qnx/blackberrysetupwidget.cpp
blob: d4146b19fb5ab60fa5957c9fdcb67ef4991909ac (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
/**************************************************************************
**
** Copyright (C) 2014 BlackBerry Limited. All rights reserved.
**
** Contact: BlackBerry (qt@blackberry.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://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** 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 "blackberrysetupwidget.h"

#include "blackberryconfigurationmanager.h"
#include "blackberryapilevelconfiguration.h"
#include "blackberrysigningutils.h"
#include "blackberrydeviceconfigurationwizard.h"
#include "blackberryinstallwizard.h"
#include "blackberrycertificate.h"
#include "qnxconstants.h"

#include <coreplugin/coreconstants.h>
#include <projectexplorer/devicesupport/devicemanager.h>

#include <QFileInfo>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QDesktopServices>
#include <QWizard>
#include <QUrl>
#include <QMessageBox>

namespace Qnx {
namespace Internal {

SetupItem::SetupItem(const QString &desc, QWidget *parent)
: QFrame(parent)
{
    m_timer.setSingleShot(true);
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(validate()));

    setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
    QVBoxLayout *frameLayout = new QVBoxLayout(this);

    QHBoxLayout *childLayout = new QHBoxLayout;
    frameLayout->addLayout(childLayout);

    m_icon = new QLabel;
    childLayout->addWidget(m_icon);

    m_label = new QLabel;
    m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    childLayout->addWidget(m_label);

    m_button = new QPushButton;
    childLayout->addWidget(m_button);
    QObject::connect(m_button, SIGNAL(clicked()), this, SLOT(onFixPressed()));

    if (!desc.isEmpty()) {
        m_desc = new QLabel(desc);
        m_desc->setWordWrap(true);
        QFont font = m_desc->font();
        font.setItalic(true);
        m_desc->setFont(font);
        frameLayout->addWidget(m_desc);
    }
}

void SetupItem::set(Status status, const QString &message, const QString &fixText)
{
    QString icon;
    switch (status) {
    case Ok:
        icon = QLatin1String(Qnx::Constants::QNX_OK_ICON);
        break;
    case Info:
        icon = QLatin1String(Core::Constants::ICON_INFO);
        break;
    case Warning:
        icon = QLatin1String(Core::Constants::ICON_WARNING);
        break;
    case Error:
        icon = QLatin1String(Core::Constants::ICON_ERROR);
        break;
    }
    m_icon->setPixmap(QPixmap(icon));
    m_label->setText(message);
    m_button->setVisible(!fixText.isEmpty());
    m_button->setText(fixText);
}

void SetupItem::onFixPressed()
{
    fix();
    validate();
}

void SetupItem::validateLater()
{
    // BlackBerryConfigurationManager.settingsChanged and DeviceManager.updated signals
    // may be emitted multiple times during the same event handling. This would result in multiple
    // validation() calls even through just one is needed.
    // QTimer allows to merge those multiple signal emits into a single validate() call.
    m_timer.start();
}

APILevelSetupItem::APILevelSetupItem(QWidget *parent)
: SetupItem(tr("API Level defines kits, Qt versions, compilers, debuggers needed"
               " for building BlackBerry applications."), parent)
{
    validate();
    connect(BlackBerryConfigurationManager::instance(), SIGNAL(settingsChanged()),
            this, SLOT(validateLater()));
}

void APILevelSetupItem::validate()
{
    FoundTypes found = resolvedFoundType();
    if (!found.testFlag(Any))
        set(Error, tr("No API Level is installed."), tr("Install"));
    else if (!found.testFlag(Valid))
        set(Error, tr("No valid API Level is installed."), tr("Install"));
    else if (!found.testFlag(Active))
        set(Error, tr("Valid API Level is not activated."), tr("Activate"));
    else if (!found.testFlag(V_10_2))
        set(Warning, tr("Valid API Level 10.2 or newer is not installed."), tr("Install"));
    else if (!found.testFlag(V_10_2_AS_DEFAULT))
        set(Warning, tr("Valid API Level 10.2 or newer is not set as default."), tr("Set"));
    else
        set(Ok, tr("API Level is configured."));
    // TODO: should we check something more e.g. BB10 kits are valid?
}

void APILevelSetupItem::fix()
{
    FoundTypes found = resolvedFoundType();
    if (!found.testFlag(Any) || !found.testFlag(Valid)) {
        installAPILevel();
    } else if (!found.testFlag(Active)) {
        foreach (BlackBerryApiLevelConfiguration *config,
                BlackBerryConfigurationManager::instance()->apiLevels()) {
            if (config->isValid() && !config->isActive()) {
                config->activate();
                break;
            }
        }
    } else if (!found.testFlag(V_10_2)) {
        // TODO: install filter for 10.2 only
        installAPILevel();
    } else if (!found.testFlag(V_10_2_AS_DEFAULT)) {
        BlackBerryConfigurationManager::instance()->setDefaultConfiguration(0);
    }
}

APILevelSetupItem::FoundTypes APILevelSetupItem::resolvedFoundType()
{
    FoundTypes found;

    // TODO: for now, all Trunk versions are understood as 10.2 compliant
    QnxVersionNumber version_10_2(QLatin1String("10.2.0.0"));
    foreach (BlackBerryApiLevelConfiguration *config,
            BlackBerryConfigurationManager::instance()->apiLevels()) {
        found |= Any;
        if (config->isValid()) {
            found |= Valid;
            if (config->isActive())
                found |= Active;
            if (config->version() > version_10_2)
                found |= V_10_2;
        }
    }

    BlackBerryApiLevelConfiguration *config =
            BlackBerryConfigurationManager::instance()->defaultApiLevel();
    if (config && config->version() > version_10_2)
        found |= V_10_2_AS_DEFAULT;

    return found;
}

void APILevelSetupItem::installAPILevel()
{
    BlackBerryInstallWizard wizard(
            BlackBerryInstallerDataHandler::InstallMode,
            BlackBerryInstallerDataHandler::ApiLevel, QString(), this);
    connect(&wizard, SIGNAL(processFinished()), this, SLOT(handleInstallationFinished()));
    wizard.exec();
}

void APILevelSetupItem::handleInstallationFinished()
{
    // manually-added API Levels are automatically registered by BlackBerryInstallWizard
    // auto-detected API Levels needs to reloaded explicitly
    BlackBerryConfigurationManager::instance()->loadAutoDetectedConfigurations(
            BlackBerryConfigurationManager::ApiLevel);
    validate();
}

SigningKeysSetupItem::SigningKeysSetupItem(QWidget *parent)
: SetupItem(tr("Signing keys are needed for signing BlackBerry applications"
                       " and managing debug tokens."), parent)
{
    validate();
    connect(&BlackBerrySigningUtils::instance(), SIGNAL(defaultCertificateLoaded(int)),
            this, SLOT(validate()));
}

void SigningKeysSetupItem::validate()
{
    BlackBerrySigningUtils &utils = BlackBerrySigningUtils::instance();
    if (utils.hasLegacyKeys())
        set(Error, tr("Found legacy BlackBerry signing keys."), tr("Update"));
    else if (!utils.hasRegisteredKeys())
        set(Error, tr("Cannot find BlackBerry signing keys."), tr("Request"));
    else if (!QFileInfo::exists(BlackBerryConfigurationManager::instance()->defaultKeystorePath()))
        set(Error, tr("Cannot find developer certificate."), tr("Create"));
    else if (utils.defaultCertificateOpeningStatus() != BlackBerrySigningUtils::Opened)
        set(Info, tr("Developer certificate is not opened."), tr("Open"));
    else
        set(Ok, tr("Signing keys are ready to use."));
}

void SigningKeysSetupItem::fix()
{
    BlackBerrySigningUtils &utils = BlackBerrySigningUtils::instance();
    if (utils.hasLegacyKeys()) {
        QDesktopServices::openUrl(QUrl(QLatin1String(Qnx::Constants::QNX_LEGACY_KEYS_URL)));
    } else if (!utils.hasRegisteredKeys()) {
        QDesktopServices::openUrl(QUrl(QLatin1String(Qnx::Constants::QNX_REGISTER_KEYS_URL)));
    } else if (!QFileInfo::exists(BlackBerryConfigurationManager::instance()->defaultKeystorePath())) {
        set(Info, tr("Opening certificate..."));
        utils.createCertificate();
    } else if (utils.defaultCertificateOpeningStatus() != BlackBerrySigningUtils::Opened) {
        connect(&utils, SIGNAL(defaultCertificateLoaded(int)), this, SLOT(defaultCertificateLoaded(int)));
        utils.openDefaultCertificate(this);
    }
}

void SigningKeysSetupItem::defaultCertificateLoaded(int status)
{
    BlackBerrySigningUtils &utils = BlackBerrySigningUtils::instance();
    disconnect(&utils, SIGNAL(defaultCertificateLoaded(int)), this, SLOT(defaultCertificateLoaded(int)));
    switch (status) {
    case BlackBerryCertificate::Success:
        // handled by the connect in ctor already
        break;
    case BlackBerryCertificate::WrongPassword:
        QMessageBox::critical(this, tr("Qt Creator"), tr("Invalid certificate password."));
        break;
    case BlackBerryCertificate::Busy:
    case BlackBerryCertificate::InvalidOutputFormat:
    case BlackBerryCertificate::Error:
        QMessageBox::critical(this, tr("Qt Creator"), tr("Error loading certificate."));
        break;
    }
}

DeviceSetupItem::DeviceSetupItem(QWidget *parent)
: SetupItem(tr("BlackBerry 10 device or simulator is needed for running BlackBerry applications."),
            parent)
{
    validate();
    connect(ProjectExplorer::DeviceManager::instance(), SIGNAL(updated()),
            this, SLOT(validateLater()));
}

void DeviceSetupItem::validate()
{
    bool found = false;
    ProjectExplorer::DeviceManager *manager = ProjectExplorer::DeviceManager::instance();
    for (int i = 0; i < manager->deviceCount(); i ++) {
        ProjectExplorer::IDevice::ConstPtr device = manager->deviceAt(i);
        if (device->type() == Constants::QNX_BB_OS_TYPE) {
            found = true;
            break;
        }
    }
    if (!found)
        set(Error, tr("No BlackBerry 10 device or simulator is registered."), tr("Add"));
    else
        set(Ok, tr("BlackBerry 10 device or simulator is registered."));
    // TODO: check for existence of an API Level matching a device?
}

void DeviceSetupItem::fix()
{
    BlackBerryDeviceConfigurationWizard wizard(this);
    if (wizard.exec() == QDialog::Accepted)
        ProjectExplorer::DeviceManager::instance()->addDevice(wizard.device());
}

BlackBerrySetupWidget::BlackBerrySetupWidget(QWidget *parent)
    : QWidget(parent)
{
    QVBoxLayout *layout = new QVBoxLayout(this);
    setLayout(layout);

    layout->addWidget(new APILevelSetupItem);
    layout->addWidget(new SigningKeysSetupItem);
    layout->addWidget(new DeviceSetupItem);

    layout->addStretch();

    QLabel *howTo = new QLabel;
    howTo->setTextFormat(Qt::RichText);
    howTo->setTextInteractionFlags(Qt::TextBrowserInteraction);
    howTo->setOpenExternalLinks(true);
    howTo->setText(tr("<a href=\"%1\">How to Setup Qt Creator for BlackBerry 10 development</a>")
                   .arg(QLatin1String(Qnx::Constants::QNX_BLACKBERRY_SETUP_URL)));
    layout->addWidget(howTo);
}

} // namespace Internal
} // namespace Qnx