KTextAddons

grammalectegenerateconfigoptionjob.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "grammalectegenerateconfigoptionjob.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include "textgrammarcheck_debug.h"
11#include <QRegularExpression>
12#include <QRegularExpressionMatch>
13using namespace TextGrammarCheck;
14GrammalecteGenerateConfigOptionJob::GrammalecteGenerateConfigOptionJob(QObject *parent)
15 : QObject(parent)
16{
17}
18
19GrammalecteGenerateConfigOptionJob::~GrammalecteGenerateConfigOptionJob() = default;
20
21//^([a-zA-Z0-9]+):\s*(True|False)\s*(.*)$
22void GrammalecteGenerateConfigOptionJob::start()
23{
24 if (canStart()) {
25 mProcess = new QProcess(this);
26 mProcess->setProgram(mPythonPath);
27 mProcess->setArguments(QStringList() << mGrammarlecteCliPath << QStringLiteral("-lo"));
28 connect(mProcess, &QProcess::finished, this, &GrammalecteGenerateConfigOptionJob::slotFinished);
29 connect(mProcess, &QProcess::errorOccurred, this, &GrammalecteGenerateConfigOptionJob::receivedError);
30 connect(mProcess, &QProcess::readyReadStandardError, this, &GrammalecteGenerateConfigOptionJob::receivedStdErr);
31 connect(mProcess, &QProcess::readyReadStandardOutput, this, &GrammalecteGenerateConfigOptionJob::receivedStandardOutput);
32 mProcess->start();
33 if (!mProcess->waitForStarted()) {
34 qCWarning(TEXTGRAMMARCHECK_LOG) << "Impossible to start GrammalecteGenerateConfigOptionJob";
35 Q_EMIT error();
37 }
38 } else {
39 qCWarning(TEXTGRAMMARCHECK_LOG) << "Impossible to start GrammalecteGenerateConfigOptionJob";
40 Q_EMIT error();
42 }
43}
44
45bool GrammalecteGenerateConfigOptionJob::canStart() const
46{
47 if (mPythonPath.isEmpty() || mGrammarlecteCliPath.isEmpty()) {
48 return false;
49 }
50 return true;
51}
52
53void GrammalecteGenerateConfigOptionJob::receivedError()
54{
55 mLastError += mProcess->errorString();
56}
57
58void GrammalecteGenerateConfigOptionJob::receivedStdErr()
59{
60 mLastError += QLatin1StringView(mProcess->readAllStandardError());
61}
62
63QString GrammalecteGenerateConfigOptionJob::pythonPath() const
64{
65 return mPythonPath;
66}
67
68void GrammalecteGenerateConfigOptionJob::setPythonPath(const QString &pythonPath)
69{
70 mPythonPath = pythonPath;
71}
72
73QString GrammalecteGenerateConfigOptionJob::grammarlecteCliPath() const
74{
75 return mGrammarlecteCliPath;
76}
77
78void GrammalecteGenerateConfigOptionJob::setGrammarlecteCliPath(const QString &grammarlecteCliPath)
79{
80 mGrammarlecteCliPath = grammarlecteCliPath;
81}
82
83void GrammalecteGenerateConfigOptionJob::receivedStandardOutput()
84{
85 mResult += QString::fromUtf8(mProcess->readAllStandardOutput());
86}
87
88void GrammalecteGenerateConfigOptionJob::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
89{
90 if (exitStatus != 0 || exitCode != 0) {
91 qCWarning(TEXTGRAMMARCHECK_LOG) << "GrammalecteGenerateConfigOptionJob ERROR: " << mLastError;
92 Q_EMIT error();
93 } else {
94 Q_EMIT finished(parseResult());
95 }
97}
98
99QVector<GrammalecteGenerateConfigOptionJob::Option> GrammalecteGenerateConfigOptionJob::parseResult() const
100{
102 static const QRegularExpression reg(QStringLiteral("^([a-zA-Z0-9]+):\\s*(True|False)\\s*(.*)$"));
103 const QList<QStringView> lst = QStringView(mResult).split(QLatin1Char('\n'));
104 for (const QStringView &str : lst) {
105 const QRegularExpressionMatch match = reg.matchView(str);
106 if (match.hasMatch()) {
107 const QString optionName = match.captured(1);
108 const QString value = match.captured(2);
109 const QString description = match.captured(3);
110 if (!optionName.isEmpty() && !description.isEmpty() && !value.isEmpty()) {
111 if (description == QLatin1Char('?')) {
112 continue;
113 }
114 GrammalecteGenerateConfigOptionJob::Option opt;
115 opt.description = description;
116 opt.optionName = optionName;
117 opt.defaultValue = (value == "True"_L1);
118 opts.append(std::move(opt));
119 }
120 }
121 }
122 return opts;
123}
124
125#include "moc_grammalectegenerateconfigoptionjob.cpp"
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
QString errorString() const const
void append(QList< T > &&value)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
void errorOccurred(QProcess::ProcessError error)
void finished(int exitCode, QProcess::ExitStatus exitStatus)
QByteArray readAllStandardError()
QByteArray readAllStandardOutput()
void readyReadStandardError()
void readyReadStandardOutput()
void setArguments(const QStringList &arguments)
void setProgram(const QString &program)
void start(OpenMode mode)
bool waitForStarted(int msecs)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:02:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.