summaryrefslogtreecommitdiff
path: root/src/plugins/madde/maemodeploybymountsteps.cpp
blob: 0503b9d72efa809e4f7b9fb8e31041ec4e13e591 (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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: http://www.qt-project.org/
**
** 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.
**
**
**************************************************************************/
#include "maemodeploybymountsteps.h"

#include "maemodeploymentmounter.h"
#include "maemoglobal.h"
#include "maemomountspecification.h"
#include "maemopackagecreationstep.h"
#include "maemopackageinstaller.h"
#include "maemoqemumanager.h"
#include "maemoremotecopyfacility.h"
#include "qt4maemodeployconfiguration.h"

#include <projectexplorer/project.h>
#include <projectexplorer/target.h>
#include <qt4projectmanager/qt4buildconfiguration.h>
#include <qtsupport/baseqtversion.h>
#include <qtsupport/qtprofileinformation.h>
#include <remotelinux/abstractremotelinuxdeployservice.h>
#include <remotelinux/deployablefile.h>
#include <remotelinux/deploymentinfo.h>
#include <remotelinux/linuxdevice.h>
#include <utils/qtcassert.h>
#include <ssh/sshconnection.h>

#include <QFileInfo>
#include <QList>

using namespace ProjectExplorer;
using namespace RemoteLinux;

namespace Madde {
namespace Internal {
class AbstractMaemoDeployByMountService : public AbstractRemoteLinuxDeployService
{
    Q_OBJECT
    Q_DISABLE_COPY(AbstractMaemoDeployByMountService)
protected:
    AbstractMaemoDeployByMountService(QObject *parent);

    QString deployMountPoint() const;

private slots:
    void handleMounted();
    void handleUnmounted();
    void handleMountError(const QString &errorMsg);
    void handleInstallationFinished(const QString &errorMsg);

private:
    virtual QList<MaemoMountSpecification> mountSpecifications() const=0;
    virtual void doInstall() = 0;
    virtual void cancelInstallation() = 0;
    virtual void handleInstallationSuccess() = 0;

    void doDeviceSetup();
    void stopDeviceSetup();
    void doDeploy();
    void stopDeployment();

    void unmount();
    void setFinished();

    MaemoDeploymentMounter * const m_mounter;
    enum State { Inactive, Mounting, Installing, Unmounting } m_state;
    bool m_stopRequested;
};

class MaemoMountAndInstallPackageService : public AbstractMaemoDeployByMountService
{
    Q_OBJECT

public:
    MaemoMountAndInstallPackageService(QObject *parent);

    void setPackageFilePath(const QString &filePath) { m_packageFilePath = filePath; }

private:
    bool isDeploymentNecessary() const;

    QList<MaemoMountSpecification> mountSpecifications() const;
    void doInstall();
    void cancelInstallation();
    void handleInstallationSuccess();

    MaemoDebianPackageInstaller * const m_installer;
    QString m_packageFilePath;
};

class MaemoMountAndCopyFilesService : public AbstractMaemoDeployByMountService
{
    Q_OBJECT

public:
    MaemoMountAndCopyFilesService(QObject *parent);

    void setDeployableFiles(const QList<DeployableFile> &deployableFiles) {
        m_deployableFiles = deployableFiles;
    }

private:
    bool isDeploymentNecessary() const;

    QList<MaemoMountSpecification> mountSpecifications() const;
    void doInstall();
    void cancelInstallation();
    void handleInstallationSuccess();

    Q_SLOT void handleFileCopied(const RemoteLinux::DeployableFile&deployable);

    MaemoRemoteCopyFacility * const m_copyFacility;
    mutable QList<DeployableFile> m_filesToCopy;
    QList<DeployableFile> m_deployableFiles;
};


AbstractMaemoDeployByMountService::AbstractMaemoDeployByMountService(QObject *parent)
    : AbstractRemoteLinuxDeployService(parent),
      m_mounter(new MaemoDeploymentMounter(this)),
      m_state(Inactive),
      m_stopRequested(false)
{
    connect(m_mounter, SIGNAL(setupDone()), SLOT(handleMounted()));
    connect(m_mounter, SIGNAL(tearDownDone()), SLOT(handleUnmounted()));
    connect(m_mounter, SIGNAL(error(QString)), SLOT(handleMountError(QString)));
    connect(m_mounter, SIGNAL(reportProgress(QString)), SIGNAL(progressMessage(QString)));
    connect(m_mounter, SIGNAL(debugOutput(QString)), SIGNAL(stdErrData(QString)));
}

void AbstractMaemoDeployByMountService::doDeviceSetup()
{
    QTC_ASSERT(m_state == Inactive, return);

    if (deviceConfiguration()->machineType() == IDevice::Hardware) {
        handleDeviceSetupDone(true);
        return;
    }

    if (MaemoQemuManager::instance().qemuIsRunning()) {
        handleDeviceSetupDone(true);
        return;
    }

    MaemoQemuRuntime rt;
    const int qtId = QtSupport::QtProfileInformation::qtVersionId(profile());
    if (MaemoQemuManager::instance().runtimeForQtVersion(qtId, &rt)) {
        MaemoQemuManager::instance().startRuntime();
        emit errorMessage(tr("Cannot deploy: Qemu was not running. "
            "It has now been started up for you, but it will take "
            "a bit of time until it is ready. Please try again then."));
    } else {
        emit errorMessage(tr("Cannot deploy: You want to deploy to Qemu, but it is not enabled "
            "for this Qt version."));
    }
    handleDeviceSetupDone(false);
}

void AbstractMaemoDeployByMountService::stopDeviceSetup()
{
    QTC_ASSERT(m_state == Inactive, return);

    handleDeviceSetupDone(false);
}

void AbstractMaemoDeployByMountService::doDeploy()
{
    QTC_ASSERT(m_state == Inactive, return);

    if (!buildConfiguration()) {
        emit errorMessage(tr("Missing build configuration."));
        setFinished();
        return;
    }

    m_state = Mounting;
    m_mounter->setupMounts(connection(), mountSpecifications(), profile());
}

void AbstractMaemoDeployByMountService::stopDeployment()
{
    switch (m_state) {
    case Installing:
        m_stopRequested = true;
        cancelInstallation();

        // TODO: Possibly unsafe, because the mount point may still be in use if the
        // application did not exit immediately.
        unmount();
        break;
    case Mounting:
    case Unmounting:
        m_stopRequested = true;
        break;
    case Inactive:
        qWarning("%s: Unexpected state 'Inactive'.", Q_FUNC_INFO);
        break;
    }
}

void AbstractMaemoDeployByMountService::unmount()
{
    m_state = Unmounting;
    m_mounter->tearDownMounts();
}

void AbstractMaemoDeployByMountService::handleMounted()
{
    QTC_ASSERT(m_state == Mounting, return);

    if (m_stopRequested) {
        unmount();
        return;
    }

    m_state = Installing;
    doInstall();
}

void AbstractMaemoDeployByMountService::handleUnmounted()
{
    QTC_ASSERT(m_state == Unmounting, return);

    setFinished();
}

void AbstractMaemoDeployByMountService::handleMountError(const QString &errorMsg)
{
    QTC_ASSERT(m_state == Mounting, return);

    emit errorMessage(errorMsg);
    setFinished();
}

void AbstractMaemoDeployByMountService::handleInstallationFinished(const QString &errorMsg)
{
    QTC_ASSERT(m_state == Installing, return);

    if (errorMsg.isEmpty())
        handleInstallationSuccess();
    else
        emit errorMessage(errorMsg);
    unmount();
}

void AbstractMaemoDeployByMountService::setFinished()
{
    m_state = Inactive;
    m_stopRequested = false;
    handleDeploymentDone();
}

QString AbstractMaemoDeployByMountService::deployMountPoint() const
{
    return MaemoGlobal::homeDirOnDevice(deviceConfiguration()->sshParameters().userName)
        + QLatin1String("/deployMountPoint_")
        + buildConfiguration()->target()->project()->displayName();
}


MaemoMountAndInstallPackageService::MaemoMountAndInstallPackageService(QObject *parent)
    : AbstractMaemoDeployByMountService(parent), m_installer(new MaemoDebianPackageInstaller(this))
{
    connect(m_installer, SIGNAL(stdoutData(QString)), SIGNAL(stdOutData(QString)));
    connect(m_installer, SIGNAL(stderrData(QString)), SIGNAL(stdErrData(QString)));
    connect(m_installer, SIGNAL(finished(QString)), SLOT(handleInstallationFinished(QString)));
}

bool MaemoMountAndInstallPackageService::isDeploymentNecessary() const
{
    return hasChangedSinceLastDeployment(DeployableFile(m_packageFilePath, QString()));
}

QList<MaemoMountSpecification> MaemoMountAndInstallPackageService::mountSpecifications() const
{
    const QString localDir = QFileInfo(m_packageFilePath).absolutePath();
    return QList<MaemoMountSpecification>()
        << MaemoMountSpecification(localDir, deployMountPoint());
}

void MaemoMountAndInstallPackageService::doInstall()
{
    const QString remoteFilePath = deployMountPoint() + QLatin1Char('/')
        + QFileInfo(m_packageFilePath).fileName();
    m_installer->installPackage(deviceConfiguration(), remoteFilePath, false);
}

void MaemoMountAndInstallPackageService::cancelInstallation()
{
    m_installer->cancelInstallation();
}

void MaemoMountAndInstallPackageService::handleInstallationSuccess()
{
    saveDeploymentTimeStamp(DeployableFile(m_packageFilePath, QString()));
    emit progressMessage(tr("Package installed."));
}


MaemoMountAndCopyFilesService::MaemoMountAndCopyFilesService(QObject *parent)
    : AbstractMaemoDeployByMountService(parent), m_copyFacility(new MaemoRemoteCopyFacility(this))
{
    connect(m_copyFacility, SIGNAL(stdoutData(QString)), SIGNAL(stdOutData(QString)));
    connect(m_copyFacility, SIGNAL(stderrData(QString)), SIGNAL(stdErrData(QString)));
    connect(m_copyFacility, SIGNAL(progress(QString)), SIGNAL(progressMessage(QString)));
    connect(m_copyFacility, SIGNAL(fileCopied(RemoteLinux::DeployableFile)),
        SLOT(handleFileCopied(RemoteLinux::DeployableFile)));
    connect(m_copyFacility, SIGNAL(finished(QString)), SLOT(handleInstallationFinished(QString)));
}

bool MaemoMountAndCopyFilesService::isDeploymentNecessary() const
{
    m_filesToCopy.clear();
    for (int i = 0; i < m_deployableFiles.count(); ++i) {
        const DeployableFile &d = m_deployableFiles.at(i);
        if (hasChangedSinceLastDeployment(d) || QFileInfo(d.localFilePath).isDir())
            m_filesToCopy << d;
    }
    return !m_filesToCopy.isEmpty();
}

QList<MaemoMountSpecification> MaemoMountAndCopyFilesService::mountSpecifications() const
{
    QList<MaemoMountSpecification> mountSpecs;
#ifdef Q_OS_WIN
    bool drivesToMount[26];
    qFill(drivesToMount, drivesToMount + sizeof drivesToMount / sizeof drivesToMount[0], false);
    for (int i = 0; i < m_filesToCopy.count(); ++i) {
        const QString localDir = QFileInfo(m_filesToCopy.at(i).localFilePath).canonicalPath();
        const char driveLetter = localDir.at(0).toLower().toLatin1();
        if (driveLetter < 'a' || driveLetter > 'z') {
            qWarning("Weird: drive letter is '%c'.", driveLetter);
            continue;
        }

        const int index = driveLetter - 'a';
        if (drivesToMount[index])
            continue;

        const QString mountPoint = deployMountPoint() + QLatin1Char('/') + QLatin1Char(driveLetter);
        const MaemoMountSpecification mountSpec(localDir.left(3), mountPoint);
        mountSpecs << mountSpec;
        drivesToMount[index] = true;
    }
#else
    mountSpecs << MaemoMountSpecification(QLatin1String("/"), deployMountPoint());
#endif
    return mountSpecs;
}

void MaemoMountAndCopyFilesService::doInstall()
{
    m_copyFacility->copyFiles(connection(), deviceConfiguration(), m_filesToCopy,
        deployMountPoint());
}

void MaemoMountAndCopyFilesService::cancelInstallation()
{
    m_copyFacility->cancel();
}

void MaemoMountAndCopyFilesService::handleInstallationSuccess()
{
    emit progressMessage(tr("All files copied."));
}

void MaemoMountAndCopyFilesService::handleFileCopied(const DeployableFile &deployable)
{
    saveDeploymentTimeStamp(deployable);
}

MaemoInstallPackageViaMountStep::MaemoInstallPackageViaMountStep(BuildStepList *bsl)
    : AbstractRemoteLinuxDeployStep(bsl, stepId())
{
    ctor();
}

MaemoInstallPackageViaMountStep::MaemoInstallPackageViaMountStep(BuildStepList *bsl,
        MaemoInstallPackageViaMountStep *other)
    : AbstractRemoteLinuxDeployStep(bsl, other)
{
    ctor();
}

void MaemoInstallPackageViaMountStep::ctor()
{
    m_deployService = new MaemoMountAndInstallPackageService(this);
    setDefaultDisplayName(displayName());
}

AbstractRemoteLinuxDeployService *MaemoInstallPackageViaMountStep::deployService() const
{
    return m_deployService;
}

bool MaemoInstallPackageViaMountStep::initInternal(QString *error)
{
    const AbstractMaemoPackageCreationStep * const pStep
        = deployConfiguration()->earlierBuildStep<MaemoDebianPackageCreationStep>(this);
    if (!pStep) {
        if (error)
            *error = tr("No Debian package creation step found.");
        return false;
    }
    m_deployService->setPackageFilePath(pStep->packageFilePath());
    return deployService()->isDeploymentPossible(error);
}

Core::Id MaemoInstallPackageViaMountStep::stepId()
{
    return Core::Id("MaemoMountAndInstallDeployStep");
}

QString MaemoInstallPackageViaMountStep::displayName()
{
    return tr("Deploy package via UTFS mount");
}


MaemoCopyFilesViaMountStep::MaemoCopyFilesViaMountStep(BuildStepList *bsl)
    : AbstractRemoteLinuxDeployStep(bsl, stepId())
{
    ctor();
}

MaemoCopyFilesViaMountStep::MaemoCopyFilesViaMountStep(BuildStepList *bsl,
        MaemoCopyFilesViaMountStep *other)
    : AbstractRemoteLinuxDeployStep(bsl, other)
{
    ctor();
}

void MaemoCopyFilesViaMountStep::ctor()
{
    m_deployService = new MaemoMountAndCopyFilesService(this);
    setDefaultDisplayName(displayName());
}

AbstractRemoteLinuxDeployService *MaemoCopyFilesViaMountStep::deployService() const
{
    return m_deployService;
}

bool MaemoCopyFilesViaMountStep::initInternal(QString *error)
{
    QList<DeployableFile> deployableFiles;
    const DeploymentInfo * const deploymentInfo = deployConfiguration()->deploymentInfo();
    const int deployableCount = deploymentInfo->deployableCount();
    for (int i = 0; i < deployableCount; ++i)
        deployableFiles << deploymentInfo->deployableAt(i);
    m_deployService->setDeployableFiles(deployableFiles);
    return deployService()->isDeploymentPossible(error);
}

Core::Id MaemoCopyFilesViaMountStep::stepId()
{
    return Core::Id("MaemoMountAndCopyDeployStep");
}

QString MaemoCopyFilesViaMountStep::displayName()
{
    return tr("Deploy files via UTFS mount");
}

} // namespace Internal
} // namespace Madde

#include "maemodeploybymountsteps.moc"