maemoqemumanager.cpp 22.5 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
30
31
32
**
**************************************************************************/

ck's avatar
ck committed
33
#include "maemoqemumanager.h"
34

35
#include "maemoglobal.h"
36
#include "maemoqemuruntimeparser.h"
37
#include "maemosettingspages.h"
38
#include "remotelinuxrunconfiguration.h"
39
#include "qt4maemotarget.h"
dt's avatar
dt committed
40
#include "maemoqtversion.h"
41
42
43

#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
44
#include <coreplugin/uniqueidmanager.h>
45
46
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
47
#include <coreplugin/icontext.h>
48
49
50
#include <coreplugin/modemanager.h>

#include <projectexplorer/projectexplorer.h>
51
#include <projectexplorer/project.h>
52
#include <projectexplorer/session.h>
53
#include <qtsupport/qtversionmanager.h>
54
#include <utils/filesystemwatcher.h>
55

56
#include <QtCore/QDebug>
57
58
59
#include <QtCore/QDir>
#include <QtCore/QList>
#include <QtCore/QSet>
ck's avatar
ck committed
60
#include <QtCore/QStringBuilder>
61

62
#include <QtGui/QAction>
63
64
65
#include <QtGui/QDesktopServices>
#include <QtGui/QMessageBox>

66
67
#include <limits.h>

68
69
using namespace ProjectExplorer;
using namespace Qt4ProjectManager;
70
71
using namespace RemoteLinux;
using namespace RemoteLinux::Internal;
72

ck's avatar
ck committed
73
MaemoQemuManager *MaemoQemuManager::m_instance = 0;
74
75
76

const QSize iconSize = QSize(24, 20);

ck's avatar
ck committed
77
MaemoQemuManager::MaemoQemuManager(QObject *parent)
78
79
80
    : QObject(parent)
    , m_qemuAction(0)
    , m_qemuProcess(new QProcess(this))
81
    , m_runningQtId(INT_MIN)
82
    , m_userTerminated(false)
83
84
    , m_runtimeRootWatcher(0)
    , m_runtimeFolderWatcher(0)
85
86
87
88
89
{
    m_qemuStarterIcon.addFile(":/qt-maemo/images/qemu-run.png", iconSize);
    m_qemuStarterIcon.addFile(":/qt-maemo/images/qemu-stop.png", iconSize,
        QIcon::Normal, QIcon::On);

90
    m_qemuAction = new QAction("MeeGo Emulator", this);
91
    m_qemuAction->setIcon(m_qemuStarterIcon.pixmap(iconSize));
92
    m_qemuAction->setToolTip(tr("Start MeeGo Emulator"));
93
94
95
96
97
    connect(m_qemuAction, SIGNAL(triggered()), this, SLOT(startRuntime()));

    Core::ICore *core = Core::ICore::instance();
    Core::ActionManager *actionManager = core->actionManager();
    Core::Command *qemuCommand = actionManager->registerAction(m_qemuAction,
98
        "MaemoEmulator", Core::Context(Core::Constants::C_GLOBAL));
99
100
101
102
    qemuCommand->setAttribute(Core::Command::CA_UpdateText);
    qemuCommand->setAttribute(Core::Command::CA_UpdateIcon);

    Core::ModeManager *modeManager = core->modeManager();
103
    modeManager->addAction(qemuCommand->action(), 1);
104
105
    m_qemuAction->setEnabled(false);
    m_qemuAction->setVisible(false);
106
107

    // listen to qt version changes to update the start button
108
    connect(QtSupport::QtVersionManager::instance(), SIGNAL(qtVersionsChanged(QList<int>)),
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        this, SLOT(qtVersionsChanged(QList<int>)));

    // listen to project add, remove and startup changes to udate start button
    SessionManager *session = ProjectExplorerPlugin::instance()->session();
    connect(session, SIGNAL(projectAdded(ProjectExplorer::Project*)), this,
        SLOT(projectAdded(ProjectExplorer::Project*)));
    connect(session, SIGNAL(projectRemoved(ProjectExplorer::Project*)), this,
        SLOT(projectRemoved(ProjectExplorer::Project*)));
    connect(session, SIGNAL(startupProjectChanged(ProjectExplorer::Project*)),
        this, SLOT(projectChanged(ProjectExplorer::Project*)));

    connect(m_qemuProcess, SIGNAL(error(QProcess::ProcessError)), this,
        SLOT(qemuProcessError(QProcess::ProcessError)));
    connect(m_qemuProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this,
        SLOT(qemuProcessFinished()));
ck's avatar
ck committed
124
125
126
127
    connect(m_qemuProcess, SIGNAL(readyReadStandardOutput()), this,
        SLOT(qemuOutput()));
    connect(m_qemuProcess, SIGNAL(readyReadStandardError()), this,
        SLOT(qemuOutput()));
128
129
    connect(this, SIGNAL(qemuProcessStatus(QemuStatus, QString)),
        this, SLOT(qemuStatusChanged(QemuStatus, QString)));
130
}
131

