qmlstandaloneapp.cpp 12.7 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 57
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "qmlstandaloneapp.h"

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

namespace QmlProjectManager {
namespace Internal {

QmlStandaloneApp::QmlStandaloneApp()
    : m_loadDummyData(false)
    , m_orientation(Auto)
    , m_networkEnabled(false)
{
}

QString QmlStandaloneApp::symbianUidForPath(const QString &path)
{
    quint32 hash = 5381;
    for (int i = 0; i < path.size(); ++i) {
        const char c = path.at(i).toAscii();
        hash ^= c + ((c - i) << i % 20) + ((c + i) << (i + 5) % 20) + ((c - 2 * i) << (i + 10) % 20) + ((c + 2 * i) << (i + 15) % 20);
    }
    return QString::fromLatin1("0xE")
            + QString::fromLatin1("%1").arg(hash, 7, 16, QLatin1Char('0')).right(7);
}

58 59 60 61 62 63 64 65 66 67
void QmlStandaloneApp::setMainQmlFile(const QString &qmlFile)
{
    m_mainQmlFile.setFile(qmlFile);
}

QString QmlStandaloneApp::mainQmlFile() const
{
    return path(MainQml, Target);
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
void QmlStandaloneApp::setOrientation(Orientation orientation)
{
    m_orientation = orientation;
}

QmlStandaloneApp::Orientation QmlStandaloneApp::orientation() const
{
    return m_orientation;
}

void QmlStandaloneApp::setProjectName(const QString &name)
{
    m_projectName = name;
}

QString QmlStandaloneApp::projectName() const
{
    return m_projectName;
}

void QmlStandaloneApp::setProjectPath(const QString &path)
{
90
    m_projectPath.setFile(path);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}

void QmlStandaloneApp::setSymbianSvgIcon(const QString &icon)
{
    m_symbianSvgIcon = icon;
}

QString QmlStandaloneApp::symbianSvgIcon() const
{
    return path(SymbianSvgIcon, Source);
}

void QmlStandaloneApp::setSymbianTargetUid(const QString &uid)
{
    m_symbianTargetUid = uid;
}

QString QmlStandaloneApp::symbianTargetUid() const
{
    return !m_symbianTargetUid.isEmpty() ? m_symbianTargetUid
111
        : symbianUidForPath(path(AppProfile, Target));
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

void QmlStandaloneApp::setLoadDummyData(bool loadIt)
{
    m_loadDummyData = loadIt;
}

bool QmlStandaloneApp::loadDummyData() const
{
    return m_loadDummyData;
}

void QmlStandaloneApp::setNetworkEnabled(bool enabled)
{
    m_networkEnabled = enabled;
}

bool QmlStandaloneApp::networkEnabled() const
{
    return m_networkEnabled;
}

QString QmlStandaloneApp::path(Path path, Location location) const
{
136 137 138
    const QString qmlRootFolder = QLatin1String("qml/")
                                  + (useExistingMainQml() ? m_mainQmlFile.dir().dirName() : m_projectName)
                                  + QLatin1Char('/');
139 140 141 142 143 144 145 146 147 148 149 150 151
    const QString sourceRoot = QLatin1String(":/qmlproject/wizards/templates/");
    const QString cppSourceSubDir = QLatin1String("cpp/");
    const QString cppTargetSubDir = cppSourceSubDir;
    const QString qmlExtension = QLatin1String(".qml");
    const QString mainCpp = QLatin1String("main.cpp");
    const QString appViewCpp = QLatin1String("qmlapplicationview.cpp");
    const QString appViewH = QLatin1String("qmlapplicationview.h");
    const QString symbianIcon = QLatin1String("symbianicon.svg");
    const char* const errorMessage = "QmlStandaloneApp::path() needs more work";

    switch (location) {
        case Source: {
            switch (path) {
152
                case MainQml:           return sourceRoot + QLatin1String("qml/app/app.qml");
153 154 155 156 157
                case AppProfile:        return sourceRoot + QLatin1String("app.pro");
                case MainCpp:           return sourceRoot + cppSourceSubDir + mainCpp;
                case AppViewerCpp:      return sourceRoot + cppSourceSubDir + appViewCpp;
                case AppViewerH:        return sourceRoot + cppSourceSubDir + appViewH;
                case SymbianSvgIcon:    return !m_symbianSvgIcon.isEmpty() ? m_symbianSvgIcon
158
                                                    : sourceRoot + cppSourceSubDir + symbianIcon;
159 160 161 162
                default:                qFatal(errorMessage);
            }
        }
        case Target: {
163 164
            const QString pathBase = m_projectPath.absoluteFilePath() + QLatin1Char('/')
                                     + m_projectName + QLatin1Char('/');
165
            switch (path) {
166 167
                case MainQml:           return useExistingMainQml() ? m_mainQmlFile.canonicalFilePath()
                                                    : pathBase + qmlRootFolder + m_projectName + qmlExtension;
168
                case AppProfile:        return pathBase + m_projectName + QLatin1String(".pro");
169
                case AppProfilePath:    return pathBase;
170 171 172 173
                case MainCpp:           return pathBase + cppTargetSubDir + mainCpp;
                case AppViewerCpp:      return pathBase + cppTargetSubDir + appViewCpp;
                case AppViewerH:        return pathBase + cppTargetSubDir + appViewH;
                case SymbianSvgIcon:    return pathBase + cppTargetSubDir + symbianIcon;
174
                case QmlDir:            return pathBase + qmlRootFolder;
175 176 177 178
                default:                qFatal(errorMessage);
            }
        }
        case AppProfileRelative: {
179
            const QDir appProFilePath(this->path(AppProfilePath, Target));
180
            switch (path) {
181 182
                case MainQml:           return useExistingMainQml() ? appProFilePath.relativeFilePath(m_mainQmlFile.canonicalFilePath())
                                                        : qmlRootFolder + m_projectName + qmlExtension;
183 184 185 186
                case MainCpp:           return cppTargetSubDir + mainCpp;
                case AppViewerCpp:      return cppTargetSubDir + appViewCpp;
                case AppViewerH:        return cppTargetSubDir + appViewH;
                case SymbianSvgIcon:    return cppTargetSubDir + symbianIcon;
187 188
                case QmlDir:            return useExistingMainQml() ? appProFilePath.relativeFilePath(m_mainQmlFile.canonicalPath())
                                                        : QString(qmlRootFolder).remove(qmlRootFolder.length() - 1, 1);
189 190 191 192 193
                default:                qFatal(errorMessage);
            }
        }
        default: { /* case MainCppRelative: */
            switch (path) {
194 195
                case MainQml:           return useExistingMainQml() ? qmlRootFolder + m_mainQmlFile.fileName()
                                                        : QString(qmlRootFolder + m_projectName + qmlExtension);
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
                default:                qFatal(errorMessage);
            }
        }
    }
    return QString();
}


static QString insertParameter(const QString &line, const QString &parameter)
{
    return QString(line).replace(QRegExp(QLatin1String("\\([^()]+\\)")),
                                 QLatin1Char('(') + parameter + QLatin1Char(')'));
}

QByteArray QmlStandaloneApp::generateMainCpp(const QString *errorMessage) const
{
    Q_UNUSED(errorMessage)

    QFile sourceFile(path(MainCpp, Source));
    sourceFile.open(QIODevice::ReadOnly);
    Q_ASSERT(sourceFile.isOpen());
    QTextStream in(&sourceFile);

    QByteArray mainCppContent;
    QTextStream out(&mainCppContent, QIODevice::WriteOnly);

    QString line;
    do {
        line = in.readLine();
        if (line.contains(QLatin1String("// MAINQML"))) {
            line = insertParameter(line, QLatin1Char('"')
                                   + path(MainQml, MainCppRelative) + QLatin1Char('"'));
        } else if (line.contains(QLatin1String("// IMPORTPATHSLIST"))) {
            continue;
        } else if (line.contains(QLatin1String("// SETIMPORTPATHLIST"))) {
            continue;
        } else if (line.contains(QLatin1String("// ORIENTATION"))) {
            if (m_orientation == Auto)
                continue;
            else
                line = insertParameter(line, QLatin1String("QmlApplicationView::")
                                       + QLatin1String(m_orientation == LockLandscape ?
                                                       "LockLandscape" : "LockPortrait"));
        } else if (line.contains(QLatin1String("// LOADDUMMYDATA"))) {
            continue;
        }
        const int commentIndex = line.indexOf(QLatin1String(" //"));
        if (commentIndex != -1)
            line.truncate(commentIndex);
        out << line << endl;
    } while (!line.isNull());

    return mainCppContent;
}

QByteArray QmlStandaloneApp::generateProFile(const QString *errorMessage) const
{
    Q_UNUSED(errorMessage)

    QFile proFile(path(AppProfile, Source));
    proFile.open(QIODevice::ReadOnly);
    Q_ASSERT(proFile.isOpen());
    QTextStream in(&proFile);

    QByteArray proFileContent;
    QTextStream out(&proFileContent, QIODevice::WriteOnly);

    QString line;
    QString valueOnNextLine;
    bool uncommentNextLine = false;
    do {
        line = in.readLine();

        if (line.contains(QLatin1String("# MAINQML"))) {
            valueOnNextLine = path(MainQml, AppProfileRelative);
        } else if (line.contains(QLatin1String("# TARGETUID3"))) {
            valueOnNextLine = symbianTargetUid();
        } else if (line.contains(QLatin1String("# DEPLOYMENTFOLDERS"))) {
            valueOnNextLine = path(QmlDir, AppProfileRelative);
        } else if (line.contains(QLatin1String("# ORIENTATIONLOCK")) && m_orientation == QmlStandaloneApp::Auto) {
            uncommentNextLine = true;
        } else if (line.contains(QLatin1String("# NETWORKACCESS")) && !m_networkEnabled) {
            uncommentNextLine = true;
        }

        // Remove all marker comments
        if (line.trimmed().startsWith(QLatin1Char('#'))
            && line.trimmed().endsWith(QLatin1Char('#')))
            continue;

        if (!valueOnNextLine.isEmpty()) {
            out << line.left(line.indexOf(QLatin1Char('=')) + 2)
                    << QDir::fromNativeSeparators(valueOnNextLine) << endl;
            valueOnNextLine.clear();
            continue;
        }

        if (uncommentNextLine) {
            out << QLatin1String("#") << line << endl;
            uncommentNextLine = false;
            continue;
        }
        out << line << endl;
    } while (!line.isNull());

    return proFileContent;
}

#ifndef CREATORLESSTEST
static Core::GeneratedFile generateFileCopy(const QString &source,
                                            const QString &target,
                                            bool openEditor = false)
{
    QFile sourceFile(source);
    sourceFile.open(QIODevice::ReadOnly);
    Q_ASSERT(sourceFile.isOpen());
    Core::GeneratedFile generatedFile(target);
    generatedFile.setBinary(true);
    generatedFile.setBinaryContents(sourceFile.readAll());
    if (openEditor)
        generatedFile.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
    return generatedFile;
}

Core::GeneratedFiles QmlStandaloneApp::generateFiles(QString *errorMessage) const
{
    Core::GeneratedFiles files;

    Core::GeneratedFile generatedProFile(path(AppProfile, Target));
    generatedProFile.setContents(generateProFile(errorMessage));
    generatedProFile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
    files.append(generatedProFile);

329 330
    if (!useExistingMainQml())
        files.append(generateFileCopy(path(MainQml, Source), path(MainQml, Target), true));
331 332 333 334 335 336 337 338 339 340 341 342 343

    Core::GeneratedFile generatedMainCppFile(path(MainCpp, Target));
    generatedMainCppFile.setContents(generateMainCpp(errorMessage));
    files.append(generatedMainCppFile);

    files.append(generateFileCopy(path(AppViewerCpp, Source), path(AppViewerCpp, Target)));
    files.append(generateFileCopy(path(AppViewerH, Source), path(AppViewerH, Target)));
    files.append(generateFileCopy(path(SymbianSvgIcon, Source), path(SymbianSvgIcon, Target)));

    return files;
}
#endif // CREATORLESSTEST

344 345 346 347 348
bool QmlStandaloneApp::useExistingMainQml() const
{
    return !m_mainQmlFile.filePath().isEmpty();
}

349 350
} // namespace Internal
} // namespace QmlProjectManager