KIO

davjob.cpp
1 // -*- c++ -*-
2 /*
3  This file is part of the KDE libraries
4  SPDX-FileCopyrightText: 2002 Jan-Pascal van Best <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "davjob.h"
10 
11 #include <QDataStream>
12 
13 #include "httpmethod_p.h"
14 
15 #include "jobclasses.h"
16 #include "job.h"
17 #include "job_p.h"
18 
19 using namespace KIO;
20 
22 class KIO::DavJobPrivate: public KIO::TransferJobPrivate
23 {
24 public:
25  explicit DavJobPrivate(const QUrl &url)
26  : TransferJobPrivate(url, KIO::CMD_SPECIAL, QByteArray(), QByteArray())
27  {}
28  QByteArray savedStaticData;
29  QByteArray str_response;
30  QDomDocument m_response;
31  //TransferJob *m_subJob;
32  //bool m_suspended;
33 
34  Q_DECLARE_PUBLIC(DavJob)
35 
36  static inline DavJob *newJob(const QUrl &url, int method, const QString &request,
37  JobFlags flags)
38  {
39  DavJob *job = new DavJob(*new DavJobPrivate(url), method, request);
41  if (!(flags & HideProgressInfo)) {
43  }
44  return job;
45  }
46 };
47 
48 DavJob::DavJob(DavJobPrivate &dd, int method, const QString &request)
49  : TransferJob(dd)
50 {
51  // We couldn't set the args when calling the parent constructor,
52  // so do it now.
53  Q_D(DavJob);
54  QDataStream stream(&d->m_packedArgs, QIODevice::WriteOnly);
55  stream << (int) 7 << d->m_url << method;
56  // Same for static data
57  if (! request.isEmpty()) {
58  d->staticData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + request.toUtf8();
59  d->staticData.chop(1);
60  d->savedStaticData = d->staticData;
61  stream << static_cast<qint64>(d->staticData.size());
62  } else {
63  stream << static_cast<qint64>(-1);
64  }
65 }
66 
68 {
69  return d_func()->m_response;
70 }
71 
72 void DavJob::slotData(const QByteArray &data)
73 {
74  Q_D(DavJob);
75  if (d->m_redirectionURL.isEmpty() || !d->m_redirectionURL.isValid() || error()) {
76  unsigned int oldSize = d->str_response.size();
77  d->str_response.resize(oldSize + data.size());
78  memcpy(d->str_response.data() + oldSize, data.data(), data.size());
79  }
80 }
81 
82 void DavJob::slotFinished()
83 {
84  Q_D(DavJob);
85  //qDebug() << d->str_response;
86  if (!d->m_redirectionURL.isEmpty() && d->m_redirectionURL.isValid() &&
87  (d->m_command == CMD_SPECIAL)) {
88  QDataStream istream(d->m_packedArgs);
89  int s_cmd, s_method;
90  qint64 s_size;
91  QUrl s_url;
92  istream >> s_cmd;
93  istream >> s_url;
94  istream >> s_method;
95  istream >> s_size;
96  // PROPFIND
97  if ((s_cmd == 7) && (s_method == (int)KIO::DAV_PROPFIND)) {
98  d->m_packedArgs.truncate(0);
99  QDataStream stream(&d->m_packedArgs, QIODevice::WriteOnly);
100  stream << (int)7 << d->m_redirectionURL << (int)KIO::DAV_PROPFIND << s_size;
101  }
102  } else if (! d->m_response.setContent(d->str_response, true)) {
103  // An error occurred parsing the XML response
104  QDomElement root = d->m_response.createElementNS(QStringLiteral("DAV:"), QStringLiteral("error-report"));
105  d->m_response.appendChild(root);
106 
107  QDomElement el = d->m_response.createElementNS(QStringLiteral("DAV:"), QStringLiteral("offending-response"));
108  QDomText textnode = d->m_response.createTextNode(QString::fromUtf8(d->str_response));
109  el.appendChild(textnode);
110  root.appendChild(el);
111  }
112  //qDebug() << d->m_response.toString();
113  TransferJob::slotFinished();
114  d->staticData = d->savedStaticData; // Need to send DAV request to this host too
115 }
116 
117 /* Convenience methods */
118 
119 DavJob *KIO::davPropFind(const QUrl &url, const QDomDocument &properties, const QString &depth, JobFlags flags)
120 {
121  DavJob *job = DavJobPrivate::newJob(url, (int) KIO::DAV_PROPFIND, properties.toString(), flags);
122  job->addMetaData(QStringLiteral("davDepth"), depth);
123  return job;
124 }
125 
126 DavJob *KIO::davPropPatch(const QUrl &url, const QDomDocument &properties, JobFlags flags)
127 {
128  return DavJobPrivate::newJob(url, (int) KIO::DAV_PROPPATCH, properties.toString(),
129  flags);
130 }
131 
132 DavJob *KIO::davSearch(const QUrl &url, const QString &nsURI, const QString &qName, const QString &query, JobFlags flags)
133 {
134  QDomDocument doc;
135  QDomElement searchrequest = doc.createElementNS(QStringLiteral("DAV:"), QStringLiteral("searchrequest"));
136  QDomElement searchelement = doc.createElementNS(nsURI, qName);
137  QDomText text = doc.createTextNode(query);
138  searchelement.appendChild(text);
139  searchrequest.appendChild(searchelement);
140  doc.appendChild(searchrequest);
141  return DavJobPrivate::newJob(url, KIO::DAV_SEARCH, doc.toString(), flags);
142 }
143 
144 DavJob *KIO::davReport(const QUrl &url, const QString &report, const QString &depth, JobFlags flags)
145 {
146  DavJob *job = DavJobPrivate::newJob(url, (int) KIO::DAV_REPORT, report, flags);
147  job->addMetaData(QStringLiteral("davDepth"), depth);
148  return job;
149 }
150 
void addMetaData(const QString &key, const QString &value)
Add key/value pair to the meta data that is sent to the slave.
Definition: job.cpp:231
QDomNode appendChild(const QDomNode &newChild)
void setUiDelegate(KJobUiDelegate *delegate)
A namespace for KIO globals.
Definition: authinfo.h:21
QString toString(int indent) const const
Hide progress information dialog, i.e.
Definition: job_base.h:275
void chop(int n)
QDomElement createElementNS(const QString &nsURI, const QString &qName)
QString fromUtf8(const char *str, int size)
bool isEmpty() const const
KIOCORE_EXPORT KJobUiDelegate * createDefaultJobUiDelegate()
Convenience method: use default factory, if there&#39;s one, to create a delegate and return it...
KIOCORE_EXPORT KJobTrackerInterface * getJobTracker()
Returns the job tracker to be used by all KIO jobs (in which HideProgressInfo is not set) ...
Definition: jobtracker.cpp:14
The transfer job pumps data into and/or out of a Slave.
Definition: davjob.h:42
QDomText createTextNode(const QString &value)
KIOCORE_EXPORT DavJob * davPropPatch(const QUrl &url, const QDomDocument &properties, JobFlags flags=DefaultFlags)
Creates a new DavJob that issues a PROPPATCH command.
Definition: davjob.cpp:126
if(recurs()&&!first)
char * data()
KIOCORE_EXPORT DavJob * davSearch(const QUrl &url, const QString &nsURI, const QString &qName, const QString &query, JobFlags flags=DefaultFlags)
Creates a new DavJob that issues a SEARCH command.
Definition: davjob.cpp:132
KIOCORE_EXPORT DavJob * davReport(const QUrl &url, const QString &report, const QString &depth, JobFlags flags=DefaultFlags)
Creates a new DavJob that issues a REPORT command.
Definition: davjob.cpp:144
virtual void registerJob(KJob *job)
The transfer job pumps data into and/or out of a Slave.
Definition: transferjob.h:26
int size() const const
KIOCORE_EXPORT DavJob * davPropFind(const QUrl &url, const QDomDocument &properties, const QString &depth, JobFlags flags=DefaultFlags)
Creates a new DavJob that issues a PROPFIND command.
Definition: davjob.cpp:119
QDomDocument & response()
Returns the response as a QDomDocument.
Definition: davjob.cpp:67
int error() const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Oct 27 2020 23:05:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.