Isis 3 Programmer Reference
TemplateList.cpp
1 
7 /* SPDX-License-Identifier: CC0-1.0 */
8 
9 #include "TemplateList.h"
10 #include <QDir>
11 
12 #include "FileName.h"
13 #include "IException.h"
14 #include "Project.h"
15 #include "XmlStackedHandlerReader.h"
16 
17 namespace Isis {
26  TemplateList::TemplateList(QString name, QString type, QString path, QObject *parent) : QObject(parent){
27  m_name = name;
28  m_path = path;
29  m_type = type;
30  }
31 
32 
39  }
40 
41 
48  QList<Template *>(other) {
49  m_path = other.m_path;
50  m_name = other.m_name;
51  m_type = other.m_type;
52  }
53 
54 
63  QObject(parent) {
64  xmlReader->pushContentHandler(new XmlHandler(this, project));
65  }
66 
67 
72  // At the moment, this only occurs during an ImportTemplateWorkOrder undo, where it is handled
73  }
74 
75 
81  QString TemplateList::name() const{
82  return m_name;
83  }
84 
85 
91  QString TemplateList::type() const{
92  return m_type;
93  }
94 
95 
101  QString TemplateList::path() const{
102  return m_path;
103  }
104 
105 
111  void TemplateList::setName(QString newName) {
112  m_name = newName;
113  }
114 
115 
121  void TemplateList::setType(QString newType) {
122  m_type = newType;
123  }
124 
125 
131  void TemplateList::setPath(QString newPath) {
132  m_path = newPath;
133  }
134 
135 
144  foreach (Template *currentTemplate, *this) {
145  currentTemplate->deleteFromDisk();
146  }
147 
148  if (!m_path.isEmpty()) {
149  QFile::remove(project->templateRoot() + "/" + m_path + "/templates.xml");
150 
151  QDir dir;
152  dir.rmdir(project->templateRoot() + "/" + m_path);
153  }
154  }
155 
156 
180  void TemplateList::save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot)
181  const {
182 
183  if (m_type == "maps") {
184  stream.writeStartElement("mapTemplateList");
185  }
186  else if (m_type == "registrations") {
187  stream.writeStartElement("regTemplateList");
188  }
189  else {
191  QString("Attempting to save unsupported template file type: [%1]").arg(m_type),
192  _FILEINFO_);
193  }
194  stream.writeAttribute("name", m_name);
195  stream.writeAttribute("type", m_type);
196  stream.writeAttribute("path", m_path);
197 
198  FileName settingsFileName(Project::templateRoot(newProjectRoot.toString())
199  + "/" + m_type + "/" + m_name + "/templates.xml");
200 
201  if (!settingsFileName.dir().mkpath(settingsFileName.path())) {
203  QString("Failed to create directory [%1]").arg(settingsFileName.path()),
204  _FILEINFO_);
205  }
206 
207  QFile templateListContentsFile(settingsFileName.toString());
208 
209  if (!templateListContentsFile.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
211  QString("Unable to save template information for [%1] because [%2] could not be opened "
212  "for writing")
213  .arg(m_name).arg(settingsFileName.original()),
214  _FILEINFO_);
215  }
216 
217  QXmlStreamWriter templateDetailsWriter(&templateListContentsFile);
218  templateDetailsWriter.setAutoFormatting(true);
219  templateDetailsWriter.writeStartDocument();
220  templateDetailsWriter.writeStartElement("templates");
221 
222  foreach (Template *currentTemplate, *this) {
223  currentTemplate->save(templateDetailsWriter, project, newProjectRoot);
224 
225  QString newPath = newProjectRoot.toString() + "/templates/" + m_type + "/" + m_name;
226 
227  if (currentTemplate->fileName() !=
228  newPath + "/" + FileName(currentTemplate->fileName()).name()) {
229  QFile::copy(currentTemplate->fileName(),
230  newPath + "/" + FileName(currentTemplate->fileName()).name() );
231  }
232  }
233 
234  templateDetailsWriter.writeEndElement();
235  templateDetailsWriter.writeEndDocument();
236 
237  stream.writeEndElement();
238  }
239 
240 
249  m_templateList = templateList;
250  m_project = project;
251  }
252 
253 
265  bool TemplateList::XmlHandler::startElement(const QString &namespaceURI, const QString &localName,
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");
272 
273  if (!name.isEmpty()) {
274  m_templateList->setName(name);
275  }
276 
277  if (!type.isEmpty()) {
278  m_templateList->setType(type);
279  }
280 
281  if (!path.isEmpty()) {
282  m_templateList->setPath(path);
283  }
284  }
285  else if (localName == "template") {
286  m_templateList->append(new Template(m_project->templateRoot(), reader()));
287  }
288  }
289 
290  return true;
291  }
292 
293 
307  bool TemplateList::XmlHandler::endElement(const QString &namespaceURI, const QString &localName,
308  const QString &qName) {
309  if (localName == "mapTemplateList" || localName == "regTemplateList") {
310  XmlHandler handler(m_templateList, m_project);
311 
313  reader.pushContentHandler(&handler);
314  reader.setErrorHandler(&handler);
315 
316  QString templateListXmlPath = m_project->templateRoot() + "/" + m_templateList->type() + "/"
317  + m_templateList->name() + "/templates.xml";
318  templateListXmlPath = QDir::cleanPath(templateListXmlPath);
319 
320  QFile file(templateListXmlPath);
321 
322  if (!file.open(QFile::ReadOnly)) {
324  QString("Unable to open [%1] with read access")
325  .arg(templateListXmlPath),
326  _FILEINFO_);
327  }
328 
329  QXmlInputSource xmlInputSource(&file);
330  if (!reader.parse(xmlInputSource))
332  tr("Failed to open TemplateList XML [%1]").arg(templateListXmlPath),
333  _FILEINFO_);
334  }
335 
336  return XmlStackedHandler::endElement(namespaceURI, localName, qName);
337  }
338 }
Isis::TemplateList::save
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Convert this TemplateList into XML format for saving/restoring capabilities.
Definition: TemplateList.cpp:180
Isis::Project::templateRoot
static QString templateRoot(QString projectRoot)
Appends the root directory name 'templates' to the project .
Definition: Project.cpp:2125
Isis::TemplateList::XmlHandler
Definition: TemplateList.h:70
Isis::IException::Io
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:155
QList
This is free and unencumbered software released into the public domain.
Definition: BoxcarCachingAlgorithm.h:13
Project.h
Isis::TemplateList::XmlHandler::startElement
virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
Handle an XML start element.
Definition: TemplateList.cpp:265
Isis::FileName::name
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:162
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::XmlStackedHandlerReader::pushContentHandler
virtual void pushContentHandler(XmlStackedHandler *newHandler)
Push a contentHandler and maybe continue parsing...
Definition: XmlStackedHandlerReader.cpp:55
Isis::TemplateList::XmlHandler::endElement
virtual bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName)
Handle an XML end element.
Definition: TemplateList.cpp:307
Isis::TemplateList
Definition: TemplateList.h:35
Isis::TemplateList::path
QString path() const
Get the path to these Templates in the TemplateList (relative to project root).
Definition: TemplateList.cpp:101
Isis::Template::deleteFromDisk
void deleteFromDisk()
Delete the template from disk.
Definition: Template.cpp:88
Isis::XmlStackedHandlerReader
Manage a stack of content handlers for reading XML files.
Definition: XmlStackedHandlerReader.h:30
Isis::Project
The main project for ipce.
Definition: Project.h:289
Isis::Template::save
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Method to write this Template object's member data to an XML stream.
Definition: Template.cpp:105
Isis::FileName::dir
QDir dir() const
Returns the path of the file's parent directory as a QDir object.
Definition: FileName.cpp:465
Isis::Template::fileName
QString fileName() const
Get the file name that this Template represents.
Definition: Template.cpp:50
Isis::TemplateList::deleteFromDisk
void deleteFromDisk(Project *project)
Delete all of the contained Templates from disk.
Definition: TemplateList.cpp:143
Isis::TemplateList::~TemplateList
~TemplateList()
Destructor.
Definition: TemplateList.cpp:71
Isis::Template
Definition: Template.h:30
Isis::TemplateList::TemplateList
TemplateList(QString name, QString type, QString path, QObject *parent=NULL)
Create a template from a file name, type, and path.
Definition: TemplateList.cpp:26
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::FileName::toString
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
Definition: FileName.cpp:515
Isis::TemplateList::XmlHandler::m_templateList
TemplateList * m_templateList
TemplateList to be read or written.
Definition: TemplateList.h:83
Isis::TemplateList::name
QString name() const
Get the human-readable name of this TemplateList.
Definition: TemplateList.cpp:81
Isis::Project::templateRoot
QString templateRoot() const
Accessor for the root directory of the template data.
Definition: Project.cpp:2135
Isis::TemplateList::type
QString type() const
Get the type of template in this TemplateList.
Definition: TemplateList.cpp:91
Isis::TemplateList::XmlHandler::XmlHandler
XmlHandler(TemplateList *templateList, Project *project)
Create an XML Handler (reader/writer) that can populate the TemplateList class data.
Definition: TemplateList.cpp:248
Isis::TemplateList::setType
void setType(QString newType)
Set the type of template for of this TemplateList.
Definition: TemplateList.cpp:121
Isis::TemplateList::setPath
void setPath(QString newPath)
Set the relative path (from the project root) to this TemplateList's folder.
Definition: TemplateList.cpp:131
QObject
Isis::FileName::original
QString original() const
Returns the full file name including the file path.
Definition: FileName.cpp:212
Isis::TemplateList::XmlHandler::m_project
Project * m_project
Project that contains the template list.
Definition: TemplateList.h:84
Isis::TemplateList::setName
void setName(QString newName)
Set the human-readable name of this TemplateList.
Definition: TemplateList.cpp:111
Isis::FileName::path
QString path() const
Returns the path of the file name.
Definition: FileName.cpp:103
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16