s60devicerunconfiguration.cpp 34.5 KB
Newer Older
con's avatar
con committed
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
28
29
**
**************************************************************************/

30
#include "s60devicerunconfiguration.h"
31
#include "s60devicerunconfigurationwidget.h"
32
33
34
35
36
#include "qt4project.h"
#include "qtversionmanager.h"
#include "profilereader.h"
#include "s60manager.h"
#include "s60devices.h"
37
38
#include "s60runconfigbluetoothstarter.h"
#include "bluetoothlistener_gui.h"
39
#include "serialdevicelister.h"
40
#include "qt4buildconfiguration.h"
41
42
43

#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
44
#include <coreplugin/progressmanager/progressmanager.h>
45
#include <utils/qtcassert.h>
con's avatar
con committed
46
#include <utils/pathchooser.h>
47
#include <projectexplorer/projectexplorerconstants.h>
48
#include <projectexplorer/persistentsettings.h>
49
#include <projectexplorer/project.h>
con's avatar
con committed
50
#include <projectexplorer/buildconfiguration.h>
51

52
53
#include <debugger/debuggermanager.h>

54
55
56
#include <QtGui/QMessageBox>
#include <QtGui/QMainWindow>

57
using namespace ProjectExplorer;
dt's avatar
dt committed
58
using namespace Qt4ProjectManager;
59
60
using namespace Qt4ProjectManager::Internal;

61
62
enum { debug = 0 };

63
64
65
66
67
68
69
static const int    PROGRESS_PACKAGECREATED = 100;
static const int    PROGRESS_PACKAGESIGNED = 200;
static const int    PROGRESS_DEPLOYBASE = 200;
static const int    PROGRESS_PACKAGEDEPLOYED = 300;
static const int    PROGRESS_PACKAGEINSTALLED = 400;
static const int    PROGRESS_MAX = 400;

70
71
72
73
74
75
76
77
78
// Format information about a file
static QString lsFile(const QString &f)
{
    QString rc;
    const QFileInfo fi(f);
    QTextStream str(&rc);
    str << fi.size() << ' ' << fi.lastModified().toString(Qt::ISODate) << ' ' << QDir::toNativeSeparators(fi.absoluteFilePath());
    return rc;
}
79

80
81
82
83
// ======== S60DeviceRunConfiguration
S60DeviceRunConfiguration::S60DeviceRunConfiguration(Project *project, const QString &proFilePath)
    : RunConfiguration(project),
    m_proFilePath(proFilePath),
con's avatar
con committed
84
    m_cachedTargetInformationValid(false),
85
86
87
88
89
90
91
#ifdef Q_OS_WIN
    m_serialPortName(QLatin1String("COM5")),
    m_communicationType(SerialPortCommunication),
#else
    m_serialPortName(QLatin1String(SerialDeviceLister::linuxBlueToothDeviceRootC) + QLatin1Char('0')),
    m_communicationType(BlueToothCommunication),
#endif
con's avatar
con committed
92
    m_signingMode(SignSelf)
93
94
{
    if (!m_proFilePath.isEmpty())
95
        setDisplayName(tr("%1 on Symbian Device").arg(QFileInfo(m_proFilePath).completeBaseName()));
96
    else
97
        setDisplayName(tr("QtS60DeviceRunConfiguration"));
98

99
100
    connect(project, SIGNAL(targetInformationChanged()),
            this, SLOT(invalidateCachedTargetInformation()));
101
102
103

    connect(project, SIGNAL(proFileUpdated(Qt4ProjectManager::Internal::Qt4ProFileNode*)),
            this, SLOT(proFileUpdate(Qt4ProjectManager::Internal::Qt4ProFileNode*)));
104
105
}

106
107
108
109
110
111
112
void S60DeviceRunConfiguration::proFileUpdate(Qt4ProjectManager::Internal::Qt4ProFileNode *pro)
{
    if (m_proFilePath == pro->path())
        invalidateCachedTargetInformation();
}


113
114
115
116
S60DeviceRunConfiguration::~S60DeviceRunConfiguration()
{
}

dt's avatar
dt committed
117
118
119
120
121
Qt4Project *S60DeviceRunConfiguration::qt4Project() const
{
    return static_cast<Qt4Project *>(project());
}

122
QString S60DeviceRunConfiguration::id() const
123
{
124
125
126
    return QLatin1String("Qt4ProjectManager.DeviceRunConfiguration");
}

127
128
129
ProjectExplorer::ToolChain::ToolChainType S60DeviceRunConfiguration::toolChainType(
        ProjectExplorer::BuildConfiguration *configuration) const
{
130
131
    if (Qt4BuildConfiguration *bc = qobject_cast<Qt4BuildConfiguration *>(configuration))
        return bc->toolChainType();
132
133
134
    return ProjectExplorer::ToolChain::INVALID;
}

135
136
ProjectExplorer::ToolChain::ToolChainType S60DeviceRunConfiguration::toolChainType() const
{
137
138
    if (Qt4BuildConfiguration *bc = qobject_cast<Qt4BuildConfiguration *>(project()->activeBuildConfiguration()))
        return bc->toolChainType();
139
    return ProjectExplorer::ToolChain::INVALID;
140
141
}

