Libkdav2

davcollectionmodifyjob.cpp
1/*
2 Copyright (c) 2010 Grégory Oestreicher <greg@kamago.net>
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 "davcollectionmodifyjob.h"
20#include "davmanager.h"
21
22#include "daverror.h"
23#include "utils.h"
24#include "davjob.h"
25
26using namespace KDAV2;
27
29 : DavJobBase(parent), mUrl(url)
30{
31}
32
33void DavCollectionModifyJob::setProperty(const QString &prop, const QString &value, const QString &ns)
34{
35 QDomElement propElement;
36
37 if (ns.isEmpty()) {
38 propElement = mQuery.createElement(prop);
39 } else {
40 propElement = mQuery.createElementNS(ns, prop);
41 }
42
43 const QDomText textElement = mQuery.createTextNode(value);
44 propElement.appendChild(textElement);
45
46 mSetProperties << propElement;
47}
48
50{
51 QDomElement propElement;
52
53 if (ns.isEmpty()) {
54 propElement = mQuery.createElement(prop);
55 } else {
56 propElement = mQuery.createElementNS(ns, prop);
57 }
58
59 mRemoveProperties << propElement;
60}
61
63{
64 if (mSetProperties.isEmpty() && mRemoveProperties.isEmpty()) {
65 setError(ERR_COLLECTIONMODIFY_NO_PROPERITES);
66 setErrorTextFromDavError();
67 emitResult();
68 return;
69 }
70
71 QDomDocument mQuery;
72 QDomElement propertyUpdateElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("propertyupdate"));
73 mQuery.appendChild(propertyUpdateElement);
74
75 if (!mSetProperties.isEmpty()) {
76 QDomElement setElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("set"));
77 propertyUpdateElement.appendChild(setElement);
78
79 QDomElement propElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
80 setElement.appendChild(propElement);
81
82 foreach (const QDomElement &element, mSetProperties) {
83 propElement.appendChild(element);
84 }
85 }
86
87 if (!mRemoveProperties.isEmpty()) {
88 QDomElement removeElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("remove"));
89 propertyUpdateElement.appendChild(removeElement);
90
91 QDomElement propElement = mQuery.createElementNS(QStringLiteral("DAV:"), QStringLiteral("prop"));
92 removeElement.appendChild(propElement);
93
94 foreach (const QDomElement &element, mSetProperties) {
95 propElement.appendChild(element);
96 }
97 }
98
99 auto job = DavManager::self()->createPropPatchJob(mUrl.url(), mQuery);
100 connect(job, &DavJob::result, this, &DavCollectionModifyJob::davJobFinished);
101}
102
103void DavCollectionModifyJob::davJobFinished(KJob *job)
104{
105 auto davJob = static_cast<DavJob*>(job);
106 if (davJob->error()) {
107 setErrorFromJob(davJob, ERR_COLLECTIONMODIFY);
108 emitResult();
109 return;
110 }
111
112 const QDomDocument response = davJob->response();
113 QDomElement responseElement = Utils::firstChildElementNS(response.documentElement(), QStringLiteral("DAV:"), QStringLiteral("response"));
114
115 bool hasError = false;
116
117 // parse all propstats answers to get the eventual errors
118 const QDomNodeList propstats = responseElement.elementsByTagNameNS(QStringLiteral("DAV:"), QStringLiteral("propstat"));
119 for (int i = 0; i < propstats.length(); ++i) {
120 const QDomElement propstatElement = propstats.item(i).toElement();
121 const QDomElement statusElement = Utils::firstChildElementNS(propstatElement, QStringLiteral("DAV:"), QStringLiteral("status"));
122
123 const QString statusText = statusElement.text();
124 if (statusText.contains(QStringLiteral("200"))) {
125 continue;
126 } else {
127 // Generic error
128 hasError = true;
129 break;
130 }
131 }
132
133 if (hasError) {
134
136 // Trying to get more information about the error
137 const QDomElement responseDescriptionElement = Utils::firstChildElementNS(responseElement, QStringLiteral("DAV:"), QStringLiteral("responsedescription"));
138 if (!responseDescriptionElement.isNull()) {
139 description = responseDescriptionElement.text();
140 }
141 setDavError(Error{ERR_COLLECTIONMODIFY_RESPONSE, 0, 0, description, 0});
142 }
143
144 emitResult();
145}
void start() override
Starts the job.
DavCollectionModifyJob(const DavUrl &url, QObject *parent=nullptr)
Creates a new dav collection modify job.
void setProperty(const QString &property, const QString &value, const QString &ns=QString())
Sets the property that shall be modified by the job.
void removeProperty(const QString &property, const QString &ns)
Sets the property that shall be removed by the job.
base class for the jobs used by the resource.
Definition davjobbase.h:38
void setErrorFromJob(DavJob *, ErrorNumber jobErrorCode=ERR_PROBLEM_WITH_REQUEST)
Set the error of this job from a failed DavJob (executed by this job).
static DavManager * self()
Returns the global instance of the DAV manager.
DavJob * createPropPatchJob(const QUrl &url, const QDomDocument &document)
Returns a preconfigured DAV PROPPATCH job.
A helper class to combine url and protocol of a DAV url.
Definition davurl.h:36
QUrl url() const
Returns the url that identifies the DAV object.
Definition davurl.cpp:41
void description(KJob *job, const QString &title, const QPair< QString, QString > &field1=QPair< QString, QString >(), const QPair< QString, QString > &field2=QPair< QString, QString >())
void emitResult()
void result(KJob *job)
void setError(int errorCode)
QDomElement KPIMKDAV2_EXPORT firstChildElementNS(const QDomElement &parent, const QString &namespaceUri, const QString &tagName)
Returns the first child element of parent that has the given tagName and is part of the namespaceUri.
Definition utils.cpp:37
QDomElement createElement(const QString &tagName)
QDomElement createElementNS(const QString &nsURI, const QString &qName)
QDomText createTextNode(const QString &value)
QDomElement documentElement() const const
QDomNodeList elementsByTagNameNS(const QString &nsURI, const QString &localName) const const
QString text() const const
QDomNode appendChild(const QDomNode &newChild)
bool isNull() const const
QDomElement toElement() const const
QDomNode item(int index) const const
int length() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.