9#include "TemplateList.h"
13#include "IException.h"
15#include "XmlStackedHandlerReader.h"
49 m_path = other.m_path;
50 m_name = other.m_name;
51 m_type = other.m_type;
64 xmlReader->pushContentHandler(
new XmlHandler(
this, project));
144 foreach (
Template *currentTemplate, *
this) {
145 currentTemplate->deleteFromDisk();
148 if (!m_path.isEmpty()) {
149 QFile::remove(project->
templateRoot() +
"/" + m_path +
"/templates.xml");
183 if (m_type ==
"maps") {
184 stream.writeStartElement(
"mapTemplateList");
186 else if (m_type ==
"registrations") {
187 stream.writeStartElement(
"regTemplateList");
191 QString(
"Attempting to save unsupported template file type: [%1]").arg(m_type),
194 stream.writeAttribute(
"name", m_name);
195 stream.writeAttribute(
"type", m_type);
196 stream.writeAttribute(
"path", m_path);
199 +
"/" + m_type +
"/" + m_name +
"/templates.xml");
201 if (!settingsFileName.
dir().mkpath(settingsFileName.
path())) {
203 QString(
"Failed to create directory [%1]").arg(settingsFileName.
path()),
207 QFile templateListContentsFile(settingsFileName.
toString());
209 if (!templateListContentsFile.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
211 QString(
"Unable to save template information for [%1] because [%2] could not be opened "
213 .arg(m_name).arg(settingsFileName.
original()),
217 QXmlStreamWriter templateDetailsWriter(&templateListContentsFile);
218 templateDetailsWriter.setAutoFormatting(
true);
219 templateDetailsWriter.writeStartDocument();
220 templateDetailsWriter.writeStartElement(
"templates");
222 foreach (
Template *currentTemplate, *
this) {
223 currentTemplate->save(templateDetailsWriter, project, newProjectRoot);
225 QString newPath = newProjectRoot.
toString() +
"/templates/" + m_type +
"/" + m_name;
227 if (currentTemplate->fileName() !=
228 newPath +
"/" +
FileName(currentTemplate->fileName()).
name()) {
229 QFile::copy(currentTemplate->fileName(),
230 newPath +
"/" +
FileName(currentTemplate->fileName()).
name() );
234 templateDetailsWriter.writeEndElement();
235 templateDetailsWriter.writeEndDocument();
237 stream.writeEndElement();
266 const QString &qName,
const QXmlAttributes &atts) {
267 if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
268 if (localName ==
"mapTemplateList" || localName ==
"regTemplateList") {
269 QString
name = atts.value(
"name");
270 QString
type = atts.value(
"type");
271 QString
path = atts.value(
"path");
273 if (!
name.isEmpty()) {
274 m_templateList->setName(
name);
277 if (!
type.isEmpty()) {
278 m_templateList->setType(
type);
281 if (!
path.isEmpty()) {
282 m_templateList->setPath(
path);
285 else if (localName ==
"template") {
286 m_templateList->append(
new Template(m_project->templateRoot(), reader()));
308 const QString &qName) {
309 if (localName ==
"mapTemplateList" || localName ==
"regTemplateList") {
310 XmlHandler handler(m_templateList, m_project);
314 reader.setErrorHandler(&handler);
316 QString templateListXmlPath = m_project->templateRoot() +
"/" + m_templateList->type() +
"/"
317 + m_templateList->name() +
"/templates.xml";
318 templateListXmlPath = QDir::cleanPath(templateListXmlPath);
320 QFile file(templateListXmlPath);
322 if (!file.open(QFile::ReadOnly)) {
324 QString(
"Unable to open [%1] with read access")
325 .arg(templateListXmlPath),
329 QXmlInputSource xmlInputSource(&file);
330 if (!reader.parse(xmlInputSource))
332 tr(
"Failed to open TemplateList XML [%1]").arg(templateListXmlPath),
336 return XmlStackedHandler::endElement(namespaceURI, localName, qName);
File name manipulation and expansion.
QString path() const
Returns the path of the file name.
QDir dir() const
Returns the path of the file's parent directory as a QDir object.
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
QString original() const
Returns the full file name including the file path.
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
@ Io
A type of error that occurred when performing an actual I/O operation.
The main project for ipce.
static QString templateRoot(QString projectRoot)
Appends the root directory name 'templates' to the project .
QString templateRoot() const
Accessor for the root directory of the template data.
TemplateList * m_templateList
TemplateList to be read or written.
Project * m_project
Project that contains the template list.
virtual bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName)
Handle an XML end element.
virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
Handle an XML start element.
XmlHandler(TemplateList *templateList, Project *project)
Create an XML Handler (reader/writer) that can populate the TemplateList class data.
void setPath(QString newPath)
Set the relative path (from the project root) to this TemplateList's folder.
TemplateList(QString name, QString type, QString path, QObject *parent=NULL)
Create a template from a file name, type, and path.
QString name() const
Get the human-readable name of this TemplateList.
~TemplateList()
Destructor.
void deleteFromDisk(Project *project)
Delete all of the contained Templates from disk.
QString path() const
Get the path to these Templates in the TemplateList (relative to project root).
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Convert this TemplateList into XML format for saving/restoring capabilities.
void setName(QString newName)
Set the human-readable name of this TemplateList.
QString type() const
Get the type of template in this TemplateList.
void setType(QString newType)
Set the type of template for of this TemplateList.
Manage a stack of content handlers for reading XML files.
virtual void pushContentHandler(XmlStackedHandler *newHandler)
Push a contentHandler and maybe continue parsing...
This is free and unencumbered software released into the public domain.
This is free and unencumbered software released into the public domain.