142
bool S60DeviceRunConfiguration::isEnabled(ProjectExplorer::BuildConfiguration *configuration) const
143
{
144
145
    Qt4BuildConfiguration *qt4bc = static_cast<Qt4BuildConfiguration *>(configuration);
    const ToolChain::ToolChainType type = qt4bc->toolChainType();
con's avatar
con committed
146
    return type == ToolChain::GCCE || type == ToolChain::RVCT_ARMV5 || type == ToolChain::RVCT_ARMV6;
147
148
149
150
151
152
153
154
155
156
157
}

QWidget *S60DeviceRunConfiguration::configurationWidget()
{
    return new S60DeviceRunConfigurationWidget(this);
}

void S60DeviceRunConfiguration::save(PersistentSettingsWriter &writer) const
{
    const QDir projectDir = QFileInfo(project()->file()->fileName()).absoluteDir();
    writer.saveValue("ProFile", projectDir.relativeFilePath(m_proFilePath));
con's avatar
con committed
158
159
160
    writer.saveValue("SigningMode", (int)m_signingMode);
    writer.saveValue("CustomSignaturePath", m_customSignaturePath);
    writer.saveValue("CustomKeyPath", m_customKeyPath);
con's avatar
con committed
161
    writer.saveValue("SerialPortName", m_serialPortName);
162
    writer.saveValue("CommunicationType", m_communicationType);
163
164
165
166
167
168
169
170
    RunConfiguration::save(writer);
}

void S60DeviceRunConfiguration::restore(const PersistentSettingsReader &reader)
{
    RunConfiguration::restore(reader);
    const QDir projectDir = QFileInfo(project()->file()->fileName()).absoluteDir();
    m_proFilePath = projectDir.filePath(reader.restoreValue("ProFile").toString());
con's avatar
con committed
171
172
173
    m_signingMode = (SigningMode)reader.restoreValue("SigningMode").toInt();
    m_customSignaturePath = reader.restoreValue("CustomSignaturePath").toString();
    m_customKeyPath = reader.restoreValue("CustomKeyPath").toString();
con's avatar
con committed
174
    m_serialPortName = reader.restoreValue("SerialPortName").toString().trimmed();
175
    m_communicationType = reader.restoreValue("CommunicationType").toInt();
con's avatar
con committed
176
177
178
179
180
181
182
183
184
185
}

QString S60DeviceRunConfiguration::serialPortName() const
{
    return m_serialPortName;
}

void S60DeviceRunConfiguration::setSerialPortName(const QString &name)
{
    m_serialPortName = name.trimmed();
186
187
}

188
189
190
191
192
193
194
195
196
197
int S60DeviceRunConfiguration::communicationType() const
{
    return m_communicationType;
}

void S60DeviceRunConfiguration::setCommunicationType(int t)
{
    m_communicationType = t;
}

198
199
200
201
202
203
QString S60DeviceRunConfiguration::targetName() const
{
    const_cast<S60DeviceRunConfiguration *>(this)->updateTarget();
    return m_targetName;
}

con's avatar
con committed
204
QString S60DeviceRunConfiguration::basePackageFilePath() const
205
206
{
    const_cast<S60DeviceRunConfiguration *>(this)->updateTarget();
con's avatar
con committed
207
    return m_baseFileName;
208
209
}

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
QString S60DeviceRunConfiguration::symbianPlatform() const
{
    const_cast<S60DeviceRunConfiguration *>(this)->updateTarget();
    return m_platform;
}

QString S60DeviceRunConfiguration::symbianTarget() const
{
    const_cast<S60DeviceRunConfiguration *>(this)->updateTarget();
    return m_target;
}

QString S60DeviceRunConfiguration::packageTemplateFileName() const
{
    const_cast<S60DeviceRunConfiguration *>(this)->updateTarget();
    return m_packageTemplateFileName;
}

con's avatar
con committed
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
S60DeviceRunConfiguration::SigningMode S60DeviceRunConfiguration::signingMode() const
{
    return m_signingMode;
}

void S60DeviceRunConfiguration::setSigningMode(SigningMode mode)
{
    m_signingMode = mode;
}

QString S60DeviceRunConfiguration::customSignaturePath() const
{
    return m_customSignaturePath;
}

void S60DeviceRunConfiguration::setCustomSignaturePath(const QString &path)
{
    m_customSignaturePath = path;
}

QString S60DeviceRunConfiguration::customKeyPath() const
{
    return m_customKeyPath;
}

void S60DeviceRunConfiguration::setCustomKeyPath(const QString &path)
{
    m_customKeyPath = path;
}

258
259
260
261
262
263
264
265
QString S60DeviceRunConfiguration::packageFileName() const
{
    QString rc = basePackageFilePath();
    if (!rc.isEmpty())
        rc += QLatin1String(".pkg");
    return rc;
}

266
QString S60DeviceRunConfiguration::localExecutableFileName() const
267
{
268
269
    Qt4BuildConfiguration *qt4bc = qobject_cast<Qt4BuildConfiguration *>(project()->activeBuildConfiguration());
    S60Devices::Device device = S60Manager::instance()->deviceForQtVersion(qt4bc->qtVersion());
270
271
272
273
274
275

    QString localExecutable = device.epocRoot;
    localExecutable += QString::fromLatin1("/epoc32/release/%1/%2/%3.exe")
                       .arg(symbianPlatform()).arg(symbianTarget()).arg(targetName());
    qDebug() << localExecutable;
    return QDir::toNativeSeparators(localExecutable);
276
277
}

