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 "translatesavesettingsjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 using namespace RocketChatRestApi;
0015 TranslateSaveSettingsJob::TranslateSaveSettingsJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 TranslateSaveSettingsJob::~TranslateSaveSettingsJob() = default;
0021 
0022 bool TranslateSaveSettingsJob::start()
0023 {
0024     if (!canStart()) {
0025         deleteLater();
0026         return false;
0027     }
0028     submitPostRequest(json());
0029     return true;
0030 }
0031 
0032 void TranslateSaveSettingsJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0033 {
0034     const QJsonObject replyObject = replyJson.object();
0035 
0036     if (replyObject[QLatin1String("success")].toBool()) {
0037         addLoggerInfo(QByteArrayLiteral("TranslateSaveSettingsJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0038         Q_EMIT translateSavesettingsDone();
0039     } else {
0040         emitFailedMessage(replyErrorString, replyObject);
0041         addLoggerWarning(QByteArrayLiteral("TranslateSaveSettingsJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0042     }
0043 }
0044 
0045 TranslateSaveSettingsJob::SettingType TranslateSaveSettingsJob::type() const
0046 {
0047     return mType;
0048 }
0049 
0050 void TranslateSaveSettingsJob::setType(RocketChatRestApi::TranslateSaveSettingsJob::SettingType type)
0051 {
0052     mType = type;
0053 }
0054 
0055 QString TranslateSaveSettingsJob::language() const
0056 {
0057     return mLanguage;
0058 }
0059 
0060 void TranslateSaveSettingsJob::setLanguage(const QString &language)
0061 {
0062     mLanguage = language;
0063 }
0064 
0065 bool TranslateSaveSettingsJob::autoTranslate() const
0066 {
0067     return mAutoTranslate;
0068 }
0069 
0070 void TranslateSaveSettingsJob::setAutoTranslate(bool autoTranslate)
0071 {
0072     mAutoTranslate = autoTranslate;
0073 }
0074 
0075 QString TranslateSaveSettingsJob::roomId() const
0076 {
0077     return mRoomId;
0078 }
0079 
0080 void TranslateSaveSettingsJob::setRoomId(const QString &roomId)
0081 {
0082     mRoomId = roomId;
0083 }
0084 
0085 bool TranslateSaveSettingsJob::requireHttpAuthentication() const
0086 {
0087     return true;
0088 }
0089 
0090 bool TranslateSaveSettingsJob::canStart() const
0091 {
0092     if (!RestApiAbstractJob::canStart()) {
0093         return false;
0094     }
0095     if (mRoomId.isEmpty()) {
0096         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "TranslateSaveSettingsJob: mRoomId is empty";
0097         return false;
0098     }
0099     if (mType == LanguageSetting && mLanguage.isEmpty()) {
0100         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "TranslateSaveSettingsJob: mLanguage is empty";
0101         return false;
0102     }
0103     if (mType == Undefined) {
0104         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "TranslateSaveSettingsJob: mType is not defined";
0105         return false;
0106     }
0107     return true;
0108 }
0109 
0110 QNetworkRequest TranslateSaveSettingsJob::request() const
0111 {
0112     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::AutoTranslateSaveSettings);
0113     QNetworkRequest request(url);
0114     addAuthRawHeader(request);
0115     addRequestAttribute(request);
0116     return request;
0117 }
0118 
0119 QJsonDocument TranslateSaveSettingsJob::json() const
0120 {
0121     QJsonObject jsonObj;
0122     jsonObj[QLatin1String("roomId")] = mRoomId;
0123     switch (mType) {
0124     case AutoTranslateSetting:
0125         jsonObj[QLatin1String("field")] = QStringLiteral("autoTranslate");
0126         jsonObj[QLatin1String("value")] = mAutoTranslate;
0127         break;
0128     case LanguageSetting:
0129         jsonObj[QLatin1String("field")] = QStringLiteral("autoTranslateLanguage");
0130         jsonObj[QLatin1String("value")] = mLanguage;
0131         break;
0132     case Undefined:
0133         break;
0134     }
0135     const QJsonDocument postData = QJsonDocument(jsonObj);
0136     return postData;
0137 }
0138 
0139 #include "moc_translatesavesettingsjob.cpp"