Kgapi

teamdrivecreatejob.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 "teamdrivecreatejob.h"
10#include "driveservice.h"
11#include "teamdrive.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 TeamdriveCreateJob::Private
26{
27public:
28 Private(TeamdriveCreateJob *parent);
29 void processNext();
30
31 TeamdrivesList teamdrives;
32 QString requestId;
33
34private:
35 TeamdriveCreateJob *const q;
36};
37
38TeamdriveCreateJob::Private::Private(TeamdriveCreateJob *parent)
39 : q(parent)
40{
41}
42
43void TeamdriveCreateJob::Private::processNext()
44{
45 if (teamdrives.isEmpty()) {
46 q->emitFinished();
47 return;
48 }
49
50 const TeamdrivePtr teamdrive = teamdrives.takeFirst();
51
52 QUrl url = DriveService::fetchTeamdrivesUrl();
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 = Teamdrive::toJSON(teamdrive);
63 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
64}
65
66TeamdriveCreateJob::TeamdriveCreateJob(const QString &requestId, const TeamdrivePtr &teamdrive, const AccountPtr &account, QObject *parent)
68 , d(new Private(this))
69{
70 d->requestId = requestId;
71 d->teamdrives << teamdrive;
72}
73
74TeamdriveCreateJob::TeamdriveCreateJob(const QString &requestId, const TeamdrivesList &teamdrives, const AccountPtr &account, QObject *parent)
75 : CreateJob(account, parent)
76 , d(new Private(this))
77{
78 d->requestId = requestId;
79 d->teamdrives = teamdrives;
80}
81
82TeamdriveCreateJob::~TeamdriveCreateJob() = default;
83
84QString TeamdriveCreateJob::requestId() const
85{
86 return d->requestId;
87}
88
89void TeamdriveCreateJob::start()
90{
91 d->processNext();
92}
93
94ObjectsList TeamdriveCreateJob::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 << Teamdrive::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_teamdrivecreatejob.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 Nov 22 2024 12:08:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.