maemorunconfiguration.cpp 25.7 KB
Newer Older
ck's avatar
ck 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
26
27
28
29
30
31
/**************************************************************************
**
** 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
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "maemorunconfiguration.h"

ck's avatar
ck committed
32
#include "maemodeviceconfigurations.h"
ck's avatar
ck committed
33
#include "maemomanager.h"
34
#include "maemorunconfigurationwidget.h"
35
#include "maemoruncontrol.h"
ck's avatar
ck committed
36
37
38
#include "maemotoolchain.h"
#include "profilereader.h"
#include "qt4project.h"
39
#include "qt4buildconfiguration.h"
ck's avatar
ck committed
40
41
42
43
44
45
46
47
48
49
50
51
52

#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/pathchooser.h>
#include <utils/qtcassert.h>
#include <projectexplorer/persistentsettings.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/session.h>

#include <QtCore/QDebug>
#include <QtCore/QProcess>

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
namespace {
const char * const MAEMO_RC_ID("Qt4ProjectManager.MaemoRunConfiguration");
const char * const MAEMO_RC_ID_PREFIX("Qt4ProjectManager.MaemoRunConfiguration.");

bool hasMaemoRunConfig(ProjectExplorer::Project* project)
{
    Qt4ProjectManager::Qt4Project *qt4Project(
        qobject_cast<Qt4ProjectManager::Qt4Project *>(project));
    if (qt4Project) {
        QList<ProjectExplorer::RunConfiguration *> list =
                qt4Project->runConfigurations();
        foreach (ProjectExplorer::RunConfiguration *rc, list) {
            if (qobject_cast<Qt4ProjectManager::Internal::MaemoRunConfiguration *>(rc))
                return true;
        }
    }
    return false;
}

QString targetFromId(const QString &id)
{
    QString prefix(MAEMO_RC_ID_PREFIX);
    if (!id.startsWith(prefix))
        return QString();
    return id.mid(prefix.size());
}

QString targetToId(const QString &target)
{
    return QString::fromLatin1(MAEMO_RC_ID_PREFIX) + target;
}

} // namespace

static const QLatin1String ArgumentsKey("Arguments");
static const QLatin1String SimulatorPathKey("Simulator");
static const QLatin1String DeviceIdKey("DeviceId");
static const QLatin1String LastDeployedKey("LastDeployed");
static const QLatin1String DebuggingHelpersLastDeployedKey(
    "DebuggingHelpersLastDeployed");
static const QLatin1String ProFileKey("ProFile");

95
96
namespace Qt4ProjectManager {
namespace Internal {
ck's avatar
ck committed
97

98
using namespace ProjectExplorer;
ck's avatar
ck committed
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

void ErrorDumper::printToStream(QProcess::ProcessError error)
{
    QString reason;
    switch (error) {
        case QProcess::FailedToStart:
            reason = "The process failed to start. Either the invoked program is"
                " missing, or you may have insufficient permissions to invoke "
                "the program.";
            break;

        case QProcess::Crashed:
            reason = "The process crashed some time after starting successfully.";
            break;

        case QProcess::Timedout:
            reason = "The last waitFor...() function timed out. The state of "
                "QProcess is unchanged, and you can try calling waitFor...() "
                "again.";
            break;

        case QProcess::WriteError:
            reason = "An error occurred when attempting to write to the process."
                " For example, the process may not be running, or it may have "
                "closed its input channel.";
            break;

        case QProcess::ReadError:
            reason = "An error occurred when attempting to read from the process."
                " For example, the process may not be running.";
            break;

        default:
            reason = "QProcess::UnknownError";
            break;
    }
    qWarning() << "Failed to run emulator. Reason:" << reason;
}


// #pragma mark -- MaemoRunConfiguration

141
MaemoRunConfiguration::MaemoRunConfiguration(Qt4Project *project,
ck's avatar
ck committed
142
        const QString &proFilePath)
143
    : RunConfiguration(project, QLatin1String(MAEMO_RC_ID))
ck's avatar
ck committed
144
145
146
147
    , m_proFilePath(proFilePath)
    , m_cachedTargetInformationValid(false)
    , m_cachedSimulatorInformationValid(false)
    , qemu(0)
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
{
    ctor();
}

MaemoRunConfiguration::MaemoRunConfiguration(Qt4Project *project,
                                             MaemoRunConfiguration *source)
    : RunConfiguration(project, source)
    , m_executable(source->m_executable)
    , m_proFilePath(source->m_proFilePath)
    , m_cachedTargetInformationValid(false)
    , m_simulator(source->m_simulator)
    , m_simulatorArgs(source->m_simulatorArgs)
    , m_simulatorPath(source->m_simulatorPath)
    , m_visibleSimulatorParameter(source->m_visibleSimulatorParameter)
    , m_cachedSimulatorInformationValid(false)
    , m_isUserSetSimulator(source->m_isUserSetSimulator)
    , m_userSimulatorPath(source->m_userSimulatorPath)
    , m_gdbPath(source->m_gdbPath)
    , m_devConfig(source->m_devConfig)
    , m_arguments(source->m_arguments)
    , m_lastDeployed(source->m_lastDeployed)
    , m_debuggingHelpersLastDeployed(source->m_debuggingHelpersLastDeployed)
    , qemu(0)
#if USE_SSL_PASSWORD
    , m_remoteUserPassword(source->m_remoteUserPassword)
    , m_remoteHostRequiresPassword(source->m_remoteHostRequiresPassword)
#endif
{
    ctor();
}

void MaemoRunConfiguration::ctor()
ck's avatar
ck committed
180
181
{
    if (!m_proFilePath.isEmpty()) {
182
        setDisplayName(tr("%1 on Maemo device").arg(QFileInfo(m_proFilePath)
ck's avatar
ck committed
183
184
            .completeBaseName()));
    } else {
185
        setDisplayName(tr("MaemoRunConfiguration"));
ck's avatar
ck committed
186
187
    }

ck's avatar
ck committed
188
189
190
    connect(&MaemoDeviceConfigurations::instance(), SIGNAL(updated()),
            this, SLOT(updateDeviceConfigurations()));

191
    connect(qt4Project(), SIGNAL(targetInformationChanged()),
192
193
            this, SLOT(invalidateCachedTargetInformation()));

194
    connect(qt4Project(), SIGNAL(targetInformationChanged()),
195
196
            this, SLOT(enabledStateChanged()));

197
    connect(qt4Project(), SIGNAL(proFileUpdated(Qt4ProjectManager::Internal::Qt4ProFileNode*)),
198
            this, SLOT(proFileUpdate(Qt4ProjectManager::Internal::Qt4ProFileNode*)));
ck's avatar
ck committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

    qemu = new QProcess(this);
    connect(qemu, SIGNAL(error(QProcess::ProcessError)), &dumper,
        SLOT(printToStream(QProcess::ProcessError)));
    connect(qemu, SIGNAL(finished(int, QProcess::ExitStatus)), this,
        SLOT(qemuProcessFinished()));
}

MaemoRunConfiguration::~MaemoRunConfiguration()
{
    if (qemu && qemu->state() != QProcess::NotRunning) {
        qemu->terminate();
        qemu->kill();
    }
    delete qemu;
    qemu = NULL;
}

217
Qt4Project *MaemoRunConfiguration::qt4Project() const
ck's avatar
ck committed
218
{
219
    return static_cast<Qt4Project *>(project());
ck's avatar
ck committed
220
221
}

222
bool MaemoRunConfiguration::isEnabled(ProjectExplorer::BuildConfiguration *config) const
ck's avatar
ck committed
223
{
224
    Qt4BuildConfiguration *qt4bc = qobject_cast<Qt4BuildConfiguration*>(config);
225
226
    QTC_ASSERT(qt4bc, return false);
    ToolChain::ToolChainType type = qt4bc->toolChainType();
ck's avatar
ck committed
227
228
229
230
231
232
233
234
    return type == ToolChain::GCC_MAEMO;
}

QWidget *MaemoRunConfiguration::configurationWidget()
{
    return new MaemoRunConfigurationWidget(this);
}

235
236
237
238
239
240
void MaemoRunConfiguration::proFileUpdate(Qt4ProjectManager::Internal::Qt4ProFileNode *pro)
{
    if (m_proFilePath == pro->path())
        invalidateCachedTargetInformation();
}

241
QVariantMap MaemoRunConfiguration::toMap() const
ck's avatar
ck committed
242
{
243
244
245
    QVariantMap map(RunConfiguration::toMap());
    map.insert(DeviceIdKey, m_devConfig.internalId);
    map.insert(ArgumentsKey, m_arguments);
246

247
248
    map.insert(LastDeployedKey, m_lastDeployed);
    map.insert(DebuggingHelpersLastDeployedKey,
ck's avatar
ck committed
249
250
        m_debuggingHelpersLastDeployed);

251
    map.insert(SimulatorPathKey, m_simulatorPath);
252

253
254
    const QDir &dir = QFileInfo(qt4Project()->file()->fileName()).absoluteDir();
    map.insert(ProFileKey, dir.relativeFilePath(m_proFilePath));
ck's avatar
ck committed
255

256
    return map;
ck's avatar
ck committed
257
258
}

259
bool MaemoRunConfiguration::fromMap(const QVariantMap &map)
ck's avatar
ck committed
260
{
ck's avatar
ck committed
261
    setDeviceConfig(MaemoDeviceConfigurations::instance().
262
263
        find(map.value(DeviceIdKey, 0).toInt()));
    m_arguments = map.value(ArgumentsKey).toStringList();
264

265
    m_lastDeployed = map.value(LastDeployedKey).toDateTime();
ck's avatar
ck committed
266
    m_debuggingHelpersLastDeployed =
267
268
269
        map.value(DebuggingHelpersLastDeployedKey).toDateTime();

    m_simulatorPath = map.value(SimulatorPathKey).toString();
ck's avatar
ck committed
270

271
272
    const QDir &dir = QFileInfo(qt4Project()->file()->fileName()).absoluteDir();
    m_proFilePath = dir.filePath(map.value(ProFileKey).toString());
273

274
    return RunConfiguration::fromMap(map);
ck's avatar
ck committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
}

bool MaemoRunConfiguration::currentlyNeedsDeployment() const
{
    return fileNeedsDeployment(executable(), m_lastDeployed);
}

void MaemoRunConfiguration::wasDeployed()
{
    m_lastDeployed = QDateTime::currentDateTime();
}

bool MaemoRunConfiguration::hasDebuggingHelpers() const
{
289
    Qt4BuildConfiguration *qt4bc = qt4Project()->activeQt4BuildConfiguration();
290
    return qt4bc->qtVersion()->hasDebuggingHelper();
ck's avatar
ck committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
}

bool MaemoRunConfiguration::debuggingHelpersNeedDeployment() const
{
    if (hasDebuggingHelpers())
        return fileNeedsDeployment(dumperLib(), m_debuggingHelpersLastDeployed);
    return false;
}

void MaemoRunConfiguration::debuggingHelpersDeployed()
{
    m_debuggingHelpersLastDeployed = QDateTime::currentDateTime();
}

bool MaemoRunConfiguration::fileNeedsDeployment(const QString &path,
    const QDateTime &lastDeployed) const
{
    return !lastDeployed.isValid()
        || QFileInfo(path).lastModified() > lastDeployed;
}

312
void MaemoRunConfiguration::setDeviceConfig(const MaemoDeviceConfig &devConf)
313
{
ck's avatar
ck committed
314
    m_devConfig = devConf;
315
316
}

317
MaemoDeviceConfig MaemoRunConfiguration::deviceConfig() const
318
{
ck's avatar
ck committed
319
    return m_devConfig;
ck's avatar
ck committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
}

const QString MaemoRunConfiguration::sshCmd() const
{
    return cmd(QString::fromLocal8Bit("ssh"));
}

const QString MaemoRunConfiguration::scpCmd() const
{
    return cmd(QString::fromLocal8Bit("scp"));
}

const QString MaemoRunConfiguration::cmd(const QString &cmdName) const
{
    QString command(cmdName);
#ifdef Q_OS_WIN
    command = maddeRoot() + QLatin1String("/bin/") + command
          + QLatin1String(".exe");
#endif
    return command;
}

const MaemoToolChain *MaemoRunConfiguration::toolchain() const
{
kh1's avatar
kh1 committed
344
    Qt4BuildConfiguration *qt4bc = qobject_cast<Qt4BuildConfiguration *>
345
        (qt4Project()->activeBuildConfiguration());
346
    QTC_ASSERT(qt4bc, return 0);
ck's avatar
ck committed
347
    MaemoToolChain *tc = dynamic_cast<MaemoToolChain *>(
348
        qt4bc->toolChain() );
ck's avatar
ck committed
349
350
351
352
353
354
    QTC_ASSERT(tc != 0, return 0);
    return tc;
}

const QString MaemoRunConfiguration::gdbCmd() const
{
355
356
357
    if (const MaemoToolChain *tc = toolchain())
        return tc->targetRoot() + "/bin/gdb";
    return QString();
ck's avatar
ck committed
358
359
360
361
}

QString MaemoRunConfiguration::maddeRoot() const
{
362
    if (const MaemoToolChain *tc = toolchain())
kh1's avatar
kh1 committed
363
        return tc->maddeRoot();
364
    return QString();
ck's avatar
ck committed
365
366
367
368
}

const QString MaemoRunConfiguration::sysRoot() const
{
369
    if (const MaemoToolChain *tc = toolchain())
ck's avatar
ck committed
370
        return tc->sysrootRoot();
371
    return QString();
ck's avatar
ck committed
372
373
}

374
375
const QStringList MaemoRunConfiguration::arguments() const
{
ck's avatar
ck committed
376
    return m_arguments;
377
378
}

ck's avatar
ck committed
379
380
const QString MaemoRunConfiguration::dumperLib() const
{
381
    Qt4BuildConfiguration *qt4bc = qt4Project()->activeQt4BuildConfiguration();
382
    return qt4bc->qtVersion()->debuggingHelperLibrary();
ck's avatar
ck committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
}

QString MaemoRunConfiguration::executable() const
{
    const_cast<MaemoRunConfiguration*> (this)->updateTarget();
    return m_executable;
}

QString MaemoRunConfiguration::simulatorPath() const
{
    qDebug("MaemoRunConfiguration::simulatorPath() called, %s",
        qPrintable(m_simulatorPath));

    const_cast<MaemoRunConfiguration*> (this)->updateSimulatorInformation();
    return m_simulatorPath;
}

QString MaemoRunConfiguration::visibleSimulatorParameter() const
{
    qDebug("MaemoRunConfiguration::visibleSimulatorParameter() called");

    const_cast<MaemoRunConfiguration*> (this)->updateSimulatorInformation();
    return m_visibleSimulatorParameter;
}

QString MaemoRunConfiguration::simulator() const
{
    const_cast<MaemoRunConfiguration*> (this)->updateSimulatorInformation();
    return m_simulator;
}

QString MaemoRunConfiguration::simulatorArgs() const
{
    const_cast<MaemoRunConfiguration*> (this)->updateSimulatorInformation();
    return m_simulatorArgs;
}

void MaemoRunConfiguration::setArguments(const QStringList &args)
{
ck's avatar
ck committed
422
    m_arguments = args;
ck's avatar
ck committed
423
424
425
426
427
428
429
430
431
432
433
434
435
436
}


bool MaemoRunConfiguration::isQemuRunning() const
{
    return (qemu && qemu->state() != QProcess::NotRunning);
}

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

437
void MaemoRunConfiguration::setUserSimulatorPath(const QString &path)
ck's avatar
ck committed
438
{
439
    qDebug("MaemoRunConfiguration::setUserSimulatorPath() called, "
ck's avatar
ck committed
440
441
442
        "m_simulatorPath: %s, new path: %s", qPrintable(m_simulatorPath),
        qPrintable(path));

443
444
    m_isUserSetSimulator = true;
    if (m_userSimulatorPath != path)
ck's avatar
ck committed
445
446
        m_cachedSimulatorInformationValid = false;

447
    m_userSimulatorPath = path;
ck's avatar
ck committed
448
449
450
451
452
453
454
455
456
457
458
459
    emit cachedSimulatorInformationChanged();
}

void MaemoRunConfiguration::invalidateCachedSimulatorInformation()
{
    qDebug("MaemoRunConfiguration::invalidateCachedSimulatorInformation() "
        "called");

    m_cachedSimulatorInformationValid = false;
    emit cachedSimulatorInformationChanged();
}

460
461
462
463
464
465
466
467
468
void MaemoRunConfiguration::resetCachedSimulatorInformation()
{
    m_userSimulatorPath.clear();
    m_isUserSetSimulator = false;

    m_cachedSimulatorInformationValid = false;
    emit cachedSimulatorInformationChanged();
}

ck's avatar
ck committed
469
470
471
472
473
474
475
476
void MaemoRunConfiguration::updateTarget()
{
    if (m_cachedTargetInformationValid)
        return;

    m_executable = QString::null;
    m_cachedTargetInformationValid = true;

477
    Qt4TargetInformation info = qt4Project()->targetInformation(qt4Project()->activeQt4BuildConfiguration(),
478
479
480
                                                             m_proFilePath);
    if (info.error != Qt4TargetInformation::NoError) {
        if (info.error == Qt4TargetInformation::ProParserError) {
ck's avatar
ck committed
481
482
            Core::ICore::instance()->messageManager()->printToOutputPane(tr(
                "Could not parse %1. The Maemo run configuration %2 "
483
                "can not be started.").arg(m_proFilePath).arg(displayName()));
ck's avatar
ck committed
484
        }
485
486
        emit targetInformationChanged();
        return;
ck's avatar
ck committed
487
488
    }

489
    m_executable = QDir::cleanPath(info.workingDir + QLatin1Char('/') + info.target);
490

ck's avatar
ck committed
491
492
493
494
495
496
497
498
499
500
501
    emit targetInformationChanged();
}

void MaemoRunConfiguration::updateSimulatorInformation()
{
    if (m_cachedSimulatorInformationValid)
        return;

    m_simulator = QString::null;
    m_simulatorArgs == QString::null;
    m_cachedSimulatorInformationValid = true;
502
503
504
505
    m_simulatorPath = QDir::toNativeSeparators(m_userSimulatorPath);
    m_visibleSimulatorParameter = tr("Could not autodetect target simulator, "
        "please choose one on your own.");

506
507
508
509
    if (!m_isUserSetSimulator) {
        if (const MaemoToolChain *tc = toolchain())
            m_simulatorPath = QDir::toNativeSeparators(tc->simulatorRoot());
    }
ck's avatar
ck committed
510
511
512
513
514
515

    if (!m_simulatorPath.isEmpty()) {
        m_visibleSimulatorParameter = tr("'%1' is not a valid Maemo simulator.")
            .arg(m_simulatorPath);
    }

516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    QDir dir(m_simulatorPath);
    if (dir.exists(m_simulatorPath)) {
        const QStringList &files = dir.entryList(QDir::Files | QDir::NoSymLinks
            | QDir::NoDotAndDotDot);
        if (files.count() >= 2) {
            const QLatin1String info("information");
            if (files.contains(info)) {
                QFile file(m_simulatorPath + QLatin1Char('/') + info);
                if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                    QMap<QString, QString> map;
                    QTextStream stream(&file);
                    while (!stream.atEnd()) {
                        const QString &line = stream.readLine().trimmed();
                        const int index = line.indexOf(QLatin1Char('='));
                        map.insert(line.mid(0, index).remove(QLatin1Char('\'')),
                            line.mid(index + 1).remove(QLatin1Char('\'')));
                    }

                    m_simulator = map.value(QLatin1String("runcommand"));
                    m_simulatorArgs = map.value(QLatin1String("runcommand_args"));

                    m_visibleSimulatorParameter = m_simulator
ck's avatar
ck committed
538
#ifdef Q_OS_WIN
539
                        + QLatin1String(".exe")
ck's avatar
ck committed
540
#endif
541
542
                        + QLatin1Char(' ') + m_simulatorArgs;
                }
ck's avatar
ck committed
543
544
            }
        }
545
546
547
    } else {
        m_visibleSimulatorParameter = tr("'%1' could not be found. Please "
            "choose a simulator on your own.").arg(m_simulatorPath);
ck's avatar
ck committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
    }

    emit cachedSimulatorInformationChanged();
}

void MaemoRunConfiguration::startStopQemu()
{
    if (qemu->state() != QProcess::NotRunning) {
        if (qemu->state() == QProcess::Running) {
            qemu->terminate();
            qemu->kill();
            emit qemuProcessStatus(false);
        }
        return;
    }

    QString root = maddeRoot();
    if (root.isEmpty() || simulator().isEmpty())
        return;

    const QLatin1Char colon(';');
    const QString path = QDir::toNativeSeparators(root + QLatin1Char('/'));

    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    env.insert("PATH", env.value("Path") + colon + path + QLatin1String("bin"));
    env.insert("PATH", env.value("Path") + colon + path + QLatin1String("madlib"));

    qemu->setProcessEnvironment(env);
    qemu->setWorkingDirectory(simulatorPath());

    QString app = root + QLatin1String("/madlib/") + simulator()
#ifdef Q_OS_WIN
        + QLatin1String(".exe")
#endif
    ;   // keep

    qemu->start(app + QLatin1Char(' ') + simulatorArgs(), QIODevice::ReadWrite);
    emit qemuProcessStatus(qemu->waitForStarted());
}

void MaemoRunConfiguration::qemuProcessFinished()
{
    emit qemuProcessStatus(false);
}

void MaemoRunConfiguration::enabledStateChanged()
{
    MaemoManager::instance()->setQemuSimulatorStarterEnabled(isEnabled());
}

ck's avatar
ck committed
598
599
600
601
602
603
604
605
606
void MaemoRunConfiguration::updateDeviceConfigurations()
{
    qDebug("%s: Current devid = %llu", Q_FUNC_INFO, m_devConfig.internalId);
    m_devConfig =
        MaemoDeviceConfigurations::instance().find(m_devConfig.internalId);
    qDebug("%s: new devid = %llu", Q_FUNC_INFO, m_devConfig.internalId);
    emit deviceConfigurationsUpdated();
}

ck's avatar
ck committed
607
608
// #pragma mark -- MaemoRunConfigurationFactory

609
610
MaemoRunConfigurationFactory::MaemoRunConfigurationFactory(QObject *parent) :
    IRunConfigurationFactory(parent)
ck's avatar
ck committed
611
{
612
613
614
615
616
617
618
    ProjectExplorerPlugin *explorer = ProjectExplorerPlugin::instance();
    connect(explorer->session(), SIGNAL(projectAdded(ProjectExplorer::Project*)),
            this, SLOT(projectAdded(ProjectExplorer::Project*)));
    connect(explorer->session(), SIGNAL(projectRemoved(ProjectExplorer::Project*)),
            this, SLOT(projectRemoved(ProjectExplorer::Project*)));
    connect(explorer, SIGNAL(currentProjectChanged(ProjectExplorer::Project*)),
            this, SLOT(currentProjectChanged(ProjectExplorer::Project*)));
ck's avatar
ck committed
619
620
621
622
623
624
}

MaemoRunConfigurationFactory::~MaemoRunConfigurationFactory()
{
}

625
626
bool MaemoRunConfigurationFactory::canCreate(Project *parent,
                                             const QString &id) const
ck's avatar
ck committed
627
{
628
629
630
    if (!qobject_cast<Qt4Project *>(parent))
        return false;
    return id.startsWith(QLatin1String(MAEMO_RC_ID));
ck's avatar
ck committed
631
632
}

633
634
bool MaemoRunConfigurationFactory::canRestore(Project *parent,
                                              const QVariantMap &map) const
ck's avatar
ck committed
635
{
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
    QString id(ProjectExplorer::idFromMap(map));
    return canCreate(parent, id);
}

bool MaemoRunConfigurationFactory::canClone(Project *parent,
                                            RunConfiguration *source) const
{
    QString id(source->id());
    return canCreate(parent, id);
}

QStringList MaemoRunConfigurationFactory::availableCreationIds(Project *pro) const
{
    Qt4Project *qt4Project(qobject_cast<Qt4Project *>(pro));
    if (!qt4Project)
        return QStringList();

    return qt4Project->applicationProFilePathes(QLatin1String(MAEMO_RC_ID_PREFIX));
ck's avatar
ck committed
654
655
}

656
QString MaemoRunConfigurationFactory::displayNameForId(const QString &id) const
ck's avatar
ck committed
657
{
658
659
660
661
    QString target(targetFromId(id));
    if (target.isEmpty())
        return QString();
    return tr("%1 on Maemo Device").arg(QFileInfo(target).completeBaseName());
ck's avatar
ck committed
662
663
}

664
665
RunConfiguration *MaemoRunConfigurationFactory::create(Project *parent,
                                                       const QString &id)
ck's avatar
ck committed
666
{
667
668
669
670
671
672
    if (!canCreate(parent, id))
        return 0;
    Qt4Project *pqt4parent(static_cast<Qt4Project *>(parent));
    MaemoRunConfiguration *rc(new MaemoRunConfiguration(pqt4parent, targetFromId(id)));
    setupRunConfiguration(rc);
    return rc;
ck's avatar
ck committed
673

674
}
ck's avatar
ck committed
675

676
677
678
679
680
681
682
RunConfiguration *MaemoRunConfigurationFactory::restore(Project *parent,
                                                        const QVariantMap &map)
{
    if (!canRestore(parent, map))
        return 0;
    Qt4Project *qt4Project(qobject_cast<Qt4Project *>(parent));
    Q_ASSERT(qt4Project);
ck's avatar
ck committed
683

684
685
686
687
    MaemoRunConfiguration *rc(new MaemoRunConfiguration(qt4Project, QString()));
    if (!rc->fromMap(map)) {
        delete rc;
        return 0;
ck's avatar
ck committed
688
689
    }

690
691
692
693
694
695
696
697
698
699
700
701
702
703
    setupRunConfiguration(rc);
    return rc;
}

RunConfiguration *MaemoRunConfigurationFactory::clone(Project *parent,
                                                      RunConfiguration *source)
{
    if (!canClone(parent, source))
        return 0;
    Qt4Project *qt4Project(static_cast<Qt4Project *>(parent));
    MaemoRunConfiguration *old(static_cast<MaemoRunConfiguration *>(source));
    MaemoRunConfiguration *rc(new MaemoRunConfiguration(qt4Project, old));

    setupRunConfiguration(rc);
ck's avatar
ck committed
704
705
706
707

    return rc;
}

708
void MaemoRunConfigurationFactory::setupRunConfiguration(MaemoRunConfiguration *rc)
ck's avatar
ck committed
709
{
710
711
712
713
714
715
716
717
718
    if (!rc)
        return;

    connect(rc->project(), SIGNAL(runConfigurationsEnabledStateChanged()),
            rc, SLOT(enabledStateChanged()));
    connect(MaemoManager::instance(), SIGNAL(startStopQemu()),
            rc, SLOT(startStopQemu()));
    connect(rc, SIGNAL(qemuProcessStatus(bool)),
            MaemoManager::instance(), SLOT(updateQemuSimulatorStarter(bool)));
ck's avatar
ck committed
719
720
}

721
void MaemoRunConfigurationFactory::addedRunConfiguration(ProjectExplorer::RunConfiguration *rc)
ck's avatar
ck committed
722
{
723
    if (hasMaemoRunConfig(rc->project()))
ck's avatar
ck committed
724
        MaemoManager::instance()->addQemuSimulatorStarter(rc->project());
ck's avatar
ck committed
725
726
}

727
void MaemoRunConfigurationFactory::removedRunConfiguration(ProjectExplorer::RunConfiguration *rc)
ck's avatar
ck committed
728
{
729
    if (!hasMaemoRunConfig(rc->project()))
ck's avatar
ck committed
730
        MaemoManager::instance()->removeQemuSimulatorStarter(rc->project());
ck's avatar
ck committed
731
732
733
}

void MaemoRunConfigurationFactory::projectAdded(
734
    ProjectExplorer::Project *project)
ck's avatar
ck committed
735
{
736
737
738
739
    connect(project, SIGNAL(addedRunConfiguration(ProjectExplorer::RunConfiguration*)),
            this, SLOT(addedRunConfiguration(ProjectExplorer::RunConfiguration*)));
    connect(project, SIGNAL(removedRunConfiguration(ProjectExplorer::RunConfiguration*)),
            this, SLOT(removedRunConfiguration(ProjectExplorer::RunConfiguration*)));
740

ck's avatar
ck committed
741
742
743
744
745
    if (hasMaemoRunConfig(project))
        MaemoManager::instance()->addQemuSimulatorStarter(project);
}

void MaemoRunConfigurationFactory::projectRemoved(
746
    ProjectExplorer::Project *project)
ck's avatar
ck committed
747
{
748
749
750
751
    disconnect(project, SIGNAL(addedRunConfiguration(ProjectExplorer::RunConfiguration*)),
               this, SLOT(addedRunConfiguration(ProjectExplorer::RunConfiguration*)));
    disconnect(project, SIGNAL(removedRunConfiguration(ProjectExplorer::RunConfiguration*)),
               this, SLOT(removedRunConfiguration(ProjectExplorer::RunConfiguration*)));
752

ck's avatar
ck committed
753
754
755
756
757
    if (hasMaemoRunConfig(project))
        MaemoManager::instance()->removeQemuSimulatorStarter(project);
}

void MaemoRunConfigurationFactory::currentProjectChanged(
758
    ProjectExplorer::Project *project)
ck's avatar
ck committed
759
760
761
762
763
764
{
    bool hasRunConfig = hasMaemoRunConfig(project);
    MaemoManager::instance()->setQemuSimulatorStarterEnabled(hasRunConfig);

    bool isRunning = false;
    if (Qt4Project *qt4Project = qobject_cast<Qt4Project *>(project)) {
765
766
767
        RunConfiguration *rc = qt4Project->activeRunConfiguration();
        if (MaemoRunConfiguration *mrc = qobject_cast<MaemoRunConfiguration *>(rc))
            isRunning = mrc->isQemuRunning();
ck's avatar
ck committed
768
769
770
771
772
773
774
775
776
777
778
779
780
    }
    MaemoManager::instance()->updateQemuSimulatorStarter(isRunning);
}


// #pragma mark -- MaemoRunControlFactory


MaemoRunControlFactory::MaemoRunControlFactory(QObject *parent)
    : IRunControlFactory(parent)
{
}

781
bool MaemoRunControlFactory::canRun(RunConfiguration *runConfiguration,
ck's avatar
ck committed
782
783
    const QString &mode) const
{
784
    return qobject_cast<MaemoRunConfiguration *>(runConfiguration)
ck's avatar
ck committed
785
786
787
788
        && (mode == ProjectExplorer::Constants::RUNMODE
        || mode == ProjectExplorer::Constants::DEBUGMODE);
}

789
RunControl* MaemoRunControlFactory::create(RunConfiguration *runConfig,
ck's avatar
ck committed
790
791
    const QString &mode)
{
dt's avatar
dt committed
792
    MaemoRunConfiguration *rc = qobject_cast<MaemoRunConfiguration *>(runConfig);
793
    Q_ASSERT(rc);
ck's avatar
ck committed
794
795
796
797
798
799
800
801
802
803
804
805
    Q_ASSERT(mode == ProjectExplorer::Constants::RUNMODE
        || mode == ProjectExplorer::Constants::DEBUGMODE);
    if (mode == ProjectExplorer::Constants::RUNMODE)
        return new MaemoRunControl(rc);
    return new MaemoDebugRunControl(rc);
}

QString MaemoRunControlFactory::displayName() const
{
    return tr("Run on device");
}

806
QWidget* MaemoRunControlFactory::configurationWidget(RunConfiguration *config)
ck's avatar
ck committed
807
808
809
810
811
{
    Q_UNUSED(config)
    return 0;
}

812
813
} // namespace Internal
} // namespace Qt4ProjectManager