diff --git a/sources/editor/qetelementeditor.cpp b/sources/editor/qetelementeditor.cpp index d5ef05c42..d936573cf 100644 --- a/sources/editor/qetelementeditor.cpp +++ b/sources/editor/qetelementeditor.cpp @@ -763,16 +763,16 @@ void QETElementEditor::fromFile(const QString &filepath) { @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); } /** diff --git a/sources/fileelementdefinition.cpp b/sources/fileelementdefinition.cpp index 3decbfe7c..7667bcd15 100644 --- a/sources/fileelementdefinition.cpp +++ b/sources/fileelementdefinition.cpp @@ -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 @@ bool FileElementDefinition::setXml(const QDomElement &xml_element) { @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)); } /** diff --git a/sources/fileelementscategory.cpp b/sources/fileelementscategory.cpp index b96e99096..5c146d19b 100644 --- a/sources/fileelementscategory.cpp +++ b/sources/fileelementscategory.cpp @@ -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 @@ bool FileElementsCategory::write() { 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)); } /** diff --git a/sources/qet.cpp b/sources/qet.cpp index 134b2bc33..478945110 100644 --- a/sources/qet.cpp +++ b/sources/qet.cpp @@ -528,3 +528,38 @@ QString QET::titleBlockColumnLengthToString(const TitleBlockColumnLength &icl) 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); +} diff --git a/sources/qet.h b/sources/qet.h index 19d8ef376..fa855e2b3 100644 --- a/sources/qet.h +++ b/sources/qet.h @@ -148,5 +148,6 @@ namespace QET { qreal correctAngle(const qreal &); bool compareCanonicalFilePaths(const QString &, const QString &); QString titleBlockColumnLengthToString(const TitleBlockColumnLength &); + bool writeXmlFile(QDomDocument &, const QString &, QString * = 0); } #endif diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 95e80ff75..37ec97bcb 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -464,26 +464,16 @@ bool QETProject::write() { 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); } /** diff --git a/sources/titleblock/templatescollection.cpp b/sources/titleblock/templatescollection.cpp index da3d510fb..62f869e3b 100644 --- a/sources/titleblock/templatescollection.cpp +++ b/sources/titleblock/templatescollection.cpp @@ -417,17 +417,11 @@ bool TitleBlockTemplatesFilesCollection::setTemplateXmlDescription(const QString // 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); diff --git a/sources/titleblocktemplate.cpp b/sources/titleblocktemplate.cpp index b5ec6c15b..250ded6d0 100644 --- a/sources/titleblocktemplate.cpp +++ b/sources/titleblocktemplate.cpp @@ -126,12 +126,6 @@ bool TitleBlockTemplate::loadFromXmlElement(const QDomElement &xml_element) { 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 @@ bool TitleBlockTemplate::saveToXmlFile(const QString &filepath) { 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)); } /**