bardescriptordocument.cpp 9.17 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
/**************************************************************************
**
** Copyright (C) 2011 - 2013 Research In Motion
**
** Contact: Research In Motion (blackberry-qt@qnx.com)
** Contact: KDAB (info@kdab.com)
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "bardescriptordocument.h"

#include "qnxconstants.h"
35
#include "bardescriptoreditor.h"
36 37 38 39
#include "bardescriptoreditorwidget.h"
#include "bardescriptordocumentnodehandlers.h"

#include <coreplugin/editormanager/ieditor.h>
40
#include <coreplugin/editormanager/editormanager.h>
41 42
#include <utils/qtcassert.h>

43 44 45 46
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QTextCodec>
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

using namespace Qnx;
using namespace Qnx::Internal;

BarDescriptorDocument::BarDescriptorDocument(BarDescriptorEditorWidget *editorWidget)
    : Core::TextDocument(editorWidget)
    , m_nodeHandlers(QList<BarDescriptorDocumentAbstractNodeHandler *>())
    , m_editorWidget(editorWidget)
{
    // General
    registerNodeHandler(new BarDescriptorDocumentIdNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentVersionNumberNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentBuildIdNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentAuthorNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentAuthorIdNodeHandler(m_editorWidget));

    // Application
    registerNodeHandler(new BarDescriptorDocumentApplicationNameNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentApplicationDescriptionNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentApplicationIconNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentSplashScreenNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentInitialWindowNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentArgNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentActionNodeHandler(m_editorWidget));
    registerNodeHandler(new BarDescriptorDocumentEnvNodeHandler(m_editorWidget));

    // Assets
    registerNodeHandler(new BarDescriptorDocumentAssetNodeHandler(m_editorWidget));
75 76 77 78 79 80 81

    // blackberry-nativepackager requires the XML file to be in UTF-8 encoding,
    // force if possible
    if (QTextCodec *defaultUTF8 = QTextCodec::codecForName("UTF-8"))
        setCodec(defaultUTF8);
    else
        setCodec(Core::EditorManager::instance()->defaultTextCodec());
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

BarDescriptorDocument::~BarDescriptorDocument()
{
    while (!m_nodeHandlers.isEmpty()) {
        BarDescriptorDocumentAbstractNodeHandler *nodeHandler = m_nodeHandlers.takeFirst();
        delete nodeHandler;
    }
}

bool BarDescriptorDocument::open(QString *errorString, const QString &fileName) {
    QString contents;
    if (read(fileName, &contents, errorString) != Utils::TextFileFormat::ReadSuccess)
        return false;

97
    setFileName(fileName);
98 99 100 101 102 103 104 105 106

    bool result = loadContent(contents);

    if (!result)
        *errorString = tr("%1 does not appear to be a valid application descriptor file").arg(QDir::toNativeSeparators(fileName));

    return result;
}

107
bool BarDescriptorDocument::save(QString *errorString, const QString &fn, bool autoSave)
108 109
{
    QTC_ASSERT(!autoSave, return false);
110
    QTC_ASSERT(fn.isEmpty(), return false);
111

112
    bool result = write(fileName(), xmlSource(), errorString);
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
    if (!result)
        return false;

    m_editorWidget->setDirty(false);
    emit changed();
    return true;
}

QString BarDescriptorDocument::defaultPath() const
{
    QFileInfo fi(fileName());
    return fi.absolutePath();
}

QString BarDescriptorDocument::suggestedFileName() const
{
    QFileInfo fi(fileName());
    return fi.fileName();
}

QString BarDescriptorDocument::mimeType() const
{
    return QLatin1String(Constants::QNX_BAR_DESCRIPTOR_MIME_TYPE);
}

bool BarDescriptorDocument::shouldAutoSave() const
{
    return false;
}

bool BarDescriptorDocument::isModified() const
{
    return m_editorWidget->isDirty();
}

bool BarDescriptorDocument::isSaveAsAllowed() const
{
    return false;
}

Core::IDocument::ReloadBehavior BarDescriptorDocument::reloadBehavior(Core::IDocument::ChangeTrigger state, Core::IDocument::ChangeType type) const
{
    if (type == TypeRemoved || type == TypePermissions)
        return BehaviorSilent;
    if (type == TypeContents && state == TriggerInternal && !isModified())
        return BehaviorSilent;
    return BehaviorAsk;
}

bool BarDescriptorDocument::reload(QString *errorString, Core::IDocument::ReloadFlag flag, Core::IDocument::ChangeType type)
{
    Q_UNUSED(type);

    if (flag == Core::IDocument::FlagIgnore)
        return true;

169
    return open(errorString, fileName());
170 171
}

172
void BarDescriptorDocument::setFileName(const QString &newName)
173
{
174 175
    m_editorWidget->editor()->setDisplayName(QFileInfo(newName).fileName());
    IDocument::setFileName(newName);
176 177 178 179
}

QString BarDescriptorDocument::xmlSource() const
{
180 181
    BarDescriptorEditor *editor = qobject_cast<BarDescriptorEditor*>(m_editorWidget->editor());
    QTC_ASSERT(editor, return QString());
182

183 184 185 186 187
    if (editor->activePage() == BarDescriptorEditor::Source) {
        return m_editorWidget->xmlSource();
    } else {
        QDomDocument doc;
        doc.appendChild(doc.createProcessingInstruction(QLatin1String("xml"), QLatin1String("version='1.0' encoding='") + QLatin1String(codec()->name()) + QLatin1String("' standalone='no'")));
188

189 190 191
        // QNX
        QDomElement rootElem = doc.createElement(QLatin1String("qnx"));
        rootElem.setAttribute(QLatin1String("xmlns"), QLatin1String("http://www.qnx.com/schemas/application/1.0"));
192

193 194 195
        QMap<int, BarDescriptorDocumentAbstractNodeHandler*> nodeHandlerMap;
        foreach (BarDescriptorDocumentAbstractNodeHandler *nodeHandler, m_nodeHandlers)
            nodeHandlerMap.insertMulti(nodeHandler->order(), nodeHandler);
196

197 198 199
        QList<BarDescriptorDocumentAbstractNodeHandler*> nodeHandlers = nodeHandlerMap.values();
        foreach (BarDescriptorDocumentAbstractNodeHandler *nodeHandler, nodeHandlers)
            rootElem.appendChild(nodeHandler->toNode(doc));
200

201 202 203 204
        doc.appendChild(rootElem);

        return doc.toString(4);
    }
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
}

bool BarDescriptorDocument::loadContent(const QString &xmlSource, QString *errorMessage, int *errorLine)
{
    QDomDocument doc;
    bool result = doc.setContent(xmlSource, errorMessage, errorLine);
    if (!result)
        return false;

    QDomElement docElem = doc.documentElement();
    if (docElem.tagName() != QLatin1String("qnx"))
        return false;

    m_editorWidget->clear();

    removeUnknownNodeHandlers();
    foreach (BarDescriptorDocumentAbstractNodeHandler *nodeHandler, m_nodeHandlers)
        nodeHandler->clear();

    QDomNode node = docElem.firstChildElement();
    while (!node.isNull()) {
        BarDescriptorDocumentAbstractNodeHandler *nodeHandler = nodeHandlerForDomNode(node);
        if (!nodeHandler) {
            nodeHandler = new BarDescriptorDocumentUnknownNodeHandler(m_editorWidget);
            registerNodeHandler(nodeHandler);
        }

        if (!nodeHandler->handle(node))
            return false;

        node = node.nextSibling();
    }

    m_editorWidget->setXmlSource(xmlSource);

    return true;
}

void BarDescriptorDocument::registerNodeHandler(BarDescriptorDocumentAbstractNodeHandler *nodeHandler)
{
    m_nodeHandlers << nodeHandler;
}

BarDescriptorDocumentAbstractNodeHandler *BarDescriptorDocument::nodeHandlerForDomNode(const QDomNode &node)
{
    foreach (BarDescriptorDocumentAbstractNodeHandler *handler, m_nodeHandlers) {
        if (handler->canHandle(node) && !dynamic_cast<BarDescriptorDocumentUnknownNodeHandler*>(handler))
            return handler;
    }

    return 0;
}

void BarDescriptorDocument::removeUnknownNodeHandlers()
{
    for (int i = m_nodeHandlers.size() - 1; i >= 0; --i) {
        BarDescriptorDocumentUnknownNodeHandler *nodeHandler = dynamic_cast<BarDescriptorDocumentUnknownNodeHandler*>(m_nodeHandlers[i]);
        if (nodeHandler) {
            m_nodeHandlers.removeAt(i);
            delete nodeHandler;
        }
    }
}