s60emulatorrunconfiguration.cpp 13.3 KB
Newer Older
con's avatar
con committed
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).
con's avatar
con committed
6 7 8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
con's avatar
con committed
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
con's avatar
con committed
15 16 17 18 19 20 21 22 23 24
**
** 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.
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31 32 33
**
**************************************************************************/

34 35 36
#include "s60emulatorrunconfiguration.h"

#include "qt4project.h"
Tobias Hunger's avatar
Tobias Hunger committed
37
#include "qt4target.h"
38
#include "s60manager.h"
39
#include "qt4symbiantarget.h"
40
#include "qt4projectmanagerconstants.h"
dt's avatar
dt committed
41
#include "qtoutputformatter.h"
42

43 44
#include <projectexplorer/projectexplorerconstants.h>

45
#include <utils/qtcassert.h>
46
#include <utils/detailswidget.h>
47

48
#include <QtGui/QLabel>
49 50
#include <QtGui/QVBoxLayout>
#include <QtGui/QFormLayout>
51

52
using namespace ProjectExplorer;
dt's avatar
dt committed
53
using namespace Qt4ProjectManager;
54 55
using namespace Qt4ProjectManager::Internal;

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
namespace {
const char * const S60_EMULATOR_RC_ID("Qt4ProjectManager.S60EmulatorRunConfiguration");
const char * const S60_EMULATOR_RC_PREFIX("Qt4ProjectManager.S60EmulatorRunConfiguration.");

const char * const PRO_FILE_KEY("Qt4ProjectManager.S60EmulatorRunConfiguration.ProFile");

QString pathFromId(const QString &id)
{
    if (!id.startsWith(QLatin1String(S60_EMULATOR_RC_PREFIX)))
        return QString();
    return id.mid(QString::fromLatin1(S60_EMULATOR_RC_PREFIX).size());
}

QString pathToId(const QString &path)
{
    return QString::fromLatin1(S60_EMULATOR_RC_PREFIX) + path;
}

}

76
// ======== S60EmulatorRunConfiguration
77

78
S60EmulatorRunConfiguration::S60EmulatorRunConfiguration(Qt4BaseTarget *parent, const QString &proFilePath) :
Tobias Hunger's avatar
Tobias Hunger committed
79
    RunConfiguration(parent, QLatin1String(S60_EMULATOR_RC_ID)),
80 81
    m_proFilePath(proFilePath),
    m_validParse(parent->qt4Project()->validParse(proFilePath))
82 83 84 85
{
    ctor();
}

86
S60EmulatorRunConfiguration::S60EmulatorRunConfiguration(Qt4BaseTarget *parent, S60EmulatorRunConfiguration *source) :
Tobias Hunger's avatar
Tobias Hunger committed
87
    RunConfiguration(parent, source),
88 89
    m_proFilePath(source->m_proFilePath),
    m_validParse(source->m_validParse)
90 91 92 93 94
{
    ctor();
}

void S60EmulatorRunConfiguration::ctor()
95 96
{
    if (!m_proFilePath.isEmpty())
97 98
        //: S60 emulator run configuration default display name, %1 is base pro-File name
        setDefaultDisplayName(tr("%1 in Symbian Emulator").arg(QFileInfo(m_proFilePath).completeBaseName()));
99
    else
100 101
        //: S60 emulator run configuration default display name (no pro-file name)
        setDefaultDisplayName(tr("Run on Symbian Emulator"));
102 103 104 105 106
    Qt4Project *pro = qt4Target()->qt4Project();
    connect(pro, SIGNAL(proFileUpdated(Qt4ProjectManager::Internal::Qt4ProFileNode*,bool)),
            this, SLOT(proFileUpdate(Qt4ProjectManager::Internal::Qt4ProFileNode*,bool)));
    connect(pro, SIGNAL(proFileInvalidated(Qt4ProjectManager::Internal::Qt4ProFileNode *)),
            this, SLOT(proFileInvalidated(Qt4ProjectManager::Internal::Qt4ProFileNode *)));
107 108
}

109

110 111 112 113
S60EmulatorRunConfiguration::~S60EmulatorRunConfiguration()
{
}

114
void S60EmulatorRunConfiguration::handleParserState(bool success)
115
{
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    bool enabled = isEnabled();
    m_validParse = success;
    if (enabled != isEnabled()) {
        emit isEnabledChanged(!enabled);
    }
}

