1 | /* |
2 | SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #include "carddavprotocol_p.h" |
8 | |
9 | #include <QDomDocument> |
10 | #include <QStringList> |
11 | #include <QUrl> |
12 | |
13 | using namespace KDAV; |
14 | |
15 | class CarddavCollectionQueryBuilder : public XMLQueryBuilder |
16 | { |
17 | public: |
18 | QDomDocument buildQuery() const override |
19 | { |
20 | QDomDocument document; |
21 | |
22 | QDomElement propfindElement = document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("propfind" )); |
23 | document.appendChild(newChild: propfindElement); |
24 | |
25 | QDomElement propElement = document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("prop" )); |
26 | propfindElement.appendChild(newChild: propElement); |
27 | |
28 | propElement.appendChild(newChild: document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("displayname" ))); |
29 | propElement.appendChild(newChild: document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("resourcetype" ))); |
30 | propElement.appendChild(newChild: document.createElementNS(QStringLiteral("http://calendarserver.org/ns/" ), QStringLiteral("getctag" ))); |
31 | |
32 | return document; |
33 | } |
34 | |
35 | QString mimeType() const override |
36 | { |
37 | return QString(); |
38 | } |
39 | }; |
40 | |
41 | class CarddavListItemsQueryBuilder : public XMLQueryBuilder |
42 | { |
43 | public: |
44 | QDomDocument buildQuery() const override |
45 | { |
46 | QDomDocument document; |
47 | |
48 | QDomElement propfindElement = document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("propfind" )); |
49 | document.appendChild(newChild: propfindElement); |
50 | |
51 | QDomElement propElement = document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("prop" )); |
52 | propfindElement.appendChild(newChild: propElement); |
53 | |
54 | propElement.appendChild(newChild: document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("displayname" ))); |
55 | propElement.appendChild(newChild: document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("resourcetype" ))); |
56 | propElement.appendChild(newChild: document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("getetag" ))); |
57 | |
58 | return document; |
59 | } |
60 | |
61 | QString mimeType() const override |
62 | { |
63 | return QStringLiteral("text/directory" ); |
64 | } |
65 | }; |
66 | |
67 | class CarddavMultigetQueryBuilder : public XMLQueryBuilder |
68 | { |
69 | public: |
70 | QDomDocument buildQuery() const override |
71 | { |
72 | QDomDocument document; |
73 | const QStringList urls = parameter(QStringLiteral("urls" )).toStringList(); |
74 | if (urls.isEmpty()) { |
75 | return document; |
76 | } |
77 | |
78 | QDomElement multigetElement = document.createElementNS(QStringLiteral("urn:ietf:params:xml:ns:carddav" ), QStringLiteral("addressbook-multiget" )); |
79 | document.appendChild(newChild: multigetElement); |
80 | |
81 | QDomElement propElement = document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("prop" )); |
82 | multigetElement.appendChild(newChild: propElement); |
83 | |
84 | propElement.appendChild(newChild: document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("getetag" ))); |
85 | QDomElement addressDataElement = document.createElementNS(QStringLiteral("urn:ietf:params:xml:ns:carddav" ), QStringLiteral("address-data" )); |
86 | addressDataElement.appendChild(newChild: document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("allprop" ))); |
87 | propElement.appendChild(newChild: addressDataElement); |
88 | |
89 | for (const QString &url : urls) { |
90 | QDomElement hrefElement = document.createElementNS(QStringLiteral("DAV:" ), QStringLiteral("href" )); |
91 | const QUrl pathUrl = QUrl::fromUserInput(userInput: url); |
92 | const QDomText textNode = document.createTextNode(data: pathUrl.toString()); |
93 | hrefElement.appendChild(newChild: textNode); |
94 | |
95 | multigetElement.appendChild(newChild: hrefElement); |
96 | } |
97 | |
98 | return document; |
99 | } |
100 | |
101 | QString mimeType() const override |
102 | { |
103 | return QString(); |
104 | } |
105 | }; |
106 | |
107 | CarddavProtocol::CarddavProtocol() |
108 | { |
109 | } |
110 | |
111 | bool CarddavProtocol::supportsPrincipals() const |
112 | { |
113 | return true; |
114 | } |
115 | |
116 | bool CarddavProtocol::useReport() const |
117 | { |
118 | return false; |
119 | } |
120 | |
121 | bool CarddavProtocol::useMultiget() const |
122 | { |
123 | return true; |
124 | } |
125 | |
126 | QString CarddavProtocol::principalHomeSet() const |
127 | { |
128 | return QStringLiteral("addressbook-home-set" ); |
129 | } |
130 | |
131 | QString CarddavProtocol::principalHomeSetNS() const |
132 | { |
133 | return QStringLiteral("urn:ietf:params:xml:ns:carddav" ); |
134 | } |
135 | |
136 | XMLQueryBuilder::Ptr CarddavProtocol::collectionsQuery() const |
137 | { |
138 | return XMLQueryBuilder::Ptr(new CarddavCollectionQueryBuilder()); |
139 | } |
140 | |
141 | bool CarddavProtocol::containsCollection(const QDomElement &propElem) const |
142 | { |
143 | return !propElem.elementsByTagNameNS(QStringLiteral("urn:ietf:params:xml:ns:carddav" ), QStringLiteral("addressbook" )).isEmpty(); |
144 | } |
145 | |
146 | QList<XMLQueryBuilder::Ptr> CarddavProtocol::itemsQueries() const |
147 | { |
148 | QList<XMLQueryBuilder::Ptr> ret; |
149 | ret << XMLQueryBuilder::Ptr(new CarddavListItemsQueryBuilder()); |
150 | return ret; |
151 | } |
152 | |
153 | XMLQueryBuilder::Ptr CarddavProtocol::itemsReportQuery(const QStringList &urls) const |
154 | { |
155 | XMLQueryBuilder::Ptr ret(new CarddavMultigetQueryBuilder()); |
156 | ret->setParameter(QStringLiteral("urls" ), value: urls); |
157 | return ret; |
158 | } |
159 | |
160 | QString CarddavProtocol::responseNamespace() const |
161 | { |
162 | return QStringLiteral("urn:ietf:params:xml:ns:carddav" ); |
163 | } |
164 | |
165 | QString CarddavProtocol::dataTagName() const |
166 | { |
167 | return QStringLiteral("address-data" ); |
168 | } |
169 | |
170 | DavCollection::ContentTypes CarddavProtocol::collectionContentTypes(const QDomElement &) const |
171 | { |
172 | return DavCollection::Contacts; |
173 | } |
174 | |