KTextAddons

exportlibreofficeautocorrection.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "exportlibreofficeautocorrection.h"
8#include "autocorrectionutils.h"
9#include "textautocorrection_debug.h"
10#include <KZip>
11#include <QDir>
12#include <QTemporaryFile>
13#include <QXmlStreamWriter>
14
15using namespace TextAutoCorrectionCore;
16
17ExportLibreOfficeAutocorrection::ExportLibreOfficeAutocorrection() = default;
18
19ExportLibreOfficeAutocorrection::~ExportLibreOfficeAutocorrection()
20{
21 delete mZip;
22}
23
24bool ExportLibreOfficeAutocorrection::exportData(const QString &language, const QString &fileName, QString &errorMessage, const QString &writablePath)
25{
26 Q_UNUSED(errorMessage);
27 const QString libreOfficeWritableLocalAutoCorrectionPath =
28 writablePath.isEmpty() ? AutoCorrectionUtils::libreOfficeWritableLocalAutoCorrectionPath() : writablePath;
29 QDir().mkpath(libreOfficeWritableLocalAutoCorrectionPath);
30 QString fixLangExtension = language;
31 fixLangExtension.replace(QLatin1Char('_'), QLatin1Char('-'));
32 const QString fname = fileName.isEmpty() ? libreOfficeWritableLocalAutoCorrectionPath + QStringLiteral("acor_%1.dat").arg(fixLangExtension) : fileName;
33 // qDebug() << " fname " << fname;
34 mZip = new KZip(fname);
35 const bool result = mZip->open(QIODevice::WriteOnly);
36 if (!result) {
37 qCWarning(TEXTAUTOCORRECTION_LOG) << "Impossible to open " << fileName;
38 return false;
39 }
40 if (!exportDocumentList()) {
41 return false;
42 }
43 if (!exportSentenceExceptList()) {
44 return false;
45 }
46 if (!exportWordExceptList()) {
47 return false;
48 }
49 if (!exportManifest()) {
50 return false;
51 }
52 mZip->close();
53 delete mZip;
54 mZip = nullptr;
55 return true;
56}
57
58bool ExportLibreOfficeAutocorrection::exportDocumentList()
59{
60 QTemporaryFile temporaryShareFile;
61 temporaryShareFile.open();
62 QXmlStreamWriter streamWriter(&temporaryShareFile);
63
64 streamWriter.setAutoFormatting(true);
65 streamWriter.setAutoFormattingIndent(2);
66 streamWriter.writeStartDocument();
67
68 streamWriter.writeStartElement(QStringLiteral("block-list:block-list"));
69 streamWriter.writeAttribute(QStringLiteral("xmlns:block-list"), QStringLiteral("http://openoffice.org/2001/block-list"));
70 QHashIterator<QString, QString> i(mAutocorrectEntries);
71 while (i.hasNext()) {
72 i.next();
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();
77 }
78 streamWriter.writeEndElement();
79 streamWriter.writeEndDocument();
80 temporaryShareFile.close();
81 mZip->addLocalFile(temporaryShareFile.fileName(), QStringLiteral("DocumentList.xml"));
82 return true;
83}
84
85bool ExportLibreOfficeAutocorrection::exportSentenceExceptList()
86{
87 QTemporaryFile temporaryShareFile;
88 temporaryShareFile.open();
89
90 QXmlStreamWriter streamWriter(&temporaryShareFile);
91
92 streamWriter.setAutoFormatting(true);
93 streamWriter.setAutoFormattingIndent(2);
94 streamWriter.writeStartDocument();
95
96 streamWriter.writeStartElement(QStringLiteral("block-list:block-list"));
97 streamWriter.writeAttribute(QStringLiteral("xmlns:block-list"), QStringLiteral("http://openoffice.org/2001/block-list"));
98
99 QSet<QString>::const_iterator upper = mUpperCaseExceptions.constBegin();
100 while (upper != mUpperCaseExceptions.constEnd()) {
101 streamWriter.writeStartElement(QStringLiteral("block-list:block"));
102 streamWriter.writeAttribute(QStringLiteral("block-list:abbreviated-name"), *upper);
103 streamWriter.writeEndElement();
104 ++upper;
105 }
106 streamWriter.writeEndElement();
107 streamWriter.writeEndDocument();
108 temporaryShareFile.close();
109
110 mZip->addLocalFile(temporaryShareFile.fileName(), QStringLiteral("SentenceExceptList.xml"));
111 return true;
112}
113
114bool ExportLibreOfficeAutocorrection::exportWordExceptList()
115{
116 QTemporaryFile temporaryShareFile;
117 temporaryShareFile.open();
118
119 QXmlStreamWriter streamWriter(&temporaryShareFile);
120
121 streamWriter.setAutoFormatting(true);
122 streamWriter.setAutoFormattingIndent(2);
123 streamWriter.writeStartDocument();
124
125 streamWriter.writeStartElement(QStringLiteral("block-list:block-list"));
126 streamWriter.writeAttribute(QStringLiteral("xmlns:block-list"), QStringLiteral("http://openoffice.org/2001/block-list"));
127
128 QSet<QString>::const_iterator twoUpper = mTwoUpperLetterExceptions.constBegin();
129 while (twoUpper != mTwoUpperLetterExceptions.constEnd()) {
130 streamWriter.writeStartElement(QStringLiteral("block-list:block"));
131 streamWriter.writeAttribute(QStringLiteral("block-list:abbreviated-name"), *twoUpper);
132 streamWriter.writeEndElement();
133 ++twoUpper;
134 }
135 streamWriter.writeEndElement();
136 streamWriter.writeEndDocument();
137 temporaryShareFile.close();
138
139 mZip->addLocalFile(temporaryShareFile.fileName(), QStringLiteral("WordExceptList.xml"));
140 return true;
141}
142
143bool ExportLibreOfficeAutocorrection::exportManifest()
144{
145 QTemporaryFile temporaryShareFile;
146 temporaryShareFile.open();
147
148 QXmlStreamWriter streamWriter(&temporaryShareFile);
149 streamWriter.setAutoFormatting(true);
150 streamWriter.setAutoFormattingIndent(2);
151 streamWriter.writeStartDocument();
152
153 streamWriter.writeStartElement(QStringLiteral("manifest:manifest"));
154
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();
159
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();
164
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();
169
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();
174
175 streamWriter.writeEndElement();
176 streamWriter.writeEndDocument();
177 temporaryShareFile.close();
178
179 // Add mimetype file
181 mZip->writeFile(QStringLiteral("mimetype"), "");
183 mZip->addLocalFile(temporaryShareFile.fileName(), QStringLiteral("META-INF/manifest.xml"));
184 return true;
185}
virtual bool close()
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)
DeflateCompression
NoCompression
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.