7#include "exportlibreofficeautocorrection.h"
8#include "autocorrectionutils.h"
9#include "textautocorrection_debug.h"
12#include <QTemporaryFile>
13#include <QXmlStreamWriter>
15using namespace TextAutoCorrectionCore;
17ExportLibreOfficeAutocorrection::ExportLibreOfficeAutocorrection() =
default;
19ExportLibreOfficeAutocorrection::~ExportLibreOfficeAutocorrection()
24bool ExportLibreOfficeAutocorrection::exportData(
const QString &language,
const QString &fileName,
QString &errorMessage,
const QString &writablePath)
26 Q_UNUSED(errorMessage);
27 const QString libreOfficeWritableLocalAutoCorrectionPath =
28 writablePath.
isEmpty() ? AutoCorrectionUtils::libreOfficeWritableLocalAutoCorrectionPath() : writablePath;
29 QDir().
mkpath(libreOfficeWritableLocalAutoCorrectionPath);
30 QString fixLangExtension = language;
32 const QString fname = fileName.
isEmpty() ? libreOfficeWritableLocalAutoCorrectionPath + QStringLiteral(
"acor_%1.dat").
arg(fixLangExtension) : fileName;
34 mZip =
new KZip(fname);
37 qCWarning(TEXTAUTOCORRECTION_LOG) <<
"Impossible to open " << fileName;
40 if (!exportDocumentList()) {
43 if (!exportSentenceExceptList()) {
46 if (!exportWordExceptList()) {
49 if (!exportManifest()) {
58bool ExportLibreOfficeAutocorrection::exportDocumentList()
61 temporaryShareFile.
open();
64 streamWriter.setAutoFormatting(
true);
65 streamWriter.setAutoFormattingIndent(2);
66 streamWriter.writeStartDocument();
68 streamWriter.writeStartElement(QStringLiteral(
"block-list:block-list"));
69 streamWriter.writeAttribute(QStringLiteral(
"xmlns:block-list"), QStringLiteral(
"http://openoffice.org/2001/block-list"));
73 streamWriter.writeStartElement(QStringLiteral(
"block-list:block"));
74 streamWriter.writeAttribute(QStringLiteral(
"block-list:abbreviated-name"), i.key());
75 streamWriter.writeAttribute(QStringLiteral(
"block-list:name"), i.value());
76 streamWriter.writeEndElement();
78 streamWriter.writeEndElement();
79 streamWriter.writeEndDocument();
80 temporaryShareFile.
close();
85bool ExportLibreOfficeAutocorrection::exportSentenceExceptList()
88 temporaryShareFile.
open();
92 streamWriter.setAutoFormatting(
true);
93 streamWriter.setAutoFormattingIndent(2);
94 streamWriter.writeStartDocument();
96 streamWriter.writeStartElement(QStringLiteral(
"block-list:block-list"));
97 streamWriter.writeAttribute(QStringLiteral(
"xmlns:block-list"), QStringLiteral(
"http://openoffice.org/2001/block-list"));
100 while (upper != mUpperCaseExceptions.
constEnd()) {
101 streamWriter.writeStartElement(QStringLiteral(
"block-list:block"));
102 streamWriter.writeAttribute(QStringLiteral(
"block-list:abbreviated-name"), *upper);
103 streamWriter.writeEndElement();
106 streamWriter.writeEndElement();
107 streamWriter.writeEndDocument();
108 temporaryShareFile.
close();
114bool ExportLibreOfficeAutocorrection::exportWordExceptList()
117 temporaryShareFile.
open();
121 streamWriter.setAutoFormatting(
true);
122 streamWriter.setAutoFormattingIndent(2);
123 streamWriter.writeStartDocument();
125 streamWriter.writeStartElement(QStringLiteral(
"block-list:block-list"));
126 streamWriter.writeAttribute(QStringLiteral(
"xmlns:block-list"), QStringLiteral(
"http://openoffice.org/2001/block-list"));
129 while (twoUpper != mTwoUpperLetterExceptions.
constEnd()) {
130 streamWriter.writeStartElement(QStringLiteral(
"block-list:block"));
131 streamWriter.writeAttribute(QStringLiteral(
"block-list:abbreviated-name"), *twoUpper);
132 streamWriter.writeEndElement();
135 streamWriter.writeEndElement();
136 streamWriter.writeEndDocument();
137 temporaryShareFile.
close();
143bool ExportLibreOfficeAutocorrection::exportManifest()
146 temporaryShareFile.
open();
149 streamWriter.setAutoFormatting(
true);
150 streamWriter.setAutoFormattingIndent(2);
151 streamWriter.writeStartDocument();
153 streamWriter.writeStartElement(QStringLiteral(
"manifest:manifest"));
155 streamWriter.writeStartElement(QStringLiteral(
"manifest:file-entry"));
156 streamWriter.writeAttribute(QStringLiteral(
"manifest:full-path"), QStringLiteral(
"/"));
157 streamWriter.writeAttribute(QStringLiteral(
"manifest:media-type"),
QString());
158 streamWriter.writeEndElement();
160 streamWriter.writeStartElement(QStringLiteral(
"manifest:file-entry"));
161 streamWriter.writeAttribute(QStringLiteral(
"manifest:full-path"), QStringLiteral(
"DocumentList.xml"));
162 streamWriter.writeAttribute(QStringLiteral(
"manifest:media-type"),
QString());
163 streamWriter.writeEndElement();
165 streamWriter.writeStartElement(QStringLiteral(
"manifest:file-entry"));
166 streamWriter.writeAttribute(QStringLiteral(
"manifest:full-path"), QStringLiteral(
"SentenceExceptList.xml"));
167 streamWriter.writeAttribute(QStringLiteral(
"manifest:media-type"),
QString());
168 streamWriter.writeEndElement();
170 streamWriter.writeStartElement(QStringLiteral(
"manifest:file-entry"));
171 streamWriter.writeAttribute(QStringLiteral(
"manifest:full-path"), QStringLiteral(
"WordExceptList.xml"));
172 streamWriter.writeAttribute(QStringLiteral(
"manifest:media-type"), QStringLiteral(
"text/xml"));
173 streamWriter.writeEndElement();
175 streamWriter.writeEndElement();
176 streamWriter.writeEndDocument();
177 temporaryShareFile.
close();
181 mZip->
writeFile(QStringLiteral(
"mimetype"),
"");
bool addLocalFile(const QString &fileName, const QString &destName)
virtual bool open(QIODevice::OpenMode mode)
bool writeFile(const QString &name, QByteArrayView data, mode_t perm=0100644, const QString &user=QString(), const QString &group=QString(), const QDateTime &atime=QDateTime(), const QDateTime &mtime=QDateTime(), const QDateTime &ctime=QDateTime())
void setCompression(Compression c)
bool mkpath(const QString &dirPath) const const
virtual void close() override
const_iterator constBegin() const const
const_iterator constEnd() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
virtual QString fileName() const const override