Kgapi

taskmodifyjob.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 "taskmodifyjob.h"
10#include "private/queuehelper_p.h"
11#include "task.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 TaskModifyJob::Private
21{
22public:
23 QueueHelper<TaskPtr> tasks;
24 QString taskListId;
25};
26
27TaskModifyJob::TaskModifyJob(const TaskPtr &task, const QString &taskListId, const AccountPtr &account, QObject *parent)
28 : ModifyJob(account, parent)
29 , d(new Private)
30{
31 d->tasks << task;
32 d->taskListId = taskListId;
33}
34
35TaskModifyJob::TaskModifyJob(const TasksList &tasks, const QString &taskListId, const AccountPtr &account, QObject *parent)
36 : ModifyJob(account, parent)
37 , d(new Private)
38{
39 d->tasks = tasks;
40 d->taskListId = taskListId;
41}
42
44
46{
47 if (d->tasks.atEnd()) {
49 return;
50 }
51
52 const TaskPtr task = d->tasks.current();
53 const QUrl url = TasksService::updateTaskUrl(d->taskListId, task->uid());
54 QNetworkRequest request(url);
55
56 const QByteArray rawData = TasksService::taskToJSON(task);
57
58 QStringList headers;
59 const auto rawHeaderList = request.rawHeaderList();
60 headers.reserve(rawHeaderList.size());
61 for (const QByteArray &str : std::as_const(rawHeaderList)) {
62 headers << QLatin1StringView(str) + QLatin1StringView(": ") + QLatin1StringView(request.rawHeader(str));
63 }
64
65 enqueueRequest(request, rawData, QStringLiteral("application/json"));
66}
67
69{
70 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
71 ContentType ct = Utils::stringToContentType(contentType);
73 if (ct != KGAPI2::JSON) {
75 setErrorString(tr("Invalid response content type"));
77 return items;
78 }
79
81 d->tasks.currentProcessed();
82 // Enqueue next item or finish
83 start();
84
85 return items;
86}
87
88#include "moc_taskmodifyjob.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 tasks in a tasklist in user's Google Tasks account.
~TaskModifyJob() override
Destructor.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::ModifyJob::handleReplyWithItems implementation.
void start() override
KGAPI2::Job::start implementation.
TaskModifyJob(const TaskPtr &task, const QString &taskListId, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will write changes in given task to corresponding task in a tasklist with given...
QByteArray taskToJSON(const TaskPtr &task)
Serializes a Task object into JSON.
TaskPtr JSONToTask(const QByteArray &jsonData)
Parses JSON data into a Task object.
QUrl updateTaskUrl(const QString &tasklistID, const QString &taskID)
Returns URL for updating a task.
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 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.