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

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/branches/0.3@1640 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
xavier
2012-04-09 01:03:08 +00:00
parent 6ab83f3517
commit 318df55a37
8 changed files with 61 additions and 71 deletions

View File

@@ -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);
}