Libkdav2

davmanager.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 "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 
32 using namespace KDAV2;
33 
34 DavManager *DavManager::mSelf = nullptr;
35 
36 DavManager::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 
61 void 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 
66 DavJob *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 
73 DavJob *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 
80 DavJob *DavManager::createDeleteJob(const QUrl &url)
81 {
82  setConnectionSettings(url);
83  auto reply = mWebDav->remove(url.path());
84  return new DavJob{reply, url};
85 }
86 
87 
88 DavJob *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 
98 DavJob *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 
105 DavJob *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 
112 DavJob *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 
119 DavJob *DavManager::createMkColJob(const QUrl &url)
120 {
121  setConnectionSettings(url);
122  auto reply = mWebDav->mkdir(url.path());
123  return new DavJob{reply, url};
124 }
125 
126 DavJob *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 
133 DavJob *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 
140 const DavProtocolBase *DavManager::davProtocol(Protocol protocol)
141 {
142  if (createProtocol(protocol)) {
143  return mProtocols[ protocol ];
144  } else {
145  return nullptr;
146  }
147 }
148 
149 bool 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 
174 {
175  return DavManager::self()->mWebDav;
176 }
177 
179 {
180  mIgnoreSslErrors = ignore;
181 }
182 
DavJob * createPropFindJob(const QUrl &url, const QDomDocument &document, const QString &depth=QStringLiteral("1"))
Returns a preconfigured DAV PROPFIND job.
Definition: davmanager.cpp:66
QString userName(QUrl::ComponentFormattingOptions options) const const
static QNetworkAccessManager * networkAccessManager()
Provides access to the internally used network access manager.
Definition: davmanager.cpp:173
static DavManager * self()
Returns the global instance of the DAV manager.
Definition: davmanager.cpp:52
DavJob * createDeleteJob(const QUrl &url)
Returns a preconfigured DAV DELETE job.
Definition: davmanager.cpp:80
QString host(QUrl::ComponentFormattingOptions options) const const
DavJob * createMkColJob(const QUrl &url)
Returns a preconfigured DAV MKCOL job.
Definition: davmanager.cpp:119
int port(int defaultPort) const const
DavJob * createCreateJob(const QByteArray &data, const QUrl &url, const QByteArray &contentType)
Returns a preconfigured DAV PUT job with a If-None-Match header.
Definition: davmanager.cpp:105
void setIgnoreSslErrors(bool)
Ignore all ssl errors.
Definition: davmanager.cpp:178
QMapIterator::Item next()
const DavProtocolBase * davProtocol(Protocol protocol)
Returns the DAV protocol dialect object for the given DAV protocol.
Definition: davmanager.cpp:140
int toInt(bool *ok, int base) const const
DavJob * createReportJob(const QUrl &url, const QDomDocument &document, const QString &depth=QStringLiteral("1"))
Returns a preconfigured DAV REPORT job.
Definition: davmanager.cpp:73
QString path(QUrl::ComponentFormattingOptions options) const const
const T & value() const const
QString scheme() const const
QString password(QUrl::ComponentFormattingOptions options) const const
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.
Definition: davmanager.cpp:112
~DavManager()
Destroys the DAV manager.
Definition: davmanager.cpp:42
DavJob * createGetJob(const QUrl &url)
Returns a preconfigured DAV GET job.
Definition: davmanager.cpp:88
DavJob * createMkCalendarJob(const QUrl &url, const QDomDocument &document)
Returns a preconfigured DAV MKCALENDAR job.
Definition: davmanager.cpp:133
DavJob * createPropPatchJob(const QUrl &url, const QDomDocument &document)
Returns a preconfigured DAV PROPPATCH job.
Definition: davmanager.cpp:98
A factory class for handling DAV jobs.
Definition: davmanager.h:49
Base class for various DAV groupware dialects.
bool hasNext() const const
QByteArray toByteArray(int indent) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 27 2022 23:10:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.