Kgapi

filefetchjob.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 "filefetchjob.h"
10#include "debug.h"
11#include "driveservice.h"
12#include "file.h"
13#include "filesearchquery.h"
14#include "utils.h"
15
16#include <QNetworkReply>
17#include <QNetworkRequest>
18#include <QUrlQuery>
19
20using namespace KGAPI2;
21using namespace KGAPI2::Drive;
22
23class Q_DECL_HIDDEN FileFetchJob::Private
24{
25public:
26 Private(FileFetchJob *parent);
27 void processNext();
28
29 FileSearchQuery searchQuery;
30 QStringList filesIDs;
31 bool isFeed = false;
32 bool includeItemsFromAllDrives = true;
33 bool supportsAllDrives = true;
34
35 bool updateViewedDate = false;
36
37 QStringList fields;
38
39private:
40 FileFetchJob *const q;
41};
42
43FileFetchJob::Private::Private(FileFetchJob *parent)
44 : q(parent)
45{
46}
47
48void FileFetchJob::Private::processNext()
49{
50 QUrl url;
51
52 if (isFeed) {
53 url = DriveService::fetchFilesUrl();
54
55 QUrlQuery query(url);
56 if (!searchQuery.isEmpty()) {
57 query.addQueryItem(QStringLiteral("q"), searchQuery.serialize());
58 }
59
60 query.addQueryItem(QStringLiteral("includeItemsFromAllDrives"), Utils::bool2Str(includeItemsFromAllDrives));
61
62 url.setQuery(query);
63
64 if (!fields.isEmpty()) {
65 // Deserializing requires kind attribute, always force add it
66 if (!fields.contains(File::Fields::Kind)) {
67 fields << File::Fields::Kind;
68 }
69 Job *baseJob = dynamic_cast<Job *>(q);
70 baseJob->setFields({File::Fields::Etag,
71 File::Fields::Kind,
72 File::Fields::NextLink,
73 File::Fields::NextPageToken,
74 File::Fields::SelfLink,
75 Job::buildSubfields(File::Fields::Items, fields)});
76 }
77 } else {
78 if (filesIDs.isEmpty()) {
79 q->emitFinished();
80 return;
81 }
82
83 const QString fileId = filesIDs.takeFirst();
84 url = DriveService::fetchFileUrl(fileId);
85
86 if (!fields.isEmpty()) {
87 // Deserializing requires kind attribute, always force add it
88 if (!fields.contains(File::Fields::Kind)) {
89 fields << File::Fields::Kind;
90 }
91 Job *baseJob = dynamic_cast<Job *>(q);
92 baseJob->setFields(fields);
93 }
94 }
95
96 QUrlQuery withDriveSupportQuery(url);
97 withDriveSupportQuery.addQueryItem(QStringLiteral("supportsAllDrives"), Utils::bool2Str(supportsAllDrives));
98 url.setQuery(withDriveSupportQuery);
99
100 QNetworkRequest request(url);
101 q->enqueueRequest(request);
102}
103
104FileFetchJob::FileFetchJob(const QString &fileId, const AccountPtr &account, QObject *parent)
106 , d(new Private(this))
107{
108 d->filesIDs << fileId;
109}
110
111FileFetchJob::FileFetchJob(const QStringList &filesIds, const AccountPtr &account, QObject *parent)
112 : FetchJob(account, parent)
113 , d(new Private(this))
114{
115 d->filesIDs << filesIds;
116}
117
118FileFetchJob::FileFetchJob(const AccountPtr &account, QObject *parent)
119 : FetchJob(account, parent)
120 , d(new Private(this))
121{
122 d->isFeed = true;
123}
124
125FileFetchJob::FileFetchJob(const FileSearchQuery &query, const AccountPtr &account, QObject *parent)
126 : FetchJob(account, parent)
127 , d(new Private(this))
128{
129 d->isFeed = true;
130 d->searchQuery = query;
131}
132
133FileFetchJob::~FileFetchJob()
134{
135 delete d;
136}
137
138bool FileFetchJob::updateViewedDate() const
139{
140 return d->updateViewedDate;
141}
142
143void FileFetchJob::setUpdateViewedDate(bool updateViewedDate)
144{
145 if (isRunning()) {
146 qCWarning(KGAPIDebug) << "Can't modify updateViewedDate property when job is running.";
147 return;
148 }
149
150 d->updateViewedDate = updateViewedDate;
151}
152
153void FileFetchJob::start()
154{
155 d->processNext();
156}
157
158void FileFetchJob::setFields(const QStringList &fields)
159{
160 if (isRunning()) {
161 qCWarning(KGAPIDebug) << "Called setFields() on running job. Ignoring.";
162 return;
163 }
164
165 d->fields = fields;
166}
167
168QStringList FileFetchJob::fields() const
169{
170 return d->fields;
171}
172
173bool FileFetchJob::includeItemsFromAllDrives() const
174{
175 return d->includeItemsFromAllDrives;
176}
177
178void FileFetchJob::setIncludeItemsFromAllDrives(bool includeItemsFromAllDrives)
179{
180 d->includeItemsFromAllDrives = includeItemsFromAllDrives;
181}
182
183bool FileFetchJob::supportsAllDrives() const
184{
185 return d->supportsAllDrives;
186}
187
188void FileFetchJob::setSupportsAllDrives(bool supportsAllDrives)
189{
190 d->supportsAllDrives = supportsAllDrives;
191}
192
193const QStringList &FileFetchJob::FieldShorthands::basicFields()
194{
195 static const QStringList basicFields = {File::Fields::Id,
196 File::Fields::Title,
197 File::Fields::MimeType,
198 File::Fields::CreatedDate,
199 File::Fields::ModifiedDate,
200 File::Fields::FileSize,
201 File::Fields::DownloadUrl,
202 File::Fields::Permissions};
203 return basicFields;
204}
205
206const QStringList &FileFetchJob::FieldShorthands::accessFields()
207{
208 static const QStringList accessFields = {File::Fields::CreatedDate,
209 File::Fields::ModifiedDate,
210 File::Fields::ModifiedByMeDate,
211 File::Fields::LastModifiedByMeDate,
212 File::Fields::LastViewedByMeDate,
213 File::Fields::MarkedViewedByMeDate};
214 return accessFields;
215}
216
217const QStringList &FileFetchJob::FieldShorthands::sharingFields()
218{
219 static const QStringList sharingFields = {File::Fields::SharedWithMeDate,
220 File::Fields::WritersCanShare,
221 File::Fields::Shared,
222 File::Fields::Owners,
223 File::Fields::SharingUser,
224 File::Fields::OwnerNames};
225 return sharingFields;
226}
227
228ObjectsList FileFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
229{
231
232 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
233 ContentType ct = Utils::stringToContentType(contentType);
234 if (ct == KGAPI2::JSON) {
235 if (d->isFeed) {
236 FeedData feedData;
237
238 items << File::fromJSONFeed(rawData, feedData);
239
240 if (feedData.nextPageUrl.isValid()) {
241 QNetworkRequest request(feedData.nextPageUrl);
242 enqueueRequest(request);
243 }
244
245 } else {
246 items << File::fromJSON(rawData);
247
248 d->processNext();
249 }
250 } else {
252 setErrorString(tr("Invalid response content type"));
253 emitFinished();
254 return items;
255 }
256
257 return items;
258}
259
260#include "moc_filefetchjob.cpp"
FileSearchQuery class allows simply building even complex file search queries for FileFetchJob.
Structure to store additional information about a feed.
Definition types.h:24
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
Abstract base class for all jobs in LibKGAPI.
Definition job.h:41
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
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
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 contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
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.