importkmailautocorrection.cpp 6.73 KB
Newer Older
1
/*
2
  SPDX-FileCopyrightText: 2012-2020 Laurent Montel <montel@kde.org>
3

4
  SPDX-License-Identifier: GPL-2.0-or-later
5 6 7 8 9
*/

#include "importkmailautocorrection.h"

#include <QFile>
Laurent Montel's avatar
Laurent Montel committed
10
#include <QXmlStreamReader>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include "pimcommon_debug.h"
using namespace PimCommon;

ImportKMailAutocorrection::ImportKMailAutocorrection(QWidget *parent)
    : ImportAbstractAutocorrection(parent)
{
}

ImportKMailAutocorrection::~ImportKMailAutocorrection()
{
}

bool ImportKMailAutocorrection::import(const QString &fileName, LoadAttribute loadAttribute)
{
    QFile xmlFile(fileName);
    if (!xmlFile.open(QIODevice::ReadOnly)) {
        return false;
    }
Laurent Montel's avatar
Laurent Montel committed
29
    QXmlStreamReader xml(&xmlFile);
30 31
    mMaxFindStringLenght = 0;
    mMinFindStringLenght = 0;
Laurent Montel's avatar
Laurent Montel committed
32 33
    if (xml.readNextStartElement()) {
        while (xml.readNextStartElement()) {
34 35
            const QStringRef xmlName = xml.name();
            if (xmlName == QLatin1String("UpperCaseExceptions")) {
Laurent Montel's avatar
Laurent Montel committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49
                if (loadAttribute == All) {
                    while (xml.readNextStartElement()) {
                        const QStringRef tagname = xml.name();
                        if (tagname == QLatin1String("word")) {
                            if (xml.attributes().hasAttribute(QStringLiteral("exception"))) {
                                const QString exception = xml.attributes().value(QStringLiteral("exception")).toString();
                                mUpperCaseExceptions += exception;
                                xml.skipCurrentElement();
                            }
                        }
                    }
                } else {
                    xml.skipCurrentElement();
                }
50
            } else if (xmlName == QLatin1String("TwoUpperLetterExceptions")) {
Laurent Montel's avatar
Laurent Montel committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
                if (loadAttribute == All) {
                    while (xml.readNextStartElement()) {
                        const QStringRef tagname = xml.name();
                        if (tagname == QLatin1String("word")) {
                            if (xml.attributes().hasAttribute(QStringLiteral("exception"))) {
                                const QString exception = xml.attributes().value(QStringLiteral("exception")).toString();
                                mTwoUpperLetterExceptions += exception;
                                xml.skipCurrentElement();
                            }
                        } else {
                            xml.skipCurrentElement();
                        }
                    }
                } else {
                    xml.skipCurrentElement();
                }
67
            } else if (xmlName == QLatin1String("DoubleQuote")) {
Laurent Montel's avatar
Laurent Montel committed
68
                if (loadAttribute == All) {
69 70 71 72 73
                    if (xml.readNextStartElement()) {
                        const QStringRef tagname = xml.name();
                        if (tagname == QLatin1String("doublequote")) {
                            mTypographicDoubleQuotes.begin = xml.attributes().value(QStringLiteral("begin")).toString().at(0);
                            mTypographicDoubleQuotes.end = xml.attributes().value(QStringLiteral("end")).toString().at(0);
Laurent Montel's avatar
Laurent Montel committed
74
                            xml.skipCurrentElement();
75 76 77
                        } else {
                            xml.skipCurrentElement();
                        }
78
                        xml.skipCurrentElement();
79
                    }
Laurent Montel's avatar
Laurent Montel committed
80 81 82
                } else {
                    xml.skipCurrentElement();
                }
83
            } else if (xmlName == QLatin1String("SimpleQuote")) {
Laurent Montel's avatar
Laurent Montel committed
84
                if (loadAttribute == All) {
85 86 87
                    if (xml.readNextStartElement()) {
                        const QStringRef tagname = xml.name();
                        if (tagname == QLatin1String("simplequote")) {
88 89 90 91 92 93 94 95
                            const QString simpleQuoteBegin = xml.attributes().value(QStringLiteral("begin")).toString();
                            if (!simpleQuoteBegin.isEmpty()) { //crash when we have old data with bug.
                                mTypographicSingleQuotes.begin = simpleQuoteBegin.at(0);
                            }
                            const QString simpleQuoteEnd = xml.attributes().value(QStringLiteral("end")).toString();
                            if (!simpleQuoteEnd.isEmpty()) { //crash when we have old data with bug.
                                mTypographicSingleQuotes.end = simpleQuoteEnd.at(0);
                            }
Laurent Montel's avatar
Laurent Montel committed
96
                            xml.skipCurrentElement();
97 98 99
                        } else {
                            xml.skipCurrentElement();
                        }
100
                        xml.skipCurrentElement();
101
                    }
Laurent Montel's avatar
Laurent Montel committed
102 103 104
                } else {
                    xml.skipCurrentElement();
                }
105
            } else if (xmlName == QLatin1String("SuperScript")) {
Laurent Montel's avatar
Laurent Montel committed
106
                if (loadAttribute == All || loadAttribute == SuperScript) {
107 108 109 110 111 112
                    while (xml.readNextStartElement()) {
                        const QStringRef tagname = xml.name();
                        if (tagname == QLatin1String("item")) {
                            const QString find = xml.attributes().value(QStringLiteral("find")).toString();
                            const QString super = xml.attributes().value(QStringLiteral("super")).toString();
                            mSuperScriptEntries.insert(find, super);
Laurent Montel's avatar
Laurent Montel committed
113
                            xml.skipCurrentElement();
114 115 116 117 118 119 120
                        } else {
                            xml.skipCurrentElement();
                        }
                    }
                } else {
                    xml.skipCurrentElement();
                }
121
            } else if (xmlName == QLatin1String("items")) {
122 123 124 125 126 127 128 129 130 131
                if (loadAttribute == All) {
                    while (xml.readNextStartElement()) {
                        const QStringRef tagname = xml.name();
                        if (tagname == QLatin1String("item")) {
                            const QString find = xml.attributes().value(QStringLiteral("find")).toString();
                            const QString replace = xml.attributes().value(QStringLiteral("replace")).toString();
                            const int findLenght(find.length());
                            mMaxFindStringLenght = qMax(findLenght, mMaxFindStringLenght);
                            mMinFindStringLenght = qMin(findLenght, mMinFindStringLenght);
                            mAutocorrectEntries.insert(find, replace);
Laurent Montel's avatar
Laurent Montel committed
132
                            xml.skipCurrentElement();
133 134 135 136
                        } else {
                            xml.skipCurrentElement();
                        }
                    }
Laurent Montel's avatar
Laurent Montel committed
137 138 139 140 141 142 143 144 145
                } else {
                    xml.skipCurrentElement();
                }
            } else {
                //TODO verify
                xml.skipCurrentElement();
            }
        }
    }
146 147
    return true;
}