Kgapi

teamdrivefetchjob.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 "teamdrivefetchjob.h"
10#include "debug.h"
11#include "driveservice.h"
12#include "teamdrive.h"
13#include "utils.h"
14
15#include <QNetworkReply>
16#include <QNetworkRequest>
17#include <QUrlQuery>
18
19namespace
20{
21static const QString MaxResultsAttr = QStringLiteral("maxResults");
22static const QString UseDomainAdminAccessAttr = QStringLiteral("useDomainAdminAccess");
23}
24using namespace KGAPI2;
25using namespace KGAPI2::Drive;
26
27class Q_DECL_HIDDEN TeamdriveFetchJob::Private
28{
29public:
30 Private(TeamdriveFetchJob *parent);
31
32 TeamdriveSearchQuery searchQuery;
33 QString teamdriveId;
34
35 int maxResults = 0;
36 bool useDomainAdminAccess = false;
37
38 QStringList fields;
39
40private:
41 TeamdriveFetchJob *const q;
42};
43
44TeamdriveFetchJob::Private::Private(TeamdriveFetchJob *parent)
45 : q(parent)
46{
47}
48
49TeamdriveFetchJob::TeamdriveFetchJob(const QString &teamdriveId, const AccountPtr &account, QObject *parent)
50 : FetchJob(account, parent)
51 , d(new Private(this))
52{
53 d->teamdriveId = teamdriveId;
54}
55
56TeamdriveFetchJob::TeamdriveFetchJob(const TeamdriveSearchQuery &query, const AccountPtr &account, QObject *parent)
57 : FetchJob(account, parent)
58 , d(new Private(this))
59{
60 d->useDomainAdminAccess = true;
61 d->searchQuery = query;
62}
63
64TeamdriveFetchJob::TeamdriveFetchJob(const AccountPtr &account, QObject *parent)
65 : FetchJob(account, parent)
66 , d(new Private(this))
67{
68}
69
70TeamdriveFetchJob::~TeamdriveFetchJob() = default;
71
72void TeamdriveFetchJob::setMaxResults(int maxResults)
73{
74 if (isRunning()) {
75 qCWarning(KGAPIDebug) << "Can't modify maxResults property when job is running";
76 return;
77 }
78
79 d->maxResults = maxResults;
80}
81
82int TeamdriveFetchJob::maxResults() const
83{
84 return d->maxResults;
85}
86
87void TeamdriveFetchJob::setUseDomainAdminAccess(bool useDomainAdminAccess)
88{
89 if (isRunning()) {
90 qCWarning(KGAPIDebug) << "Can't modify useDomainAdminAccess property when job is running";
91 return;
92 }
93
94 d->useDomainAdminAccess = useDomainAdminAccess;
95}
96
97bool TeamdriveFetchJob::useDomainAdminAccess() const
98{
99 return d->useDomainAdminAccess;
100}
101
102void TeamdriveFetchJob::setFields(const QStringList &fields)
103{
104 if (isRunning()) {
105 qCWarning(KGAPIDebug) << "Called setFields() on running job. Ignoring.";
106 return;
107 }
108
109 d->fields = fields;
110}
111
112QStringList TeamdriveFetchJob::fields() const
113{
114 return d->fields;
115}
116
117void TeamdriveFetchJob::start()
118{
119 QUrl url;
120 if (d->teamdriveId.isEmpty()) {
121 url = DriveService::fetchTeamdrivesUrl();
122 applyRequestParameters(url);
123 } else {
124 url = DriveService::fetchTeamdriveUrl(d->teamdriveId);
125 if (!d->fields.isEmpty()) {
126 // Deserializing requires kind attribute, always force add it
127 if (!d->fields.contains(Teamdrive::Fields::Kind)) {
128 d->fields << Teamdrive::Fields::Kind;
129 }
130 Job::setFields(d->fields);
131 }
132 }
133
134 QNetworkRequest request(url);
135 enqueueRequest(request);
136}
137
138ObjectsList TeamdriveFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
139{
140 FeedData feedData;
141 feedData.requestUrl = reply->url();
142
144
145 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
146 ContentType ct = Utils::stringToContentType(contentType);
147 if (ct == KGAPI2::JSON) {
148 if (d->teamdriveId.isEmpty()) {
149 items << Teamdrive::fromJSONFeed(rawData, feedData);
150 } else {
151 items << Teamdrive::fromJSON(rawData);
152 }
153 } else {
155 setErrorString(tr("Invalid response content type"));
156 emitFinished();
157 return items;
158 }
159
160 if (feedData.nextPageUrl.isValid()) {
161 // Reapply query options
162 applyRequestParameters(feedData.nextPageUrl);
163 QNetworkRequest request(feedData.nextPageUrl);
164 enqueueRequest(request);
165 }
166
167 return items;
168}
169
170void TeamdriveFetchJob::applyRequestParameters(QUrl &url)
171{
172 QUrlQuery query(url);
173 if (d->maxResults != 0) {
174 query.addQueryItem(MaxResultsAttr, QString::number(d->maxResults));
175 }
176 if (d->useDomainAdminAccess != false) {
177 query.addQueryItem(UseDomainAdminAccessAttr, Utils::bool2Str(d->useDomainAdminAccess));
178 }
179 if (!d->searchQuery.isEmpty()) {
180 query.addQueryItem(QStringLiteral("q"), d->searchQuery.serialize());
181 }
182 if (!d->fields.isEmpty()) {
183 // Deserializing requires kind attribute, always force add it
184 if (!d->fields.contains(Teamdrive::Fields::Kind)) {
185 d->fields << Teamdrive::Fields::Kind;
186 }
187 QString itemFields = Job::buildSubfields(Teamdrive::Fields::Items, d->fields);
188 Job::setFields({Teamdrive::Fields::Kind, Teamdrive::Fields::NextPageToken, itemFields});
189 }
190 url.setQuery(query);
191}
192
193#include "moc_teamdrivefetchjob.cpp"
TeamdriveSearchQuery class allows simply building even complex team drive search queries for Teamdriv...
Structure to store additional information about a feed.
Definition types.h:24
QUrl requestUrl
Original URL of the request.
Definition types.h:39
QUrl nextPageUrl
Link to next page of feed.
Definition types.h:38
Abstract superclass for all jobs that fetch resources from Google.
Definition fetchjob.h:25
virtual ObjectsList items() const
Returns all items fetched by this job.
Definition fetchjob.cpp:41
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition job.cpp:401
void setFields(const QStringList &fields)
Set subset of fields to include in the response.
Definition job.cpp:471
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
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
QVariant header(QNetworkRequest::KnownHeaders header) const const
QUrl url() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString number(double n, char format, int precision)
bool isValid() 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 Mon Nov 18 2024 12:17:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.