Kgapi

taskcreatejob.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 "taskcreatejob.h"
10#include "account.h"
11#include "debug.h"
12#include "private/queuehelper_p.h"
13#include "task.h"
14#include "tasksservice.h"
15#include "utils.h"
16
17#include <QNetworkReply>
18#include <QNetworkRequest>
19#include <QUrlQuery>
20
21using namespace KGAPI2;
22
23namespace
24{
25static const auto ParentParam = QStringLiteral("parent");
26static const auto PreviousParam = QStringLiteral("previous");
27}
28
29class Q_DECL_HIDDEN TaskCreateJob::Private
30{
31public:
32 QueueHelper<TaskPtr> tasks;
33 QString taskListId;
34 QString parentId;
35 QString previousId;
36};
37
38TaskCreateJob::TaskCreateJob(const TaskPtr &task, const QString &taskListId, const AccountPtr &account, QObject *parent)
39 : CreateJob(account, parent)
40 , d(new Private)
41{
42 d->tasks << task;
43 d->taskListId = taskListId;
44}
45
46TaskCreateJob::TaskCreateJob(const TasksList &tasks, const QString &taskListId, const AccountPtr &account, QObject *parent)
47 : CreateJob(account, parent)
48 , d(new Private)
49{
50 d->tasks = tasks;
51 d->taskListId = taskListId;
52}
53
55
57{
58 return d->parentId;
59}
60
62{
63 if (isRunning()) {
64 qCWarning(KGAPIDebug) << "Can't modify parentItem property when job is running!";
65 return;
66 }
67
68 d->parentId = parentId;
69}
70
72{
73 return d->previousId;
74}
75
76void TaskCreateJob::setPrevious(const QString &previousId)
77{
78 if (isRunning()) {
79 qCWarning(KGAPIDebug) << "Can't modify previous property when job is running!";
80 return;
81 }
82
83 d->previousId = previousId;
84}
85
87{
88 if (d->tasks.atEnd()) {
90 return;
91 }
92
93 const TaskPtr task = d->tasks.current();
94
95 QUrl url = TasksService::createTaskUrl(d->taskListId);
96 QUrlQuery query(url);
97 if (!d->parentId.isEmpty()) {
98 query.addQueryItem(ParentParam, d->parentId);
99 }
100 if (!d->previousId.isEmpty()) {
101 query.addQueryItem(PreviousParam, d->previousId);
102 }
103 url.setQuery(query);
104 QNetworkRequest request(url);
105
106 const QByteArray rawData = TasksService::taskToJSON(task);
107
108 enqueueRequest(request, rawData, QStringLiteral("application/json"));
109}
110
112{
113 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
114 ContentType ct = Utils::stringToContentType(contentType);
116 if (ct != KGAPI2::JSON) {
118 setErrorString(tr("Invalid response content type"));
119 emitFinished();
120 return items;
121 }
122
124 d->tasks.currentProcessed();
125
126 // Enqueue next item or finish
127 start();
128
129 return items;
130}
131
132#include "moc_taskcreatejob.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
bool isRunning
Whether the job is running.
Definition job.h:67
Base class for all objects.
Definition object.h:31
A job to create one or more new tasks in Google Tasks.
~TaskCreateJob() override
Destructor.
void setParentItem(const QString &parentId)
Sets ID of parent task to create new tasks in.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
A reply handler that returns items parsed from @ rawData.
void start() override
This method is invoked when job is started.
QString parentItem
Specified ID of item that the newly created tasks will be subtasks of.
QString previous
Previous sibling task identifier.
TaskCreateJob(const TaskPtr &task, const QString &taskListId, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will create given task in a tasklist with id taskListId.
void setPrevious(const QString &previousId)
Sets previous sibling task identifier.
QByteArray taskToJSON(const TaskPtr &task)
Serializes a Task object into JSON.
QUrl createTaskUrl(const QString &tasklistID)
Returns URL to create a single task.
TaskPtr JSONToTask(const QByteArray &jsonData)
Parses JSON data into a Task 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
QVariant header(QNetworkRequest::KnownHeaders header) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QSharedPointer< X > dynamicCast() const const
void setQuery(const QString &query, ParsingMode mode)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.