Kgapi

taskcreatejob.cpp
1 /*
2  * This file is part of LibKGAPI library
3  *
4  * SPDX-FileCopyrightText: 2013 Daniel Vrátil <[email protected]>
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 "tasksservice.h"
11 #include "account.h"
12 #include "../debug.h"
13 #include "utils.h"
14 #include "task.h"
15 #include "private/queuehelper_p.h"
16 
17 #include <QNetworkRequest>
18 #include <QNetworkReply>
19 #include <QUrlQuery>
20 
21 using namespace KGAPI2;
22 
23 namespace {
24  static const auto ParentParam = QStringLiteral("parent");
25  static const auto PreviousParam = QStringLiteral("previous");
26 }
27 
28 class Q_DECL_HIDDEN TaskCreateJob::Private
29 {
30  public:
31  QueueHelper<TaskPtr> tasks;
32  QString taskListId;
33  QString parentId;
34  QString previousId;
35 };
36 
37 TaskCreateJob::TaskCreateJob(const TaskPtr& task, const QString& taskListId,
38  const AccountPtr& account, QObject* parent):
39  CreateJob(account, parent),
40  d(new Private)
41 {
42  d->tasks << task;
43  d->taskListId = taskListId;
44 }
45 
46 TaskCreateJob::TaskCreateJob(const TasksList& tasks, const QString& taskListId,
47  const AccountPtr& account, QObject* parent):
48  CreateJob(account, parent),
49  d(new Private)
50 {
51  d->tasks = tasks;
52  d->taskListId = taskListId;
53 }
54 
56 
58 {
59  return d->parentId;
60 }
61 
63 {
64  if (isRunning()) {
65  qCWarning(KGAPIDebug) << "Can't modify parentItem property when job is running!";
66  return;
67  }
68 
69  d->parentId = parentId;
70 }
71 
73 {
74  return d->previousId;
75 }
76 
77 void TaskCreateJob::setPrevious(const QString &previousId)
78 {
79  if (isRunning()) {
80  qCWarning(KGAPIDebug) << "Can't modify previous property when job is running!";
81  return;
82  }
83 
84  d->previousId = previousId;
85 }
86 
88 {
89  if (d->tasks.atEnd()) {
90  emitFinished();
91  return;
92  }
93 
94  const TaskPtr task = d->tasks.current();
95 
96  QUrl url = TasksService::createTaskUrl(d->taskListId);
97  QUrlQuery query(url);
98  if (!d->parentId.isEmpty()) {
99  query.addQueryItem(ParentParam, d->parentId);
100  }
101  if (!d->previousId.isEmpty()) {
102  query.addQueryItem(PreviousParam, d->previousId);
103  }
104  url.setQuery(query);
105  QNetworkRequest request(url);
106 
107  const QByteArray rawData = TasksService::taskToJSON(task);
108 
109  enqueueRequest(request, rawData, QStringLiteral("application/json"));
110 }
111 
113 {
114  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
115  ContentType ct = Utils::stringToContentType(contentType);
117  if (ct != KGAPI2::JSON) {
119  setErrorString(tr("Invalid response content type"));
120  emitFinished();
121  return items;
122  }
123 
124  items << TasksService::JSONToTask(rawData).dynamicCast<Object>();
125  d->tasks.currentProcessed();
126 
127  // Enqueue next item or finish
128  start();
129 
130  return items;
131 }
132 
133 
~TaskCreateJob() override
Destructor.
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:425
bool isRunning() const
Whether job is running.
QByteArray taskToJSON(const TaskPtr &task)
Serializes a Task object into JSON.
virtual ObjectsList items() const
Definition: createjob.cpp:40
Base class for all objects.
Definition: object.h:31
Abstract superclass for all jobs that create new objects on the server.
Definition: createjob.h:25
void setPrevious(const QString &previousId)
Sets previous sibling task identifier.
QString parentItem() const
Returns ID of task the new items will be stored as subtasks of.
QString tr(const char *sourceText, const char *disambiguation, int n)
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 setParentItem(const QString &parentId)
Sets ID of parent task to create new tasks in.
void addQueryItem(const QString &key, const QString &value)
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:497
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:16
void start() override
This method is invoked when job is started.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
A reply handler that returns items parsed from @ rawData.
QUrl createTaskUrl(const QString &tasklistID)
Returns URL to create a single task.
LibKGAPI error - Google returned invalid response.
Definition: types.h:180
QVariant header(QNetworkRequest::KnownHeaders header) const const
virtual void emitFinished()
Emits Job::finished() signal.
Definition: job.cpp:479
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition: job.cpp:390
QSharedPointer< X > dynamicCast() const const
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:375
void setQuery(const QString &query, QUrl::ParsingMode mode)
A job to create one or more new tasks in Google Tasks.
Definition: taskcreatejob.h:25
QObject * parent() const const
TaskPtr JSONToTask(const QByteArray &jsonData)
Parses JSON data into a Task object.
QString toString() const const
ContentType
Definition: types.h:207
QString previous() const
Previous sibling task identifier.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Sep 19 2020 23:09:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.