278
279
280
281
void S60DeviceRunConfiguration::updateTarget()
{
    if (m_cachedTargetInformationValid)
        return;
282
283
284
285
286
287
    Qt4TargetInformation info = qt4Project()->targetInformation(qt4Project()->activeQt4BuildConfiguration(),
                                                                m_proFilePath);
    if (info.error != Qt4TargetInformation::NoError) {
        if (info.error == Qt4TargetInformation::ProParserError) {
            Core::ICore::instance()->messageManager()->printToOutputPane(
                    tr("Could not parse %1. The QtS60 Device run configuration %2 can not be started.")
288
                    .arg(m_proFilePath).arg(displayName()));
289
290
        }
        m_targetName = QString::null;
con's avatar
con committed
291
        m_baseFileName = QString::null;
292
293
        m_packageTemplateFileName = QString::null;
        m_platform = QString::null;
294
295
296
297
        m_cachedTargetInformationValid = true;
        emit targetInformationChanged();
        return;
    }
298

299
    m_targetName = info.target;
dt's avatar
dt committed
300

301
    m_baseFileName = info.workingDir + QLatin1Char('/') + m_targetName;
302
    m_packageTemplateFileName = m_baseFileName + QLatin1String("_template.pkg");
303

304
    Qt4BuildConfiguration *qt4bc = qt4Project()->activeQt4BuildConfiguration();
305
    switch (qt4bc->toolChainType()) {
306
307
    case ToolChain::GCCE:
    case ToolChain::GCCE_GNUPOC:
308
        m_platform = QLatin1String("gcce");
309
310
        break;
    case ToolChain::RVCT_ARMV5:
311
        m_platform = QLatin1String("armv5");
312
313
        break;
    default: // including ToolChain::RVCT_ARMV6_GNUPOC:
314
        m_platform = QLatin1String("armv6");
315
316
        break;
    }
317
    if (qt4bc->qmakeBuildConfiguration() & QtVersion::DebugBuild)
318
        m_target = QLatin1String("udeb");
con's avatar
con committed
319
    else
320
321
        m_target = QLatin1String("urel");
    m_baseFileName += QLatin1Char('_') + m_platform + QLatin1Char('_') + m_target;
322

323
324
325
326
327
328
329
330
331
332
    m_cachedTargetInformationValid = true;
    emit targetInformationChanged();
}

void S60DeviceRunConfiguration::invalidateCachedTargetInformation()
{
    m_cachedTargetInformationValid = false;
    emit targetInformationChanged();
}

con's avatar
con committed
333

334
335
336
337
338
339
340
341
342
343
344
// ======== S60DeviceRunConfigurationFactory

S60DeviceRunConfigurationFactory::S60DeviceRunConfigurationFactory(QObject *parent)
    : IRunConfigurationFactory(parent)
{
}

S60DeviceRunConfigurationFactory::~S60DeviceRunConfigurationFactory()
{
}

345
bool S60DeviceRunConfigurationFactory::canRestore(const QString &id) const
346
{
347
    return id == "Qt4ProjectManager.DeviceRunConfiguration";
348
349
}

350
QStringList S60DeviceRunConfigurationFactory::availableCreationIds(Project *pro) const
351
352
353
354
355
356
{
    Qt4Project *qt4project = qobject_cast<Qt4Project *>(pro);
    if (qt4project) {
        QStringList applicationProFiles;
        QList<Qt4ProFileNode *> list = qt4project->applicationProFiles();
        foreach (Qt4ProFileNode * node, list) {
357
            applicationProFiles.append("QtSymbianDeviceRunConfiguration." + node->path());
358
359
360
361
362
363
364
        }
        return applicationProFiles;
    } else {
        return QStringList();
    }
}

365
QString S60DeviceRunConfigurationFactory::displayNameForId(const QString &id) const
366
{
367
    QString fileName = id.mid(QString("QtSymbianDeviceRunConfiguration.").size());
368
    return tr("%1 on Symbian Device").arg(QFileInfo(fileName).completeBaseName());
369
370
}

371
RunConfiguration *S60DeviceRunConfigurationFactory::create(Project *project, const QString &id)
372
373
374
{
    Qt4Project *p = qobject_cast<Qt4Project *>(project);
    Q_ASSERT(p);
375
376
    if (id.startsWith("QtSymbianDeviceRunConfiguration.")) {
        QString fileName = id.mid(QString("QtSymbianDeviceRunConfiguration.").size());
377
        return new S60DeviceRunConfiguration(p, fileName);
378
    }
379
    Q_ASSERT(id == "Qt4ProjectManager.DeviceRunConfiguration");
380
    // The right path is set in restoreSettings
381
    RunConfiguration *rc = new S60DeviceRunConfiguration(p, QString::null);
382
383
384
    return rc;
}

385
// ======== S60DeviceRunControlBase
Friedemann Kleint's avatar
Friedemann Kleint committed
386

387
S60DeviceRunControlBase::S60DeviceRunControlBase(RunConfiguration *runConfiguration) :
388
    RunControl(runConfiguration),    
389
    m_toolChain(ProjectExplorer::ToolChain::INVALID),
390
    m_makesis(new QProcess(this)),
391
    m_signsis(0),
392
    m_launcher(0)
