[qet] [1640] Implemented QET::writeXmlFile() to handle every XML file generation in a single place.

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 1640
Author:   xavier
Date:     2012-04-09 03:03:08 +0200 (Mon, 09 Apr 2012)
Log Message:
-----------
Implemented QET::writeXmlFile() to handle every XML file generation in a single place.

Modified Paths:
--------------
    branches/0.3/sources/editor/qetelementeditor.cpp
    branches/0.3/sources/fileelementdefinition.cpp
    branches/0.3/sources/fileelementscategory.cpp
    branches/0.3/sources/qet.cpp
    branches/0.3/sources/qet.h
    branches/0.3/sources/qetproject.cpp
    branches/0.3/sources/titleblock/templatescollection.cpp
    branches/0.3/sources/titleblocktemplate.cpp

Modified: branches/0.3/sources/editor/qetelementeditor.cpp
===================================================================
--- branches/0.3/sources/editor/qetelementeditor.cpp	2012-04-08 20:51:48 UTC (rev 1639)
+++ branches/0.3/sources/editor/qetelementeditor.cpp	2012-04-09 01:03:08 UTC (rev 1640)
@@ -763,16 +763,16 @@
 	@return true en cas de reussite, false sinon
 */
 bool QETElementEditor::toFile(const QString &fn) {
-	QFile file(fn);
-	if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
-		QET::MessageBox::warning(this, tr("Erreur", "message box title"), tr("Impossible d'\351crire dans ce fichier", "message box content"));
-		return(false);
+	QDomDocument element_xml = ce_scene -> toXml();
+	bool writing = QET::writeXmlFile(element_xml, fn);
+	if (!writing) {
+		QET::MessageBox::warning(
+			this,
+			tr("Erreur", "message box title"),
+			tr("Impossible d'\351crire dans ce fichier", "message box content")
+		);
 	}
-	QTextStream out(&file);
-	out.setCodec("UTF-8");
-	out << ce_scene -> toXml().toString(4);
-	file.close();
-	return(true);
+	return(writing);
 }
 
 /**

Modified: branches/0.3/sources/fileelementdefinition.cpp
===================================================================
--- branches/0.3/sources/fileelementdefinition.cpp	2012-04-08 20:51:48 UTC (rev 1639)
+++ branches/0.3/sources/fileelementdefinition.cpp	2012-04-09 01:03:08 UTC (rev 1640)
@@ -20,6 +20,8 @@
 #include "fileelementscategory.h"
 #include "fileelementscollection.h"
 #include "qetapp.h"
+#include "qet.h"
+
 /**
 	Constructeur
 	@param uri Chemin du fichier contenant la definition de l'element
@@ -74,16 +76,7 @@
 	@return true si l'operation a reussi, false sinon
 */
 bool FileElementDefinition::write() {
-	QFile file(file_path);
-	
-	// le fichier doit etre accessible en ecriture
-	if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return(false);
-	
-	QTextStream out(&file);
-	out.setCodec("UTF-8");
-	out << xml_element_.toString(4);
-	file.close();
-	return(true);
+	return(QET::writeXmlFile(xml_element_, file_path));
 }
 
 /**

Modified: branches/0.3/sources/fileelementscategory.cpp
===================================================================
--- branches/0.3/sources/fileelementscategory.cpp	2012-04-08 20:51:48 UTC (rev 1639)
+++ branches/0.3/sources/fileelementscategory.cpp	2012-04-09 01:03:08 UTC (rev 1640)
@@ -18,6 +18,8 @@
 #include "fileelementscategory.h"
 #include "fileelementscollection.h"
 #include "fileelementdefinition.h"
+#include "qet.h"
+
 /**
 	Constructeur
 	@param path Chemin du dossier de la categorie
@@ -412,21 +414,8 @@
 	document.appendChild(root);
 	root.appendChild(category_names.toXml(document));
 	
-	// repere le chemin du fichier de configuration de la categorie
-	QFile directory_conf(cat_dir.absolutePath() + "/qet_directory");
-	
-	// ouvre le fichier
-	if (!directory_conf.open(QIODevice::Text | QIODevice::WriteOnly)) return(false);
-	
-	// ecrit le fichier
-	QTextStream out(&directory_conf);
-	out.setCodec("UTF-8");
-	out << document.toString(4);
-	
-	// ferme le fichier
-	directory_conf.close();
-	
-	return(true);
+	QString filepath = cat_dir.absolutePath() + "/qet_directory";
+	return(QET::writeXmlFile(document, filepath));
 }
 
 /**

Modified: branches/0.3/sources/qet.cpp
===================================================================
--- branches/0.3/sources/qet.cpp	2012-04-08 20:51:48 UTC (rev 1639)
+++ branches/0.3/sources/qet.cpp	2012-04-09 01:03:08 UTC (rev 1640)
@@ -528,3 +528,38 @@
 	else if (icl == RelativeToRemainingLength) type_str = "relative to remaining";
 	return(type_str);
 }
+
+/**
+	Export an XML document to an UTF-8 text file indented with 4 spaces, with LF
+	end of lines and no BOM.
+	@param xml_doc An XML document to be exported
+	@param filepath Path to the file to be written
+	@param error_message If non-zero, will contain an error message explaining
+	what happened when this function returns false.
+	@return false if an error occured, true otherwise
+*/
+bool QET::writeXmlFile(QDomDocument &xml_doc, const QString &filepath, QString *error_message) {
+	QFile file(filepath);
+	
+	// Note: we do not set QIODevice::Text to avoid generating CRLF end of lines
+	bool file_opening = file.open(QIODevice::WriteOnly);
+	if (!file_opening) {
+		if (error_message) {
+			*error_message = QString(
+				QObject::tr(
+					"Impossible d'ouvrir le fichier %1 en \351criture, erreur %2 rencontr\351e.",
+					"error message when attempting to write an XML file"
+				)
+			).arg(filepath).arg(file.error());
+		}
+		return(false);
+	}
+	
+	QTextStream out(&file);
+	out.setCodec("UTF-8");
+	out.setGenerateByteOrderMark(false);
+	out << xml_doc.toString(4);
+	file.close();
+	
+	return(true);
+}

