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 "grammalecteresultjob.h"
0008 #include "textgrammarcheck_debug.h"
0009 
0010 #include <QFileInfo>
0011 #include <QTemporaryFile>
0012 using namespace TextGrammarCheck;
0013 GrammalecteResultJob::GrammalecteResultJob(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 GrammalecteResultJob::~GrammalecteResultJob() = default;
0019 
0020 void GrammalecteResultJob::start()
0021 {
0022     if (canStart()) {
0023         mProcess = new QProcess(this);
0024 
0025         auto file = new QTemporaryFile(this);
0026         file->open();
0027         file->setPermissions(QFile::ReadUser);
0028         file->write(mText.toUtf8());
0029         file->close();
0030 
0031         mProcess->setProgram(mPythonPath);
0032         QStringList args;
0033         args.reserve(6);
0034         args << mGrammarlecteCliPath;
0035         if (!mArguments.isEmpty()) {
0036             args << QStringLiteral("-on") << mArguments;
0037         }
0038         args << QStringLiteral("-f") << file->fileName() << QStringLiteral("-j");
0039         mProcess->setArguments(args);
0040         connect(mProcess, &QProcess::finished, this, &GrammalecteResultJob::slotFinished);
0041         connect(mProcess, &QProcess::errorOccurred, this, &GrammalecteResultJob::receivedError);
0042         connect(mProcess, &QProcess::readyReadStandardError, this, &GrammalecteResultJob::receivedStdErr);
0043         connect(mProcess, &QProcess::readyReadStandardOutput, this, &GrammalecteResultJob::receivedStandardOutput);
0044 
0045         mProcess->start();
0046         if (!mProcess->waitForStarted()) {
0047             qCWarning(TEXTGRAMMARCHECK_LOG) << "Impossible to start grammarresultjob";
0048             Q_EMIT error(ErrorType::Unknown);
0049             deleteLater();
0050         }
0051     } else {
0052         if (mErrorType != ErrorType::TextIsEmpty) {
0053             Q_EMIT error(mErrorType);
0054         }
0055         deleteLater();
0056     }
0057 }
0058 
0059 void GrammalecteResultJob::receivedStandardOutput()
0060 {
0061     mResult += QString::fromUtf8(mProcess->readAllStandardOutput());
0062 }
0063 
0064 void GrammalecteResultJob::receivedError()
0065 {
0066     mLastError += mProcess->errorString();
0067 }
0068 
0069 void GrammalecteResultJob::receivedStdErr()
0070 {
0071     mLastError += QLatin1String(mProcess->readAllStandardError());
0072 }
0073 
0074 void GrammalecteResultJob::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
0075 {
0076     if (exitStatus != 0 || exitCode != 0) {
0077         qCWarning(TEXTGRAMMARCHECK_LOG) << "Error during running GrammarResultJob: " << mLastError;
0078     } else {
0079         Q_EMIT finished(mResult);
0080     }
0081     deleteLater();
0082 }
0083 
0084 QStringList GrammalecteResultJob::arguments() const
0085 {
0086     return mArguments;
0087 }
0088 
0089 void GrammalecteResultJob::setArguments(const QStringList &arguments)
0090 {
0091     mArguments = arguments;
0092 }
0093 
0094 QString GrammalecteResultJob::grammarlecteCliPath() const
0095 {
0096     return mGrammarlecteCliPath;
0097 }
0098 
0099 void GrammalecteResultJob::setGrammarlecteCliPath(const QString &grammarlecteCliPath)
0100 {
0101     mGrammarlecteCliPath = grammarlecteCliPath;
0102 }
0103 
0104 QString GrammalecteResultJob::pythonPath() const
0105 {
0106     return mPythonPath;
0107 }
0108 
0109 void GrammalecteResultJob::setPythonPath(const QString &pythonPath)
0110 {
0111     mPythonPath = pythonPath;
0112 }
0113 
0114 static bool hasNotEmptyText(const QString &text)
0115 {
0116     for (int i = 0, total = text.length(); i < total; ++i) {
0117         if (!text.at(i).isSpace()) {
0118             return true;
0119         }
0120     }
0121     return false;
0122 }
0123 
0124 bool GrammalecteResultJob::canStart()
0125 {
0126     if (!hasNotEmptyText(mText)) {
0127         mErrorType = ErrorType::TextIsEmpty;
0128         return false;
0129     }
0130     if (mGrammarlecteCliPath.isEmpty()) {
0131         mErrorType = ErrorType::GrammalecteMissing;
0132         return false;
0133     }
0134     if (mPythonPath.isEmpty()) {
0135         mErrorType = ErrorType::PythonPathMissing;
0136         return false;
0137     }
0138     if (!QFileInfo::exists(mPythonPath)) {
0139         mErrorType = ErrorType::PythonPathNotExist;
0140         return false;
0141     }
0142     if (!QFileInfo::exists(mGrammarlecteCliPath)) {
0143         mErrorType = ErrorType::GrammarlectCliNotExist;
0144         return false;
0145     }
0146     return true;
0147 }
0148 
0149 QString GrammalecteResultJob::text() const
0150 {
0151     return mText;
0152 }
0153 
0154 void GrammalecteResultJob::setText(const QString &text)
0155 {
0156     mText = text;
0157 }
0158 
0159 #include "moc_grammalecteresultjob.cpp"