393
394
395
396
{
    // connect for automatically reporting the "finished deploy" state to the progress manager
    connect(this, SIGNAL(finished()), this, SLOT(reportDeployFinished()));

con's avatar
con committed
397
398
399
400
401
402
403
404
    connect(m_makesis, SIGNAL(readyReadStandardError()),
            this, SLOT(readStandardError()));
    connect(m_makesis, SIGNAL(readyReadStandardOutput()),
            this, SLOT(readStandardOutput()));
    connect(m_makesis, SIGNAL(error(QProcess::ProcessError)),
            this, SLOT(makesisProcessFailed()));
    connect(m_makesis, SIGNAL(finished(int,QProcess::ExitStatus)),
            this, SLOT(makesisProcessFinished()));
405

406
    S60DeviceRunConfiguration *s60runConfig = qobject_cast<S60DeviceRunConfiguration *>(runConfiguration);
dt's avatar
dt committed
407
408
409

    Qt4BuildConfiguration *activeBuildConf = s60runConfig->qt4Project()->activeQt4BuildConfiguration();

410
    QTC_ASSERT(s60runConfig, return);
411
    m_toolChain = s60runConfig->toolChainType();
412
    m_serialPortName = s60runConfig->serialPortName();
413
    m_serialPortFriendlyName = S60Manager::instance()->serialDeviceLister()->friendlyNameForPort(m_serialPortName);
414
    m_communicationType = s60runConfig->communicationType();
415
416
    m_targetName = s60runConfig->targetName();
    m_baseFileName = s60runConfig->basePackageFilePath();
417
418
419
    m_symbianPlatform = s60runConfig->symbianPlatform();
    m_symbianTarget = s60runConfig->symbianTarget();
    m_packageTemplateFile = s60runConfig->packageTemplateFileName();
con's avatar
con committed
420
    m_workingDirectory = QFileInfo(m_baseFileName).absolutePath();
421
    m_qtDir = activeBuildConf->qtVersion()->versionInfo().value("QT_INSTALL_DATA");
422
423
424
    m_useCustomSignature = (s60runConfig->signingMode() == S60DeviceRunConfiguration::SignCustom);
    m_customSignaturePath = s60runConfig->customSignaturePath();
    m_customKeyPath = s60runConfig->customKeyPath();
425

426
    const S60Devices::Device device = S60Manager::instance()->deviceForQtVersion(activeBuildConf->qtVersion());
427
428
429
430
    switch (m_toolChain) {
    case ProjectExplorer::ToolChain::GCCE_GNUPOC:
    case ProjectExplorer::ToolChain::RVCT_ARMV6_GNUPOC: {
            // 'sis' is a make target here. Set up with correct environment
431
            ProjectExplorer::ToolChain *toolchain = activeBuildConf->toolChain();
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
            m_makesisTool = toolchain->makeCommand();
            m_toolsDirectory = device.epocRoot + QLatin1String("/epoc32/tools");
            ProjectExplorer::Environment env = ProjectExplorer::Environment::systemEnvironment();
            toolchain->addToEnvironment(env);
            m_makesis->setEnvironment(env.toStringList());
        }
        break;
    default:        
        m_toolsDirectory = device.toolsRoot + QLatin1String("/epoc32/tools");        
        m_makesisTool = m_toolsDirectory + "/makesis.exe";
        // Set up signing packages
        m_signsis = new QProcess(this);
        connect(m_signsis, SIGNAL(readyReadStandardError()),
                this, SLOT(readStandardError()));
        connect(m_signsis, SIGNAL(readyReadStandardOutput()),
                this, SLOT(readStandardOutput()));
        connect(m_signsis, SIGNAL(error(QProcess::ProcessError)),
                this, SLOT(signsisProcessFailed()));
        connect(m_signsis, SIGNAL(finished(int,QProcess::ExitStatus)),
                this, SLOT(signsisProcessFinished()));
        break;
    }
    m_executableFileName = s60runConfig->localExecutableFileName();
455
456
    m_packageFilePath = s60runConfig->packageFileName();
    m_packageFile = QFileInfo(m_packageFilePath).fileName();
457
458
459
    if (debug)
        qDebug() << "S60DeviceRunControlBase" << m_targetName << ProjectExplorer::ToolChain::toolChainName(m_toolChain)
                 << m_serialPortName << m_communicationType << m_workingDirectory;
460
}
461

462
463
464
465
466
467
468
469
S60DeviceRunControlBase::~S60DeviceRunControlBase()
{
    if (m_launcher) {
        m_launcher->deleteLater();
        m_launcher = 0;
    }
}

