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

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 "grammalecteresultwidget.h"
0008 #include "common/grammarresulttextedit.h"
0009 #include "grammalectemanager.h"
0010 #include "grammalecteparser.h"
0011 #include "textgrammarcheck_debug.h"
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <QJsonDocument>
0015 using namespace TextGrammarCheck;
0016 GrammalecteResultWidget::GrammalecteResultWidget(QWidget *parent)
0017     : GrammarResultWidget(parent)
0018 {
0019 }
0020 
0021 GrammalecteResultWidget::~GrammalecteResultWidget() = default;
0022 
0023 void GrammalecteResultWidget::checkGrammar()
0024 {
0025     auto job = new GrammalecteResultJob(this);
0026     job->setPythonPath(GrammalecteManager::self()->pythonPath());
0027     job->setGrammarlecteCliPath(GrammalecteManager::self()->grammalectePath());
0028     job->setArguments(GrammalecteManager::self()->options());
0029     job->setText(mResult->toPlainText());
0030     connect(job, &GrammalecteResultJob::finished, this, &GrammalecteResultWidget::slotCheckGrammarFinished);
0031     connect(job, &GrammalecteResultJob::error, this, &GrammalecteResultWidget::slotError);
0032     job->start();
0033 }
0034 
0035 void GrammalecteResultWidget::slotCheckGrammarFinished(const QString &result)
0036 {
0037     GrammalecteParser parser;
0038     const QJsonDocument doc = QJsonDocument::fromJson(result.toUtf8());
0039     const QJsonObject fields = doc.object();
0040     applyGrammarResult(parser.parseResult(fields));
0041 }
0042 
0043 void GrammalecteResultWidget::slotError(GrammalecteResultJob::ErrorType error)
0044 {
0045     QString str;
0046     switch (error) {
0047     case GrammalecteResultJob::ErrorType::NoError:
0048         break;
0049     case GrammalecteResultJob::ErrorType::TextIsEmpty:
0050         qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: text is empty";
0051         break;
0052     case GrammalecteResultJob::ErrorType::PythonPathMissing:
0053         qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: missing python path";
0054         str = i18n("Python path is missing.");
0055         break;
0056     case GrammalecteResultJob::ErrorType::GrammalecteMissing:
0057         qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: missing grammalectepath";
0058         str = i18n("Grammalecte path not found.");
0059         break;
0060     case GrammalecteResultJob::ErrorType::Unknown:
0061         qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: unknown error";
0062         break;
0063     case GrammalecteResultJob::ErrorType::PythonPathNotExist:
0064         qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: python exec doesn't exist";
0065         str = i18n("Grammalecte program file not found.");
0066         break;
0067     case GrammalecteResultJob::ErrorType::GrammarlectCliNotExist:
0068         qCWarning(TEXTGRAMMARCHECK_LOG) << "An error found during executing GrammalecteResultJob: grammalecte cli not found.";
0069         str = i18n("Grammalecte cli file not found.");
0070         break;
0071     }
0072     if (!str.isEmpty()) {
0073         KMessageBox::error(this, str, i18n("Grammalecte error"));
0074     }
0075 }
0076 
0077 #include "moc_grammalecteresultwidget.cpp"