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 "davmanager.h" 0020 0021 #include "protocols/caldavprotocol.h" 0022 #include "protocols/carddavprotocol.h" 0023 #include "protocols/groupdavprotocol.h" 0024 #include "davjob.h" 0025 #include "qwebdavlib/qwebdav.h" 0026 0027 #include "libkdav2_debug.h" 0028 0029 #include <QtCore/QUrl> 0030 #include <QtXml/QDomDocument> 0031 0032 using namespace KDAV2; 0033 0034 DavManager *DavManager::mSelf = nullptr; 0035 0036 DavManager::DavManager() 0037 : mWebDav{new QWebdav}, 0038 mIgnoreSslErrors{true} 0039 { 0040 } 0041 0042 DavManager::~DavManager() 0043 { 0044 QMapIterator<Protocol, DavProtocolBase *> it(mProtocols); 0045 while (it.hasNext()) { 0046 it.next(); 0047 delete it.value(); 0048 } 0049 delete mWebDav; 0050 } 0051 0052 DavManager *DavManager::self() 0053 { 0054 if (!mSelf) { 0055 mSelf = new DavManager(); 0056 } 0057 0058 return mSelf; 0059 } 0060 0061 void DavManager::setConnectionSettings(const QUrl &url) 0062 { 0063 mWebDav->setConnectionSettings(url.scheme() == "https" ? QWebdav::HTTPS : QWebdav::HTTP, url.host(), "/", url.userName(), url.password(), url.port(0), mIgnoreSslErrors); 0064 } 0065 0066 DavJob *DavManager::createPropFindJob(const QUrl &url, const QDomDocument &document, const QString &depth) 0067 { 0068 setConnectionSettings(url); 0069 auto reply = mWebDav->propfind(url.path(), document.toByteArray(), depth.toInt()); 0070 return new DavJob{reply, url}; 0071 } 0072 0073 DavJob *DavManager::createReportJob(const QUrl &url, const QDomDocument &document, const QString &depth) 0074 { 0075 setConnectionSettings(url); 0076 auto reply = mWebDav->report(url.path(), document.toByteArray(), depth.toInt()); 0077 return new DavJob{reply, url}; 0078 } 0079 0080 DavJob *DavManager::createDeleteJob(const QUrl &url) 0081 { 0082 setConnectionSettings(url); 0083 auto reply = mWebDav->remove(url.path()); 0084 return new DavJob{reply, url}; 0085 } 0086 0087 0088 DavJob *DavManager::createGetJob(const QUrl &url) 0089 { 0090 setConnectionSettings(url); 0091 // Work around a strange bug in Zimbra (seen at least on CE 5.0.18) : if the user-agent 0092 // contains "Mozilla", some strange debug data is displayed in the shared calendars. 0093 // This kinda mess up the events parsing... 0094 auto reply = mWebDav->get(url.path(), {{"User-Agent", "KDAV2"}}); 0095 return new DavJob{reply, url}; 0096 } 0097 0098 DavJob *DavManager::createPropPatchJob(const QUrl &url, const QDomDocument &document) 0099 { 0100 setConnectionSettings(url); 0101 auto reply = mWebDav->proppatch(url.path(), document.toByteArray()); 0102 return new DavJob{reply, url}; 0103 } 0104 0105 DavJob *DavManager::createCreateJob(const QByteArray &data, const QUrl &url, const QByteArray &contentType) 0106 { 0107 setConnectionSettings(url); 0108 auto reply = mWebDav->put(url.path(), data, {{"Content-Type", contentType}, {"If-None-Match", "*"}}); 0109 return new DavJob{reply, url}; 0110 } 0111 0112 DavJob *DavManager::createModifyJob(const QByteArray &data, const QUrl &url, const QByteArray &contentType, const QByteArray &etag) 0113 { 0114 setConnectionSettings(url); 0115 auto reply = mWebDav->put(url.path(), data, {{"Content-Type", contentType}, {"If-Match", etag}}); 0116 return new DavJob{reply, url}; 0117 } 0118 0119 DavJob *DavManager::createMkColJob(const QUrl &url) 0120 { 0121 setConnectionSettings(url); 0122 auto reply = mWebDav->mkdir(url.path()); 0123 return new DavJob{reply, url}; 0124 } 0125 0126 DavJob *DavManager::createMkColJob(const QUrl &url, const QDomDocument &document) 0127 { 0128 setConnectionSettings(url); 0129 auto reply = mWebDav->mkdir(url.path(), document.toByteArray()); 0130 return new DavJob{reply, url}; 0131 } 0132 0133 DavJob *DavManager::createMkCalendarJob(const QUrl &url, const QDomDocument &document) 0134 { 0135 setConnectionSettings(url); 0136 auto reply = mWebDav->mkcalendar(url.path(), document.toByteArray()); 0137 return new DavJob{reply, url}; 0138 } 0139 0140 const DavProtocolBase *DavManager::davProtocol(Protocol protocol) 0141 { 0142 if (createProtocol(protocol)) { 0143 return mProtocols[ protocol ]; 0144 } else { 0145 return nullptr; 0146 } 0147 } 0148 0149 bool DavManager::createProtocol(Protocol protocol) 0150 { 0151 if (mProtocols.contains(protocol)) { 0152 return true; 0153 } 0154 0155 switch (protocol) { 0156 case KDAV2::CalDav: 0157 mProtocols.insert(KDAV2::CalDav, new CaldavProtocol()); 0158 break; 0159 case KDAV2::CardDav: 0160 mProtocols.insert(KDAV2::CardDav, new CarddavProtocol()); 0161 break; 0162 case KDAV2::GroupDav: 0163 mProtocols.insert(KDAV2::GroupDav, new GroupdavProtocol()); 0164 break; 0165 default: 0166 qCCritical(KDAV2_LOG) << "Unknown protocol: " << static_cast<int>(protocol); 0167 return false; 0168 } 0169 0170 return true; 0171 } 0172 0173 QNetworkAccessManager *DavManager::networkAccessManager() 0174 { 0175 return DavManager::self()->mWebDav; 0176 } 0177 0178 void DavManager::setIgnoreSslErrors(bool ignore) 0179 { 0180 mIgnoreSslErrors = ignore; 0181 } 0182