KTextAddons

importlibreofficeautocorrection.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "importlibreofficeautocorrection.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include "textautocorrection_debug.h"
11#include <KLocalizedString>
12#include <KZip>
13#include <QDomDocument>
14#include <QFile>
15#include <QTemporaryDir>
16
17using namespace TextAutoCorrectionCore;
18
19ImportLibreOfficeAutocorrection::ImportLibreOfficeAutocorrection() = default;
20
21ImportLibreOfficeAutocorrection::~ImportLibreOfficeAutocorrection()
22{
23 closeArchive();
24}
25
26void ImportLibreOfficeAutocorrection::closeArchive()
27{
28 if (mArchive) {
29 if (mArchive->isOpen()) {
30 mArchive->close();
31 }
32 delete mArchive;
33 mArchive = nullptr;
34 }
35
36 delete mTempDir;
37 mTempDir = nullptr;
38}
39
40bool ImportLibreOfficeAutocorrection::import(const QString &fileName, QString &errorMessage, LoadAttribute loadAttribute)
41{
42 // We Don't have it in LibreOffice
43 if (loadAttribute == SuperScript) {
44 return false;
45 }
46 closeArchive();
47 mArchive = new KZip(fileName);
48 const bool result = mArchive->open(QIODevice::ReadOnly);
49 if (result) {
50 importAutoCorrectionFile();
51 return true;
52 } else {
53 qCWarning(TEXTAUTOCORRECTION_LOG) << "Impossible to open archive file";
54 errorMessage = i18n("Archive cannot be opened in read mode.");
55 return false;
56 }
57}
58
59void ImportLibreOfficeAutocorrection::importAutoCorrectionFile()
60{
61 mTempDir = new QTemporaryDir();
62 const KArchiveDirectory *archiveDirectory = mArchive->directory();
63 // Replace word
64 if (!importFile(DOCUMENT, archiveDirectory)) {
65 qCWarning(TEXTAUTOCORRECTION_LOG) << " Impossible to import DOCUMENT";
66 return;
67 }
68
69 // No tread as end of line
70 if (!importFile(SENTENCE, archiveDirectory)) {
71 qCWarning(TEXTAUTOCORRECTION_LOG) << " Impossible to import SENTENCE";
72 return;
73 }
74
75 // Two upper letter
76 if (!importFile(WORD, archiveDirectory)) {
77 qCWarning(TEXTAUTOCORRECTION_LOG) << " Impossible to import WORD";
78 return;
79 }
80}
81
82bool ImportLibreOfficeAutocorrection::importFile(Type type, const KArchiveDirectory *archiveDirectory)
83{
84 const KArchiveEntry *documentList = nullptr;
85
86 QString archiveFileName;
87 switch (type) {
88 case DOCUMENT:
89 archiveFileName = QStringLiteral("DocumentList.xml");
90 break;
91 case SENTENCE:
92 archiveFileName = QStringLiteral("SentenceExceptList.xml");
93 break;
94 case WORD:
95 archiveFileName = QStringLiteral("WordExceptList.xml");
96 break;
97 default:
98 return false;
99 }
100 documentList = archiveDirectory->entry(archiveFileName);
101 if (!documentList) {
102 qCWarning(TEXTAUTOCORRECTION_LOG) << "Archive doesn't have file: " << archiveFileName;
103 return false;
104 }
105 if (documentList->isFile()) {
106 const auto archiveFile = static_cast<const KArchiveFile *>(documentList);
107 archiveFile->copyTo(mTempDir->path());
108 QFile file(mTempDir->path() + QLatin1Char('/') + archiveFileName);
109 if (!file.open(QIODevice::ReadOnly)) {
110 qCWarning(TEXTAUTOCORRECTION_LOG) << "Impossible to open " << file.fileName();
111 }
112 QDomDocument doc;
113 if (loadDomElement(doc, &file)) {
115 if (list.isNull()) {
116 qCDebug(TEXTAUTOCORRECTION_LOG) << "No list defined in " << type;
117 } else {
118 for (QDomElement e = list.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
119 const QString tag = e.tagName();
120 if (tag == "block-list:block"_L1) {
121 switch (type) {
122 case DOCUMENT:
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);
130 }
131 break;
132 case SENTENCE:
133 if (e.hasAttribute(QStringLiteral("block-list:abbreviated-name"))) {
134 mUpperCaseExceptions.insert(e.attribute(QStringLiteral("block-list:abbreviated-name")));
135 }
136
137 break;
138 case WORD:
139 if (e.hasAttribute(QStringLiteral("block-list:abbreviated-name"))) {
140 mTwoUpperLetterExceptions.insert(e.attribute(QStringLiteral("block-list:abbreviated-name")));
141 }
142 break;
143 }
144 } else {
145 qCDebug(TEXTAUTOCORRECTION_LOG) << " unknown tag " << tag;
146 }
147 }
148 }
149 }
150 } else {
151 return false;
152 }
153 return true;
154}
155
156bool ImportLibreOfficeAutocorrection::loadDomElement(QDomDocument &doc, QFile *file)
157{
158 const QDomDocument::ParseResult parseResult = doc.setContent(file);
159 if (!parseResult) {
160 qCDebug(TEXTAUTOCORRECTION_LOG) << "Unable to load document.Parse error in line " << parseResult.errorLine << ", col " << parseResult.errorColumn
161 << ": " << parseResult.errorMessage;
162 return false;
163 }
164 return true;
165}
const KArchiveEntry * entry(const QString &name) const
virtual bool isFile() const
bool copyTo(const QString &dest) const
virtual bool close()
virtual bool open(QIODevice::OpenMode mode)
const KArchiveDirectory * directory() const
bool isOpen() 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
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.