html5app.cpp 9.38 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "html5app.h"

#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QRegExp>
#include <QtCore/QTextStream>
#include <QtCore/QCoreApplication>

#ifndef CREATORLESSTEST
#include <coreplugin/icore.h>
#endif // CREATORLESSTEST

namespace Qt4ProjectManager {
namespace Internal {

const QString appViewerBaseName(QLatin1String("html5applicationviewer"));
const QString appViewerPriFileName(appViewerBaseName + QLatin1String(".pri"));
const QString appViewerCppFileName(appViewerBaseName + QLatin1String(".cpp"));
const QString appViewerHFileName(appViewerBaseName + QLatin1String(".h"));
const QString appViewerOriginsSubDir(appViewerBaseName + QLatin1Char('/'));

Html5App::Html5App()
    : AbstractMobileApp()
57
    , m_mainHtmlMode(ModeGenerate)
58
    , m_touchOptimizedNavigationEnabled(false)
59
60
61
62
63
64
65
{
}

Html5App::~Html5App()
{
}

66
void Html5App::setMainHtml(Mode mode, const QString &data)
67
{
68
69
70
    Q_ASSERT(mode != ModeGenerate || data.isEmpty());
    m_mainHtmlMode = mode;
    m_mainHtmlData = data;
71
72
}

73
Html5App::Mode Html5App::mainHtmlMode() const
74
{
75
    return m_mainHtmlMode;
76
77
}

78
79
80
81
82
83
84
85
86
87
void Html5App::setTouchOptimizedNavigationEnabled(bool enabled)
{
    m_touchOptimizedNavigationEnabled = enabled;
}

bool Html5App::touchOptimizedNavigationEnabled() const
{
    return m_touchOptimizedNavigationEnabled;
}

88
89
90
91
92
93
QString Html5App::pathExtended(int fileType) const
{
    const QString appViewerTargetSubDir = appViewerOriginsSubDir;
    const QString indexHtml = QLatin1String("index.html");
    const QString pathBase = outputPathBase();
    const QDir appProFilePath(pathBase);
94
95
96
97
98
    const bool generateHtml = m_mainHtmlMode == ModeGenerate;
    const bool importHtml = m_mainHtmlMode == ModeImport;
    const QFileInfo importedHtmlFile(m_mainHtmlData);
    const QString htmlSubDir = importHtml ? importedHtmlFile.canonicalPath().split(QLatin1Char('/')).last() + QLatin1Char('/')
                                          : QString::fromLatin1("html/");
99
100

    switch (fileType) {
101
102
103
104
105
        case MainHtml:                      return generateHtml ? pathBase + htmlSubDir + indexHtml
                                                                : importedHtmlFile.canonicalFilePath();
        case MainHtmlDeployed:              return generateHtml ? QString(htmlSubDir + indexHtml)
                                                                : htmlSubDir + importedHtmlFile.fileName();
        case MainHtmlOrigin:                return originsRoot() + QLatin1String("html/") + indexHtml;
106
107
108
109
110
111
112
        case AppViewerPri:                  return pathBase + appViewerTargetSubDir + appViewerPriFileName;
        case AppViewerPriOrigin:            return originsRoot() + appViewerOriginsSubDir + appViewerPriFileName;
        case AppViewerCpp:                  return pathBase + appViewerTargetSubDir + appViewerCppFileName;
        case AppViewerCppOrigin:            return originsRoot() + appViewerOriginsSubDir + appViewerCppFileName;
        case AppViewerH:                    return pathBase + appViewerTargetSubDir + appViewerHFileName;
        case AppViewerHOrigin:              return originsRoot() + appViewerOriginsSubDir + appViewerHFileName;
        case HtmlDir:                       return pathBase + htmlSubDir;
113
114
        case HtmlDirProFileRelative:        return generateHtml ? QString(htmlSubDir).remove(htmlSubDir.length() - 1, 1)
                                                                : appProFilePath.relativeFilePath(importedHtmlFile.canonicalPath());
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
        default:                            qFatal("Html5App::pathExtended() needs more work");
    }
    return QString();
}

QString Html5App::originsRoot() const
{
    return templatesRoot() + QLatin1String("html5app/");
}

QString Html5App::mainWindowClassName() const
{
    return QLatin1String("Html5ApplicationViewer");
}

bool Html5App::adaptCurrentMainCppTemplateLine(QString &line) const
{
    const QLatin1Char quote('"');
    bool adaptLine = true;
134
135
136
137
138
139
140
141
142
143
    if (line.contains(QLatin1String("// MAINHTMLFILE"))) {
        if (m_mainHtmlMode != ModeUrl)
            insertParameter(line, quote + path(MainHtmlDeployed) + quote);
        else
            adaptLine = false;
    } else if (line.contains(QLatin1String("// MAINHTMLURL"))) {
        if (m_mainHtmlMode == ModeUrl)
            insertParameter(line, quote + m_mainHtmlData + quote);
        else
            adaptLine = false;
144
145
146
147
148
149
    }
    return adaptLine;
}

void Html5App::handleCurrentProFileTemplateLine(const QString &line,
    QTextStream &proFileTemplate, QTextStream &proFile,
150
    bool &commentOutNextLine) const
151
{
Alessandro Portale's avatar
Alessandro Portale committed
152
    Q_UNUSED(proFileTemplate)
153
    Q_UNUSED(proFile)
Alessandro Portale's avatar
Alessandro Portale committed
154
    if (line.contains(QLatin1String("# TOUCH_OPTIMIZED_NAVIGATION")))
155
        commentOutNextLine = !m_touchOptimizedNavigationEnabled;
156
157
158
159
160
161
}

#ifndef CREATORLESSTEST
Core::GeneratedFiles Html5App::generateFiles(QString *errorMessage) const
{
    Core::GeneratedFiles files = AbstractMobileApp::generateFiles(errorMessage);
162
163
    if (m_mainHtmlMode == ModeGenerate) {
        files.append(file(generateFile(Html5AppGeneratedFileInfo::MainHtmlFile, errorMessage), path(MainHtml)));
164
        files.last().setAttributes(Core::GeneratedFile::OpenEditorAttribute);
165
    }
166
167
168
169
170
171
172
173
174
175
176
177
178
179

    files.append(file(generateFile(Html5AppGeneratedFileInfo::AppViewerPriFile, errorMessage), path(AppViewerPri)));
    files.append(file(generateFile(Html5AppGeneratedFileInfo::AppViewerCppFile, errorMessage), path(AppViewerCpp)));
    files.append(file(generateFile(Html5AppGeneratedFileInfo::AppViewerHFile, errorMessage), path(AppViewerH)));

    return files;
}
#endif // CREATORLESSTEST

QByteArray Html5App::generateFileExtended(int fileType,
    bool *versionAndCheckSum, QString *comment, QString *errorMessage) const
{
    QByteArray data;
    switch (fileType) {
180
181
        case Html5AppGeneratedFileInfo::MainHtmlFile:
            data = readBlob(path(MainHtmlOrigin), errorMessage);
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
            break;
        case Html5AppGeneratedFileInfo::AppViewerPriFile:
            data = readBlob(path(AppViewerPriOrigin), errorMessage);
            data.append(readBlob(path(DeploymentPriOrigin), errorMessage));
            *comment = ProFileComment;
            *versionAndCheckSum = true;
            break;
        case Html5AppGeneratedFileInfo::AppViewerCppFile:
            data = readBlob(path(AppViewerCppOrigin), errorMessage);
            *versionAndCheckSum = true;
            break;
        case Html5AppGeneratedFileInfo::AppViewerHFile:
        default:
            data = readBlob(path(AppViewerHOrigin), errorMessage);
            *versionAndCheckSum = true;
            break;
    }
    return data;
}

int Html5App::stubVersionMinor() const
{
    return StubVersion;
}

QList<AbstractGeneratedFileInfo> Html5App::updateableFiles(const QString &mainProFile) const
{
    QList<AbstractGeneratedFileInfo> result;
    static const struct {
        int fileType;
        QString fileName;
    } files[] = {
        {Html5AppGeneratedFileInfo::AppViewerPriFile, appViewerPriFileName},
        {Html5AppGeneratedFileInfo::AppViewerHFile, appViewerHFileName},
        {Html5AppGeneratedFileInfo::AppViewerCppFile, appViewerCppFileName}
    };
    const QFileInfo mainProFileInfo(mainProFile);
    const int size = sizeof(files) / sizeof(files[0]);
    for (int i = 0; i < size; ++i) {
        const QString fileName = mainProFileInfo.dir().absolutePath()
                + QLatin1Char('/') + appViewerOriginsSubDir + files[i].fileName;
        if (!QFile::exists(fileName))
            continue;
        Html5AppGeneratedFileInfo file;
        file.fileType = files[i].fileType;
        file.fileInfo = QFileInfo(fileName);
        file.currentVersion = AbstractMobileApp::makeStubVersion(Html5App::StubVersion);
        result.append(file);
    }
    if (result.count() != size)
        result.clear(); // All files must be found. No wrong/partial updates, please.
    return result;
}

236
237
238
QList<DeploymentFolder> Html5App::deploymentFolders() const
{
    QList<DeploymentFolder> result;
239
240
    if (m_mainHtmlMode != ModeUrl)
        result.append(DeploymentFolder(path(HtmlDirProFileRelative), QLatin1String(".")));
241
242
243
    return result;
}

244
245
246
247
const int Html5App::StubVersion = 10;

} // namespace Internal
} // namespace Qt4ProjectManager