~ubuntu-sdk-team/qtcreator-plugin-remotelinux/trunk

« back to all changes in this revision

Viewing changes to src/qnx/blackberrysetupwidget.cpp

  • Committer: CI bot
  • Author(s): Benjamin Zeller
  • Date: 2014-06-16 10:28:43 UTC
  • mfrom: (4.2.4 remotelinux)
  • Revision ID: ps-jenkins@lists.canonical.com-20140616102843-8juvmjvzwlzsboyw
Migrating to Qt5.3 and QtC 3.1 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
**
 
3
** Copyright (C) 2014 BlackBerry Limited. All rights reserved.
 
4
**
 
5
** Contact: BlackBerry (qt@blackberry.com)
 
6
**
 
7
** This file is part of Qt Creator.
 
8
**
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
****************************************************************************/
 
30
 
 
31
#include "blackberrysetupwidget.h"
 
32
 
 
33
#include "blackberryconfigurationmanager.h"
 
34
#include "blackberryapilevelconfiguration.h"
 
35
#include "blackberrysigningutils.h"
 
36
#include "blackberrydeviceconfigurationwizard.h"
 
37
#include "blackberryinstallwizard.h"
 
38
#include "blackberrycertificate.h"
 
39
#include "qnxconstants.h"
 
40
 
 
41
#include <projectexplorer/devicesupport/devicemanager.h>
 
42
 
 
43
#include <QFileInfo>
 
44
#include <QFrame>
 
45
#include <QHBoxLayout>
 
46
#include <QLabel>
 
47
#include <QPushButton>
 
48
#include <QDesktopServices>
 
49
#include <QWizard>
 
50
#include <QUrl>
 
51
#include <QMessageBox>
 
