2
* Copyright (C) 2003 Justin Karneges
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
#include "safedelete.h"
23
#include "xmpp_task.h"
24
#include "xmpp_client.h"
25
#include "xmpp_xmlcommon.h"
29
class Task::TaskPrivate
39
bool insig, deleteme, autoDelete;
43
Task::Task(Task *parent)
48
d->client = parent->client();
49
d->id = client()->genUniqueId();
50
connect(d->client, SIGNAL(disconnected()), SLOT(clientDisconnected()));
53
Task::Task(Client *parent, bool)
59
connect(d->client, SIGNAL(disconnected()), SLOT(clientDisconnected()));
73
d->autoDelete = false;
77
Task *Task::parent() const
79
return (Task *)QObject::parent();
82
Client *Task::client() const
87
QDomDocument *Task::doc() const
89
return client()->doc();
92
QString Task::id() const
97
bool Task::success() const
102
int Task::statusCode() const
104
return d->statusCode;
107
const QString & Task::statusString() const
109
return d->statusString;
112
void Task::go(bool autoDelete)
114
d->autoDelete = autoDelete;
116
if (!client() || !&client()->stream()) {
117
qWarning("Task::go(): attempted to send a task over the broken connection.");
127
bool Task::take(const QDomElement &x)
129
const QObjectList p = children();
131
// pass along the xml
133
for(QObjectList::ConstIterator it = p.begin(); it != p.end(); ++it) {
135
if(!obj->inherits("XMPP::Task"))
138
t = static_cast<Task*>(obj);
146
void Task::safeDelete()
153
SafeDelete::deleteSingle(this);
160
void Task::onDisconnect()
164
d->statusCode = ErrDisc;
165
d->statusString = tr("Disconnected");
167
// delay this so that tasks that react don't block the shutdown
168
QTimer::singleShot(0, this, SLOT(done()));
172
void Task::send(const QDomElement &x)
177
void Task::setSuccess(int code, const QString &str)
181
d->statusCode = code;
182
d->statusString = str;
187
void Task::setError(const QDomElement &e)
191
getErrorFromElement(e, d->client->streamBaseNS(), &d->statusCode, &d->statusString);
196
void Task::setError(int code, const QString &str)
200
d->statusCode = code;
201
d->statusString = str;
208
if(d->done || d->insig)
212
if(d->deleteme || d->autoDelete)
220
SafeDelete::deleteSingle(this);
223
void Task::clientDisconnected()
228
void Task::debug(const char *fmt, ...)
233
str.vsprintf(fmt, ap);
238
void Task::debug(const QString &str)
240
client()->debug(QString("%1: ").arg(metaObject()->className()) + str);
245
* \brief verifiys a stanza is a IQ reply for this task
247
* it checks that the stanze is form the jid the request was send to and the id and the namespace (if given) match.
249
* it further checks that the sender jid is not empty (except if \a to is our server), it's not from
250
* our bare jid (except if send to one of our resources or our server)
251
* \param x the stanza to test
252
* \param to the Jid this task send a IQ to
253
* \param id the id of the send IQ
254
* \param xmlns the expected namespace if the reply (if non empty)
255
* \return true if it's a valid reply
258
bool Task::iqVerify(const QDomElement &x, const Jid &to, const QString &id, const QString &xmlns)
260
if(x.tagName() != "iq")
263
Jid from(x.attribute("from"));
264
Jid local = client()->jid();
265
Jid server = client()->host();
269
// allowed if we are querying the server
270
if(!to.isEmpty() && !to.compare(server))
274
else if(from.compare(local, false) || from.compare(local.domain(),false)) {
275
// allowed if we are querying ourself or the server
276
if(!to.isEmpty() && !to.compare(local, false) && !to.compare(server))
279
// from anywhere else?
281
if(!from.compare(to))
286
if(x.attribute("id") != id)
290
if(!xmlns.isEmpty()) {
291
if(queryNS(x) != xmlns)