Libkdav2

davmanager.cpp
1/*
2 Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
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 "davmanager.h"
20
21#include "protocols/caldavprotocol.h"
22#include "protocols/carddavprotocol.h"
23#include "protocols/groupdavprotocol.h"
24#include "davjob.h"
25#include "qwebdavlib/qwebdav.h"
26
27#include "libkdav2_debug.h"
28
29#include <QtCore/QUrl>
30#include <QtXml/QDomDocument>
31
32using namespace KDAV2;
33
34DavManager *DavManager::mSelf = nullptr;
35
36DavManager::DavManager()
37 : mWebDav{new QWebdav},
38 mIgnoreSslErrors{true}
39{
40}
41
43{
45 while (it.hasNext()) {
46 it.next();
47 delete it.value();
48 }
49 delete mWebDav;
50}
51
53{
54 if (!mSelf) {
55 mSelf = new DavManager();
56 }
57
58 return mSelf;
59}
60
61void DavManager::setConnectionSettings(const QUrl &url)
62{
63 mWebDav->setConnectionSettings(url.scheme() == "https" ? QWebdav::HTTPS : QWebdav::HTTP, url.host(), "/", url.userName(), url.password(), url.port(0), mIgnoreSslErrors);
64}
65
66DavJob *DavManager::createPropFindJob(const QUrl &url, const QDomDocument &document, const QString &depth)
67{
68 setConnectionSettings(url);
69 auto reply = mWebDav->propfind(url.path(), document.toByteArray(), depth.toInt());
70 return new DavJob{reply, url};
71}
72
73DavJob *DavManager::createReportJob(const QUrl &url, const QDomDocument &document, const QString &depth)
74{
75 setConnectionSettings(url);
76 auto reply = mWebDav->report(url.path(), document.toByteArray(), depth.toInt());
77 return new DavJob{reply, url};
78}
79
81{
82 setConnectionSettings(url);
83 auto reply = mWebDav->remove(url.path());
84 return new DavJob{reply, url};
85}
86
87
88DavJob *DavManager::createGetJob(const QUrl &url)
89{
90 setConnectionSettings(url);
91 // Work around a strange bug in Zimbra (seen at least on CE 5.0.18) : if the user-agent
92 // contains "Mozilla", some strange debug data is displayed in the shared calendars.
93 // This kinda mess up the events parsing...
94 auto reply = mWebDav->get(url.path(), {{"User-Agent", "KDAV2"}});
95 return new DavJob{reply, url};
96}
97
98DavJob *DavManager::createPropPatchJob(const QUrl &url, const QDomDocument &document)
99{
100 setConnectionSettings(url);
101 auto reply = mWebDav->proppatch(url.path(), document.toByteArray());
102 return new DavJob{reply, url};
103}
104
105DavJob *DavManager::createCreateJob(const QByteArray &data, const QUrl &url, const QByteArray &contentType)
106{
107 setConnectionSettings(url);
108 auto reply = mWebDav->put(url.path(), data, {{"Content-Type", contentType}, {"If-None-Match", "*"}});
109 return new DavJob{reply, url};
110}
111
112DavJob *DavManager::createModifyJob(const QByteArray &data, const QUrl &url, const QByteArray &contentType, const QByteArray &etag)
113{
114 setConnectionSettings(url);
115 auto reply = mWebDav->put(url.path(), data, {{"Content-Type", contentType}, {"If-Match", etag}});
116 return new DavJob{reply, url};
117}
118
120{
121 setConnectionSettings(url);
122 auto reply = mWebDav->mkdir(url.path());
123 return new DavJob{reply, url};
124}
125
126DavJob *DavManager::createMkColJob(const QUrl &url, const QDomDocument &document)
127{
128 setConnectionSettings(url);
129 auto reply = mWebDav->mkdir(url.path(), document.toByteArray());
130 return new DavJob{reply, url};
131}
132
133DavJob *DavManager::createMkCalendarJob(const QUrl &url, const QDomDocument &document)
134{
135 setConnectionSettings(url);
136 auto reply = mWebDav->mkcalendar(url.path(), document.toByteArray());
137 return new DavJob{reply, url};
138}
139
141{
142 if (createProtocol(protocol)) {
143 return mProtocols[ protocol ];
144 } else {
145 return nullptr;
146 }
147}
148
149bool DavManager::createProtocol(Protocol protocol)
150{
151 if (mProtocols.contains(protocol)) {
152 return true;
153 }
154
155 switch (protocol) {
156 case KDAV2::CalDav:
157 mProtocols.insert(KDAV2::CalDav, new CaldavProtocol());
158 break;
159 case KDAV2::CardDav:
160 mProtocols.insert(KDAV2::CardDav, new CarddavProtocol());
161 break;
162 case KDAV2::GroupDav:
163 mProtocols.insert(KDAV2::GroupDav, new GroupdavProtocol());
164 break;
165 default:
166 qCCritical(KDAV2_LOG) << "Unknown protocol: " << static_cast<int>(protocol);
167 return false;
168 }
169
170 return true;
171}
172
177
179{
180 mIgnoreSslErrors = ignore;
181}
182
A factory class for handling DAV jobs.
Definition davmanager.h:50
DavJob * createPropFindJob(const QUrl &url, const QDomDocument &document, const QString &depth=QStringLiteral("1"))
Returns a preconfigured DAV PROPFIND job.
DavJob * createModifyJob(const QByteArray &data, const QUrl &url, const QByteArray &contentType, const QByteArray &etag)
Returns a preconfigured DAV PUT job with a If-Match header, that matches the.
void setIgnoreSslErrors(bool)
Ignore all ssl errors.
DavJob * createMkColJob(const QUrl &url)
Returns a preconfigured DAV MKCOL job.
DavJob * createGetJob(const QUrl &url)
Returns a preconfigured DAV GET job.
~DavManager()
Destroys the DAV manager.
static DavManager * self()
Returns the global instance of the DAV manager.
DavJob * createDeleteJob(const QUrl &url)
Returns a preconfigured DAV DELETE job.
DavJob * createReportJob(const QUrl &url, const QDomDocument &document, const QString &depth=QStringLiteral("1"))
Returns a preconfigured DAV REPORT job.
const DavProtocolBase * davProtocol(Protocol protocol)
Returns the DAV protocol dialect object for the given DAV protocol.
DavJob * createCreateJob(const QByteArray &data, const QUrl &url, const QByteArray &contentType)
Returns a preconfigured DAV PUT job with a If-None-Match header.
DavJob * createPropPatchJob(const QUrl &url, const QDomDocument &document)
Returns a preconfigured DAV PROPPATCH job.
DavJob * createMkCalendarJob(const QUrl &url, const QDomDocument &document)
Returns a preconfigured DAV MKCALENDAR job.
static QNetworkAccessManager * networkAccessManager()
Provides access to the internally used network access manager.
Base class for various DAV groupware dialects.
QByteArray toByteArray(int indent) const const
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
bool hasNext() const const
const T & value() const const
int toInt(bool *ok, int base) const const
QString host(ComponentFormattingOptions options) const const
QString password(ComponentFormattingOptions options) const const
QString path(ComponentFormattingOptions options) const const
int port(int defaultPort) const const
QString scheme() const const
QString userName(ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:50:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.