summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/ssimanager.cpp
blob: 066e93fadf25445e09b4cdd756a815a085b074ed (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
	Kopete Oscar Protocol
	ssimanager.cpp - SSI management

	Copyright ( c ) 2004 Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
	Copyright ( c ) 2004 Matt Rogers <mattr@kde.org>

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

	based on ssidata.h and ssidata.cpp ( c ) 2002 Tom Linsky <twl6@po.cwru.edu>

	*************************************************************************
	*                                                                       *
	* 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 "ssimanager.h"
#include <kdebug.h>
#include "oscarutils.h"

// -------------------------------------------------------------------

class SSIManagerPrivate
{
public:
	QValueList<Oscar::SSI> SSIList;
	QValueList<WORD> groupIdList;
	QValueList<WORD> itemIdList;
	bool complete;
	DWORD lastModTime;
	WORD maxContacts;
	WORD maxGroups;
	WORD maxVisible;
	WORD maxInvisible;
	WORD maxIgnore;
	WORD nextContactId;
	WORD nextGroupId;
};

SSIManager::SSIManager( QObject *parent, const char *name )
 : QObject( parent, name )
{
	d = new SSIManagerPrivate;
	d->complete = false;
	d->lastModTime = 0;
	d->nextContactId = 0;
	d->nextGroupId = 0;
	d->maxContacts = 999;
	d->maxGroups = 999;
	d->maxIgnore = 999;
	d->maxInvisible = 999;
	d->maxVisible = 999;
}


SSIManager::~SSIManager()
{
	clear();
	delete d;
}

void SSIManager::clear()
{
	//delete all SSIs from the list
	if ( d->SSIList.count() > 0 )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Clearing the SSI list" << endl;
		QValueList<Oscar::SSI>::iterator it = d->SSIList.begin();
		
		while ( it != d->SSIList.end() && d->SSIList.count() > 0 )
			it = d->SSIList.remove( it );
	};
	
	d->itemIdList.clear();
	d->groupIdList.clear();
	d->complete = false;
	d->lastModTime = 0;
	d->nextContactId = 0;
	d->nextGroupId = 0;
}

WORD SSIManager::nextContactId()
{
	if ( d->nextContactId == 0 )
		d->nextContactId++;
	
	d->nextContactId = findFreeId( d->itemIdList, d->nextContactId );
	
	if ( d->nextContactId == 0xFFFF )
	{
		kdWarning(OSCAR_RAW_DEBUG) << k_funcinfo << "No free id!" << endl;
		return 0xFFFF;
	}
	
	if ( d->itemIdList.contains( d->nextContactId ) == 0 )
		d->itemIdList.append( d->nextContactId );
		
	return d->nextContactId++;
}

WORD SSIManager::nextGroupId()
{
	if ( d->nextGroupId == 0 )
		d->nextGroupId++;
	
	d->nextGroupId = findFreeId( d->groupIdList, d->nextGroupId );
	
	if ( d->nextGroupId == 0xFFFF )
	{
		kdWarning(OSCAR_RAW_DEBUG) << k_funcinfo << "No free group id!" << endl;
		return 0xFFFF;
	}
	
	if ( d->groupIdList.contains( d->nextGroupId ) == 0 )
		d->groupIdList.append( d->nextGroupId );
	
	return d->nextGroupId++;
}

WORD SSIManager::numberOfItems() const
{
	return d->SSIList.count();
}

DWORD SSIManager::lastModificationTime() const
{
	return d->lastModTime;
}

void SSIManager::setLastModificationTime( DWORD lastTime )
{
	d->lastModTime = lastTime;
}

void SSIManager::setParameters( WORD maxContacts, WORD maxGroups, WORD maxVisible, WORD maxInvisible, WORD maxIgnore )
{
	//I'm not using k_funcinfo for these debug statements because of
	//the function's long signature
	QString funcName = QString::fromLatin1( "[void SSIManager::setParameters] " );
	kdDebug(OSCAR_RAW_DEBUG) << funcName << "Max number of contacts allowed in SSI: "
		<< maxContacts << endl;
	kdDebug(OSCAR_RAW_DEBUG) << funcName << "Max number of groups allowed in SSI: "
		<< maxGroups << endl;
	kdDebug(OSCAR_RAW_DEBUG) << funcName << "Max number of contacts allowed on visible list: "
		<< maxVisible << endl;
	kdDebug(OSCAR_RAW_DEBUG) << funcName << "Max number of contacts allowed on invisible list: "
		<< maxInvisible << endl;
	kdDebug(OSCAR_RAW_DEBUG) << funcName << "Max number of contacts allowed on ignore list: "
		<< maxIgnore << endl;
	
	d->maxContacts = maxContacts;
	d->maxGroups = maxGroups;
	d->maxInvisible = maxInvisible;
	d->maxVisible = maxVisible;
	d->maxIgnore = maxIgnore;
}