132
Utils::FileSystemWatcher *MaemoQemuManager::runtimeRootWatcher()
133
134
{
    if (!m_runtimeRootWatcher) {
135
136
        m_runtimeRootWatcher = new Utils::FileSystemWatcher(this);
        m_runtimeRootWatcher->setObjectName(QLatin1String("MaemoQemuRuntimeRootWatcher"));
137
138
139
140
141
142
        connect(m_runtimeRootWatcher, SIGNAL(directoryChanged(QString)), this,
            SLOT(runtimeRootChanged(QString)));
    }
    return m_runtimeRootWatcher;
}

143
Utils::FileSystemWatcher *MaemoQemuManager::runtimeFolderWatcher()
144
145
{
    if (!m_runtimeFolderWatcher) {
146
147
        m_runtimeFolderWatcher = new Utils::FileSystemWatcher(this);
        m_runtimeFolderWatcher->setObjectName(QLatin1String("MaemoQemuRuntimeFolderWatcher"));
148
149
150
151
        connect(m_runtimeFolderWatcher, SIGNAL(directoryChanged(QString)), this,
            SLOT(runtimeFolderChanged(QString)));
    }
    return m_runtimeFolderWatcher;
152
153
}

ck's avatar
ck committed
154
MaemoQemuManager::~MaemoQemuManager()
155
156
{
    terminateRuntime();
157
    m_instance = 0;
158
159
}

ck's avatar
ck committed
160
MaemoQemuManager &MaemoQemuManager::instance(QObject *parent)
161
{
162
    if (m_instance == 0)
ck's avatar
ck committed
163
        m_instance = new MaemoQemuManager(parent);
164
165
166
    return *m_instance;
}

167
bool MaemoQemuManager::runtimeForQtVersion(int uniqueId, MaemoQemuRuntime *rt) const
168
{
169
    *rt = m_runtimes.value(uniqueId, MaemoQemuRuntime());
170
    return rt->isValid();
171
172
}

173
174
175
176
177
bool MaemoQemuManager::qemuIsRunning() const
{
    return m_runningQtId != INT_MIN;
}

ck's avatar
ck committed
178
void MaemoQemuManager::qtVersionsChanged(const QList<int> &uniqueIds)
179
{
180
    QtSupport::QtVersionManager *manager = QtSupport::QtVersionManager::instance();
181
182
    foreach (int uniqueId, uniqueIds) {
        if (manager->isValidId(uniqueId)) {
dt's avatar
dt committed
183
184
185
            MaemoQtVersion *version = dynamic_cast<MaemoQtVersion *>(manager->version(uniqueId));

            if (version) {
186
187
                MaemoQemuRuntime runtime
                    = MaemoQemuRuntimeParser::parseRuntime(version);
188
                if (runtime.isValid()) {
189
                    m_runtimes.insert(uniqueId, runtime);
190
191
192
                    if (!runtimeRootWatcher()->watchesDirectory(runtime.m_watchPath))
                        runtimeRootWatcher()->addDirectory(runtime.m_watchPath,
                                                           Utils::FileSystemWatcher::WatchAllChanges);
193
194
195
                } else {
                    m_runtimes.remove(uniqueId);
                }
196
197
198
199
200
201
202
203
204
205
206
207
            }
        } else {
            // this qt version has been removed from the settings
            m_runtimes.remove(uniqueId);
            if (uniqueId == m_runningQtId) {
                terminateRuntime();
                emit qemuProcessStatus(QemuUserReason, tr("Qemu has been shut "
                    "down, because you removed the corresponding Qt version."));
            }
        }
    }

208
    showOrHideQemuButton();
209
210
}

