1 /*
2 SPDX-FileCopyrightText: 2019-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "grammalectegenerateconfigoptionjob.h"
8 #include "libgrammalecte_debug.h"
9 #include <QRegularExpression>
10 #include <QRegularExpressionMatch>
11
GrammalecteGenerateConfigOptionJob(QObject * parent)12 GrammalecteGenerateConfigOptionJob::GrammalecteGenerateConfigOptionJob(QObject *parent)
13 : QObject(parent)
14 {
15 }
16
~GrammalecteGenerateConfigOptionJob()17 GrammalecteGenerateConfigOptionJob::~GrammalecteGenerateConfigOptionJob()
18 {
19 }
20
21 //^([a-zA-Z0-9]+):\s*(True|False)\s*(.*)$
start()22 void 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, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &GrammalecteGenerateConfigOptionJob::slotFinished);
29 connect(mProcess, qOverload<QProcess::ProcessError>(&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(LIBGRAMMALECTE_PLUGIN_LOG) << "Impossible to start GrammalecteGenerateConfigOptionJob";
35 Q_EMIT error();
36 deleteLater();
37 }
38 } else {
39 qCWarning(LIBGRAMMALECTE_PLUGIN_LOG) << "Impossible to start GrammalecteGenerateConfigOptionJob";
40 Q_EMIT error();
41 deleteLater();
42 }
43 }
44
canStart() const45 bool GrammalecteGenerateConfigOptionJob::canStart() const
46 {
47 if (mPythonPath.isEmpty() || mGrammarlecteCliPath.isEmpty()) {
48 return false;
49 }
50 return true;
51 }
52
receivedError()53 void GrammalecteGenerateConfigOptionJob::receivedError()
54 {
55 mLastError += mProcess->errorString();
56 }
57
receivedStdErr()58 void GrammalecteGenerateConfigOptionJob::receivedStdErr()
59 {
60 mLastError += QLatin1String(mProcess->readAllStandardError());
61 }
62
pythonPath() const63 QString GrammalecteGenerateConfigOptionJob::pythonPath() const
64 {
65 return mPythonPath;
66 }
67
setPythonPath(const QString & pythonPath)68 void GrammalecteGenerateConfigOptionJob::setPythonPath(const QString &pythonPath)
69 {
70 mPythonPath = pythonPath;
71 }
72
grammarlecteCliPath() const73 QString GrammalecteGenerateConfigOptionJob::grammarlecteCliPath() const
74 {
75 return mGrammarlecteCliPath;
76 }
77
setGrammarlecteCliPath(const QString & grammarlecteCliPath)78 void GrammalecteGenerateConfigOptionJob::setGrammarlecteCliPath(const QString &grammarlecteCliPath)
79 {
80 mGrammarlecteCliPath = grammarlecteCliPath;
81 }
82
receivedStandardOutput()83 void GrammalecteGenerateConfigOptionJob::receivedStandardOutput()
84 {
85 mResult += QString::fromUtf8(mProcess->readAllStandardOutput());
86 }
87
slotFinished(int exitCode,QProcess::ExitStatus exitStatus)88 void GrammalecteGenerateConfigOptionJob::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
89 {
90 if (exitStatus != 0 || exitCode != 0) {
91 qCWarning(LIBGRAMMALECTE_PLUGIN_LOG) << "GrammalecteGenerateConfigOptionJob ERROR: " << mLastError;
92 Q_EMIT error();
93 } else {
94 Q_EMIT finished(parseResult());
95 }
96 deleteLater();
97 }
98
parseResult() const99 QVector<GrammalecteGenerateConfigOptionJob::Option> GrammalecteGenerateConfigOptionJob::parseResult() const
100 {
101 QVector<GrammalecteGenerateConfigOptionJob::Option> opts;
102 static const QRegularExpression reg(QStringLiteral("^([a-zA-Z0-9]+):\\s*(True|False)\\s*(.*)$"));
103 const QStringList lst = mResult.split(QLatin1Char('\n'));
104 for (const QString &str : lst) {
105 const QRegularExpressionMatch match = reg.match(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 == QLatin1String("True"));
118 opts.append(opt);
119 }
120 }
121 }
122 return opts;
123 }
124