Commit ed69c5f9 authored by Tobias Nätterlund's avatar Tobias Nätterlund Committed by Nicolas Arnaud-Cormos

QNX: Add editor for Bar descriptor file

Change-Id: I7ad9bb9448ef467eea353d361b72474fe1b8c8f7
Reviewed-by: Nicolas Arnaud-Cormos's avatarNicolas Arnaud-Cormos <nicolas@kdab.com>
parent d3286c2c
/**************************************************************************
**
** 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"
#include "bardescriptoreditorwidget.h"
#include "bardescriptordocumentnodehandlers.h"
#include <coreplugin/editormanager/ieditor.h>
#include <utils/qtcassert.h>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QTextCodec>
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));
}
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;
m_fileName = fileName;
m_editorWidget->editor()->setDisplayName(QFileInfo(fileName).fileName());
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;
}
bool BarDescriptorDocument::save(QString *errorString, const QString &fileName, bool autoSave)
{
QTC_ASSERT(!autoSave, return false);
QTC_ASSERT(fileName.isEmpty(), return false);
bool result = write(m_fileName, xmlSource(), errorString);
if (!result)
return false;
m_editorWidget->setDirty(false);
emit changed();
return true;
}
QString BarDescriptorDocument::fileName() const
{
return m_fileName;
}
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;
return open(errorString, m_fileName);
}
void BarDescriptorDocument::rename(const QString &newName)
{
const QString oldFilename = m_fileName;
m_fileName = newName;
m_editorWidget->editor()->setDisplayName(QFileInfo(m_fileName).fileName());
emit fileNameChanged(oldFilename, newName);
emit changed();
}
QString BarDescriptorDocument::xmlSource() const
{
QDomDocument doc;
doc.appendChild(doc.createProcessingInstruction(QLatin1String("xml"), QLatin1String("version='1.0' encoding='") + QLatin1String(codec()->name()) + QLatin1String("' standalone='no'")));
// QNX
QDomElement rootElem = doc.createElement(QLatin1String("qnx"));
rootElem.setAttribute(QLatin1String("xmlns"), QLatin1String("http://www.qnx.com/schemas/application/1.0"));
QMap<int, BarDescriptorDocumentAbstractNodeHandler*> nodeHandlerMap;
foreach (BarDescriptorDocumentAbstractNodeHandler *nodeHandler, m_nodeHandlers)
nodeHandlerMap.insertMulti(nodeHandler->order(), nodeHandler);
QList<BarDescriptorDocumentAbstractNodeHandler*> nodeHandlers = nodeHandlerMap.values();
foreach (BarDescriptorDocumentAbstractNodeHandler *nodeHandler, nodeHandlers)
rootElem.appendChild(nodeHandler->toNode(doc));
doc.appendChild(rootElem);
return doc.toString(4);
}
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;
}
}
}
/**************************************************************************
**
** 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.
**
****************************************************************************/
#ifndef QNX_INTERNAL_BARDESCRIPTORDOCUMENT_H
#define QNX_INTERNAL_BARDESCRIPTORDOCUMENT_H
#include <coreplugin/textdocument.h>
#include <QDomNode>
namespace Qnx {
namespace Internal {
struct BarDescriptorAsset {
QString source;
QString destination;
bool entry;
};
class BarDescriptorEditorWidget;
class BarDescriptorDocumentAbstractNodeHandler;
class BarDescriptorDocument : public Core::TextDocument
{
Q_OBJECT
public:
explicit BarDescriptorDocument(BarDescriptorEditorWidget *editorWidget);
~BarDescriptorDocument();
bool open(QString *errorString, const QString &fileName);
bool save(QString *errorString, const QString &fileName = QString(), bool autoSave = false);
QString fileName() const;
QString defaultPath() const;
QString suggestedFileName() const;
QString mimeType() const;
bool shouldAutoSave() const;
bool isModified() const;
bool isSaveAsAllowed() const;
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
void rename(const QString &newName);
QString xmlSource() const;
bool loadContent(const QString &xmlSource, QString *errorMessage = 0, int *errorLine = 0);
private:
void registerNodeHandler(BarDescriptorDocumentAbstractNodeHandler *nodeHandler);
BarDescriptorDocumentAbstractNodeHandler *nodeHandlerForDomNode(const QDomNode &node);
void removeUnknownNodeHandlers();
QList<BarDescriptorDocumentAbstractNodeHandler *> m_nodeHandlers;
QString m_fileName;
BarDescriptorEditorWidget *m_editorWidget;
};
} // namespace Internal
} // namespace Qnx
#endif // QNX_INTERNAL_BARDESCRIPTORDOCUMENT_H
This diff is collapsed.
/**************************************************************************
**
** 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.
**
****************************************************************************/
#ifndef QNX_INTERNAL_BARDESCRIPTORDOCUMENTNODEHANDLERS_H
#define QNX_INTERNAL_BARDESCRIPTORDOCUMENTNODEHANDLERS_H
#include <QDomNode>
#include <QSharedPointer>
namespace Qnx {
namespace Internal {
class BarDescriptorEditorWidget;
class BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentAbstractNodeHandler(BarDescriptorEditorWidget *editorWidget);
virtual ~BarDescriptorDocumentAbstractNodeHandler();
virtual bool canHandle(const QDomNode &node) const = 0;
bool handle(const QDomNode &node);
virtual QDomNode toNode(QDomDocument &doc) const = 0;
void clear();
int order() const;
protected:
BarDescriptorEditorWidget *editorWidget() const;
virtual bool fromNode(const QDomNode &node) = 0;
bool canHandleSimpleTextElement(const QDomNode &node, const QString &tagName) const;
QString loadSimpleTextElement(const QDomNode &node);
QDomElement createSimpleTextElement(QDomDocument &doc, const QString &tagName, const QString &textValue) const;
private:
BarDescriptorEditorWidget *m_editorWidget;
int m_order;
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentIdNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentIdNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentVersionNumberNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentVersionNumberNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentBuildIdNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentBuildIdNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentApplicationNameNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentApplicationNameNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentApplicationDescriptionNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentApplicationDescriptionNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentApplicationIconNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentApplicationIconNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentSplashScreenNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentSplashScreenNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentAssetNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentAssetNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentInitialWindowNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentInitialWindowNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentActionNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentActionNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentArgNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentArgNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentEnvNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentEnvNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentAuthorNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentAuthorNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentAuthorIdNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{
public:
BarDescriptorDocumentAuthorIdNodeHandler(BarDescriptorEditorWidget *editorWidget);
bool canHandle(const QDomNode &node) const;
QDomNode toNode(QDomDocument &doc) const;
protected:
bool fromNode(const QDomNode &node);
};
// ----------------------------------------------------------------------------
class BarDescriptorDocumentUnknownNodeHandler : public BarDescriptorDocumentAbstractNodeHandler
{