Kgapi

calendarmodifyjob.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 "calendarmodifyjob.h"
24 #include "calendarservice.h"
25 #include "account.h"
26 #include "calendar.h"
27 #include "../debug.h"
28 #include "utils.h"
29 #include "private/queuehelper_p.h"
30 
31 
32 #include <QNetworkRequest>
33 #include <QNetworkReply>
34 
35 using namespace KGAPI2;
36 
37 class Q_DECL_HIDDEN CalendarModifyJob::Private
38 {
39  public:
40  QueueHelper<CalendarPtr> calendars;
41 };
42 
43 CalendarModifyJob::CalendarModifyJob(const CalendarPtr& calendar, const AccountPtr& account, QObject* parent):
44  ModifyJob(account, parent),
45  d(new Private)
46 {
47  d->calendars << calendar;
48 }
49 
51  ModifyJob(account, parent),
52  d(new Private)
53 {
54  d->calendars = calendars;
55 }
56 
58 
60 {
61  if (d->calendars.atEnd()) {
62  emitFinished();
63  return;
64  }
65 
66  const CalendarPtr calendar = d->calendars.current();
67  const auto request = CalendarService::prepareRequest(CalendarService::updateCalendarUrl(calendar->uid()));
68  const QByteArray rawData = CalendarService::calendarToJSON(calendar);
69 
70  enqueueRequest(request, rawData, QStringLiteral("application/json"));
71 }
72 
74 {
75  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
76  ContentType ct = Utils::stringToContentType(contentType);
78  if (ct != KGAPI2::JSON) {
80  setErrorString(tr("Invalid response content type"));
81  emitFinished();
82  return items;
83  }
84 
86  d->calendars.currentProcessed();
87  // Enqueue next item or finish
88  start();
89 
90  return items;
91 }
92 
93 
QNetworkRequest prepareRequest(const QUrl &url)
Preparse a QNetworkRequest for given URL.
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:437
CalendarModifyJob(const CalendarPtr &calendar, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will write changes in given calendar to corresponding calendar on user&#39;s Google...
Base class for all objects.
Definition: object.h:45
QUrl updateCalendarUrl(const QString &calendarID)
Returns URL for updating existing calendar.
void start() override
KGAPI2::Job::start implementation.
QString tr(const char *sourceText, const char *disambiguation, int n)
~CalendarModifyJob() override
Destructor.
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
LibKGAPI error - Google returned invalid response.
Definition: types.h:194
A job to modify calendars on user&#39;s Google Calendar account.
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
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:387
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::ModifyJob::handleReplyWithItems implementation.
Abstract superclass for all jobs that somehow modify resources on Google.
Definition: modifyjob.h:38
QObject * parent() const const
QString toString() const const
QByteArray calendarToJSON(const CalendarPtr &calendar)
Serializes calendar into JSON.
ContentType
Definition: types.h:219
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:36:27 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.