Kgapi

eventmodifyjob.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 "eventmodifyjob.h"
10 #include "calendarservice.h"
11 #include "account.h"
12 #include "../debug.h"
13 #include "event.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 EventModifyJob::Private
24 {
25  public:
26  QueueHelper<EventPtr> events;
27  QString calendarId;
28  SendUpdatesPolicy updatesPolicy = SendUpdatesPolicy::All;
29 };
30 
31 EventModifyJob::EventModifyJob(const EventPtr& event, const QString& calendarId, const AccountPtr& account, QObject* parent):
32  ModifyJob(account, parent),
33  d(new Private)
34 {
35  d->events << event;
36  d->calendarId = calendarId;
37 }
38 
40  ModifyJob(account, parent),
41  d(new Private)
42 {
43  d->events = events;
44  d->calendarId = calendarId;
45 }
46 
48 
49 void EventModifyJob::setSendUpdates(KGAPI2::SendUpdatesPolicy updatesPolicy)
50 {
51  if (d->updatesPolicy != updatesPolicy) {
52  d->updatesPolicy = updatesPolicy;
53  Q_EMIT sendUpdatesChanged(d->updatesPolicy);
54  }
55 }
56 
57 SendUpdatesPolicy EventModifyJob::sendUpdates() const
58 {
59  return d->updatesPolicy;
60 }
61 
63 {
64  if (d->events.atEnd()) {
65  emitFinished();
66  return;
67  }
68 
69  const EventPtr event = d->events.current();
70  const auto request = CalendarService::prepareRequest(CalendarService::updateEventUrl(d->calendarId, event->id(), d->updatesPolicy));
72 
73  enqueueRequest(request, rawData, QStringLiteral("application/json"));
74 }
75 
77 {
78  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
79  ContentType ct = Utils::stringToContentType(contentType);
81  if (ct != KGAPI2::JSON) {
83  setErrorString(tr("Invalid response content type"));
84  emitFinished();
85  return items;
86  }
87 
88  items << CalendarService::JSONToEvent(rawData).dynamicCast<Event>();
89  d->events.currentProcessed();
90  // Enqueue next item or finish
91  start();
92 
93  return items;
94 }
95 
96 
QNetworkRequest prepareRequest(const QUrl &url)
Preparse a QNetworkRequest for given URL.
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:423
QUrl updateEventUrl(const QString &calendarID, const QString &eventID, SendUpdatesPolicy updatePolicy)
Returns URL for updating a single event.
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual bool event(QEvent *e)
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:495
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:16
Represents a single event from Google Calendar.
Definition: event.h:29
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:477
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition: job.cpp:388
QSharedPointer< X > dynamicCast() const const
A job to modify events in a calendar on user&#39;s Google Calendar account.
ObjectsList items() const
Definition: modifyjob.cpp:43
EventModifyJob(const EventPtr &event, const QString &calendarId, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will write changes in given event to corresponding event in calendar with given...
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:373
~EventModifyJob() override
Destructor.
Abstract superclass for all jobs that somehow modify resources on Google.
Definition: modifyjob.h:24
SendUpdatesPolicy
Determines whether Google Calendar should send updates to participants.
Definition: enums.h:27
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::ModifyJob::handleReplyWithItems implementation.
QByteArray eventToJSON(const EventPtr &event, EventSerializeFlags flags)
Serializes Event into JSON.
QObject * parent() const const
QString toString() const const
EventPtr JSONToEvent(const QByteArray &jsonData)
Parses event JSON into Event object.
Q_EMITQ_EMIT
void start() override
KGAPI2::Job::start implementation.
ContentType
Definition: types.h:205
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 5 2020 23:10:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.