Kgapi

tasklistcreatejob.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 "tasklistcreatejob.h"
10 #include "tasksservice.h"
11 #include "account.h"
12 #include "../debug.h"
13 #include "tasklist.h"
14 #include "utils.h"
15 #include "private/queuehelper_p.h"
16 
17 #include <QNetworkRequest>
18 #include <QNetworkReply>
19 
20 
21 using namespace KGAPI2;
22 
23 class Q_DECL_HIDDEN TaskListCreateJob::Private
24 {
25  public:
26  QueueHelper<TaskListPtr> taskLists;
27 };
28 
29 TaskListCreateJob::TaskListCreateJob(const TaskListPtr& taskList, const AccountPtr& account, QObject* parent):
30  CreateJob(account, parent),
31  d(new Private)
32 {
33  d->taskLists << taskList;
34 }
35 
37  const AccountPtr& account, QObject* parent):
38  CreateJob(account, parent),
39  d(new Private)
40 {
41  d->taskLists = taskLists;
42 }
43 
45 
47 {
48  if (d->taskLists.atEnd()) {
49  emitFinished();
50  return;
51  }
52 
53  const TaskListPtr taskList = d->taskLists.current();
54 
56  QNetworkRequest request(url);
57 
58  const QByteArray rawData = TasksService::taskListToJSON(taskList);
59 
60  QStringList headers;
61  const auto rawHeaderList = request.rawHeaderList();
62  headers.reserve(rawHeaderList.size());
63  for (const QByteArray &str : qAsConst(rawHeaderList)) {
64  headers << QLatin1String(str) + QLatin1String(": ") + QLatin1String(request.rawHeader(str));
65  }
66 
67  enqueueRequest(request, rawData, QStringLiteral("application/json"));
68 }
69 
71 {
72  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
73  ContentType ct = Utils::stringToContentType(contentType);
75  if (ct != KGAPI2::JSON) {
77  setErrorString(tr("Invalid response content type"));
78  emitFinished();
79  return items;
80  }
81 
82  items << TasksService::JSONToTaskList(rawData).dynamicCast<Object>();
83  d->taskLists.currentProcessed();
84  // Enqueue next item or finish
85  start();
86 
87  return items;
88 }
89 
90 
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::CreateJob::handleReplyWithItems implementation.
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:425
TaskListPtr JSONToTaskList(const QByteArray &jsonData)
Parses JSON data into a TaskList object.
QUrl createTaskListUrl()
Returns URL for creating a new tasklist.
virtual ObjectsList items() const
Definition: createjob.cpp:40
QList< QByteArray > rawHeaderList() const const
Base class for all objects.
Definition: object.h:31
void start() override
KGAPI2::Job::start implementation.
Abstract superclass for all jobs that create new objects on the server.
Definition: createjob.h:25
void reserve(int alloc)
QByteArray rawHeader(const QByteArray &headerName) const const
~TaskListCreateJob() override
Destructor.
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:497
QByteArray taskListToJSON(const TaskListPtr &taskList)
Serializes a TaskList into JSON data.
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:16
LibKGAPI error - Google returned invalid response.
Definition: types.h:180
TaskListCreateJob(const TaskListPtr &taskList, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will create given taskList in user&#39;s Google Tasks account.
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
A job to create one or more new tasklists in user&#39;s Google Tasks account.
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:375
QObject * parent() const const
QString toString() const const
ContentType
Definition: types.h:207
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.