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 "grammalectegenerateconfigoptionjob.h"
0008 #include "textgrammarcheck_debug.h"
0009 #include <QRegularExpression>
0010 #include <QRegularExpressionMatch>
0011 using namespace TextGrammarCheck;
0012 GrammalecteGenerateConfigOptionJob::GrammalecteGenerateConfigOptionJob(QObject *parent)
0013     : QObject(parent)
0014 {
0015 }
0016 
0017 GrammalecteGenerateConfigOptionJob::~GrammalecteGenerateConfigOptionJob() = default;
0018 
0019 //^([a-zA-Z0-9]+):\s*(True|False)\s*(.*)$
0020 void GrammalecteGenerateConfigOptionJob::start()
0021 {
0022     if (canStart()) {
0023         mProcess = new QProcess(this);
0024         mProcess->setProgram(mPythonPath);
0025         mProcess->setArguments(QStringList() << mGrammarlecteCliPath << QStringLiteral("-lo"));
0026         connect(mProcess, &QProcess::finished, this, &GrammalecteGenerateConfigOptionJob::slotFinished);
0027         connect(mProcess, &QProcess::errorOccurred, this, &GrammalecteGenerateConfigOptionJob::receivedError);
0028         connect(mProcess, &QProcess::readyReadStandardError, this, &GrammalecteGenerateConfigOptionJob::receivedStdErr);
0029         connect(mProcess, &QProcess::readyReadStandardOutput, this, &GrammalecteGenerateConfigOptionJob::receivedStandardOutput);
0030         mProcess->start();
0031         if (!mProcess->waitForStarted()) {
0032             qCWarning(TEXTGRAMMARCHECK_LOG) << "Impossible to start GrammalecteGenerateConfigOptionJob";
0033             Q_EMIT error();
0034             deleteLater();
0035         }
0036     } else {
0037         qCWarning(TEXTGRAMMARCHECK_LOG) << "Impossible to start GrammalecteGenerateConfigOptionJob";
0038         Q_EMIT error();
0039         deleteLater();
0040     }
0041 }
0042 
0043 bool GrammalecteGenerateConfigOptionJob::canStart() const
0044 {
0045     if (mPythonPath.isEmpty() || mGrammarlecteCliPath.isEmpty()) {
0046         return false;
0047     }
0048     return true;
0049 }
0050 
0051 void GrammalecteGenerateConfigOptionJob::receivedError()
0052 {
0053     mLastError += mProcess->errorString();
0054 }
0055 
0056 void GrammalecteGenerateConfigOptionJob::receivedStdErr()
0057 {
0058     mLastError += QLatin1String(mProcess->readAllStandardError());
0059 }
0060 
0061 QString GrammalecteGenerateConfigOptionJob::pythonPath() const
0062 {
0063     return mPythonPath;
0064 }
0065 
0066 void GrammalecteGenerateConfigOptionJob::setPythonPath(const QString &pythonPath)
0067 {
0068     mPythonPath = pythonPath;
0069 }
0070 
0071 QString GrammalecteGenerateConfigOptionJob::grammarlecteCliPath() const
0072 {
0073     return mGrammarlecteCliPath;
0074 }
0075 
0076 void GrammalecteGenerateConfigOptionJob::setGrammarlecteCliPath(const QString &grammarlecteCliPath)
0077 {
0078     mGrammarlecteCliPath = grammarlecteCliPath;
0079 }
0080 
0081 void GrammalecteGenerateConfigOptionJob::receivedStandardOutput()
0082 {
0083     mResult += QString::fromUtf8(mProcess->readAllStandardOutput());
0084 }
0085 
0086 void GrammalecteGenerateConfigOptionJob::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
0087 {
0088     if (exitStatus != 0 || exitCode != 0) {
0089         qCWarning(TEXTGRAMMARCHECK_LOG) << "GrammalecteGenerateConfigOptionJob ERROR: " << mLastError;
0090         Q_EMIT error();
0091     } else {
0092         Q_EMIT finished(parseResult());
0093     }
0094     deleteLater();
0095 }
0096 
0097 QVector<GrammalecteGenerateConfigOptionJob::Option> GrammalecteGenerateConfigOptionJob::parseResult() const
0098 {
0099     QVector<GrammalecteGenerateConfigOptionJob::Option> opts;
0100     static const QRegularExpression reg(QStringLiteral("^([a-zA-Z0-9]+):\\s*(True|False)\\s*(.*)$"));
0101     const QStringList lst = mResult.split(QLatin1Char('\n'));
0102     for (const QString &str : lst) {
0103         const QRegularExpressionMatch match = reg.match(str);
0104         if (match.hasMatch()) {
0105             const QString optionName = match.captured(1);
0106             const QString value = match.captured(2);
0107             const QString description = match.captured(3);
0108             if (!optionName.isEmpty() && !description.isEmpty() && !value.isEmpty()) {
0109                 if (description == QLatin1Char('?')) {
0110                     continue;
0111                 }
0112                 GrammalecteGenerateConfigOptionJob::Option opt;
0113                 opt.description = description;
0114                 opt.optionName = optionName;
0115                 opt.defaultValue = (value == QLatin1String("True"));
0116                 opts.append(std::move(opt));
0117             }
0118         }
0119     }
0120     return opts;
0121 }
0122 
0123 #include "moc_grammalectegenerateconfigoptionjob.cpp"