blackberryndksettingswidget.cpp 12.5 KB
Newer Older
1 2
/**************************************************************************
**
3
** Copyright (C) 2013 BlackBerry Limited. All rights reserved.
4
**
5
** Contact: BlackBerry (qt@blackberry.com)
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
** 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.
**
****************************************************************************/

32 33
#include "blackberryndksettingswidget.h"
#include "ui_blackberryndksettingswidget.h"
34
#include "qnxutils.h"
35
#include "blackberrysigningutils.h"
36
#include "blackberrysetupwizard.h"
37

38 39 40
#include "blackberryconfigurationmanager.h"
#include "blackberryconfiguration.h"

41 42 43 44 45
#include <utils/pathchooser.h>

#include <coreplugin/icore.h>

#include <QMessageBox>
46 47 48 49
#include <QFileDialog>

#include <QStandardItemModel>
#include <QTreeWidgetItem>
50 51 52 53

namespace Qnx {
namespace Internal {

54
BlackBerryNDKSettingsWidget::BlackBerryNDKSettingsWidget(QWidget *parent) :
55
    QWidget(parent),
56 57 58
    m_ui(new Ui_BlackBerryNDKSettingsWidget),
    m_autoDetectedNdks(0),
    m_manualNdks(0)
59
{
60
    m_bbConfigManager = &BlackBerryConfigurationManager::instance();
61
    m_ui->setupUi(this);
62

63 64 65 66
    m_ui->activateNdkTargetButton->setEnabled(false);
    m_ui->deactivateNdkTargetButton->setEnabled(false);

    m_activatedTargets << m_bbConfigManager->activeConfigurations();
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    m_ui->ndksTreeWidget->header()->setResizeMode(QHeaderView::Stretch);
    m_ui->ndksTreeWidget->header()->setStretchLastSection(false);
    m_ui->ndksTreeWidget->setHeaderItem(new QTreeWidgetItem(QStringList() << tr("NDK") << tr("NDK Environment File")));
    m_ui->ndksTreeWidget->setTextElideMode(Qt::ElideNone);
    m_ui->ndksTreeWidget->setColumnCount(2);
    m_autoDetectedNdks = new QTreeWidgetItem(m_ui->ndksTreeWidget);
    m_autoDetectedNdks->setText(0, tr("Auto-Detected"));
    m_autoDetectedNdks->setFirstColumnSpanned(true);
    m_autoDetectedNdks->setFlags(Qt::ItemIsEnabled);
    m_manualNdks = new QTreeWidgetItem(m_ui->ndksTreeWidget);
    m_manualNdks->setText(0, tr("Manual"));
    m_manualNdks->setFirstColumnSpanned(true);
    m_manualNdks->setFlags(Qt::ItemIsEnabled);

    m_ui->ndksTreeWidget->expandAll();

    updateNdkList();
85

86
    connect(m_ui->wizardButton, SIGNAL(clicked()), this, SLOT(launchBlackBerrySetupWizard()));
87 88
    connect(m_ui->addNdkButton, SIGNAL(clicked()), this, SLOT(addNdkTarget()));
    connect(m_ui->removeNdkButton, SIGNAL(clicked()), this, SLOT(removeNdkTarget()));
89 90
    connect(m_ui->activateNdkTargetButton, SIGNAL(clicked()), this, SLOT(activateNdkTarget()));
    connect(m_ui->deactivateNdkTargetButton, SIGNAL(clicked()), this, SLOT(deactivateNdkTarget()));
91
    connect(m_ui->ndksTreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(updateInfoTable(QTreeWidgetItem*)));
92 93 94 95 96 97 98 99
}

void BlackBerryNDKSettingsWidget::setWizardMessageVisible(bool visible)
{
    m_ui->wizardLabel->setVisible(visible);
    m_ui->wizardButton->setVisible(visible);
}

100
bool BlackBerryNDKSettingsWidget::hasActiveNdk() const
101
{
102
    return !m_bbConfigManager->configurations().isEmpty();
103 104
}

105 106 107 108 109 110 111 112 113 114
QList<BlackBerryConfiguration *> BlackBerryNDKSettingsWidget::activatedTargets()
{
    return m_activatedTargets;
}

QList<BlackBerryConfiguration *> BlackBerryNDKSettingsWidget::deactivatedTargets()
{
    return m_deactivatedTargets;
}

115 116
void BlackBerryNDKSettingsWidget::launchBlackBerrySetupWizard() const
{
117 118
    BlackBerrySigningUtils &blackBerryUtils = BlackBerrySigningUtils::instance();
    const bool alreadyConfigured = blackBerryUtils.hasRegisteredKeys() && blackBerryUtils.hasDefaultCertificate();
119 120 121 122 123 124 125

    if (alreadyConfigured) {
        QMessageBox::information(0, tr("Qt Creator"),
            tr("It appears that your BlackBerry environment has already been configured."));
            return;
    }

126
    BlackBerrySetupWizard wizard(Core::ICore::mainWindow());
127
    connect(&wizard, SIGNAL(ndkTargetsUpdated()), this, SLOT(updateNdkList()));
128 129 130
    wizard.exec();
}

131
void BlackBerryNDKSettingsWidget::updateInfoTable(QTreeWidgetItem* currentItem)
132
{
133 134 135 136
    if (!currentItem)
        return;

    QString envFilePath = currentItem->text(1);
137
    if (envFilePath.isEmpty()) {
138
        m_ui->removeNdkButton->setEnabled(false);
139 140
        m_ui->activateNdkTargetButton->setEnabled(false);
        m_ui->deactivateNdkTargetButton->setEnabled(false);
141
        return;
142 143
    }

144
    BlackBerryConfiguration *config = m_bbConfigManager->configurationFromEnvFile(Utils::FileName::fromString(envFilePath));
145 146 147
    if (!config)
        return;

148 149 150 151 152 153 154 155 156 157 158 159
    m_ui->baseNameLabel->setText(config->displayName());
    m_ui->ndkPathLabel->setText(QDir::toNativeSeparators(config->ndkPath()));
    m_ui->hostLabel->setText(QDir::toNativeSeparators(config->qnxHost()));
    m_ui->targetLabel->setText(QDir::toNativeSeparators(config->sysRoot().toString()));
    m_ui->versionLabel->clear();
    // TODO: Add a versionNumber attribute for the BlackBerryConfiguration class
    if (config->isAutoDetected()) {
        foreach (const NdkInstallInformation &ndkInfo, QnxUtils::installedNdks()) {
            if (ndkInfo.name == config->displayName()) {
                m_ui->versionLabel->setText(ndkInfo.version);
                break;
            }
160 161
        }
    }
162

163
    updateUi(currentItem, config);
164 165 166 167
}

void BlackBerryNDKSettingsWidget::updateNdkList()
{
168 169 170
    qDeleteAll(m_autoDetectedNdks->takeChildren());
    qDeleteAll(m_manualNdks->takeChildren());

171 172 173 174
    foreach (BlackBerryConfiguration *config, m_bbConfigManager->configurations()) {
        QTreeWidgetItem *parent = config->isAutoDetected() ? m_autoDetectedNdks : m_manualNdks;
        QTreeWidgetItem *item = new QTreeWidgetItem(parent);
        item->setText(0, config->displayName());
175
        item->setText(1, config->ndkEnvFile().toString());
176
        QFont font;
177
        font.setBold(config->isActive() || m_activatedTargets.contains(config));
178 179
        item->setFont(0, font);
        item->setFont(1, font);
180 181 182 183 184 185
    }

    if (m_autoDetectedNdks->child(0)) {
        m_autoDetectedNdks->child(0)->setSelected(true);
        updateInfoTable(m_autoDetectedNdks->child(0));
    }
186 187
}

188
void BlackBerryNDKSettingsWidget::addNdkTarget()
189
{
190
    launchBlackBerryInstallerWizard(BlackBerryInstallerDataHandler::InstallMode);
191
    emit targetsUpdated();
192 193
}

194
void BlackBerryNDKSettingsWidget::removeNdkTarget()
195
{
196 197 198
    if (!m_ui->ndksTreeWidget->currentItem())
        return;

199 200
    QString ndk = m_ui->ndksTreeWidget->currentItem()->text(0);
    QString envFilePath = m_ui->ndksTreeWidget->currentItem()->text(1);
201 202 203 204 205 206 207

    BlackBerryConfiguration *config = m_bbConfigManager->configurationFromEnvFile(Utils::FileName::fromString(envFilePath));
    if (!config)
        return;

    if (config->isAutoDetected()) {
        uninstallNdkTarget();
208
        emit targetsUpdated();
209 210 211
        return;
    }

212 213 214
    QMessageBox::StandardButton button =
            QMessageBox::question(Core::ICore::mainWindow(),
                                  tr("Clean BlackBerry 10 Configuration"),
215
                                  tr("Are you sure you want to remove:\n %1?").arg(ndk),
216 217
                                  QMessageBox::Yes | QMessageBox::No);

218
    if (button == QMessageBox::Yes) {
219 220
        m_activatedTargets.removeOne(config);
        m_deactivatedTargets.removeOne(config);
221 222
        m_bbConfigManager->removeConfiguration(config);
        m_manualNdks->removeChild(m_ui->ndksTreeWidget->currentItem());
223
        emit targetsUpdated();
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    }
}

void BlackBerryNDKSettingsWidget::activateNdkTarget()
{
    if (!m_ui->ndksTreeWidget->currentItem())
        return;

    QString envFilePath = m_ui->ndksTreeWidget->currentItem()->text(1);

    BlackBerryConfiguration *config = m_bbConfigManager->configurationFromEnvFile(Utils::FileName::fromString(envFilePath));
    if (config && !m_activatedTargets.contains(config)) {
        m_activatedTargets << config;
        if (m_deactivatedTargets.contains(config))
           m_deactivatedTargets.removeAt(m_deactivatedTargets.indexOf(config));

        updateUi(m_ui->ndksTreeWidget->currentItem(), config);
241
        emit targetsUpdated();
242
    }
243 244 245 246 247 248 249 250
}

void BlackBerryNDKSettingsWidget::deactivateNdkTarget()
{
    if (!m_ui->ndksTreeWidget->currentItem())
        return;

    QString envFilePath = m_ui->ndksTreeWidget->currentItem()->text(1);
251

252 253 254 255 256
    BlackBerryConfiguration *config = m_bbConfigManager->configurationFromEnvFile(Utils::FileName::fromString(envFilePath));
    if (config && m_activatedTargets.contains(config)) {
        m_deactivatedTargets << config;
        m_activatedTargets.removeAt(m_activatedTargets.indexOf(config));
        updateUi(m_ui->ndksTreeWidget->currentItem(), config);
257
        emit targetsUpdated();
258 259 260 261 262 263 264 265 266 267 268 269 270
    }
}

void BlackBerryNDKSettingsWidget::updateUi(QTreeWidgetItem *item, BlackBerryConfiguration *config)
{
    if (!item || !config)
        return;

    QFont font;
    font.setBold(m_activatedTargets.contains(config));
    item->setFont(0, font);
    item->setFont(1, font);

271 272 273
    m_ui->activateNdkTargetButton->setEnabled(!m_activatedTargets.contains(config));
    m_ui->deactivateNdkTargetButton->setEnabled(m_activatedTargets.contains(config)
                                                && m_activatedTargets.size() > 1);
274 275 276
    // Disable remove button for auto detected pre-10.2 NDKs (uninstall wizard doesn't handle them)
    m_ui->removeNdkButton->setEnabled(!(config->isAutoDetected()
                                            && QnxUtils::sdkInstallerPath(config->ndkPath()).isEmpty()));
277 278
}

279
void BlackBerryNDKSettingsWidget::uninstallNdkTarget()
280
{
281 282 283 284
    const QMessageBox::StandardButton answer = QMessageBox::question(this, tr("Confirmation"),
                                                                     tr("Are you sure you want to uninstall %1?").
                                                                     arg(m_ui->baseNameLabel->text()),
                                                                     QMessageBox::Yes | QMessageBox::No);
285

286 287 288
    if (answer == QMessageBox::Yes)
        launchBlackBerryInstallerWizard(BlackBerryInstallerDataHandler::UninstallMode, m_ui->versionLabel->text());
}
289

290 291 292
void BlackBerryNDKSettingsWidget::handleInstallationFinished()
{
    m_bbConfigManager->loadAutoDetectedConfigurations();
293
    updateNdkList();
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
void BlackBerryNDKSettingsWidget::handleUninstallationFinished()
{
    if (!m_ui->ndksTreeWidget->currentItem())
        return;

    QString targetName = m_ui->ndksTreeWidget->currentItem()->text(0);
    QString envFilePath = m_ui->ndksTreeWidget->currentItem()->text(1);
    // Check if the target is corrrecly uninstalled
    foreach (const NdkInstallInformation &ndk, QnxUtils::installedNdks()) {
        if (ndk.name == targetName)
            return;
    }

    BlackBerryConfiguration *config = m_bbConfigManager->configurationFromEnvFile(Utils::FileName::fromString(envFilePath));
    if (m_activatedTargets.contains(config))
        m_activatedTargets.removeAt(m_activatedTargets.indexOf(config));
    else if (m_deactivatedTargets.contains(config))
        m_deactivatedTargets.removeAt(m_deactivatedTargets.indexOf(config));

    m_bbConfigManager->removeConfiguration(config);

    updateNdkList();
}

void BlackBerryNDKSettingsWidget::launchBlackBerryInstallerWizard(BlackBerryInstallerDataHandler::Mode mode,
                                                                  const QString& targetVersion)
{
    BlackBerryInstallWizard wizard(mode, targetVersion, this);
    if (mode == BlackBerryInstallerDataHandler::InstallMode)
        connect(&wizard, SIGNAL(processFinished()), this, SLOT(handleInstallationFinished()));
    else
        connect(&wizard, SIGNAL(processFinished()), this, SLOT(handleUninstallationFinished()));

    wizard.exec();
}

332 333 334
} // namespace Internal
} // namespace Qnx