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 "types.h"
14#include "utils.h"
15
16#include <QNetworkReply>
17#include <QNetworkRequest>
18#include <QUrlQuery>
19
20using namespace KGAPI2;
21
22class Q_DECL_HIDDEN EventFetchJob::Private
23{
24public:
25 QString calendarId;
26 QString eventId;
27 QString filter;
28 QString syncToken;
29 QList<Event::EventType> eventTypes = { Event::EventType::Default, Event::EventType::FocusTime, Event::EventType::OutOfOffice };
30 bool fetchDeleted = true;
31 quint64 updatedTimestamp = 0;
32 quint64 timeMin = 0;
33 quint64 timeMax = 0;
34};
35
36EventFetchJob::EventFetchJob(const QString &calendarId, const AccountPtr &account, QObject *parent)
38 , d(new Private)
39{
40 d->calendarId = calendarId;
41}
42
43EventFetchJob::EventFetchJob(const QString &eventId, const QString &calendarId, const AccountPtr &account, QObject *parent)
45 , d(new Private)
46{
47 d->calendarId = calendarId;
48 d->eventId = eventId;
49}
50
52
54{
55 if (isRunning()) {
56 qCWarning(KGAPIDebug) << "Can't modify fetchDeleted property when job is running";
57 return;
58 }
59
60 d->fetchDeleted = fetchDeleted;
61}
62
64{
65 return d->fetchDeleted;
66}
67
69{
70 if (isRunning()) {
71 qCWarning(KGAPIDebug) << "Can't modify setFetchOnlyUpdated property when job is running";
72 return;
73 }
74
75 d->updatedTimestamp = timestamp;
76}
77
79{
80 return d->updatedTimestamp;
81}
82
83void EventFetchJob::setTimeMax(quint64 timestamp)
84{
85 if (isRunning()) {
86 qCWarning(KGAPIDebug) << "Can't modify timeMax property when job is running";
87 return;
88 }
89
90 d->timeMax = timestamp;
91}
92
94{
95 return d->timeMax;
96}
97
99{
100 d->syncToken = syncToken;
101}
102
104{
105 return d->syncToken;
106}
107
108void EventFetchJob::setTimeMin(quint64 timestamp)
109{
110 if (isRunning()) {
111 qCWarning(KGAPIDebug) << "Can't modify timeMin property when job is running";
112 return;
113 }
114
115 d->timeMin = timestamp;
116}
117
119{
120 return d->timeMin;
121}
122
124{
125 if (isRunning()) {
126 qCWarning(KGAPIDebug) << "Can't modify filter property when job is running";
127 return;
128 }
129
130 d->filter = query;
131}
132
134{
135 return d->filter;
136}
137
139{
140 QUrl url;
141 if (d->eventId.isEmpty()) {
142 url = CalendarService::fetchEventsUrl(d->calendarId);
143 QUrlQuery query(url);
144 query.addQueryItem(QStringLiteral("showDeleted"), Utils::bool2Str(d->fetchDeleted));
145 if (!d->filter.isEmpty()) {
146 query.addQueryItem(QStringLiteral("q"), d->filter);
147 }
148 if (d->syncToken.isEmpty()) {
149 if (d->updatedTimestamp > 0) {
150 query.addQueryItem(QStringLiteral("updatedMin"), Utils::ts2Str(d->updatedTimestamp));
151 }
152 if (d->timeMin > 0) {
153 query.addQueryItem(QStringLiteral("timeMin"), Utils::ts2Str(d->timeMin));
154 }
155 if (d->timeMax > 0) {
156 query.addQueryItem(QStringLiteral("timeMax"), Utils::ts2Str(d->timeMax));
157 }
158 } else {
159 query.addQueryItem(QStringLiteral("syncToken"), d->syncToken);
160 }
161 for (auto eventType : d->eventTypes) {
162 query.addQueryItem(QStringLiteral("eventTypes"), CalendarService::eventTypeToString(eventType));
163 }
164 url.setQuery(query);
165 } else {
166 url = CalendarService::fetchEventUrl(d->calendarId, d->eventId);
167 }
169 enqueueRequest(request);
170}
171
172bool EventFetchJob::handleError(int errorCode, const QByteArray &rawData)
173{
174 if (errorCode == KGAPI2::Gone) {
175 // Full sync required by server, redo request with no updatedMin and no syncToken
176 d->updatedTimestamp = 0;
177 d->syncToken.clear();
178 start();
179 return true;
180 }
181
182 return FetchJob::handleError(errorCode, rawData);
183}
184
185ObjectsList EventFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
186{
187 FeedData feedData;
188 feedData.requestUrl = reply->url();
189 ObjectsList items;
190 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
191 ContentType ct = Utils::stringToContentType(contentType);
192 if (ct == KGAPI2::JSON) {
193 if (d->eventId.isEmpty()) {
194 items = CalendarService::parseEventJSONFeed(rawData, feedData);
195 } else {
197 }
198 d->syncToken = feedData.syncToken;
199 } else {
201 setErrorString(tr("Invalid response content type"));
202 emitFinished();
203 return items;
204 }
205
206 if (feedData.nextPageUrl.isValid()) {
207 const auto request = CalendarService::prepareRequest(feedData.nextPageUrl);
208 enqueueRequest(request);
209 }
210
211 return items;
212}
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
FetchJob(QObject *parent=nullptr)
Constructor for jobs that don't require authentication.
Definition fetchjob.cpp:24
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
AccountPtr account() const
Returns account used to authenticate requests.
Definition job.cpp:436
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
QObject(QObject *parent)
QObject * parent() 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-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:57:30 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.