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 "spotlightjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QNetworkReply>
0013 #include <QUrlQuery>
0014 using namespace RocketChatRestApi;
0015 SpotlightJob::SpotlightJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 SpotlightJob::~SpotlightJob() = default;
0021 
0022 bool SpotlightJob::start()
0023 {
0024     if (!canStart()) {
0025         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start SpotlightJob job";
0026         deleteLater();
0027         return false;
0028     }
0029     submitGetRequest();
0030     addStartRestApiInfo(QByteArrayLiteral("SpotlightJob start"));
0031     return true;
0032 }
0033 
0034 void SpotlightJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0035 {
0036     const QJsonObject replyObject = replyJson.object();
0037     if (replyObject[QLatin1String("success")].toBool()) {
0038         addLoggerInfo(QByteArrayLiteral("SpotlightJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0039         Q_EMIT spotlightDone(replyObject);
0040     } else {
0041         emitFailedMessage(replyErrorString, replyObject);
0042         addLoggerWarning(QByteArrayLiteral("SpotlightJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0043     }
0044 }
0045 
0046 QString SpotlightJob::searchPattern() const
0047 {
0048     return mSearchPattern;
0049 }
0050 
0051 void SpotlightJob::setSearchPattern(const QString &searchPattern)
0052 {
0053     mSearchPattern = searchPattern;
0054 }
0055 
0056 bool SpotlightJob::requireHttpAuthentication() const
0057 {
0058     return true;
0059 }
0060 
0061 QNetworkRequest SpotlightJob::request() const
0062 {
0063     QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::Spotlight);
0064     QUrlQuery queryUrl;
0065     queryUrl.addQueryItem(QStringLiteral("query"), mSearchPattern);
0066     url.setQuery(queryUrl);
0067     QNetworkRequest req(url);
0068     addAuthRawHeader(req);
0069     return req;
0070 }
0071 
0072 bool SpotlightJob::canStart() const
0073 {
0074     if (mSearchPattern.trimmed().isEmpty()) {
0075         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "SpotlightJob: searchpattern is empty";
0076         return false;
0077     }
0078     if (!RestApiAbstractJob::canStart()) {
0079         return false;
0080     }
0081     return true;
0082 }
0083 
0084 #include "moc_spotlightjob.cpp"