File: reader.cpp

package info (click to toggle)
buteo-sync-plugin-caldav 0.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 824 kB
  • sloc: cpp: 5,966; xml: 782; makefile: 17
file content (183 lines) | stat: -rw-r--r-- 5,737 bytes parent folder | download
1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * This file is part of buteo-sync-plugin-caldav package
 *
 * Copyright (C) 2013 Jolla Ltd. and/or its subsidiary(-ies).
 *
 * Contributors: Mani Chandrasekar <maninc@gmail.com>
 *
 * 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 "reader_p.h"
#include "logging_p.h"

#include <QDebug>
#include <QUrl>
#include <QList>
#include <QRegExp>
#include <QByteArray>
#include <QXmlStreamReader>

namespace {
   /* Some servers don't XML-escape the ics content when they return it
    * in the XML stream, so we need to fix any issues.
    * Note that this can cause line-lengths to exceed the spec (due to
    * & -> &amp; expansion etc) but our iCal parser is more robust than
    * our XML parser, so this works. */
    QByteArray xmlSanitiseIcsData(const QByteArray &data) {
        QList<QByteArray> lines = data.split('\n');
        int depth = 0;
        bool inCData = false;
        QByteArray retn;
        retn.reserve(data.size());
        for (QList<QByteArray>::const_iterator it = lines.constBegin(); it != lines.constEnd(); it++) {
            QByteArray line = *it;
            if (line.contains("BEGIN:VCALENDAR")) {
                depth += 1;
                inCData = line.contains("<![CDATA[");
            } else if (line.contains("END:VCALENDAR")) {
                depth -= 1;
                inCData = false;
            } else if (depth > 0 && !inCData) {
                // We're inside a VCALENDAR/ics block.
                // First, hack to turn sanitised input into malformed input:
                line.replace("&amp;",  "&");
                line.replace("&quot;", "\"");
                line.replace("&apos;", "'");
                line.replace("&lt;",   "<");
                line.replace("&gt;",   ">");
                // Then, fix for malformed input:
                QString lineStr(line);
                // RegExp should avoid escaping & when this character is starting
                // a valid numeric character reference (decimal or hexadecimal).
                // Other HTLML entities like &nbsp; seems to make iCal parser
                // fails, so we're encoding them.
                lineStr.replace(QRegExp("&(?!#[0-9]+;|#x[0-9A-Fa-f]+;)"), "&amp;");
                line = lineStr.toUtf8();
                line.replace('"',  "&quot;");
                line.replace('\'', "&apos;");
                line.replace('<',  "&lt;");
                line.replace('>',  "&gt;");
            }
            retn.append(line);
            retn.append('\n');
        }
        return retn;
    }
}

Reader::Reader(QObject *parent)
    : QObject(parent)
{
}

Reader::~Reader()
{
    delete mReader;
}

void Reader::read(const QByteArray &data)
{
    delete mReader;
    mReader = new QXmlStreamReader(xmlSanitiseIcsData(data));
    while (mReader->readNextStartElement()) {
        if (mReader->name() == "multistatus") {
            mValidResponse = true;
            readMultiStatus();
        }
    }
}

bool Reader::hasError() const
{
    if (!mReader)
        return false;

    return !mValidResponse;
}

const QList<Buteo::Dav::Resource>& Reader::results() const
{
    return mResults;
}

void Reader::readMultiStatus()
{
    while (mReader->readNextStartElement()) {
        if (mReader->name() == "response") {
            readResponse();
        } else {
            mReader->skipCurrentElement();
        }
    }
}

void Reader::readResponse()
{
    Buteo::Dav::Resource resource;
    while (mReader->readNextStartElement()) {
        if (mReader->name() == "href") {
            resource.href = QUrl::fromPercentEncoding(mReader->readElementText().toUtf8());
        } else if (mReader->name() == "propstat") {
            readPropStat(&resource);
        } else if (mReader->name() == "status") {
            resource.status = mReader->readElementText();
        } else {
            mReader->skipCurrentElement();
        }
    }
    if (resource.href.isEmpty()) {
        qCWarning(lcDav) << "Ignoring received calendar object data, is missing href value";
        return;
    }

    mResults.append(resource);
}

void Reader::readPropStat(Buteo::Dav::Resource *resource)
{
    while (mReader->readNextStartElement()) {
        if (mReader->name() == "prop") {
            readProp(resource);
        } else if (mReader->name() == "status") {
            resource->status = mReader->readElementText();
        } else {
            mReader->skipCurrentElement();
        }
    }
}

void Reader::readProp(Buteo::Dav::Resource *resource)
{
    while (mReader->readNextStartElement()) {
        if (mReader->name() == "getetag") {
            resource->etag = mReader->readElementText();
        } else if (mReader->name() == "calendar-data") {
            resource->data = mReader->readElementText(QXmlStreamReader::IncludeChildElements).trimmed();
        } else {
            mReader->skipCurrentElement();
        }
    }
}

QList<Buteo::Dav::Resource> Buteo::Dav::Resource::fromData(const QByteArray &data, bool *isOk)
{
    Reader reader;
    reader.read(data);
    if (isOk)
        *isOk = !reader.hasError();
    return reader.results();
}