Libkdav2

davcollectionmodifyjob.cpp
1 /*
2  Copyright (c) 2010 Gr├ęgory Oestreicher <[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 "davcollectionmodifyjob.h"
20 #include "davmanager.h"
21 
22 #include "daverror.h"
23 #include "utils.h"
24 #include "davjob.h"
25 
26 using namespace KDAV2;
27 
29  : DavJobBase(parent), mUrl(url)
30 {
31 }
32 
33 void 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 
103 void 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;
117 
118  // parse all propstats answers to get the eventual errors
119  const QDomNodeList propstats = responseElement.elementsByTagNameNS(QStringLiteral("DAV:"), QStringLiteral("propstat"));
120  for (int i = 0; i < propstats.length(); ++i) {
121  const QDomElement propstatElement = propstats.item(i).toElement();
122  const QDomElement statusElement = Utils::firstChildElementNS(propstatElement, QStringLiteral("DAV:"), QStringLiteral("status"));
123 
124  const QString statusText = statusElement.text();
125  if (statusText.contains(QStringLiteral("200"))) {
126  continue;
127  } else {
128  // Generic error
129  hasError = true;
130  break;
131  }
132  }
133 
134  if (hasError) {
135  setError(ERR_COLLECTIONMODIFY_RESPONSE);
136 
137  // Trying to get more information about the error
138  const QDomElement responseDescriptionElement = Utils::firstChildElementNS(responseElement, QStringLiteral("DAV:"), QStringLiteral("responsedescription"));
139  if (!responseDescriptionElement.isNull()) {
140  setJobErrorText(responseDescriptionElement.text());
141  }
142  }
143 
144  emitResult();
145 }
QUrl url() const
Returns the url that identifies the DAV object.
Definition: davurl.cpp:41
QDomNode item(int index) const const
QDomNodeList elementsByTagNameNS(const QString &nsURI, const QString &localName) const const
void emitResult()
QDomNode appendChild(const QDomNode &newChild)
static DavManager * self()
Returns the global instance of the DAV manager.
Definition: davmanager.cpp:52
base class for the jobs used by the resource.
Definition: davjobbase.h:37
void setError(int errorCode)
void setProperty(const QString &property, const QString &value, const QString &ns=QString())
Sets the property that shall be modified by the job.
QDomElement documentElement() const const
QDomElement createElementNS(const QString &nsURI, const QString &qName)
A helper class to combine url and protocol of a DAV url.
Definition: davurl.h:35
QDomElement toElement() const const
QString text() const const
bool isEmpty() const const
QDomText createTextNode(const QString &value)
void start() override
Starts the job.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
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
bool isNull() const const
bool isEmpty() const const
void removeProperty(const QString &property, const QString &ns)
Sets the property that shall be removed by the job.
DavJob * createPropPatchJob(const QUrl &url, const QDomDocument &document)
Returns a preconfigured DAV PROPPATCH job.
Definition: davmanager.cpp:98
int length() const const
QDomElement createElement(const QString &tagName)
DavCollectionModifyJob(const DavUrl &url, QObject *parent=nullptr)
Creates a new dav collection modify job.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString errorText() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jul 10 2020 23:16:51 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.