~ubuntu-branches/ubuntu/utopic/kadu/utopic

« back to all changes in this revision

Viewing changes to plugins/jabber_protocol/3rdparty/libiris-win/src/xmpp/xmpp-im/xmpp_task.cpp

  • Committer: Package Import Robot
  • Author(s): Patryk Cisek
  • Date: 2014-10-02 11:55:19 UTC
  • mfrom: (0.48.1) (0.44.2) (43.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20141002115519-sxn21g16t596llc0
Tags: 1.0-2
* Using dpkg-buildflags to set compilation and linker flags.
* Added 03-no-CMAKE_CXX_FLAGS-overwrite.patch to make it possible to set
  compilation flags.
* Removed lintian override for RPATH in Kadu's plugins.
* Using chrpath to strip Kadu's plugins from RPATH, since they're not
  needed there.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003  Justin Karneges
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 *
 
18
 */
 
19
 
 
20
#include <QTimer>
 
21
 
 
22
#include "safedelete.h"
 
23
#include "xmpp_task.h"
 
24
#include "xmpp_client.h"
 
25
#include "xmpp_xmlcommon.h"
 
26
 
 
27
using namespace XMPP;
 
28
 
 
29
class Task::TaskPrivate
 
30
{
 
31
public:
 
32
        TaskPrivate() {}
 
33
 
 
34
        QString id;
 
35
        bool success;
 
36
        int statusCode;
 
37
        QString statusString;
 
38
        Client *client;
 
39
        bool insig, deleteme, autoDelete;
 
40
        bool done;
 
41
};
 
42
 
 
43
Task::Task(Task *parent)
 
44
:QObject(parent)
 
45
{
 
46
        init();
 
47
 
 
48
        d->client = parent->client();
 
49
        d->id = client()->genUniqueId();
 
50
        connect(d->client, SIGNAL(disconnected()), SLOT(clientDisconnected()));
 
51
}
 
52
 
 
53
Task::Task(Client *parent, bool)
 
54
:QObject(0)
 
55
{
 
56
        init();
 
57
 
 
58
        d->client = parent;
 
59
        connect(d->client, SIGNAL(disconnected()), SLOT(clientDisconnected()));
 
60
}
 
61
 
 
62
Task::~Task()
 
63
{
 
64
        delete d;
 
65
}
 
66
 
 
67
void Task::init()
 
68
{
 
69
        d = new TaskPrivate;
 
70
        d->success = false;
 
71
        d->insig = false;
 
72
        d->deleteme = false;
 
73
        d->autoDelete = false;
 
74
        d->done = false;
 
75
}
 
76
 
 
77
Task *Task::parent() const
 
78
{
 
79
        return (Task *)QObject::parent();
 
80
}
 
81
 
 
82
Client *Task::client() const
 
83
{
 
84
        return d->client;
 
85
}
 
86
 
 
87
QDomDocument *Task::doc() const
 
88
{
 
89
        return client()->doc();
 
90
}
 
91
 
 
92
QString Task::id() const
 
93
{
 
94
        return d->id;
 
95
}
 
96
 
 
97
bool Task::success() const
 
98
{
 
99
        return d->success;
 
100
}
 
101
 
 
102
int Task::statusCode() const
 
103
{
 
104
        return d->statusCode;
 
105
}
 
106
 
 
107
const QString & Task::statusString() const
 
108
{
 
109
        return d->statusString;
 
110
}
 
111
 
 
112
void Task::go(bool autoDelete)
 
113
{
 
114
        d->autoDelete = autoDelete;
 
115
 
 
116
        if (!client() || !&client()->stream()) {
 
117
                qWarning("Task::go(): attempted to send a task over the broken connection.");
 
118
                if (autoDelete) {
 
119
                        deleteLater();
 
120
                }
 
121
        }
 
122
        else {
 
123
                onGo();
 
124
        }
 
125
}
 
126
 
 
127
bool Task::take(const QDomElement &x)
 
128
{
 
129
        const QObjectList p = children();
 
130
 
 
131
        // pass along the xml
 
132
        Task *t;
 
133
        for(QObjectList::ConstIterator it = p.begin(); it != p.end(); ++it) {
 
134
                QObject *obj = *it;
 
135
                if(!obj->inherits("XMPP::Task"))
 
136
                        continue;
 
137
 
 
138
                t = static_cast<Task*>(obj);
 
139
                if(t->take(x))
 
140
                        return true;
 
141
        }
 
142
 
 
143
        return false;
 
144
}
 
145
 
 
146
void Task::safeDelete()
 
147
{
 
148
        if(d->deleteme)
 
149
                return;
 
150
 
 
151
        d->deleteme = true;
 
152
        if(!d->insig)
 
153
                SafeDelete::deleteSingle(this);
 
154
}
 
155
 
 
156
void Task::onGo()
 
157
{
 
158
}
 
159
 
 
160
void Task::onDisconnect()
 
161
{
 
162
        if(!d->done) {
 
163
                d->success = false;
 
164
                d->statusCode = ErrDisc;
 
165
                d->statusString = tr("Disconnected");
 
166
 
 
167
                // delay this so that tasks that react don't block the shutdown
 
168
                QTimer::singleShot(0, this, SLOT(done()));
 
169
        }
 
170
}
 
171
 
 
172
void Task::send(const QDomElement &x)
 
173
{
 
174
        client()->send(x);
 
175
}
 
176
 
 
177
void Task::setSuccess(int code, const QString &str)
 
178
{
 
179
        if(!d->done) {
 
180
                d->success = true;
 
181
                d->statusCode = code;
 
182
                d->statusString = str;
 
183
                done();
 
184
        }
 
185
}
 
186
 
 
187
void Task::setError(const QDomElement &e)
 
188
{
 
189
        if(!d->done) {
 
190
                d->success = false;
 
191
                getErrorFromElement(e, d->client->streamBaseNS(), &d->statusCode, &d->statusString);
 
192
                done();
 
193
        }
 
194
}
 
195
 
 
196
void Task::setError(int code, const QString &str)
 
197
{
 
198
        if(!d->done) {
 
199
                d->success = false;
 
200
                d->statusCode = code;
 
201
                d->statusString = str;
 
202
                done();
 
203
        }
 
204
}
 
205
 
 
206
void Task::done()
 
207
{
 
208
        if(d->done || d->insig)
 
209
                return;
 
210
        d->done = true;
 
211
 
 
212
        if(d->deleteme || d->autoDelete)
 
213
                d->deleteme = true;
 
214
 
 
215
        d->insig = true;
 
216
        finished();
 
217
        d->insig = false;
 
218
 
 
219
        if(d->deleteme)
 
220
                SafeDelete::deleteSingle(this);
 
221
}
 
222
 
 
223
void Task::clientDisconnected()
 
224
{
 
225
        onDisconnect();
 
226
}
 
227
 
 
228
void Task::debug(const char *fmt, ...)
 
229
{
 
230
        va_list ap;
 
231
        va_start(ap, fmt);
 
232
        QString str;
 
233
        str.vsprintf(fmt, ap);
 
234
        va_end(ap);
 
235
        debug(str);
 
236
}
 
237
 
 
238
void Task::debug(const QString &str)
 
239
{
 
240
        client()->debug(QString("%1: ").arg(metaObject()->className()) + str);
 
241
}
 
242
 
 
243
 
 
244
/**
 
245
 * \brief verifiys a stanza is a IQ reply for this task
 
246
 *
 
247
 * it checks that the stanze is form the jid the request was send to and the id and the namespace (if given) match.
 
248
 *
 
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
 
256
*/
 
257
 
 
258
bool Task::iqVerify(const QDomElement &x, const Jid &to, const QString &id, const QString &xmlns)
 
259
{
 
260
        if(x.tagName() != "iq")
 
261
                return false;
 
262
 
 
263
        Jid from(x.attribute("from"));
 
264
        Jid local = client()->jid();
 
265
        Jid server = client()->host();
 
266
 
 
267
        // empty 'from' ?
 
268
        if(from.isEmpty()) {
 
269
                // allowed if we are querying the server
 
270
                if(!to.isEmpty() && !to.compare(server))
 
271
                        return false;
 
272
        }
 
273
        // from ourself?
 
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))
 
277
                        return false;
 
278
        }
 
279
        // from anywhere else?
 
280
        else {
 
281
                if(!from.compare(to))
 
282
                        return false;
 
283
        }
 
284
 
 
285
        if(!id.isEmpty()) {
 
286
                if(x.attribute("id") != id)
 
287
                        return false;
 
288
        }
 
289
 
 
290
        if(!xmlns.isEmpty()) {
 
291
                if(queryNS(x) != xmlns)
 
292
                        return false;
 
293
        }
 
294
 
 
295
        return true;
 
296
}
 
297