Kgapi

changefetchjob.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 "changefetchjob.h"
10#include "change.h"
11#include "debug.h"
12#include "driveservice.h"
13#include "utils.h"
14
15#include <QNetworkReply>
16#include <QNetworkRequest>
17#include <QUrlQuery>
18
19using namespace KGAPI2;
20using namespace KGAPI2::Drive;
21
22class Q_DECL_HIDDEN ChangeFetchJob::Private
23{
24public:
25 Private(ChangeFetchJob *parent);
26
27 QString changeId;
28
29 bool includeDeleted = true;
30 bool includeSubscribed = true;
31 int maxResults = 0;
32 qlonglong startChangeId = 0;
33 bool includeItemsFromAllDrives = true;
34 bool supportsAllDrives = true;
35
36private:
37 ChangeFetchJob *const q;
38};
39
40ChangeFetchJob::Private::Private(ChangeFetchJob *parent)
41 : q(parent)
42{
43}
44
45ChangeFetchJob::ChangeFetchJob(const QString &changeId, const AccountPtr &account, QObject *parent)
46 : FetchJob(account, parent)
47 , d(new Private(this))
48{
49 d->changeId = changeId;
50}
51
52ChangeFetchJob::ChangeFetchJob(const AccountPtr &account, QObject *parent)
53 : FetchJob(account, parent)
54 , d(new Private(this))
55{
56}
57
58ChangeFetchJob::~ChangeFetchJob()
59{
60 delete d;
61}
62
63void ChangeFetchJob::setIncludeDeleted(bool includeDeleted)
64{
65 if (isRunning()) {
66 qCWarning(KGAPIDebug) << "Can't modify includeDeleted property when job is running";
67 return;
68 }
69
70 d->includeDeleted = includeDeleted;
71}
72
73bool ChangeFetchJob::includeDeleted() const
74{
75 return d->includeDeleted;
76}
77
78void ChangeFetchJob::setIncludeSubscribed(bool includeSubscribed)
79{
80 if (isRunning()) {
81 qCWarning(KGAPIDebug) << "Can't modify includeSubscribed property when job is running";
82 return;
83 }
84
85 d->includeSubscribed = includeSubscribed;
86}
87
88bool ChangeFetchJob::includeSubscribed() const
89{
90 return d->includeSubscribed;
91}
92
93void ChangeFetchJob::setMaxResults(int maxResults)
94{
95 if (isRunning()) {
96 qCWarning(KGAPIDebug) << "Can't modify maxResults property when job is running";
97 return;
98 }
99
100 d->maxResults = maxResults;
101}
102
103int ChangeFetchJob::maxResults() const
104{
105 return d->maxResults;
106}
107
108void ChangeFetchJob::setStartChangeId(qlonglong startChangeId)
109{
110 if (isRunning()) {
111 qCWarning(KGAPIDebug) << "Can't modify startChangeId property when job is running";
112 }
113
114 d->startChangeId = startChangeId;
115}
116
117qlonglong ChangeFetchJob::startChangeId() const
118{
119 return d->startChangeId;
120}
121
122bool ChangeFetchJob::includeItemsFromAllDrives() const
123{
124 return d->includeItemsFromAllDrives;
125}
126
127void ChangeFetchJob::setIncludeItemsFromAllDrives(bool includeItemsFromAllDrives)
128{
129 d->includeItemsFromAllDrives = includeItemsFromAllDrives;
130}
131
132bool ChangeFetchJob::supportsAllDrives() const
133{
134 return d->supportsAllDrives;
135}
136
137void ChangeFetchJob::setSupportsAllDrives(bool supportsAllDrives)
138{
139 d->supportsAllDrives = supportsAllDrives;
140}
141
142void ChangeFetchJob::start()
143{
144 QUrl url;
145 if (d->changeId.isEmpty()) {
146 url = DriveService::fetchChangesUrl();
147 QUrlQuery query(url);
148 query.addQueryItem(QStringLiteral("includeDeleted"), Utils::bool2Str(d->includeDeleted));
149 query.addQueryItem(QStringLiteral("includeSubscribed"), Utils::bool2Str(d->includeSubscribed));
150 if (d->maxResults > 0) {
151 query.addQueryItem(QStringLiteral("maxResults"), QString::number(d->maxResults));
152 }
153 if (d->startChangeId > 0) {
154 query.addQueryItem(QStringLiteral("startChangeId"), QString::number(d->startChangeId));
155 }
156 query.addQueryItem(QStringLiteral("includeItemsFromAllDrives"), Utils::bool2Str(d->includeItemsFromAllDrives));
157 url.setQuery(query);
158 } else {
159 url = DriveService::fetchChangeUrl(d->changeId);
160 }
161
162 QUrlQuery query(url);
163 query.addQueryItem(QStringLiteral("supportsAllDrives"), Utils::bool2Str(d->supportsAllDrives));
164 url.setQuery(query);
165
166 QNetworkRequest request(url);
167 enqueueRequest(request);
168}
169
170ObjectsList ChangeFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
171{
172 FeedData feedData;
173 feedData.requestUrl = reply->url();
174
176
177 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
178 ContentType ct = Utils::stringToContentType(contentType);
179 if (ct == KGAPI2::JSON) {
180 if (d->changeId.isEmpty()) {
181 items << Change::fromJSONFeed(rawData, feedData);
182 } else {
183 items << Change::fromJSON(rawData);
184 }
185 } else {
187 setErrorString(tr("Invalid response content type"));
188 emitFinished();
189 return items;
190 }
191
192 if (feedData.nextPageUrl.isValid()) {
193 QNetworkRequest request(feedData.nextPageUrl);
194 enqueueRequest(request);
195 }
196
197 return items;
198}
199
200#include "moc_changefetchjob.cpp"
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
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)
bool isEmpty() const const
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 Nov 29 2024 11:54:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.