1 | /***************************************************************************** |
2 | * Copyright (C) 2003 - 2004 by Frerich Raabe <raabe@kde.org> * |
3 | * Tobias Koenig <tokoe@kde.org> * |
4 | * Copyright (C) 2006 by Narayan Newton <narayannewton@gmail.com> * |
5 | * * |
6 | * This program is distributed in the hope that it will be useful, but * |
7 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * |
8 | * or FITNESS FOR A PARTICULAR PURPOSE. For licensing and distribution * |
9 | * details, check the accompanying file 'COPYING.BSD'. * |
10 | *****************************************************************************/ |
11 | /** |
12 | @file |
13 | This file is part of the API for accessing XML-RPC Servers |
14 | and defines the Client class. |
15 | |
16 | @brief |
17 | Defines the Client class. |
18 | |
19 | @author Frerich Raabe <raabe@kde.org> |
20 | @author Tobias Koenig <tokoe@kde.org> |
21 | @author Narayan Newton <narayannewton@gmail.com> |
22 | */ |
23 | |
24 | #include "client.h" |
25 | #include "query.h" |
26 | |
27 | #include <kio/job.h> |
28 | #include <kdebug.h> |
29 | |
30 | #include <QtCore/QVariant> |
31 | |
32 | using namespace KXmlRpc; |
33 | |
34 | class Client::Private |
35 | { |
36 | public: |
37 | Private() : mUserAgent( "KDE XMLRPC resources" ), mDigestAuth( false ){} |
38 | |
39 | void queryFinished( Query * ); |
40 | |
41 | KUrl mUrl; |
42 | QString mUserAgent; |
43 | bool mDigestAuth; |
44 | QList<Query*> mPendingQueries; |
45 | }; |
46 | |
47 | void Client::Private::queryFinished( Query *query ) |
48 | { |
49 | mPendingQueries.removeAll( query ); |
50 | query->deleteLater(); |
51 | } |
52 | |
53 | Client::Client( QObject *parent ) |
54 | : QObject( parent ), d( new Private ) |
55 | { |
56 | } |
57 | |
58 | Client::Client( const KUrl &url, QObject *parent ) |
59 | : QObject( parent ), d( new Private ) |
60 | { |
61 | d->mUrl = url; |
62 | } |
63 | |
64 | Client::~Client() |
65 | { |
66 | QList<Query *>::Iterator it; |
67 | for ( it = d->mPendingQueries.begin(); it != d->mPendingQueries.end(); ++it ) { |
68 | ( *it )->deleteLater(); |
69 | } |
70 | |
71 | d->mPendingQueries.clear(); |
72 | |
73 | delete d; |
74 | } |
75 | |
76 | void Client::setUrl( const KUrl &url ) |
77 | { |
78 | d->mUrl = url.isValid() ? url : KUrl(); |
79 | } |
80 | |
81 | KUrl Client::url() const |
82 | { |
83 | return d->mUrl; |
84 | } |
85 | |
86 | QString Client::userAgent() const |
87 | { |
88 | return d->mUserAgent; |
89 | } |
90 | |
91 | void Client::setUserAgent( const QString &userAgent ) |
92 | { |
93 | d->mUserAgent = userAgent; |
94 | } |
95 | |
96 | bool Client::isDigestAuthEnabled() const |
97 | { |
98 | return d->mDigestAuth; |
99 | } |
100 | |
101 | void Client::setDigestAuthEnabled( bool enabled ) |
102 | { |
103 | d->mDigestAuth = enabled; |
104 | } |
105 | |
106 | void Client::call( const QString &method, const QList<QVariant> &args, |
107 | QObject *msgObj, const char *messageSlot, |
108 | QObject *faultObj, const char *faultSlot, const QVariant &id ) |
109 | { |
110 | |
111 | QMap<QString, QString> metaData; |
112 | |
113 | if ( d->mUrl.isEmpty() ) { |
114 | kWarning() << "Cannot execute call to" << method << ": empty server URL" ; |
115 | } |
116 | |
117 | //Fill metadata, with userAgent and possible digest auth |
118 | if ( d->mUserAgent.isEmpty() ) { |
119 | metaData["UserAgent" ] = "KDE-XMLRPC" ; |
120 | } else { |
121 | metaData["UserAgent" ] = d->mUserAgent; |
122 | } |
123 | |
124 | if ( d->mDigestAuth ) { |
125 | metaData["WWW-Authenticate:" ] = "Digest" ; |
126 | } |
127 | |
128 | Query *query = Query::create( id, this ); |
129 | connect( query, SIGNAL(message(QList<QVariant>,QVariant)), msgObj, messageSlot ); |
130 | connect( query, SIGNAL(fault(int,QString,QVariant)), faultObj, faultSlot ); |
131 | connect( query, SIGNAL(finished(Query*)), this, SLOT(queryFinished(Query*)) ); |
132 | d->mPendingQueries.append( query ); |
133 | |
134 | query->call( d->mUrl.url(), method, args, metaData ); |
135 | } |
136 | |
137 | void Client::call( const QString &method, const QVariant &arg, |
138 | QObject *msgObj, const char *messageSlot, |
139 | QObject *faultObj, const char *faultSlot, |
140 | const QVariant &id ) |
141 | { |
142 | QList<QVariant> args; |
143 | args << arg ; |
144 | call( method, args, msgObj, messageSlot, faultObj, faultSlot, id ); |
145 | } |
146 | |
147 | void Client::call( const QString &method, int arg, |
148 | QObject *msgObj, const char *messageSlot, |
149 | QObject *faultObj, const char *faultSlot, |
150 | const QVariant &id ) |
151 | { |
152 | QList<QVariant> args; |
153 | args << QVariant( arg ); |
154 | call( method, args, msgObj, messageSlot, faultObj, faultSlot, id ); |
155 | } |
156 | |
157 | void Client::call( const QString &method, bool arg, |
158 | QObject *msgObj, const char *messageSlot, |
159 | QObject *faultObj, const char *faultSlot, |
160 | const QVariant &id ) |
161 | { |
162 | QList<QVariant> args; |
163 | args << QVariant( arg ); |
164 | call( method, args, msgObj, messageSlot, faultObj, faultSlot, id ); |
165 | } |
166 | |
167 | void Client::call( const QString &method, double arg, |
168 | QObject *msgObj, const char *messageSlot, |
169 | QObject *faultObj, const char *faultSlot, |
170 | const QVariant &id ) |
171 | { |
172 | QList<QVariant> args; |
173 | args << QVariant( arg ); |
174 | call( method, args, msgObj, messageSlot, faultObj, faultSlot, id ); |
175 | } |
176 | |
177 | void Client::call( const QString &method, const QString &arg, |
178 | QObject *msgObj, const char *messageSlot, |
179 | QObject *faultObj, const char *faultSlot, |
180 | const QVariant &id ) |
181 | { |
182 | QList<QVariant> args; |
183 | args << QVariant( arg ); |
184 | call( method, args, msgObj, messageSlot, faultObj, faultSlot, id ); |
185 | } |
186 | |
187 | void Client::call( const QString &method, const QByteArray &arg, |
188 | QObject *msgObj, const char *messageSlot, |
189 | QObject *faultObj, const char *faultSlot, |
190 | const QVariant &id ) |
191 | { |
192 | QList<QVariant> args; |
193 | args << QVariant( arg ); |
194 | call( method, args, msgObj, messageSlot, faultObj, faultSlot, id ); |
195 | } |
196 | |
197 | void Client::call( const QString &method, const QDateTime &arg, |
198 | QObject *msgObj, const char *messageSlot, |
199 | QObject *faultObj, const char *faultSlot, |
200 | const QVariant &id ) |
201 | { |
202 | QList<QVariant> args; |
203 | args << QVariant( arg ); |
204 | call( method, args, msgObj, messageSlot, faultObj, faultSlot, id ); |
205 | } |
206 | |
207 | void Client::call( const QString &method, const QStringList &arg, |
208 | QObject *msgObj, const char *messageSlot, |
209 | QObject *faultObj, const char *faultSlot, |
210 | const QVariant &id ) |
211 | { |
212 | QList<QVariant> args; |
213 | for ( int i = 0; i < arg.count(); ++i ) { |
214 | args << QVariant( arg[ i ] ); |
215 | } |
216 | |
217 | call( method, args, msgObj, messageSlot, faultObj, faultSlot, id ); |
218 | } |
219 | |
220 | #include "moc_client.cpp" |
221 | |