Kgapi

taskfetchjob.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 "taskfetchjob.h"
10#include "debug.h"
11#include "task.h"
12#include "tasksservice.h"
13#include "utils.h"
14
15#include <QNetworkReply>
16#include <QNetworkRequest>
17#include <QUrlQuery>
18
19using namespace KGAPI2;
20
21namespace
22{
23static const auto ShowDeletedParam = QStringLiteral("showDeleted");
24static const auto ShowCompletedParam = QStringLiteral("showCompleted");
25static const auto UpdatedMinParam = QStringLiteral("updatedMin");
26static const auto CompletedMinParam = QStringLiteral("completedMin");
27static const auto CompletedMaxParam = QStringLiteral("completedMax");
28static const auto DueMinParam = QStringLiteral("dueMin");
29static const auto DueMaxParam = QStringLiteral("dueMax");
30
31static constexpr bool FetchDeletedDefault = false;
32static constexpr bool FetchCompletedDefault = false;
33}
34
35class Q_DECL_HIDDEN TaskFetchJob::Private
36{
37public:
38 QString taskId;
39 QString taskListId;
40 bool fetchDeleted = true;
41 bool fetchCompleted = true;
42 quint64 updatedTimestamp;
43 quint64 completedMin;
44 quint64 completedMax;
45 quint64 dueMin;
46 quint64 dueMax;
47};
48
49TaskFetchJob::TaskFetchJob(const QString &taskListId, const AccountPtr &account, QObject *parent)
50 : FetchJob(account, parent)
51 , d(new Private())
52{
53 d->taskListId = taskListId;
54}
55
56TaskFetchJob::TaskFetchJob(const QString &taskId, const QString &taskListId, const AccountPtr &account, QObject *parent)
57 : FetchJob(account, parent)
58 , d(new Private())
59{
60 d->taskId = taskId;
61 d->taskListId = taskListId;
62}
63
65
66void TaskFetchJob::setFetchOnlyUpdated(quint64 timestamp)
67{
68 if (isRunning()) {
69 qCWarning(KGAPIDebug) << "Can't modify fetchOnlyUpdated property when job is running";
70 return;
71 }
72 d->updatedTimestamp = timestamp;
73}
74
76{
77 return d->updatedTimestamp;
78}
79
80void TaskFetchJob::setFetchCompleted(bool fetchCompleted)
81{
82 if (isRunning()) {
83 qCWarning(KGAPIDebug) << "Can't modify fetchCompleted property when job is running";
84 return;
85 }
86 d->fetchCompleted = fetchCompleted;
87}
88
90{
91 return d->fetchCompleted;
92}
93
94void TaskFetchJob::setFetchDeleted(bool fetchDeleted)
95{
96 if (isRunning()) {
97 qCWarning(KGAPIDebug) << "Can't modify fetchDeleted property when job is running";
98 return;
99 }
100 d->fetchDeleted = fetchDeleted;
101}
102
104{
105 return d->fetchDeleted;
106}
107
108void TaskFetchJob::setCompletedMin(quint64 timestamp)
109{
110 if (isRunning()) {
111 qCWarning(KGAPIDebug) << "Can't modify completedMin property when job is running";
112 return;
113 }
114 d->completedMin = timestamp;
115}
116
118{
119 return d->completedMin;
120}
121
122void TaskFetchJob::setCompletedMax(quint64 timestamp)
123{
124 if (isRunning()) {
125 qCWarning(KGAPIDebug) << "Can't modify completedMax property when job is running";
126 return;
127 }
128 d->completedMax = timestamp;
129}
130
132{
133 return d->completedMax;
134}
135
136void TaskFetchJob::setDueMin(quint64 timestamp)
137{
138 if (isRunning()) {
139 qCWarning(KGAPIDebug) << "Can't modify dueMin property when job is running";
140 return;
141 }
142 d->dueMin = timestamp;
143}
144
145quint64 TaskFetchJob::dueMin() const
146{
147 return d->dueMin;
148}
149
150void TaskFetchJob::setDueMax(quint64 timestamp)
151{
152 if (isRunning()) {
153 qCWarning(KGAPIDebug) << "Can't modify dueMax property when job is running";
154 return;
155 }
156 d->dueMax = timestamp;
157}
158
159quint64 TaskFetchJob::dueMax() const
160{
161 return d->dueMax;
162}
163
165{
166 QUrl url;
167 if (d->taskId.isEmpty()) {
168 url = TasksService::fetchAllTasksUrl(d->taskListId);
169 QUrlQuery query(url);
170 if (d->fetchDeleted != FetchDeletedDefault) {
171 query.addQueryItem(ShowDeletedParam, Utils::bool2Str(d->fetchDeleted));
172 }
173 if (d->fetchCompleted != FetchCompletedDefault) {
174 query.addQueryItem(ShowCompletedParam, Utils::bool2Str(d->fetchCompleted));
175 }
176 if (d->updatedTimestamp > 0) {
177 query.addQueryItem(UpdatedMinParam, Utils::ts2Str(d->updatedTimestamp));
178 }
179 if (d->completedMin > 0) {
180 query.addQueryItem(CompletedMinParam, Utils::ts2Str(d->completedMin));
181 }
182 if (d->completedMax > 0) {
183 query.addQueryItem(CompletedMaxParam, Utils::ts2Str(d->completedMax));
184 }
185 if (d->dueMin > 0) {
186 query.addQueryItem(DueMinParam, Utils::ts2Str(d->dueMin));
187 }
188 if (d->dueMax > 0) {
189 query.addQueryItem(DueMaxParam, Utils::ts2Str(d->dueMax));
190 }
191 url.setQuery(query);
192 } else {
193 url = TasksService::fetchTaskUrl(d->taskListId, d->taskId);
194 }
195
196 const QNetworkRequest request(url);
197 enqueueRequest(request);
198}
199
201{
202 FeedData feedData;
203 feedData.requestUrl = reply->url();
204
206 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
207 ContentType ct = Utils::stringToContentType(contentType);
208 if (ct == KGAPI2::JSON) {
209 if (d->taskId.isEmpty()) {
210 items = TasksService::parseJSONFeed(rawData, feedData);
211 } else {
213 }
214 } else {
216 setErrorString(tr("Invalid response content type"));
217 emitFinished();
218 return items;
219 }
220
221 if (feedData.nextPageUrl.isValid()) {
222 const QNetworkRequest request(feedData.nextPageUrl);
223 enqueueRequest(request);
224 }
225
226 return items;
227}
228
229#include "moc_taskfetchjob.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
A job to fetch all tasks from given tasklist in user's Google Tasks account.
void start() override
KGAPI2::Job::start implementation.
quint64 completedMin
Timestamp of the oldest completed task that will be fetched.
~TaskFetchJob() override
Destructor.
quint64 fetchOnlyUpdated
Timestamp to fetch only tasks modified since then.
void setDueMin(quint64 timestamp)
Sets timestamp of oldest due task that can be fetched.
quint64 completedMax
Timestamp of the newest completed task that will be fetched.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::FetchJob::handleReplyWithItems implementation.
quint64 dueMin
Timestamp of the oldest due task that will be fetched.
void setCompletedMax(quint64 timestamp)
Sets timestamp of newest completed task that can be fetched.
void setCompletedMin(quint64 timestamp)
Sets timestamp of oldest completed task that can be fetched.
bool fetchDeleted
Whether to fetch deleted tasks as well.
void setFetchCompleted(bool fetchCompleted=true)
Sets whether the job should fetch completed tasks.
quint64 dueMax
Timestamp of the newest due task that will be fetched.
void setFetchDeleted(bool fetchDeleted=true)
Sets whether to fetch should deleted tasks.
void setDueMax(quint64 timestamp)
Sets timestamp of newest due task that can be fetched.
TaskFetchJob(const QString &taskListId, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will fetch all tasks from a tasklist with given taskListId.
bool fetchCompleted
Whether to fetch completed tasks as well.
void setFetchOnlyUpdated(quint64 timestamp)
Sets the job to fetch only events modified since timestamp.
ObjectsList parseJSONFeed(const QByteArray &jsonFeed, FeedData &feedData)
Parses JSON feed into list of Tasks or TaskLists.
QUrl fetchAllTasksUrl(const QString &tasklistID)
Returns URL to fetch all tasks from a single tasklist.
QUrl fetchTaskUrl(const QString &tasklistID, const QString &taskID)
Returns URL for fetching a single task.
TaskPtr JSONToTask(const QByteArray &jsonData)
Parses JSON data into a Task object.
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 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:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.