summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/irc/libkirc/kirctransferserver.cpp
blob: ee9e38c1104ad24d23ba387dc37c78e42773f713 (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
/*
    kirctransfer.cpp - IRC transfer.

    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.                                   *
    *                                                                       *
    *************************************************************************
*/

#include <kdebug.h>
#include <kextsock.h>

#include "kirctransferhandler.h"

#include "kirctransferserver.h"

using namespace KIRC;

/*
TransferServer::TransferServer( TQObject *tqparent, const char *name )
	: TQObject( tqparent, name ),
	  m_socket( 0 ),
	  m_port( 0 ),
	  m_backlog( 1 )
{
}
*/
TransferServer::TransferServer(TQ_UINT16 port, int backlog, TQObject *tqparent, const char *name)
	: TQObject( tqparent, name ),
	  m_socket( 0 ),
	  m_port( port ),
	  m_backlog( backlog )
{
}

TransferServer::TransferServer(Engine *engine, TQString nick,// TQString nick_peer_adress,
			Transfer::Type type,
			TQString fileName, TQ_UINT32 fileSize,
			TQObject *tqparent, const char *name)
	: TQObject( tqparent, name ),
	  m_socket(0),
	  m_port(0),
	  m_backlog(1),
	  m_engine(engine),
	  m_nick(nick),
	  m_type(type),
	  m_fileName(fileName),
	  m_fileSize(fileSize)
{
	initServer();
}

TransferServer::~TransferServer()
{
	if (m_socket)
		delete m_socket;
}

bool TransferServer::initServer()
{
	if (!m_socket)
	{
		TQObject::connect(this, TQT_SIGNAL(incomingNewTransfer(Transfer *)),
				TransferHandler::self(), TQT_SIGNAL(transferCreated(Transfer *)));

		m_socket = new KExtendedSocket();

//		m_socket->setHost(m_socket->localAddress()->nodeName());
		if (!m_socket->setPort(m_port))
			kdDebug(14120) << k_funcinfo << "Failed to set port to" << m_port << endl;
		m_socket->setSocketFlags(KExtendedSocket::noResolve
					|KExtendedSocket::passiveSocket
					|KExtendedSocket::inetSocket );

		if (!m_socket->setTimeout(2*60)) // FIXME: allow configuration of this.
			kdDebug(14120) << k_funcinfo << "Failed to set timeout." << endl;

		TQObject::connect(m_socket, TQT_SIGNAL(readyAccept()),
				this, TQT_SLOT(readyAccept()));
		TQObject::connect(m_socket, TQT_SIGNAL(connectionFailed(int)),
				this, TQT_SLOT(connectionFailed(int)));

		m_socket->listen(m_backlog);
		m_socket->setBlockingMode(true);

		const KInetSocketAddress *localAddress = static_cast<const KInetSocketAddress *>(m_socket->localAddress());
		if (!localAddress)
		{
			kdDebug(14120) << k_funcinfo << "Not a KInetSocketAddress." << endl;
			deleteLater();
			return false;
		}

		m_port = localAddress->port();
	}
	return (m_socket->socketStatus() != KExtendedSocket::error);
}

bool TransferServer::initServer( TQ_UINT16 port, int backlog )
{
	if (m_socket)
	{
		m_port = port;
		m_backlog = backlog;
	}
	return initServer();
}

void TransferServer::readyAccept()
{
	KExtendedSocket *socket;
	m_socket->accept( socket );
	Transfer *transfer = new Transfer(m_engine, m_nick, m_type, m_fileName, m_fileSize);
	transfer->setSocket(socket);
	transfer->initiate();
	emit incomingNewTransfer(transfer);
}

void TransferServer::connectionFailed(int error)
{
	if (error!=0)
	{
		kdDebug(14120) << k_funcinfo << "Connection failed with " << m_nick << endl;
		deleteLater();
	}
}
/*
void Transfer::initClient()
{
	if(!m_socket)
	{
		connect(m_socket, TQT_SIGNAL(connectionClosed()),
			this, TQT_SLOT(slotConnectionClosed()));
		connect(m_socket, TQT_SIGNAL(delayedCloseFinished()),
			this, TQT_SLOT(slotConnectionClosed()));
		connect(m_socket, TQT_SIGNAL(error(int)),
			this, TQT_SLOT(slotError(int)));
		connect(m_socket, TQT_SIGNAL(readyRead()),
			this, TQT_SLOT(readyReadFileOut));

		m_socket->enableRead( true );
		m_socket->enableWrite( true );
	}
}
*/
#include "kirctransferserver.moc"