1
/***************************************************************************
2
* Copyright (C) 2005 - 2007 by *
3
* Max Howell, Last.fm Ltd <max@last.fm> *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
19
***************************************************************************/
22
#include <QDomElement>
24
#include <QStringList>
28
XmlRpc::toString() const
30
Q_ASSERT( !m_method.isEmpty() );
32
QString xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
34
"<methodName>" + m_method + "</methodName>" +
37
foreach (QVariant const p, m_parameters)
39
xml += "<param><value>";
43
case QVariant::String:
44
xml += "<string>" + escape( p.toString() ) + "</string>";
47
case QVariant::StringList:
48
xml += "<array><data>";
49
foreach (QString s, p.toStringList())
50
xml += "<value><string>" + escape( s ) + "</string></value>";
51
xml += "</data></array>";
55
// Support for this type not yet implemented
59
xml += "</value></param>";
62
xml += "</params></methodCall>";
68
XmlRpc::parse( QByteArray xmlResponse, QList<QVariant>& returnValues, QString& error )
71
if ( !xml.setContent( xmlResponse ) )
73
error = "Couldn't parse XML response: " + xmlResponse;
77
QDomNodeList fault = xml.elementsByTagName( "fault" );
78
if ( !fault.isEmpty() )
80
// TODO: Parse XML RPC fault struct here
81
error = "Fault present in XML response: " + xmlResponse;
85
QDomNodeList params = xml.elementsByTagName( "param" );
86
if ( params.isEmpty() )
88
error = "No params present in XML response: " + xmlResponse;
92
for ( int i = 0; i < params.count(); ++i )
94
QDomNode node = params.at( i );
96
// Skip past the pointless "<value>" tag
97
QDomElement param = node.firstChildElement().firstChildElement();
100
error = "Malformed XML: " + xmlResponse;
104
returnValues << parseValue( param );
111
XmlRpc::parseValue( const QDomElement& e )
113
QString const tag = e.tagName();
115
switch (typeFromString( tag ))
118
return unescape( e.text() );
121
return e.text().toInt();
124
return (bool) e.text().toInt();
127
QMap<QString, QVariant> map;
128
QDomNodeList nodes = e.elementsByTagName( "member" );
130
for (int j = 0; j < nodes.count(); ++j)
132
QDomNode const n = nodes.at( j );
133
QDomElement name = n.firstChildElement( "name" );
134
QDomElement value = n.firstChildElement( "value" );
136
QVariant v = parseValue( value.firstChildElement() );
138
map.insert( name.text(), v );
145
QList<QVariant> array;
147
QDomNodeList nodes = e.firstChild().childNodes();
149
for (int j = 0; j < nodes.count(); ++j) {
150
QDomNode node = nodes.at( j );
151
if (node.isElement() && node.toElement().tagName() == "value")
152
array += parseValue( node.firstChildElement() );
159
// Support for this type not yet implemented