Kgapi

drivesmodifyjob.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 "drivesmodifyjob.h"
10#include "debug.h"
11#include "drives.h"
12#include "driveservice.h"
13#include "utils.h"
14
15#include <QNetworkReply>
16#include <QNetworkRequest>
17#include <QUrlQuery>
18
19namespace
20{
21static const QString UseDomainAdminAccessAttr = QStringLiteral("useDomainAdminAccess");
22}
23using namespace KGAPI2;
24using namespace KGAPI2::Drive;
25
26class Q_DECL_HIDDEN DrivesModifyJob::Private
27{
28public:
29 Private(DrivesModifyJob *parent);
30 void processNext();
31
32 bool useDomainAdminAccess = false;
33
34 DrivesList drives;
35
36private:
37 DrivesModifyJob *const q;
38};
39
40DrivesModifyJob::Private::Private(DrivesModifyJob *parent)
41 : q(parent)
42{
43}
44
45void DrivesModifyJob::setUseDomainAdminAccess(bool useDomainAdminAccess)
46{
47 if (isRunning()) {
48 qCWarning(KGAPIDebug) << "Can't modify useDomainAdminAccess property when job is running";
49 return;
50 }
51
52 d->useDomainAdminAccess = useDomainAdminAccess;
53}
54
55bool DrivesModifyJob::useDomainAdminAccess() const
56{
57 return d->useDomainAdminAccess;
58}
59
60void DrivesModifyJob::Private::processNext()
61{
62 if (drives.isEmpty()) {
63 q->emitFinished();
64 return;
65 }
66
67 const DrivesPtr drive = drives.takeFirst();
68 QUrl url = DriveService::fetchDrivesUrl(drive->id());
69
70 QUrlQuery query(url);
71 if (useDomainAdminAccess != false) {
72 query.addQueryItem(UseDomainAdminAccessAttr, Utils::bool2Str(useDomainAdminAccess));
73 }
74 url.setQuery(query);
75
76 QNetworkRequest request(url);
77
78 const QByteArray rawData = Drives::toJSON(drive);
79 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
80}
81
82DrivesModifyJob::DrivesModifyJob(const DrivesPtr &drive, const AccountPtr &account, QObject *parent)
84 , d(new Private(this))
85{
86 d->drives << drive;
87}
88
89DrivesModifyJob::DrivesModifyJob(const DrivesList &drives, const AccountPtr &account, QObject *parent)
90 : ModifyJob(account, parent)
91 , d(new Private(this))
92{
93 d->drives << drives;
94}
95
96DrivesModifyJob::~DrivesModifyJob() = default;
97
98void DrivesModifyJob::start()
99{
100 d->processNext();
101}
102
103ObjectsList DrivesModifyJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
104{
105 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
106 ContentType ct = Utils::stringToContentType(contentType);
108 if (ct == KGAPI2::JSON) {
109 items << Drives::fromJSON(rawData);
110 } else {
112 setErrorString(tr("Invalid response content type"));
113 emitFinished();
114 return items;
115 }
116
117 // Enqueue next item or finish
118 d->processNext();
119
120 return items;
121}
122
123#include "moc_drivesmodifyjob.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
bool isRunning
Whether the job is running.
Definition job.h:67
Abstract superclass for all jobs that somehow modify resources on Google.
Definition modifyjob.h:25
ObjectsList items() const
Definition modifyjob.cpp:43
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)
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.