blackberrydebugtokenrequestdialog.cpp 9.3 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 32 33
** 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 "blackberrydebugtokenrequestdialog.h"
#include "blackberrydebugtokenrequester.h"
34
#include "blackberrydeviceinformation.h"
35
#include "blackberryconfigurationmanager.h"
36
#include "blackberrysigningutils.h"
37 38 39 40 41 42 43 44 45 46 47 48 49
#include "ui_blackberrydebugtokenrequestdialog.h"

#include <QPushButton>
#include <QDir>
#include <QMessageBox>

namespace Qnx {
namespace Internal {

BlackBerryDebugTokenRequestDialog::BlackBerryDebugTokenRequestDialog(
        QWidget *parent, Qt::WindowFlags f) :
    QDialog(parent, f),
    m_ui(new Ui_BlackBerryDebugTokenRequestDialog),
50
    m_requester(new BlackBerryDebugTokenRequester(this)),
51 52
    m_deviceInfo(new BlackBerryDeviceInformation(this)),
    m_utils(BlackBerrySigningUtils::instance())
53 54 55 56
{
    m_ui->setupUi(this);
    m_ui->progressBar->hide();
    m_ui->status->clear();
57
    m_ui->debugTokenPath->setExpectedKind(Utils::PathChooser::SaveFile);
58 59 60 61 62 63 64 65 66 67 68 69 70
    m_ui->debugTokenPath->setPromptDialogTitle(tr("Request Debug Token"));
    m_ui->debugTokenPath->setPromptDialogFilter(tr("BAR Files (*.bar)"));

    m_cancelButton = m_ui->buttonBox->button(QDialogButtonBox::Cancel);
    m_okButton = m_ui->buttonBox->button(QDialogButtonBox::Ok);
    m_okButton->setEnabled(false);

    connect(m_cancelButton, SIGNAL(clicked()),
            this, SLOT(reject()));
    connect(m_okButton, SIGNAL(clicked()),
            this, SLOT(requestDebugToken()));
    connect(m_ui->debugTokenPath, SIGNAL(changed(QString)),
            this, SLOT(validate()));
71 72
    connect(m_ui->debugTokenPath, SIGNAL(beforeBrowsing()),
            this, SLOT(setDefaultPath()));
73 74
    connect(m_ui->debugTokenPath, SIGNAL(editingFinished()),
            this, SLOT(appendExtension()));
75 76
    connect(m_ui->debugTokenPath, SIGNAL(editingFinished()),
            this, SLOT(expandPath()));
77 78 79 80
    connect(m_ui->devicePin, SIGNAL(textChanged(QString)),
            this, SLOT(validate()));
    connect(m_requester, SIGNAL(finished(int)),
            this, SLOT(debugTokenArrived(int)));
81 82 83 84 85 86 87
    connect(m_deviceInfo, SIGNAL(finished(int)),
            this, SLOT(setDevicePin(int)));
}

BlackBerryDebugTokenRequestDialog::~BlackBerryDebugTokenRequestDialog()
{
    delete m_ui;
88 89 90 91 92 93 94
}

QString BlackBerryDebugTokenRequestDialog::debugToken() const
{
    return m_ui->debugTokenPath->path();
}

95 96 97 98 99
void BlackBerryDebugTokenRequestDialog::setDevicePin(const QString &devicePin)
{
    m_ui->devicePin->setText(devicePin);
}

100 101 102 103 104 105
void BlackBerryDebugTokenRequestDialog::setTargetDetails(const QString &deviceIp, const QString &password)
{
    m_ui->devicePin->setPlaceholderText(tr("Requesting Device PIN..."));
    m_deviceInfo->setDeviceTarget(deviceIp, password);
}

106 107
void BlackBerryDebugTokenRequestDialog::validate()
{
108
    if (!m_ui->debugTokenPath->isValid() || m_ui->devicePin->text().isEmpty()) {
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
        m_okButton->setEnabled(false);
        return;
    }

    QFileInfo fileInfo(m_ui->debugTokenPath->path());

    if (!fileInfo.dir().exists()) {
        m_ui->status->setText(tr("Base directory does not exist."));
        m_okButton->setEnabled(false);
        return;
    }

    m_ui->status->clear();
    m_okButton->setEnabled(true);
}

void BlackBerryDebugTokenRequestDialog::requestDebugToken()
{
    setBusy(true);

    QFile file(m_ui->debugTokenPath->path());

    if (file.exists()) {
        const int result = QMessageBox::question(this, tr("Are you sure?"),
                tr("The file '%1' will be overwritten. Do you want to proceed?")
                .arg(file.fileName()), QMessageBox::Yes | QMessageBox::No);

        if (result & QMessageBox::Yes) {
            file.remove();
        } else {
            setBusy(false);
            return;
        }
    }

144 145
    BlackBerryConfigurationManager &configuration = BlackBerryConfigurationManager::instance();

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    bool ok;
    const QString cskPassword = m_utils.cskPassword(this, &ok);

    if (!ok) {
        setBusy(false);
        return;
    }

    const QString certificatePassword = m_utils.certificatePassword(this, &ok);

    if (!ok) {
        setBusy(false);
        return;
    }

161
    m_requester->requestDebugToken(m_ui->debugTokenPath->path(),
162 163
            cskPassword, configuration.defaultKeystorePath(),
            certificatePassword, m_ui->devicePin->text());
164 165
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
void BlackBerryDebugTokenRequestDialog::setDefaultPath()
{
    const QString path = m_ui->debugTokenPath->path();
    const QString defaultFileName = QLatin1String("/debugToken.bar");

    if (path.isEmpty()) {
        m_ui->debugTokenPath->setPath(QDir::homePath() + defaultFileName);
        return;
    }

    const QFileInfo fileInfo(path);

    if (fileInfo.isDir())
        m_ui->debugTokenPath->setPath(path + defaultFileName);
}

182 183 184 185
void BlackBerryDebugTokenRequestDialog::appendExtension()
{
    QString path = m_ui->debugTokenPath->path();

186 187 188
    if (path.isEmpty())
        return;

189 190 191 192 193 194
    if (!path.endsWith(QLatin1String(".bar"))) {
        path += QLatin1String(".bar");
        m_ui->debugTokenPath->setPath(path);
    }
}

195 196 197 198
void BlackBerryDebugTokenRequestDialog::expandPath()
{
    const QString path = m_ui->debugTokenPath->path();

199
    if (path.isEmpty() || path.startsWith(QLatin1Char('/')))
200 201 202 203 204 205 206
            return;

    const QFileInfo fileInfo(path);

    m_ui->debugTokenPath->setPath(fileInfo.absoluteFilePath());
}

207 208
void BlackBerryDebugTokenRequestDialog::debugTokenArrived(int status)
{
209
    QString errorString = tr("Failed to request debug token:") + QLatin1Char(' ');
210 211 212 213 214 215

    switch (status) {
    case BlackBerryDebugTokenRequester::Success:
        accept();
        return;
    case BlackBerryDebugTokenRequester::WrongCskPassword:
216
        m_utils.clearCskPassword();
217 218 219
        errorString += tr("Wrong CSK password.");
        break;
    case BlackBerryDebugTokenRequester::WrongKeystorePassword:
220
        m_utils.clearCertificatePassword();
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        errorString += tr("Wrong keystore password.");
        break;
    case BlackBerryDebugTokenRequester::NetworkUnreachable:
        errorString += tr("Network unreachable.");
        break;
    case BlackBerryDebugTokenRequester::IllegalPin:
        errorString += tr("Illegal device PIN.");
        break;
    case BlackBerryDebugTokenRequester::FailedToStartInferiorProcess:
        errorString += tr("Failed to start inferior process.");
        break;
    case BlackBerryDebugTokenRequester::InferiorProcessTimedOut:
        errorString += tr("Inferior processes timed out.");
        break;
    case BlackBerryDebugTokenRequester::InferiorProcessCrashed:
        errorString += tr("Inferior process has crashed.");
        break;
    case BlackBerryDebugTokenRequester::InferiorProcessReadError:
    case BlackBerryDebugTokenRequester::InferiorProcessWriteError:
        errorString += tr("Failed to communicate with the inferior process.");
        break;
242 243 244
    case BlackBerryDebugTokenRequester::NotYetRegistered:
        errorString += tr("Not yet registered to request debug tokens.");
        break;
245
    case BlackBerryDebugTokenRequester::UnknownError:
246 247 248
    default:
        m_utils.clearCertificatePassword();
        m_utils.clearCskPassword();
249 250 251 252
        errorString += tr("An unknwon error has occurred.");
        break;
    }

253 254 255 256 257
    QFile file(m_ui->debugTokenPath->path());

    if (file.exists())
        file.remove();

258 259 260 261 262
    QMessageBox::critical(this, tr("Error"), errorString);

    setBusy(false);
}

263 264 265 266 267 268 269 270 271 272 273 274 275
void BlackBerryDebugTokenRequestDialog::setDevicePin(int status)
{
    m_ui->devicePin->setPlaceholderText(QString());
    if (status != BlackBerryDeviceInformation::Success)
        return;

    const QString devicePin = m_deviceInfo->devicePin();
    if (devicePin.isEmpty())
        return;

    m_ui->devicePin->setText(devicePin);
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
void BlackBerryDebugTokenRequestDialog::setBusy(bool busy)
{
    m_okButton->setEnabled(!busy);
    m_cancelButton->setEnabled(!busy);
    m_ui->debugTokenPath->setEnabled(!busy);
    m_ui->devicePin->setEnabled(!busy);
    m_ui->progressBar->setVisible(busy);

    if (busy)
        m_ui->status->setText(tr("Requesting debug token..."));
    else
        m_ui->status->clear();
}

}
} // namespace Qnx