KTextAddons

languagetoolgetlistoflanguagejob.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 "languagetoolgetlistoflanguagejob.h"
8#include "textgrammarcheck_debug.h"
9
10#include <QNetworkAccessManager>
11#include <QNetworkReply>
12using namespace TextGrammarCheck;
13LanguageToolGetListOfLanguageJob::LanguageToolGetListOfLanguageJob(QObject *parent)
14 : QObject(parent)
15{
16}
17
18LanguageToolGetListOfLanguageJob::~LanguageToolGetListOfLanguageJob() = default;
19
20bool LanguageToolGetListOfLanguageJob::canStart() const
21{
22 if (!mNetworkAccessManager || mUrl.isEmpty()) {
23 return false;
24 }
25 return true;
26}
27
28void LanguageToolGetListOfLanguageJob::start()
29{
30 if (!canStart()) {
31 qCWarning(TEXTGRAMMARCHECK_LOG) << "Impossible to start LanguageToolGetListOfLanguageJob";
33 return;
34 }
36 addRequestAttribute(request);
37 QNetworkReply *reply = mNetworkAccessManager->get(request);
38 connect(reply, &QNetworkReply::finished, this, &LanguageToolGetListOfLanguageJob::slotCheckListOfLanguagesFinished);
39 connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &LanguageToolGetListOfLanguageJob::slotFinish);
40}
41
42void LanguageToolGetListOfLanguageJob::slotFinish(QNetworkReply *reply)
43{
44 if (reply->error() != QNetworkReply::NoError) {
45 qCWarning(TEXTGRAMMARCHECK_LOG) << " Error reply - " << reply->errorString();
46 Q_EMIT error(reply->errorString());
47 }
48}
49
50void LanguageToolGetListOfLanguageJob::slotCheckListOfLanguagesFinished()
51{
53 if (reply) {
54 const QByteArray data = reply->readAll();
55 Q_EMIT finished(QString::fromUtf8(data));
56 reply->deleteLater();
57 }
59}
60
61void LanguageToolGetListOfLanguageJob::addRequestAttribute(QNetworkRequest &request) const
62{
63 request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/x-www-form-urlencoded"));
64}
65
66QString LanguageToolGetListOfLanguageJob::listOfLanguagePath() const
67{
68 return mListOfLanguagePath;
69}
70
71void LanguageToolGetListOfLanguageJob::setListOfLanguagePath(const QString &getListOfLanguagePath)
72{
73 mListOfLanguagePath = getListOfLanguagePath;
74}
75
76QNetworkAccessManager *LanguageToolGetListOfLanguageJob::networkAccessManager() const
77{
78 return mNetworkAccessManager;
79}
80
81void LanguageToolGetListOfLanguageJob::setNetworkAccessManager(QNetworkAccessManager *networkAccessManager)
82{
83 mNetworkAccessManager = networkAccessManager;
84}
85
86QString LanguageToolGetListOfLanguageJob::url() const
87{
88 return mUrl;
89}
90
91void LanguageToolGetListOfLanguageJob::setUrl(const QString &url)
92{
93 mUrl = url;
94}
95
96#include "moc_languagetoolgetlistoflanguagejob.cpp"
QString errorString() const const
QByteArray readAll()
void finished(QNetworkReply *reply)
QNetworkReply * get(const QNetworkRequest &request)
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
QString fromUtf8(QByteArrayView str)
bool isEmpty() 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.