void S60EmulatorRunConfiguration::proFileInvalidated(Qt4ProjectManager::Internal::Qt4ProFileNode *pro)
{
    if (m_proFilePath != pro->path())
        return;
    handleParserState(false);
}

void S60EmulatorRunConfiguration::proFileUpdate(Qt4ProjectManager::Internal::Qt4ProFileNode *pro, bool success)
{
    if (m_proFilePath != pro->path())
        return;
    handleParserState(success);
    emit targetInformationChanged();
136 137
}

138
Qt4SymbianTarget *S60EmulatorRunConfiguration::qt4Target() const
dt's avatar
dt committed
139
{
140
    return static_cast<Qt4SymbianTarget *>(target());
dt's avatar
dt committed
141 142
}

143
bool S60EmulatorRunConfiguration::isEnabled(ProjectExplorer::BuildConfiguration *configuration) const
144
{
145 146
    if (!m_validParse)
        return false;
147 148 149 150 151
    Q_ASSERT(configuration->target() == target());
    Q_ASSERT(target()->id() == Constants::S60_EMULATOR_TARGET_ID);

    const Qt4BuildConfiguration *qt4bc = qobject_cast<const Qt4BuildConfiguration *>(configuration);
    return qt4bc && qt4bc->toolChain();
152 153
}

154
QWidget *S60EmulatorRunConfiguration::createConfigurationWidget()
155 156 157 158
{
    return new S60EmulatorRunConfigurationWidget(this);
}

dt's avatar
dt committed
159 160 161 162 163
ProjectExplorer::OutputFormatter *S60EmulatorRunConfiguration::createOutputFormatter() const
{
    return new QtOutputFormatter(qt4Target()->qt4Project());
}

164
QVariantMap S60EmulatorRunConfiguration::toMap() const
165
{
166
    QVariantMap map(ProjectExplorer::RunConfiguration::toMap());
167
    const QDir projectDir = QDir(target()->project()->projectDirectory());
168 169
    map.insert(QLatin1String(PRO_FILE_KEY), projectDir.relativeFilePath(m_proFilePath));
    return map;
170 171
}

172
bool S60EmulatorRunConfiguration::fromMap(const QVariantMap &map)
173
{
174
    const QDir projectDir = QDir(target()->project()->projectDirectory());
175 176
    m_proFilePath = projectDir.filePath(map.value(QLatin1String(PRO_FILE_KEY)).toString());

177 178
    if (m_proFilePath.isEmpty())
        return false;
179 180 181

    m_validParse = qt4Target()->qt4Project()->validParse(m_proFilePath);

182 183 184
    //: S60 emulator run configuration default display name, %1 is base pro-File name
    setDefaultDisplayName(tr("%1 in Symbian Emulator").arg(QFileInfo(m_proFilePath).completeBaseName()));

185
    return RunConfiguration::fromMap(map);
186 187 188 189
}

QString S60EmulatorRunConfiguration::executable() const
{
Tobias Hunger's avatar
Tobias Hunger committed
190
    Qt4BuildConfiguration *qt4bc = qt4Target()->activeBuildConfiguration();
191
    QtVersion *qtVersion = qt4bc->qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
192
    QString baseDir = qtVersion->systemRoot();
193
    QString qmakeBuildConfig = "urel";
194
    if (qt4bc->qmakeBuildConfiguration() & QtVersion::DebugBuild)
195 196 197
        qmakeBuildConfig = "udeb";
    baseDir += "/epoc32/release/winscw/" + qmakeBuildConfig;

dt's avatar
dt committed
198 199 200 201 202
    TargetInformation ti = qt4Target()->qt4Project()->rootProjectNode()->targetInformation(m_proFilePath);
    if (!ti.valid)
        return QString();
    QString executable = QDir::toNativeSeparators(QDir::cleanPath(baseDir + QLatin1Char('/') + ti.target));
    executable += QLatin1String(".exe");
203

dt's avatar
dt committed
204
    return executable;
205 206
}

207 208 209 210 211
QString S60EmulatorRunConfiguration::proFilePath() const
{
    return m_proFilePath;
}

212 213 214 215 216
// ======== S60EmulatorRunConfigurationWidget

S60EmulatorRunConfigurationWidget::S60EmulatorRunConfigurationWidget(S60EmulatorRunConfiguration *runConfiguration,
                                                                     QWidget *parent)
    : QWidget(parent),