52
 
 
53
namespace Qnx {
 
54
namespace Internal {
 
55
 
 
56
SetupItem::SetupItem(const QString &desc, QWidget *parent)
 
57
: QFrame(parent)
 
58
{
 
59
    m_timer.setSingleShot(true);
 
60
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(validate()));
 
61
 
 
62
    setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
 
63
    QVBoxLayout *frameLayout = new QVBoxLayout(this);
 
64
 
 
65
    QHBoxLayout *childLayout = new QHBoxLayout;
 
66
    frameLayout->addLayout(childLayout);
 
67
 
 
68
    m_icon = new QLabel;
 
69
    childLayout->addWidget(m_icon);
 
70
 
 
71
    m_label = new QLabel;
 
72
    m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
 
73
    childLayout->addWidget(m_label);
 
74
 
 
75
    m_button = new QPushButton;
 
76
    childLayout->addWidget(m_button);
 
77
    QObject::connect(m_button, SIGNAL(clicked()), this, SLOT(onFixPressed()));
 
78
 
 
79
    if (!desc.isEmpty()) {
 
80
        m_desc = new QLabel(desc);
 
81
        m_desc->setWordWrap(true);
 
82
        QFont font = m_desc->font();
 
83
        font.setItalic(true);
 
84
        m_desc->setFont(font);
 
85
        frameLayout->addWidget(m_desc);
 
86
    }
 
87
}
 
88
 
 
89
void SetupItem::set(Status status, const QString &message, const QString &fixText)
 
90
{
 
91
    const char *icon;
 
92
    switch (status) {
 
93
    case Ok:
 
94
        icon = Qnx::Constants::QNX_OK_ICON;
 
95
        break;
 
96
    case Info:
 
97
        icon = Qnx::Constants::QNX_INFO_ICON;
 
98
        break;
 
99
    case Warning:
 
100
        icon = Qnx::Constants::QNX_WARNING_ICON;
 
101
        break;
 
102
    case Error:
 
103
        icon = Qnx::Constants::QNX_ERROR_ICON;
 
104
        break;
 
105
    }
 
106
    m_icon->setPixmap(QPixmap(QLatin1String(icon)));
 
107
    m_label->setText(message);
 
108
    m_button->setVisible(!fixText.isEmpty());
 
109
    m_button->setText(fixText);
 
110
}
 
111
 
 
112
void SetupItem::onFixPressed()
 
113
{
 
114
    fix();
 
115
    validate();
 
116
}
 
117
 
 
118
void SetupItem::validateLater()
 
119
{
 
120
    // BlackBerryConfigurationManager.settingsChanged and DeviceManager.updated signals
 
121
    // may be emitted multiple times during the same event handling. This would result in multiple
 
122
    // validation() calls even through just one is needed.
 
123
    // QTimer allows to merge those multiple signal emits into a single validate() call.
 
124
    m_timer.start();
 
125
}
 
126
 
 
127
APILevelSetupItem::APILevelSetupItem(QWidget *parent)
 
128
: SetupItem(tr("API Level defines kits, Qt versions, compilers, debuggers needed"
 
129
               " for building BlackBerry applications."), parent)
 
130
{
 
131
    validate();
 
132
    connect(BlackBerryConfigurationManager::instance(), SIGNAL(settingsChanged()),
 
133
            this, SLOT(validateLater()));
 
134
}
 
135
 
 
136
void APILevelSetupItem::validate()
 
137
{
 
138
    FoundTypes found = resolvedFoundType();
 
139
    if (!found.testFlag(Any))
 
140
        set(Error, tr("No API Level is installed."), tr("Install"));
 
141
    else if (!found.testFlag(Valid))
 
142
        set(Error, tr("No valid API Level is installed."), tr("Install"));
 
143
    else if (!found.testFlag(Active))
 
144
        set(Error, tr("Valid API Level is not activated."), tr("Activate"));
 
145
    else if (!found.testFlag(V_10_2))
 
146
        set(Warning, tr("Valid API Level 10.2 or newer is not installed."), tr("Install"));
 
147
    else if (!found.testFlag(V_10_2_AS_DEFAULT))
 
148
        set(Warning, tr("Valid API Level 10.2 or newer is not set as default."), tr("Set"));
 
149
    else
 
150
        set(Ok, tr("API Level is configured."));
 
151
    // TODO: should we check something more e.g. BB10 kits are valid?
 
152
}
 
153
 
 
154
void APILevelSetupItem::fix()
 
155
{
 
156
    FoundTypes found = resolvedFoundType();
 
157
    if (!found.testFlag(Any) || !found.testFlag(Valid)) {
 
158
        installAPILevel();
 
159
    } else if (!found.testFlag(Active)) {
 
160
        foreach (BlackBerryApiLevelConfiguration *config,
 
161
                BlackBerryConfigurationManager::instance()->apiLevels()) {
 
162
            if (config->isValid() && !config->isActive()) {
 
163
                config->activate();
 
164
                break;
 
165
            }
 
166
        }
 
167
    } else if (!found.testFlag(V_10_2)) {
 
168
        // TODO: install filter for 10.2 only
 
169
        installAPILevel();
 
170
    } else if (!found.testFlag(V_10_2_AS_DEFAULT)) {
 
171
        BlackBerryConfigurationManager::instance()->setDefaultConfiguration(0);
 
172
    }
 
173
}
 
174
 
 
175
APILevelSetupItem::FoundTypes APILevelSetupItem::resolvedFoundType()
 
176
{
 
177
    FoundTypes found;
 
178
 
 
179
    // TODO: for now, all Trunk versions are understood as 10.2 compliant
 
180
    BlackBerryVersionNumber version_10_2(QLatin1String("10.2.0.0"));
 
181
    foreach (BlackBerryApiLevelConfiguration *config,
 
182
            BlackBerryConfigurationManager::instance()->apiLevels()) {
 
183
        found |= Any;
 
184
        if (config->isValid()) {
 
185
            found |= Valid;
 
186
            if (config->isActive())
 
187
                found |= Active;
 
188
            if (config->version() > version_10_2)
 
189
                found |= V_10_2;
 
190
        }
 
191
    }
 
192
 
 
193
    BlackBerryApiLevelConfiguration *config =
 
194
            BlackBerryConfigurationManager::instance()->defaultApiLevel();
 
195
    if (config && config->version() > version_10_2)
 
196
        found |= V_10_2_AS_DEFAULT;
 
197
 
 
198
    return found;
 
199
}
 
200
 
 
201
void APILevelSetupItem::installAPILevel()
 
202
{
 
203
    BlackBerryInstallWizard wizard(
 
204
            BlackBerryInstallerDataHandler::InstallMode,
 
205
            BlackBerryInstallerDataHandler::ApiLevel, QString(), this);
 
206
    connect(&wizard, SIGNAL(processFinished()), this, SLOT(handleInstallationFinished()));
 
207
    wizard.exec();
 
208
}
 
209
 
 
210
void APILevelSetupItem::handleInstallationFinished()
 
211
{
 
212
    // manually-added API Levels are automatically registered by BlackBerryInstallWizard
 
213
    // auto-detected API Levels needs to reloaded explicitly
 
214
    BlackBerryConfigurationManager::instance()->loadAutoDetectedApiLevels();
 
215
    validate();
 
216
}
 
217
 
 
218
SigningKeysSetupItem::SigningKeysSetupItem(QWidget *parent)
 
219
: SetupItem(tr("Signing keys are needed for signing BlackBerry applications"
 
220
                       " and managing debug tokens."), parent)
 
221
{
 
222
    validate();
 
223
    connect(&BlackBerrySigningUtils::instance(), SIGNAL(defaultCertificateLoaded(int)),
 
224
            this, SLOT(validate()));
 
225
}
 
226
 
 
227
void SigningKeysSetupItem::validate()
 
228
{
 
229
    BlackBerrySigningUtils &utils = BlackBerrySigningUtils::instance();
 
230
    if (utils.hasLegacyKeys())
 
231
        set(Error, tr("Found legacy BlackBerry signing keys."), tr("Update"));
 
232
    else if (!utils.hasRegisteredKeys())
 
233
        set(Error, tr("Cannot find BlackBerry signing keys."), tr("Request"));
 
234
    else if (!QFileInfo(BlackBerryConfigurationManager::instance()->defaultKeystorePath()).exists())
 
235
        set(Error, tr("Cannot find developer certificate."), tr("Create"));
 
236
    else if (utils.defaultCertificateOpeningStatus() != BlackBerrySigningUtils::Opened)
 
237
        set(Info, tr("Developer certificate is not opened."), tr("Open"));
 
238
    else
 
239
        set(Ok, tr("Signing keys are ready to use."));
 
240
}
 
241
 
 
242
void SigningKeysSetupItem::fix()
 
243
{
 
244
    BlackBerrySigningUtils &utils = BlackBerrySigningUtils::instance();
 
245
    if (utils.hasLegacyKeys()) {
 
246
        QDesktopServices::openUrl(QUrl(QLatin1String(Qnx::Constants::QNX_LEGACY_KEYS_URL)));
 
247
    } else if (!utils.hasRegisteredKeys()) {
 
248
        QDesktopServices::openUrl(QUrl(QLatin1String(Qnx::Constants::QNX_REGISTER_KEYS_URL)));
 
249
    } else if (!QFileInfo(BlackBerryConfigurationManager::instance()->defaultKeystorePath()).exists()) {
 
250
        set(Info, tr("Opening certificate..."));
 
251
        utils.createCertificate();
 
252
    } else if (utils.defaultCertificateOpeningStatus() != BlackBerrySigningUtils::Opened) {
 
253
        connect(&utils, SIGNAL(defaultCertificateLoaded(int)), this, SLOT(defaultCertificateLoaded(int)));
 
254
        utils.openDefaultCertificate(this);
 
255
    }
 
256
}
 
257
 
 
258
void SigningKeysSetupItem::defaultCertificateLoaded(int status)
 
259
{
 
260
    BlackBerrySigningUtils &utils = BlackBerrySigningUtils::instance();
 
261
    disconnect(&utils, SIGNAL(defaultCertificateLoaded(int)), this, SLOT(defaultCertificateLoaded(int)));
 
262
    switch (status) {
 
263
    case BlackBerryCertificate::Success:
 
264
        // handled by the connect in ctor already
 
265
        break;
 
266
    case BlackBerryCertificate::WrongPassword:
 
267
        QMessageBox::critical(this, tr("Qt Creator"), tr("Invalid certificate password."));
 
268
        break;
 
269
    case BlackBerryCertificate::Busy:
 
270
    case BlackBerryCertificate::InvalidOutputFormat:
 
271
    case BlackBerryCertificate::Error:
 
272
        QMessageBox::critical(this, tr("Qt Creator"), tr("Error loading certificate."));
 
273
        break;
 
274
    }
 
275
}
 
276
 
 
277
DeviceSetupItem::DeviceSetupItem(QWidget *parent)
 
278
: SetupItem(tr("BlackBerry 10 device or simulator is needed for running BlackBerry applications."),
 
279
            parent)
 
280
{
 
281
    validate();
 
282
    connect(ProjectExplorer::DeviceManager::instance(), SIGNAL(updated()),
 
283
            this, SLOT(validateLater()));
 
284
}
 
285
 
 
286
void DeviceSetupItem::validate()
 
287
{
 
288
    bool found = false;
 
289
    ProjectExplorer::DeviceManager *manager = ProjectExplorer::DeviceManager::instance();
 
290
    for (int i = 0; i < manager->deviceCount(); i ++) {
 
291
        ProjectExplorer::IDevice::ConstPtr device = manager->deviceAt(i);
 
292
        if (device->type() == Constants::QNX_BB_OS_TYPE) {
 
293
            found = true;
 
294
            break;
 
295
        }
 
296
    }
 
297
    if (!found)
 
298
        set(Error, tr("No BlackBerry 10 device or simulator is registered."), tr("Add"));
 
299
    else
 
300
        set(Ok, tr("BlackBerry 10 device or simulator is registered."));
 
301
    // TODO: check for existence of an API Level matching a device?
 
302
}
 
303
 
 
304
void DeviceSetupItem::fix()
 
305
{
 
306
    BlackBerryDeviceConfigurationWizard wizard(this);
 
307
    if (wizard.exec() == QDialog::Accepted)
 
308
        ProjectExplorer::DeviceManager::instance()->addDevice(wizard.device());
 
309
}
 
310
 
 
311
BlackBerrySetupWidget::BlackBerrySetupWidget(QWidget *parent)
 
312
    : QWidget(parent)
 
313
{
 
314
    QVBoxLayout *layout = new QVBoxLayout(this);
 
315
    setLayout(layout);
 
316
 
 
317
    layout->addWidget(new APILevelSetupItem);
 
318
    layout->addWidget(new SigningKeysSetupItem);
 
319
    layout->addWidget(new DeviceSetupItem);
 
320
 
 
321
    layout->addStretch();
 
322
 
 
323
    QLabel *howTo = new QLabel;
 
324
    howTo->setTextFormat(Qt::RichText);
 
325
    howTo->setTextInteractionFlags(Qt::TextBrowserInteraction);
 
326
    howTo->setOpenExternalLinks(true);
 
327
    howTo->setText(tr("<a href=\"%1\">How to Setup Qt Creator for BlackBerry 10 development</a>")
 
328
                   .arg(QLatin1String(Qnx::Constants::QNX_BLACKBERRY_SETUP_URL)));
 
329
    layout->addWidget(howTo);
 
330
}
 
331
 
 
332
} // namespace Internal
 
333
} // namespace Qnx