Kgapi

drivescreatejob.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 "drivescreatejob.h"
10#include "drives.h"
11#include "driveservice.h"
12#include "utils.h"
13
14#include <QNetworkReply>
15#include <QNetworkRequest>
16#include <QUrlQuery>
17
18namespace
19{
20static const QString RequestIdParam = QStringLiteral("requestId");
21}
22using namespace KGAPI2;
23using namespace KGAPI2::Drive;
24
25class Q_DECL_HIDDEN DrivesCreateJob::Private
26{
27public:
28 Private(DrivesCreateJob *parent);
29 void processNext();
30
31 DrivesList drives;
32 QString requestId;
33
34private:
35 DrivesCreateJob *const q;
36};
37
38DrivesCreateJob::Private::Private(DrivesCreateJob *parent)
39 : q(parent)
40{
41}
42
43void DrivesCreateJob::Private::processNext()
44{
45 if (drives.isEmpty()) {
46 q->emitFinished();
47 return;
48 }
49
50 const DrivesPtr drive = drives.takeFirst();
51
52 QUrl url = DriveService::fetchDrivesUrl();
53
54 QUrlQuery query(url);
55 if (!requestId.isEmpty()) {
56 query.addQueryItem(RequestIdParam, requestId);
57 }
58 url.setQuery(query);
59
60 QNetworkRequest request(url);
61
62 const QByteArray rawData = Drives::toJSON(drive);
63 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
64}
65
66DrivesCreateJob::DrivesCreateJob(const QString &requestId, const DrivesPtr &drive, const AccountPtr &account, QObject *parent)
68 , d(new Private(this))
69{
70 d->requestId = requestId;
71 d->drives << drive;
72}
73
74DrivesCreateJob::DrivesCreateJob(const QString &requestId, const DrivesList &drives, const AccountPtr &account, QObject *parent)
75 : CreateJob(account, parent)
76 , d(new Private(this))
77{
78 d->requestId = requestId;
79 d->drives = drives;
80}
81
82DrivesCreateJob::~DrivesCreateJob() = default;
83
84QString DrivesCreateJob::requestId() const
85{
86 return d->requestId;
87}
88
89void DrivesCreateJob::start()
90{
91 d->processNext();
92}
93
94ObjectsList DrivesCreateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
95{
96 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
97 ContentType ct = Utils::stringToContentType(contentType);
99 if (ct == KGAPI2::JSON) {
100 items << Drives::fromJSON(rawData);
101 } else {
103 setErrorString(tr("Invalid response content type"));
104 emitFinished();
105 return items;
106 }
107
108 // Enqueue next item or finish
109 d->processNext();
110
111 return items;
112}
113
114#include "moc_drivescreatejob.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
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
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)
bool isEmpty() const const
void setQuery(const QString &query, ParsingMode mode)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:59:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.