Kgapi

parentreferencecreatejob.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 "parentreferencecreatejob.h"
10#include "driveservice.h"
11#include "parentreference.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 ParentReferenceCreateJob::Private
22{
23public:
24 Private(ParentReferenceCreateJob *parent);
25 void processNext();
26 bool supportsAllDrives = true;
27
28 QString fileId;
29 ParentReferencesList references;
30
31private:
32 ParentReferenceCreateJob *const q;
33};
34
35ParentReferenceCreateJob::Private::Private(ParentReferenceCreateJob *parent)
36 : q(parent)
37{
38}
39
40void ParentReferenceCreateJob::Private::processNext()
41{
42 if (references.isEmpty()) {
43 q->emitFinished();
44 return;
45 }
46
47 const ParentReferencePtr reference = references.takeFirst();
48 QUrl url = DriveService::createParentReferenceUrl(fileId);
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 = ParentReference::toJSON(reference);
57 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
58}
59
60ParentReferenceCreateJob::ParentReferenceCreateJob(const QString &fileId, const QString &parentId, const AccountPtr &account, QObject *parent)
62 , d(new Private(this))
63{
64 d->fileId = fileId;
65 d->references << ParentReferencePtr(new ParentReference(parentId));
66}
67
68ParentReferenceCreateJob::ParentReferenceCreateJob(const QString &fileId, const QStringList &parentsIds, const AccountPtr &account, QObject *parent)
69 : CreateJob(account, parent)
70 , d(new Private(this))
71{
72 d->fileId = fileId;
73 for (const QString &parentId : std::as_const(parentsIds)) {
74 d->references << ParentReferencePtr(new ParentReference(parentId));
75 }
76}
77
78ParentReferenceCreateJob::ParentReferenceCreateJob(const QString &fileId, const ParentReferencePtr &reference, const AccountPtr &account, QObject *parent)
79 : CreateJob(account, parent)
80 , d(new Private(this))
81{
82 d->fileId = fileId;
83 d->references << reference;
84}
85
86ParentReferenceCreateJob::ParentReferenceCreateJob(const QString &fileId, const ParentReferencesList &references, const AccountPtr &account, QObject *parent)
87 : CreateJob(account, parent)
88 , d(new Private(this))
89{
90 d->fileId = fileId;
91 d->references << references;
92}
93
94ParentReferenceCreateJob::~ParentReferenceCreateJob()
95{
96 delete d;
97}
98
99bool ParentReferenceCreateJob::supportsAllDrives() const
100{
101 return d->supportsAllDrives;
102}
103
104void ParentReferenceCreateJob::setSupportsAllDrives(bool supportsAllDrives)
105{
106 d->supportsAllDrives = supportsAllDrives;
107}
108
109void ParentReferenceCreateJob::start()
110{
111 d->processNext();
112}
113
114ObjectsList ParentReferenceCreateJob::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 << ParentReference::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_parentreferencecreatejob.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
ParentReference contains a reference to a file's parent.
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 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.