summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/connection.cpp
blob: 16757785177af99f3ce1824684f8422646fd5bd7 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
	Kopete Oscar Protocol
	connection.cpp - independent protocol encapsulation

	Copyright (c) 2004-2005 by Matt Rogers <mattr@kde.org>

	Based on code Copyright (c) 2004 SuSE Linux AG <http://www.suse.com>
	Based on Iris, Copyright (C) 2003  Justin Karneges

	Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>

	*************************************************************************
	*                                                                       *
	* 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 of the License, or (at your option) any later version.      *
	*                                                                       *
	*************************************************************************
*/

#include "connection.h"
#include "client.h"
#include "connector.h"
#include "oscarclientstream.h"
#include "rateclassmanager.h"
#include "task.h"
#include "transfer.h"
#include <tdeapplication.h>
#include <kdebug.h>

#include "oscartypeclasses.h"


class ConnectionPrivate
{
public:
	DWORD snacSequence;
	WORD flapSequence;

	TQValueList<int> familyList;
	RateClassManager* rateClassManager;

	ClientStream* clientStream;
	Connector* connector;
	Client* client;

	Task* root;
};



Connection::Connection( Connector* connector, ClientStream* cs, const char* name )
: TQObject( 0, name )
{
	d = new ConnectionPrivate();
	d->clientStream = cs;
	d->client = 0;
	d->connector = connector;
	d->rateClassManager = new RateClassManager( this );
	d->root = new Task( this, true /* isRoot */ );
	m_loggedIn = false;
	initSequence();

}

Connection::~Connection()
{

	delete d->rateClassManager;
	delete d->clientStream;
	delete d->connector;
	delete d->root;
	delete d;
}

void Connection::setClient( Client* c )
{
	d->client = c;
	connect( c, TQT_SIGNAL( loggedIn() ), this, TQT_SLOT( loggedIn() ) );
}

void Connection::connectToServer( const TQString& host, bool auth )
{
	connect( d->clientStream, TQT_SIGNAL( error( int ) ), this, TQT_SLOT( streamSocketError( int ) ) );
	connect( d->clientStream, TQT_SIGNAL( readyRead() ), this, TQT_SLOT( streamReadyRead() ) );
	connect( d->clientStream, TQT_SIGNAL( connected() ), this, TQT_SIGNAL( connected() ) );
	d->clientStream->connectToServer( host, auth );
}

void Connection::close()
{
	d->clientStream->close();
	reset();
}

bool Connection::isSupported( int family ) const
{
	return ( d->familyList.findIndex( family ) != -1 );
}

TQValueList<int> Connection::supportedFamilies() const
{
	return d->familyList;
}

void Connection::addToSupportedFamilies( const TQValueList<int>& familyList )
{
	d->familyList += familyList;
}

void Connection::addToSupportedFamilies( int family )
{
	d->familyList.append( family );
}

void Connection::taskError( const Oscar::SNAC& s, int errCode )
{
	d->client->notifyTaskError( s, errCode, false /*fatal*/ );
}

void Connection::fatalTaskError( const Oscar::SNAC& s, int errCode )
{
	d->client->notifyTaskError( s, errCode, true /* fatal */ );
}

Oscar::Settings* Connection::settings() const
{
	return d->client->clientSettings();
}

TQ_UINT16 Connection::flapSequence()
{
	d->flapSequence++;
	if ( d->flapSequence >= 0x8000 ) //the max flap sequence is 0x8000 ( HEX )
		d->flapSequence = 1;

	return d->flapSequence;
}

TQ_UINT32 Connection::snacSequence()
{
	d->snacSequence++;
	return d->snacSequence;
}

TQString Connection::userId() const
{
	return d->client->userId();
}

TQString Connection::password() const
{
	return d->client->password();
}

bool Connection::isIcq() const
{
	return d->client->isIcq();
}

Task* Connection::rootTask() const
{
	return d->root;
}

SSIManager* Connection::ssiManager() const
{
	return d->client->ssiManager();
}

const Oscar::ClientVersion* Connection::version() const
{
	return d->client->version();
}

bool Connection::isLoggedIn() const
{
	return m_loggedIn;
}

RateClassManager* Connection::rateManager() const
{
	return d->rateClassManager;
}

void Connection::send( Transfer* request ) const
{
	if( !d->clientStream )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "No stream to write on!" << endl;
		return;
	}
	d->rateClassManager->queue( request );

}

void Connection::forcedSend( Transfer* request ) const
{
	if ( !d->clientStream )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "No stream to write on" << endl;
		return;
	}
	d->clientStream->write( request );
}

void Connection::initSequence()
{
	d->snacSequence = ( TDEApplication::random() & 0xFFFF );
	d->flapSequence = ( TDEApplication::random() & 0xFFFF );
}

void Connection::distribute( Transfer * transfer ) const
{
	//d->rateClassManager->recalcRateLevels();
	if( !rootTask()->take( transfer ) )
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "root task refused transfer" << endl;

	delete transfer;
}

void Connection::reset()
{
	//clear the family list
	d->familyList.clear();
	d->rateClassManager->reset();
}

void Connection::streamReadyRead()
{
	// take the incoming transfer and distribute it to the task tree
	Transfer * transfer = d->clientStream->read();
	distribute( transfer );
}

void Connection::loggedIn()
{
	m_loggedIn = true;
}

void Connection::streamSocketError( int code )
{
	emit socketError( code, d->clientStream->errorText() );
}

#include "connection.moc"
//kate: tab-width 4; indent-mode csands;