7#include "importlibreofficeautocorrection.h"
10#include "textautocorrection_debug.h"
11#include <KLocalizedString>
13#include <QDomDocument>
15#include <QTemporaryDir>
17using namespace TextAutoCorrectionCore;
19ImportLibreOfficeAutocorrection::ImportLibreOfficeAutocorrection() =
default;
21ImportLibreOfficeAutocorrection::~ImportLibreOfficeAutocorrection()
26void ImportLibreOfficeAutocorrection::closeArchive()
40bool ImportLibreOfficeAutocorrection::import(
const QString &fileName,
QString &errorMessage, LoadAttribute loadAttribute)
43 if (loadAttribute == SuperScript) {
47 mArchive =
new KZip(fileName);
50 importAutoCorrectionFile();
53 qCWarning(TEXTAUTOCORRECTION_LOG) <<
"Impossible to open archive file";
59void ImportLibreOfficeAutocorrection::importAutoCorrectionFile()
64 if (!importFile(DOCUMENT, archiveDirectory)) {
65 qCWarning(TEXTAUTOCORRECTION_LOG) <<
" Impossible to import DOCUMENT";
70 if (!importFile(SENTENCE, archiveDirectory)) {
71 qCWarning(TEXTAUTOCORRECTION_LOG) <<
" Impossible to import SENTENCE";
76 if (!importFile(WORD, archiveDirectory)) {
77 qCWarning(TEXTAUTOCORRECTION_LOG) <<
" Impossible to import WORD";
82bool ImportLibreOfficeAutocorrection::importFile(Type type,
const KArchiveDirectory *archiveDirectory)
89 archiveFileName = QStringLiteral(
"DocumentList.xml");
92 archiveFileName = QStringLiteral(
"SentenceExceptList.xml");
95 archiveFileName = QStringLiteral(
"WordExceptList.xml");
100 documentList = archiveDirectory->
entry(archiveFileName);
102 qCWarning(TEXTAUTOCORRECTION_LOG) <<
"Archive doesn't have file: " << archiveFileName;
105 if (documentList->
isFile()) {
106 const auto archiveFile =
static_cast<const KArchiveFile *
>(documentList);
110 qCWarning(TEXTAUTOCORRECTION_LOG) <<
"Impossible to open " << file.fileName();
113 if (loadDomElement(doc, &file)) {
116 qCDebug(TEXTAUTOCORRECTION_LOG) <<
"No list defined in " <<
type;
118 for (
QDomElement e =
list.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
119 const QString tag = e.tagName();
120 if (tag ==
"block-list:block"_L1) {
123 if (e.hasAttribute(QStringLiteral(
"block-list:abbreviated-name")) && e.hasAttribute(QStringLiteral(
"block-list:name"))) {
124 const QString find = e.attribute(QStringLiteral(
"block-list:abbreviated-name"));
125 const QString replace = e.attribute(QStringLiteral(
"block-list:name"));
126 mAutocorrectEntries.
insert(find, replace);
127 const int findLenght(
find.length());
128 mMaxFindStringLength = qMax(findLenght, mMaxFindStringLength);
129 mMinFindStringLength = qMin(findLenght, mMinFindStringLength);
133 if (e.hasAttribute(QStringLiteral(
"block-list:abbreviated-name"))) {
134 mUpperCaseExceptions.
insert(e.attribute(QStringLiteral(
"block-list:abbreviated-name")));
139 if (e.hasAttribute(QStringLiteral(
"block-list:abbreviated-name"))) {
140 mTwoUpperLetterExceptions.
insert(e.attribute(QStringLiteral(
"block-list:abbreviated-name")));
145 qCDebug(TEXTAUTOCORRECTION_LOG) <<
" unknown tag " << tag;
156bool ImportLibreOfficeAutocorrection::loadDomElement(
QDomDocument &doc,
QFile *file)
160 qCDebug(TEXTAUTOCORRECTION_LOG) <<
"Unable to load document.Parse error in line " << parseResult.errorLine <<
", col " << parseResult.errorColumn
161 <<
": " << parseResult.errorMessage;
const KArchiveEntry * entry(const QString &name) const
virtual bool isFile() const
bool copyTo(const QString &dest) const
virtual bool open(QIODevice::OpenMode mode)
const KArchiveDirectory * directory() const
QString i18n(const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & find()
const QList< QKeySequence > & replace()
QDomElement documentElement() const const
ParseResult setContent(QAnyStringView text, ParseOptions options)
iterator insert(const Key &key, const T &value)
iterator insert(const T &value)
QString path() const const