summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/irc/libkirc/kirctransfer.h
blob: 3453f5cbb42aa1f6d71c7812aed81b29cb3409eb (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
    kirctransfer.h - DCC Handler

    Copyright (c) 2003      by Michel Hermier <[email protected]>

    Kopete    (c) 2003      by the Kopete developers <[email protected]>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

#ifndef KIRCTRANSFER_H
#define KIRCTRANSFER_H

#include <qdatastream.h>
#include <qfile.h>
#include <qhostaddress.h>
#include <qobject.h>
#include <qtextstream.h>

class KExtendedSocket;

class QFile;
class QTextCodec;

namespace KIRC
{
class Engine;

class Transfer
	: public QObject
{
	Q_OBJECT

public:
	enum Type {
		Unknown,
		Chat,
		FileOutgoing,
		FileIncoming
	};

	enum Status {
		Error_NoSocket	= -2,
		Error		= -1,
		Idle		= 0,
		HostLookup,
		Connecting,
		Connected,
		Closed
	};
public:
	Transfer(	KIRC::Engine *engine, QString nick,// QString nick_peer_adress
			Type type = Unknown,
			QObject *parent = 0L, const char *name = 0L );

	Transfer(	KIRC::Engine *engine, QString nick,// QString nick_peer_adress,
			QHostAddress peer_address, Q_UINT16 peer_port,
			Transfer::Type type,
			QObject *parent = 0L, const char *name = 0L );

	Transfer(	KIRC::Engine *engine, QString nick,// QString nick_peer_adress,
			Transfer::Type type,
			QString fileName, Q_UINT32 fileSize,
			QObject *parent = 0L, const char *name = 0L );

	Transfer(	KIRC::Engine *engine, QString nick,// QString nick_peer_adress,
			QHostAddress peer_address, Q_UINT16 peer_port,
			Transfer::Type type,
			QString fileName, Q_UINT32 fileSize,
			QObject *parent = 0L, const char *name = 0L );
/*
	For a file transfer properties are:

		KExntendedSocket	*socket
	or
		QHostAddress		peerAddress
		Q_UINT16		peerPort
	for determining the socket.

		QString			fileName
		Q_UINT32		fileSize
	for detemining the file propeties.
*//*
	Transfer(	KIRC *engine, QString nick,// QString nick_peer_adress,
			Transfer::Type type, QVariant properties,
			QObject *parent = 0L, const char *name = 0L );
*/
	~Transfer();

	KIRC::Engine *engine() const
		{ return m_engine; }
	QString nick() const
		{ return m_nick; }
	Type type() const
		{ return m_type; }
	Status status() const;

	/* Start the transfer.
	 * If not connected connect to client.
	 * Allow receiving/emitting data.
	 */
	bool initiate();

	QString fileName() const
		{ return m_fileName; }
	/* Change the file name.
	 */
	void setFileName(QString fileName)
		{ m_fileName = fileName; }
	unsigned long fileSize() const
		{ return m_fileSize; }
public slots:
	bool setSocket( KExtendedSocket *socket );
	void closeSocket();

	void setCodec( QTextCodec *codec );
	void writeLine( const QString &msg );

	void flush();

	void userAbort(QString);

signals:
	void readLine( const QString &msg );

	void fileSizeCurrent( unsigned int );
	void fileSizeAcknowledge( unsigned int );

//	void received(Q_UINT32);
//	void sent(Q_UINT32);

	void abort(QString);

	/* Emited when the transfer is complete.
	 * Usually it means that the file transfer has successfully finished.
	 */
	void complete();

protected slots:
	void slotError(int);

	void readyReadLine();

	void readyReadFileIncoming();

	void writeFileOutgoing();
	void readyReadFileOutgoing();

protected:
//	void emitSignals();
	void checkFileTransferEnd( Q_UINT32 fileSizeAck );

	KIRC::Engine *	m_engine;
	QString		m_nick;

	Type		m_type;
	KExtendedSocket *m_socket;
	bool		m_initiated;

	// Text member data
	QTextStream	m_socket_textStream;
//	QTextCodec *	m_socket_codec;

	// File member data
	QFile		m_file;
	QString		m_fileName;
	Q_UINT32	m_fileSize;
	Q_UINT32 /*usize_t*/	m_fileSizeCur;
	Q_UINT32 /*usize_t*/	m_fileSizeAck;
	QDataStream	m_socketDataStream;
	char		m_buffer[1024];
	int		m_bufferLength;

	// Data transfer measures
	Q_UINT32	m_receivedBytes;
	Q_UINT32	m_receivedBytesLimit;

	Q_UINT32	m_sentBytes;
	Q_UINT32	m_sentBytesLimit;
};

}

#endif