470
void S60DeviceRunControlBase::start()
471
{
472
473
474
475
476
477
478
    m_deployProgress = new QFutureInterface<void>;
    Core::ICore::instance()->progressManager()->addTask(m_deployProgress->future(),
                                                        tr("Deploying"),
                                                        QLatin1String("Symbian.Deploy"));
    m_deployProgress->setProgressRange(0, PROGRESS_MAX);
    m_deployProgress->setProgressValue(0);
    m_deployProgress->reportStarted();
479
    emit started();
480
481
482
483
484
    if (m_serialPortName.isEmpty()) {
        error(this, tr("There is no device plugged in."));
        emit finished();
        return;
    }
485

con's avatar
con committed
486
    emit addToOutputWindow(this, tr("Creating %1.sisx ...").arg(QDir::toNativeSeparators(m_baseFileName)));
487
    emit addToOutputWindow(this, tr("Executable file: %1").arg(lsFile(m_executableFileName)));
con's avatar
con committed
488

489
490
491
492
493
494
    QString errorMessage;
    QString settingsCategory;
    QString settingsPage;
    if (!checkConfiguration(&errorMessage, &settingsCategory, &settingsPage)) {
        error(this, errorMessage);
        emit finished();
495
496
        Core::ICore::instance()->showWarningWithOptions(tr("Debugger for Symbian Platform"),
                                                        errorMessage, QString(),
497
498
499
500
                                                        settingsCategory, settingsPage);
        return;
    }

501
502
503
504
505
506
507
508
509
510
511
512
513
514
    QStringList makeSisArgs;
    switch (m_toolChain) {
    case ProjectExplorer::ToolChain::GCCE_GNUPOC:
    case ProjectExplorer::ToolChain::RVCT_ARMV6_GNUPOC:
        makeSisArgs.push_back(QLatin1String("sis"));
        break;
    default:
        makeSisArgs.push_back(m_packageFile);
        if (!createPackageFileFromTemplate(&errorMessage)) {
            error(this, errorMessage);
            emit finished();
            return;
        }
        break;
515
    }
516

con's avatar
con committed
517
    m_makesis->setWorkingDirectory(m_workingDirectory);
518
519
520
521
    emit addToOutputWindow(this, tr("%1 %2").arg(QDir::toNativeSeparators(m_makesisTool), m_packageFile));    
    if (debug)
        qDebug() << m_makesisTool <<  makeSisArgs << m_workingDirectory;
    m_makesis->start(m_makesisTool, makeSisArgs, QIODevice::ReadOnly);
522
523
}

524
525
526
527
528
529
530
531
532
533
534
static inline void stopProcess(QProcess *p)
{
    const int timeOutMS = 200;
    if (p->state() != QProcess::Running)
        return;
    p->terminate();
    if (p->waitForFinished(timeOutMS))
        return;
    p->kill();
}

535
void S60DeviceRunControlBase::stop()
536
{
537
538
539
540
    if (m_makesis)
        stopProcess(m_makesis);
    if (m_signsis)
        stopProcess(m_signsis);
541
542
    if (m_launcher)
        m_launcher->terminate();
543
544
}

545
bool S60DeviceRunControlBase::isRunning() const
546
{
con's avatar
con committed
547
    return m_makesis->state() != QProcess::NotRunning;
548
549
}

550
void S60DeviceRunControlBase::readStandardError()
551
{
con's avatar
con committed
552
553
554
    QProcess *process = static_cast<QProcess *>(sender());
    QByteArray data = process->readAllStandardError();
    emit addToOutputWindowInline(this, QString::fromLocal8Bit(data.constData(), data.length()));
555
556
}

557
void S60DeviceRunControlBase::readStandardOutput()
558
{
con's avatar
con committed
559
560
561
562
563
    QProcess *process = static_cast<QProcess *>(sender());
    QByteArray data = process->readAllStandardOutput();
    emit addToOutputWindowInline(this, QString::fromLocal8Bit(data.constData(), data.length()));
}

564
bool S60DeviceRunControlBase::createPackageFileFromTemplate(QString *errorMessage)
565
566
567
{
    QFile packageTemplate(m_packageTemplateFile);
    if (!packageTemplate.open(QIODevice::ReadOnly)) {
568
        *errorMessage = tr("Could not read template package file '%1'").arg(QDir::toNativeSeparators(m_packageTemplateFile));
569
570
571
572
573
574
575
576
        return false;
    }
    QString contents = packageTemplate.readAll();
    packageTemplate.close();
    contents.replace(QLatin1String("$(PLATFORM)"), m_symbianPlatform);
    contents.replace(QLatin1String("$(TARGET)"), m_symbianTarget);
    QFile packageFile(m_packageFilePath);
    if (!packageFile.open(QIODevice::WriteOnly)) {
577
        *errorMessage = tr("Could not write package file '%1'").arg(QDir::toNativeSeparators(m_packageFilePath));
578
579
580
581
582
583
584
        return false;
    }
    packageFile.write(contents.toLocal8Bit());
    packageFile.close();
    return true;
}

585
void S60DeviceRunControlBase::makesisProcessFailed()
586
{
587
    processFailed(m_makesisTool, m_makesis->error());
588
589
}

590
void S60DeviceRunControlBase::makesisProcessFinished()
591
{
592
    if (m_makesis->exitCode() != 0) {
593
        error(this, tr("An error occurred while creating the package."));
594
        stop();
595
596
597
        emit finished();
        return;
    }
598
    m_deployProgress->setProgressValue(PROGRESS_PACKAGECREATED);
599
600
601
602
603
604
605
606
607
608
609
610
611
    switch (m_toolChain) {
    case ProjectExplorer::ToolChain::GCCE_GNUPOC:
    case ProjectExplorer::ToolChain::RVCT_ARMV6_GNUPOC:
        startDeployment();
        break;
    default:
        startSigning();
        break;
    }
}

