Skip to content

Latest commit

 

History

History
194 lines (170 loc) · 6.53 KB

report.cpp

File metadata and controls

194 lines (170 loc) · 6.53 KB
 
1
/*
Mar 3, 2014
Mar 3, 2014
2
* This file is part of buteo-sync-plugin-caldav package
3
4
5
6
*
* Copyright (C) 2013 Jolla Ltd. and/or its subsidiary(-ies).
*
* Contributors: Mani Chandrasekar <maninc@gmail.com>
Sep 7, 2014
Sep 7, 2014
7
* Stephan Rave <mail@stephanrave.de>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include "report.h"
#include "reader.h"
#include "settings.h"
#include <QNetworkAccessManager>
#include <QBuffer>
#include <QDebug>
#include <QStringList>
Aug 18, 2021
Aug 18, 2021
34
#include "logging.h"
Jul 22, 2014
Jul 22, 2014
35
Oct 8, 2020
Oct 8, 2020
36
37
#define PROP_URI "uri"
Apr 24, 2014
Apr 24, 2014
38
39
40
41
42
43
static const QString DateTimeFormat = QStringLiteral("yyyyMMddTHHmmss");
static const QString DateTimeFormatUTC = DateTimeFormat + QStringLiteral("Z");
static QString dateTimeToString(const QDateTime &dt)
{
if (dt.timeSpec() == Qt::UTC) {
Mar 4, 2016
Mar 4, 2016
44
return QLocale::c().toString(dt, DateTimeFormatUTC);
Apr 24, 2014
Apr 24, 2014
45
} else {
Mar 4, 2016
Mar 4, 2016
46
return QLocale::c().toString(dt, DateTimeFormat);
Apr 24, 2014
Apr 24, 2014
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
}
}
static QByteArray timeRangeFilterXml(const QDateTime &fromDateTime, const QDateTime &toDateTime)
{
QByteArray xml;
if (fromDateTime.isValid() || toDateTime.isValid()) {
xml = "<c:comp-filter name=\"VEVENT\"> <c:time-range ";
if (fromDateTime.isValid()) {
xml += "start=\"" + dateTimeToString(fromDateTime) + "\" ";
}
if (toDateTime.isValid()) {
xml += "end=\"" + dateTimeToString(toDateTime) + "\" ";
}
xml += " /></c:comp-filter>";
}
return xml;
}
Apr 10, 2014
Apr 10, 2014
67
Report::Report(QNetworkAccessManager *manager, Settings *settings, QObject *parent)
Feb 19, 2014
Feb 19, 2014
68
: Request(manager, settings, "REPORT", parent)
69
{
Aug 18, 2021
Aug 18, 2021
70
FUNCTION_CALL_TRACE(lcCalDavTrace);
71
72
}
May 7, 2015
May 7, 2015
73
void Report::getAllEvents(const QString &remoteCalendarPath, const QDateTime &fromDateTime, const QDateTime &toDateTime)
Apr 3, 2014
Apr 3, 2014
74
{
Aug 18, 2021
Aug 18, 2021
75
FUNCTION_CALL_TRACE(lcCalDavTrace);
May 7, 2015
May 7, 2015
76
sendCalendarQuery(remoteCalendarPath, fromDateTime, toDateTime, true);
77
78
}
May 7, 2015
May 7, 2015
79
void Report::getAllETags(const QString &remoteCalendarPath, const QDateTime &fromDateTime, const QDateTime &toDateTime)
Feb 19, 2014
Feb 19, 2014
80
{
Aug 18, 2021
Aug 18, 2021
81
FUNCTION_CALL_TRACE(lcCalDavTrace);
May 7, 2015
May 7, 2015
82
sendCalendarQuery(remoteCalendarPath, fromDateTime, toDateTime, false);
Sep 7, 2014
Sep 7, 2014
83
}
84
May 7, 2015
May 7, 2015
85
void Report::sendCalendarQuery(const QString &remoteCalendarPath,
Sep 7, 2014
Sep 7, 2014
86
87
88
89
const QDateTime &fromDateTime,
const QDateTime &toDateTime,
bool getCalendarData)
{
Aug 18, 2021
Aug 18, 2021
90
FUNCTION_CALL_TRACE(lcCalDavTrace);
Apr 24, 2014
Apr 24, 2014
91
92
93
QByteArray requestData = \
"<c:calendar-query xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">" \
"<d:prop>" \
Sep 7, 2014
Sep 7, 2014
94
95
96
97
98
99
"<d:getetag />";
if (getCalendarData) {
requestData += \
"<c:calendar-data />";
}
requestData += \
Apr 24, 2014
Apr 24, 2014
100
101
102
103
104
105
106
107
108
109
"</d:prop>"
"<c:filter>" \
"<c:comp-filter name=\"VCALENDAR\">";
if (fromDateTime.isValid() || toDateTime.isValid()) {
requestData.append(timeRangeFilterXml(fromDateTime, toDateTime));
}
requestData += \
"</c:comp-filter>" \
"</c:filter>" \
"</c:calendar-query>";
May 7, 2015
May 7, 2015
110
sendRequest(remoteCalendarPath, requestData);
111
112
}
May 7, 2015
May 7, 2015
113
void Report::multiGetEvents(const QString &remoteCalendarPath, const QStringList &eventHrefList)
May 20, 2019
May 20, 2019
114
{
Aug 18, 2021
Aug 18, 2021
115
FUNCTION_CALL_TRACE(lcCalDavTrace);
May 7, 2015
May 7, 2015
116
if (eventHrefList.isEmpty()) {
Apr 3, 2014
Apr 3, 2014
117
return;
118
}
Sep 7, 2014
Sep 7, 2014
119
120
QByteArray requestData = "<c:calendar-multiget xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">" \
Jun 4, 2020
Jun 4, 2020
121
"<d:prop><d:getetag /><c:calendar-data /></d:prop>";
Nov 14, 2019
Nov 14, 2019
122
for (const QString &eventHref : eventHrefList) {
Sep 7, 2014
Sep 7, 2014
123
requestData.append("<d:href>");
May 7, 2015
May 7, 2015
124
requestData.append(eventHref.toUtf8());
Sep 7, 2014
Sep 7, 2014
125
126
127
128
requestData.append("</d:href>");
}
requestData.append("</c:calendar-multiget>");
May 7, 2015
May 7, 2015
129
sendRequest(remoteCalendarPath, requestData);
Oct 8, 2020
Oct 8, 2020
130
131
mFetchedUris = eventHrefList;
May 20, 2019
May 20, 2019
132
}
Sep 7, 2014
Sep 7, 2014
133
May 7, 2015
May 7, 2015
134
void Report::sendRequest(const QString &remoteCalendarPath, const QByteArray &requestData)
Sep 7, 2014
Sep 7, 2014
135
{
Aug 18, 2021
Aug 18, 2021
136
FUNCTION_CALL_TRACE(lcCalDavTrace);
May 7, 2015
May 7, 2015
137
mRemoteCalendarPath = remoteCalendarPath;
Sep 7, 2014
Sep 7, 2014
138
Apr 3, 2014
Apr 3, 2014
139
QNetworkRequest request;
May 7, 2015
May 7, 2015
140
prepareRequest(&request, remoteCalendarPath);
141
142
request.setRawHeader("Depth", "1");
request.setRawHeader("Prefer", "return-minimal");
Nov 4, 2014
Nov 4, 2014
143
request.setHeader(QNetworkRequest::ContentLengthHeader, requestData.length());
144
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/xml; charset=utf-8");
Mar 3, 2014
Mar 3, 2014
145
QBuffer *buffer = new QBuffer(this);
Sep 7, 2014
Sep 7, 2014
146
buffer->setData(requestData);
Aug 14, 2019
Aug 14, 2019
147
// TODO: when Qt5.8 is available, remove the use of buffer, and pass requestData directly.
Mar 3, 2014
Mar 3, 2014
148
QNetworkReply *reply = mNAManager->sendCustomRequest(request, REQUEST_TYPE.toLatin1(), buffer);
Oct 8, 2020
Oct 8, 2020
149
reply->setProperty(PROP_URI, remoteCalendarPath);
Sep 7, 2014
Sep 7, 2014
150
debugRequest(request, buffer->buffer());
Sep 19, 2021
Sep 19, 2021
151
connect(reply, SIGNAL(finished()), this, SLOT(requestFinished()));
Mar 3, 2014
Mar 3, 2014
152
connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
153
154
155
this, SLOT(slotSslErrors(QList<QSslError>)));
}
Sep 19, 2021
Sep 19, 2021
156
void Report::handleReply(QNetworkReply *reply)
Feb 19, 2014
Feb 19, 2014
157
{
Aug 18, 2021
Aug 18, 2021
158
FUNCTION_CALL_TRACE(lcCalDavTrace);
Mar 3, 2014
Mar 3, 2014
159
Oct 8, 2020
Oct 8, 2020
160
const QString &uri = reply->property(PROP_URI).toString();
May 14, 2014
May 14, 2014
161
if (reply->error() != QNetworkReply::NoError) {
Sep 19, 2021
Sep 19, 2021
162
finishedWithReplyResult(uri, reply);
May 14, 2014
May 14, 2014
163
164
return;
}
165
Sep 19, 2021
Sep 19, 2021
166
const QByteArray data = reply->readAll();
Apr 10, 2014
Apr 10, 2014
167
debugReply(*reply, data);
Mar 3, 2014
Mar 3, 2014
168
Sep 7, 2014
Sep 7, 2014
169
if (!data.isNull() && !data.isEmpty()) {
170
171
Reader reader;
reader.read(data);
Dec 9, 2016
Dec 9, 2016
172
if (reader.hasError()) {
Sep 19, 2021
Sep 19, 2021
173
174
finishedWithError(uri, Buteo::SyncResults::INTERNAL_ERROR,
QString("Malformed response body for REPORT"), data);
Dec 9, 2016
Dec 9, 2016
175
176
} else {
mReceivedResources = reader.results();
Oct 8, 2020
Oct 8, 2020
177
finishedWithSuccess(uri);
Dec 9, 2016
Dec 9, 2016
178
}
Apr 10, 2014
Apr 10, 2014
179
} else {
Sep 19, 2021
Sep 19, 2021
180
181
182
finishedWithError(uri, Buteo::SyncResults::INTERNAL_ERROR,
QString("Empty response body for REPORT"),
QByteArray());
183
184
185
}
}
May 20, 2019
May 20, 2019
186
const QList<Reader::CalendarResource>& Report::receivedCalendarResources() const
Apr 10, 2014
Apr 10, 2014
187
188
189
{
return mReceivedResources;
}
Oct 8, 2020
Oct 8, 2020
190
191
192
193
194
const QStringList& Report::fetchedUris() const
{
return mFetchedUris;
}