ck's avatar
ck committed
211
void MaemoQemuManager::projectAdded(ProjectExplorer::Project *project)
212
213
214
215
216
217
218
219
220
221
222
223
224
{
    // handle all target related changes, add, remove, etc...
    connect(project, SIGNAL(addedTarget(ProjectExplorer::Target*)), this,
        SLOT(targetAdded(ProjectExplorer::Target*)));
    connect(project, SIGNAL(removedTarget(ProjectExplorer::Target*)), this,
        SLOT(targetRemoved(ProjectExplorer::Target*)));
    connect(project, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
        this, SLOT(targetChanged(ProjectExplorer::Target*)));

    foreach (Target *target, project->targets())
        targetAdded(target);
}

ck's avatar
ck committed
225
void MaemoQemuManager::projectRemoved(ProjectExplorer::Project *project)
226
227
228
229
230
231
232
233
234
235
{
    disconnect(project, SIGNAL(addedTarget(ProjectExplorer::Target*)), this,
        SLOT(targetAdded(ProjectExplorer::Target*)));
    disconnect(project, SIGNAL(removedTarget(ProjectExplorer::Target*)), this,
        SLOT(targetRemoved(ProjectExplorer::Target*)));
    disconnect(project, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
        this, SLOT(targetChanged(ProjectExplorer::Target*)));

    foreach (Target *target, project->targets())
        targetRemoved(target);
236
    showOrHideQemuButton();
237
238
}

ck's avatar
ck committed
239
void MaemoQemuManager::projectChanged(ProjectExplorer::Project *project)
240
{
241
    if (project) {
242
        toggleStarterButton(project->activeTarget());
243
244
        deviceConfigurationChanged(project->activeTarget());
    }
245
246
}

ck's avatar
ck committed
247
void MaemoQemuManager::targetAdded(ProjectExplorer::Target *target)
248
{
249
    if (!target || !MaemoGlobal::isMaemoTargetId(target->id()))
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
        return;

    // handle all run configuration changes, add, remove, etc...
    connect(target, SIGNAL(addedRunConfiguration(ProjectExplorer::RunConfiguration*)),
        this, SLOT(runConfigurationAdded(ProjectExplorer::RunConfiguration*)));
    connect(target, SIGNAL(removedRunConfiguration(ProjectExplorer::RunConfiguration*)),
        this, SLOT(runConfigurationRemoved(ProjectExplorer::RunConfiguration*)));
    connect(target, SIGNAL(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)),
        this, SLOT(runConfigurationChanged(ProjectExplorer::RunConfiguration*)));

    // handle all build configuration changes, add, remove, etc...
    connect(target, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
        this, SLOT(buildConfigurationAdded(ProjectExplorer::BuildConfiguration*)));
    connect(target, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
        this, SLOT(buildConfigurationRemoved(ProjectExplorer::BuildConfiguration*)));
    connect(target, SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
        this, SLOT(buildConfigurationChanged(ProjectExplorer::BuildConfiguration*)));

    // handle the qt version changes the build configuration uses
    connect(target, SIGNAL(environmentChanged()), this, SLOT(environmentChanged()));

271
    foreach (RunConfiguration *rc, target->runConfigurations())
272
        toggleDeviceConnections(qobject_cast<RemoteLinuxRunConfiguration*> (rc), true);
273
    toggleStarterButton(target);
274
275
}