void S60DeviceRunControlBase::startSigning()
{
612
613
614
    QString signsisTool = m_toolsDirectory + QLatin1String("/signsis.exe");
    QString sisFile = QFileInfo(m_baseFileName + QLatin1String(".sis")).fileName();
    QString sisxFile = QFileInfo(m_baseFileName + QLatin1String(".sisx")).fileName();
con's avatar
con committed
615
    QString signature = (m_useCustomSignature ? m_customSignaturePath
616
                         : m_qtDir + QLatin1String("/src/s60installs/selfsigned.cer"));
con's avatar
con committed
617
    QString key = (m_useCustomSignature ? m_customKeyPath
618
                         : m_qtDir + QLatin1String("/src/s60installs/selfsigned.key"));
619
    QStringList arguments;
620
    arguments << sisFile
con's avatar
con committed
621
622
            << sisxFile << QDir::toNativeSeparators(signature)
            << QDir::toNativeSeparators(key);
623
    m_signsis->setWorkingDirectory(m_workingDirectory);
624
    emit addToOutputWindow(this, tr("%1 %2").arg(QDir::toNativeSeparators(signsisTool), arguments.join(QString(QLatin1Char(' ')))));
625
626
627
    m_signsis->start(signsisTool, arguments, QIODevice::ReadOnly);
}

628
void S60DeviceRunControlBase::signsisProcessFailed()
629
630
631
632
{
    processFailed("signsis.exe", m_signsis->error());
}

633
void S60DeviceRunControlBase::signsisProcessFinished()
634
{
635
    if (m_signsis->exitCode() != 0) {
636
        error(this, tr("An error occurred while creating the package."));
637
        stop();
638
        emit finished();
639
    } else {
640
        m_deployProgress->setProgressValue(PROGRESS_PACKAGESIGNED);
641
        startDeployment();
642
    }
643
644
645
646
}

void S60DeviceRunControlBase::startDeployment()
{
647
    m_launcher = new trk::Launcher();
648
    connect(m_launcher, SIGNAL(finished()), this, SLOT(launcherFinished()));
649
    connect(m_launcher, SIGNAL(canNotConnect(QString)), this, SLOT(printConnectFailed(QString)));
650
    connect(m_launcher, SIGNAL(copyingStarted()), this, SLOT(printCopyingNotice()));
651
    connect(m_launcher, SIGNAL(canNotCreateFile(QString,QString)), this, SLOT(printCreateFileFailed(QString,QString)));
652
    connect(m_launcher, SIGNAL(canNotWriteFile(QString,QString)), this, SLOT(printWriteFileFailed(QString,QString)));
653
    connect(m_launcher, SIGNAL(canNotCloseFile(QString,QString)), this, SLOT(printCloseFileFailed(QString,QString)));
654
    connect(m_launcher, SIGNAL(installingStarted()), this, SLOT(printInstallingNotice()));
655
    connect(m_launcher, SIGNAL(canNotInstall(QString,QString)), this, SLOT(printInstallFailed(QString,QString)));
656
    connect(m_launcher, SIGNAL(installingFinished()), this, SLOT(printInstallingFinished()));
con's avatar
con committed
657
    connect(m_launcher, SIGNAL(copyProgress(int)), this, SLOT(printCopyProgress(int)));
658
    connect(m_launcher, SIGNAL(stateChanged(int)), this, SLOT(slotLauncherStateChanged(int)));
con's avatar
con committed
659

con's avatar
con committed
660
    //TODO sisx destination and file path user definable
661
    m_launcher->setTrkServerName(m_serialPortName);
662
    m_launcher->setSerialFrame(m_communicationType == SerialPortCommunication);
con's avatar
con committed
663
664
665
    const QString copySrc(m_baseFileName + ".sisx");
    const QString copyDst = QString("C:\\Data\\%1.sisx").arg(QFileInfo(m_baseFileName).fileName());
    const QString runFileName = QString("C:\\sys\\bin\\%1.exe").arg(m_targetName);
666
667
    m_launcher->setCopyFileName(copySrc, copyDst);
    m_launcher->setInstallFileName(copyDst);
668
    initLauncher(runFileName, m_launcher);
669
670
    emit addToOutputWindow(this, tr("Package: %1\nDeploying application to '%2'...").arg(lsFile(copySrc), m_serialPortFriendlyName));
    QString errorMessage;
671
672
673
674
675
676
677
678
679
680
681
682
    // Prompt the user to start up the Blue tooth connection    
    const trk::PromptStartCommunicationResult src =
            S60RunConfigBluetoothStarter::startCommunication(m_launcher->trkDevice(),
                                                             m_serialPortName,
                                                             m_communicationType, 0,
                                                             &errorMessage);
    switch (src) {
    case trk::PromptStartCommunicationConnected:
        break;
    case trk::PromptStartCommunicationCanceled:
    case trk::PromptStartCommunicationError:
        error(this, errorMessage);
683
        stop();
684
685
686
687
        emit finished();
        return;
    };

688
    if (!m_launcher->startServer(&errorMessage)) {
689

690
        error(this, tr("Could not connect to phone on port '%1': %2\n"
Robert Loehning's avatar
Robert Loehning committed
691
                       "Check if the phone is connected and App TRK is running.").arg(m_serialPortName, errorMessage));
692
        stop();
con's avatar
con committed
693
694
        emit finished();
    }
695
696
}

697
void S60DeviceRunControlBase::printCreateFileFailed(const QString &filename, const QString &errorMessage)
con's avatar
con committed
698
{
699
    emit addToOutputWindow(this, tr("Could not create file %1 on device: %2").arg(filename, errorMessage));
con's avatar
con committed
700
701
}

