~ubuntu-branches/ubuntu/edgy/psi/edgy

« back to all changes in this revision

Viewing changes to src/jabio.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2004-06-15 00:10:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040615001041-enywb6pcpe4sjsw6
Tags: 0.9.2-1
* New upstream release
* Set KDEDIR for ./configure so kde specific files get installed
* Don't install libpsiwidgets.so. It got installed in /usr/share
  where it doesn't belong. May be included (at a better location)
  later.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
** jabio.cpp - handles JabTasks
3
 
** Copyright (C) 2001, 2002  Justin Karneges
4
 
**
5
 
** This program is free software; you can redistribute it and/or
6
 
** modify it under the terms of the GNU General Public License
7
 
** as published by the Free Software Foundation; either version 2
8
 
** of the License, or (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 Free Software
17
 
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 
**
19
 
****************************************************************************/
20
 
 
21
 
#include"jabio.h"
22
 
#include"common.h"
23
 
 
24
 
 
25
 
/****************************************************************************
26
 
  JabTask
27
 
****************************************************************************/
28
 
JabTask::JabTask(JabTask *_parent)
29
 
:QObject(_parent)
30
 
{
31
 
        v_parent = _parent;
32
 
        v_io = parent()->io();
33
 
        v_isRoot = FALSE;
34
 
        v_isDone = FALSE;
35
 
 
36
 
        v_id = io()->getId();
37
 
}
38
 
 
39
 
JabTask::JabTask(JabIO *_parent)
40
 
:QObject(_parent)
41
 
{
42
 
        v_parent = 0;
43
 
        v_io = _parent;
44
 
        v_isRoot = TRUE;
45
 
        v_isDone = FALSE;
46
 
}
47
 
 
48
 
JabTask *JabTask::parent()
49
 
{
50
 
        return v_parent;
51
 
}
52
 
 
53
 
QDomDocument *JabTask::doc()
54
 
{
55
 
        return &io()->doc;
56
 
}
57
 
 
58
 
JabTask *JabTask::find(const char *s)
59
 
{
60
 
        const QObjectList *p = children();
61
 
        if(!p)
62
 
                return 0;
63
 
 
64
 
        QObjectListIt it(*p);
65
 
        JabTask *j;
66
 
        for(; it.current(); ++it) {
67
 
                j = static_cast<JabTask*>(it.current());
68
 
                if(j->isA(s))
69
 
                        return j;
70
 
        }
71
 
 
72
 
        return 0;
73
 
}
74
 
 
75
 
JabTask *JabTask::findById(const QString &s)
76
 
{
77
 
        const QObjectList *p = children();
78
 
        if(!p)
79
 
                return 0;
80
 
 
81
 
        QObjectListIt it(*p);
82
 
        JabTask *j;
83
 
        for(; it.current(); ++it) {
84
 
                j = static_cast<JabTask*>(it.current());
85
 
                if(j->id() == s)
86
 
                        return j;
87
 
        }
88
 
 
89
 
        return 0;
90
 
}
91
 
 
92
 
bool JabTask::stamp2TS(const QString &ts, QDateTime *d)
93
 
{
94
 
        if(ts.length() != 17)
95
 
                return FALSE;
96
 
 
97
 
        int year  = ts.mid(0,4).toInt();
98
 
        int month = ts.mid(4,2).toInt();
99
 
        int day   = ts.mid(6,2).toInt();
100
 
 
101
 
        int hour  = ts.mid(9,2).toInt();
102
 
        int min   = ts.mid(12,2).toInt();
103
 
        int sec   = ts.mid(15,2).toInt();
104
 
 
105
 
        QDate xd;
106
 
        xd.setYMD(year, month, day);
107
 
        if(!xd.isValid())
108
 
                return FALSE;
109
 
 
110
 
        QTime xt;
111
 
        xt.setHMS(hour, min, sec);
112
 
        if(!xt.isValid())
113
 
                return FALSE;
114
 
 
115
 
        d->setDate(xd);
116
 
        d->setTime(xt);
117
 
 
118
 
        return TRUE;
119
 
}
120
 
 
121
 
QString JabTask::TS2stamp(const QDateTime &d)
122
 
{
123
 
        QString str;
124
 
 
125
 
        str.sprintf("%04d%02d%02dT%02d:%02d:%02d",
126
 
                d.date().year(),
127
 
                d.date().month(),
128
 
                d.date().day(),
129
 
                d.time().hour(),
130
 
                d.time().minute(),
131
 
                d.time().second());
132
 
 
133
 
        return str;
134
 
}
135
 
 
136
 
QDomElement JabTask::textTag(const QString &name, const QString &content)
137
 
{
138
 
        QDomElement tag = doc()->createElement(name);
139
 
        QDomText text = doc()->createTextNode(content);
140
 
        tag.appendChild(text);
141
 
 
142
 
        return tag;
143
 
}
144
 
 
145
 
QString JabTask::tagContent(const QDomElement &e)
146
 
{
147
 
        // look for some tag content
148
 
        for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
149
 
                QDomText i = n.toText();
150
 
                if(i.isNull())
151
 
                        continue;
152
 
                return i.data();
153
 
        }
154
 
 
155
 
        return "";
156
 
}
157
 
 
158
 
