Kgapi

calendarcreatejob.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 "calendarcreatejob.h"
10 #include "calendarservice.h"
11 #include "object.h"
12 #include "account.h"
13 #include "calendar.h"
14 #include "../debug.h"
15 #include "utils.h"
16 #include "private/queuehelper_p.h"
17 
18 #include <QNetworkRequest>
19 #include <QNetworkReply>
20 
21 
22 using namespace KGAPI2;
23 
24 class Q_DECL_HIDDEN CalendarCreateJob::Private
25 {
26  public:
27  QueueHelper<CalendarPtr> calendars;
28 };
29 
30 CalendarCreateJob::CalendarCreateJob(const CalendarPtr& calendar, const AccountPtr& account, QObject* parent):
31  CreateJob(account, parent),
32  d(new Private)
33 {
34  d->calendars << calendar;
35 }
36 
38  CreateJob(account, parent),
39  d(new Private)
40 {
41  d->calendars = calendars;
42 }
43 
44 
46 
48 {
49  if (d->calendars.atEnd()) {
50  emitFinished();
51  return;
52  }
53 
54  const CalendarPtr calendar = d->calendars.current();
56  const QByteArray rawData = CalendarService::calendarToJSON(calendar);
57 
58  enqueueRequest(request, rawData, QStringLiteral("application/json"));
59 }
60 
62 {
63  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
64  ContentType ct = Utils::stringToContentType(contentType);
66  if (ct != KGAPI2::JSON) {
68  setErrorString(tr("Invalid response content type"));
69  emitFinished();
70  return items;
71  }
72 
74  d->calendars.currentProcessed();
75  start();
76 
77  return items;
78 }
79 
80 
void start() override
KGAPI2::Job::start implementation.
QNetworkRequest prepareRequest(const QUrl &url)
Preparse a QNetworkRequest for given URL.
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:424
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
QUrl createCalendarUrl()
Returns URL for creating a new calendar.
QString tr(const char *sourceText, const char *disambiguation, int n)
A job to create one or more new calendars in user&#39;s Google Calendar account.
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:496
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
QVariant header(QNetworkRequest::KnownHeaders header) const const
virtual void emitFinished()
Emits Job::finished() signal.
Definition: job.cpp:478
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition: job.cpp:389
QSharedPointer< X > dynamicCast() const const
CalendarCreateJob(const CalendarPtr &calendar, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will create given calendar in user&#39;s Google Calendar account.
CalendarPtr JSONToCalendar(const QByteArray &jsonData)
Parses calendar JSON data into Calendar object.
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:374
~CalendarCreateJob() override
Destructor.
QObject * parent() const const
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::CreateJob::handleReplyWithItems implementation.
QString toString() const const
QByteArray calendarToJSON(const CalendarPtr &calendar)
Serializes calendar into JSON.
ContentType
Definition: types.h:206
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 23:14:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.