qtquickapp.cpp 16 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11
12
13
14
** 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.
15
16
17
18
19
20
21
22
23
24
**
** 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.
**
con's avatar
con committed
25
26
27
28
29
30
** 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.
31
32
33
**
**************************************************************************/

34
#include "qtquickapp.h"
35
36
37
38
39

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

42
43
44
45
#ifndef CREATORLESSTEST
#include <coreplugin/icore.h>
#endif // CREATORLESSTEST

46
namespace Qt4ProjectManager {
47
48
namespace Internal {

49
50
51
52
53
54
55
const QString qmldir(QLatin1String("qmldir"));
const QString qmldir_plugin(QLatin1String("plugin"));
const QString appViewerBaseName(QLatin1String("qmlapplicationviewer"));
const QString appViewerPriFileName(appViewerBaseName + QLatin1String(".pri"));
const QString appViewerCppFileName(appViewerBaseName + QLatin1String(".cpp"));
const QString appViewerHFileName(appViewerBaseName + QLatin1String(".h"));
const QString appViewerOriginsSubDir(appViewerBaseName + QLatin1Char('/'));
56

57
QmlModule::QmlModule(const QString &uri, const QFileInfo &rootDir, const QFileInfo &qmldir,
58
                     bool isExternal, QtQuickApp *qtQuickApp)
59
    : uri(uri)
60
61
62
    , rootDir(rootDir)
    , qmldir(qmldir)
    , isExternal(isExternal)
63
    , qtQuickApp(qtQuickApp)
64
65
{}

66
QString QmlModule::path(Path path) const
67
{
68
    switch (path) {
69
70
71
72
        case Root: {
            return rootDir.canonicalFilePath();
        }
        case ContentDir: {
73
            const QDir proFile(qtQuickApp->path(QtQuickApp::AppProPath));
74
75
76
77
78
79
80
81
82
83
            return proFile.relativeFilePath(qmldir.canonicalPath());
        }
        case ContentBase: {
            const QString localRoot = rootDir.canonicalFilePath() + QLatin1Char('/');
            QDir contentDir = qmldir.dir();
            contentDir.cdUp();
            const QString localContentDir = contentDir.canonicalPath();
            return localContentDir.right(localContentDir.length() - localRoot.length());
        }
        case DeployedContentBase: {
84
            const QString modulesDir = qtQuickApp->path(QtQuickApp::ModulesDir);
85
            return modulesDir + QLatin1Char('/') + this->path(ContentBase);
86
87
88
89
90
91
92
93
94
95
96
97
        }
        default: qFatal("QmlModule::path() needs more work");
    }
    return QString();
}

QmlCppPlugin::QmlCppPlugin(const QString &name, const QFileInfo &path,
                           const QmlModule *module, const QFileInfo &proFile)
    : name(name)
    , path(path)
    , module(module)
    , proFile(proFile)
98
99
100
{
}

101
102
103
QtQuickApp::QtQuickApp()
    : AbstractMobileApp()
    , m_mainQmlMode(ModeGenerate)
104
105
106
{
}

107
QtQuickApp::~QtQuickApp()
108
109
110
111
{
    clearModulesAndPlugins();
}

112
void QtQuickApp::setMainQml(Mode mode, const QString &file)
113
{
114
115
116
    Q_ASSERT(mode != ModeGenerate || file.isEmpty());
    m_mainQmlMode = mode;
    m_mainQmlFile.setFile(file);
117
118
}

119
QtQuickApp::Mode QtQuickApp::mainQmlMode() const
120
{
121
    return m_mainQmlMode;
122
123
}

124
bool QtQuickApp::setExternalModules(const QStringList &uris,
125
                                    const QStringList &importPaths)
126
127
128
129
130
{
    clearModulesAndPlugins();
    m_importPaths.clear();
    foreach (const QFileInfo &importPath, importPaths) {
        if (!importPath.exists()) {
131
            m_error = QCoreApplication::translate(
132
                        "Qt4ProjectManager::Internal::QtQuickApp",
133
                        "The QML import path '%1' cannot be found.")
134
135
136
137
138
139
                      .arg(QDir::toNativeSeparators(importPath.filePath()));
            return false;
        } else {
            m_importPaths.append(importPath.canonicalFilePath());
        }
    }
140
141
142
    foreach (const QString &uri, uris) {
        QString uriPath = uri;
        uriPath.replace(QLatin1Char('.'), QLatin1Char('/'));
143
144
145
146
        const int modulesCount = m_modules.count();
        foreach (const QFileInfo &importPath, m_importPaths) {
            const QFileInfo qmlDirFile(
                    importPath.absoluteFilePath() + QLatin1Char('/')
147
                    + uriPath + QLatin1Char('/') + qmldir);
148
            if (qmlDirFile.exists()) {
149
                if (!addExternalModule(uri, importPath, qmlDirFile))
150
151
152
153
154
                    return false;
                break;
            }
        }
        if (modulesCount == m_modules.count()) { // no module was added
155
            m_error = QCoreApplication::translate(
156
                      "Qt4ProjectManager::Internal::QtQuickApp",
157
                      "The QML module '%1' cannot be found.").arg(uri);
158
159
160
161
162
163
164
            return false;
        }
    }
    m_error.clear();
    return true;
}

165
QString QtQuickApp::pathExtended(int fileType) const
166
{
167
    QString cleanProjectName = projectName().replace(QLatin1Char('-'), QString());
168
    const bool importQmlFile = m_mainQmlMode == ModeImport;
169
    const QString qmlSubDir = QLatin1String("qml/")
170
171
                              + (importQmlFile ? m_mainQmlFile.dir().dirName() : cleanProjectName)
                              + QLatin1Char('/');
172
    const QString appViewerTargetSubDir = appViewerOriginsSubDir;
173
    const QString mainQml = QLatin1String("main.qml");
174
    const QString pathBase = outputPathBase();
175
176
    const QDir appProFilePath(pathBase);

177
    switch (fileType) {
178
179
180
181
        case MainQml:                       return importQmlFile ? m_mainQmlFile.canonicalFilePath()
                                                                 : pathBase + qmlSubDir + mainQml;
        case MainQmlDeployed:               return importQmlFile ? qmlSubDir + m_mainQmlFile.fileName()
                                                                 : QString(qmlSubDir + mainQml);
182
        case MainQmlOrigin:                 return originsRoot() + QLatin1String("qml/app/") + mainQml;
183
        case AppViewerPri:                  return pathBase + appViewerTargetSubDir + appViewerPriFileName;
184
        case AppViewerPriOrigin:            return originsRoot() + appViewerOriginsSubDir + appViewerPriFileName;
185
        case AppViewerCpp:                  return pathBase + appViewerTargetSubDir + appViewerCppFileName;
186
        case AppViewerCppOrigin:            return originsRoot() + appViewerOriginsSubDir + appViewerCppFileName;
187
        case AppViewerH:                    return pathBase + appViewerTargetSubDir + appViewerHFileName;
188
        case AppViewerHOrigin:              return originsRoot() + appViewerOriginsSubDir + appViewerHFileName;
189
        case QmlDir:                        return pathBase + qmlSubDir;
190
191
        case QmlDirProFileRelative:         return importQmlFile ? appProFilePath.relativeFilePath(m_mainQmlFile.canonicalPath())
                                                                 : QString(qmlSubDir).remove(qmlSubDir.length() - 1, 1);
192
        case ModulesDir:                    return QLatin1String("modules");
193
        default:                            qFatal("QtQuickApp::pathExtended() needs more work");
194
195
196
197
    }
    return QString();
}

198
QString QtQuickApp::originsRoot() const
199
{
200
    return templatesRoot() + QLatin1String("qtquickapp/");
201
202
}

203
QString QtQuickApp::mainWindowClassName() const
204
{
205
    return QLatin1String("QmlApplicationViewer");
206
207
}

208
bool QtQuickApp::adaptCurrentMainCppTemplateLine(QString &line) const
209
{
210
211
212
213
214
215
216
217
218
219
220
221
222
    const QLatin1Char quote('"');
    bool adaptLine = true;
    if (line.contains(QLatin1String("// MAINQML"))) {
        insertParameter(line, quote + path(MainQmlDeployed) + quote);
    } else if (line.contains(QLatin1String("// ADDIMPORTPATH"))) {
        if (m_modules.isEmpty())
            adaptLine = false;
        else
            insertParameter(line, quote + path(ModulesDir) + quote);
    }
    return adaptLine;
}

223
void QtQuickApp::handleCurrentProFileTemplateLine(const QString &line,
224
    QTextStream &proFileTemplate, QTextStream &proFile,
225
    bool &commentOutNextLine) const
226
{
Alessandro Portale's avatar
Alessandro Portale committed
227
228
    Q_UNUSED(commentOutNextLine)
    if (line.contains(QLatin1String("# QML_IMPORT_PATH"))) {
229
230
231
232
233
234
235
236
237
238
239
240
241
242
        QString nextLine = proFileTemplate.readLine(); // eats 'QML_IMPORT_PATH ='
        if (!nextLine.startsWith(QLatin1String("QML_IMPORT_PATH =")))
            return;

        proFile << nextLine;

        const QLatin1String separator(" \\\n    ");
        const QDir proPath(path(AppProPath));
        foreach (const QString &importPath, m_importPaths) {
            const QString relativePath = proPath.relativeFilePath(importPath);
            proFile << separator << relativePath;
        }

        proFile << endl;
243
    }
244
245
}

246
void QtQuickApp::clearModulesAndPlugins()
247
248
249
250
251
252
253
{
    qDeleteAll(m_modules);
    m_modules.clear();
    qDeleteAll(m_cppPlugins);
    m_cppPlugins.clear();
}

254
bool QtQuickApp::addCppPlugin(const QString &qmldirLine, QmlModule *module)
255
256
257
258
{
    const QStringList qmldirLineElements =
            qmldirLine.split(QLatin1Char(' '), QString::SkipEmptyParts);
    if (qmldirLineElements.count() < 2) {
259
        m_error = QCoreApplication::translate(
260
                      "Qt4ProjectManager::Internal::QtQuickApp",
261
                      "Invalid '%1' entry in '%2' of module '%3'.")
262
                  .arg(qmldir_plugin).arg(qmldir).arg(module->uri);
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
        return false;
    }
    const QString name = qmldirLineElements.at(1);
    const QFileInfo path(module->qmldir.dir(), qmldirLineElements.value(2, QString()));

    // TODO: Add more magic to find a good .pro file..
    const QString proFileName = name + QLatin1String(".pro");
    const QFileInfo proFile_guess1(module->qmldir.dir(), proFileName);
    const QFileInfo proFile_guess2(QString(module->qmldir.dir().absolutePath() + QLatin1String("/../")),
                                   proFileName);
    const QFileInfo proFile_guess3(module->qmldir.dir(),
                                   QFileInfo(module->qmldir.path()).fileName() + QLatin1String(".pro"));
    const QFileInfo proFile_guess4(proFile_guess3.absolutePath() + QLatin1String("/../")
                                   + proFile_guess3.fileName());

    QFileInfo foundProFile;
    if (proFile_guess1.exists()) {
        foundProFile = proFile_guess1.canonicalFilePath();
    } else if (proFile_guess2.exists()) {
        foundProFile = proFile_guess2.canonicalFilePath();
    } else if (proFile_guess3.exists()) {
        foundProFile = proFile_guess3.canonicalFilePath();
    } else if (proFile_guess4.exists()) {
        foundProFile = proFile_guess4.canonicalFilePath();
    } else {
288
        m_error = QCoreApplication::translate(
289
                    "Qt4ProjectManager::Internal::QtQuickApp",
290
                    "No .pro file for plugin '%1' cannot be found.").arg(name);
291
292
293
294
295
296
297
298
299
        return false;
    }
    QmlCppPlugin *plugin =
            new QmlCppPlugin(name, path, module, foundProFile);
    m_cppPlugins.append(plugin);
    module->cppPlugins.insert(name, plugin);
    return true;
}

300
bool QtQuickApp::addCppPlugins(QmlModule *module)
301
302
303
{
    QFile qmlDirFile(module->qmldir.absoluteFilePath());
    if (qmlDirFile.open(QIODevice::ReadOnly)) {
304
        QTextStream in(&qmlDirFile);
305
        QString line;
306
307
        while (!(line = in.readLine()).isNull()) {
            line = line.trimmed();
308
309
            if (line.startsWith(qmldir_plugin) && !addCppPlugin(line, module))
                return false;
310
        };
311
312
313
314
    }
    return true;
}

315
bool QtQuickApp::addExternalModule(const QString &name, const QFileInfo &dir,
316
317
318
319
320
321
322
                                         const QFileInfo &contentDir)
{
    QmlModule *module = new QmlModule(name, dir, contentDir, true, this);
    m_modules.append(module);
    return addCppPlugins(module);
}

323
#ifndef CREATORLESSTEST
324
Core::GeneratedFiles QtQuickApp::generateFiles(QString *errorMessage) const
325
{
326
    Core::GeneratedFiles files = AbstractMobileApp::generateFiles(errorMessage);
327
    if (!useExistingMainQml()) {
328
        files.append(file(generateFile(QtQuickAppGeneratedFileInfo::MainQmlFile, errorMessage), path(MainQml)));
329
330
        files.last().setAttributes(Core::GeneratedFile::OpenEditorAttribute);
    }
331

332
333
334
    files.append(file(generateFile(QtQuickAppGeneratedFileInfo::AppViewerPriFile, errorMessage), path(AppViewerPri)));
    files.append(file(generateFile(QtQuickAppGeneratedFileInfo::AppViewerCppFile, errorMessage), path(AppViewerCpp)));
    files.append(file(generateFile(QtQuickAppGeneratedFileInfo::AppViewerHFile, errorMessage), path(AppViewerH)));
335
336
337
338
339

    return files;
}
#endif // CREATORLESSTEST

340
bool QtQuickApp::useExistingMainQml() const
341
342
343
344
{
    return !m_mainQmlFile.filePath().isEmpty();
}

345
const QList<QmlModule*> QtQuickApp::modules() const
346
347
348
349
{
    return m_modules;
}

350
QByteArray QtQuickApp::generateFileExtended(int fileType,
351
    bool *versionAndCheckSum, QString *comment, QString *errorMessage) const
352
353
{
    QByteArray data;
354
    switch (fileType) {
355
        case QtQuickAppGeneratedFileInfo::MainQmlFile:
356
            data = readBlob(path(MainQmlOrigin), errorMessage);
357
            break;
358
        case QtQuickAppGeneratedFileInfo::AppViewerPriFile:
359
            data = readBlob(path(AppViewerPriOrigin), errorMessage);
360
            data.append(readBlob(path(DeploymentPriOrigin), errorMessage));
361
362
            *comment = ProFileComment;
            *versionAndCheckSum = true;
363
            break;
364
        case QtQuickAppGeneratedFileInfo::AppViewerCppFile:
365
366
            data = readBlob(path(AppViewerCppOrigin), errorMessage);
            *versionAndCheckSum = true;
367
            break;
368
        case QtQuickAppGeneratedFileInfo::AppViewerHFile:
369
        default:
370
371
            data = readBlob(path(AppViewerHOrigin), errorMessage);
            *versionAndCheckSum = true;
372
373
            break;
    }
374
    return data;
375
376
}

377
int QtQuickApp::stubVersionMinor() const
378
{
379
    return StubVersion;
380
381
}

382
QList<AbstractGeneratedFileInfo> QtQuickApp::updateableFiles(const QString &mainProFile) const
383
{
384
    QList<AbstractGeneratedFileInfo> result;
385
    static const struct {
386
        int fileType;
387
388
        QString fileName;
    } files[] = {
389
390
391
        {QtQuickAppGeneratedFileInfo::AppViewerPriFile, appViewerPriFileName},
        {QtQuickAppGeneratedFileInfo::AppViewerHFile, appViewerHFileName},
        {QtQuickAppGeneratedFileInfo::AppViewerCppFile, appViewerCppFileName}
392
393
    };
    const QFileInfo mainProFileInfo(mainProFile);
394
395
    const int size = sizeof(files) / sizeof(files[0]);
    for (int i = 0; i < size; ++i) {
396
        const QString fileName = mainProFileInfo.dir().absolutePath()
397
                + QLatin1Char('/') + appViewerOriginsSubDir + files[i].fileName;
398
399
        if (!QFile::exists(fileName))
            continue;
400
        QtQuickAppGeneratedFileInfo file;
401
        file.fileType = files[i].fileType;
402
        file.fileInfo = QFileInfo(fileName);
403
        file.currentVersion = AbstractMobileApp::makeStubVersion(QtQuickApp::StubVersion);
404
405
        result.append(file);
    }
406
407
    if (result.count() != size)
        result.clear(); // All files must be found. No wrong/partial updates, please.
408
409
410
    return result;
}

411
412
413
414
415
416
417
418
419
420
QList<DeploymentFolder> QtQuickApp::deploymentFolders() const
{
    QList<DeploymentFolder> result;
    result.append(DeploymentFolder(path(QmlDirProFileRelative), QLatin1String("qml")));
    foreach (const QmlModule *module, m_modules)
        if (module->isExternal)
            result.append(DeploymentFolder(module->path(QmlModule::ContentDir), module->path(QmlModule::DeployedContentBase)));
    return result;
}

421
const int QtQuickApp::StubVersion = 11;
422

423
} // namespace Internal
424
} // namespace Qt4ProjectManager