void SSIManager::loadFromExisting( const QValueList<Oscar::SSI*>& newList )
{
	Q_UNUSED( newList );
	//FIXME: NOT Implemented!
}

bool SSIManager::hasItem( const Oscar::SSI& item ) const
{
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
	{
		Oscar::SSI s = ( *it );
		if ( s == item )
			return true;
	}
	
	return false;
}

Oscar::SSI SSIManager::findGroup( const QString &group ) const
{
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
		if ( ( *it ).type() == ROSTER_GROUP && (*it ).name().lower() == group.lower() )
			return ( *it );
	
	
	return m_dummyItem;
}

Oscar::SSI SSIManager::findGroup( int groupId ) const
{
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
		if ( ( *it ).type() == ROSTER_GROUP && (*it ).gid() == groupId )
			return ( *it );
	
	return m_dummyItem;
}

Oscar::SSI SSIManager::findContact( const QString &contact, const QString &group ) const
{
	
	if ( contact.isNull() || group.isNull() )
	{
		kdWarning(OSCAR_RAW_DEBUG) << k_funcinfo <<
			"Passed NULL name or group string, aborting!" << endl;
		
		return m_dummyItem;
	}
	
	Oscar::SSI gr = findGroup( group ); // find the parent group
	if ( gr.isValid() )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "gr->name= " << gr.name() <<
			", gr->gid= " << gr.gid() <<
			", gr->bid= " << gr.bid() <<
			", gr->type= " << gr.type() << endl;
	
		QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
		
		for ( it = d->SSIList.begin(); it != listEnd; ++it )
		{
			if ( ( *it ).type() == ROSTER_CONTACT && (*it ).name() == contact && (*it ).gid() == gr.gid() )
			{
				//we have found our contact
				kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo <<
					"Found contact " << contact << " in SSI data" << endl;
				 return ( *it );
			}
		}
	}
	else
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo <<
			"ERROR: Group '" << group << "' not found!" << endl;
	}
	return m_dummyItem;
}

Oscar::SSI SSIManager::findContact( const QString &contact ) const
{
	
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
		if ( ( *it ).type() == ROSTER_CONTACT && (*it ).name() == contact )
			return ( *it );
	
	return m_dummyItem;
}

Oscar::SSI SSIManager::findContact( int contactId ) const
{
	QValueList<Oscar::SSI>::const_iterator it,  listEnd = d->SSIList.end();
	
	for ( it = d->SSIList.begin(); it!= listEnd; ++it )
		if ( ( *it ).type() == ROSTER_CONTACT && ( *it ).bid() == contactId )
			return ( *it );
	
	return m_dummyItem;
}

Oscar::SSI SSIManager::findItemForIcon( QByteArray iconHash ) const
{
	QValueList<Oscar::SSI>::const_iterator it,  listEnd = d->SSIList.end();
	
	for ( it = d->SSIList.begin(); it!= listEnd; ++it )
	{
		if ( ( *it ).type() == ROSTER_BUDDYICONS )
		{
			TLV t = Oscar::findTLV( ( *it ).tlvList(), 0x00D5 );
			Buffer b(t.data);
			b.skipBytes(1); //don't care about flags
			BYTE iconSize = b.getByte();
			QByteArray hash( b.getBlock( iconSize ) );
			if ( hash == iconHash )
			{
				Oscar::SSI s = ( *it );
				return s;
			}
		}
	}
	return m_dummyItem;
}

