Libkdav2

carddavprotocol.cpp
1 /*
2  Copyright (c) 2010 Tobias Koenig <[email protected]>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 #include "carddavprotocol.h"
20 
21 #include <QtCore/QStringList>
22 #include <QtCore/QUrl>
23 #include <QtCore/QVariant>
24 #include <QtXml/QDomDocument>
25 
26 using namespace KDAV2;
27 
28 class CarddavCollectionQueryBuilder : public XMLQueryBuilder
29 {
30 public:
31  QDomDocument buildQuery() const Q_DECL_OVERRIDE
32  {
33  QDomDocument document;
34 
35  QDomElement propfindElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("propfind"));
36  document.appendChild(propfindElement);
37 
38  QDomElement propElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
39  propfindElement.appendChild(propElement);
40 
41  propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("displayname")));
42  propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("resourcetype")));
43  propElement.appendChild(document.createElementNS(QStringLiteral("http://calendarserver.org/ns/"), QStringLiteral("getctag")));
44 
45  return document;
46  }
47 
48  QString mimeType() const Q_DECL_OVERRIDE
49  {
50  return QString();
51  }
52 };
53 
54 class CarddavListItemsQueryBuilder : public XMLQueryBuilder
55 {
56 public:
57  QDomDocument buildQuery() const Q_DECL_OVERRIDE
58  {
59  QDomDocument document;
60 
61  QDomElement propfindElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("propfind"));
62  document.appendChild(propfindElement);
63 
64  QDomElement propElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
65  propfindElement.appendChild(propElement);
66 
67  propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("displayname")));
68  propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("resourcetype")));
69  propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("getetag")));
70 
71  return document;
72  }
73 
74  QString mimeType() const Q_DECL_OVERRIDE
75  {
76  return QStringLiteral("text/directory");
77  }
78 };
79 
80 class CarddavMultigetQueryBuilder : public XMLQueryBuilder
81 {
82 public:
83  QDomDocument buildQuery() const Q_DECL_OVERRIDE
84  {
85  QDomDocument document;
86  const QStringList urls = parameter(QStringLiteral("urls")).toStringList();
87  if (urls.isEmpty()) {
88  return document;
89  }
90 
91  QDomElement multigetElement = document.createElementNS(QStringLiteral("urn:ietf:params:xml:ns:carddav"), QStringLiteral("addressbook-multiget"));
92  document.appendChild(multigetElement);
93 
94  QDomElement propElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
95  multigetElement.appendChild(propElement);
96 
97  propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("getetag")));
98  QDomElement addressDataElement = document.createElementNS(QStringLiteral("urn:ietf:params:xml:ns:carddav"), QStringLiteral("address-data"));
99  addressDataElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("allprop")));
100  propElement.appendChild(addressDataElement);
101 
102  for (const QString &url : urls) {
103  QDomElement hrefElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("href"));
104  const QUrl pathUrl = QUrl::fromUserInput(url);
105  const QDomText textNode = document.createTextNode(pathUrl.toString());
106  hrefElement.appendChild(textNode);
107 
108  multigetElement.appendChild(hrefElement);
109  }
110 
111  return document;
112  }
113 
114  QString mimeType() const Q_DECL_OVERRIDE
115  {
116  return QString();
117  }
118 };
119 
120 CarddavProtocol::CarddavProtocol()
121 {
122 }
123 
124 bool CarddavProtocol::supportsPrincipals() const
125 {
126  return true;
127 }
128 
129 bool CarddavProtocol::supportsCTags() const
130 {
131  return true;
132 }
133 
134 bool CarddavProtocol::useReport() const
135 {
136  return false;
137 }
138 
139 bool CarddavProtocol::useMultiget() const
140 {
141  return true;
142 }
143 
144 QString CarddavProtocol::principalHomeSet() const
145 {
146  return QStringLiteral("addressbook-home-set");
147 }
148 
149 QString CarddavProtocol::principalHomeSetNS() const
150 {
151  return QStringLiteral("urn:ietf:params:xml:ns:carddav");
152 }
153 
154 XMLQueryBuilder::Ptr CarddavProtocol::collectionsQuery() const
155 {
156  return XMLQueryBuilder::Ptr(new CarddavCollectionQueryBuilder());
157 }
158 
159 QString CarddavProtocol::collectionsXQuery() const
160 {
161  const QString query(QStringLiteral("//*[local-name()='addressbook' and namespace-uri()='urn:ietf:params:xml:ns:carddav']/ancestor::*[local-name()='response' and namespace-uri()='DAV:']"));
162 
163  return query;
164 }
165 
166 QVector<XMLQueryBuilder::Ptr> CarddavProtocol::itemsQueries() const
167 {
169  ret << XMLQueryBuilder::Ptr(new CarddavListItemsQueryBuilder());
170  return ret;
171 }
172 
173 XMLQueryBuilder::Ptr CarddavProtocol::itemsReportQuery(const QStringList &urls) const
174 {
175  XMLQueryBuilder::Ptr ret(new CarddavMultigetQueryBuilder());
176  ret->setParameter(QStringLiteral("urls"), urls);
177  return ret;
178 }
179 
180 QString CarddavProtocol::responseNamespace() const
181 {
182  return QStringLiteral("urn:ietf:params:xml:ns:carddav");
183 }
184 
185 QString CarddavProtocol::dataTagName() const
186 {
187  return QStringLiteral("address-data");
188 }
189 
190 DavCollection::ContentTypes CarddavProtocol::collectionContentTypes(const QDomElement &) const
191 {
193 }
QDomElement createElementNS(const QString &nsURI, const QString &qName)
QDomText createTextNode(const QString &value)
KCALUTILS_EXPORT QString mimeType()
@ Contacts
The collection can contain contact DAV resources.
Definition: davcollection.h:63
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
QString toString(QUrl::FormattingOptions options) const const
bool isEmpty() const const
QDomNode appendChild(const QDomNode &newChild)
QUrl fromUserInput(const QString &userInput)
Base class for XML query builders.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 04:07:29 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.