KTextAddons

importkmailautocorrection.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 "importkmailautocorrection.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include <QFile>
11#include <QXmlStreamReader>
12using namespace TextAutoCorrectionCore;
13
14ImportKMailAutocorrection::ImportKMailAutocorrection() = default;
15
16ImportKMailAutocorrection::~ImportKMailAutocorrection() = default;
17
18bool ImportKMailAutocorrection::import(const QString &fileName, QString &errorMessage, LoadAttribute loadAttribute)
19{
20 Q_UNUSED(errorMessage);
21 QFile xmlFile(fileName);
22 if (!xmlFile.open(QIODevice::ReadOnly)) {
23 return false;
24 }
25 QXmlStreamReader xml(&xmlFile);
26 mMaxFindStringLength = 0;
27 mMinFindStringLength = 0;
28 if (xml.readNextStartElement()) {
29 while (xml.readNextStartElement()) {
30 const QStringView xmlName = xml.name();
31 if (xmlName == "UpperCaseExceptions"_L1) {
32 if (loadAttribute == All) {
33 while (xml.readNextStartElement()) {
34 const QStringView tagname = xml.name();
35 if (tagname == "word"_L1) {
36 if (xml.attributes().hasAttribute(QStringLiteral("exception"))) {
37 const QString exception = xml.attributes().value(QStringLiteral("exception")).toString();
38 mUpperCaseExceptions += exception;
39 xml.skipCurrentElement();
40 }
41 }
42 }
43 } else {
44 xml.skipCurrentElement();
45 }
46 } else if (xmlName == "TwoUpperLetterExceptions"_L1) {
47 if (loadAttribute == All) {
48 while (xml.readNextStartElement()) {
49 const QStringView tagname = xml.name();
50 if (tagname == "word"_L1) {
51 if (xml.attributes().hasAttribute(QStringLiteral("exception"))) {
52 const QString exception = xml.attributes().value(QStringLiteral("exception")).toString();
53 mTwoUpperLetterExceptions += exception;
54 xml.skipCurrentElement();
55 }
56 } else {
57 xml.skipCurrentElement();
58 }
59 }
60 } else {
61 xml.skipCurrentElement();
62 }
63 } else if (xmlName == "DoubleQuote"_L1) {
64 if (loadAttribute == All) {
65 if (xml.readNextStartElement()) {
66 const QStringView tagname = xml.name();
67 if (tagname == "doublequote"_L1) {
68 mTypographicDoubleQuotes.begin = xml.attributes().value(QStringLiteral("begin")).toString().at(0);
69 mTypographicDoubleQuotes.end = xml.attributes().value(QStringLiteral("end")).toString().at(0);
70 xml.skipCurrentElement();
71 } else {
72 xml.skipCurrentElement();
73 }
74 xml.skipCurrentElement();
75 }
76 } else {
77 xml.skipCurrentElement();
78 }
79 } else if (xmlName == "SimpleQuote"_L1) {
80 if (loadAttribute == All) {
81 if (xml.readNextStartElement()) {
82 const QStringView tagname = xml.name();
83 if (tagname == "simplequote"_L1) {
84 const QString simpleQuoteBegin = xml.attributes().value(QStringLiteral("begin")).toString();
85 if (!simpleQuoteBegin.isEmpty()) { // crash when we have old data with bug.
86 mTypographicSingleQuotes.begin = simpleQuoteBegin.at(0);
87 }
88 const QString simpleQuoteEnd = xml.attributes().value(QStringLiteral("end")).toString();
89 if (!simpleQuoteEnd.isEmpty()) { // crash when we have old data with bug.
90 mTypographicSingleQuotes.end = simpleQuoteEnd.at(0);
91 }
92 xml.skipCurrentElement();
93 } else {
94 xml.skipCurrentElement();
95 }
96 xml.skipCurrentElement();
97 }
98 } else {
99 xml.skipCurrentElement();
100 }
101 } else if (xmlName == "SuperScript"_L1) {
102 if (loadAttribute == All || loadAttribute == SuperScript) {
103 while (xml.readNextStartElement()) {
104 const QStringView tagname = xml.name();
105 if (tagname == "item"_L1) {
106 const QString find = xml.attributes().value(QStringLiteral("find")).toString();
107 const QString super = xml.attributes().value(QStringLiteral("super")).toString();
108 mSuperScriptEntries.insert(find, super);
109 xml.skipCurrentElement();
110 } else {
111 xml.skipCurrentElement();
112 }
113 }
114 } else {
115 xml.skipCurrentElement();
116 }
117 } else if (xmlName == "items"_L1) {
118 if (loadAttribute == All) {
119 while (xml.readNextStartElement()) {
120 const QStringView tagname = xml.name();
121 if (tagname == "item"_L1) {
122 const QString find = xml.attributes().value(QStringLiteral("find")).toString();
123 const QString replace = xml.attributes().value(QStringLiteral("replace")).toString();
124 const int findLenght(find.length());
125 mMaxFindStringLength = qMax(findLenght, mMaxFindStringLength);
126 mMinFindStringLength = qMin(findLenght, mMinFindStringLength);
127 mAutocorrectEntries.insert(find, replace);
128 xml.skipCurrentElement();
129 } else {
130 xml.skipCurrentElement();
131 }
132 }
133 } else {
134 xml.skipCurrentElement();
135 }
136 } else {
137 // TODO verify
138 xml.skipCurrentElement();
139 }
140 }
141 }
142 return true;
143}
QAction * replace(const QObject *recvr, const char *slot, QObject *parent)
KGuiItem find()
iterator insert(const Key &key, const T &value)
const QChar at(qsizetype position) const const
bool isEmpty() const const
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.