Kgapi

driveshidejob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2019 David Barchiesi <david@barchie.si>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "driveshidejob.h"
10#include "drives.h"
11#include "driveservice.h"
12#include "utils.h"
13
14#include <QNetworkReply>
15#include <QNetworkRequest>
16
17using namespace KGAPI2;
18using namespace KGAPI2::Drive;
19
20class Q_DECL_HIDDEN DrivesHideJob::Private
21{
22public:
23 Private(DrivesHideJob *parent);
24 void processNext();
25
26 bool hide = false;
27
28 DrivesList drives;
29
30private:
31 DrivesHideJob *const q;
32};
33
34DrivesHideJob::Private::Private(DrivesHideJob *parent)
35 : q(parent)
36{
37}
38
39void DrivesHideJob::Private::processNext()
40{
41 if (drives.isEmpty()) {
42 q->emitFinished();
43 return;
44 }
45
46 const DrivesPtr drive = drives.takeFirst();
47
48 QUrl url = DriveService::hideDrivesUrl(drive->id(), hide);
49
50 QNetworkRequest request(url);
51
52 // A hide request doesn't have a body
53 q->enqueueRequest(request, nullptr, QStringLiteral("application/json"));
54}
55
56DrivesHideJob::DrivesHideJob(const DrivesPtr &drive, bool hide, const AccountPtr &account, QObject *parent)
58 , d(new Private(this))
59{
60 d->drives << drive;
61 d->hide = hide;
62}
63
64DrivesHideJob::DrivesHideJob(const DrivesList &drives, bool hide, const AccountPtr &account, QObject *parent)
65 : CreateJob(account, parent)
66 , d(new Private(this))
67{
68 d->drives = drives;
69 d->hide = hide;
70}
71
72DrivesHideJob::~DrivesHideJob() = default;
73
74void DrivesHideJob::start()
75{
76 d->processNext();
77}
78
79ObjectsList DrivesHideJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
80{
81 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
82 ContentType ct = Utils::stringToContentType(contentType);
84 if (ct == KGAPI2::JSON) {
85 items << Drives::fromJSON(rawData);
86 } else {
88 setErrorString(tr("Invalid response content type"));
90 return items;
91 }
92
93 // Enqueue next item or finish
94 d->processNext();
95
96 return items;
97}
98
99#include "moc_driveshidejob.cpp"
Abstract superclass for all jobs that create new objects on the server.
Definition createjob.h:26
virtual ObjectsList items() const
Definition createjob.cpp:40
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition job.cpp:401
AccountPtr account() const
Returns account used to authenticate requests.
Definition job.cpp:436
virtual void emitFinished()
Emits Job::finished() signal.
Definition job.cpp:493
void setError(KGAPI2::Error error)
Set job error to error.
Definition job.cpp:386
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition job.cpp:513
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
ContentType
Definition types.h:210
bool isEmpty() const const
value_type takeFirst()
QVariant header(QNetworkRequest::KnownHeaders header) const const
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.