Oscar::SSI SSIManager::findItemForIconByRef( int ref ) const
{
	QValueList<Oscar::SSI>::const_iterator it,  listEnd = d->SSIList.end();
	
	for ( it = d->SSIList.begin(); it!= listEnd; ++it )
	{
		if ( ( *it ).type() == ROSTER_BUDDYICONS )
		{
			if ( ( *it ).name().toInt() == ref )
			{
				Oscar::SSI s = ( *it );
				return s;
			}
		}
	}
	return m_dummyItem;	
}

Oscar::SSI SSIManager::findItem( const QString &contact, int type ) const
{
	QValueList<Oscar::SSI>::const_iterator it,  listEnd = d->SSIList.end();
	
	for ( it = d->SSIList.begin(); it!= listEnd; ++it )
		if ( ( *it ).type() == type && ( *it ).name() == contact )
			return ( *it );
	
	return m_dummyItem;
}

QValueList<Oscar::SSI> SSIManager::groupList() const
{
	QValueList<Oscar::SSI> list;
	
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
		if ( ( *it ).type() == ROSTER_GROUP  )
			list.append( ( *it ) );
	
	return list;
}

QValueList<Oscar::SSI> SSIManager::contactList() const
{
	QValueList<Oscar::SSI> list;
	
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
		if ( ( *it ).type() == ROSTER_CONTACT  )
			list.append( ( *it ) );
	
	return list;
}

QValueList<Oscar::SSI> SSIManager::visibleList() const
{
	QValueList<Oscar::SSI> list;
	
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
		if ( ( *it ).type() == ROSTER_VISIBLE  )
			list.append( ( *it ) );
	
	return list;
}

QValueList<Oscar::SSI> SSIManager::invisibleList() const
{
	QValueList<Oscar::SSI> list;
	
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
		if ( ( *it ).type() == ROSTER_INVISIBLE  )
			list.append( ( *it ) );
	
	return list;
}

QValueList<Oscar::SSI> SSIManager::contactsFromGroup( const QString &group ) const
{
	QValueList<Oscar::SSI> list;
	
	Oscar::SSI gr = findGroup( group );
	if ( gr.isValid() )
	{
		QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
		for ( it = d->SSIList.begin(); it != listEnd; ++it )
			if ( ( *it ).type() == ROSTER_CONTACT && (*it ).gid() == gr.gid() )
				list.append( ( *it ) );
	}
	return list;
}

QValueList<Oscar::SSI> SSIManager::contactsFromGroup( int groupId ) const
{
	QValueList<Oscar::SSI> list;
	
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
		if ( ( *it ).type() == ROSTER_CONTACT && (*it ).gid() == groupId  )
			list.append( ( *it ) );
	
	return list;
}

Oscar::SSI SSIManager::visibilityItem() const
{
	Oscar::SSI item = m_dummyItem;
	QValueList<Oscar::SSI>::const_iterator it, listEnd = d->SSIList.end();
	for ( it = d->SSIList.begin(); it != listEnd; ++it )
	{
		if ( ( *it ).type() == 0x0004 )
		{
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Found visibility setting" << endl;
			item = ( *it );
			return item;
		}
	}
	
	return item;
}

void SSIManager::setListComplete( bool complete )
{
	d->complete = complete;
}

bool SSIManager::listComplete() const
{
	return d->complete;
}

bool SSIManager::newGroup( const Oscar::SSI& group )
{
	//trying to find the group by its ID
	QValueList<Oscar::SSI>::iterator it, listEnd = d->SSIList.end();
	if ( findGroup( group.name() ).isValid() )
		return false;

	if ( !group.name().isEmpty() ) //avoid the group with gid 0 and bid 0
	{	// the group is really new
		kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Adding group '" << group.name() << "' to SSI list" << endl;
		
		d->SSIList.append( group );
		addID( group );
		emit groupAdded( group );
		return true;
	}
	return false;
}

bool SSIManager::updateGroup( const Oscar::SSI& group )
{
	Oscar::SSI oldGroup = findGroup( group.name() );

	if ( oldGroup.isValid() )
	{
		removeID( oldGroup );
		d->SSIList.remove( oldGroup );
	}

	if ( d->SSIList.findIndex( group ) != -1 )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "New group is already in list." << endl;
		return false;
	}

	kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Updating group '" << group.name() << "' in SSI list" << endl;
	d->SSIList.append( group );
	addID( group );
	emit groupUpdated( group );

	return true;
}