ck's avatar
ck committed
276
void MaemoQemuManager::targetRemoved(ProjectExplorer::Target *target)
277
{
278
    if (!target || !MaemoGlobal::isMaemoTargetId(target->id()))
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
        return;

    disconnect(target, SIGNAL(addedRunConfiguration(ProjectExplorer::RunConfiguration*)),
        this, SLOT(runConfigurationAdded(ProjectExplorer::RunConfiguration*)));
    disconnect(target, SIGNAL(removedRunConfiguration(ProjectExplorer::RunConfiguration*)),
        this, SLOT(runConfigurationRemoved(ProjectExplorer::RunConfiguration*)));
    disconnect(target, SIGNAL(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)),
        this, SLOT(runConfigurationChanged(ProjectExplorer::RunConfiguration*)));

    disconnect(target, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
        this, SLOT(buildConfigurationAdded(ProjectExplorer::BuildConfiguration*)));
    disconnect(target, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
        this, SLOT(buildConfigurationRemoved(ProjectExplorer::BuildConfiguration*)));
    disconnect(target, SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
        this, SLOT(buildConfigurationChanged(ProjectExplorer::BuildConfiguration*)));

    disconnect(target, SIGNAL(environmentChanged()), this, SLOT(environmentChanged()));

297
    foreach (RunConfiguration *rc, target->runConfigurations())
298
        toggleDeviceConnections(qobject_cast<RemoteLinuxRunConfiguration*> (rc), false);
299
    showOrHideQemuButton();
300
301
}

ck's avatar
ck committed
302
void MaemoQemuManager::targetChanged(ProjectExplorer::Target *target)
303
{
304
    if (target) {
305
        toggleStarterButton(target);
306
307
        deviceConfigurationChanged(target);
    }
308
309
}

ck's avatar
ck committed
310
void MaemoQemuManager::runConfigurationAdded(ProjectExplorer::RunConfiguration *rc)
311
{
312
    if (!rc || !MaemoGlobal::isMaemoTargetId(rc->target()->id()))
313
        return;
314
    toggleDeviceConnections(qobject_cast<RemoteLinuxRunConfiguration*> (rc), true);
315
316
}

ck's avatar
ck committed
317
void MaemoQemuManager::runConfigurationRemoved(ProjectExplorer::RunConfiguration *rc)
318
{
319
    if (!rc || !MaemoGlobal::isMaemoTargetId(rc->target()->id()))
320
        return;
321
    toggleDeviceConnections(qobject_cast<RemoteLinuxRunConfiguration*> (rc), false);
322
323
}

ck's avatar
ck committed
324
void MaemoQemuManager::runConfigurationChanged(ProjectExplorer::RunConfiguration *rc)
325
326
{
    if (rc)
ck's avatar
ck committed
327
        m_qemuAction->setEnabled(targetUsesMatchingRuntimeConfig(rc->target()));
328
329
}

ck's avatar
ck committed
330
void MaemoQemuManager::buildConfigurationAdded(ProjectExplorer::BuildConfiguration *bc)
331
{
332
    if (!bc || !MaemoGlobal::isMaemoTargetId(bc->target()->id()))
333
334
335
336
337
        return;

    connect(bc, SIGNAL(environmentChanged()), this, SLOT(environmentChanged()));
}

ck's avatar
ck committed
338
void MaemoQemuManager::buildConfigurationRemoved(ProjectExplorer::BuildConfiguration *bc)
339
{
340
    if (!bc || !MaemoGlobal::isMaemoTargetId(bc->target()->id()))
341
342
343
344
345
        return;

    disconnect(bc, SIGNAL(environmentChanged()), this, SLOT(environmentChanged()));
}

ck's avatar
ck committed
346
void MaemoQemuManager::buildConfigurationChanged(ProjectExplorer::BuildConfiguration *bc)
347
348
{
    if (bc)
349
        toggleStarterButton(bc->target());
350
351
}

