Kgapi

tasklistfetchjob.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#include "tasklistfetchjob.h"
9#include "tasksservice.h"
10#include "utils.h"
11
12#include <QNetworkReply>
13#include <QNetworkRequest>
14
15using namespace KGAPI2;
16
17class Q_DECL_HIDDEN TaskListFetchJob::Private
18{
19public:
20 Private(TaskListFetchJob *parent);
21 QNetworkRequest createRequest(const QUrl &url);
22
23private:
24 TaskListFetchJob *const q;
25};
26
27TaskListFetchJob::Private::Private(TaskListFetchJob *parent)
28 : q(parent)
29{
30}
31
32QNetworkRequest TaskListFetchJob::Private::createRequest(const QUrl &url)
33{
34 QNetworkRequest request(url);
35
36 QStringList headers;
37 const auto rawHeaderList = request.rawHeaderList();
38 headers.reserve(rawHeaderList.size());
39 for (const QByteArray &str : std::as_const(rawHeaderList)) {
40 headers << QLatin1StringView(str) + QLatin1StringView(": ") + QLatin1StringView(request.rawHeader(str));
41 }
42
43 return request;
44}
45
48 , d(new Private(this))
49{
50}
51
53
55{
57 const QNetworkRequest request = d->createRequest(url);
58 enqueueRequest(request);
59}
60
62{
63 FeedData feedData;
65 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
66 ContentType ct = Utils::stringToContentType(contentType);
67 if (ct == KGAPI2::JSON) {
68 items = TasksService::parseJSONFeed(rawData, feedData);
69 } else {
71 setErrorString(tr("Invalid response content type"));
73 return items;
74 }
75
76 if (feedData.nextPageUrl.isValid()) {
77 const QNetworkRequest request = d->createRequest(feedData.nextPageUrl);
78 enqueueRequest(request);
79 }
80
81 return items;
82}
83
84#include "moc_tasklistfetchjob.cpp"
Structure to store additional information about a feed.
Definition types.h:24
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
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
A job to fetch all tasklists from user's Google Tasks account.
void start() override
KGAPI2::Job::start implementation.
~TaskListFetchJob() override
Destructor.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::FetchJob::handleReplyWithItems implementation.
TaskListFetchJob(const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will fetch all tasklists from user's Google Tasks account.
ObjectsList parseJSONFeed(const QByteArray &jsonFeed, FeedData &feedData)
Parses JSON feed into list of Tasks or TaskLists.
QUrl fetchTaskListsUrl()
Returns URL for fetching all tasklists.
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
void reserve(qsizetype size)
QVariant header(QNetworkRequest::KnownHeaders header) const const
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isValid() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.