File indexing completed on 2024-11-17 04:44:02

0001 /*
0002     Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify
0005     it under the terms of the GNU General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or
0007     (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include "carddavprotocol.h"
0020 
0021 #include <QtCore/QStringList>
0022 #include <QtCore/QUrl>
0023 #include <QtCore/QVariant>
0024 #include <QtXml/QDomDocument>
0025 
0026 using namespace KDAV2;
0027 
0028 class CarddavCollectionQueryBuilder : public XMLQueryBuilder
0029 {
0030 public:
0031     QDomDocument buildQuery() const Q_DECL_OVERRIDE
0032     {
0033         QDomDocument document;
0034 
0035         QDomElement propfindElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("propfind"));
0036         document.appendChild(propfindElement);
0037 
0038         QDomElement propElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
0039         propfindElement.appendChild(propElement);
0040 
0041         propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("displayname")));
0042         propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("resourcetype")));
0043         propElement.appendChild(document.createElementNS(QStringLiteral("http://calendarserver.org/ns/"), QStringLiteral("getctag")));
0044 
0045         return document;
0046     }
0047 
0048     QString mimeType() const Q_DECL_OVERRIDE
0049     {
0050         return QString();
0051     }
0052 };
0053 
0054 class CarddavListItemsQueryBuilder : public XMLQueryBuilder
0055 {
0056 public:
0057     QDomDocument buildQuery() const Q_DECL_OVERRIDE
0058     {
0059         QDomDocument document;
0060 
0061         QDomElement propfindElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("propfind"));
0062         document.appendChild(propfindElement);
0063 
0064         QDomElement propElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
0065         propfindElement.appendChild(propElement);
0066 
0067         propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("displayname")));
0068         propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("resourcetype")));
0069         propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("getetag")));
0070 
0071         return document;
0072     }
0073 
0074     QString mimeType() const Q_DECL_OVERRIDE
0075     {
0076         return QStringLiteral("text/directory");
0077     }
0078 };
0079 
0080 class CarddavMultigetQueryBuilder : public XMLQueryBuilder
0081 {
0082 public:
0083     QDomDocument buildQuery() const Q_DECL_OVERRIDE
0084     {
0085         QDomDocument document;
0086         const QStringList urls = parameter(QStringLiteral("urls")).toStringList();
0087         if (urls.isEmpty()) {
0088             return document;
0089         }
0090 
0091         QDomElement multigetElement = document.createElementNS(QStringLiteral("urn:ietf:params:xml:ns:carddav"), QStringLiteral("addressbook-multiget"));
0092         document.appendChild(multigetElement);
0093 
0094         QDomElement propElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
0095         multigetElement.appendChild(propElement);
0096 
0097         propElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("getetag")));
0098         QDomElement addressDataElement = document.createElementNS(QStringLiteral("urn:ietf:params:xml:ns:carddav"), QStringLiteral("address-data"));
0099         addressDataElement.appendChild(document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("allprop")));
0100         propElement.appendChild(addressDataElement);
0101 
0102         for (const QString &url : urls) {
0103             QDomElement hrefElement = document.createElementNS(QStringLiteral("DAV:"), QStringLiteral("href"));
0104             const QUrl pathUrl = QUrl::fromUserInput(url);
0105             const QDomText textNode = document.createTextNode(pathUrl.toString());
0106             hrefElement.appendChild(textNode);
0107 
0108             multigetElement.appendChild(hrefElement);
0109         }
0110 
0111         return document;
0112     }
0113 
0114     QString mimeType() const Q_DECL_OVERRIDE
0115     {
0116         return QString();
0117     }
0118 };
0119 
0120 CarddavProtocol::CarddavProtocol()
0121 {
0122 }
0123 
0124 bool CarddavProtocol::supportsPrincipals() const
0125 {
0126     return true;
0127 }
0128 
0129 bool CarddavProtocol::supportsCTags() const
0130 {
0131     return true;
0132 }
0133 
0134 bool CarddavProtocol::useReport() const
0135 {
0136     return false;
0137 }
0138 
0139 bool CarddavProtocol::useMultiget() const
0140 {
0141     return true;
0142 }
0143 
0144 QString CarddavProtocol::principalHomeSet() const
0145 {
0146     return QStringLiteral("addressbook-home-set");
0147 }
0148 
0149 QString CarddavProtocol::principalHomeSetNS() const
0150 {
0151     return QStringLiteral("urn:ietf:params:xml:ns:carddav");
0152 }
0153 
0154 XMLQueryBuilder::Ptr CarddavProtocol::collectionsQuery() const
0155 {
0156     return XMLQueryBuilder::Ptr(new CarddavCollectionQueryBuilder());
0157 }
0158 
0159 QString CarddavProtocol::collectionsXQuery() const
0160 {
0161     const QString query(QStringLiteral("//*[local-name()='addressbook' and namespace-uri()='urn:ietf:params:xml:ns:carddav']/ancestor::*[local-name()='response' and namespace-uri()='DAV:']"));
0162 
0163     return query;
0164 }
0165 
0166 QVector<XMLQueryBuilder::Ptr> CarddavProtocol::itemsQueries() const
0167 {
0168     QVector<XMLQueryBuilder::Ptr> ret;
0169     ret << XMLQueryBuilder::Ptr(new CarddavListItemsQueryBuilder());
0170     return ret;
0171 }
0172 
0173 XMLQueryBuilder::Ptr CarddavProtocol::itemsReportQuery(const QStringList &urls) const
0174 {
0175     XMLQueryBuilder::Ptr ret(new CarddavMultigetQueryBuilder());
0176     ret->setParameter(QStringLiteral("urls"), urls);
0177     return ret;
0178 }
0179 
0180 QString CarddavProtocol::responseNamespace() const
0181 {
0182     return QStringLiteral("urn:ietf:params:xml:ns:carddav");
0183 }
0184 
0185 QString CarddavProtocol::dataTagName() const
0186 {
0187     return QStringLiteral("address-data");
0188 }
0189 
0190 DavCollection::ContentTypes CarddavProtocol::collectionContentTypes(const QDomElement &) const
0191 {
0192     return DavCollection::Contacts;
0193 }