217 218 219
    m_runConfiguration(runConfiguration),
    m_detailsWidget(new Utils::DetailsWidget),
    m_executableLabel(new QLabel(m_runConfiguration->executable()))
220
{
221
    m_detailsWidget->setState(Utils::DetailsWidget::NoSummary);
222 223 224 225 226 227 228 229 230 231
    QVBoxLayout *mainBoxLayout = new QVBoxLayout();
    mainBoxLayout->setMargin(0);
    setLayout(mainBoxLayout);
    mainBoxLayout->addWidget(m_detailsWidget);
    QWidget *detailsContainer = new QWidget;
    m_detailsWidget->setWidget(detailsContainer);

    QFormLayout *detailsFormLayout = new QFormLayout();
    detailsFormLayout->setMargin(0);
    detailsContainer->setLayout(detailsFormLayout);
232

233
    detailsFormLayout->addRow(tr("Executable:"), m_executableLabel);
234 235 236

    connect(m_runConfiguration, SIGNAL(targetInformationChanged()),
            this, SLOT(updateTargetInformation()));
237 238 239

    connect(m_runConfiguration, SIGNAL(isEnabledChanged(bool)),
            this, SLOT(runConfigurationEnabledChange(bool)));
240 241

    setEnabled(m_runConfiguration->isEnabled());
242 243 244 245 246 247 248
}

void S60EmulatorRunConfigurationWidget::updateTargetInformation()
{
    m_executableLabel->setText(m_runConfiguration->executable());
}

249 250 251 252 253
void S60EmulatorRunConfigurationWidget::runConfigurationEnabledChange(bool enabled)
{
    setEnabled(enabled);
}

254 255 256 257 258 259 260 261 262 263 264
// ======== S60EmulatorRunConfigurationFactory

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

S60EmulatorRunConfigurationFactory::~S60EmulatorRunConfigurationFactory()
{
}

Tobias Hunger's avatar
Tobias Hunger committed
265
bool S60EmulatorRunConfigurationFactory::canCreate(Target *parent, const QString &id) const
266
{
267
    Qt4SymbianTarget *t = qobject_cast<Qt4SymbianTarget *>(parent);
Tobias Hunger's avatar
Tobias Hunger committed
268
    if (!t ||
269
        t->id() != QLatin1String(Constants::S60_EMULATOR_TARGET_ID))
270
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
271
    return t->qt4Project()->hasApplicationProFile(pathFromId(id));
272 273
}

Tobias Hunger's avatar
Tobias Hunger committed
274
RunConfiguration *S60EmulatorRunConfigurationFactory::create(Target *parent, const QString &id)
275
{
276 277
    if (!canCreate(parent, id))
        return 0;
278
    Qt4SymbianTarget *t = static_cast<Qt4SymbianTarget *>(parent);
Tobias Hunger's avatar
Tobias Hunger committed
279
    return new S60EmulatorRunConfiguration(t, pathFromId(id));
280 281
}

Tobias Hunger's avatar
Tobias Hunger committed
282
bool S60EmulatorRunConfigurationFactory::canRestore(Target *parent, const QVariantMap &map) const
283
{
284
    Qt4SymbianTarget *t = qobject_cast<Qt4SymbianTarget *>(parent);
Tobias Hunger's avatar
Tobias Hunger committed
285
    if (!t ||
286
        t->id() != QLatin1String(Constants::S60_EMULATOR_TARGET_ID))
287 288
        return false;
    QString id(ProjectExplorer::idFromMap(map));
Tobias Hunger's avatar
Tobias Hunger committed
289
    return id == QLatin1String(S60_EMULATOR_RC_ID);
290 291
}

Tobias Hunger's avatar
Tobias Hunger committed
292
RunConfiguration *S60EmulatorRunConfigurationFactory::restore(Target *parent, const QVariantMap &map)
293
{
294 295
    if (!canRestore(parent, map))
        return 0;
296 297
    Qt4SymbianTarget *t = static_cast<Qt4SymbianTarget *>(parent);
    S60EmulatorRunConfiguration *rc = new S60EmulatorRunConfiguration(t, QString());
298 299 300 301 302 303
    if (rc->fromMap(map))
        return rc;
    delete rc;
    return 0;
}

Tobias Hunger's avatar
Tobias Hunger committed
304
bool S60EmulatorRunConfigurationFactory::canClone(Target *parent, RunConfiguration *source) const
305
{
Tobias Hunger's avatar
Tobias Hunger committed
306
    return canCreate(parent, source->id());
307 308
}

