~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/libUnicorn/WebService/XmlRpc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2007-12-31 09:49:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071231094954-ix1amvcsj9pk61ya
Tags: 1:1.4.1.57486.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #180254), remaining changes:
  - debian/rules;
    - Added dh_icons
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 - 2007 by                                          *
 
3
 *      Max Howell, Last.fm Ltd <max@last.fm>                              *
 
4
 *                                                                         *
 
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.                                   *
 
9
 *                                                                         *
 
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.                          *
 
14
 *                                                                         *
 
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
 ***************************************************************************/
 
20
 
 
21
#include "XmlRpc.h"
 
22
#include <QDomElement>
 
23
#include <QDomNode>
 
24
#include <QStringList>
 
25
 
 
26
 
 
27
QString
 
28
XmlRpc::toString() const
 
29
{
 
30
    Q_ASSERT( !m_method.isEmpty() );
 
31
 
 
32
    QString xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 
33
                      "<methodCall>"
 
34
                          "<methodName>" + m_method + "</methodName>" +
 
35
                          "<params>";
 
36
 
 
37
    foreach (QVariant const p, m_parameters)
 
38
    {
 
39
        xml += "<param><value>";
 
40
 
 
41
        switch (p.type())
 
42
        {
 
43
            case QVariant::String:
 
44
                xml += "<string>" + escape( p.toString() ) + "</string>";
 
45
                break;
 
46
 
 
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>";
 
52
                break;
 
53
 
 
54
            default:
 
55
                // Support for this type not yet implemented
 
56
                Q_ASSERT( false );
 
57
        }
 
58
 
 
59
        xml += "</value></param>";
 
60
    }
 
61
 
 
62
    xml += "</params></methodCall>";
 
63
 
 
64
    return xml;
 
65
}
 
66
 
 
67
bool
 
68
XmlRpc::parse( QByteArray xmlResponse, QList<QVariant>& returnValues, QString& error )
 
69
{
 
70
    QDomDocument xml;
 
71
    if ( !xml.setContent( xmlResponse ) )
 
72
    {
 
73
        error = "Couldn't parse XML response: " + xmlResponse;
 
74
        return false;
 
75
    }
 
76
 
 
77
    QDomNodeList fault = xml.elementsByTagName( "fault" );
 
78
    if ( !fault.isEmpty() )
 
79
    {
 
80
        // TODO: Parse XML RPC fault struct here
 
81
        error = "Fault present in XML response: " + xmlResponse;
 
82
        return false;
 
83
    }
 
84
 
 
85
    QDomNodeList params = xml.elementsByTagName( "param" );
 
86
    if ( params.isEmpty() )
 
87
    {
 
88
        error = "No params present in XML response: " + xmlResponse;
 
89
        return false;
 
90
    }
 
91
 
 
92
    for ( int i = 0; i < params.count(); ++i )
 
93
    {
 
94
        QDomNode node = params.at( i );
 
95
 
 
96
        // Skip past the pointless "<value>" tag
 
97
        QDomElement param = node.firstChildElement().firstChildElement();
 
98
        if ( param.isNull() )
 
99
        {
 
100
            error = "Malformed XML: " + xmlResponse;
 
101
            return false;
 
102
        }
 
103
        else
 
104
            returnValues << parseValue( param );
 
105
    }
 
106
 
 
107
    return true;
 
108
}
 
109
 
 
110
QVariant
 
111
XmlRpc::parseValue( const QDomElement& e )
 
112
{
 
113
    QString const tag = e.tagName();
 
114
    
 
115
    switch (typeFromString( tag ))
 
116
    {
 
117
    case String:
 
118
        return unescape( e.text() );
 
119
 
 
120
    case Integer:
 
121
        return e.text().toInt();
 
122
    
 
123
    case Boolean:
 
124
        return (bool) e.text().toInt();
 
125
 
 
126
    case Struct: {
 
127
        QMap<QString, QVariant> map;
 
128
        QDomNodeList nodes = e.elementsByTagName( "member" );
 
129
 
 
130
        for (int j = 0; j < nodes.count(); ++j) 
 
131
        {
 
132
            QDomNode const n = nodes.at( j );
 
133
            QDomElement name = n.firstChildElement( "name" );
 
134
            QDomElement value = n.firstChildElement( "value" );
 
135
 
 
136
            QVariant v = parseValue( value.firstChildElement() );
 
137
 
 
138
            map.insert( name.text(), v );
 
139
        }
 
140
 
 
141
        return map;
 
142
    }
 
143
 
 
144
    case Array: {
 
145
        QList<QVariant> array;
 
146
 
 
147
        QDomNodeList nodes = e.firstChild().childNodes();
 
148
 
 
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() );
 
153
        }
 
154
 
 
155
        return array;
 
156
    }
 
157
        
 
158
    default:
 
159
        // Support for this type not yet implemented
 
160
        Q_ASSERT( false );
 
161
        return QVariant();
 
162
    }
 
163
}