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