KTextAddons

grammalecteresultjob.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 "grammalecteresultjob.h"
8#include "textgrammarcheck_debug.h"
9
10#include <QFileInfo>
11#include <QTemporaryFile>
12using namespace TextGrammarCheck;
13GrammalecteResultJob::GrammalecteResultJob(QObject *parent)
14 : QObject(parent)
15{
16}
17
18GrammalecteResultJob::~GrammalecteResultJob() = default;
19
20void GrammalecteResultJob::start()
21{
22 if (canStart()) {
23 mProcess = new QProcess(this);
24
25 auto file = new QTemporaryFile(this);
26 file->open();
27 file->setPermissions(QFile::ReadUser);
28 file->write(mText.toUtf8());
29 file->close();
30
31 mProcess->setProgram(mPythonPath);
32 QStringList args;
33 args.reserve(6);
34 args << mGrammarlecteCliPath;
35 if (!mArguments.isEmpty()) {
36 args << QStringLiteral("-on") << mArguments;
37 }
38 args << QStringLiteral("-f") << file->fileName() << QStringLiteral("-j");
39 mProcess->setArguments(args);
40 connect(mProcess, &QProcess::finished, this, &GrammalecteResultJob::slotFinished);
41 connect(mProcess, &QProcess::errorOccurred, this, &GrammalecteResultJob::receivedError);
42 connect(mProcess, &QProcess::readyReadStandardError, this, &GrammalecteResultJob::receivedStdErr);
43 connect(mProcess, &QProcess::readyReadStandardOutput, this, &GrammalecteResultJob::receivedStandardOutput);
44
45 mProcess->start();
46 if (!mProcess->waitForStarted()) {
47 qCWarning(TEXTGRAMMARCHECK_LOG) << "Impossible to start grammarresultjob";
48 Q_EMIT error(ErrorType::Unknown);
50 }
51 } else {
52 if (mErrorType != ErrorType::TextIsEmpty) {
53 Q_EMIT error(mErrorType);
54 }
56 }
57}
58
59void GrammalecteResultJob::receivedStandardOutput()
60{
61 mResult += QString::fromUtf8(mProcess->readAllStandardOutput());
62}
63
64void GrammalecteResultJob::receivedError()
65{
66 mLastError += mProcess->errorString();
67}
68
69void GrammalecteResultJob::receivedStdErr()
70{
71 mLastError += QLatin1StringView(mProcess->readAllStandardError());
72}
73
74void GrammalecteResultJob::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
75{
76 if (exitStatus != 0 || exitCode != 0) {
77 qCWarning(TEXTGRAMMARCHECK_LOG) << "Error during running GrammarResultJob: " << mLastError;
78 } else {
79 Q_EMIT finished(mResult);
80 }
82}
83
84QStringList GrammalecteResultJob::arguments() const
85{
86 return mArguments;
87}
88
89void GrammalecteResultJob::setArguments(const QStringList &arguments)
90{
91 mArguments = arguments;
92}
93
94QString GrammalecteResultJob::grammarlecteCliPath() const
95{
96 return mGrammarlecteCliPath;
97}
98
99void GrammalecteResultJob::setGrammarlecteCliPath(const QString &grammarlecteCliPath)
100{
101 mGrammarlecteCliPath = grammarlecteCliPath;
102}
103
104QString GrammalecteResultJob::pythonPath() const
105{
106 return mPythonPath;
107}
108
109void GrammalecteResultJob::setPythonPath(const QString &pythonPath)
110{
111 mPythonPath = pythonPath;
112}
113
114static bool hasNotEmptyText(const QString &text)
115{
116 for (int i = 0, total = text.length(); i < total; ++i) {
117 if (!text.at(i).isSpace()) {
118 return true;
119 }
120 }
121 return false;
122}
123
124bool GrammalecteResultJob::canStart()
125{
126 if (!hasNotEmptyText(mText)) {
127 mErrorType = ErrorType::TextIsEmpty;
128 return false;
129 }
130 if (mGrammarlecteCliPath.isEmpty()) {
131 mErrorType = ErrorType::GrammalecteMissing;
132 return false;
133 }
134 if (mPythonPath.isEmpty()) {
135 mErrorType = ErrorType::PythonPathMissing;
136 return false;
137 }
138 if (!QFileInfo::exists(mPythonPath)) {
139 mErrorType = ErrorType::PythonPathNotExist;
140 return false;
141 }
142 if (!QFileInfo::exists(mGrammarlecteCliPath)) {
143 mErrorType = ErrorType::GrammarlectCliNotExist;
144 return false;
145 }
146 return true;
147}
148
149QString GrammalecteResultJob::text() const
150{
151 return mText;
152}
153
154void GrammalecteResultJob::setText(const QString &text)
155{
156 mText = text;
157}
158
159#include "moc_grammalecteresultjob.cpp"
bool isSpace(char32_t ucs4)
bool exists() const const
QString errorString() const const
bool isEmpty() const const
void reserve(qsizetype size)
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)
const QChar at(qsizetype position) const const
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
qsizetype length() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.