KIO
davjob.cpp
Go to the documentation of this file.
00001 // -*- c++ -*- 00002 /* This file is part of the KDE libraries 00003 Copyright (C) 2002 Jan-Pascal van Best <janpascal@vanbest.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "davjob.h" 00022 00023 #include <kurl.h> 00024 00025 #include <QtCore/QObject> 00026 #include <QtCore/QCharRef> 00027 #include <QtCore/QMutableStringListIterator> 00028 #include <QtCore/QPointer> 00029 #include <QtXml/QDomDocument> 00030 00031 #include <sys/types.h> 00032 #include <sys/stat.h> 00033 00034 #include <kdebug.h> 00035 #include <kio/http.h> 00036 00037 #include "jobclasses.h" 00038 #include "global.h" 00039 #include "job.h" 00040 #include "job_p.h" 00041 00042 #include "jobuidelegate.h" 00043 00044 using namespace KIO; 00045 00047 class KIO::DavJobPrivate: public KIO::TransferJobPrivate 00048 { 00049 public: 00050 DavJobPrivate(const KUrl& url) 00051 : TransferJobPrivate(url, KIO::CMD_SPECIAL, QByteArray(), QByteArray()) 00052 {} 00053 QByteArray savedStaticData; 00054 QByteArray str_response; 00055 QDomDocument m_response; 00056 //TransferJob *m_subJob; 00057 //bool m_suspended; 00058 00059 Q_DECLARE_PUBLIC(DavJob) 00060 00061 static inline DavJob *newJob(const KUrl &url, int method, const QString &request, 00062 JobFlags flags) 00063 { 00064 DavJob *job = new DavJob(*new DavJobPrivate(url), method, request); 00065 job->setUiDelegate(new JobUiDelegate); 00066 if (!(flags & HideProgressInfo)) 00067 KIO::getJobTracker()->registerJob(job); 00068 return job; 00069 } 00070 }; 00071 00072 DavJob::DavJob(DavJobPrivate &dd, int method, const QString &request) 00073 : TransferJob(dd) 00074 { 00075 // We couldn't set the args when calling the parent constructor, 00076 // so do it now. 00077 Q_D(DavJob); 00078 QDataStream stream( &d->m_packedArgs, QIODevice::WriteOnly ); 00079 stream << (int) 7 << d->m_url << method; 00080 // Same for static data 00081 if ( ! request.isEmpty() ) { 00082 d->staticData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + request.toUtf8(); 00083 d->staticData.truncate( d->staticData.size() - 1 ); 00084 d->savedStaticData = d->staticData; 00085 } 00086 } 00087 00088 QDomDocument& DavJob::response() 00089 { 00090 return d_func()->m_response; 00091 } 00092 00093 void DavJob::slotData( const QByteArray& data ) 00094 { 00095 Q_D(DavJob); 00096 if(d->m_redirectionURL.isEmpty() || !d->m_redirectionURL.isValid() || error()) { 00097 unsigned int oldSize = d->str_response.size(); 00098 d->str_response.resize( oldSize + data.size() ); 00099 memcpy( d->str_response.data() + oldSize, data.data(), data.size() ); 00100 } 00101 } 00102 00103 void DavJob::slotFinished() 00104 { 00105 Q_D(DavJob); 00106 // kDebug(7113) << d->str_response; 00107 if (!d->m_redirectionURL.isEmpty() && d->m_redirectionURL.isValid() && 00108 (d->m_command == CMD_SPECIAL)) { 00109 QDataStream istream( d->m_packedArgs ); 00110 int s_cmd, s_method; 00111 KUrl s_url; 00112 istream >> s_cmd; 00113 istream >> s_url; 00114 istream >> s_method; 00115 // PROPFIND 00116 if ( (s_cmd == 7) && (s_method == (int)KIO::DAV_PROPFIND) ) { 00117 d->m_packedArgs.truncate(0); 00118 QDataStream stream( &d->m_packedArgs, QIODevice::WriteOnly ); 00119 stream << (int)7 << d->m_redirectionURL << (int)KIO::DAV_PROPFIND; 00120 } 00121 } else if ( ! d->m_response.setContent( d->str_response, true ) ) { 00122 // An error occurred parsing the XML response 00123 QDomElement root = d->m_response.createElementNS( "DAV:", "error-report" ); 00124 d->m_response.appendChild( root ); 00125 00126 QDomElement el = d->m_response.createElementNS( "DAV:", "offending-response" ); 00127 QDomText textnode = d->m_response.createTextNode( d->str_response ); 00128 el.appendChild( textnode ); 00129 root.appendChild( el ); 00130 } 00131 // kDebug(7113) << d->m_response.toString(); 00132 TransferJob::slotFinished(); 00133 d->staticData = d->savedStaticData; // Need to send DAV request to this host too 00134 } 00135 00136 /* Convenience methods */ 00137 00138 DavJob* KIO::davPropFind( const KUrl& url, const QDomDocument& properties, const QString &depth, JobFlags flags ) 00139 { 00140 DavJob *job = DavJobPrivate::newJob(url, (int) KIO::DAV_PROPFIND, properties.toString(), flags); 00141 job->addMetaData( "davDepth", depth ); 00142 return job; 00143 } 00144 00145 00146 DavJob* KIO::davPropPatch( const KUrl& url, const QDomDocument& properties, JobFlags flags ) 00147 { 00148 return DavJobPrivate::newJob(url, (int) KIO::DAV_PROPPATCH, properties.toString(), 00149 flags); 00150 } 00151 00152 DavJob* KIO::davSearch( const KUrl& url, const QString& nsURI, const QString& qName, const QString& query, JobFlags flags ) 00153 { 00154 QDomDocument doc; 00155 QDomElement searchrequest = doc.createElementNS( "DAV:", "searchrequest" ); 00156 QDomElement searchelement = doc.createElementNS( nsURI, qName ); 00157 QDomText text = doc.createTextNode( query ); 00158 searchelement.appendChild( text ); 00159 searchrequest.appendChild( searchelement ); 00160 doc.appendChild( searchrequest ); 00161 return DavJobPrivate::newJob(url, KIO::DAV_SEARCH, doc.toString(), flags); 00162 } 00163 00164 DavJob* KIO::davReport( const KUrl& url, const QString& report, const QString &depth, JobFlags flags ) 00165 { 00166 DavJob *job = DavJobPrivate::newJob(url, (int) KIO::DAV_REPORT, report, flags); 00167 job->addMetaData( "davDepth", depth ); 00168 return job; 00169 } 00170 00171 #include "davjob.moc"
KDE 4.6 API Reference