Kgapi

tasklistcreatejob.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 "tasklistcreatejob.h"
10#include "private/queuehelper_p.h"
11#include "tasklist.h"
12#include "tasksservice.h"
13#include "utils.h"
14
15#include <QNetworkReply>
16#include <QNetworkRequest>
17
18using namespace KGAPI2;
19
20class Q_DECL_HIDDEN TaskListCreateJob::Private
21{
22public:
23 QueueHelper<TaskListPtr> taskLists;
24};
25
26TaskListCreateJob::TaskListCreateJob(const TaskListPtr &taskList, const AccountPtr &account, QObject *parent)
27 : CreateJob(account, parent)
28 , d(new Private)
29{
30 d->taskLists << taskList;
31}
32
33TaskListCreateJob::TaskListCreateJob(const TaskListsList &taskLists, const AccountPtr &account, QObject *parent)
34 : CreateJob(account, parent)
35 , d(new Private)
36{
37 d->taskLists = taskLists;
38}
39
41
43{
44 if (d->taskLists.atEnd()) {
46 return;
47 }
48
49 const TaskListPtr taskList = d->taskLists.current();
50
52 QNetworkRequest request(url);
53
54 const QByteArray rawData = TasksService::taskListToJSON(taskList);
55
56 QStringList headers;
57 const auto rawHeaderList = request.rawHeaderList();
58 headers.reserve(rawHeaderList.size());
59 for (const QByteArray &str : std::as_const(rawHeaderList)) {
60 headers << QLatin1StringView(str) + QLatin1StringView(": ") + QLatin1StringView(request.rawHeader(str));
61 }
62
63 enqueueRequest(request, rawData, QStringLiteral("application/json"));
64}
65
67{
68 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
69 ContentType ct = Utils::stringToContentType(contentType);
71 if (ct != KGAPI2::JSON) {
73 setErrorString(tr("Invalid response content type"));
75 return items;
76 }
77
79 d->taskLists.currentProcessed();
80 // Enqueue next item or finish
81 start();
82
83 return items;
84}
85
86#include "moc_tasklistcreatejob.cpp"
Abstract superclass for all jobs that create new objects on the server.
Definition createjob.h:26
virtual ObjectsList items() const
Definition createjob.cpp:40
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
Base class for all objects.
Definition object.h:31
A job to create one or more new tasklists in user's Google Tasks account.
void start() override
KGAPI2::Job::start implementation.
TaskListCreateJob(const TaskListPtr &taskList, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will create given taskList in user's Google Tasks account.
~TaskListCreateJob() override
Destructor.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::CreateJob::handleReplyWithItems implementation.
QByteArray taskListToJSON(const TaskListPtr &taskList)
Serializes a TaskList into JSON data.
TaskListPtr JSONToTaskList(const QByteArray &jsonData)
Parses JSON data into a TaskList object.
QUrl createTaskListUrl()
Returns URL for creating a new tasklist.
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
QByteArray rawHeader(const QByteArray &headerName) const const
QList< QByteArray > rawHeaderList() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QSharedPointer< X > dynamicCast() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:55:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.