KTextAddons

languagetoolresultjob.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 "languagetoolresultjob.h"
8#include "textgrammarcheck_debug.h"
9
10#include <QNetworkAccessManager>
11#include <QNetworkReply>
12using namespace TextGrammarCheck;
13LanguageToolResultJob::LanguageToolResultJob(QObject *parent)
14 : QObject(parent)
15{
16}
17
18LanguageToolResultJob::~LanguageToolResultJob() = default;
19
20static bool hasNotEmptyText(const QString &text)
21{
22 for (int i = 0; i < text.length(); ++i) {
23 if (!text.at(i).isSpace()) {
24 return true;
25 }
26 }
27 return false;
28}
29
30bool LanguageToolResultJob::canStart() const
31{
32 return canStartError() == LanguageToolResultJob::JobError::NotError;
33}
34
35LanguageToolResultJob::JobError LanguageToolResultJob::canStartError() const
36{
37 if (!mNetworkAccessManager) {
38 return LanguageToolResultJob::JobError::NetworkManagerNotDefined;
39 }
40 if (!hasNotEmptyText(mText)) {
41 return LanguageToolResultJob::JobError::EmptyText;
42 }
43 if (mUrl.isEmpty()) {
44 return LanguageToolResultJob::JobError::UrlNotDefined;
45 }
46 if (mLanguage.isEmpty()) {
47 return LanguageToolResultJob::JobError::LanguageNotDefined;
48 }
49 return LanguageToolResultJob::JobError::NotError;
50}
51
52void LanguageToolResultJob::start()
53{
54 const LanguageToolResultJob::JobError errorType = canStartError();
55 switch (errorType) {
56 case LanguageToolResultJob::JobError::EmptyText:
57 return;
58 case LanguageToolResultJob::JobError::UrlNotDefined:
59 case LanguageToolResultJob::JobError::NetworkManagerNotDefined:
60 case LanguageToolResultJob::JobError::LanguageNotDefined:
61 qCWarning(TEXTGRAMMARCHECK_LOG) << "Impossible to start language tool";
62 return;
63 case LanguageToolResultJob::JobError::NotError:
64 break;
65 }
67 addRequestAttribute(request);
68 const QByteArray ba = "text=" + mText.toUtf8() + "&language=" + mLanguage.toLatin1();
69 QNetworkReply *reply = mNetworkAccessManager->post(request, ba);
70 connect(reply, &QNetworkReply::finished, this, &LanguageToolResultJob::slotCheckGrammarFinished);
71 connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &LanguageToolResultJob::slotFinish);
72}
73
74void LanguageToolResultJob::slotFinish(QNetworkReply *reply)
75{
76 if (reply->error() != QNetworkReply::NoError) {
77 qCWarning(TEXTGRAMMARCHECK_LOG) << " Error reply - " << reply->errorString();
78 Q_EMIT error(reply->errorString());
79 }
80}
81
82void LanguageToolResultJob::slotCheckGrammarFinished()
83{
85 if (reply) {
86 const QByteArray data = reply->readAll();
87 Q_EMIT finished(QString::fromUtf8(data));
88 reply->deleteLater();
89 }
91}
92
93void LanguageToolResultJob::addRequestAttribute(QNetworkRequest &request) const
94{
95 request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/x-www-form-urlencoded"));
96}
97
98QString LanguageToolResultJob::language() const
99{
100 return mLanguage;
101}
102
103void LanguageToolResultJob::setLanguage(const QString &language)
104{
105 mLanguage = language;
106}
107
108QString LanguageToolResultJob::url() const
109{
110 return mUrl;
111}
112
113void LanguageToolResultJob::setUrl(const QString &url)
114{
115 mUrl = url;
116}
117
118QStringList LanguageToolResultJob::arguments() const
119{
120 return mArguments;
121}
122
123void LanguageToolResultJob::setArguments(const QStringList &arguments)
124{
125 mArguments = arguments;
126}
127
128QNetworkAccessManager *LanguageToolResultJob::networkAccessManager() const
129{
130 return mNetworkAccessManager;
131}
132
133void LanguageToolResultJob::setNetworkAccessManager(QNetworkAccessManager *networkAccessManager)
134{
135 mNetworkAccessManager = networkAccessManager;
136}
137
138QString LanguageToolResultJob::text() const
139{
140 return mText;
141}
142
143void LanguageToolResultJob::setText(const QString &text)
144{
145 mText = text;
146}
147
148#include "moc_languagetoolresultjob.cpp"
bool isSpace(char32_t ucs4)
QString errorString() const const
QByteArray readAll()
void finished(QNetworkReply *reply)
QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)
NetworkError error() const const
void setHeader(KnownHeaders header, const QVariant &value)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
T qobject_cast(QObject *object)
QObject * sender() const const
const QChar at(qsizetype position) const const
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
qsizetype length() const const
QByteArray toLatin1() const const
QByteArray toUtf8() const const
QUrl fromUserInput(const QString &userInput, const QString &workingDirectory, UserInputResolutionOptions options)
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.