File indexing completed on 2024-05-12 05:22:18

0001 /*
0002  * This file is part of LibKGAPI library
0003  *
0004  * SPDX-FileCopyrightText: 2019 David Barchiesi <david@barchie.si>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "driveshidejob.h"
0010 #include "drives.h"
0011 #include "driveservice.h"
0012 #include "utils.h"
0013 
0014 #include <QNetworkReply>
0015 #include <QNetworkRequest>
0016 
0017 using namespace KGAPI2;
0018 using namespace KGAPI2::Drive;
0019 
0020 class Q_DECL_HIDDEN DrivesHideJob::Private
0021 {
0022 public:
0023     Private(DrivesHideJob *parent);
0024     void processNext();
0025 
0026     bool hide = false;
0027 
0028     DrivesList drives;
0029 
0030 private:
0031     DrivesHideJob *const q;
0032 };
0033 
0034 DrivesHideJob::Private::Private(DrivesHideJob *parent)
0035     : q(parent)
0036 {
0037 }
0038 
0039 void DrivesHideJob::Private::processNext()
0040 {
0041     if (drives.isEmpty()) {
0042         q->emitFinished();
0043         return;
0044     }
0045 
0046     const DrivesPtr drive = drives.takeFirst();
0047 
0048     QUrl url = DriveService::hideDrivesUrl(drive->id(), hide);
0049 
0050     QNetworkRequest request(url);
0051 
0052     // A hide request doesn't have a body
0053     q->enqueueRequest(request, nullptr, QStringLiteral("application/json"));
0054 }
0055 
0056 DrivesHideJob::DrivesHideJob(const DrivesPtr &drive, bool hide, const AccountPtr &account, QObject *parent)
0057     : CreateJob(account, parent)
0058     , d(new Private(this))
0059 {
0060     d->drives << drive;
0061     d->hide = hide;
0062 }
0063 
0064 DrivesHideJob::DrivesHideJob(const DrivesList &drives, bool hide, const AccountPtr &account, QObject *parent)
0065     : CreateJob(account, parent)
0066     , d(new Private(this))
0067 {
0068     d->drives = drives;
0069     d->hide = hide;
0070 }
0071 
0072 DrivesHideJob::~DrivesHideJob() = default;
0073 
0074 void DrivesHideJob::start()
0075 {
0076     d->processNext();
0077 }
0078 
0079 ObjectsList DrivesHideJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
0080 {
0081     const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
0082     ContentType ct = Utils::stringToContentType(contentType);
0083     ObjectsList items;
0084     if (ct == KGAPI2::JSON) {
0085         items << Drives::fromJSON(rawData);
0086     } else {
0087         setError(KGAPI2::InvalidResponse);
0088         setErrorString(tr("Invalid response content type"));
0089         emitFinished();
0090         return items;
0091     }
0092 
0093     // Enqueue next item or finish
0094     d->processNext();
0095 
0096     return items;
0097 }
0098 
0099 #include "moc_driveshidejob.cpp"