KIO

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

KDE's Doxygen guidelines are available online.