Kgapi

freebusyqueryjob.cpp
1/*
2 * SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7#include "freebusyqueryjob.h"
8#include "calendarservice.h"
9#include "utils.h"
10
11#include <QJsonDocument>
12#include <QNetworkAccessManager>
13#include <QNetworkReply>
14#include <QNetworkRequest>
15#include <QVariantMap>
16
17using namespace KGAPI2;
18
19class Q_DECL_HIDDEN FreeBusyQueryJob::Private
20{
21public:
22 Private(const QString &id, const QDateTime &timeMin, const QDateTime &timeMax)
23 : id(id)
24 , timeMin(timeMin)
25 , timeMax(timeMax)
26 {
27 }
28
29 const QString id;
30 const QDateTime timeMin;
31 const QDateTime timeMax;
33};
34
35FreeBusyQueryJob::FreeBusyQueryJob(const QString &id, const QDateTime &timeMin, const QDateTime &timeMax, const AccountPtr &account, QObject *parent)
36 : FetchJob(account, parent)
37 , d(new FreeBusyQueryJob::Private(id, timeMin, timeMax))
38{
39}
40
41FreeBusyQueryJob::~FreeBusyQueryJob() = default;
42
43FreeBusyQueryJob::BusyRangeList FreeBusyQueryJob::busy() const
44{
45 return d->busy;
46}
47
48QString FreeBusyQueryJob::id() const
49{
50 return d->id;
51}
52
53QDateTime FreeBusyQueryJob::timeMin() const
54{
55 return d->timeMin;
56}
57
58QDateTime FreeBusyQueryJob::timeMax() const
59{
60 return d->timeMax;
61}
62
63void FreeBusyQueryJob::start()
64{
65 QVariantMap requestData({{QStringLiteral("timeMin"), Utils::rfc3339DateToString(d->timeMin)},
66 {QStringLiteral("timeMax"), Utils::rfc3339DateToString(d->timeMax)},
67 {QStringLiteral("items"), QVariantList({QVariantMap({{QStringLiteral("id"), d->id}})})}});
68 QJsonDocument document = QJsonDocument::fromVariant(requestData);
69 const QByteArray json = document.toJson(QJsonDocument::Compact);
70
72 enqueueRequest(request, json, QStringLiteral("application/json"));
73}
74
75void FreeBusyQueryJob::dispatchRequest(QNetworkAccessManager *accessManager, const QNetworkRequest &request, const QByteArray &data, const QString &contentType)
76{
77 QNetworkRequest r = request;
78 if (!r.hasRawHeader("Content-Type")) {
80 }
81
82 accessManager->post(r, data);
83}
84
85void FreeBusyQueryJob::handleReply(const QNetworkReply *reply, const QByteArray &rawData)
86{
87 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
88 ContentType ct = Utils::stringToContentType(contentType);
89 if (ct == KGAPI2::JSON) {
90 const QJsonDocument document = QJsonDocument::fromJson(rawData);
91 const QVariantMap data = document.toVariant().toMap();
92 const QVariantMap cals = data[QStringLiteral("calendars")].toMap();
93 const QVariantMap cal = cals[d->id].toMap();
94 if (cal.contains(QStringLiteral("errors"))) {
96 setErrorString(tr("FreeBusy information is not available"));
97 } else {
98 const QVariantList busyList = cal[QStringLiteral("busy")].toList();
99 for (const QVariant &busyV : busyList) {
100 const QVariantMap busy = busyV.toMap();
101 d->busy << BusyRange{Utils::rfc3339DateFromString(busy[QStringLiteral("start")].toString()),
102 Utils::rfc3339DateFromString(busy[QStringLiteral("end")].toString())};
103 }
104 }
105 } else {
107 setErrorString(tr("Invalid response content type"));
108 }
109
110 emitFinished();
111}
Abstract superclass for all jobs that fetch resources from Google.
Definition fetchjob.h:25
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
char * toString(const EngineQuery &query)
QNetworkRequest prepareRequest(const QUrl &url)
Preparse a QNetworkRequest for given URL.
QUrl freeBusyQueryUrl()
Returns URL for freebusy queries.
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
@ NotFound
Requested object was not found on the remote side.
Definition types.h:200
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
ContentType
Definition types.h:210
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonDocument fromVariant(const QVariant &variant)
QVariant toVariant() const const
QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)
QVariant header(QNetworkRequest::KnownHeaders header) const const
bool hasRawHeader(const QByteArray &headerName) const const
void setHeader(KnownHeaders header, const QVariant &value)
QString tr(const char *sourceText, const char *disambiguation, int n)
QMap< QString, QVariant > toMap() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:36:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.