Tobias Hunger's avatar
Tobias Hunger committed
309
RunConfiguration *S60EmulatorRunConfigurationFactory::clone(Target *parent, RunConfiguration *source)
310 311 312
{
    if (!canClone(parent, source))
        return 0;
313
    Qt4SymbianTarget *t = static_cast<Qt4SymbianTarget *>(parent);
Tobias Hunger's avatar
Tobias Hunger committed
314
    return new S60EmulatorRunConfiguration(t, QString());
315 316
}

Tobias Hunger's avatar
Tobias Hunger committed
317
QStringList S60EmulatorRunConfigurationFactory::availableCreationIds(Target *parent) const
318
{
319
    Qt4SymbianTarget *t = qobject_cast<Qt4SymbianTarget *>(parent);
Tobias Hunger's avatar
Tobias Hunger committed
320
    if (!t ||
321
        t->id() != QLatin1String(Constants::S60_EMULATOR_TARGET_ID))
322 323
        return QStringList();

Tobias Hunger's avatar
Tobias Hunger committed
324
    return t->qt4Project()->applicationProFilePathes(QLatin1String(S60_EMULATOR_RC_PREFIX));
325 326 327 328
}

QString S60EmulatorRunConfigurationFactory::displayNameForId(const QString &id) const
{
329
    if (!pathFromId(id).isEmpty())
330
        return tr("%1 in Symbian Emulator").arg(QFileInfo(pathFromId(id)).completeBaseName());
331
    return QString();
332 333 334 335
}

// ======== S60EmulatorRunControl

dt's avatar
dt committed
336 337
S60EmulatorRunControl::S60EmulatorRunControl(S60EmulatorRunConfiguration *runConfiguration, QString mode)
    : RunControl(runConfiguration, mode)
338
{
339 340
    // FIXME: This should be configurable!
    Utils::Environment env = runConfiguration->qt4Target()->activeBuildConfiguration()->environment();
341
    m_applicationLauncher.setEnvironment(env);
342 343

    m_executable = runConfiguration->executable();
344 345
    connect(&m_applicationLauncher, SIGNAL(applicationError(QString)),
            this, SLOT(slotError(QString)));
346 347
    connect(&m_applicationLauncher, SIGNAL(appendMessage(QString, ProjectExplorer::OutputFormat)),
            this, SLOT(slotAppendMessage(QString, ProjectExplorer::OutputFormat)));
348 349 350 351 352 353 354 355
    connect(&m_applicationLauncher, SIGNAL(processExited(int)),
            this, SLOT(processExited(int)));
    connect(&m_applicationLauncher, SIGNAL(bringToForegroundRequested(qint64)),
            this, SLOT(bringApplicationToForeground(qint64)));
}

void S60EmulatorRunControl::start()
{
356
    m_applicationLauncher.start(ApplicationLauncher::Gui, m_executable, QString());
357 358
    emit started();

359
    QString msg = tr("Starting %1...").arg(QDir::toNativeSeparators(m_executable));
360
    appendMessage(msg, NormalMessageFormat);
361 362
}

363
RunControl::StopResult S60EmulatorRunControl::stop()
364 365
{
    m_applicationLauncher.stop();
366
    return StoppedSynchronously;
367 368 369 370 371 372 373
}

bool S60EmulatorRunControl::isRunning() const
{
    return m_applicationLauncher.isRunning();
}

374 375 376 377 378
QIcon S60EmulatorRunControl::icon() const
{
    return QIcon(ProjectExplorer::Constants::ICON_RUN_SMALL);
}

379 380
void S60EmulatorRunControl::slotError(const QString & err)
{
381
    appendMessage(err, ErrorMessageFormat);
382 383 384
    emit finished();
}

385
void S60EmulatorRunControl::slotAppendMessage(const QString &line, OutputFormat format)
386 387 388 389
{
    static QString prefix = tr("[Qt Message]");
    static int prefixLength = prefix.length();
    int index = line.indexOf(prefix);
390
    if (index != -1)
391
        appendMessage(line.mid(index + prefixLength + 1), format);
392 393 394 395
}

void S60EmulatorRunControl::processExited(int exitCode)
{
396
    QString msg = tr("%1 exited with code %2");
397
    appendMessage(msg, exitCode ? ErrorMessageFormat : NormalMessageFormat);
398 399
    emit finished();
}