summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/rateinfotask.cpp
blob: f19cf792956ecb965803fe6ca8cbb2bdf6946442 (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
/*
   Kopete Oscar Protocol
   rateinfotask.cpp - Fetch the rate class information

   Copyright (c) 2004 Matt Rogers <[email protected]>

   Kopete (c) 2002-2004 by the Kopete developers <[email protected]>

   *************************************************************************
   *                                                                       *
   * 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 "rateinfotask.h"

#include <qvaluelist.h>
#include <kdebug.h>
#include "rateclass.h"
#include "rateclassmanager.h"
#include "oscartypes.h"
#include "oscarutils.h"
#include "transfer.h"
#include "connection.h"

using namespace Oscar;

RateInfoTask::RateInfoTask( Task* parent )
		: Task( parent )
{
	connect( this, SIGNAL( gotRateLimits() ), this, SLOT( sendRateInfoAck() ) );
}


RateInfoTask::~RateInfoTask()
{
	
}


bool RateInfoTask::forMe( const Transfer* transfer ) const
{
	const SnacTransfer* st = dynamic_cast<const SnacTransfer*>( transfer );
	if ( st && st->snacService() == 1 && st->snacSubtype() == 7 )
		return true;
	else
		return false;
}

bool RateInfoTask::take( Transfer* transfer )
{
	if ( forMe( transfer ) )
	{
		setTransfer( transfer );
		handleRateInfoResponse();
		setTransfer( 0 );
		return true;
	}
	return false;
}

void RateInfoTask::onGo()
{
	sendRateInfoRequest();
}

void RateInfoTask::sendRateInfoRequest()
{
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "sending rate info request (SNAC 0x01, 0x06)" << endl;
	FLAP f = { 0x02, 0, 0 };
	SNAC s = { 0x0001, 0x0006, 0x0000, client()->snacSequence() };
	Buffer* buffer = new Buffer();
	Transfer* st = createTransfer( f, s, buffer );
	send( st );
}

void RateInfoTask::handleRateInfoResponse()
{
	QValueList<RateClass*> rates;
	Oscar::RateInfo ri;
	
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "handling rate info response (SNAC 0x01, 0x07)" << endl;
	Buffer* buffer = transfer()->buffer();
	
	int numClasses = buffer->getWord();
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Got " << numClasses << " rate classes" << endl;
	for ( int i = 0; i < numClasses; i++ )
	{
		RateClass* newClass = new RateClass( client()->rateManager() );
		//parse rate classes and put them somewhere
		ri.classId = buffer->getWord();
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Rate class: " << ri.classId << endl;
		//discard the rest (for right now)
		ri.windowSize = buffer->getDWord(); //window size
		ri.clearLevel = buffer->getDWord(); //clear level
		ri.alertLevel = buffer->getDWord(); //alert level
		ri.limitLevel = buffer->getDWord(); //limit level
		ri.disconnectLevel = buffer->getDWord(); //disconnect level
		ri.currentLevel = buffer->getDWord(); //current level
		ri.initialLevel = ri.currentLevel;
		ri.maxLevel = buffer->getDWord(); //max level
		ri.lastTime = buffer->getDWord(); //last time
		ri.currentState = buffer->getByte(); //current state
		
		newClass->setRateInfo( ri );
		rates.append( newClass );
	}

	int groupNum = 0;
	int numGroupPairs = 0;
	
	for ( int i = 0; i < numClasses; i++ )
	{
		groupNum = buffer->getWord();
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Adding snac members to group " << groupNum << endl;
		
		RateClass* rc = 0L;
		QValueList<RateClass*>::iterator it = rates.begin();
		for ( ; it != rates.end(); ++it )
		{
			if ( ( *it )->id() == groupNum )
			{
				rc = ( *it );
				break;
			}
		}
			
		m_rateGroups.append( groupNum );
		numGroupPairs = buffer->getWord();
		for ( int j = 0; j < numGroupPairs; j++ )
		{
			WORD family = buffer->getWord();
			WORD subtype = buffer->getWord();
			rc->addMember( family, subtype );
		}
	}

	QValueList<RateClass*>::iterator it = rates.begin();
	QValueList<RateClass*>::iterator rcEnd = rates.end();
	for ( ; it != rcEnd; ++it )
		client()->rateManager()->registerClass( ( *it ) );
	
	emit gotRateLimits();
}

void RateInfoTask::sendRateInfoAck()
{
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "sending rate info acknowledgement" << endl;
	FLAP f = { 0x02, 0, 0 };
	SNAC s = { 0x0001, 0x0008, 0x0000, client()->snacSequence() };
	Buffer* buffer = new Buffer();

	QValueListConstIterator<int> cit = m_rateGroups.begin();
	QValueListConstIterator<int> end = m_rateGroups.end();
	for ( cit = m_rateGroups.begin(); cit != end; ++cit )
	{
		//kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Adding rate " << (*cit) << " to rate ack" << endl;
		buffer->addWord( (*cit) );
	}

	Transfer* st = createTransfer( f, s, buffer );
	send( st );
	setSuccess( 0, QString::null );
}

#include "rateinfotask.moc"

//kate: tab-width 4; indent-mode csands;