Kgapi

permissioncreatejob.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 "permissioncreatejob.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 sendNotificationEmailsDefault = true;
24static constexpr bool useDomainAdminAccessDefault = false;
25}
26
27class Q_DECL_HIDDEN PermissionCreateJob::Private
28{
29public:
30 Private(PermissionCreateJob *parent);
31 void processNext();
32
33 PermissionsList permissions;
34 QString fileId;
35 QString emailMessage;
36 bool sendNotificationEmails;
37 bool supportsAllDrives = true;
38 bool useDomainAdminAccess;
39
40private:
41 PermissionCreateJob *const q;
42};
43
44PermissionCreateJob::Private::Private(PermissionCreateJob *parent)
45 : sendNotificationEmails(sendNotificationEmailsDefault)
46 , useDomainAdminAccess(useDomainAdminAccessDefault)
47 , q(parent)
48{
49}
50
51void PermissionCreateJob::Private::processNext()
52{
53 if (permissions.isEmpty()) {
54 q->emitFinished();
55 return;
56 }
57
58 const PermissionPtr permission = permissions.takeFirst();
59
60 QUrl url = DriveService::createPermissionUrl(fileId);
61
62 QUrlQuery query(url);
63 query.addQueryItem(QStringLiteral("supportsAllDrives"), Utils::bool2Str(supportsAllDrives));
64
65 if (sendNotificationEmails != sendNotificationEmailsDefault) {
66 query.addQueryItem(QStringLiteral("sendNotificationEmails"), Utils::bool2Str(sendNotificationEmails));
67 }
68
69 if (!emailMessage.isEmpty()) {
70 query.addQueryItem(QStringLiteral("emailMessage"), emailMessage);
71 }
72
73 if (useDomainAdminAccess != useDomainAdminAccessDefault) {
74 query.addQueryItem(QStringLiteral("useDomainAdminAccess"), Utils::bool2Str(useDomainAdminAccess));
75 }
76
77 url.setQuery(query);
78 QNetworkRequest request(url);
79
80 const QByteArray rawData = Permission::toJSON(permission);
81 q->enqueueRequest(request, rawData, QStringLiteral("application/json"));
82}
83
84PermissionCreateJob::PermissionCreateJob(const QString &fileId, const PermissionPtr &permission, const AccountPtr &account, QObject *parent)
86 , d(new Private(this))
87{
88 d->fileId = fileId;
89 d->permissions << permission;
90}
91
92PermissionCreateJob::PermissionCreateJob(const QString &fileId, const PermissionsList &permissions, const AccountPtr &account, QObject *parent)
93 : CreateJob(account, parent)
94 , d(new Private(this))
95{
96 d->fileId = fileId;
97 d->permissions = permissions;
98}
99
100PermissionCreateJob::~PermissionCreateJob() = default;
101
102QString PermissionCreateJob::emailMessage() const
103{
104 return d->emailMessage;
105}
106
107void PermissionCreateJob::setEmailMessage(const QString &emailMessage)
108{
109 d->emailMessage = emailMessage;
110}
111
112bool PermissionCreateJob::sendNotificationEmails() const
113{
114 return d->sendNotificationEmails;
115}
116
117void PermissionCreateJob::setSendNotificationEmails(bool sendNotificationEmails)
118{
119 d->sendNotificationEmails = sendNotificationEmails;
120}
121
122bool PermissionCreateJob::supportsAllDrives() const
123{
124 return d->supportsAllDrives;
125}
126
127void PermissionCreateJob::setSupportsAllDrives(bool supportsAllDrives)
128{
129 d->supportsAllDrives = supportsAllDrives;
130}
131
132bool PermissionCreateJob::useDomainAdminAccess() const
133{
134 return d->useDomainAdminAccess;
135}
136
137void PermissionCreateJob::setUseDomainAdminAccess(bool useDomainAdminAccess)
138{
139 d->useDomainAdminAccess = useDomainAdminAccess;
140}
141
142void PermissionCreateJob::start()
143{
144 d->processNext();
145}
146
147ObjectsList PermissionCreateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
148{
149 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
150 ContentType ct = Utils::stringToContentType(contentType);
152 if (ct == KGAPI2::JSON) {
153 items << Permission::fromJSON(rawData);
154 } else {
156 setErrorString(tr("Invalid response content type"));
157 emitFinished();
158 }
159
160 // Enqueue next item or finish
161 d->processNext();
162
163 return items;
164}
165
166#include "moc_permissioncreatejob.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
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
std::optional< QSqlQuery > query(const QString &queryStatement)
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)
bool isEmpty() const const
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.