From 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kresources/blogging/xmlrpcjob.cpp | 430 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 430 insertions(+) create mode 100644 kresources/blogging/xmlrpcjob.cpp (limited to 'kresources/blogging/xmlrpcjob.cpp') diff --git a/kresources/blogging/xmlrpcjob.cpp b/kresources/blogging/xmlrpcjob.cpp new file mode 100644 index 00000000..76bee44f --- /dev/null +++ b/kresources/blogging/xmlrpcjob.cpp @@ -0,0 +1,430 @@ +/* This file is part of the KDE libraries + Copyright (C) 2004 Reinhold Kainhofer + + Based on the davjob: + Copyright (C) 2002 Jan-Pascal van Best + XML-RPC specific parts taken from the xmlrpciface: + Copyright (C) 2003 - 2004 by Frerich Raabe + Tobias Koenig + + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "xmlrpcjob.h" + +#include +#include + +#include +#include +#include +#include +#include + + +#define KIO_ARGS QByteArray packedArgs; \ + QDataStream stream( packedArgs, IO_WriteOnly ); stream + +using namespace KIO; + +namespace KIO { + class XMLRPCResult + { + friend class XmlrpcJob; + public: + XMLRPCResult() {} + bool success() const { return m_success; } + int errorCode() const { return m_errorCode; } + QString errorString() const { return m_errorString; } + QValueList data() const { return m_data; } + private: + bool m_success; + int m_errorCode; + QString m_errorString; + QValueList m_data; + }; +} + +class XmlrpcJob::XmlrpcJobPrivate +{ +public: +// QByteArray savedStaticData; +}; + + +XmlrpcJob::XmlrpcJob( const KURL& url, const QString& method, + const QValueList ¶ms, bool showProgressInfo) + : TransferJob( url, KIO::CMD_SPECIAL, QByteArray(), QByteArray(), + showProgressInfo ) +{ + d = new XmlrpcJobPrivate; + // We couldn't set the args when calling the parent constructor, + // so do it now. + QDataStream stream( m_packedArgs, IO_WriteOnly ); + stream << (int)1 << url; +kdDebug()<<"XMLrpcJob::url="<savedStaticData = staticData.copy(); + } + addMetaData( "UserAgent", "KDE XML-RPC TransferJob" ); + addMetaData( "content-type", "Content-Type: text/xml; charset=utf-8" ); + addMetaData( "ConnectTimeout", "50" ); +} + +XmlrpcJob::~XmlrpcJob() +{ + delete d; + d = 0; +} + +QString XmlrpcJob::markupCall( const QString &cmd, + const QValueList &args ) +{ +kdDebug()<<"XmlrpcJob::markupCall, cmd="<\r\n\r\n"; + + markup += "" + cmd + "\r\n"; + + if ( !args.isEmpty() ) + { + markup += "\r\n"; + QValueList::ConstIterator it = args.begin(); + QValueList::ConstIterator end = args.end(); + for ( ; it != end; ++it ) + markup += "\r\n" + marshal( *it ) + "\r\n"; + markup += "\r\n"; + } + + markup += "\r\n"; + + return markup; +} + + + + + +void XmlrpcJob::slotData( const QByteArray& data ) +{ +kdDebug()<<"XmlrpcJob::slotData()"<> s_cmd; + istream >> s_url; + istream >> s_method; + // PROPFIND + if ( (s_cmd == 7) && (s_method == (int)KIO::HTTP_POST) ) { + m_packedArgs.truncate(0); + QDataStream stream( m_packedArgs, IO_WriteOnly ); + stream << (int)7 << m_redirectionURL << (int)KIO::HTTP_POST; + } + } else */ + + kdDebug() << "\033[35;40mResult: " << m_str_response << "\033[0;0m" << endl; + QDomDocument doc; + QString errMsg; + int errLine, errCol; + if ( doc.setContent( m_str_response, false, &errMsg, &errLine, &errCol ) ) { + if ( isMessageResponse( doc ) ) { + m_response = parseMessageResponse( doc ).data(); + m_responseType = XMLRPCMessageResponse; + } else if ( isFaultResponse( doc ) ) { + // TODO: Set the error of the job + m_response.clear(); + m_response << QVariant( parseFaultResponse( doc ).errorString() ); + m_responseType = XMLRPCFaultResponse; + } else { + // TODO: Set the error of the job + m_response.clear(); + m_response << QVariant( i18n( "Unknown type of XML markup received. " + "Markup: \n %1" ).arg( m_str_response ) ); + m_responseType = XMLRPCUnknownResponse; + } + + } else { + // TODO: if we can't parse the XML response, set the correct error message! +// emit fault( -1, i18n( "Received invalid XML markup: %1 at %2:%3" ) +// .arg( errMsg ).arg( errLine ).arg( errCol ), m_id ); + } + + TransferJob::slotFinished(); +// TODO: Redirect: if( d ) staticData = d->savedStaticData.copy(); +// Need to send XMLRPC request to this host too +} + + + + + +bool XmlrpcJob::isMessageResponse( const QDomDocument &doc ) +{ + return doc.documentElement().firstChild().toElement() + .tagName().lower() == "params"; +} + +XMLRPCResult XmlrpcJob::parseMessageResponse( const QDomDocument &doc ) +{ + XMLRPCResult response; + response.m_success = true; + + QDomNode paramNode = doc.documentElement().firstChild().firstChild(); + while ( !paramNode.isNull() ) { + response.m_data << demarshal( paramNode.firstChild().toElement() ); + paramNode = paramNode.nextSibling(); + } + + return response; +} + + + + + +bool XmlrpcJob::isFaultResponse( const QDomDocument &doc ) +{ + return doc.documentElement().firstChild().toElement() + .tagName().lower() == "fault"; +} + +XMLRPCResult XmlrpcJob::parseFaultResponse( const QDomDocument &doc ) +{ + XMLRPCResult response; + response.m_success = false; + + QDomNode errorNode = doc.documentElement().firstChild().firstChild(); + const QVariant errorVariant = demarshal( errorNode.toElement() ); + response.m_errorCode = errorVariant.toMap() [ "faultCode" ].toInt(); + response.m_errorString = errorVariant.toMap() [ "faultString" ].toString(); + + return response; +} + + + + + +QString XmlrpcJob::marshal( const QVariant &arg ) +{ + switch ( arg.type() ) + { + case QVariant::String: + case QVariant::CString: + return "" + arg.toString() + "\r\n"; + case QVariant::Int: + return "" + QString::number( arg.toInt() ) + + "\r\n"; + case QVariant::Double: + return "" + QString::number( arg.toDouble() ) + + "\r\n"; + case QVariant::Bool: + { + QString markup = ""; + markup += arg.toBool() ? "1" : "0"; + markup += "\r\n"; + return markup; + } + case QVariant::ByteArray: + return "" + KCodecs::base64Encode( arg.toByteArray() ) + + "\r\n"; + case QVariant::DateTime: + return "" + + arg.toDateTime().toString( Qt::ISODate ) + + "\r\n"; + case QVariant::List: + { + QString markup = "\r\n"; + const QValueList args = arg.toList(); + QValueList::ConstIterator it = args.begin(); + QValueList::ConstIterator end = args.end(); + for ( ; it != end; ++it ) + markup += marshal( *it ); + markup += "\r\n"; + return markup; + } + case QVariant::Map: + { + QString markup = "\r\n"; + QMap map = arg.toMap(); + QMap::ConstIterator it = map.begin(); + QMap::ConstIterator end = map.end(); + for ( ; it != end; ++it ) + { + markup += "\r\n"; + markup += "" + it.key() + "\r\n"; + markup += marshal( it.data() ); + markup += "\r\n"; + } + markup += "\r\n"; + return markup; + } + default: + kdWarning() << "Failed to marshal unknown variant type: " + << arg.type() << endl; + }; + return QString::null; +} + +QVariant XmlrpcJob::demarshal( const QDomElement &elem ) +{ + Q_ASSERT( elem.tagName().lower() == "value" ); + + if ( !elem.hasChildNodes() ) { + // it doesn't have child nodes, so no explicit type name was given, + // i.e. here comes the value instead of + // here comes the value + // Assume in that case: + // Actually, the element will still have a child node, so this will not help here. + // The dirty hack is at the end of this method. +kdDebug()<<"XmlrpcJob::demarshal: No child nodes, assume type=string. Text: "<= 0 ) { + // It's in the format 20041120T...., so adjust it to correct + // ISO 8601 Format 2004-11-20T..., else QDateTime::fromString won't work: + text = text.insert( 6, '-' ); + text = text.insert( 4, '-' ); + } + return QVariant( QDateTime::fromString( text, Qt::ISODate ) ); + + } else if ( typeName == "array" ) { + + QValueList values; + QDomNode valueNode = typeElement.firstChild().firstChild(); + while ( !valueNode.isNull() ) { + values << demarshal( valueNode.toElement() ); + valueNode = valueNode.nextSibling(); + } + return QVariant( values ); + + } else if ( typeName == "struct" ) { + + QMap map; + QDomNode memberNode = typeElement.firstChild(); + while ( !memberNode.isNull() ) { + const QString key = memberNode.toElement().elementsByTagName( "name" ).item( 0 ).toElement().text(); + const QVariant data = demarshal( memberNode.toElement().elementsByTagName( "value" ).item( 0 ).toElement() ); + map[ key ] = data; + memberNode = memberNode.nextSibling(); + } + return QVariant( map ); + + } else { + + kdWarning() << "Cannot demarshal unknown type " << typeName << ", text= " << typeElement.text() << endl; + // FIXME: This is just a workaround, for the issue mentioned at the beginning of this method. + return QVariant( elem.text() ); + } + + return QVariant(); +} + + + + + +/* Convenience methods */ + +XmlrpcJob* KIO::xmlrpcCall( const KURL& url, const QString &method, const QValueList ¶ms, bool showProgressInfo ) +{ + if ( url.isEmpty() ) { + kdWarning() << "Cannot execute call to " << method << ": empty server URL" << endl; + return 0; + } + XmlrpcJob *job = new XmlrpcJob( url, method, params, showProgressInfo ); +// job->addMetaData( "xmlrpcDepth", depth ); + + return job; +} + +XmlrpcJob* KIO::xmlrpcCall( const KURL& url, const QString &method, + const QVariant &arg, bool showProgressInfo ) +{ + QValueList args; + args << arg; + return KIO::xmlrpcCall( url, method, args, showProgressInfo ); +} + +XmlrpcJob* KIO::xmlrpcCall( const KURL& url, const QString &method, + const QStringList &arg, bool showProgressInfo ) +{ + QValueList args; + QStringList::ConstIterator it = arg.begin(); + QStringList::ConstIterator end = arg.end(); + for ( ; it != end; ++it ) + args << QVariant( *it ); + return KIO::xmlrpcCall( url, method, args, showProgressInfo ); +} + +template +XmlrpcJob* KIO::xmlrpcCall( const KURL& url, const QString &method, + const QValueList&arg, bool showProgressInfo ) +{ + QValueList args; + + typename QValueList::ConstIterator it = arg.begin(); + typename QValueList::ConstIterator end = arg.end(); + for ( ; it != end; ++it ) + args << QVariant( *it ); + return KIO::xmlrpcCall( url, method, args, showProgressInfo ); +} + +#include "xmlrpcjob.moc" -- cgit v1.2.3