File indexing completed on 2024-12-22 04:45:13

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "getsupportedlanguagesjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 #include <KLocalizedString>
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 #include <QUrlQuery>
0015 using namespace RocketChatRestApi;
0016 GetSupportedLanguagesJob::GetSupportedLanguagesJob(QObject *parent)
0017     : RestApiAbstractJob(parent)
0018 {
0019 }
0020 
0021 GetSupportedLanguagesJob::~GetSupportedLanguagesJob() = default;
0022 
0023 bool GetSupportedLanguagesJob::requireHttpAuthentication() const
0024 {
0025     return true;
0026 }
0027 
0028 bool GetSupportedLanguagesJob::start()
0029 {
0030     if (!canStart()) {
0031         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start GetSupportedLanguagesJob";
0032         deleteLater();
0033         return false;
0034     }
0035     submitGetRequest();
0036     addStartRestApiInfo(QByteArrayLiteral("GetSupportedLanguagesJob: get message starting"));
0037     return true;
0038 }
0039 
0040 void GetSupportedLanguagesJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0041 {
0042     const QJsonObject replyObject = replyJson.object();
0043     if (replyObject[QLatin1String("success")].toBool()) {
0044         addLoggerInfo(QByteArrayLiteral("GetSupportedLanguagesJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0045         Q_EMIT getSupportedLanguagesDone(replyObject);
0046     } else {
0047         emitFailedMessage(replyErrorString, replyObject);
0048         addLoggerWarning(QByteArrayLiteral("GetSupportedLanguagesJob: Problem when we tried to GetSupportedLanguages : ")
0049                          + replyJson.toJson(QJsonDocument::Indented));
0050     }
0051 }
0052 
0053 bool GetSupportedLanguagesJob::needTargetLanguage() const
0054 {
0055     return mNeedTargetLanguage;
0056 }
0057 
0058 void GetSupportedLanguagesJob::setNeedTargetLanguage(bool newNeedTargetLanguage)
0059 {
0060     mNeedTargetLanguage = newNeedTargetLanguage;
0061 }
0062 
0063 QNetworkRequest GetSupportedLanguagesJob::request() const
0064 {
0065     QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::AutoTranslateGetSupportedLanguages);
0066     if (mNeedTargetLanguage) {
0067         QUrlQuery queryUrl;
0068         queryUrl.addQueryItem(QStringLiteral("targetLanguage"), QStringLiteral("en"));
0069         url.setQuery(queryUrl);
0070     }
0071     QNetworkRequest request(url);
0072     addAuthRawHeader(request);
0073     addRequestAttribute(request, false);
0074     return request;
0075 }
0076 
0077 bool GetSupportedLanguagesJob::canStart() const
0078 {
0079     if (!RestApiAbstractJob::canStart()) {
0080         return false;
0081     }
0082     return true;
0083 }
0084 
0085 QString GetSupportedLanguagesJob::errorMessage(const QString &str, const QJsonObject &detail)
0086 {
0087     if (str == QLatin1String("invalid-params")) {
0088         return i18n("Invalid Parameters");
0089     }
0090     return RestApiAbstractJob::errorMessage(str, detail);
0091 }
0092 
0093 #include "moc_getsupportedlanguagesjob.cpp"