File indexing completed on 2024-12-22 04:28:07
0001 /* 0002 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "importkmailautocorrection.h" 0008 0009 #include <QFile> 0010 #include <QXmlStreamReader> 0011 using namespace TextAutoCorrectionCore; 0012 0013 ImportKMailAutocorrection::ImportKMailAutocorrection() = default; 0014 0015 ImportKMailAutocorrection::~ImportKMailAutocorrection() = default; 0016 0017 bool ImportKMailAutocorrection::import(const QString &fileName, QString &errorMessage, LoadAttribute loadAttribute) 0018 { 0019 Q_UNUSED(errorMessage); 0020 QFile xmlFile(fileName); 0021 if (!xmlFile.open(QIODevice::ReadOnly)) { 0022 return false; 0023 } 0024 QXmlStreamReader xml(&xmlFile); 0025 mMaxFindStringLength = 0; 0026 mMinFindStringLength = 0; 0027 if (xml.readNextStartElement()) { 0028 while (xml.readNextStartElement()) { 0029 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0030 const QStringRef xmlName = xml.name(); 0031 #else 0032 const QStringView xmlName = xml.name(); 0033 #endif 0034 if (xmlName == QLatin1String("UpperCaseExceptions")) { 0035 if (loadAttribute == All) { 0036 while (xml.readNextStartElement()) { 0037 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0038 const QStringRef tagname = xml.name(); 0039 #else 0040 const QStringView tagname = xml.name(); 0041 #endif 0042 if (tagname == QLatin1String("word")) { 0043 if (xml.attributes().hasAttribute(QStringLiteral("exception"))) { 0044 const QString exception = xml.attributes().value(QStringLiteral("exception")).toString(); 0045 mUpperCaseExceptions += exception; 0046 xml.skipCurrentElement(); 0047 } 0048 } 0049 } 0050 } else { 0051 xml.skipCurrentElement(); 0052 } 0053 } else if (xmlName == QLatin1String("TwoUpperLetterExceptions")) { 0054 if (loadAttribute == All) { 0055 while (xml.readNextStartElement()) { 0056 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0057 const QStringRef tagname = xml.name(); 0058 #else 0059 const QStringView tagname = xml.name(); 0060 #endif 0061 if (tagname == QLatin1String("word")) { 0062 if (xml.attributes().hasAttribute(QStringLiteral("exception"))) { 0063 const QString exception = xml.attributes().value(QStringLiteral("exception")).toString(); 0064 mTwoUpperLetterExceptions += exception; 0065 xml.skipCurrentElement(); 0066 } 0067 } else { 0068 xml.skipCurrentElement(); 0069 } 0070 } 0071 } else { 0072 xml.skipCurrentElement(); 0073 } 0074 } else if (xmlName == QLatin1String("DoubleQuote")) { 0075 if (loadAttribute == All) { 0076 if (xml.readNextStartElement()) { 0077 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0078 const QStringRef tagname = xml.name(); 0079 #else 0080 const QStringView tagname = xml.name(); 0081 #endif 0082 if (tagname == QLatin1String("doublequote")) { 0083 mTypographicDoubleQuotes.begin = xml.attributes().value(QStringLiteral("begin")).toString().at(0); 0084 mTypographicDoubleQuotes.end = xml.attributes().value(QStringLiteral("end")).toString().at(0); 0085 xml.skipCurrentElement(); 0086 } else { 0087 xml.skipCurrentElement(); 0088 } 0089 xml.skipCurrentElement(); 0090 } 0091 } else { 0092 xml.skipCurrentElement(); 0093 } 0094 } else if (xmlName == QLatin1String("SimpleQuote")) { 0095 if (loadAttribute == All) { 0096 if (xml.readNextStartElement()) { 0097 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0098 const QStringRef tagname = xml.name(); 0099 #else 0100 const QStringView tagname = xml.name(); 0101 #endif 0102 if (tagname == QLatin1String("simplequote")) { 0103 const QString simpleQuoteBegin = xml.attributes().value(QStringLiteral("begin")).toString(); 0104 if (!simpleQuoteBegin.isEmpty()) { // crash when we have old data with bug. 0105 mTypographicSingleQuotes.begin = simpleQuoteBegin.at(0); 0106 } 0107 const QString simpleQuoteEnd = xml.attributes().value(QStringLiteral("end")).toString(); 0108 if (!simpleQuoteEnd.isEmpty()) { // crash when we have old data with bug. 0109 mTypographicSingleQuotes.end = simpleQuoteEnd.at(0); 0110 } 0111 xml.skipCurrentElement(); 0112 } else { 0113 xml.skipCurrentElement(); 0114 } 0115 xml.skipCurrentElement(); 0116 } 0117 } else { 0118 xml.skipCurrentElement(); 0119 } 0120 } else if (xmlName == QLatin1String("SuperScript")) { 0121 if (loadAttribute == All || loadAttribute == SuperScript) { 0122 while (xml.readNextStartElement()) { 0123 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0124 const QStringRef tagname = xml.name(); 0125 #else 0126 const QStringView tagname = xml.name(); 0127 #endif 0128 if (tagname == QLatin1String("item")) { 0129 const QString find = xml.attributes().value(QStringLiteral("find")).toString(); 0130 const QString super = xml.attributes().value(QStringLiteral("super")).toString(); 0131 mSuperScriptEntries.insert(find, super); 0132 xml.skipCurrentElement(); 0133 } else { 0134 xml.skipCurrentElement(); 0135 } 0136 } 0137 } else { 0138 xml.skipCurrentElement(); 0139 } 0140 } else if (xmlName == QLatin1String("items")) { 0141 if (loadAttribute == All) { 0142 while (xml.readNextStartElement()) { 0143 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0144 const QStringRef tagname = xml.name(); 0145 #else 0146 const QStringView tagname = xml.name(); 0147 #endif 0148 if (tagname == QLatin1String("item")) { 0149 const QString find = xml.attributes().value(QStringLiteral("find")).toString(); 0150 const QString replace = xml.attributes().value(QStringLiteral("replace")).toString(); 0151 const int findLenght(find.length()); 0152 mMaxFindStringLength = qMax(findLenght, mMaxFindStringLength); 0153 mMinFindStringLength = qMin(findLenght, mMinFindStringLength); 0154 mAutocorrectEntries.insert(find, replace); 0155 xml.skipCurrentElement(); 0156 } else { 0157 xml.skipCurrentElement(); 0158 } 0159 } 0160 } else { 0161 xml.skipCurrentElement(); 0162 } 0163 } else { 0164 // TODO verify 0165 xml.skipCurrentElement(); 0166 } 0167 } 0168 } 0169 return true; 0170 }