bool SSIManager::removeGroup( const Oscar::SSI& group )
{
	QString groupName = group.name();
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Removing group " << group.name() << endl;
	int remcount = d->SSIList.remove( group );
	removeID( group );
	
	if ( remcount == 0 )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "No groups removed" << endl;
		return false;
	}
	
	emit groupRemoved( groupName );
	return true;
}

bool SSIManager::removeGroup( const QString &group )
{
	Oscar::SSI gr = findGroup( group );
	
	if ( gr.isValid() && removeGroup( gr )  )
	{
		return true;
	}
	else
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Group " << group << " not found." << endl;

	return false;
}

bool SSIManager::newContact( const Oscar::SSI& contact )
{
	if ( d->SSIList.findIndex( contact ) == -1 )
	{
		kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Adding contact '" << contact.name() << "' to SSI list" << endl;
		addID( contact );
		d->SSIList.append( contact );
		emit contactAdded( contact );
	}
	else
		return false;
	return true;
}

bool SSIManager::updateContact( const Oscar::SSI& contact )
{
	Oscar::SSI oldContact = findContact( contact.name() );

	if ( oldContact.isValid() )
	{
		removeID( oldContact );
		d->SSIList.remove( oldContact );
	}

	if ( d->SSIList.findIndex( contact ) != -1 )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "New contact is already in list." << endl;
		return false;
	}

	kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Updating contact '" << contact.name() << "' in SSI list" << endl;
	addID( contact );
	d->SSIList.append( contact );
	emit contactUpdated( contact );
	
	return true;
}

bool SSIManager::removeContact( const Oscar::SSI& contact )
{
	QString contactName = contact.name();
	int remcount = d->SSIList.remove( contact );
	removeID( contact );
	
	if ( remcount == 0 )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "No contacts were removed." << endl;
		return false;
	}
	
	emit contactRemoved( contactName );
	return true;
}

bool SSIManager::removeContact( const QString &contact )
{
	Oscar::SSI ct = findContact( contact );
	
	if ( ct.isValid() && removeContact( ct ) )
		return true;
	else
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Contact " << contact << " not found." << endl;
	
	return false;
}

bool SSIManager::newItem( const Oscar::SSI& item )
{
	if ( d->SSIList.findIndex( item ) != -1 )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Item is already in list." << endl;
		return false;
	}

	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Adding item " << item.toString() << endl;
	d->SSIList.append( item );
	addID( item );
	return true;
}

bool SSIManager::updateItem( const Oscar::SSI& item )
{
	Oscar::SSI oldItem = findItem( item.name(), item.type() );

	if ( oldItem.isValid() )
	{
		removeID( oldItem );
		d->SSIList.remove( oldItem );
	}

	if ( d->SSIList.findIndex( item ) != -1 )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "New item is already in list." << endl;
		return false;
	}

	kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Updating item in SSI list" << endl;
	addID( item );
	d->SSIList.append( item );
	return true;
}

bool SSIManager::removeItem( const Oscar::SSI& item )
{
	int remcount = d->SSIList.remove( item );
	removeID( item );

	if ( remcount == 0 )
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "No items were removed." << endl;
		return false;
	}
	
	return true;
}

void SSIManager::addID( const Oscar::SSI& item )
{
	if ( item.type() == ROSTER_GROUP )
	{
		if ( d->groupIdList.contains( item.gid() ) == 0 )
			d->groupIdList.append( item.gid() );
	}
	else
	{
		if ( d->itemIdList.contains( item.bid() ) == 0 )
			d->itemIdList.append( item.bid() );
	}
}

void SSIManager::removeID( const Oscar::SSI& item )
{
	if ( item.type() == ROSTER_GROUP )
	{
		d->groupIdList.remove( item.gid() );
	
		if ( d->nextGroupId > item.gid() )
			d->nextGroupId = item.gid();
	}
	else
	{
		d->itemIdList.remove( item.bid() );
		
		if ( d->nextContactId > item.bid() )
			d->nextContactId = item.bid();
	}
}

WORD SSIManager::findFreeId( const QValueList<WORD>& idList, WORD fromId ) const
{
	for ( WORD id = fromId; id < 0x8000; id++ )
	{
		if ( idList.contains( id ) == 0 )
			return id;
	}
	
	return 0xFFFF;
}

#include "ssimanager.moc"

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