ck's avatar
ck committed
352
void MaemoQemuManager::environmentChanged()
353
354
355
356
{
    // likely to happen when the qt version changes the build config is using
    if (ProjectExplorerPlugin *explorer = ProjectExplorerPlugin::instance()) {
        if (Project *project = explorer->session()->startupProject())
357
            toggleStarterButton(project->activeTarget());
358
359
360
    }
}

ck's avatar
ck committed
361
void MaemoQemuManager::deviceConfigurationChanged(ProjectExplorer::Target *target)
362
{
ck's avatar
ck committed
363
    m_qemuAction->setEnabled(targetUsesMatchingRuntimeConfig(target));
364
365
}

ck's avatar
ck committed
366
void MaemoQemuManager::startRuntime()
367
368
369
370
371
{
    m_userTerminated = false;
    Project *p = ProjectExplorerPlugin::instance()->session()->startupProject();
    if (!p)
        return;
372
    QtSupport::BaseQtVersion *version;
ck's avatar
ck committed
373
374
    if (!targetUsesMatchingRuntimeConfig(p->activeTarget(), &version)) {
        qWarning("Strange: Qemu button was enabled, but target does not match.");
375
        return;
ck's avatar
ck committed
376
    }
377

ck's avatar
ck committed
378
    m_runningQtId = version->uniqueId();
379
    const MaemoQemuRuntime rt = m_runtimes.value(version->uniqueId());
380
    m_qemuProcess->setProcessEnvironment(rt.environment());
ck's avatar
ck committed
381
    m_qemuProcess->setWorkingDirectory(rt.m_root);
382
    m_qemuProcess->start(rt.m_bin % QLatin1Char(' ') % rt.m_args);
ck's avatar
ck committed
383
384
    if (!m_qemuProcess->waitForStarted())
        return;
385

ck's avatar
ck committed
386
387
388
    emit qemuProcessStatus(QemuStarting);
    connect(m_qemuAction, SIGNAL(triggered()), this, SLOT(terminateRuntime()));
    disconnect(m_qemuAction, SIGNAL(triggered()), this, SLOT(startRuntime()));
389
390
}

ck's avatar
ck committed
391
void MaemoQemuManager::terminateRuntime()
392
393
394
395
396
397
398
399
400
401
402
403
{
    m_userTerminated = true;

    if (m_qemuProcess->state() != QProcess::NotRunning) {
        m_qemuProcess->terminate();
        m_qemuProcess->kill();
    }

    connect(m_qemuAction, SIGNAL(triggered()), this, SLOT(startRuntime()));
    disconnect(m_qemuAction, SIGNAL(triggered()), this, SLOT(terminateRuntime()));
}

ck's avatar
ck committed
404
void MaemoQemuManager::qemuProcessFinished()
405
{
406
    m_runningQtId = INT_MIN;
407
    QemuStatus status = QemuFinished;
ck's avatar
ck committed
408
    QString error;
409
410

    if (!m_userTerminated) {
ck's avatar
ck committed
411
412
413
414
415
416
417
        if (m_qemuProcess->exitStatus() == QProcess::CrashExit) {
            status = QemuCrashed;
            error = m_qemuProcess->errorString();
        } else if (m_qemuProcess->exitCode() != 0) {
            error = tr("Qemu finished with error: Exit code was %1.")
                .arg(m_qemuProcess->exitCode());
        }
418
419
420
    }

    m_userTerminated = false;
ck's avatar
ck committed
421
    emit qemuProcessStatus(status, error);
422
423
}

ck's avatar
ck committed
424
void MaemoQemuManager::qemuProcessError(QProcess::ProcessError error)
425
426
427
428
429
{
    if (error == QProcess::FailedToStart)
        emit qemuProcessStatus(QemuFailedToStart, m_qemuProcess->errorString());
}

