File indexing completed on 2024-05-05 17:00:22

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "serverinfojob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QNetworkReply>
0013 using namespace RocketChatRestApi;
0014 ServerInfoJob::ServerInfoJob(QObject *parent)
0015     : RestApiAbstractJob(parent)
0016 {
0017 }
0018 
0019 ServerInfoJob::~ServerInfoJob() = default;
0020 
0021 bool ServerInfoJob::start()
0022 {
0023     if (!canStart()) {
0024         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start server info job";
0025         deleteLater();
0026         return false;
0027     }
0028 
0029     submitGetRequest();
0030     addStartRestApiInfo("ServerInfoJob::start");
0031     return true;
0032 }
0033 
0034 // Since 2.0.0 we don't use v1 path. Need to exclude it.
0035 QNetworkRequest ServerInfoJob::request() const
0036 {
0037     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ServerInfo, QString(), mUseDeprecatedVersion);
0038     QNetworkRequest request(url);
0039     if (mForceRequiresAuthentication) {
0040         addAuthRawHeader(request);
0041     }
0042     addRequestAttribute(request, false);
0043 
0044     return request;
0045 }
0046 
0047 bool ServerInfoJob::requireHttpAuthentication() const
0048 {
0049     return mForceRequiresAuthentication;
0050 }
0051 
0052 bool ServerInfoJob::useDeprecatedVersion() const
0053 {
0054     return mUseDeprecatedVersion;
0055 }
0056 
0057 void ServerInfoJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0058 {
0059     const QJsonObject replyObject = replyJson.object();
0060     // TODO send replyObject too. Need by administrator server info.
0061     if (replyObject[QLatin1String("success")].toBool()) {
0062         QString versionStr;
0063         if (mUseDeprecatedVersion) {
0064             const QJsonObject version = replyObject.value(QStringLiteral("info")).toObject();
0065             versionStr = version.value(QStringLiteral("version")).toString();
0066             addLoggerInfo(QByteArrayLiteral("ServerInfoJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0067             Q_EMIT serverInfoDone(versionStr, replyObject);
0068         } else {
0069             versionStr = replyObject.value(QStringLiteral("version")).toString();
0070             addLoggerInfo(QByteArrayLiteral("ServerInfoJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0071             Q_EMIT serverInfoDone(versionStr, replyObject);
0072         }
0073     } else {
0074         Q_EMIT serverInfoFailed(mUseDeprecatedVersion);
0075         addLoggerWarning(QByteArrayLiteral("ServerInfoJob::slotServerInfoFinished: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0076         if (!mUseDeprecatedVersion) {
0077             emitFailedMessage(replyErrorString, replyObject);
0078         }
0079     }
0080 }
0081 
0082 bool ServerInfoJob::forceRequiresAuthentication() const
0083 {
0084     return mForceRequiresAuthentication;
0085 }
0086 
0087 void ServerInfoJob::setForceRequiresAuthentication(bool forceRequiresAuthentication)
0088 {
0089     mForceRequiresAuthentication = forceRequiresAuthentication;
0090 }
0091 
0092 void ServerInfoJob::setUseDeprecatedVersion(bool useDeprecatedVersion)
0093 {
0094     mUseDeprecatedVersion = useDeprecatedVersion;
0095 }
0096 
0097 #include "moc_serverinfojob.cpp"