1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#include <QtCore/QDateTime>
#include <QtCore/QEventLoop>
#include <QtCore/QDebug>
#include "xmlrpcinterface.h"
#include "xmlrpcpendingcall.h"
XmlRpcPendingCall::XmlRpcPendingCall(QNetworkReply *reply, XmlRpcInterface *parent)
: QObject(parent), _reply(reply), _state(StateWaitingReply)
{
connect(_reply, SIGNAL(finished()), SLOT(handleRequestFinished()));
_reply->setParent(this);
}
void XmlRpcPendingCall::waitForFinished()
{
if (_state == StateWaitingReply) {
QEventLoop loop;
connect(this, SIGNAL(finished(XmlRpcPendingCall*)), &loop, SLOT(quit()));
loop.exec();
}
}
bool XmlRpcPendingCall::decodeMethodResponse(QXmlStreamReader *r)
{
if (r->readNextStartElement()) {
if (r->name() == "fault") {
_state = StateFaultReceived;
if (r->readNextStartElement()) {
if (r->name() != "value") return false;
_value = decodeValue(r);
if (!_value.isValid()) return false;
}
return true;
} else if (r->name() == "params") {
_state = StateReplyReceived;
if (r->readNextStartElement()) {
if (r->name() != "param") return false;
if (r->readNextStartElement()) {
if (r->name() != "value") return false;
_value = decodeValue(r);
if (!_value.isValid()) return false;
} else {
return false;
}
}
return true;
}
}
return false;
}
QVariant XmlRpcPendingCall::decodeValue(QXmlStreamReader *r)
{
Q_ASSERT(r->isStartElement() && r->name() == "value");
QVariant value;
if (r->readNextStartElement()) {
if (r->name() == "string") {
value = QVariant::fromValue(r->readElementText());
} else if (r->name() == "int") {
bool ok;
value = QVariant::fromValue(r->readElementText().toInt(&ok));
if (!ok) value.clear();
} else if (r->name() == "boolean") {
bool ok;
value = QVariant::fromValue<bool>(r->readElementText().toInt(&ok));
if (!ok) value.clear();
} else if (r->name() == "double") {
bool ok;
value = QVariant::fromValue(r->readElementText().toDouble(&ok));
if (!ok) value.clear();
} else if (r->name() == "dateTime.iso8601") {
QString text = r->readElementText();
QDateTime dateTime = QDateTime::fromString(text, Qt::ISODate);
if (!dateTime.isValid()) {
// Qt seems not be happy without dashes
text.insert(4, '-');
text.insert(7, '-');
dateTime = QDateTime::fromString(text, Qt::ISODate);
if (!dateTime.isValid()) {
qWarning() << "Invalid dateTime format" << text;
return QVariant();
}
}
value = QVariant::fromValue(dateTime);
} else if (r->name() == "base64") {
QByteArray data = r->readElementText().toAscii();
value = QVariant::fromValue(QByteArray::fromBase64(data));
} else if (r->name() == "array") {
QList<QVariant> list;
if (!r->readNextStartElement() || r->name() != "data") {
qWarning() << "Unexpected element inside <array>:" << r->name();
return QVariant();
}
while (r->readNextStartElement()) {
if (r->name() != "value") return QVariant();
QVariant value = decodeValue(r);
if (!value.isValid()) return QVariant();
list.append(value);
}
if (r->readNextStartElement()) {
// No other elements
qWarning() << "Unexpected element inside <array>:" << r->name();
return QVariant();
}
value = QVariant::fromValue(list);
} else if (r->name() == "struct") {
QMap<QString, QVariant> map;
while (r->readNextStartElement()) {
if (r->name() != "member") return QVariant();
QString name;
if (r->readNextStartElement() && r->name() == "name") {
name = r->readElementText();
} else {
qWarning() << "Malformed struct";
return QVariant();
}
if (r->readNextStartElement() && r->name() == "value") {
QVariant value = decodeValue(r);
if (!value.isValid()) return QVariant();
map.insert(name, value);
} else {
qWarning() << "Malformed struct";
return QVariant();
}
if (r->readNextStartElement()) {
// No other elements
qWarning() << "Unexpected element inside <member>" << r->name();
return false;
}
}
value = QVariant::fromValue(map);
} else {
qWarning() << "Unknown value type:" << r->name();
}
}
if (r->readNextStartElement()) {
// There is more than one element inside this <value>
qWarning() << "More than element inside <value>";
return QVariant();
} else if (r->isEndElement() && r->name() == "value") {
// Everything OK
return value;
} else {
qWarning() << "Expected </value> instead of" << r->name();
return QVariant();
}
}
void XmlRpcPendingCall::handleRequestFinished()
{
Q_ASSERT(_state == StateWaitingReply);
QNetworkReply::NetworkError error = _reply->error();
if (error == QNetworkReply::NoError) {
QByteArray data = _reply->readAll();
#if XML_RPC_DEBUG
qDebug() << "Response:" << data;
#endif
QXmlStreamReader reader(data);
bool parse_ok = false;
if (reader.readNextStartElement()) {
if (reader.name() == "methodResponse") {
parse_ok = decodeMethodResponse(&reader);
}
}
if (parse_ok) {
Q_ASSERT(_state == StateReplyReceived || _state == StateFaultReceived);
} else {
qWarning() << "Parse error!";
QVariantMap obj;
obj.insert("code", QVariant(reader.error()));
obj.insert("message", QVariant(reader.errorString()));
_value = obj;
_state = StateParseError;
}
} else {
qWarning() << "Network error!" << error;
QVariantMap obj;
obj.insert("code", QVariant(error));
obj.insert("message", QVariant(_reply->errorString()));
_value = obj;
_state = StateNetworkError;
}
emit finished(this);
}
|