Kgapi

teamdrivemodifyjob.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 "teamdrivemodifyjob.h"
10#include "driveservice.h"
11#include "teamdrive.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 TeamdriveModifyJob::Private
21{
22public:
23 Private(TeamdriveModifyJob *parent);
24 void processNext();
25
26 TeamdrivesList teamdrives;
27
28private:
29 TeamdriveModifyJob *const q;
30};
31
32TeamdriveModifyJob::Private::Private(TeamdriveModifyJob *parent)
33 : q(parent)
34{
35}
36
37void TeamdriveModifyJob::Private::processNext()
38{
39 if (teamdrives.isEmpty()) {
40 q->emitFinished();
41 return;
42 }
43
44 const TeamdrivePtr teamdrive = teamdrives.takeFirst();
45 const QUrl url = DriveService::fetchTeamdriveUrl(teamdrive->id());
46
47 QNetworkRequest request(url);
48
49 const QByteArray rawData = Teamdrive::toJSON(teamdrive);
50 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
51}
52
53TeamdriveModifyJob::TeamdriveModifyJob(const TeamdrivePtr &teamdrive, const AccountPtr &account, QObject *parent)
55 , d(new Private(this))
56{
57 d->teamdrives << teamdrive;
58}
59
60TeamdriveModifyJob::TeamdriveModifyJob(const TeamdrivesList &teamdrives, const AccountPtr &account, QObject *parent)
61 : ModifyJob(account, parent)
62 , d(new Private(this))
63{
64 d->teamdrives << teamdrives;
65}
66
67TeamdriveModifyJob::~TeamdriveModifyJob() = default;
68
69void TeamdriveModifyJob::start()
70{
71 d->processNext();
72}
73
74ObjectsList TeamdriveModifyJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
75{
76 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
77 ContentType ct = Utils::stringToContentType(contentType);
79 if (ct == KGAPI2::JSON) {
80 items << Teamdrive::fromJSON(rawData);
81 } else {
83 setErrorString(tr("Invalid response content type"));
85 return items;
86 }
87
88 // Enqueue next item or finish
89 d->processNext();
90
91 return items;
92}
93
94#include "moc_teamdrivemodifyjob.cpp"
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
Abstract superclass for all jobs that somehow modify resources on Google.
Definition modifyjob.h:25
ObjectsList items() const
Definition modifyjob.cpp:43
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 20 2024 11:59:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.