Isis 3 Programmer Reference
TemplateList.cpp
Go to the documentation of this file.
1 
24 #include "TemplateList.h"
25 #include <QDir>
26 
27 #include "FileName.h"
28 #include "IException.h"
29 #include "Project.h"
30 #include "XmlStackedHandlerReader.h"
31 
32 namespace Isis {
41  TemplateList::TemplateList(QString name, QString type, QString path, QObject *parent) : QObject(parent){
42  m_name = name;
43  m_path = path;
44  m_type = type;
45  }
46 
47 
54  }
55 
56 
63  QList<Template *>(other) {
64  m_path = other.m_path;
65  m_name = other.m_name;
66  m_type = other.m_type;
67  }
68 
69 
78  QObject(parent) {
79  xmlReader->pushContentHandler(new XmlHandler(this, project));
80  }
81 
82 
87  // At the moment, this only occurs during an ImportTemplateWorkOrder undo, where it is handled
88  }
89 
90 
96  QString TemplateList::name() const{
97  return m_name;
98  }
99 
100 
106  QString TemplateList::type() const{
107  return m_type;
108  }
109 
110 
116  QString TemplateList::path() const{
117  return m_path;
118  }
119 
120 
126  void TemplateList::setName(QString newName) {
127  m_name = newName;
128  }
129 
130 
136  void TemplateList::setType(QString newType) {
137  m_type = newType;
138  }
139 
140 
146  void TemplateList::setPath(QString newPath) {
147  m_path = newPath;
148  }
149 
150 
159  foreach (Template *currentTemplate, *this) {
160  currentTemplate->deleteFromDisk();
161  }
162 
163  if (!m_path.isEmpty()) {
164  QFile::remove(project->templateRoot() + "/" + m_path + "/templates.xml");
165 
166  QDir dir;
167  dir.rmdir(project->templateRoot() + "/" + m_path);
168  }
169  }
170 
171 
195  void TemplateList::save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot)
196  const {
197 
198  if (m_type == "maps") {
199  stream.writeStartElement("mapTemplateList");
200  }
201  else if (m_type == "registrations") {
202  stream.writeStartElement("regTemplateList");
203  }
204  else {
206  QString("Attempting to save unsupported template file type: [%1]").arg(m_type),
207  _FILEINFO_);
208  }
209  stream.writeAttribute("name", m_name);
210  stream.writeAttribute("type", m_type);
211  stream.writeAttribute("path", m_path);
212 
213  FileName settingsFileName(Project::templateRoot(newProjectRoot.toString())
214  + "/" + m_type + "/" + m_name + "/templates.xml");
215 
216  if (!settingsFileName.dir().mkpath(settingsFileName.path())) {
218  QString("Failed to create directory [%1]").arg(settingsFileName.path()),
219  _FILEINFO_);
220  }
221 
222  QFile templateListContentsFile(settingsFileName.toString());
223 
224  if (!templateListContentsFile.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
226  QString("Unable to save template information for [%1] because [%2] could not be opened "
227  "for writing")
228  .arg(m_name).arg(settingsFileName.original()),
229  _FILEINFO_);
230  }
231 
232  QXmlStreamWriter templateDetailsWriter(&templateListContentsFile);
233  templateDetailsWriter.setAutoFormatting(true);
234  templateDetailsWriter.writeStartDocument();
235  templateDetailsWriter.writeStartElement("templates");
236 
237  foreach (Template *currentTemplate, *this) {
238  currentTemplate->save(templateDetailsWriter, project, newProjectRoot);
239 
240  QString newPath = newProjectRoot.toString() + "/templates/" + m_type + "/" + m_name;
241 
242  if (currentTemplate->fileName() !=
243  newPath + "/" + FileName(currentTemplate->fileName()).name()) {
244  QFile::copy(currentTemplate->fileName(),
245  newPath + "/" + FileName(currentTemplate->fileName()).name() );
246  }
247  }
248 
249  templateDetailsWriter.writeEndElement();
250  templateDetailsWriter.writeEndDocument();
251 
252  stream.writeEndElement();
253  }
254 
255 
264  m_templateList = templateList;
265  m_project = project;
266  }
267 
268 
280  bool TemplateList::XmlHandler::startElement(const QString &namespaceURI, const QString &localName,
281  const QString &qName, const QXmlAttributes &atts) {
282  if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
283  if (localName == "mapTemplateList" || localName == "regTemplateList") {
284  QString name = atts.value("name");
285  QString type = atts.value("type");
286  QString path = atts.value("path");
287 
288  if (!name.isEmpty()) {
289  m_templateList->setName(name);
290  }
291 
292  if (!type.isEmpty()) {
293  m_templateList->setType(type);
294  }
295 
296  if (!path.isEmpty()) {
297  m_templateList->setPath(path);
298  }
299  }
300  else if (localName == "template") {
301  m_templateList->append(new Template(m_project->templateRoot(), reader()));
302  }
303  }
304 
305  return true;
306  }
307 
308 
322  bool TemplateList::XmlHandler::endElement(const QString &namespaceURI, const QString &localName,
323  const QString &qName) {
324  if (localName == "mapTemplateList" || localName == "regTemplateList") {
325  XmlHandler handler(m_templateList, m_project);
326 
328  reader.pushContentHandler(&handler);
329  reader.setErrorHandler(&handler);
330 
331  QString templateListXmlPath = m_project->templateRoot() + "/" + m_templateList->type() + "/"
332  + m_templateList->name() + "/templates.xml";
333  templateListXmlPath = QDir::cleanPath(templateListXmlPath);
334 
335  QFile file(templateListXmlPath);
336 
337  if (!file.open(QFile::ReadOnly)) {
339  QString("Unable to open [%1] with read access")
340  .arg(templateListXmlPath),
341  _FILEINFO_);
342  }
343 
344  QXmlInputSource xmlInputSource(&file);
345  if (!reader.parse(xmlInputSource))
347  tr("Failed to open TemplateList XML [%1]").arg(templateListXmlPath),
348  _FILEINFO_);
349  }
350 
351  return XmlStackedHandler::endElement(namespaceURI, localName, qName);
352  }
353 }
static QString templateRoot(QString projectRoot)
Appends the root directory name &#39;templates&#39; to the project .
Definition: Project.cpp:2125
void deleteFromDisk()
Delete the template from disk.
Definition: Template.cpp:88
QString path() const
Returns the path of the file name.
Definition: FileName.cpp:119
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Convert this TemplateList into XML format for saving/restoring capabilities.
The main project for ipce.
Definition: Project.h:289
File name manipulation and expansion.
Definition: FileName.h:116
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Method to write this Template object&#39;s member data to an XML stream.
Definition: Template.cpp:105
QString fileName() const
Get the file name that this Template represents.
Definition: Template.cpp:50
QString path() const
Get the path to these Templates in the TemplateList (relative to project root).
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:171
virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
Handle an XML start element.
virtual bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName)
Handle an XML end element.
void deleteFromDisk(Project *project)
Delete all of the contained Templates from disk.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
virtual void pushContentHandler(XmlStackedHandler *newHandler)
Push a contentHandler and maybe continue parsing...
XmlHandler(TemplateList *templateList, Project *project)
Create an XML Handler (reader/writer) that can populate the TemplateList class data.
~TemplateList()
Destructor.
QString original() const
Returns the full file name including the file path.
Definition: FileName.cpp:228
QDir dir() const
Returns the path of the file&#39;s parent directory as a QDir object.
Definition: FileName.cpp:481
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.
Project * m_project
Project that contains the template list.
Definition: TemplateList.h:98
QString templateRoot() const
Accessor for the root directory of the template data.
Definition: Project.cpp:2135
TemplateList * m_templateList
TemplateList to be read or written.
Definition: TemplateList.h:97
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
Definition: FileName.cpp:531
QString type() const
Get the type of template in this TemplateList.
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void setType(QString newType)
Set the type of template for of this TemplateList.
void setPath(QString newPath)
Set the relative path (from the project root) to this TemplateList&#39;s folder.
void setName(QString newName)
Set the human-readable name of this TemplateList.
Manage a stack of content handlers for reading XML files.