File indexing completed on 2024-05-12 16:16:03

0001 /*
0002    SPDX-FileCopyrightText: 2019-2023 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "languagetoolgrammarerror.h"
0008 #include "languagetoolmanager.h"
0009 #include "textgrammarcheck_debug.h"
0010 
0011 #include <QJsonArray>
0012 using namespace TextGrammarCheck;
0013 LanguageToolGrammarError::LanguageToolGrammarError() = default;
0014 
0015 LanguageToolGrammarError::~LanguageToolGrammarError() = default;
0016 
0017 void LanguageToolGrammarError::parse(const QJsonObject &obj, int blockindex)
0018 {
0019     // We use block id index based on 1 in API
0020     mBlockId = blockindex;
0021     mError = obj[QStringLiteral("message")].toString();
0022     mStart = obj[QStringLiteral("offset")].toInt(-1);
0023     mLength = obj[QStringLiteral("length")].toInt(-1);
0024     mSuggestions = parseSuggestion(obj);
0025     const QJsonObject rulesObj = obj[QStringLiteral("rule")].toObject();
0026     if (!rulesObj.isEmpty()) {
0027         mRule = rulesObj[QStringLiteral("id")].toString();
0028         const QJsonArray urlArray = rulesObj[QStringLiteral("urls")].toArray();
0029         if (!urlArray.isEmpty()) {
0030             if (urlArray.count() > 1) {
0031                 qCWarning(TEXTGRAMMARCHECK_LOG) << "LanguageToolGrammarError::parse : more than 1 url found. Perhaps need to adapt api ";
0032             }
0033             mUrl = urlArray.at(0)[QLatin1String("value")].toString();
0034             // qDebug() << " mUrl" << mUrl;
0035         }
0036     }
0037     if (!mRule.isEmpty() && !mTesting) {
0038         mColor = LanguageToolManager::self()->grammarColorForError(mRule);
0039     } else {
0040         mColor = QColor(Qt::red);
0041     }
0042 }
0043 
0044 void LanguageToolGrammarError::setTesting(bool b)
0045 {
0046     mTesting = b;
0047 }
0048 
0049 QStringList LanguageToolGrammarError::parseSuggestion(const QJsonObject &obj)
0050 {
0051     QStringList lst;
0052     const QJsonArray array = obj[QStringLiteral("replacements")].toArray();
0053     for (const QJsonValue &current : array) {
0054         if (current.type() == QJsonValue::Object) {
0055             const QJsonObject suggestionObject = current.toObject();
0056             lst.append(suggestionObject[QLatin1String("value")].toString());
0057         }
0058     }
0059     // qDebug() << " lst : " << lst;
0060     return lst;
0061 }