ck's avatar
ck committed
430
void MaemoQemuManager::qemuStatusChanged(QemuStatus status, const QString &error)
431
432
433
434
435
436
437
{
    bool running = false;
    switch (status) {
        case QemuStarting:
            running = true;
            break;
        case QemuFailedToStart:
438
439
            QMessageBox::warning(0, tr("Qemu error"),
                tr("Qemu failed to start: %1"));
440
            break;
441
        case QemuCrashed:
442
            MaemoQemuSettingsPage::showQemuCrashDialog();
443
444
445
            break;
        case QemuFinished:
        case QemuUserReason:
446
447
            if (!error.isEmpty())
                QMessageBox::warning(0, tr("Qemu error"), error);
448
449
450
451
452
453
454
455
            break;
        default:
            Q_ASSERT(!"Missing handling of Qemu status");
    }

    updateStarterIcon(running);
}

ck's avatar
ck committed
456
void MaemoQemuManager::qemuOutput()
ck's avatar
ck committed
457
458
459
460
461
{
    qDebug("%s", m_qemuProcess->readAllStandardOutput().data());
    qDebug("%s", m_qemuProcess->readAllStandardError().data());
}

462
463
464
void MaemoQemuManager::runtimeRootChanged(const QString &directory)
{
    QList<int> uniqueIds;
465
    QMap<int, MaemoQemuRuntime>::const_iterator it;
466
467
468
469
470
471
    for (it = m_runtimes.constBegin(); it != m_runtimes.constEnd(); ++it) {
        if (QDir(it.value().m_watchPath) == QDir(directory))
            uniqueIds.append(it.key());
    }

    foreach (int uniqueId, uniqueIds) {
472
        MaemoQemuRuntime runtime = m_runtimes.value(uniqueId, MaemoQemuRuntime());
473
474
475
476
477
478
479
480
481
482
        if (runtime.isValid()) {
            if (QFile::exists(runtime.m_root)) {
                // nothing changed, so we can remove it
                uniqueIds.removeAll(uniqueId);
            }
        } else {
            if (QFile::exists(runtime.m_root)) {
                if (!QFile::exists(runtime.m_root + QLatin1String("/information"))) {
                    // install might be still in progress
                    uniqueIds.removeAll(uniqueId);
483
484
                    runtimeFolderWatcher()->addDirectory(runtime.m_root,
                                                         Utils::FileSystemWatcher::WatchAllChanges);
485
486
487
488
489
490
491
492
493
494
495
                }
            }
        }
    }
    notify(uniqueIds);
}

void MaemoQemuManager::runtimeFolderChanged(const QString &directory)
{
    if (QFile::exists(directory + QLatin1String("/information"))) {
        QList<int> uniqueIds;
496
        QMap<int, MaemoQemuRuntime>::const_iterator it;
497
498
499
500
501
        for (it = m_runtimes.constBegin(); it != m_runtimes.constEnd(); ++it) {
            if (QDir(it.value().m_root) == QDir(directory))
                uniqueIds.append(it.key());
        }
        notify(uniqueIds);
502
        if (m_runtimeFolderWatcher)
503
            m_runtimeFolderWatcher->removeDirectory(directory);
504
505
506
    }
}

507
508
// -- private

ck's avatar
ck committed
509
void MaemoQemuManager::updateStarterIcon(bool running)
510
511
512
513
514
{
    QIcon::State state;
    QString toolTip;
    if (running) {
        state = QIcon::On;
515
        toolTip = tr("Stop MeeGo Emulator");
516
517
    } else {
        state = QIcon::Off;
518
        toolTip = tr("Start MeeGo Emulator");
519
520
521
522
523
524
525
    }

    m_qemuAction->setToolTip(toolTip);
    m_qemuAction->setIcon(m_qemuStarterIcon.pixmap(iconSize, QIcon::Normal,
        state));
}

