grammalecteresultjob.cpp 4.05 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   SPDX-FileCopyrightText: 2019-2021 Laurent Montel <montel@kde.org>
3
4

   SPDX-License-Identifier: GPL-2.0-or-later
Laurent Montel's avatar
Laurent Montel committed
5
6
*/

7
#include "grammalecteresultjob.h"
Laurent Montel's avatar
Laurent Montel committed
8
#include "libgrammalecte_debug.h"
Laurent Montel's avatar
Laurent Montel committed
9

Laurent Montel's avatar
Laurent Montel committed
10
#include <QFileInfo>
Laurent Montel's avatar
Laurent Montel committed
11
12
#include <QTemporaryFile>

13
GrammalecteResultJob::GrammalecteResultJob(QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
14
15
16
17
    : QObject(parent)
{
}

18
GrammalecteResultJob::~GrammalecteResultJob()
Laurent Montel's avatar
Laurent Montel committed
19
20
21
{
}

22
void GrammalecteResultJob::start()
Laurent Montel's avatar
Laurent Montel committed
23
24
{
    if (canStart()) {
Laurent Montel's avatar
Laurent Montel committed
25
26
        mProcess = new QProcess(this);

27
        auto file = new QTemporaryFile(this);
Laurent Montel's avatar
Laurent Montel committed
28
29
30
31
32
33
        file->open();
        file->setPermissions(QFile::ReadUser);
        file->write(mText.toUtf8());
        file->close();

        mProcess->setProgram(mPythonPath);
Laurent Montel's avatar
Laurent Montel committed
34
        QStringList args;
Laurent Montel's avatar
Laurent Montel committed
35
        args.reserve(6);
Laurent Montel's avatar
Laurent Montel committed
36
37
38
39
40
41
        args << mGrammarlecteCliPath;
        if (!mArguments.isEmpty()) {
            args << QStringLiteral("-on") << mArguments;
        }
        args << QStringLiteral("-f") << file->fileName() << QStringLiteral("-j");
        mProcess->setArguments(args);
Laurent Montel's avatar
Laurent Montel committed
42
        connect(mProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &GrammalecteResultJob::slotFinished);
Laurent Montel's avatar
Laurent Montel committed
43
        connect(mProcess, qOverload<QProcess::ProcessError>(&QProcess::errorOccurred), this, &GrammalecteResultJob::receivedError);
44
45
        connect(mProcess, &QProcess::readyReadStandardError, this, &GrammalecteResultJob::receivedStdErr);
        connect(mProcess, &QProcess::readyReadStandardOutput, this, &GrammalecteResultJob::receivedStandardOutput);
Laurent Montel's avatar
Laurent Montel committed
46

Laurent Montel's avatar
Laurent Montel committed
47
        mProcess->start();
Laurent Montel's avatar
Laurent Montel committed
48
        if (!mProcess->waitForStarted()) {
Laurent Montel's avatar
Laurent Montel committed
49
            qCWarning(LIBGRAMMALECTE_PLUGIN_LOG) << "Impossible to start grammarresultjob";
Laurent Montel's avatar
Laurent Montel committed
50
            Q_EMIT error(ErrorType::Unknown);
51
52
            deleteLater();
        }
Laurent Montel's avatar
Laurent Montel committed
53
    } else {
Laurent Montel's avatar
Laurent Montel committed
54
55
56
        if (mErrorType != ErrorType::TextIsEmpty) {
            Q_EMIT error(mErrorType);
        }
57
        deleteLater();
Laurent Montel's avatar
Laurent Montel committed
58
59
60
    }
}

61
void GrammalecteResultJob::receivedStandardOutput()
Laurent Montel's avatar
Laurent Montel committed
62
63
64
65
{
    mResult += QString::fromUtf8(mProcess->readAllStandardOutput());
}

66
void GrammalecteResultJob::receivedError()
Laurent Montel's avatar
Laurent Montel committed
67
{
68
    mLastError += mProcess->errorString();
Laurent Montel's avatar
Laurent Montel committed
69
70
}

71
void GrammalecteResultJob::receivedStdErr()
Laurent Montel's avatar
Laurent Montel committed
72
{
73
    mLastError += QLatin1String(mProcess->readAllStandardError());
Laurent Montel's avatar
Laurent Montel committed
74
75
}

76
void GrammalecteResultJob::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
77
{
Laurent Montel's avatar
Laurent Montel committed
78
    if (exitStatus != 0 || exitCode != 0) {
Laurent Montel's avatar
Laurent Montel committed
79
        qCWarning(LIBGRAMMALECTE_PLUGIN_LOG) << "Error during running GrammarResultJob: " << mLastError;
Laurent Montel's avatar
Laurent Montel committed
80
81
82
83
    } else {
        Q_EMIT finished(mResult);
    }
    deleteLater();
84
85
}

86
QStringList GrammalecteResultJob::arguments() const
87
88
89
90
{
    return mArguments;
}

91
void GrammalecteResultJob::setArguments(const QStringList &arguments)
92
93
94
95
{
    mArguments = arguments;
}

96
QString GrammalecteResultJob::grammarlecteCliPath() const
97
98
99
100
{
    return mGrammarlecteCliPath;
}

101
void GrammalecteResultJob::setGrammarlecteCliPath(const QString &grammarlecteCliPath)
102
103
104
105
{
    mGrammarlecteCliPath = grammarlecteCliPath;
}

106
QString GrammalecteResultJob::pythonPath() const
107
108
109
110
{
    return mPythonPath;
}

111
void GrammalecteResultJob::setPythonPath(const QString &pythonPath)
112
113
114
115
{
    mPythonPath = pythonPath;
}

Laurent Montel's avatar
Laurent Montel committed
116
117
static bool hasNotEmptyText(const QString &text)
{
Laurent Montel's avatar
Laurent Montel committed
118
    for (int i = 0, total = text.length(); i < total; ++i) {
Laurent Montel's avatar
Laurent Montel committed
119
120
121
122
123
124
125
        if (!text.at(i).isSpace()) {
            return true;
        }
    }
    return false;
}

Laurent Montel's avatar
Laurent Montel committed
126
bool GrammalecteResultJob::canStart()
Laurent Montel's avatar
Laurent Montel committed
127
{
Laurent Montel's avatar
Laurent Montel committed
128
129
130
    if (!hasNotEmptyText(mText)) {
        mErrorType = ErrorType::TextIsEmpty;
        return false;
Laurent Montel's avatar
Laurent Montel committed
131
    }
Laurent Montel's avatar
Laurent Montel committed
132
133
134
135
136
137
138
139
    if (mGrammarlecteCliPath.isEmpty()) {
        mErrorType = ErrorType::GrammalecteMissing;
        return false;
    }
    if (mPythonPath.isEmpty()) {
        mErrorType = ErrorType::PythonPathMissing;
        return false;
    }
Laurent Montel's avatar
Laurent Montel committed
140
141
142
143
144
145
146
147
    if (!QFileInfo::exists(mPythonPath)) {
        mErrorType = ErrorType::PythonPathNotExist;
        return false;
    }
    if (!QFileInfo::exists(mGrammarlecteCliPath)) {
        mErrorType = ErrorType::GrammarlectCliNotExist;
        return false;
    }
Laurent Montel's avatar
Laurent Montel committed
148
    return true;
Laurent Montel's avatar
Laurent Montel committed
149
150
}

151
QString GrammalecteResultJob::text() const
Laurent Montel's avatar
Laurent Montel committed
152
153
154
155
{
    return mText;
}

156
void GrammalecteResultJob::setText(const QString &text)
Laurent Montel's avatar
Laurent Montel committed
157
158
159
{
    mText = text;
}