summaryrefslogtreecommitdiff
path: root/src/plugins/qt4projectmanager/qt-s60/s60deploystep.cpp
blob: dbad2092b9146e514ac9a20c165c2b649e5d80d0 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
95
96
97
98
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
141
142
143
144
145
146
147
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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.
**
** 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.
**
** 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "s60deploystep.h"

#include "qt4buildconfiguration.h"
#include "s60deployconfiguration.h"
#include "s60devicerunconfiguration.h"
#include "s60runconfigbluetoothstarter.h"
#include "codadevice.h"
#include "trkruncontrol.h"
#include "codaruncontrol.h"

#include <coreplugin/icore.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/target.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <qt4projectmanagerconstants.h>

#include <symbianutils/launcher.h>
#include <symbianutils/symbiandevicemanager.h>
#include <utils/qtcassert.h>

#include <QtGui/QMessageBox>
#include <QtGui/QMainWindow>

#include <QtCore/QTimer>
#include <QtCore/QDateTime>
#include <QtCore/QDir>
#include <QtCore/QEventLoop>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>

#include <QtNetwork/QTcpSocket>

using namespace ProjectExplorer;
using namespace Qt4ProjectManager::Internal;

enum { debug = 0 };

static const quint64  DEFAULT_CHUNK_SIZE = 40000;

namespace {
const char * const S60_DEPLOY_STEP_ID = "Qt4ProjectManager.S60DeployStep";
}

static inline bool ensureDeleteFile(const QString &fileName, QString *errorMessage)
{
    QFile file(fileName);
    if (file.exists() && !file.remove()) {
        *errorMessage = S60DeployStep::tr("Unable to remove existing file '%1': %2").arg(fileName, file.errorString());
        return false;
    }
    return true;
}

static inline bool renameFile(const QString &sourceName, const QString &targetName,
                              QString *errorMessage)
{
    if (sourceName == targetName)
        return true;
    if (!ensureDeleteFile(targetName, errorMessage))
        return false;
    QFile source(sourceName);
    if (!source.rename(targetName)) {
        *errorMessage = S60DeployStep::tr("Unable to rename file '%1' to '%2': %3")
                .arg(sourceName, targetName, source.errorString());
        return false;
    }
    return true;
}

// #pragma mark -- S60DeployStep

S60DeployStep::S60DeployStep(ProjectExplorer::BuildStepList *bc,
                             S60DeployStep *bs):
    BuildStep(bc, bs), m_timer(0),
    m_timeoutTimer(new QTimer(this)),
    m_releaseDeviceAfterLauncherFinish(bs->m_releaseDeviceAfterLauncherFinish),
    m_handleDeviceRemoval(bs->m_handleDeviceRemoval),
    m_launcher(0),
    m_eventLoop(0),
    m_state(StateUninit),
    m_putWriteOk(false),
    m_putLastChunkSize(0),
    m_putChunkSize(DEFAULT_CHUNK_SIZE),
    m_currentFileIndex(0),
    m_channel(bs->m_channel),
    m_deployCanceled(false),
    m_copyProgress(0)
{
    ctor();
}

S60DeployStep::S60DeployStep(ProjectExplorer::BuildStepList *bc):
    BuildStep(bc, QLatin1String(S60_DEPLOY_STEP_ID)), m_timer(0),
    m_timeoutTimer(new QTimer(this)),
    m_releaseDeviceAfterLauncherFinish(true),
    m_handleDeviceRemoval(true),
    m_launcher(0),
    m_eventLoop(0),
    m_state(StateUninit),
    m_putWriteOk(false),
    m_putLastChunkSize(0),
    m_putChunkSize(DEFAULT_CHUNK_SIZE),
    m_currentFileIndex(0),
    m_channel(S60DeployConfiguration::CommunicationTrkSerialConnection),
    m_deployCanceled(false),
    m_copyProgress(0)
{
    ctor();
}

void S60DeployStep::ctor()
{
    //: Qt4 Deploystep display name
    setDefaultDisplayName(tr("Deploy"));
    m_timeoutTimer->setSingleShot(true);
    m_timeoutTimer->setInterval(2000);
    connect(m_timeoutTimer, SIGNAL(timeout()), this, SLOT(timeout()));
}

S60DeployStep::~S60DeployStep()
{
    delete m_timer;
    delete m_launcher;
    delete m_eventLoop;
}