ck's avatar
ck committed
526
void MaemoQemuManager::toggleStarterButton(Target *target)
527
528
529
{
    int uniqueId = -1;
    if (target) {
530
        if (AbstractQt4MaemoTarget *qt4Target = qobject_cast<AbstractQt4MaemoTarget*>(target)) {
531
            if (Qt4BuildConfiguration *bc = qt4Target->activeBuildConfiguration()) {
532
                if (QtSupport::BaseQtVersion *version = bc->qtVersion())
533
534
535
536
537
                    uniqueId = version->uniqueId();
            }
        }
    }

538
    if (uniqueId >= 0 && (m_runtimes.isEmpty() || !m_runtimes.contains(uniqueId)))
539
540
        qtVersionsChanged(QList<int>() << uniqueId);

541
542
543
544
    bool isRunning = m_qemuProcess->state() != QProcess::NotRunning;
    if (m_runningQtId == uniqueId)
        isRunning = false;

545
546
547
548
549
550
551
552
    const Project * const p
        = ProjectExplorerPlugin::instance()->session()->startupProject();
    const bool qemuButtonEnabled
        = p && p->activeTarget() && MaemoGlobal::isMaemoTargetId(p->activeTarget()->id())
            && m_runtimes.value(uniqueId, MaemoQemuRuntime()).isValid()
            && targetUsesMatchingRuntimeConfig(target) && !isRunning;
    m_qemuAction->setEnabled(qemuButtonEnabled);
    showOrHideQemuButton();
553
554
}

ck's avatar
ck committed
555
bool MaemoQemuManager::sessionHasMaemoTarget() const
556
557
558
{
    ProjectExplorerPlugin *explorer = ProjectExplorerPlugin::instance();
    const QList<Project*> &projects = explorer->session()->projects();
559
560
    foreach (const Project *p, projects) {
        foreach (const Target * const target, p->targets()) {
561
            if (MaemoGlobal::isMaemoTargetId(target->id()))
562
563
564
565
                return true;
        }
    }
    return false;
566
567
}

ck's avatar
ck committed
568
bool MaemoQemuManager::targetUsesMatchingRuntimeConfig(Target *target,
569
    QtSupport::BaseQtVersion **qtVersion)
570
571
572
{
    if (!target)
        return false;
573
574
    if (target != target->project()->activeTarget())
        return false;
575

576
577
    RemoteLinuxRunConfiguration *mrc =
        qobject_cast<RemoteLinuxRunConfiguration *> (target->activeRunConfiguration());
ck's avatar
ck committed
578
579
580
581
582
583
    if (!mrc)
        return false;
    Qt4BuildConfiguration *bc
        = qobject_cast<Qt4BuildConfiguration *>(target->activeBuildConfiguration());
    if (!bc)
        return false;
584
    QtSupport::BaseQtVersion *version = bc->qtVersion();
585
    if (!version || !m_runtimes.value(version->uniqueId(), MaemoQemuRuntime()).isValid())
ck's avatar
ck committed
586
587
        return false;

ck's avatar
ck committed
588
589
    if (qtVersion)
        *qtVersion = version;
590
591
    const LinuxDeviceConfiguration::ConstPtr &config = mrc->deviceConfig();
    return config && config->type() == LinuxDeviceConfiguration::Emulator;
592
593
}

594
595
596
597
598
599
void MaemoQemuManager::notify(const QList<int> uniqueIds)
{
    qtVersionsChanged(uniqueIds);
    environmentChanged();   // to toggle the start button
}

600
void MaemoQemuManager::toggleDeviceConnections(RemoteLinuxRunConfiguration *mrc,
601
602
603
604
605
606
607
608
609
610
611
612
613
    bool _connect)
{
    if (!mrc)
        return;

    if (_connect) { // handle device configuration changes
        connect(mrc, SIGNAL(deviceConfigurationChanged(ProjectExplorer::Target*)),
            this, SLOT(deviceConfigurationChanged(ProjectExplorer::Target*)));
    } else {
        disconnect(mrc, SIGNAL(deviceConfigurationChanged(ProjectExplorer::Target*)),
            this, SLOT(deviceConfigurationChanged(ProjectExplorer::Target*)));
    }
}
614
615
616
617
618
619
620
621

void MaemoQemuManager::showOrHideQemuButton()
{
    const bool showButton = !m_runtimes.isEmpty() && sessionHasMaemoTarget();
    if (!showButton)
        terminateRuntime();
    m_qemuAction->setVisible(showButton);
}