QDomElement JabTask::findSubTag(const QDomElement &e, const QString &name, bool *found)
159
 
{
160
 
        if(found)
161
 
                *found = FALSE;
162
 
 
163
 
        for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
164
 
                QDomElement i = n.toElement();
165
 
                if(i.isNull())
166
 
                        continue;
167
 
                if(i.tagName() == name) {
168
 
                        if(found)
169
 
                                *found = TRUE;
170
 
                        return i;
171
 
                }
172
 
        }
173
 
 
174
 
        QDomElement tmp;
175
 
        return tmp;
176
 
}
177
 
 
178
 
QDomElement JabTask::createIQ(const QString &type, const QString &to, const QString &id)
179
 
{
180
 
        QDomElement iq = doc()->createElement("iq");
181
 
        if(!type.isEmpty())
182
 
                iq.setAttribute("type", type);
183
 
        if(!to.isEmpty())
184
 
                iq.setAttribute("to", to);
185
 
        if(!id.isEmpty())
186
 
                iq.setAttribute("id", id);
187
 
 
188
 
        return iq;
189
 
}
190
 
 
191
 
QDomElement JabTask::queryTag(const QDomElement &e)
192
 
{
193
 
        bool found;
194
 
        QDomElement q = findSubTag(e, "query", &found);
195
 
        return q;
196
 
}
197
 
 
198
 
QString JabTask::queryNS(const QDomElement &e)
199
 
{
200
 
        bool found;
201
 
        QDomElement q = findSubTag(e, "query", &found);
202
 
        if(found)
203
 
                return q.attribute("xmlns");
204
 
 
205
 
        return "";
206
 
}
207
 
 
208
 
JabRoster JabTask::xmlReadRoster(const QDomElement &q, bool push)
209
 
{
210
 
        JabRoster r;
211
 
 
212
 
        for(QDomNode n = q.firstChild(); !n.isNull(); n = n.nextSibling()) {
213
 
                QDomElement i = n.toElement();
214
 
                if(i.isNull())
215
 
                        continue;
216
 
 
217
 
                if(i.tagName() == "item") {
218
 
                        JabRosterEntry entry;
219
 
                        bool found;
220
 
 
221
 
                        entry.jid = i.attribute("jid");
222
 
                        entry.sub = i.attribute("subscription");
223
 
                        entry.nick = i.attribute("name");
224
 
 
225
 
                        // ask?
226
 
                        found = i.hasAttribute("ask");
227
 
                        if(found && i.attribute("ask") == "subscribe")
228
 
                                entry.ask = 1;
229
 
                        else
230
 
                                entry.ask = 0;
231
 
 
232
 
                        if(push)
233
 
                                entry.push = TRUE;
234
 
 
235
 
                        // grab the groups
236
 
                        for(QDomNode n = i.firstChild(); !n.isNull(); n = n.nextSibling()) {
237
 
                                QDomElement tag = n.toElement();
238
 
                                if(tag.isNull())
239
 
                                        continue;
240
 
 
241
 
                                if(tag.tagName() == "group")
242
 
                                        entry.groups.append(tagContent(tag));
243
 
                        }
244
 
 
245
 
                        JabRosterEntry *tmp = new JabRosterEntry(entry);
246
 
                        r.add(tmp);
247
 
                }
248
 
        }
249
 
 
250
 
        return r;
251
 
}
252
 
 
253
 
QString JabTask::getErrorString(const QDomElement &e)
254
 
{
255
 
        bool found;
256
 
        QDomElement tag = findSubTag(e, "error", &found);
257
 
        if(!found)
258
 
                return "";
259
 
 
260
 
        return tagContent(tag);
261
 
}
262
 
 
263
 
void JabTask::done(JabTask *j)
264
 
