Kgapi

eventmovejob.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 "eventmovejob.h"
10 #include "account.h"
11 #include "calendarservice.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 EventMoveJob::Private
24 {
25 public:
26  QueueHelper<QString> eventsIds;
27  QString source;
28  QString destination;
29 };
30 
31 EventMoveJob::EventMoveJob(const EventPtr &event, const QString &sourceCalendarId,
32  const QString &destinationCalendarId, const AccountPtr &account,
33  QObject *parent):
34  ModifyJob(account, parent),
35  d(new Private())
36 {
37  d->eventsIds << event->id();
38  d->source = sourceCalendarId;
39  d->destination = destinationCalendarId;
40 }
41 
42 EventMoveJob::EventMoveJob(const EventsList &events, const QString &sourceCalendarId,
43  const QString &destinationCalendarId, const AccountPtr &account,
44  QObject *parent):
45  ModifyJob(account, parent),
46  d(new Private())
47 {
48  for (const EventPtr &event : events) {
49  d->eventsIds << event->id();
50  }
51  d->source = sourceCalendarId;
52  d->destination = destinationCalendarId;
53 }
54 
55 EventMoveJob::EventMoveJob(const QString &eventId, const QString &sourceCalendarId,
56  const QString &destinationCalendarId, const AccountPtr &account,
57  QObject *parent):
58  ModifyJob(account, parent),
59  d(new Private())
60 {
61  d->eventsIds << eventId;
62  d->source = sourceCalendarId;
63  d->destination = destinationCalendarId;
64 }
65 
66 EventMoveJob::EventMoveJob(const QStringList &eventsIds, const QString &sourceCalendarId,
67  const QString &destinationCalendarId, const AccountPtr &account,
68  QObject *parent):
69  ModifyJob(account, parent),
70  d(new Private())
71 {
72  d->eventsIds = eventsIds;
73  d->source = sourceCalendarId;
74  d->destination = destinationCalendarId;
75 }
76 
77 EventMoveJob::~EventMoveJob() = default;
78 
80 {
81  if (d->eventsIds.atEnd()) {
82  emitFinished();
83  return;
84  }
85 
86  const QString eventId = d->eventsIds.current();
87  const auto request = CalendarService::prepareRequest(CalendarService::moveEventUrl(d->source, d->destination, eventId));
88 
89  enqueueRequest(request);
90 }
91 
92 void EventMoveJob::dispatchRequest(QNetworkAccessManager *accessManager, const QNetworkRequest &request, const QByteArray &data, const QString &contentType)
93 {
94  Q_UNUSED(data)
95  Q_UNUSED(contentType)
96 
97  accessManager->post(request, QByteArray());
98 }
99 
101 {
102  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
103  ContentType ct = Utils::stringToContentType(contentType);
105  if (ct != KGAPI2::JSON) {
107  setErrorString(tr("Invalid response content type"));
108  emitFinished();
109  return items;
110  }
111 
112  items << CalendarService::JSONToEvent(rawData).dynamicCast<Event>();
113  d->eventsIds.currentProcessed();
114  // Enqueue next item or finish
115  start();
116 
117  return items;
118 }
119 
120 
121 
QNetworkRequest prepareRequest(const QUrl &url)
Preparse a QNetworkRequest for given URL.
KGAPI2::ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::Job::handleReplyWithItems implementation.
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:423
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual bool event(QEvent *e)
~EventMoveJob() override
Destructor.
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:495
QNetworkReply * post(const QNetworkRequest &request, QIODevice *data)
A job that moves events between given calendars.
Definition: eventmovejob.h:26
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
void start() override
KGAPI2::Job::start implementation.
ObjectsList items() const
Definition: modifyjob.cpp:43
EventMoveJob(const EventPtr &event, const QString &sourceCalendarId, const QString &destinationCalendarId, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will move given event from a calendar with sourceCalendarId to calendar with de...
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:373
Abstract superclass for all jobs that somehow modify resources on Google.
Definition: modifyjob.h:24
QUrl moveEventUrl(const QString &sourceCalendar, const QString &destCalendar, const QString &eventID)
Returns URL for moving event between calendars.
QObject * parent() const const
QString toString() const const
EventPtr JSONToEvent(const QByteArray &jsonData)
Parses event JSON into Event object.
void dispatchRequest(QNetworkAccessManager *accessManager, const QNetworkRequest &request, const QByteArray &data, const QString &contentType) override
KGAPI2::Job::dispatchRequest implementation.
ContentType
Definition: types.h:205
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 1 2020 23:08:03 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.