702
703
704
705
706
void S60DeviceRunControlBase::printWriteFileFailed(const QString &filename, const QString &errorMessage)
{
    emit addToOutputWindow(this, tr("Could not write to file %1 on device: %2").arg(filename, errorMessage));
}

707
708
709
710
711
712
void S60DeviceRunControlBase::printCloseFileFailed(const QString &filename, const QString &errorMessage)
{
    const QString msg = tr("Could not close file %1 on device: %2. It will be closed when App TRK is closed.");
    emit addToOutputWindow(this, msg.arg(filename, errorMessage));
}

713
714
715
716
717
void S60DeviceRunControlBase::printConnectFailed(const QString &errorMessage)
{
    emit addToOutputWindow(this, tr("Could not connect to App TRK on device: %1. Restarting App TRK might help.").arg(errorMessage));
}

718
void S60DeviceRunControlBase::printCopyingNotice()
719
{
720
    emit addToOutputWindow(this, tr("Copying install file..."));
721
722
}

723
void S60DeviceRunControlBase::printCopyProgress(int progress)
con's avatar
con committed
724
{
725
    m_deployProgress->setProgressValue(PROGRESS_DEPLOYBASE + progress);
con's avatar
con committed
726
727
}

728
void S60DeviceRunControlBase::printInstallingNotice()
con's avatar
con committed
729
{
730
    m_deployProgress->setProgressValue(PROGRESS_PACKAGEDEPLOYED);
con's avatar
con committed
731
732
733
    emit addToOutputWindow(this, tr("Installing application..."));
}

734
735
736
737
738
739
740
741
void S60DeviceRunControlBase::printInstallingFinished()
{
    m_deployProgress->setProgressValue(PROGRESS_PACKAGEINSTALLED);
    m_deployProgress->reportFinished();
    delete m_deployProgress;
    m_deployProgress = 0;
}

742
743
744
745
746
void S60DeviceRunControlBase::printInstallFailed(const QString &filename, const QString &errorMessage)
{
    emit addToOutputWindow(this, tr("Could not install from package %1 on device: %2").arg(filename, errorMessage));
}

747
748
749
750
751
752
753
void S60DeviceRunControlBase::launcherFinished()
{
    m_launcher->deleteLater();
    m_launcher = 0;
    handleLauncherFinished();
}

754
755
756
757
758
759
760
761
762
void S60DeviceRunControlBase::reportDeployFinished()
{
    if (m_deployProgress) {
        m_deployProgress->reportFinished();
        delete m_deployProgress;
        m_deployProgress = 0;
    }
}

763
764
765
QMessageBox *S60DeviceRunControlBase::createTrkWaitingMessageBox(const QString &port, QWidget *parent)
{
    const QString title  = QCoreApplication::translate("Qt4ProjectManager::Internal::S60DeviceRunControlBase",
Robert Loehning's avatar
Robert Loehning committed
766
                                                       "Waiting for App TRK");
767
    const QString text = QCoreApplication::translate("Qt4ProjectManager::Internal::S60DeviceRunControlBase",
Robert Loehning's avatar
Robert Loehning committed
768
                                                     "Please start App TRK on %1.").arg(port);
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
    QMessageBox *rc = new QMessageBox(QMessageBox::Information, title, text,
                                      QMessageBox::Cancel, parent);
    return rc;
}

void S60DeviceRunControlBase::slotLauncherStateChanged(int s)
{
    if (s == trk::Launcher::WaitingForTrk) {
        QMessageBox *mb = S60DeviceRunControlBase::createTrkWaitingMessageBox(m_launcher->trkServerName(),
                                                     Core::ICore::instance()->mainWindow());
        connect(m_launcher, SIGNAL(stateChanged(int)), mb, SLOT(close()));
        connect(mb, SIGNAL(finished(int)), this, SLOT(slotWaitingForTrkClosed()));
        mb->open();
    }
}

void S60DeviceRunControlBase::slotWaitingForTrkClosed()
{
    if (m_launcher && m_launcher->state() == trk::Launcher::WaitingForTrk) {
        stop();
        error(this, tr("Canceled."));        
        emit finished();
    }
}

794
795
796
797
798
799
800
801
802
803
804
805
806
807
void S60DeviceRunControlBase::processFailed(const QString &program, QProcess::ProcessError errorCode)
{
    QString errorString;
    switch (errorCode) {
    case QProcess::FailedToStart:
        errorString = tr("Failed to start %1.");
        break;
    case QProcess::Crashed:
        errorString = tr("%1 has unexpectedly finished.");
        break;
    default:
        errorString = tr("An error has occurred while running %1.");
    }
    error(this, errorString.arg(program));
808
    stop();
809
810
811
    emit finished();
}

Friedemann Kleint's avatar
Friedemann Kleint committed
812
813
814
815
816
void S60DeviceRunControlBase::printApplicationOutput(const QString &output)
{
    emit addToOutputWindowInline(this, output);
}

817
818
819
820
821
822
823
bool S60DeviceRunControlBase::checkConfiguration(QString * /* errorMessage */,
                                                 QString * /* settingsCategory */,
                                                 QString * /* settingsPage */) const
{
    return true;
}

824
// =============== S60DeviceRunControl
825
S60DeviceRunControl::S60DeviceRunControl(ProjectExplorer::RunConfiguration *runConfiguration) :
826
827
828
829
830
831
832
833
834
835
    S60DeviceRunControlBase(runConfiguration)
{
}