Modified: branches/0.3/sources/qet.h
===================================================================
--- branches/0.3/sources/qet.h	2012-04-08 20:51:48 UTC (rev 1639)
+++ branches/0.3/sources/qet.h	2012-04-09 01:03:08 UTC (rev 1640)
@@ -148,5 +148,6 @@
 	qreal correctAngle(const qreal &);
 	bool compareCanonicalFilePaths(const QString &, const QString &);
 	QString titleBlockColumnLengthToString(const TitleBlockColumnLength  &);
+	bool writeXmlFile(QDomDocument &, const QString &, QString * = 0);
 }
 #endif

Modified: branches/0.3/sources/qetproject.cpp
===================================================================
--- branches/0.3/sources/qetproject.cpp	2012-04-08 20:51:48 UTC (rev 1639)
+++ branches/0.3/sources/qetproject.cpp	2012-04-09 01:03:08 UTC (rev 1640)
@@ -464,26 +464,16 @@
 		return(true);
 	}
 	
-	// ouvre le fichier en ecriture
-	QFile file(file_path_);
-	bool file_opening = file.open(QIODevice::WriteOnly | QIODevice::Text);
-	if (!file_opening) {
-		qDebug() << qPrintable(QString("QETProject::write() : unable to open %1 with write access [%2]").arg(file_path_).arg(QET::pointerString(this)));
-		return(false);
-	}
-	
-	qDebug() << qPrintable(QString("QETProject::write() : writing to file %1 [%2]").arg(file_path_).arg(QET::pointerString(this)));
-	
 	// realise l'export en XML du projet dans le document XML interne
 	document_root_.clear();
 	document_root_.appendChild(document_root_.importNode(toXml().documentElement(), true));
 	
-	QTextStream out(&file);
-	out.setCodec("UTF-8");
-	out << document_root_.toString(4);
-	file.close();
-	
-	return(true);
+	QString error_message;
+	bool writing = QET::writeXmlFile(document_root_, file_path_, &error_message);
+	if (!writing) {
+		qDebug() << qPrintable(QString("QETProject::write() : %1 [%2]").arg(error_message).arg(QET::pointerString(this)));
+	}
+	return(writing);
 }
 
 /**

Modified: branches/0.3/sources/titleblock/templatescollection.cpp
===================================================================
--- branches/0.3/sources/titleblock/templatescollection.cpp	2012-04-08 20:51:48 UTC (rev 1639)
+++ branches/0.3/sources/titleblock/templatescollection.cpp	2012-04-09 01:03:08 UTC (rev 1640)
@@ -417,17 +417,11 @@
 	// prevent the watcher from emitting signals while we open and write to file
 	blockSignals(true);
 	
-	QFile xml_file(path(template_name));
-	if (!xml_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
-		return(false);
-	}
 	QDomDocument doc;
 	doc.appendChild(doc.importNode(xml_element, true));
 	
-	QTextStream out(&xml_file);
-	out.setCodec("UTF-8");
-	out << doc.toString(4);
-	xml_file.close();
+	bool writing = QET::writeXmlFile(doc, path(template_name));
+	if (!writing) return(false);
 	
 	// emit a single signal for the change
 	blockSignals(false);

Modified: branches/0.3/sources/titleblocktemplate.cpp
===================================================================
--- branches/0.3/sources/titleblocktemplate.cpp	2012-04-08 20:51:48 UTC (rev 1639)
+++ branches/0.3/sources/titleblocktemplate.cpp	2012-04-09 01:03:08 UTC (rev 1640)
@@ -126,12 +126,6 @@
 bool TitleBlockTemplate::saveToXmlFile(const QString &filepath) {
 	if (filepath.isEmpty()) return(false);
 	
-	// open the file
-	QFile xml_file(filepath);
-	if (!xml_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
-		return(false);
-	}
-	
 	// generate the XML document
 	QDomDocument doc;
 	QDomElement e = doc.createElement("root");
@@ -139,13 +133,7 @@
 	if (!saving) return(false);
 	doc.appendChild(e);
 	
-	// write the file
-	QTextStream out(&xml_file);
-	out.setCodec("UTF-8");
-	out << doc.toString(4);
-	xml_file.close();
-	
-	return(true);
+	return(QET::writeXmlFile(doc, filepath));
 }
 
 /**


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/