Kgapi

eventfetchjob.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 "eventfetchjob.h"
10#include "account.h"
11#include "calendarservice.h"
12#include "debug.h"
13#include "event.h"
14#include "fetchjob.h"
15#include "types.h"
16#include "utils.h"
17
18#include <QNetworkReply>
19#include <QNetworkRequest>
20#include <QUrlQuery>
21
22using namespace KGAPI2;
23
24class Q_DECL_HIDDEN EventFetchJob::Private
25{
26public:
27 QString calendarId;
28 QString eventId;
30 QString syncToken;
31 QList<Event::EventType> eventTypes = { Event::EventType::Default, Event::EventType::FocusTime, Event::EventType::OutOfOffice };
32 bool fetchDeleted = true;
33 quint64 updatedTimestamp = 0;
34 quint64 timeMin = 0;
35 quint64 timeMax = 0;
36};
37
38EventFetchJob::EventFetchJob(const QString &calendarId, const AccountPtr &account, QObject *parent)
39 : FetchJob(account, parent)
40 , d(new Private)
41{
42 d->calendarId = calendarId;
43}
44
45EventFetchJob::EventFetchJob(const QString &eventId, const QString &calendarId, const AccountPtr &account, QObject *parent)
46 : FetchJob(account, parent)
47 , d(new Private)
48{
49 d->calendarId = calendarId;
50 d->eventId = eventId;
51}
52
54
55void EventFetchJob::setFetchDeleted(bool fetchDeleted)
56{
57 if (isRunning()) {
58 qCWarning(KGAPIDebug) << "Can't modify fetchDeleted property when job is running";
59 return;
60 }
61
62 d->fetchDeleted = fetchDeleted;
63}
64
66{
67 return d->fetchDeleted;
68}
69
71{
72 if (isRunning()) {
73 qCWarning(KGAPIDebug) << "Can't modify setFetchOnlyUpdated property when job is running";
74 return;
75 }
76
77 d->updatedTimestamp = timestamp;
78}
79
81{
82 return d->updatedTimestamp;
83}
84
85void EventFetchJob::setTimeMax(quint64 timestamp)
86{
87 if (isRunning()) {
88 qCWarning(KGAPIDebug) << "Can't modify timeMax property when job is running";
89 return;
90 }
91
92 d->timeMax = timestamp;
93}
94
96{
97 return d->timeMax;
98}
99
101{
102 d->syncToken = syncToken;
103}
104
106{
107 return d->syncToken;
108}
109
110void EventFetchJob::setTimeMin(quint64 timestamp)
111{
112 if (isRunning()) {
113 qCWarning(KGAPIDebug) << "Can't modify timeMin property when job is running";
114 return;
115 }
116
117 d->timeMin = timestamp;
118}
119
121{
122 return d->timeMin;
123}
124
126{
127 if (isRunning()) {
128 qCWarning(KGAPIDebug) << "Can't modify filter property when job is running";
129 return;
130 }
131
132 d->filter = query;
133}
134
136{
137 return d->filter;
138}
139
141{
142 QUrl url;
143 if (d->eventId.isEmpty()) {
144 url = CalendarService::fetchEventsUrl(d->calendarId);
145 QUrlQuery query(url);
146 query.addQueryItem(QStringLiteral("showDeleted"), Utils::bool2Str(d->fetchDeleted));
147 if (!d->filter.isEmpty()) {
148 query.addQueryItem(QStringLiteral("q"), d->filter);
149 }
150 if (d->syncToken.isEmpty()) {
151 if (d->updatedTimestamp > 0) {
152 query.addQueryItem(QStringLiteral("updatedMin"), Utils::ts2Str(d->updatedTimestamp));
153 }
154 if (d->timeMin > 0) {
155 query.addQueryItem(QStringLiteral("timeMin"), Utils::ts2Str(d->timeMin));
156 }
157 if (d->timeMax > 0) {
158 query.addQueryItem(QStringLiteral("timeMax"), Utils::ts2Str(d->timeMax));
159 }
160 } else {
161 query.addQueryItem(QStringLiteral("syncToken"), d->syncToken);
162 }
163 for (auto eventType : d->eventTypes) {
164 query.addQueryItem(QStringLiteral("eventTypes"), CalendarService::eventTypeToString(eventType));
165 }
166 url.setQuery(query);
167 } else {
168 url = CalendarService::fetchEventUrl(d->calendarId, d->eventId);
169 }
171 enqueueRequest(request);
172}
173
174bool EventFetchJob::handleError(int errorCode, const QByteArray &rawData)
175{
176 if (errorCode == KGAPI2::Gone) {
177 // Full sync required by server, redo request with no updatedMin and no syncToken
178 d->updatedTimestamp = 0;
179 d->syncToken.clear();
180 start();
181 return true;
182 }
183
184 return FetchJob::handleError(errorCode, rawData);
185}
186
188{
189 FeedData feedData;
190 feedData.requestUrl = reply->url();
192 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
193 ContentType ct = Utils::stringToContentType(contentType);
194 if (ct == KGAPI2::JSON) {
195 if (d->eventId.isEmpty()) {
196 items = CalendarService::parseEventJSONFeed(rawData, feedData);
197 } else {
199 }
200 d->syncToken = feedData.syncToken;
201 } else {
203 setErrorString(tr("Invalid response content type"));
204 emitFinished();
205 return items;
206 }
207
208 if (feedData.nextPageUrl.isValid()) {
209 const auto request = CalendarService::prepareRequest(feedData.nextPageUrl);
210 enqueueRequest(request);
211 }
212
213 return items;
214}
A job to fetch all events from given calendar in user's Google Calendar account.
EventFetchJob(const QString &calendarId, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will fetch all events from a calendar with given calendarId.
void setSyncToken(const QString &syncToken)
Sets token for incremental updates.
void start() override
KGAPI2::Job::start implementation.
QString syncToken
A token to fetch updates incrementally.
void setFetchOnlyUpdated(quint64 timestamp)
Sets the job to fetch only events modified since timestamp.
bool fetchDeleted
Whether to fetch deleted events as well.
quint64 timeMin
Timestamp of the oldest event that will be fetched.
void setFetchDeleted(bool fetchDeleted=true)
Sets whether to fetch deleted events.
void setTimeMax(quint64 timestamp)
Sets timestamp of newest event that can be fetched.
bool handleError(int errorCode, const QByteArray &rawData) override
KGAPI2::Job::handleError implementation.
quint64 fetchOnlyUpdated
Timestamp to fetch only events modified since then.
void setTimeMin(quint64 timestamp)
Sets timestamp of older events that can be fetched.
quint64 timeMax
Timestamp of the newest event that will be fetched.
QString filter
A filter to fetch only events matching fulltext filter.
~EventFetchJob() override
Destructor.
void setFilter(const QString &query)
Sets fulltext filter.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::FetchJob::handleReplyWithItems implementation.
Structure to store additional information about a feed.
Definition types.h:24
QString syncToken
Sync token that can be used for incremental updates by some of the services.
Definition types.h:42
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
virtual bool handleError(int statusCode, const QByteArray &rawData)
Called when an error occurs.
Definition job.cpp:549
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
Base class for all objects.
Definition object.h:31
ObjectsList parseEventJSONFeed(const QByteArray &jsonFeed, FeedData &feedData)
Parses JSON feed into list of Events.
QNetworkRequest prepareRequest(const QUrl &url)
Preparse a QNetworkRequest for given URL.
QString eventTypeToString(Event::EventType eventType)
Converts event type enum value to string.
EventPtr JSONToEvent(const QByteArray &jsonData)
Parses event JSON into Event object.
QUrl fetchEventsUrl(const QString &calendarID)
Returns URL for fetching all events from a specific calendar.
QUrl fetchEventUrl(const QString &calendarID, const QString &eventID)
Returns URL for fetching a single event from a specific calendar.
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
@ Gone
The requested data does not exist anymore on the remote site.
Definition types.h:202
@ 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)
QSharedPointer< X > dynamicCast() const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
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 Sat Dec 21 2024 17:00:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.