Kgapi

taskmodifyjob.cpp
1 /*
2  * This file is part of LibKGAPI library
3  *
4  * Copyright (C) 2013 Daniel Vrátil <[email protected]>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
23 #include "taskmodifyjob.h"
24 #include "tasksservice.h"
25 #include "account.h"
26 #include "../debug.h"
27 #include "task.h"
28 #include "utils.h"
29 #include "private/queuehelper_p.h"
30 
31 #include <QNetworkRequest>
32 #include <QNetworkReply>
33 
34 
35 using namespace KGAPI2;
36 
37 class Q_DECL_HIDDEN TaskModifyJob::Private
38 {
39  public:
40  QueueHelper<TaskPtr> tasks;
41  QString taskListId;
42 };
43 
44 TaskModifyJob::TaskModifyJob(const TaskPtr& task, const QString& taskListId,
45  const AccountPtr& account, QObject* parent):
46  ModifyJob(account, parent),
47  d(new Private)
48 {
49  d->tasks << task;
50  d->taskListId = taskListId;
51 }
52 
53 TaskModifyJob::TaskModifyJob(const TasksList& tasks, const QString& taskListId,
54  const AccountPtr& account, QObject* parent):
55  ModifyJob(account, parent),
56  d(new Private)
57 {
58  d->tasks = tasks;
59  d->taskListId = taskListId;
60 }
61 
63 
65 {
66  if (d->tasks.atEnd()) {
67  emitFinished();
68  return;
69  }
70 
71  const TaskPtr task = d->tasks.current();
72  const QUrl url = TasksService::updateTaskUrl(d->taskListId, task->uid());
73  QNetworkRequest request(url);
74 
75  const QByteArray rawData = TasksService::taskToJSON(task);
76 
77  QStringList headers;
78  const auto rawHeaderList = request.rawHeaderList();
79  headers.reserve(rawHeaderList.size());
80  for (const QByteArray &str : qAsConst(rawHeaderList)) {
81  headers << QLatin1String(str) + QLatin1String(": ") + QLatin1String(request.rawHeader(str));
82  }
83 
84  enqueueRequest(request, rawData, QStringLiteral("application/json"));
85 }
86 
88 {
89  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
90  ContentType ct = Utils::stringToContentType(contentType);
92  if (ct != KGAPI2::JSON) {
94  setErrorString(tr("Invalid response content type"));
95  emitFinished();
96  return items;
97  }
98 
99  items << TasksService::JSONToTask(rawData).dynamicCast<Object>();
100  d->tasks.currentProcessed();
101  // Enqueue next item or finish
102  start();
103 
104  return items;
105 }
106 
107 
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:437
A job to modify tasks in a tasklist in user&#39;s Google Tasks account.
Definition: taskmodifyjob.h:40
QByteArray taskToJSON(const TaskPtr &task)
Serializes a Task object into JSON.
Base class for all objects.
Definition: object.h:45
void reserve(int alloc)
QUrl updateTaskUrl(const QString &tasklistID, const QString &taskID)
Returns URL for updating a task.
void start() override
KGAPI2::Job::start implementation.
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:509
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:30
~TaskModifyJob() override
Destructor.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::ModifyJob::handleReplyWithItems 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...
LibKGAPI error - Google returned invalid response.
Definition: types.h:194
QVariant header(QNetworkRequest::KnownHeaders header) const const
virtual void emitFinished()
Emits Job::finished() signal.
Definition: job.cpp:491
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition: job.cpp:402
QSharedPointer< X > dynamicCast() const const
ObjectsList items() const
Definition: modifyjob.cpp:57
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:387
Abstract superclass for all jobs that somehow modify resources on Google.
Definition: modifyjob.h:38
QObject * parent() const const
TaskPtr JSONToTask(const QByteArray &jsonData)
Parses JSON data into a Task object.
QString toString() const const
ContentType
Definition: types.h:219
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 31 2020 22:38:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.