bool S60DeployStep::init()
{
    Qt4BuildConfiguration *bc = static_cast<Qt4BuildConfiguration *>(buildConfiguration());
    S60DeployConfiguration *deployConfiguration = static_cast<S60DeployConfiguration *>(bc->target()->activeDeployConfiguration());
    if (!deployConfiguration)
        return false;
    m_serialPortName = deployConfiguration->serialPortName();
    m_serialPortFriendlyName = SymbianUtils::SymbianDeviceManager::instance()->friendlyNameForPort(m_serialPortName);
    m_packageFileNamesWithTarget = deployConfiguration->packageFileNamesWithTargetInfo();
    m_signedPackages = deployConfiguration->signedPackages();
    m_installationDrive = deployConfiguration->installationDrive();
    m_silentInstall = deployConfiguration->silentInstall();

    switch (deployConfiguration->communicationChannel()) {
    case S60DeployConfiguration::CommunicationTrkSerialConnection:
        break;
    case S60DeployConfiguration::CommunicationCodaTcpConnection:
        m_address = deployConfiguration->deviceAddress();
        m_port = deployConfiguration->devicePort().toInt();
    default:
        break;
    }
    m_channel = deployConfiguration->communicationChannel();

    if (m_signedPackages.isEmpty()) {
        appendMessage(tr("No package has been found. Please specify at least one installation package."), true);
        return false;
    }

    if (m_channel == S60DeployConfiguration::CommunicationTrkSerialConnection) {
        QString message;
        if (m_launcher) {
            trk::Launcher::releaseToDeviceManager(m_launcher);
            delete m_launcher;
            m_launcher = 0;
        }

        m_launcher = trk::Launcher::acquireFromDeviceManager(m_serialPortName, this, &message);
        if (!message.isEmpty() || !m_launcher) {
            if (m_launcher)
                trk::Launcher::releaseToDeviceManager(m_launcher);
            delete m_launcher;
            m_launcher = 0;
            appendMessage(message, true);
            return true;
        }

        // Prompt the user to start up the Bluetooth connection
        const trk::PromptStartCommunicationResult src =
                S60RunConfigBluetoothStarter::startCommunication(m_launcher->trkDevice(),
                                                                 0, &message);
        if (src != trk::PromptStartCommunicationConnected) {
            if (!message.isEmpty())
                trk::Launcher::releaseToDeviceManager(m_launcher);
            delete m_launcher;
            m_launcher = 0;
            appendMessage(message, true);
            return false;
        }
    }
    return true;
}

QVariantMap S60DeployStep::toMap() const
{
    return BuildStep::toMap();
}

bool S60DeployStep::fromMap(const QVariantMap &map)
{
    return BuildStep::fromMap(map);
}

void S60DeployStep::appendMessage(const QString &error, bool isError)
{
    emit addOutput(error, isError?ProjectExplorer::BuildStep::ErrorMessageOutput:
                                  ProjectExplorer::BuildStep::MessageOutput);
}

