Kgapi

revisionmodifyjob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "revisionmodifyjob.h"
10#include "driveservice.h"
11#include "revision.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 RevisionModifyJob::Private
21{
22public:
23 Private(RevisionModifyJob *parent);
24 void processNext();
25
26 QString fileId;
27 RevisionsList revisions;
28
29private:
30 RevisionModifyJob *const q;
31};
32
33RevisionModifyJob::Private::Private(RevisionModifyJob *parent)
34 : q(parent)
35{
36}
37
38void RevisionModifyJob::Private::processNext()
39{
40 if (revisions.isEmpty()) {
41 q->emitFinished();
42 return;
43 }
44
45 const RevisionPtr revision = revisions.takeFirst();
46 const QUrl url = DriveService::modifyRevisionUrl(fileId, revision->id());
47
48 QNetworkRequest request(url);
49
50 const QByteArray rawData = Revision::toJSON(revision);
51 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
52}
53
54RevisionModifyJob::RevisionModifyJob(const QString &fileId, const RevisionPtr &revision, const AccountPtr &account, QObject *parent)
56 , d(new Private(this))
57{
58 d->fileId = fileId;
59 d->revisions << revision;
60}
61
62RevisionModifyJob::RevisionModifyJob(const QString &fileId, const RevisionsList &revisions, const AccountPtr &account, QObject *parent)
63 : ModifyJob(account, parent)
64 , d(new Private(this))
65{
66 d->fileId = fileId;
67 d->revisions << revisions;
68}
69
70RevisionModifyJob::~RevisionModifyJob()
71{
72 delete d;
73}
74
75void RevisionModifyJob::start()
76{
77 d->processNext();
78}
79
80ObjectsList RevisionModifyJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
81{
82 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
83 ContentType ct = Utils::stringToContentType(contentType);
85 if (ct == KGAPI2::JSON) {
86 items << Revision::fromJSON(rawData);
87 } else {
89 setErrorString(tr("Invalid response content type"));
91 }
92
93 // Enqueue next item or finish
94 d->processNext();
95
96 return items;
97}
98
99#include "moc_revisionmodifyjob.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 Sat Dec 21 2024 17:00:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.