Kgapi

tasklistmodifyjob.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 "tasklistmodifyjob.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 TaskListModifyJob::Private
21{
22public:
23 QueueHelper<TaskListPtr> taskLists;
24};
25
26TaskListModifyJob::TaskListModifyJob(const TaskListPtr &taskList, const AccountPtr &account, QObject *parent)
27 : ModifyJob(account, parent)
28 , d(new Private)
29{
30 d->taskLists << taskList;
31}
32
33TaskListModifyJob::TaskListModifyJob(const TaskListsList &taskLists, const AccountPtr &account, QObject *parent)
34 : ModifyJob(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
51 const QUrl url = TasksService::updateTaskListUrl(taskList->uid());
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_tasklistmodifyjob.cpp"
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
Abstract superclass for all jobs that somehow modify resources on Google.
Definition modifyjob.h:25
ObjectsList items() const
Definition modifyjob.cpp:43
Base class for all objects.
Definition object.h:31
A job to modify tasklists in user's Google Tasks account.
void start() override
KGAPI2::Job::start implementation.
TaskListModifyJob(const TaskListPtr &taskList, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will write changes in given taskList to corresponding tasklist in user's Google...
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::ModifyJob::handleReplyWithItems implementation.
~TaskListModifyJob() override
Destructor.
QUrl updateTaskListUrl(const QString &tasklistID)
Returns URL for modifying a tasklist.
QByteArray taskListToJSON(const TaskListPtr &taskList)
Serializes a TaskList into JSON data.
TaskListPtr JSONToTaskList(const QByteArray &jsonData)
Parses JSON data into a TaskList 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
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 Jul 19 2024 11:58:18 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.