Kgapi

childreferencecreatejob.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 "childreferencecreatejob.h"
10#include "childreference.h"
11#include "driveservice.h"
12#include "utils.h"
13
14#include <QNetworkReply>
15#include <QNetworkRequest>
16#include <QUrlQuery>
17
18using namespace KGAPI2;
19using namespace KGAPI2::Drive;
20
21class Q_DECL_HIDDEN ChildReferenceCreateJob::Private
22{
23public:
24 Private(ChildReferenceCreateJob *parent);
25 void processNext();
26
27 QString folderId;
28 ChildReferencesList references;
29 bool supportsAllDrives = true;
30
31private:
32 ChildReferenceCreateJob *const q;
33};
34
35ChildReferenceCreateJob::Private::Private(ChildReferenceCreateJob *parent)
36 : q(parent)
37{
38}
39
40void ChildReferenceCreateJob::Private::processNext()
41{
42 if (references.isEmpty()) {
43 q->emitFinished();
44 return;
45 }
46
47 const ChildReferencePtr reference = references.takeFirst();
48 QUrl url = DriveService::createChildReference(folderId);
49
50 QUrlQuery withDriveSupportQuery(url);
51 withDriveSupportQuery.addQueryItem(QStringLiteral("supportsAllDrives"), Utils::bool2Str(supportsAllDrives));
52 url.setQuery(withDriveSupportQuery);
53
54 QNetworkRequest request(url);
55
56 const QByteArray rawData = ChildReference::toJSON(reference);
57 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
58}
59
60ChildReferenceCreateJob::ChildReferenceCreateJob(const QString &folderId, const QString &childId, const AccountPtr &account, QObject *parent)
62 , d(new Private(this))
63{
64 d->folderId = folderId;
65 d->references << ChildReferencePtr(new ChildReference(childId));
66}
67
68ChildReferenceCreateJob::ChildReferenceCreateJob(const QString &folderId, const QStringList &childrenIds, const AccountPtr &account, QObject *parent)
69 : CreateJob(account, parent)
70 , d(new Private(this))
71{
72 d->folderId = folderId;
73 for (const QString &childId : std::as_const(childrenIds)) {
74 d->references << ChildReferencePtr(new ChildReference(childId));
75 }
76}
77
78ChildReferenceCreateJob::ChildReferenceCreateJob(const QString &folderId, const ChildReferencePtr &reference, const AccountPtr &account, QObject *parent)
79 : CreateJob(account, parent)
80 , d(new Private(this))
81{
82 d->folderId = folderId;
83 d->references << reference;
84}
85
86ChildReferenceCreateJob::ChildReferenceCreateJob(const QString &folderId, const ChildReferencesList &references, const AccountPtr &account, QObject *parent)
87 : CreateJob(account, parent)
88 , d(new Private(this))
89{
90 d->folderId = folderId;
91 d->references << references;
92}
93
94ChildReferenceCreateJob::~ChildReferenceCreateJob()
95{
96 delete d;
97}
98
99bool ChildReferenceCreateJob::supportsAllDrives() const
100{
101 return d->supportsAllDrives;
102}
103
104void ChildReferenceCreateJob::setSupportsAllDrives(bool supportsAllDrives)
105{
106 d->supportsAllDrives = supportsAllDrives;
107}
108
109void ChildReferenceCreateJob::start()
110{
111 d->processNext();
112}
113
114ObjectsList ChildReferenceCreateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
115{
116 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
117 ContentType ct = Utils::stringToContentType(contentType);
119 if (ct == KGAPI2::JSON) {
120 items << ChildReference::fromJSON(rawData);
121 } else {
123 setErrorString(tr("Invalid response content type"));
124 emitFinished();
125 }
126
127 // Enqueue next item or finish
128 d->processNext();
129
130 return items;
131}
132
133#include "moc_childreferencecreatejob.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
ChildReference contains a reference to a folder's child.
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)
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 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.