Kgapi

drivesfetchjob.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 "drivesfetchjob.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 MaxResultsAttr = QStringLiteral("maxResults");
22static const QString UseDomainAdminAccessAttr = QStringLiteral("useDomainAdminAccess");
23}
24using namespace KGAPI2;
25using namespace KGAPI2::Drive;
26
27class Q_DECL_HIDDEN DrivesFetchJob::Private
28{
29public:
30 Private(DrivesFetchJob *parent);
31
32 DrivesSearchQuery searchQuery;
33 QString drivesId;
34
35 int maxResults = 0;
36 QVariant useDomainAdminAccess;
37
38 QStringList fields;
39
40private:
41 DrivesFetchJob *const q;
42};
43
44DrivesFetchJob::Private::Private(DrivesFetchJob *parent)
45 : q(parent)
46{
47}
48
49DrivesFetchJob::DrivesFetchJob(const QString &drivesId, const AccountPtr &account, QObject *parent)
50 : FetchJob(account, parent)
51 , d(new Private(this))
52{
53 d->drivesId = drivesId;
54}
55
56DrivesFetchJob::DrivesFetchJob(const DrivesSearchQuery &query, const AccountPtr &account, QObject *parent)
57 : FetchJob(account, parent)
58 , d(new Private(this))
59{
60 d->searchQuery = query;
61}
62
63DrivesFetchJob::DrivesFetchJob(const AccountPtr &account, QObject *parent)
64 : FetchJob(account, parent)
65 , d(new Private(this))
66{
67}
68
69DrivesFetchJob::~DrivesFetchJob() = default;
70
71void DrivesFetchJob::setMaxResults(int maxResults)
72{
73 if (isRunning()) {
74 qCWarning(KGAPIDebug) << "Can't modify maxResults property when job is running";
75 return;
76 }
77
78 d->maxResults = maxResults;
79}
80
81int DrivesFetchJob::maxResults() const
82{
83 return d->maxResults;
84}
85
86void DrivesFetchJob::setUseDomainAdminAccess(bool useDomainAdminAccess)
87{
88 if (isRunning()) {
89 qCWarning(KGAPIDebug) << "Can't modify useDomainAdminAccess property when job is running";
90 return;
91 }
92
93 d->useDomainAdminAccess = useDomainAdminAccess;
94}
95
96bool DrivesFetchJob::useDomainAdminAccess() const
97{
98 return d->useDomainAdminAccess.toBool();
99}
100
101void DrivesFetchJob::setFields(const QStringList &fields)
102{
103 if (isRunning()) {
104 qCWarning(KGAPIDebug) << "Called setFields() on running job. Ignoring.";
105 return;
106 }
107
108 d->fields = fields;
109}
110
111QStringList DrivesFetchJob::fields() const
112{
113 return d->fields;
114}
115
116void DrivesFetchJob::start()
117{
118 QUrl url;
119 if (d->drivesId.isEmpty()) {
120 url = DriveService::fetchDrivesUrl();
121 applyRequestParameters(url);
122 } else {
123 url = DriveService::fetchDrivesUrl(d->drivesId);
124 if (!d->fields.isEmpty()) {
125 // Deserializing requires kind attribute, always force add it
126 if (!d->fields.contains(Drives::Fields::Kind)) {
127 d->fields << Drives::Fields::Kind;
128 }
129 Job::setFields(d->fields);
130 }
131 }
132
133 QNetworkRequest request(url);
134 enqueueRequest(request);
135}
136
137ObjectsList DrivesFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
138{
139 FeedData feedData;
140 feedData.requestUrl = reply->url();
141
143
144 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
145 ContentType ct = Utils::stringToContentType(contentType);
146 if (ct == KGAPI2::JSON) {
147 if (d->drivesId.isEmpty()) {
148 items << Drives::fromJSONFeed(rawData, feedData);
149 } else {
150 items << Drives::fromJSON(rawData);
151 }
152 } else {
154 setErrorString(tr("Invalid response content type"));
155 emitFinished();
156 return items;
157 }
158
159 if (feedData.nextPageUrl.isValid()) {
160 // Reapply query options
161 applyRequestParameters(feedData.nextPageUrl);
162 QNetworkRequest request(feedData.nextPageUrl);
163 enqueueRequest(request);
164 }
165
166 return items;
167}
168
169void DrivesFetchJob::applyRequestParameters(QUrl &url)
170{
171 QUrlQuery query(url);
172 if (d->maxResults != 0) {
173 query.addQueryItem(MaxResultsAttr, QString::number(d->maxResults));
174 }
175 if (!d->useDomainAdminAccess.isNull()) {
176 query.addQueryItem(UseDomainAdminAccessAttr, Utils::bool2Str(d->useDomainAdminAccess.toBool()));
177 }
178 if (!d->searchQuery.isEmpty()) {
179 query.addQueryItem(QStringLiteral("q"), d->searchQuery.serialize());
180 }
181 if (!d->fields.isEmpty()) {
182 // Deserializing requires kind attribute, always force add it
183 if (!d->fields.contains(Drives::Fields::Kind)) {
184 d->fields << Drives::Fields::Kind;
185 }
186 QString itemFields = Job::buildSubfields(Drives::Fields::Items, d->fields);
187 Job::setFields({Drives::Fields::Kind, Drives::Fields::NextPageToken, itemFields});
188 }
189 url.setQuery(query);
190}
191
192#include "moc_drivesfetchjob.cpp"
DrivesSearchQuery class allows simply building even complex shared drive search queries for DrivesFet...
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 Fri Dec 6 2024 12:11:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.