{
265
 
        io()->doneJT(j);
266
 
        childFinished(j);
267
 
}
268
 
 
269
 
QString JabTask::toString()
270
 
{
271
 
        return "";
272
 
}
273
 
 
274
 
bool JabTask::fromString(const QString &)
275
 
{
276
 
        return FALSE;
277
 
}
278
 
 
279
 
void JabTask::setSuccess(bool b)
280
 
{
281
 
        v_success = b;
282
 
        v_isDone = TRUE;
283
 
        finished(this);
284
 
        JabTask *p = parent();
285
 
        if(p)
286
 
                p->done(this);
287
 
}
288
 
 
289
 
void JabTask::setError(const QString &s)
290
 
{
291
 
        v_errorString = s;
292
 
}
293
 
 
294
 
void JabTask::send(const QDomElement &x)
295
 
{
296
 
        io()->send(x);
297
 
}
298
 
 
299
 
bool JabTask::take(const QDomElement &x)
300
 
{
301
 
        const QObjectList *p = children();
302
 
        if(!p)
303
 
                return FALSE;
304
 
 
305
 
        // pass along the xml
306
 
        QObjectListIt it(*p);
307
 
        JabTask *j;
308
 
        for(; it.current(); ++it) {
309
 
                j = static_cast<JabTask*>(it.current());
310
 
                if(j->take(x))
311
 
                        return TRUE;
312
 
        }
313
 
 
314
 
        return FALSE;
315
 
}
316
 
 
317
 
void JabTask::childEvent(QChildEvent *e)
318
 
{
319
 
        if(e->inserted())
320
 
                childAdded((JabTask *)e->child());
321
 
        else if(e->removed())
322
 
                childRemoved((JabTask *)e->child());
323
 
}
324
 
 
325
 
 
326
 
/****************************************************************************
327
 
  JabIO
328
 
****************************************************************************/
329
 
JabIO::JabIO()
330
 
{
331
 
        id_seed = 0xabcd;
332
 
        v_root = new JabTask(this);
333
 
}
334
 
 
335
 
JabIO::~JabIO()
336
 
{
337
 
}
338
 
 
339
 
QString JabIO::getId()
340
 
{
341
 
        QString s;
342
 
        s.sprintf("a%x", id_seed);
343
 
        id_seed += 0x10;
344
 
        return s;
345
 
}
346
 
 
347
 
JabTask *JabIO::root()
348
 
{
349
 
        return v_root;
350
 
}
351
 
 
352
 
void JabIO::reset()
353
 
{
354
 
        clear();
355
 
}
356
 
 
357
 
void JabIO::clear()
358
 
{
359
 
        const QObjectList *p = root()->children();
360
 
        if(!p)
361
 
                return;
362
 
        QObjectListIt it(*p);
363
 
        for(; it.current(); ++it) {
364
 
                JabTask *j = (JabTask *)it.current();
365
 
                if(j->isClearable())
366
 
                        j->deleteLater();
367
 
        }
368
 
}
369
 
 
370
 
void JabIO::doneJT(JabTask *p)
371
 
{
372
 
        if(p->parent()->isRoot())
373
 
                done(p);
374
 
        else
375
 
                anyDone(p);
376
 
}
377
 
 
378
 
void JabIO::distribute(const QDomElement &x)
379
 
{
380
 
        if(!root()->take(x)) {
381
 
                //printf("JabIO: packet was ignored.\n");
382
 
        }
383
 
}
384
 
 
385
 
void JabIO::send(const QDomElement &x)
386
 
{
387
 
        QString out;
388
 
        QTextStream ts(&out, IO_WriteOnly);
389
 
        x.save(ts, 0);
390
 
 
391
 
        pdb(DEBUG_JABBER, QString("JabIO: outgoing: [\n%1]\n").arg(out));
392
 
        outgoingPacket(x);
393
 
}
394
 
 
395
 
void JabIO::incomingPacket(const QDomElement &x)
396
 
{
397
 
        QString out;
398
 
        QTextStream ts(&out, IO_WriteOnly);
399
 
        x.save(ts, 0);
400
 
 
401
 
        pdb(DEBUG_JABXML, QString("JabIO: incoming: [\n%1]\n").arg(out));
402
 
        distribute(x);
403
 
}
404
 
 
405
 
JabTask *JabIO::find(const char *s)
406
 
{
407
 
        return root()->find(s);
408
 
}
409
 
 
410
 
JabTask *JabIO::findById(const QString &s)
411
 
{
412
 
        return root()->findById(s);
413
 
}