void S60DeviceRunControl::initLauncher(const QString &executable, trk::Launcher *launcher)
{
     connect(launcher, SIGNAL(startingApplication()), this, SLOT(printStartingNotice()));
     connect(launcher, SIGNAL(applicationRunning(uint)), this, SLOT(printRunNotice(uint)));
     connect(launcher, SIGNAL(canNotRun(QString)), this, SLOT(printRunFailNotice(QString)));
     connect(launcher, SIGNAL(applicationOutputReceived(QString)), this, SLOT(printApplicationOutput(QString)));
836
     launcher->addStartupActions(trk::Launcher::ActionCopyInstallRun);
837
838
839
840
841
842
843
844
845
     launcher->setFileName(executable);
}

void S60DeviceRunControl::handleLauncherFinished()
{
     emit finished();
     emit addToOutputWindow(this, tr("Finished."));
 }

con's avatar
con committed
846
847
void S60DeviceRunControl::printStartingNotice()
{
848
    emit addToOutputWindow(this, tr("Starting application..."));
con's avatar
con committed
849
850
851
852
}

void S60DeviceRunControl::printRunNotice(uint pid)
{
con's avatar
con committed
853
    emit addToOutputWindow(this, tr("Application running with pid %1.").arg(pid));
con's avatar
con committed
854
855
}

856
857
858
859
void S60DeviceRunControl::printRunFailNotice(const QString &errorMessage) {
    emit addToOutputWindow(this, tr("Could not start application: %1").arg(errorMessage));
}

860
861
// ======== S60DeviceDebugRunControl

862
S60DeviceDebugRunControl::S60DeviceDebugRunControl(S60DeviceRunConfiguration *runConfiguration) :
863
864
    S60DeviceRunControlBase(runConfiguration),
    m_startParams(new Debugger::DebuggerStartParameters)
865
{
866
    Debugger::DebuggerManager *dm = Debugger::DebuggerManager::instance();
867
868
    S60DeviceRunConfiguration *rc = qobject_cast<S60DeviceRunConfiguration *>(runConfiguration);
    QTC_ASSERT(dm && rc, return);
869
870
871
872

    connect(dm, SIGNAL(debuggingFinished()),
            this, SLOT(debuggingFinished()), Qt::QueuedConnection);
    connect(dm, SIGNAL(applicationOutputAvailable(QString)),
Friedemann Kleint's avatar
Friedemann Kleint committed
873
            this, SLOT(printApplicationOutput(QString)),
874
875
876
            Qt::QueuedConnection);

    m_startParams->remoteChannel = rc->serialPortName();
877
    m_startParams->remoteChannelType = rc->communicationType();
878
879
    m_startParams->startMode = Debugger::StartInternal;
    m_startParams->toolChainType = rc->toolChainType();
880
881
882
883
884
885

    m_localExecutableFileName = rc->localExecutableFileName();
    const int lastDotPos = m_localExecutableFileName.lastIndexOf(QLatin1Char('.'));
    if (lastDotPos != -1) {
        m_startParams->symbolFileName = m_localExecutableFileName.mid(0, lastDotPos) + QLatin1String(".sym");
    }
886
887
}

888
void S60DeviceDebugRunControl::stop()
con's avatar
con committed
889
{
890
891
892
893
894
895
896
897
898
899
900
    S60DeviceRunControlBase::stop();
    Debugger::DebuggerManager *dm = Debugger::DebuggerManager::instance();
    QTC_ASSERT(dm, return)
    if (dm->state() == Debugger::DebuggerNotReady)
        dm->exitDebugger();
}

S60DeviceDebugRunControl::~S60DeviceDebugRunControl()
{
}

901
void S60DeviceDebugRunControl::initLauncher(const QString &executable, trk::Launcher *launcher)
902
903
904
{
    // No setting an executable on the launcher causes it to deploy only
    m_startParams->executable = executable;
905
906
    // Prefer the '*.sym' file over the '.exe', which should exist at the same
    // location in debug builds
907
908
909
910

    if (!QFileInfo(m_startParams->symbolFileName).isFile()) {
        m_startParams->symbolFileName.clear();
        emit addToOutputWindow(this, tr("Warning: Cannot locate the symbol file belonging to %1.").arg(m_localExecutableFileName));
911
    }
912

913
    launcher->addStartupActions(trk::Launcher::ActionCopyInstall);
914
915
916
917
918
919
920
921
922
923
924
}

void S60DeviceDebugRunControl::handleLauncherFinished()
{
    emit addToOutputWindow(this, tr("Launching debugger..."));
    Debugger::DebuggerManager::instance()->startNewDebugger(m_startParams);
}

void S60DeviceDebugRunControl::debuggingFinished()
{
    emit addToOutputWindow(this, tr("Debugging finished."));
con's avatar
con committed
925
    emit finished();
926
}
927
928
929
930
931
932
933
934
935
936

bool S60DeviceDebugRunControl::checkConfiguration(QString *errorMessage,
                                                  QString *settingsCategory,
                                                  QString *settingsPage) const
{
    return Debugger::DebuggerManager::instance()->checkDebugConfiguration(m_startParams->toolChainType,
                                                                          errorMessage,
                                                                          settingsCategory,
                                                                          settingsPage);
}