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

« back to all changes in this revision

Viewing changes to src/jabstream.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2002-04-19 02:28:44 UTC
  • Revision ID: james.westby@ubuntu.com-20020419022844-za7xgai5qyfd9xv6
Tags: upstream-0.8.5
ImportĀ upstreamĀ versionĀ 0.8.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
** jabstream.h - handles a Jabber XML stream
 
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
#ifndef JABSTREAM_H
 
22
#define JABSTREAM_H
 
23
 
 
24
#include<qxml.h>
 
25
#include<qdom.h>
 
26
#include<qobject.h>
 
27
#include<qstring.h>
 
28
#include<qtimer.h>
 
29
#include<qptrstack.h>
 
30
#include<qptrqueue.h>
 
31
#include<qsocket.h>
 
32
#include"sslfilter.h"
 
33
 
 
34
 
 
35
#define JABSTREAM_ERR_DNS         0
 
36
#define JABSTREAM_ERR_CONNREFUSED 1
 
37
#define JABSTREAM_ERR_CONNTIMEOUT 2
 
38
#define JABSTREAM_ERR_DISC        3
 
39
#define JABSTREAM_ERR_SOCKET      4
 
40
#define JABSTREAM_ERR_HANDSHAKE   5
 
41
 
 
42
 
 
43
/****************************************************************************
 
44
  JabStream
 
45
 
 
46
  This class handles a Jabber XML stream.  It collects XML data into
 
47
  QDomElements and signals the chunks back to the caller.  There is no
 
48
  actual Jabber logic in this class.  It simply takes care of XML and
 
49
  a socket connection.
 
50
 
 
51
  HOWTO:
 
52
 
 
53
    JabStream stream;
 
54
    stream.connectToHost("jabber.org", 5222");
 
55
 
 
56
  The class communicates back to you via signals:
 
57
 
 
58
    connected()                       - connection success
 
59
    error(int)                        - connection/stream error
 
60
    packetReady(const QDomElement &)  - new XML chunk ready
 
61
 
 
62
****************************************************************************/
 
63
 
 
64
class JabXmlHandler : public QObject, public QXmlDefaultHandler
 
65
{
 
66
        Q_OBJECT
 
67
public:
 
68
        JabXmlHandler(QDomDocument *);
 
69
 
 
70
        // Xml functions (reimplemented)
 
71
        bool startDocument();
 
72
        bool startElement(const QString &, const QString &, const QString &, const QXmlAttributes &);
 
73
        bool endElement(const QString &, const QString &, const QString &);
 
74
        bool characters(const QString &);
 
75
 
 
76
        QString toLower(QString s);
 
77
 
 
78
signals:
 
79
        void packetReady(const QDomElement &);
 
80
        void handshake(bool, const QString &);
 
81
 
 
82
private:
 
83
        QString indent, characterData;
 
84
        int depth;
 
85
        QDomDocument *doc;
 
86
        QDomElement chunk, current;
 
87
};
 
88
 
 
89
 
 
90
class JabStream : public QObject
 
91
{
 
92
        Q_OBJECT
 
93
public:
 
94
        JabStream();
 
95
        ~JabStream();
 
96
 
 
97
        void connectToHost(const QString &host, int port);
 
98
        void disc();
 
99
        bool isConnected() { return v_isHandShaken; }
 
100
        QString id() { return v_id; }
 
101
 
 
102
        void setNoop(int);
 
103
 
 
104
        bool isSSLSupported();
 
105
        bool isSSLEnabled() { return use_ssl; }
 
106
        void setSSLEnabled(bool);
 
107
 
 
108
        static QCString encodeXML(QString);
 
109
        static QCString elemToString(const QDomElement &);
 
110
 
 
111
signals:
 
112
        void connected();
 
113
        void error(int);
 
114
        void receivePacket(const QDomElement &);
 
115
 
 
116
public slots:
 
117
        void sendPacket(const QDomElement &);
 
118
        void sendString(const QCString &);
 
119
 
 
120
private slots:
 
121
        // QSocket slots
 
122
        void sock_connected();
 
123
        void sock_disconnected();
 
124
        void sock_readyRead();
 
125
        void sock_error(int);
 
126
 
 
127
        // SSLFilter
 
128
        void ssl_outgoingReady();
 
129
        void ssl_readyRead();
 
130
        void ssl_error();
 
131
 
 
132
        // delayed functions so we can "get out" of a QSocket slot before continuing
 
133
        void delayedProcessError();
 
134
        void delayedProcessReceived();
 
135
        void delayedProcessHandShake();
 
136
 
 
137
        // prevent NAT/connection timeouts
 
138
        void doNoop();
 
139
 
 
140
        // JabXmlHandler slots
 
141
        void packetReady(const QDomElement &);
 
142
        void handshake(bool, const QString &);
 
143
 
 
144
private:
 
145
        QSocket *sock;
 
146
        SSLFilter *ssl;
 
147
        QDomDocument *doc;
 
148
        QXmlInputSource *src;
 
149
        QXmlSimpleReader *reader;
 
150
        JabXmlHandler *handler;
 
151
 
 
152
        QTimer *t;
 
153
        bool v_isConnected, v_isHandShaken, first_time;
 
154
        QString host;
 
155
        int port;
 
156
        bool use_ssl;
 
157
 
 
158
        int errType;
 
159
        int noop_time;
 
160
 
 
161
        QString v_id;
 
162
 
 
163
        QPtrQueue<QDomElement> in;
 
164
 
 
165
        void processIncomingData(const QByteArray &);
 
166
};
 
167
 
 
168
#endif