void S60DeployStep::reportError(const QString &error)
{
    emit addOutput(error, ProjectExplorer::BuildStep::ErrorMessageOutput);
    emit addTask(ProjectExplorer::Task(ProjectExplorer::Task::Error,
                                       error,
                                       QString(), -1,
                                       ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
    emit finished(false);
}

bool S60DeployStep::processPackageName(QString &errorMessage)
{
    for (int i = 0; i < m_signedPackages.count(); ++i) {
        QFileInfo packageInfo(m_signedPackages.at(i));
        // support for 4.6.1 and pre, where make sis creates 'targetname_armX_udeb.sis' instead of 'targetname.sis'
        QFileInfo packageWithTargetInfo(m_packageFileNamesWithTarget.at(i));
        // does the 4.6.1 version exist?
        if (packageWithTargetInfo.exists() && packageWithTargetInfo.isFile()) {
            // is the 4.6.1 version newer? (to guard against behavior change Qt Creator 1.3 --> 2.0)
            if (!packageInfo.exists() || packageInfo.lastModified() < packageWithTargetInfo.lastModified()) { //TODO change the QtCore
                // the 'targetname_armX_udeb.sis' crap exists and is new, rename it
                appendMessage(tr("Renaming new package '%1' to '%2'")
                              .arg(QDir::toNativeSeparators(m_packageFileNamesWithTarget.at(i)),
                                   QDir::toNativeSeparators(m_signedPackages.at(i))), false);
                return renameFile(m_packageFileNamesWithTarget.at(i), m_signedPackages.at(i), &errorMessage);
            } else {
                // the 'targetname_armX_udeb.sis' crap exists but is old, remove it
                appendMessage(tr("Removing old package '%1'")
                              .arg(QDir::toNativeSeparators(m_packageFileNamesWithTarget.at(i))),
                              false);
                ensureDeleteFile(m_packageFileNamesWithTarget.at(i), &errorMessage);
            }
        }
        if (!packageInfo.exists() || !packageInfo.isFile()) {
            errorMessage = tr("'%1': Package file not found").arg(m_signedPackages.at(i));
            return false;
        }
    }
    return true;
}

void S60DeployStep::start()
{
    QString errorMessage;

    bool serialConnection = (m_channel == S60DeployConfiguration::CommunicationTrkSerialConnection
            || m_channel == S60DeployConfiguration::CommunicationCodaSerialConnection);
    bool trkClient = m_channel == S60DeployConfiguration::CommunicationTrkSerialConnection;

    if ((serialConnection && m_serialPortName.isEmpty())
            || (trkClient && !m_launcher)) {
        errorMessage = tr("No device is connected. Please connect a device and try again.");
        reportError(errorMessage);
        return;
    }
    if (!trkClient) {
        QTC_ASSERT(!m_codaDevice.data(), return);
        if (m_address.isEmpty() && !serialConnection) {
            errorMessage = tr("No address for a device has been defined. Please define an address and try again.");
            reportError(errorMessage);
            return;
        }
    }

    // make sure we have the right name of the sis package
    if (processPackageName(errorMessage)) {
        startDeployment();
    } else {
        errorMessage = tr("Failed to find package %1").arg(errorMessage);
        reportError(errorMessage);
        stop();
    }
}

void S60DeployStep::stop()
{
    if (m_channel == S60DeployConfiguration::CommunicationTrkSerialConnection) {
        if (m_launcher)
            m_launcher->terminate();
    } else {
        if (m_codaDevice) {
            switch (state()) {
            case StateSendingData:
                closeFiles();
                break;
            default:
                break; //should also stop the package installatrion, but CODA does not support it yet
            }
            disconnect(m_codaDevice.data(), 0, this, 0);
            SymbianUtils::SymbianDeviceManager::instance()->releaseCodaDevice(m_codaDevice);
        }
    }
    setState(StateUninit);
    emit finished(false);
}

void S60DeployStep::setupConnections()
{
    if (m_channel == S60DeployConfiguration::CommunicationTrkSerialConnection
            || m_channel == S60DeployConfiguration::CommunicationCodaSerialConnection)
        connect(SymbianUtils::SymbianDeviceManager::instance(), SIGNAL(deviceRemoved(SymbianUtils::SymbianDevice)),
                this, SLOT(deviceRemoved(SymbianUtils::SymbianDevice)));

    if (m_channel == S60DeployConfiguration::CommunicationTrkSerialConnection) {
        connect(m_launcher, SIGNAL(finished()), this, SLOT(launcherFinished()));
        connect(m_launcher, SIGNAL(canNotConnect(QString)), this, SLOT(connectFailed(QString)));
        connect(m_launcher, SIGNAL(copyingStarted(QString)), this, SLOT(printCopyingNotice(QString)));
        connect(m_launcher, SIGNAL(canNotCreateFile(QString,QString)), this, SLOT(createFileFailed(QString,QString)));
        connect(m_launcher, SIGNAL(canNotWriteFile(QString,QString)), this, SLOT(writeFileFailed(QString,QString)));
        connect(m_launcher, SIGNAL(canNotCloseFile(QString,QString)), this, SLOT(closeFileFailed(QString,QString)));
        connect(m_launcher, SIGNAL(installingStarted(QString)), this, SLOT(printInstallingNotice(QString)));
        connect(m_launcher, SIGNAL(canNotInstall(QString,QString)), this, SLOT(installFailed(QString,QString)));
        connect(m_launcher, SIGNAL(installingFinished()), this, SLOT(printInstallingFinished()));
        connect(m_launcher, SIGNAL(stateChanged(int)), this, SLOT(slotLauncherStateChanged(int)));
        connect(m_launcher, SIGNAL(copyProgress(int)), this, SLOT(setCopyProgress(int)));
    } else {
        connect(m_codaDevice.data(), SIGNAL(error(QString)), this, SLOT(slotError(QString)));
        connect(m_codaDevice.data(), SIGNAL(logMessage(QString)), this, SLOT(slotTrkLogMessage(QString)));
        connect(m_codaDevice.data(), SIGNAL(tcfEvent(Coda::CodaEvent)), this, SLOT(slotCodaEvent(Coda::CodaEvent)), Qt::DirectConnection);
        connect(m_codaDevice.data(), SIGNAL(serialPong(QString)), this, SLOT(slotSerialPong(QString)));
        connect(this, SIGNAL(manualInstallation()), this, SLOT(showManualInstallationInfo()));
    }
}

void S60DeployStep::startDeployment()
{
    if (m_channel == S60DeployConfiguration::CommunicationTrkSerialConnection) {
        QTC_ASSERT(m_launcher, return);
    }
    QTC_ASSERT(!m_codaDevice.data(), return);

    // We need to defer setupConnections() in the case of CommunicationCodaSerialConnection
    //setupConnections();

    if (m_channel == S60DeployConfiguration::CommunicationTrkSerialConnection) {
        setupConnections();
        QStringList copyDst;
        foreach (const QString &signedPackage, m_signedPackages)
            copyDst << QString::fromLatin1("%1:\\Data\\%2").arg(m_installationDrive).arg(QFileInfo(signedPackage).fileName());

        m_launcher->setCopyFileNames(m_signedPackages, copyDst);
        m_launcher->setInstallFileNames(copyDst);
        m_launcher->setInstallationDrive(m_installationDrive);
        m_launcher->setInstallationMode(m_silentInstall?trk::Launcher::InstallationModeSilentAndUser:
                                                        trk::Launcher::InstallationModeUser);
        m_launcher->addStartupActions(trk::Launcher::ActionCopyInstall);

        // TODO readd information about packages? msgListFile(m_signedPackage)
        appendMessage(tr("Deploying application to '%2'...").arg(m_serialPortFriendlyName), false);

        QString errorMessage;
        if (!m_launcher->startServer(&errorMessage)) {
            errorMessage = tr("Could not connect to phone on port '%1': %2\n"
                              "Check if the phone is connected and App TRK is running.").arg(m_serialPortName, errorMessage);
            reportError(errorMessage);
            stop();
        }
    } else if (m_channel == S60DeployConfiguration::CommunicationCodaSerialConnection) {
        appendMessage(tr("Deploying application to '%1'...").arg(m_serialPortFriendlyName), false);
        m_codaDevice = SymbianUtils::SymbianDeviceManager::instance()->getCodaDevice(m_serialPortName);       
        bool ok = m_codaDevice && m_codaDevice->device()->isOpen();
        if (!ok) {
            QString deviceError = tr("No such port");
            if (m_codaDevice)
                deviceError = m_codaDevice->device()->errorString();
            reportError(tr("Could not open serial device: %1").arg(deviceError));
            stop();
            return;
        }
        setupConnections();
        setState(StateConnecting);
        m_codaDevice->sendSerialPing(false);
    } else {
        m_codaDevice = QSharedPointer<Coda::CodaDevice>(new Coda::CodaDevice);
        setupConnections();
        const QSharedPointer<QTcpSocket> codaSocket(new QTcpSocket);
        m_codaDevice->setDevice(codaSocket);
        codaSocket->connectToHost(m_address, m_port);
        setState(StateConnecting);
        appendMessage(tr("Connecting to %1:%2...").arg(m_address).arg(m_port), false);
    }
    QTimer::singleShot(4000, this, SLOT(checkForTimeout()));
}

void S60DeployStep::run(QFutureInterface<bool> &fi)
{
    m_futureInterface = &fi;
    m_deployResult = true;
    m_deployCanceled = false;
    disconnect(this);

    m_futureInterface->setProgressRange(0, 100*m_signedPackages.count());

    if (m_channel == S60DeployConfiguration::CommunicationTrkSerialConnection) {
        connect(this, SIGNAL(finished(bool)), this, SLOT(launcherFinished(bool)));
        connect(this, SIGNAL(finishNow(bool)), this, SLOT(launcherFinished(bool)), Qt::DirectConnection);
    } else {
        connect(this, SIGNAL(finished(bool)), this, SLOT(deploymentFinished(bool)));
        connect(this, SIGNAL(finishNow(bool)), this, SLOT(deploymentFinished(bool)), Qt::DirectConnection);
        connect(this, SIGNAL(allFilesSent()), this, SLOT(startInstalling()), Qt::DirectConnection);
        connect(this, SIGNAL(allFilesInstalled()), this, SIGNAL(finished()), Qt::DirectConnection);
    }

    connect(this, SIGNAL(copyProgressChanged(int)), this, SLOT(updateProgress(int)));

    start();
    m_timer = new QTimer();
    connect(m_timer, SIGNAL(timeout()), this, SLOT(checkForCancel()), Qt::DirectConnection);
    m_timer->start(500);
    m_eventLoop = new QEventLoop();
    m_eventLoop->exec();
    m_timer->stop();
    delete m_timer;
    m_timer = 0;

    if (m_codaDevice) {
        disconnect(m_codaDevice.data(), 0, this, 0);
        SymbianUtils::SymbianDeviceManager::instance()->releaseCodaDevice(m_codaDevice);
    }

    delete m_eventLoop;
    m_eventLoop = 0;
    fi.reportResult(m_deployResult);
    m_futureInterface = 0;
}

void S60DeployStep::slotError(const QString &error)
{
    reportError(tr("Error: %1").arg(error));
}

void S60DeployStep::slotTrkLogMessage(const QString &log)
{
    if (debug > 1)
        qDebug() << "CODA log:" << log;
}

void S60DeployStep::slotSerialPong(const QString &message)
{
    if (debug)
        qDebug() << "CODA serial pong:" << message;
    handleConnected();
}

void S60DeployStep::slotCodaEvent(const Coda::CodaEvent &event)
{
    if (debug)
        qDebug() << "CODA event:" << "Type:" << event.type() << "Message:" << event.toString();

    switch (event.type()) {
    case Coda::CodaEvent::LocatorHello:
        handleConnected();
        break;
    default:
        if (debug)
            qDebug() << "Unhandled event:" << "Type:" << event.type() << "Message:" << event.toString();
        break;
    }
}

void S60DeployStep::handleConnected()
{
    if (state() >= StateConnected)
        return;
    setState(StateConnected);
    emit codaConnected();
    startTransferring();
}

void S60DeployStep::initFileSending()
{
    QTC_ASSERT(m_currentFileIndex < m_signedPackages.count(), return);
    QTC_ASSERT(m_currentFileIndex >= 0, return);
    QTC_ASSERT(m_codaDevice, return);

    const unsigned flags =
            Coda::CodaDevice::FileSystem_TCF_O_WRITE
            |Coda::CodaDevice::FileSystem_TCF_O_CREAT
            |Coda::CodaDevice::FileSystem_TCF_O_TRUNC;
    m_putWriteOk = false;

    QString packageName(QFileInfo(m_signedPackages.at(m_currentFileIndex)).fileName());
    QString remoteFileLocation = QString::fromLatin1("%1:\\Data\\%2").arg(m_installationDrive).arg(packageName);
    m_codaDevice->sendFileSystemOpenCommand(Coda::CodaCallback(this, &S60DeployStep::handleFileSystemOpen),
                                           remoteFileLocation.toAscii(), flags);
    appendMessage(tr("Copying \"%1\"...").arg(packageName), false);
    m_timeoutTimer->start();
}

void S60DeployStep::initFileInstallation()
{
    QTC_ASSERT(m_currentFileIndex < m_signedPackages.count(), return);
    QTC_ASSERT(m_currentFileIndex >= 0, return);

    if (!m_codaDevice)
        return;

    QString packageName(QFileInfo(m_signedPackages.at(m_currentFileIndex)).fileName());
    QString remoteFileLocation = QString::fromLatin1("%1:\\Data\\%2").arg(m_installationDrive).arg(packageName);
    if (m_silentInstall) {
        m_codaDevice->sendSymbianInstallSilentInstallCommand(Coda::CodaCallback(this, &S60DeployStep::handleSymbianInstall),
                                                            remoteFileLocation.toAscii(), QString::fromLatin1("%1:").arg(m_installationDrive).toAscii());
        appendMessage(tr("Installing package \"%1\" on drive %2:...").arg(packageName).arg(m_installationDrive), false);
    } else {
        m_codaDevice->sendSymbianInstallUIInstallCommand(Coda::CodaCallback(this, &S60DeployStep::handleSymbianInstall),
                                                        remoteFileLocation.toAscii());
        appendMessage(tr("Please continue the installation on your device."), false);
        emit manualInstallation();
    }
}

void S60DeployStep::startTransferring()
{
    m_currentFileIndex = 0;
    initFileSending();
    setState(StateSendingData);
}

void S60DeployStep::startInstalling()
{
    m_currentFileIndex = 0;
    initFileInstallation();
    setState(StateInstalling);
}

void S60DeployStep::handleFileSystemOpen(const Coda::CodaCommandResult &result)
{
    if (result.type != Coda::CodaCommandResult::SuccessReply) {
        reportError(tr("Could not open remote file: %1").arg(result.errorString()));
        return;
    }

    if (result.values.size() < 1 || result.values.at(0).data().isEmpty()) {
        reportError(tr("Internal error: No filehandle obtained"));
        return;
    }

    m_remoteFileHandle = result.values.at(0).data();

    const QString fileName = m_signedPackages.at(m_currentFileIndex);
    m_putFile.reset(new QFile(fileName));
    if (!m_putFile->open(QIODevice::ReadOnly)) { // Should not fail, was checked before
        reportError(tr("Could not open local file %1: %2").arg(fileName, m_putFile->errorString()));
        return;
    }
    putSendNextChunk();
}

void S60DeployStep::handleSymbianInstall(const Coda::CodaCommandResult &result)
{
    if (result.type == Coda::CodaCommandResult::SuccessReply) {
        appendMessage(tr("Installation has finished"), false);
        if (++m_currentFileIndex >= m_signedPackages.count()) {
            setState(StateFinished);
            emit allFilesInstalled();
        } else
            initFileInstallation();
    } else {
        reportError(tr("Installation failed: %1; "
                       "see %2 for descriptions of the error codes")
                    .arg(result.errorString(),
                         QLatin1String("http://wiki.forum.nokia.com/index.php/Symbian_OS_Error_Codes")));
    }
}

void S60DeployStep::putSendNextChunk()
{
    if (!m_codaDevice)
        return;
    QTC_ASSERT(m_putFile, return);

    // Read and send off next chunk
    const quint64 pos = m_putFile->pos();
    const QByteArray data = m_putFile->read(m_putChunkSize);
    const quint64 size = m_putFile->size();
    if (data.isEmpty()) {
        m_putWriteOk = true;
        closeFiles();
        setCopyProgress(100);
    } else {
        m_putLastChunkSize = data.size();
        if (debug > 1)
            qDebug("Writing %llu bytes to remote file '%s' at %llu\n",
                   m_putLastChunkSize,
                   m_remoteFileHandle.constData(), pos);
        m_codaDevice->sendFileSystemWriteCommand(Coda::CodaCallback(this, &S60DeployStep::handleFileSystemWrite),
                                                m_remoteFileHandle, data, unsigned(pos));
        setCopyProgress((100*(m_putLastChunkSize+pos))/size);
        m_timeoutTimer->start();
    }
}

void S60DeployStep::closeFiles()
{
    m_putFile.reset();
    QTC_ASSERT(m_codaDevice, return);

    emit addOutput(QLatin1String("\n"), ProjectExplorer::BuildStep::MessageOutput);
    m_codaDevice->sendFileSystemCloseCommand(Coda::CodaCallback(this, &S60DeployStep::handleFileSystemClose),
                                            m_remoteFileHandle);

}

void S60DeployStep::handleFileSystemWrite(const Coda::CodaCommandResult &result)
{
    m_timeoutTimer->stop();
    // Close remote file even if copy fails
    m_putWriteOk = result;
    if (!m_putWriteOk) {
        QString packageName(QFileInfo(m_signedPackages.at(m_currentFileIndex)).fileName());
        reportError(tr("Could not write to file %1 on device: %2").arg(packageName).arg(result.errorString()));
    }

    if (!m_putWriteOk || m_putLastChunkSize < m_putChunkSize) {
        closeFiles();
    } else {
        putSendNextChunk();
    }
}

void S60DeployStep::handleFileSystemClose(const Coda::CodaCommandResult &result)
{
    if (result.type == Coda::CodaCommandResult::SuccessReply) {
        if (debug)
            qDebug("File closed.\n");
        if (++m_currentFileIndex >= m_signedPackages.count())
            emit allFilesSent();
        else
            initFileSending();
    } else {
        reportError(tr("Failed to close the remote file: %1").arg(result.toString()));
    }
}

void S60DeployStep::checkForTimeout()
{
    if (state() != StateConnecting)
        return;
    QMessageBox *mb = CodaRunControl::createCodaWaitingMessageBox(Core::ICore::instance()->mainWindow());
    connect(this, SIGNAL(codaConnected()), mb, SLOT(close()));
    connect(this, SIGNAL(finished()), mb, SLOT(close()));
    connect(this, SIGNAL(finishNow()), mb, SLOT(close()));
    connect(mb, SIGNAL(finished(int)), this, SLOT(slotWaitingForTckTrkClosed(int)));
    mb->open();
}

void S60DeployStep::showManualInstallationInfo()
{
    const QString title  = tr("Installation");
    const QString text = tr("Please continue the installation on your device.");
    QMessageBox *mb = new QMessageBox(QMessageBox::Information, title, text,
                                      QMessageBox::Ok, Core::ICore::instance()->mainWindow());
    connect(this, SIGNAL(allFilesInstalled()), mb, SLOT(close()));
    connect(this, SIGNAL(finished()), mb, SLOT(close()));
    connect(this, SIGNAL(finishNow()), mb, SLOT(close()));
    mb->open();
}

void S60DeployStep::slotWaitingForTckTrkClosed(int result)
{
    if (result == QMessageBox::Cancel)
        m_deployCanceled = true;
}

void S60DeployStep::setReleaseDeviceAfterLauncherFinish(bool v)
{
    m_releaseDeviceAfterLauncherFinish = v;
}

void S60DeployStep::slotLauncherStateChanged(int s)
{
    if (s == trk::Launcher::WaitingForTrk) {
        QMessageBox *mb = TrkRunControl::createTrkWaitingMessageBox(m_launcher->trkServerName(),
                                                                          Core::ICore::instance()->mainWindow());
        connect(m_launcher, SIGNAL(stateChanged(int)), mb, SLOT(close()));
        connect(mb, SIGNAL(finished(int)), this, SLOT(slotWaitingForTrkClosed()));
        mb->open();
    }
}

void S60DeployStep::slotWaitingForTrkClosed()
{
    if (m_launcher && m_launcher->state() == trk::Launcher::WaitingForTrk)
        m_deployCanceled = true;
}

void S60DeployStep::createFileFailed(const QString &filename, const QString &errorMessage)
{
    reportError(tr("Could not create file %1 on device: %2").arg(filename, errorMessage));
}

void S60DeployStep::writeFileFailed(const QString &filename, const QString &errorMessage)
{
    reportError(tr("Could not write to file %1 on device: %2").arg(filename, errorMessage));
}

void S60DeployStep::closeFileFailed(const QString &filename, const QString &errorMessage)
{
    const QString msg = tr("Could not close file %1 on device: %2. It will be closed when App TRK is closed.");
    reportError( msg.arg(filename, errorMessage));
}

void S60DeployStep::connectFailed(const QString &errorMessage)
{
    reportError(tr("Could not connect to App TRK on device: %1. Restarting App TRK might help.").arg(errorMessage));
}

void S60DeployStep::printCopyingNotice(const QString &fileName)
{
    appendMessage(tr("Copying \"%1\"...").arg(fileName), false);
}

void S60DeployStep::printInstallingNotice(const QString &packageName)
{
    appendMessage(tr("Installing package \"%1\" on drive %2:...").arg(packageName).arg(m_installationDrive), false);
}

void S60DeployStep::printInstallingFinished()
{
    appendMessage(tr("Installation has finished"), false);
}

void S60DeployStep::installFailed(const QString &filename, const QString &errorMessage)
{
    reportError(tr("Could not install from package %1 on device: %2").arg(filename, errorMessage));
}

void S60DeployStep::checkForCancel()
{
    if ((m_futureInterface->isCanceled() || m_deployCanceled) && m_timer->isActive()) {
        m_timer->stop();
        stop();
        QString canceledText(tr("Deployment has been cancelled."));
        appendMessage(canceledText, true);
        emit addTask(ProjectExplorer::Task(ProjectExplorer::Task::Error,
                                           canceledText,
                                           QString(), -1,
                                           ProjectExplorer::Constants::TASK_CATEGORY_BUILDSYSTEM));
        emit finishNow(false);
    }
}

void S60DeployStep::launcherFinished(bool success)
{
    m_deployResult = success;
    if(m_deployResult && m_futureInterface)
        m_futureInterface->setProgressValue(m_futureInterface->progressMaximum());
    if (m_releaseDeviceAfterLauncherFinish && m_launcher) {
        m_handleDeviceRemoval = false;
        trk::Launcher::releaseToDeviceManager(m_launcher);
    }
    if (m_launcher)
        m_launcher->deleteLater();
    m_launcher = 0;
    if (m_eventLoop)
        m_eventLoop->exit();
}

void S60DeployStep::deploymentFinished(bool success)
{
    m_deployResult = success;
    if(m_deployResult && m_futureInterface)
        m_futureInterface->setProgressValue(m_futureInterface->progressMaximum());
    if (m_eventLoop)
        m_eventLoop->exit();
}

void S60DeployStep::deviceRemoved(const SymbianUtils::SymbianDevice &d)
{
    if (m_handleDeviceRemoval && d.portName() == m_serialPortName)
        reportError(tr("The device '%1' has been disconnected").arg(d.friendlyName()));
}

void S60DeployStep::setCopyProgress(int progress)
{
    if (progress < 0)
        progress = 0;
    else if (progress > 100)
        progress = 100;
    if (copyProgress() == progress)
        return;
    m_copyProgress = progress;
    emit addOutput(QLatin1String("."), ProjectExplorer::BuildStep::MessageOutput, DontAppendNewline);
    emit copyProgressChanged(m_copyProgress);
}

int S60DeployStep::copyProgress() const
{
    return m_copyProgress;
}

void S60DeployStep::updateProgress(int progress)
{
    //This would show the percentage on the Compile output
    //appendMessage(tr("Copy percentage: %1%").arg((m_currentFileIndex*100 + progress) /m_signedPackages.count()), false);
    int copyProgress = ((m_currentFileIndex*100 + progress) /m_signedPackages.count());
    int entireProgress = copyProgress * 0.8; //the copy progress is just 80% of the whole deployment progress
    m_futureInterface->setProgressValueAndText(entireProgress, tr("Copy progress: %1%").arg(copyProgress));
}

void S60DeployStep::timeout()
{
    reportError(tr("A timeout while deploying has occurred. CODA might not be responding. Try reconnecting the device."));
}

// #pragma mark -- S60DeployStepWidget

BuildStepConfigWidget *S60DeployStep::createConfigWidget()
{
    return new S60DeployStepWidget();
}

S60DeployStepWidget::S60DeployStepWidget() : ProjectExplorer::BuildStepConfigWidget()
{
}

void S60DeployStepWidget::init()
{
}

QString S60DeployStepWidget::summaryText() const
{
    return QString("<b>%1</b>").arg(displayName());
}

QString S60DeployStepWidget::displayName() const
{
    return tr("Deploy SIS Package");
}

// #pragma mark -- S60DeployStepFactory

S60DeployStepFactory::S60DeployStepFactory(QObject *parent) :
    ProjectExplorer::IBuildStepFactory(parent)
{
}

S60DeployStepFactory::~S60DeployStepFactory()
{
}

bool S60DeployStepFactory::canCreate(ProjectExplorer::BuildStepList *parent, const QString &id) const
{
    if (parent->id() != QLatin1String(ProjectExplorer::Constants::BUILDSTEPS_DEPLOY))
        return false;
    if (parent->target()->id() != QLatin1String(Constants::S60_DEVICE_TARGET_ID))
        return false;
    return (id == QLatin1String(S60_DEPLOY_STEP_ID));
}

ProjectExplorer::BuildStep *S60DeployStepFactory::create(ProjectExplorer::BuildStepList *parent, const QString &id)
{
    if (!canCreate(parent, id))
        return 0;
    return new S60DeployStep(parent);
}

bool S60DeployStepFactory::canClone(ProjectExplorer::BuildStepList *parent, ProjectExplorer::BuildStep *source) const
{
    if (!canCreate(parent, source->id()))
        return false;
    if (!qobject_cast<S60DeployStep *>(source))
        return false;
    return true;
}

ProjectExplorer::BuildStep *S60DeployStepFactory::clone(ProjectExplorer::BuildStepList *parent, ProjectExplorer::BuildStep *source)
{
    if (!canClone(parent, source))
        return 0;
    return new S60DeployStep(parent, static_cast<S60DeployStep *>(source));
}

bool S60DeployStepFactory::canRestore(ProjectExplorer::BuildStepList *parent, const QVariantMap &map) const
{
    QString id(ProjectExplorer::idFromMap(map));
    return canCreate(parent, id);
}

ProjectExplorer::BuildStep *S60DeployStepFactory::restore(ProjectExplorer::BuildStepList *parent, const QVariantMap &map)
{
    if (!canRestore(parent, map))
        return 0;
    S60DeployStep *bs = new S60DeployStep(parent);
    if (bs->fromMap(map))
        return bs;
    delete bs;
    return 0;
}

QStringList S60DeployStepFactory::availableCreationIds(ProjectExplorer::BuildStepList *parent) const
{
    if (parent->id() == QLatin1String(ProjectExplorer::Constants::BUILDSTEPS_DEPLOY)
            && parent->target()->id() == QLatin1String(Constants::S60_DEVICE_TARGET_ID))
        return QStringList() << QLatin1String(S60_DEPLOY_STEP_ID);
    return QStringList();
}

QString S60DeployStepFactory::displayNameForId(const QString &id) const
{
    if (id == QLatin1String(S60_DEPLOY_STEP_ID))
        return tr("Deploy SIS Package");
    return QString();
}