Kgapi

eventcreatejob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "eventcreatejob.h"
10#include "account.h"
11#include "calendarservice.h"
12#include "event.h"
13#include "private/queuehelper_p.h"
14#include "utils.h"
15
16#include <QNetworkReply>
17#include <QNetworkRequest>
18
19using namespace KGAPI2;
20
21class Q_DECL_HIDDEN EventCreateJob::Private
22{
23public:
24 QueueHelper<EventPtr> events;
25 QString calendarId;
26 SendUpdatesPolicy updatesPolicy = SendUpdatesPolicy::All;
27};
28
29EventCreateJob::EventCreateJob(const EventPtr &event, const QString &calendarId, const AccountPtr &account, QObject *parent)
30 : CreateJob(account, parent)
31 , d(new Private)
32{
33 d->events << event;
34 d->calendarId = calendarId;
35}
36
37EventCreateJob::EventCreateJob(const EventsList &events, const QString &calendarId, const AccountPtr &account, QObject *parent)
38 : CreateJob(account, parent)
39 , d(new Private)
40{
41 d->events = events;
42 d->calendarId = calendarId;
43}
44
46
47void EventCreateJob::setSendUpdates(SendUpdatesPolicy policy)
48{
49 if (d->updatesPolicy != policy) {
50 d->updatesPolicy = policy;
51 Q_EMIT sendUpdatesChanged(d->updatesPolicy);
52 }
53}
54
55SendUpdatesPolicy EventCreateJob::sendUpdates() const
56{
57 return d->updatesPolicy;
58}
59
61{
62 if (d->events.atEnd()) {
64 return;
65 }
66
67 const EventPtr event = d->events.current();
68 QUrl requestUrl;
69
70 // If the organizer is different from the account name, import a private copy of the event in the user's calendar,
71 // or normally create it otherwise. This prevents that Google Calendar creates a copy event when accepting invitations
72 // to events created by others.
73 if (!event->attendees().isEmpty() && !event->organizer().isEmpty() && event->organizer().email() != this->account()->accountName()) {
74 requestUrl = CalendarService::importEventUrl(d->calendarId, d->updatesPolicy);
75 } else {
76 requestUrl = CalendarService::createEventUrl(d->calendarId, d->updatesPolicy);
77 }
78
79 const auto request = CalendarService::prepareRequest(requestUrl);
80 const QByteArray rawData = CalendarService::eventToJSON(event, CalendarService::EventSerializeFlag::NoID);
81
82 enqueueRequest(request, rawData, QStringLiteral("application/json"));
83}
84
86{
87 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
88 ContentType ct = Utils::stringToContentType(contentType);
90 if (ct != KGAPI2::JSON) {
92 setErrorString(tr("Invalid response content type"));
94 return items;
95 }
96
98 d->events.currentProcessed();
99 // Enqueue next item or finish
100 start();
101
102 return items;
103}
Abstract superclass for all jobs that create new objects on the server.
Definition createjob.h:26
virtual ObjectsList items() const
Definition createjob.cpp:40
A job to create one or more new events in given calendar.
void start() override
KGAPI2::Job::start implementation.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::CreateJob::handleReplyWithItems implementation.
EventCreateJob(const EventPtr &event, const QString &calendarId, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will create given event in a calendar with given calendarID.
~EventCreateJob() override
Destructor.
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition job.cpp:401
virtual void emitFinished()
Emits Job::finished() signal.
Definition job.cpp:493
void setError(KGAPI2::Error error)
Set job error to error.
Definition job.cpp:386
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition job.cpp:513
Base class for all objects.
Definition object.h:31
QByteArray eventToJSON(const EventPtr &event, EventSerializeFlags flags)
Serializes Event into JSON.
QUrl createEventUrl(const QString &calendarID, SendUpdatesPolicy updatePolicy)
Returns URL creating new events.
QUrl importEventUrl(const QString &calendarID, SendUpdatesPolicy updatePolicy)
Returns URL importing private copies of existing events.
QNetworkRequest prepareRequest(const QUrl &url)
Preparse a QNetworkRequest for given URL.
EventPtr JSONToEvent(const QByteArray &jsonData)
Parses event JSON into Event object.
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
SendUpdatesPolicy
Determines whether Google Calendar should send updates to participants.
Definition enums.h:22
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
ContentType
Definition types.h:210
QVariant header(QNetworkRequest::KnownHeaders header) const const
Q_EMITQ_EMIT
virtual bool event(QEvent *e)
QString tr(const char *sourceText, const char *disambiguation, int n)
QSharedPointer< X > dynamicCast() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.