summaryrefslogtreecommitdiff
path: root/src/plugins/madde/qt4maemodeployconfiguration.cpp
blob: 49ecdc1c3c0b4373de77e0a292cb158a9b935f1f (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
/**************************************************************************
**
** 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 "qt4maemodeployconfiguration.h"

#include "debianmanager.h"
#include "maddeuploadandinstallpackagesteps.h"
#include "maemoconstants.h"
#include "maemodeploybymountsteps.h"
#include "maemoglobal.h"
#include "maemoinstalltosysrootstep.h"
#include "maemopackagecreationstep.h"

#include <coreplugin/icore.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/deployablefile.h>
#include <projectexplorer/target.h>
#include <projectexplorer/projectexplorer.h>
#include <qt4projectmanager/qt4buildconfiguration.h>
#include <qt4projectmanager/qt4project.h>
#include <qtsupport/qtprofileinformation.h>
#include <qtsupport/qtsupportconstants.h>
#include <remotelinux/remotelinuxcheckforfreediskspacestep.h>
#include <remotelinux/remotelinuxdeployconfigurationwidget.h>
#include <utils/qtcassert.h>

#include <QFileInfo>
#include <QString>

#include <QMainWindow>
#include <QMessageBox>

using namespace ProjectExplorer;
using namespace Qt4ProjectManager;
using namespace RemoteLinux;

namespace Madde {
namespace Internal {

Qt4MaemoDeployConfiguration::Qt4MaemoDeployConfiguration(ProjectExplorer::Target *target,
        const Core::Id id, const QString &displayName)
    : RemoteLinuxDeployConfiguration(target, id, displayName)
{
    init();
}

Qt4MaemoDeployConfiguration::Qt4MaemoDeployConfiguration(ProjectExplorer::Target *target,
        Qt4MaemoDeployConfiguration *source)
    : RemoteLinuxDeployConfiguration(target, source)
{
    init();
}

DeployConfigurationWidget *Qt4MaemoDeployConfiguration::configurationWidget() const
{
    return new RemoteLinuxDeployConfigurationWidget;
}

Qt4MaemoDeployConfiguration::~Qt4MaemoDeployConfiguration() {}

Core::Id Qt4MaemoDeployConfiguration::fremantleWithPackagingId()
{
    return Core::Id("DeployToFremantleWithPackaging");
}

Core::Id Qt4MaemoDeployConfiguration::fremantleWithoutPackagingId()
{
    return Core::Id("DeployToFremantleWithoutPackaging");
}

Core::Id Qt4MaemoDeployConfiguration::harmattanId()
{
    return Core::Id("DeployToHarmattan");
}

void Qt4MaemoDeployConfiguration::debianDirChanged(const Utils::FileName &dir)
{
    if (dir == DebianManager::debianDirectory(target()))
        emit packagingChanged();
}

void Qt4MaemoDeployConfiguration::setupPackaging()
{
    if (target()->project()->activeTarget() != target())
        return;

    disconnect(target()->project(), SIGNAL(fileListChanged()), this, SLOT(setupPackaging()));
    setupDebianPackaging();
}

void Qt4MaemoDeployConfiguration::setupDebianPackaging()
{
    Qt4BuildConfiguration *bc = qobject_cast<Qt4BuildConfiguration *>(target()->activeBuildConfiguration());
    if (!bc || !target()->profile())
        return;

    Utils::FileName debianDir = DebianManager::debianDirectory(target());
    Core::Id deviceType = ProjectExplorer::DeviceTypeProfileInformation::deviceTypeId(target()->profile());
    DebianManager *dm = DebianManager::instance();
    QString projectName = target()->project()->displayName();

    DebianManager::ActionStatus status = DebianManager::createTemplate(bc, debianDir);

    if (status == DebianManager::NoActionRequired ||
            status == DebianManager::ActionFailed)
        return;

    if (!DebianManager::hasPackageManagerIcon(debianDir)) {
        // Such a file is created by the mobile wizards.
        Utils::FileName iconPath = Utils::FileName::fromString(target()->project()->projectDirectory());
        iconPath.appendPath(projectName + QLatin1String("64.png"));
        if (iconPath.toFileInfo().exists())
            dm->setPackageManagerIcon(debianDir, deviceType, iconPath);
    }



    dm->monitor(debianDir);
    connect(dm, SIGNAL(debianDirectoryChanged(Utils::FileName)), this, SLOT(debianDirChanged(Utils::FileName)));

    // Set up aegis manifest on harmattan:
    if (deviceType == HarmattanOsType) {
        Utils::FileName manifest = debianDir;
        const QString manifestName = QLatin1String("manifest.aegis");
        manifest.appendPath(manifestName);
        const QFile aegisFile(manifest.toString());
        if (!aegisFile.exists()) {
            Utils::FileReader reader;
            if (!reader.fetch(Core::ICore::resourcePath()
                              + QLatin1String("/templates/shared/") + manifestName)) {
                qDebug("Reading manifest template failed.");
                return;
            }
            QString content = QString::fromUtf8(reader.data());
            content.replace(QLatin1String("%%PROJECTNAME%%"), projectName);
            Utils::FileSaver writer(aegisFile.fileName(), QIODevice::WriteOnly);
            writer.write(content.toUtf8());
            if (!writer.finalize()) {
                qDebug("Failure writing manifest file.");
                return;
            }
        }
    }

    emit packagingChanged();

    // fix path:
    QStringList files = DebianManager::debianFiles(debianDir);
    Utils::FileName path = Utils::FileName::fromString(QDir(target()->project()->projectDirectory())
                                                       .relativeFilePath(debianDir.toString()));
    QStringList relativeFiles;
    foreach (const QString &f, files) {
        Utils::FileName fn = path;
        fn.appendPath(f);
        relativeFiles << fn.toString();
    }

    addFilesToProject(relativeFiles);
}

void Qt4MaemoDeployConfiguration::addFilesToProject(const QStringList &files)
{
    if (files.isEmpty())
        return;

    const QString list = QLatin1String("<ul><li>") + files.join(QLatin1String("</li><li>"))
            + QLatin1String("</li></ul>");
    QMessageBox::StandardButton button =
            QMessageBox::question(Core::ICore::mainWindow(),
                                  tr("Add Packaging Files to Project"),
                                  tr("<html>Qt Creator has set up the following files to enable "
                                     "packaging:\n   %1\nDo you want to add them to the project?</html>")
                                  .arg(list), QMessageBox::Yes | QMessageBox::No);
    if (button == QMessageBox::Yes)
        ProjectExplorer::ProjectExplorerPlugin::instance()
                ->addExistingFiles(target()->project()->rootProjectNode(), files);
}

void Qt4MaemoDeployConfiguration::init()
{
    connect(target()->project(), SIGNAL(fileListChanged()), this, SLOT(setupPackaging()));
}

Qt4MaemoDeployConfigurationFactory::Qt4MaemoDeployConfigurationFactory(QObject *parent)
    : DeployConfigurationFactory(parent)
{ setObjectName(QLatin1String("Qt4MaemoDeployConfigurationFactory")); }

QList<Core::Id> Qt4MaemoDeployConfigurationFactory::availableCreationIds(Target *parent) const
{
    QList<Core::Id> ids;
    if (!canHandle(parent))
        return ids;

    Core::Id deviceType = ProjectExplorer::DeviceTypeProfileInformation::deviceTypeId(parent->profile());
    if (deviceType == Maemo5OsType)
        ids << Qt4MaemoDeployConfiguration::fremantleWithPackagingId()
            << Qt4MaemoDeployConfiguration::fremantleWithoutPackagingId();
    else if (deviceType == HarmattanOsType)
        ids << Qt4MaemoDeployConfiguration::harmattanId();
    return ids;
}

QString Qt4MaemoDeployConfigurationFactory::displayNameForId(const Core::Id id) const
{
    if (id == Qt4MaemoDeployConfiguration::fremantleWithoutPackagingId())
        return tr("Copy Files to Maemo5 Device");
    else if (id == Qt4MaemoDeployConfiguration::fremantleWithPackagingId())
        return tr("Build Debian Package and Install to Maemo5 Device");
    else if (id == Qt4MaemoDeployConfiguration::harmattanId())
        return tr("Build Debian Package and Install to Harmattan Device");
    return QString();
}

bool Qt4MaemoDeployConfigurationFactory::canCreate(Target *parent,
    const Core::Id id) const
{
    return availableCreationIds(parent).contains(id);
}

DeployConfiguration *Qt4MaemoDeployConfigurationFactory::create(Target *parent,
    const Core::Id id)
{
    Q_ASSERT(canCreate(parent, id));

    const QString displayName = displayNameForId(id);
    DeployConfiguration * const dc = new Qt4MaemoDeployConfiguration(parent, id, displayName);
    if (id == Qt4MaemoDeployConfiguration::fremantleWithoutPackagingId()) {
        dc->stepList()->insertStep(0, new MaemoMakeInstallToSysrootStep(dc->stepList()));
        dc->stepList()->insertStep(1, new RemoteLinuxCheckForFreeDiskSpaceStep(dc->stepList()));
        dc->stepList()->insertStep(2, new MaemoCopyFilesViaMountStep(dc->stepList()));
    } else if (id == Qt4MaemoDeployConfiguration::fremantleWithPackagingId()) {
        dc->stepList()->insertStep(0, new MaemoDebianPackageCreationStep(dc->stepList()));
        dc->stepList()->insertStep(1, new MaemoInstallDebianPackageToSysrootStep(dc->stepList()));
        dc->stepList()->insertStep(2, new RemoteLinuxCheckForFreeDiskSpaceStep(dc->stepList()));
        dc->stepList()->insertStep(3, new MaemoInstallPackageViaMountStep(dc->stepList()));
    } else if (id == Qt4MaemoDeployConfiguration::harmattanId()) {
        dc->stepList()->insertStep(0, new MaemoDebianPackageCreationStep(dc->stepList()));
        dc->stepList()->insertStep(1, new MaemoInstallDebianPackageToSysrootStep(dc->stepList()));
        dc->stepList()->insertStep(2, new RemoteLinuxCheckForFreeDiskSpaceStep(dc->stepList()));
        dc->stepList()->insertStep(3, new MaemoUploadAndInstallPackageStep(dc->stepList()));
    }
    return dc;
}

bool Qt4MaemoDeployConfigurationFactory::canRestore(Target *parent, const QVariantMap &map) const
{
    Core::Id id = idFromMap(map);
    return canHandle(parent) && availableCreationIds(parent).contains(id)
            && MaemoGlobal::supportsMaemoDevice(parent->profile());
}

DeployConfiguration *Qt4MaemoDeployConfigurationFactory::restore(Target *parent, const QVariantMap &map)
{
    QTC_ASSERT(canRestore(parent, map), return 0);

    Qt4MaemoDeployConfiguration * const dc
        = qobject_cast<Qt4MaemoDeployConfiguration *>(create(parent, idFromMap(map)));
    if (!dc->fromMap(map)) {
        delete dc;
        return 0;
    }
    return dc;
}

DeployConfiguration *Qt4MaemoDeployConfigurationFactory::clone(Target *parent,
    DeployConfiguration *product)
{
    if (!canClone(parent, product))
        return 0;
    return new Qt4MaemoDeployConfiguration(parent,
                                           qobject_cast<Qt4MaemoDeployConfiguration *>(product));
}

bool Qt4MaemoDeployConfigurationFactory::canHandle(Target *parent) const
{
    if (!qobject_cast<Qt4ProjectManager::Qt4Project *>(parent->project()))
        return false;
    if (!parent->project()->supportsProfile(parent->profile()))
        return false;
    return MaemoGlobal::supportsMaemoDevice(parent->profile());
}

} // namespace Internal
} // namespace Madde