Kgapi

permissionmodifyjob.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 "permissionmodifyjob.h"
10#include "driveservice.h"
11#include "permission.h"
12#include "utils.h"
13
14#include <QNetworkReply>
15#include <QNetworkRequest>
16#include <QUrlQuery>
17
18using namespace KGAPI2;
19using namespace KGAPI2::Drive;
20
21namespace
22{
23static constexpr bool removeExpirationDefault = false;
24static constexpr bool transferOwnershipDefault = false;
25static constexpr bool useDomainAdminAccessDefault = false;
26}
27
28class Q_DECL_HIDDEN PermissionModifyJob::Private
29{
30public:
31 Private(PermissionModifyJob *parent);
32 void processNext();
33
34 QString fileId;
35 PermissionsList permissions;
36 bool supportsAllDrives;
37 bool removeExpiration;
38 bool transferOwnership;
39 bool useDomainAdminAccess;
40
41private:
42 PermissionModifyJob *const q;
43};
44
45PermissionModifyJob::Private::Private(PermissionModifyJob *parent)
46 : supportsAllDrives(true)
47 , removeExpiration(removeExpirationDefault)
48 , transferOwnership(transferOwnershipDefault)
49 , useDomainAdminAccess(useDomainAdminAccessDefault)
50 , q(parent)
51{
52}
53
54void PermissionModifyJob::Private::processNext()
55{
56 if (permissions.isEmpty()) {
57 q->emitFinished();
58 return;
59 }
60
61 const PermissionPtr permission = permissions.takeFirst();
62 QUrl url = DriveService::modifyPermissionUrl(fileId, permission->id());
63
64 QUrlQuery query(url);
65 query.addQueryItem(QStringLiteral("supportsAllDrives"), Utils::bool2Str(supportsAllDrives));
66
67 if (removeExpiration != removeExpirationDefault) {
68 query.addQueryItem(QStringLiteral("removeExpiration"), Utils::bool2Str(removeExpiration));
69 }
70
71 if (transferOwnership != transferOwnershipDefault) {
72 query.addQueryItem(QStringLiteral("transferOwnership"), Utils::bool2Str(transferOwnership));
73 }
74
75 if (useDomainAdminAccess != useDomainAdminAccessDefault) {
76 query.addQueryItem(QStringLiteral("useDomainAdminAccess"), Utils::bool2Str(useDomainAdminAccess));
77 }
78
79 url.setQuery(query);
80 QNetworkRequest request(url);
81
82 const QByteArray rawData = Permission::toJSON(permission);
83 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
84}
85
86PermissionModifyJob::PermissionModifyJob(const QString &fileId, const PermissionPtr &permission, const AccountPtr &account, QObject *parent)
88 , d(new Private(this))
89{
90 d->fileId = fileId;
91 d->permissions << permission;
92}
93
94PermissionModifyJob::PermissionModifyJob(const QString &fileId, const PermissionsList &permissions, const AccountPtr &account, QObject *parent)
95 : ModifyJob(account, parent)
96 , d(new Private(this))
97{
98 d->fileId = fileId;
99 d->permissions << permissions;
100}
101
102PermissionModifyJob::~PermissionModifyJob() = default;
103
104bool PermissionModifyJob::removeExpiration() const
105{
106 return d->removeExpiration;
107}
108
109void PermissionModifyJob::setRemoveExpiration(bool removeExpiration)
110{
111 d->removeExpiration = removeExpiration;
112}
113
114bool PermissionModifyJob::supportsAllDrives() const
115{
116 return d->supportsAllDrives;
117}
118
119void PermissionModifyJob::setSupportsAllDrives(bool supportsAllDrives)
120{
121 d->supportsAllDrives = supportsAllDrives;
122}
123
124bool PermissionModifyJob::transferOwnership() const
125{
126 return d->transferOwnership;
127}
128
129void PermissionModifyJob::setTransferOwnership(bool transferOwnership)
130{
131 d->transferOwnership = transferOwnership;
132}
133
134bool PermissionModifyJob::useDomainAdminAccess() const
135{
136 return d->useDomainAdminAccess;
137}
138
139void PermissionModifyJob::setUseDomainAdminAccess(bool useDomainAdminAccess)
140{
141 d->useDomainAdminAccess = useDomainAdminAccess;
142}
143
144void PermissionModifyJob::start()
145{
146 d->processNext();
147}
148
149ObjectsList PermissionModifyJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
150{
151 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
152 ContentType ct = Utils::stringToContentType(contentType);
154 if (ct == KGAPI2::JSON) {
155 items << Permission::fromJSON(rawData);
156 } else {
158 setErrorString(tr("Invalid response content type"));
159 emitFinished();
160 }
161
162 // Enqueue next item or finish
163 d->processNext();
164
165 return items;
166}
167
168#include "moc_permissionmodifyjob.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
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 Mon Nov 4 2024 16:36:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.