1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "s60deploystep.h"
36 #include "qt4buildconfiguration.h"
37 #include "s60deployconfiguration.h"
38 #include "s60devicerunconfiguration.h"
39 #include "s60runconfigbluetoothstarter.h"
41 #include <coreplugin/icore.h>
42 #include <projectexplorer/buildsteplist.h>
43 #include <projectexplorer/target.h>
44 #include <projectexplorer/projectexplorerconstants.h>
45 #include <qt4projectmanagerconstants.h>
47 #include <symbianutils/launcher.h>
48 #include <symbianutils/symbiandevicemanager.h>
50 #include <QtGui/QMessageBox>
51 #include <QtGui/QMainWindow>
53 #include <QtCore/QTimer>
54 #include <QtCore/QDateTime>
55 #include <QtCore/QDir>
56 #include <QtCore/QEventLoop>
58 using namespace ProjectExplorer;
59 using namespace Qt4ProjectManager::Internal;
62 const char * const S60_DEPLOY_STEP_ID = "Qt4ProjectManager.S60DeployStep";
65 static inline bool ensureDeleteFile(const QString &fileName, QString *errorMessage)
68 if (file.exists() && !file.remove()) {
69 *errorMessage = S60DeployStep::tr("Unable to remove existing file '%1': %2").arg(fileName, file.errorString());
75 static inline bool renameFile(const QString &sourceName, const QString &targetName,
76 QString *errorMessage)
78 if (sourceName == targetName)
80 if (!ensureDeleteFile(targetName, errorMessage))
82 QFile source(sourceName);
83 if (!source.rename(targetName)) {
84 *errorMessage = S60DeployStep::tr("Unable to rename file '%1' to '%2': %3")
85 .arg(sourceName, targetName, source.errorString());
91 // #pragma mark -- S60DeployStep
93 S60DeployStep::S60DeployStep(ProjectExplorer::BuildStepList *bc,
95 BuildStep(bc, bs), m_timer(0),
96 m_releaseDeviceAfterLauncherFinish(bs->m_releaseDeviceAfterLauncherFinish),
97 m_handleDeviceRemoval(bs->m_handleDeviceRemoval),
98 m_launcher(0), m_eventLoop(0)
103 S60DeployStep::S60DeployStep(ProjectExplorer::BuildStepList *bc):
104 BuildStep(bc, QLatin1String(S60_DEPLOY_STEP_ID)), m_timer(0),
105 m_releaseDeviceAfterLauncherFinish(true),
106 m_handleDeviceRemoval(true), m_launcher(0), m_eventLoop(0)
111 void S60DeployStep::ctor()
113 //: Qt4 Deploystep display name
114 setDefaultDisplayName(tr("Deploy"));
117 S60DeployStep::~S60DeployStep()
125 bool S60DeployStep::init()
127 Qt4BuildConfiguration *bc = static_cast<Qt4BuildConfiguration *>(buildConfiguration());
128 S60DeployConfiguration* deployConfiguration = static_cast<S60DeployConfiguration *>(bc->target()->activeDeployConfiguration());
129 if(!deployConfiguration)
131 m_serialPortName = deployConfiguration->serialPortName();
132 m_serialPortFriendlyName = SymbianUtils::SymbianDeviceManager::instance()->friendlyNameForPort(m_serialPortName);
133 m_packageFileNamesWithTarget = deployConfiguration->packageFileNamesWithTargetInfo();
134 m_signedPackages = deployConfiguration->signedPackages();
135 m_installationDrive = deployConfiguration->installationDrive();
136 m_silentInstall = deployConfiguration->silentInstall();
140 trk::Launcher::releaseToDeviceManager(m_launcher);
145 m_launcher = trk::Launcher::acquireFromDeviceManager(m_serialPortName, this, &message);
146 if (!message.isEmpty() || !m_launcher) {
148 trk::Launcher::releaseToDeviceManager(m_launcher);
151 appendMessage(message, true);
154 // Prompt the user to start up the Blue tooth connection
155 const trk::PromptStartCommunicationResult src =
156 S60RunConfigBluetoothStarter::startCommunication(m_launcher->trkDevice(),
158 if (src != trk::PromptStartCommunicationConnected) {
159 if (!message.isEmpty())
160 trk::Launcher::releaseToDeviceManager(m_launcher);
163 appendMessage(message, true);
169 QVariantMap S60DeployStep::toMap() const
171 return BuildStep::toMap();
174 bool S60DeployStep::fromMap(const QVariantMap &map)
176 return BuildStep::fromMap(map);
179 void S60DeployStep::appendMessage(const QString &error, bool isError)
181 emit addOutput(error, isError?ProjectExplorer::BuildStep::ErrorMessageOutput:
182 ProjectExplorer::BuildStep::MessageOutput);
185 bool S60DeployStep::processPackageName(QString &errorMessage)
187 for (int i = 0; i < m_signedPackages.count(); ++i) {
188 QFileInfo packageInfo(m_signedPackages.at(i));
189 // support for 4.6.1 and pre, where make sis creates 'targetname_armX_udeb.sis' instead of 'targetname.sis'
190 QFileInfo packageWithTargetInfo(m_packageFileNamesWithTarget.at(i));
191 // does the 4.6.1 version exist?
192 if (packageWithTargetInfo.exists() && packageWithTargetInfo.isFile()) {
193 // is the 4.6.1 version newer? (to guard against behavior change Qt Creator 1.3 --> 2.0)
194 if (!packageInfo.exists() || packageInfo.lastModified() < packageWithTargetInfo.lastModified()) { //TODO change the QtCore
195 // the 'targetname_armX_udeb.sis' crap exists and is new, rename it
196 appendMessage(tr("Renaming new package '%1' to '%2'")
197 .arg(QDir::toNativeSeparators(m_packageFileNamesWithTarget.at(i)),
198 QDir::toNativeSeparators(m_signedPackages.at(i))), false);
199 return renameFile(m_packageFileNamesWithTarget.at(i), m_signedPackages.at(i), &errorMessage);
201 // the 'targetname_armX_udeb.sis' crap exists but is old, remove it
202 appendMessage(tr("Removing old package '%1'")
203 .arg(QDir::toNativeSeparators(m_packageFileNamesWithTarget.at(i))),
205 ensureDeleteFile(m_packageFileNamesWithTarget.at(i), &errorMessage);
208 if (!packageInfo.exists() || !packageInfo.isFile()) {
209 errorMessage = tr("'%1': Package file not found").arg(m_signedPackages.at(i));
216 void S60DeployStep::start()
218 QString errorMessage;
220 if (m_serialPortName.isEmpty() || !m_launcher) {
221 errorMessage = tr("No device is connected. Please connect a device and try again.");
222 appendMessage(errorMessage, true);
223 emit addTask(ProjectExplorer::Task(ProjectExplorer::Task::Error,
226 ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
231 // make sure we have the right name of the sis package
232 if (processPackageName(errorMessage)) {
235 errorMessage = tr("Failed to find package %1").arg(errorMessage);
236 appendMessage(errorMessage, true);
237 emit addTask(ProjectExplorer::Task(ProjectExplorer::Task::Error,
240 ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
246 void S60DeployStep::stop()
249 m_launcher->terminate();
253 void S60DeployStep::setupConnections()
255 connect(SymbianUtils::SymbianDeviceManager::instance(), SIGNAL(deviceRemoved(SymbianUtils::SymbianDevice)),
256 this, SLOT(deviceRemoved(SymbianUtils::SymbianDevice)));
257 connect(m_launcher, SIGNAL(finished()), this, SLOT(launcherFinished()));
259 connect(m_launcher, SIGNAL(canNotConnect(QString)), this, SLOT(connectFailed(QString)));
260 connect(m_launcher, SIGNAL(copyingStarted(QString)), this, SLOT(printCopyingNotice(QString)));
261 connect(m_launcher, SIGNAL(canNotCreateFile(QString,QString)), this, SLOT(createFileFailed(QString,QString)));
262 connect(m_launcher, SIGNAL(canNotWriteFile(QString,QString)), this, SLOT(writeFileFailed(QString,QString)));
263 connect(m_launcher, SIGNAL(canNotCloseFile(QString,QString)), this, SLOT(closeFileFailed(QString,QString)));
264 connect(m_launcher, SIGNAL(installingStarted(QString)), this, SLOT(printInstallingNotice(QString)));
265 connect(m_launcher, SIGNAL(canNotInstall(QString,QString)), this, SLOT(installFailed(QString,QString)));
266 connect(m_launcher, SIGNAL(installingFinished()), this, SLOT(printInstallingFinished()));
267 connect(m_launcher, SIGNAL(stateChanged(int)), this, SLOT(slotLauncherStateChanged(int)));
270 void S60DeployStep::startDeployment()
272 Q_ASSERT(m_launcher);
277 foreach (const QString &signedPackage, m_signedPackages)
278 copyDst << QString::fromLatin1("%1:\\Data\\%2").arg(m_installationDrive).arg(QFileInfo(signedPackage).fileName());
280 m_launcher->setCopyFileNames(m_signedPackages, copyDst);
281 m_launcher->setInstallFileNames(copyDst);
282 m_launcher->setInstallationDrive(m_installationDrive);
283 m_launcher->setInstallationMode(m_silentInstall?trk::Launcher::InstallationModeSilentAndUser:
284 trk::Launcher::InstallationModeUser);
285 m_launcher->addStartupActions(trk::Launcher::ActionCopyInstall);
287 // TODO readd information about packages? msgListFile(m_signedPackage)
288 appendMessage(tr("Deploying application to '%2'...").arg(m_serialPortFriendlyName), false);
290 QString errorMessage;
291 if (!m_launcher->startServer(&errorMessage)) {
292 errorMessage = tr("Could not connect to phone on port '%1': %2\n"
293 "Check if the phone is connected and App TRK is running.").arg(m_serialPortName, errorMessage);
294 appendMessage(errorMessage, true);
300 void S60DeployStep::run(QFutureInterface<bool> &fi)
302 m_futureInterface = &fi;
303 m_deployResult = true;
304 connect(this, SIGNAL(finished()),
305 this, SLOT(launcherFinished()));
306 connect(this, SIGNAL(finishNow()),
307 this, SLOT(launcherFinished()), Qt::DirectConnection);
310 m_timer = new QTimer();
311 connect(m_timer, SIGNAL(timeout()), this, SLOT(checkForCancel()), Qt::DirectConnection);
313 m_eventLoop = new QEventLoop();
321 fi.reportResult(m_deployResult);
322 m_futureInterface = 0;
325 void S60DeployStep::setReleaseDeviceAfterLauncherFinish(bool v)
327 m_releaseDeviceAfterLauncherFinish = v;
330 void S60DeployStep::slotLauncherStateChanged(int s)
332 if (s == trk::Launcher::WaitingForTrk) {
333 QMessageBox *mb = S60DeviceRunControl::createTrkWaitingMessageBox(m_launcher->trkServerName(),
334 Core::ICore::instance()->mainWindow());
335 connect(m_launcher, SIGNAL(stateChanged(int)), mb, SLOT(close()));
336 connect(mb, SIGNAL(finished(int)), this, SLOT(slotWaitingForTrkClosed()));
341 void S60DeployStep::slotWaitingForTrkClosed()
343 if (m_launcher && m_launcher->state() == trk::Launcher::WaitingForTrk) {
345 appendMessage(tr("Canceled."), true);
350 void S60DeployStep::createFileFailed(const QString &filename, const QString &errorMessage)
352 appendMessage(tr("Could not create file %1 on device: %2").arg(filename, errorMessage), true);
353 m_deployResult = false;
356 void S60DeployStep::writeFileFailed(const QString &filename, const QString &errorMessage)
358 appendMessage(tr("Could not write to file %1 on device: %2").arg(filename, errorMessage), true);
359 m_deployResult = false;
362 void S60DeployStep::closeFileFailed(const QString &filename, const QString &errorMessage)
364 const QString msg = tr("Could not close file %1 on device: %2. It will be closed when App TRK is closed.");
365 appendMessage( msg.arg(filename, errorMessage), true);
366 m_deployResult = false;
369 void S60DeployStep::connectFailed(const QString &errorMessage)
371 appendMessage(tr("Could not connect to App TRK on device: %1. Restarting App TRK might help.").arg(errorMessage), true);
372 m_deployResult = false;
375 void S60DeployStep::printCopyingNotice(const QString &fileName)
377 appendMessage(tr("Copying \"%1\"...").arg(fileName), false);
380 void S60DeployStep::printInstallingNotice(const QString &packageName)
382 appendMessage(tr("Installing package \"%1\" on drive %2:...").arg(packageName).arg(m_installationDrive), false);
385 void S60DeployStep::printInstallingFinished()
387 appendMessage(tr("Installation has finished"), false);
390 void S60DeployStep::installFailed(const QString &filename, const QString &errorMessage)
392 appendMessage(tr("Could not install from package %1 on device: %2").arg(filename, errorMessage), true);
393 m_deployResult = false;
396 void S60DeployStep::checkForCancel()
398 if (m_futureInterface->isCanceled() && m_timer->isActive()) {
401 appendMessage(tr("Canceled."), true);
406 void S60DeployStep::launcherFinished()
408 if (m_releaseDeviceAfterLauncherFinish && m_launcher) {
409 m_handleDeviceRemoval = false;
410 trk::Launcher::releaseToDeviceManager(m_launcher);
413 m_launcher->deleteLater();
419 void S60DeployStep::deviceRemoved(const SymbianUtils::SymbianDevice &d)
421 if (m_handleDeviceRemoval && d.portName() == m_serialPortName) {
422 appendMessage(tr("The device '%1' has been disconnected").arg(d.friendlyName()), true);
427 // #pragma mark -- S60DeployStepWidget
429 BuildStepConfigWidget *S60DeployStep::createConfigWidget()
431 return new S60DeployStepWidget();
434 S60DeployStepWidget::S60DeployStepWidget() : ProjectExplorer::BuildStepConfigWidget()
438 void S60DeployStepWidget::init()
442 QString S60DeployStepWidget::summaryText() const
444 return QString("<b>%1</b>").arg(displayName());
447 QString S60DeployStepWidget::displayName() const
449 return tr("Deploy SIS Package");
452 // #pragma mark -- S60DeployStepFactory
454 S60DeployStepFactory::S60DeployStepFactory(QObject *parent) :
455 ProjectExplorer::IBuildStepFactory(parent)
459 S60DeployStepFactory::~S60DeployStepFactory()
463 bool S60DeployStepFactory::canCreate(ProjectExplorer::BuildStepList *parent, const QString &id) const
465 if (parent->id() != QLatin1String(ProjectExplorer::Constants::BUILDSTEPS_DEPLOY))
467 if (parent->target()->id() != QLatin1String(Constants::S60_DEVICE_TARGET_ID))
469 return (id == QLatin1String(S60_DEPLOY_STEP_ID));
472 ProjectExplorer::BuildStep *S60DeployStepFactory::create(ProjectExplorer::BuildStepList *parent, const QString &id)
474 if (!canCreate(parent, id))
476 return new S60DeployStep(parent);
479 bool S60DeployStepFactory::canClone(ProjectExplorer::BuildStepList *parent, ProjectExplorer::BuildStep *source) const
481 if (!canCreate(parent, source->id()))
483 if (!qobject_cast<S60DeployStep *>(source))
488 ProjectExplorer::BuildStep *S60DeployStepFactory::clone(ProjectExplorer::BuildStepList *parent, ProjectExplorer::BuildStep *source)
490 if (!canClone(parent, source))
492 return new S60DeployStep(parent, static_cast<S60DeployStep *>(source));
495 bool S60DeployStepFactory::canRestore(ProjectExplorer::BuildStepList *parent, const QVariantMap &map) const
497 QString id(ProjectExplorer::idFromMap(map));
498 return canCreate(parent, id);
501 ProjectExplorer::BuildStep *S60DeployStepFactory::restore(ProjectExplorer::BuildStepList *parent, const QVariantMap &map)
503 if (!canRestore(parent, map))
505 S60DeployStep *bs = new S60DeployStep(parent);
506 if (bs->fromMap(map))
512 QStringList S60DeployStepFactory::availableCreationIds(ProjectExplorer::BuildStepList *parent) const
514 if (parent->id() == QLatin1String(ProjectExplorer::Constants::BUILDSTEPS_DEPLOY)
515 && parent->target()->id() == QLatin1String(Constants::S60_DEVICE_TARGET_ID))
516 return QStringList() << QLatin1String(S60_DEPLOY_STEP_ID);
517 return QStringList();
520 QString S60DeployStepFactory::displayNameForId(const QString &id) const
522 if (id == QLatin1String(S60_DEPLOY_STEP_ID))
523 return tr("Deploy SIS Package");