File indexing completed on 2024-06-16 04:52:27

0001 /*
0002     Copyright (c) 2010 Grégory Oestreicher <greg@kamago.net>
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 "davcollectionmodifyjob.h"
0020 #include "davmanager.h"
0021 
0022 #include "daverror.h"
0023 #include "utils.h"
0024 #include "davjob.h"
0025 
0026 using namespace KDAV2;
0027 
0028 DavCollectionModifyJob::DavCollectionModifyJob(const DavUrl &url, QObject *parent)
0029     : DavJobBase(parent), mUrl(url)
0030 {
0031 }
0032 
0033 void DavCollectionModifyJob::setProperty(const QString &prop, const QString &value, const QString &ns)
0034 {
0035     QDomElement propElement;
0036 
0037     if (ns.isEmpty()) {
0038         propElement = mQuery.createElement(prop);
0039     } else {
0040         propElement = mQuery.createElementNS(ns, prop);
0041     }
0042 
0043     const QDomText textElement = mQuery.createTextNode(value);
0044     propElement.appendChild(textElement);
0045 
0046     mSetProperties << propElement;
0047 }
0048 
0049 void DavCollectionModifyJob::removeProperty(const QString &prop, const QString &ns)
0050 {
0051     QDomElement propElement;
0052 
0053     if (ns.isEmpty()) {
0054         propElement = mQuery.createElement(prop);
0055     } else {
0056         propElement = mQuery.createElementNS(ns, prop);
0057     }
0058 
0059     mRemoveProperties << propElement;
0060 }
0061 
0062 void DavCollectionModifyJob::start()
0063 {
0064     if (mSetProperties.isEmpty() && mRemoveProperties.isEmpty()) {
0065         setError(ERR_COLLECTIONMODIFY_NO_PROPERITES);
0066         setErrorTextFromDavError();
0067         emitResult();
0068         return;
0069     }
0070 
0071     QDomDocument mQuery;
0072     QDomElement propertyUpdateElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("propertyupdate"));
0073     mQuery.appendChild(propertyUpdateElement);
0074 
0075     if (!mSetProperties.isEmpty()) {
0076         QDomElement setElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("set"));
0077         propertyUpdateElement.appendChild(setElement);
0078 
0079         QDomElement propElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
0080         setElement.appendChild(propElement);
0081 
0082         foreach (const QDomElement &element, mSetProperties) {
0083             propElement.appendChild(element);
0084         }
0085     }
0086 
0087     if (!mRemoveProperties.isEmpty()) {
0088         QDomElement removeElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("remove"));
0089         propertyUpdateElement.appendChild(removeElement);
0090 
0091         QDomElement propElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
0092         removeElement.appendChild(propElement);
0093 
0094         foreach (const QDomElement &element, mSetProperties) {
0095             propElement.appendChild(element);
0096         }
0097     }
0098 
0099     auto job = DavManager::self()->createPropPatchJob(mUrl.url(), mQuery);
0100     connect(job, &DavJob::result, this, &DavCollectionModifyJob::davJobFinished);
0101 }
0102 
0103 void DavCollectionModifyJob::davJobFinished(KJob *job)
0104 {
0105     auto davJob = static_cast<DavJob*>(job);
0106     if (davJob->error()) {
0107         setErrorFromJob(davJob, ERR_COLLECTIONMODIFY);
0108         emitResult();
0109         return;
0110     }
0111 
0112     const QDomDocument response = davJob->response();
0113     QDomElement responseElement = Utils::firstChildElementNS(response.documentElement(), QStringLiteral("DAV:"), QStringLiteral("response"));
0114 
0115     bool hasError = false;
0116 
0117     // parse all propstats answers to get the eventual errors
0118     const QDomNodeList propstats = responseElement.elementsByTagNameNS(QStringLiteral("DAV:"), QStringLiteral("propstat"));
0119     for (int i = 0; i < propstats.length(); ++i) {
0120         const QDomElement propstatElement = propstats.item(i).toElement();
0121         const QDomElement statusElement = Utils::firstChildElementNS(propstatElement, QStringLiteral("DAV:"), QStringLiteral("status"));
0122 
0123         const QString statusText = statusElement.text();
0124         if (statusText.contains(QStringLiteral("200"))) {
0125             continue;
0126         } else {
0127             // Generic error
0128             hasError = true;
0129             break;
0130         }
0131     }
0132 
0133     if (hasError) {
0134 
0135         QString description;
0136         // Trying to get more information about the error
0137         const QDomElement responseDescriptionElement = Utils::firstChildElementNS(responseElement, QStringLiteral("DAV:"), QStringLiteral("responsedescription"));
0138         if (!responseDescriptionElement.isNull()) {
0139             description = responseDescriptionElement.text();
0140         }
0141         setDavError(Error{ERR_COLLECTIONMODIFY_RESPONSE, 0, 0, description, 0});
0142     }
0143 
0144     emitResult();
0145 }