summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/libiris/iris/jabber/xmpp_ibb.h
blob: 15e1f2851f6cec46f0c54fe572ac88b23fe725a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * ibb.h - Inband bytestream
 * Copyright (C) 2001, 2002  Justin Karneges
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1307  USA
 *
 */

#ifndef JABBER_IBB_H
#define JABBER_IBB_H

#include<qobject.h>
#include<qdom.h>
#include<qstring.h>
#include<qptrlist.h>
#include"bytestream.h"
#include"im.h"

namespace XMPP
{
	class Client;
	class IBBManager;

	// this is an IBB connection.  use it much like a qsocket
	class IBBConnection : public ByteStream
	{
		Q_OBJECT
	public:
		enum { ErrRequest, ErrData };
		enum { Idle, Requesting, WaitingForAccept, Active };
		IBBConnection(IBBManager *);
		~IBBConnection();

		void connectToJid(const Jid &peer, const QDomElement &comment);
		void accept();
		void close();

		int state() const;
		Jid peer() const;
		QString streamid() const;
		QDomElement comment() const;

		bool isOpen() const;
		void write(const QByteArray &);
		QByteArray read(int bytes=0);
		int bytesAvailable() const;
		int bytesToWrite() const;

	signals:
		void connected();

	private slots:
		void ibb_finished();
		void trySend();

	private:
		class Private;
		Private *d;

		void reset(bool clear=false);

		friend class IBBManager;
		void waitForAccept(const Jid &peer, const QString &sid, const QDomElement &comment, const QString &iq_id);
		void takeIncomingData(const QByteArray &, bool close);
	};

	typedef QPtrList<IBBConnection> IBBConnectionList;
	typedef QPtrListIterator<IBBConnection> IBBConnectionListIt;
	class IBBManager : public QObject
	{
		Q_OBJECT
	public:
		IBBManager(Client *);
		~IBBManager();

		Client *client() const;

		IBBConnection *takeIncoming();

	signals:
		void incomingReady();

	private slots:
		void ibb_incomingRequest(const Jid &from, const QString &id, const QDomElement &);
		void ibb_incomingData(const Jid &from, const QString &streamid, const QString &id, const QByteArray &data, bool close);

	private:
		class Private;
		Private *d;

		QString genKey() const;

		friend class IBBConnection;
		IBBConnection *findConnection(const QString &sid, const Jid &peer="") const;
		QString genUniqueKey() const;
		void link(IBBConnection *);
		void unlink(IBBConnection *);
		void doAccept(IBBConnection *c, const QString &id);
		void doReject(IBBConnection *c, const QString &id, int, const QString &);
	};

	class JT_IBB : public Task
	{
		Q_OBJECT
	public:
		enum { ModeRequest, ModeSendData };
		JT_IBB(Task *, bool serve=false);
		~JT_IBB();

		void request(const Jid &, const QDomElement &comment);
		void sendData(const Jid &, const QString &streamid, const QByteArray &data, bool close);
		void respondSuccess(const Jid &, const QString &id, const QString &streamid);
		void respondError(const Jid &, const QString &id, int code, const QString &str);
		void respondAck(const Jid &to, const QString &id);

		void onGo();
		bool take(const QDomElement &);

		QString streamid() const;
		Jid jid() const;
		int mode() const;

	signals:
		void incomingRequest(const Jid &from, const QString &id, const QDomElement &);
		void incomingData(const Jid &from, const QString &streamid, const QString &id, const QByteArray &data, bool close);

	private:
		class Private;
		Private *d;
	};
}

#endif