Kgapi

taskfetchjob.cpp
1 /*
2  * This file is part of LibKGAPI library
3  *
4  * SPDX-FileCopyrightText: 2013 Daniel Vrátil <[email protected]>
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 "tasksservice.h"
11 #include "account.h"
12 #include "../debug.h"
13 #include "task.h"
14 #include "utils.h"
15 
16 #include <QNetworkRequest>
17 #include <QNetworkReply>
18 #include <QUrlQuery>
19 
20 
21 using namespace KGAPI2;
22 
23 namespace {
24  static const auto ShowDeletedParam = QStringLiteral("showDeleted");
25  static const auto ShowCompletedParam = QStringLiteral("showCompleted");
26  static const auto UpdatedMinParam = QStringLiteral("updatedMin");
27  static const auto CompletedMinParam = QStringLiteral("completedMin");
28  static const auto CompletedMaxParam = QStringLiteral("completedMax");
29  static const auto DueMinParam = QStringLiteral("dueMin");
30  static const auto DueMaxParam = QStringLiteral("dueMax");
31 
32  static constexpr bool FetchDeletedDefault = false;
33  static constexpr bool FetchCompletedDefault = false;
34 }
35 
36 class Q_DECL_HIDDEN TaskFetchJob::Private
37 {
38  public:
39  QString taskId;
40  QString taskListId;
41  bool fetchDeleted = true;
42  bool fetchCompleted = true;
43  quint64 updatedTimestamp;
44  quint64 completedMin;
45  quint64 completedMax;
46  quint64 dueMin;
47  quint64 dueMax;
48 };
49 
50 TaskFetchJob::TaskFetchJob(const QString& taskListId, const AccountPtr& account, QObject* parent):
51  FetchJob(account, parent),
52  d(new Private())
53 {
54  d->taskListId = taskListId;
55 }
56 
57 TaskFetchJob::TaskFetchJob(const QString& taskId, const QString& taskListId,
58  const AccountPtr& account, QObject* parent):
59  FetchJob(account, parent),
60  d(new Private())
61 {
62  d->taskId = taskId;
63  d->taskListId = taskListId;
64 }
65 
66 TaskFetchJob::~TaskFetchJob() = default;
67 
68 void TaskFetchJob::setFetchOnlyUpdated(quint64 timestamp)
69 {
70  if (isRunning()) {
71  qCWarning(KGAPIDebug) << "Can't modify fetchOnlyUpdated property when job is running";
72  return;
73  }
74  d->updatedTimestamp = timestamp;
75 }
76 
78 {
79  return d->updatedTimestamp;
80 }
81 
83 {
84  if (isRunning()) {
85  qCWarning(KGAPIDebug) << "Can't modify fetchCompleted property when job is running";
86  return;
87  }
88  d->fetchCompleted = fetchCompleted;
89 }
90 
92 {
93  return d->fetchCompleted;
94 }
95 
97 {
98  if (isRunning()) {
99  qCWarning(KGAPIDebug) << "Can't modify fetchDeleted property when job is running";
100  return;
101  }
102  d->fetchDeleted = fetchDeleted;
103 }
104 
105 bool TaskFetchJob::fetchDeleted() const
106 {
107  return d->fetchDeleted;
108 }
109 
110 void TaskFetchJob::setCompletedMin(quint64 timestamp)
111 {
112  if (isRunning()) {
113  qCWarning(KGAPIDebug) << "Can't modify completedMin property when job is running";
114  return;
115  }
116  d->completedMin = timestamp;
117 }
118 
119 quint64 TaskFetchJob::completedMin() const
120 {
121  return d->completedMin;
122 }
123 
124 void TaskFetchJob::setCompletedMax(quint64 timestamp)
125 {
126  if (isRunning()) {
127  qCWarning(KGAPIDebug) << "Can't modify completedMax property when job is running";
128  return;
129  }
130  d->completedMax = timestamp;
131 }
132 
133 quint64 TaskFetchJob::completedMax() const
134 {
135  return d->completedMax;
136 }
137 
138 void TaskFetchJob::setDueMin(quint64 timestamp)
139 {
140  if (isRunning()) {
141  qCWarning(KGAPIDebug) << "Can't modify dueMin property when job is running";
142  return;
143  }
144  d->dueMin = timestamp;
145 }
146 
147 quint64 TaskFetchJob::dueMin() const
148 {
149  return d->dueMin;
150 }
151 
152 void TaskFetchJob::setDueMax(quint64 timestamp)
153 {
154  if (isRunning()) {
155  qCWarning(KGAPIDebug) << "Can't modify dueMax property when job is running";
156  return;
157  }
158  d->dueMax = timestamp;
159 }
160 
161 quint64 TaskFetchJob::dueMax() const
162 {
163  return d->dueMax;
164 }
165 
167 {
168  QUrl url;
169  if (d->taskId.isEmpty()) {
170  url = TasksService::fetchAllTasksUrl(d->taskListId);
171  QUrlQuery query(url);
172  if (d->fetchDeleted != FetchDeletedDefault) {
173  query.addQueryItem(ShowDeletedParam, Utils::bool2Str(d->fetchDeleted));
174  }
175  if (d->fetchCompleted != FetchCompletedDefault) {
176  query.addQueryItem(ShowCompletedParam, Utils::bool2Str(d->fetchCompleted));
177  }
178  if (d->updatedTimestamp > 0) {
179  query.addQueryItem(UpdatedMinParam, Utils::ts2Str(d->updatedTimestamp));
180  }
181  if (d->completedMin > 0) {
182  query.addQueryItem(CompletedMinParam, Utils::ts2Str(d->completedMin));
183  }
184  if (d->completedMax > 0) {
185  query.addQueryItem(CompletedMaxParam, Utils::ts2Str(d->completedMax));
186  }
187  if (d->dueMin > 0) {
188  query.addQueryItem(DueMinParam, Utils::ts2Str(d->dueMin));
189  }
190  if (d->dueMax > 0) {
191  query.addQueryItem(DueMaxParam, Utils::ts2Str(d->dueMax));
192  }
193  url.setQuery(query);
194  } else {
195  url = TasksService::fetchTaskUrl(d->taskListId, d->taskId);
196  }
197 
198  const QNetworkRequest request(url);
199  enqueueRequest(request);
200 }
201 
203 {
204  FeedData feedData;
205  feedData.requestUrl = reply->url();
206 
208  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
209  ContentType ct = Utils::stringToContentType(contentType);
210  if (ct == KGAPI2::JSON) {
211  if (d->taskId.isEmpty()) {
212  items = TasksService::parseJSONFeed(rawData, feedData);
213  } else {
214  items << TasksService::JSONToTask(rawData);
215  }
216  } else {
218  setErrorString(tr("Invalid response content type"));
219  emitFinished();
220  return items;
221  }
222 
223  if (feedData.nextPageUrl.isValid()) {
224  const QNetworkRequest request(feedData.nextPageUrl);
225  enqueueRequest(request);
226  }
227 
228  return items;
229 }
230 
231 
void setCompletedMax(quint64 timestamp)
Sets timestamp of newest completed task that can be fetched.
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:425
bool isRunning() const
Whether job is running.
void setCompletedMin(quint64 timestamp)
Sets timestamp of oldest completed task that can be fetched.
void setFetchCompleted(bool fetchCompleted=true)
Sets whether the job should fetch completed tasks.
~TaskFetchJob() override
Destructor.
void start() override
KGAPI2::Job::start implementation.
QUrl nextPageUrl
Link to next page of feed.
Definition: types.h:38
QUrl fetchAllTasksUrl(const QString &tasklistID)
Returns URL to fetch all tasks from a single tasklist.
TaskFetchJob(const QString &taskListId, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will fetch all tasks from a tasklist with given taskListId. ...
Structure to store additional information about a feed.
Definition: types.h:24
quint64 dueMax() const
Returns upper date limit for fetching due tasks.
quint64 completedMax() const
Returns upper date limit for fetching completed tasks.
QString tr(const char *sourceText, const char *disambiguation, int n)
void addQueryItem(const QString &key, const QString &value)
virtual ObjectsList items() const
Returns all items fetched by this job.
Definition: fetchjob.cpp:41
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:497
void setDueMin(quint64 timestamp)
Sets timestamp of oldest due task that can be fetched.
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:16
LibKGAPI error - Google returned invalid response.
Definition: types.h:180
quint64 completedMin() const
Returns bottom date limit for fetching completed tasks.
QVariant header(QNetworkRequest::KnownHeaders header) const const
virtual void emitFinished()
Emits Job::finished() signal.
Definition: job.cpp:479
bool isValid() const const
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition: job.cpp:390
quint64 dueMin() const
Returns bottom date limit for fetching due tasks.
A job to fetch all tasks from given tasklist in user&#39;s Google Tasks account.
Definition: taskfetchjob.h:26
QUrl url() const const
void setFetchDeleted(bool fetchDeleted=true)
Sets whether to fetch should deleted tasks.
void setFetchOnlyUpdated(quint64 timestamp)
Sets the job to fetch only events modified since timestamp.
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:375
void setQuery(const QString &query, QUrl::ParsingMode mode)
Abstract superclass for all jobs that fetch resources from Google.
Definition: fetchjob.h:24
bool fetchDeleted() const
Returns whether to fetch will deleted tasks.
QObject * parent() const const
TaskPtr JSONToTask(const QByteArray &jsonData)
Parses JSON data into a Task object.
QString toString() const const
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::FetchJob::handleReplyWithItems implementation.
void setDueMax(quint64 timestamp)
Sets timestamp of newest due task that can be fetched.
bool fetchCompleted() const
Returns whether the job will fetch completed tasks.
QUrl requestUrl
Original URL of the request.
Definition: types.h:39
quint64 fetchOnlyUpdated()
Returns whether the job will fetch only modified events.
ObjectsList parseJSONFeed(const QByteArray &jsonFeed, FeedData &feedData)
Parses JSON feed into list of Tasks or TaskLists.
ContentType
Definition: types.h:207
QUrl fetchTaskUrl(const QString &tasklistID, const QString &taskID)
Returns URL for fetching a single task.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jan 14 2021 23:13:27 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.