OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qt-s60 / s60emulatorrunconfiguration.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "s60emulatorrunconfiguration.h"
35
36 #include "qt4project.h"
37 #include "qt4target.h"
38 #include "s60manager.h"
39 #include "qt4projectmanagerconstants.h"
40 #include "qtoutputformatter.h"
41
42 #include <utils/qtcassert.h>
43 #include <utils/detailswidget.h>
44
45 #include <QtGui/QLabel>
46 #include <QtGui/QVBoxLayout>
47 #include <QtGui/QFormLayout>
48
49 using namespace ProjectExplorer;
50 using namespace Qt4ProjectManager;
51 using namespace Qt4ProjectManager::Internal;
52
53 namespace {
54 const char * const S60_EMULATOR_RC_ID("Qt4ProjectManager.S60EmulatorRunConfiguration");
55 const char * const S60_EMULATOR_RC_PREFIX("Qt4ProjectManager.S60EmulatorRunConfiguration.");
56
57 const char * const PRO_FILE_KEY("Qt4ProjectManager.S60EmulatorRunConfiguration.ProFile");
58
59 QString pathFromId(const QString &id)
60 {
61     if (!id.startsWith(QLatin1String(S60_EMULATOR_RC_PREFIX)))
62         return QString();
63     return id.mid(QString::fromLatin1(S60_EMULATOR_RC_PREFIX).size());
64 }
65
66 QString pathToId(const QString &path)
67 {
68     return QString::fromLatin1(S60_EMULATOR_RC_PREFIX) + path;
69 }
70
71 }
72
73 // ======== S60EmulatorRunConfiguration
74
75 S60EmulatorRunConfiguration::S60EmulatorRunConfiguration(Qt4Target *parent, const QString &proFilePath) :
76     RunConfiguration(parent, QLatin1String(S60_EMULATOR_RC_ID)),
77     m_proFilePath(proFilePath),
78     m_validParse(parent->qt4Project()->validParse(proFilePath))
79 {
80     ctor();
81 }
82
83 S60EmulatorRunConfiguration::S60EmulatorRunConfiguration(Qt4Target *parent, S60EmulatorRunConfiguration *source) :
84     RunConfiguration(parent, source),
85     m_proFilePath(source->m_proFilePath),
86     m_validParse(source->m_validParse)
87 {
88     ctor();
89 }
90
91 void S60EmulatorRunConfiguration::ctor()
92 {
93     if (!m_proFilePath.isEmpty())
94         //: S60 emulator run configuration default display name, %1 is base pro-File name
95         setDefaultDisplayName(tr("%1 in Symbian Emulator").arg(QFileInfo(m_proFilePath).completeBaseName()));
96     else
97         //: S60 emulator run configuration default display name (no pro-file name)
98         setDefaultDisplayName(tr("Run on Symbian Emulator"));
99     Qt4Project *pro = qt4Target()->qt4Project();
100     connect(pro, SIGNAL(proFileUpdated(Qt4ProjectManager::Internal::Qt4ProFileNode*,bool)),
101             this, SLOT(proFileUpdate(Qt4ProjectManager::Internal::Qt4ProFileNode*,bool)));
102     connect(pro, SIGNAL(proFileInvalidated(Qt4ProjectManager::Internal::Qt4ProFileNode *)),
103             this, SLOT(proFileInvalidated(Qt4ProjectManager::Internal::Qt4ProFileNode *)));
104 }
105
106
107 S60EmulatorRunConfiguration::~S60EmulatorRunConfiguration()
108 {
109 }
110
111 void S60EmulatorRunConfiguration::handleParserState(bool success)
112 {
113     bool enabled = isEnabled();
114     m_validParse = success;
115     if (enabled != isEnabled()) {
116         emit isEnabledChanged(!enabled);
117     }
118 }
119
120 void S60EmulatorRunConfiguration::proFileInvalidated(Qt4ProjectManager::Internal::Qt4ProFileNode *pro)
121 {
122     if (m_proFilePath != pro->path())
123         return;
124     handleParserState(false);
125 }
126
127 void S60EmulatorRunConfiguration::proFileUpdate(Qt4ProjectManager::Internal::Qt4ProFileNode *pro, bool success)
128 {
129     if (m_proFilePath != pro->path())
130         return;
131     handleParserState(success);
132     emit targetInformationChanged();
133 }
134
135 Qt4Target *S60EmulatorRunConfiguration::qt4Target() const
136 {
137     return static_cast<Qt4Target *>(target());
138 }
139
140 bool S60EmulatorRunConfiguration::isEnabled(ProjectExplorer::BuildConfiguration *configuration) const
141 {
142     if (!m_validParse)
143         return false;
144     Qt4BuildConfiguration *qt4bc = qobject_cast<Qt4BuildConfiguration *>(configuration);
145     QTC_ASSERT(qt4bc, return false);
146     const ProjectExplorer::ToolChainType type = qt4bc->toolChainType();
147     return type == ProjectExplorer::ToolChain_WINSCW;
148 }
149
150 QWidget *S60EmulatorRunConfiguration::createConfigurationWidget()
151 {
152     return new S60EmulatorRunConfigurationWidget(this);
153 }
154
155 ProjectExplorer::OutputFormatter *S60EmulatorRunConfiguration::createOutputFormatter() const
156 {
157     return new QtOutputFormatter(qt4Target()->qt4Project());
158 }
159
160 QVariantMap S60EmulatorRunConfiguration::toMap() const
161 {
162     QVariantMap map(ProjectExplorer::RunConfiguration::toMap());
163     const QDir projectDir = QDir(target()->project()->projectDirectory());
164     map.insert(QLatin1String(PRO_FILE_KEY), projectDir.relativeFilePath(m_proFilePath));
165     return map;
166 }
167
168 bool S60EmulatorRunConfiguration::fromMap(const QVariantMap &map)
169 {
170     const QDir projectDir = QDir(target()->project()->projectDirectory());
171     m_proFilePath = projectDir.filePath(map.value(QLatin1String(PRO_FILE_KEY)).toString());
172
173     if (m_proFilePath.isEmpty())
174         return false;
175
176     m_validParse = qt4Target()->qt4Project()->validParse(m_proFilePath);
177
178     //: S60 emulator run configuration default display name, %1 is base pro-File name
179     setDefaultDisplayName(tr("%1 in Symbian Emulator").arg(QFileInfo(m_proFilePath).completeBaseName()));
180
181     return RunConfiguration::fromMap(map);
182 }
183
184 QString S60EmulatorRunConfiguration::executable() const
185 {
186     Qt4BuildConfiguration *qt4bc = qt4Target()->activeBuildConfiguration();
187     QtVersion *qtVersion = qt4bc->qtVersion();
188     QString baseDir = S60Manager::instance()->deviceForQtVersion(qtVersion).epocRoot;
189     QString qmakeBuildConfig = "urel";
190     if (qt4bc->qmakeBuildConfiguration() & QtVersion::DebugBuild)
191         qmakeBuildConfig = "udeb";
192     baseDir += "/epoc32/release/winscw/" + qmakeBuildConfig;
193
194     TargetInformation ti = qt4Target()->qt4Project()->rootProjectNode()->targetInformation(m_proFilePath);
195     if (!ti.valid)
196         return QString();
197     QString executable = QDir::toNativeSeparators(QDir::cleanPath(baseDir + QLatin1Char('/') + ti.target));
198     executable += QLatin1String(".exe");
199
200     return executable;
201 }
202
203 // ======== S60EmulatorRunConfigurationWidget
204
205 S60EmulatorRunConfigurationWidget::S60EmulatorRunConfigurationWidget(S60EmulatorRunConfiguration *runConfiguration,
206                                                                      QWidget *parent)
207     : QWidget(parent),
208     m_runConfiguration(runConfiguration),
209     m_detailsWidget(new Utils::DetailsWidget),
210     m_executableLabel(new QLabel(m_runConfiguration->executable()))
211 {
212     m_detailsWidget->setState(Utils::DetailsWidget::NoSummary);
213     QVBoxLayout *mainBoxLayout = new QVBoxLayout();
214     mainBoxLayout->setMargin(0);
215     setLayout(mainBoxLayout);
216     mainBoxLayout->addWidget(m_detailsWidget);
217     QWidget *detailsContainer = new QWidget;
218     m_detailsWidget->setWidget(detailsContainer);
219
220     QFormLayout *detailsFormLayout = new QFormLayout();
221     detailsFormLayout->setMargin(0);
222     detailsContainer->setLayout(detailsFormLayout);
223
224     detailsFormLayout->addRow(tr("Executable:"), m_executableLabel);
225
226     connect(m_runConfiguration, SIGNAL(targetInformationChanged()),
227             this, SLOT(updateTargetInformation()));
228
229     connect(m_runConfiguration, SIGNAL(isEnabledChanged(bool)),
230             this, SLOT(runConfigurationEnabledChange(bool)));
231
232     setEnabled(m_runConfiguration->isEnabled());
233 }
234
235 void S60EmulatorRunConfigurationWidget::updateTargetInformation()
236 {
237     m_executableLabel->setText(m_runConfiguration->executable());
238 }
239
240 void S60EmulatorRunConfigurationWidget::runConfigurationEnabledChange(bool enabled)
241 {
242     setEnabled(enabled);
243 }
244
245 // ======== S60EmulatorRunConfigurationFactory
246
247 S60EmulatorRunConfigurationFactory::S60EmulatorRunConfigurationFactory(QObject *parent)
248     : IRunConfigurationFactory(parent)
249 {
250 }
251
252 S60EmulatorRunConfigurationFactory::~S60EmulatorRunConfigurationFactory()
253 {
254 }
255
256 bool S60EmulatorRunConfigurationFactory::canCreate(Target *parent, const QString &id) const
257 {
258     Qt4Target * t(qobject_cast<Qt4Target *>(parent));
259     if (!t ||
260         t->id() != QLatin1String(Constants::S60_EMULATOR_TARGET_ID))
261         return false;
262     return t->qt4Project()->hasApplicationProFile(pathFromId(id));
263 }
264
265 RunConfiguration *S60EmulatorRunConfigurationFactory::create(Target *parent, const QString &id)
266 {
267     if (!canCreate(parent, id))
268         return 0;
269     Qt4Target *t(static_cast<Qt4Target *>(parent));
270     return new S60EmulatorRunConfiguration(t, pathFromId(id));
271 }
272
273 bool S60EmulatorRunConfigurationFactory::canRestore(Target *parent, const QVariantMap &map) const
274 {
275     Qt4Target * t(qobject_cast<Qt4Target *>(parent));
276     if (!t ||
277         t->id() != QLatin1String(Constants::S60_EMULATOR_TARGET_ID))
278         return false;
279     QString id(ProjectExplorer::idFromMap(map));
280     return id == QLatin1String(S60_EMULATOR_RC_ID);
281 }
282
283 RunConfiguration *S60EmulatorRunConfigurationFactory::restore(Target *parent, const QVariantMap &map)
284 {
285     if (!canRestore(parent, map))
286         return 0;
287     Qt4Target *t(static_cast<Qt4Target *>(parent));
288     S60EmulatorRunConfiguration *rc(new S60EmulatorRunConfiguration(t, QString()));
289     if (rc->fromMap(map))
290         return rc;
291     delete rc;
292     return 0;
293 }
294
295 bool S60EmulatorRunConfigurationFactory::canClone(Target *parent, RunConfiguration *source) const
296 {
297     return canCreate(parent, source->id());
298 }
299
300 RunConfiguration *S60EmulatorRunConfigurationFactory::clone(Target *parent, RunConfiguration *source)
301 {
302     if (!canClone(parent, source))
303         return 0;
304     Qt4Target *t(static_cast<Qt4Target *>(parent));
305     return new S60EmulatorRunConfiguration(t, QString());
306 }
307
308 QStringList S60EmulatorRunConfigurationFactory::availableCreationIds(Target *parent) const
309 {
310     Qt4Target * t(qobject_cast<Qt4Target *>(parent));
311     if (!t ||
312         t->id() != QLatin1String(Constants::S60_EMULATOR_TARGET_ID))
313         return QStringList();
314
315     return t->qt4Project()->applicationProFilePathes(QLatin1String(S60_EMULATOR_RC_PREFIX));
316 }
317
318 QString S60EmulatorRunConfigurationFactory::displayNameForId(const QString &id) const
319 {
320         if (!pathFromId(id).isEmpty())
321         return tr("%1 in Symbian Emulator").arg(QFileInfo(pathFromId(id)).completeBaseName());
322         return QString();
323 }
324
325 // ======== S60EmulatorRunControl
326
327 S60EmulatorRunControl::S60EmulatorRunControl(S60EmulatorRunConfiguration *runConfiguration, QString mode)
328     : RunControl(runConfiguration, mode)
329 {
330     // stuff like the EPOCROOT and EPOCDEVICE env variable
331     Utils::Environment env = Utils::Environment::systemEnvironment();
332     runConfiguration->qt4Target()->activeBuildConfiguration()->toolChain()->addToEnvironment(env);
333     m_applicationLauncher.setEnvironment(env);
334
335     m_executable = runConfiguration->executable();
336     connect(&m_applicationLauncher, SIGNAL(applicationError(QString)),
337             this, SLOT(slotError(QString)));
338     connect(&m_applicationLauncher, SIGNAL(appendMessage(QString, ProjectExplorer::OutputFormat)),
339             this, SLOT(slotAppendMessage(QString, ProjectExplorer::OutputFormat)));
340     connect(&m_applicationLauncher, SIGNAL(processExited(int)),
341             this, SLOT(processExited(int)));
342     connect(&m_applicationLauncher, SIGNAL(bringToForegroundRequested(qint64)),
343             this, SLOT(bringApplicationToForeground(qint64)));
344 }
345
346 void S60EmulatorRunControl::start()
347 {
348     m_applicationLauncher.start(ApplicationLauncher::Gui, m_executable, QString());
349     emit started();
350
351     QString msg = tr("Starting %1...").arg(QDir::toNativeSeparators(m_executable));
352     appendMessage(msg, NormalMessageFormat);
353 }
354
355 RunControl::StopResult S60EmulatorRunControl::stop()
356 {
357     m_applicationLauncher.stop();
358     return StoppedSynchronously;
359 }
360
361 bool S60EmulatorRunControl::isRunning() const
362 {
363     return m_applicationLauncher.isRunning();
364 }
365
366 void S60EmulatorRunControl::slotError(const QString & err)
367 {
368     appendMessage(err, ErrorMessageFormat);
369     emit finished();
370 }
371
372 void S60EmulatorRunControl::slotAppendMessage(const QString &line, OutputFormat format)
373 {
374     static QString prefix = tr("[Qt Message]");
375     static int prefixLength = prefix.length();
376     int index = line.indexOf(prefix);
377     if (index != -1)
378         appendMessage(line.mid(index + prefixLength + 1), format);
379 }
380
381 void S60EmulatorRunControl::processExited(int exitCode)
382 {
383     QString msg = tr("%1 exited with code %2");
384     appendMessage(msg, exitCode ? ErrorMessageFormat : NormalMessageFormat);
385     emit finished();
386 }