summaryrefslogtreecommitdiff
blob: 9b17c6766e4de08152bb60627d365c40b5b86df3 (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
Index: kopete/protocols/oscar/liboscar/ssimanager.cpp
===================================================================
--- kopete/protocols/oscar/liboscar/ssimanager.cpp	(revision 565947)
+++ kopete/protocols/oscar/liboscar/ssimanager.cpp	(working copy)
@@ -71,18 +71,65 @@
 		while ( it != d->SSIList.end() && d->SSIList.count() > 0 )
 			it = d->SSIList.remove( it );
 	};
+	
+	d->nextContactId = 0;
+	d->nextGroupId = 0;
 }
 
 WORD SSIManager::nextContactId()
 {
-	d->nextContactId++;
-	return d->nextContactId;
+	if ( d->nextContactId == 0 )
+		d->nextContactId++;
+	
+	QValueList<Oscar::SSI>::const_iterator it, listEnd;
+	
+	for ( ; d->nextContactId < 0x8000; d->nextContactId++ )
+	{
+		bool freeId = true;
+		listEnd = d->SSIList.end();
+		for ( it = d->SSIList.begin(); it != listEnd; ++it )
+		{
+			if ( ( *it ).bid() == d->nextContactId )
+			{
+				freeId = false;
+				break;
+			}
+		}
+	
+		if ( freeId )
+			return d->nextContactId;
+	}
+	
+	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "No free id!" << endl;
+	return 0xFFFF;
 }
 
 WORD SSIManager::nextGroupId()
 {
-	d->nextGroupId++;
-	return d->nextGroupId;
+	if ( d->nextGroupId == 0 )
+		d->nextGroupId++;
+	
+	QValueList<Oscar::SSI>::const_iterator it, listEnd;
+	
+	for ( ; d->nextGroupId < 0x8000; d->nextGroupId++ )
+	{
+		bool freeId = true;
+		listEnd = d->SSIList.end();
+		for ( it = d->SSIList.begin(); it != listEnd; ++it )
+		{
+			if ( ( *it ).type() == ROSTER_GROUP && ( *it ).gid() == d->nextGroupId )
+			{
+				freeId = false;
+				break;
+			}
+		}
+		
+		if ( freeId )
+			return d->nextGroupId;
+	}
+	
+	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "No free group id!" << endl;
+	return 0xFFFF;
 }
 
 WORD SSIManager::numberOfItems() const
@@ -390,8 +437,6 @@
 	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;
-		if ( group.gid() > d->nextGroupId )
-			d->nextGroupId = group.gid();
 		
 		d->SSIList.append( group );
 		emit groupAdded( group );
@@ -426,6 +471,10 @@
 	QString groupName = group.name();
 	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Removing group " << group.name() << endl;
 	int remcount = d->SSIList.remove( group );
+	
+	if ( d->nextGroupId > group.gid() )
+		d->nextGroupId = group.gid();
+	
 	if ( remcount == 0 )
 	{
 		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "No groups removed" << endl;
@@ -452,13 +501,6 @@
 
 bool SSIManager::newContact( const Oscar::SSI& contact )
 {
-	//what to validate?
-	if ( contact.bid() > d->nextContactId )
-	{
-		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Setting next contact ID to " << contact.bid() << endl;
-		d->nextContactId = contact.bid();
-	}
-	
 	if ( d->SSIList.findIndex( contact ) == -1 )
 	{
 		kdDebug( OSCAR_RAW_DEBUG ) << k_funcinfo << "Adding contact '" << contact.name() << "' to SSI list" << endl;
@@ -496,6 +538,9 @@
 	QString contactName = contact.name();
 	int remcount = d->SSIList.remove( contact );
 	
+	if ( d->nextContactId > contact.bid() )
+		d->nextContactId = contact.bid();
+	
 	if ( remcount == 0 )
 	{
 		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "No contacts were removed." << endl;
@@ -520,12 +565,6 @@
 
 bool SSIManager::newItem( const Oscar::SSI& item )
 {
-	if ( item.bid() > d->nextContactId )
-	{
-		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Setting next contact ID to " << item.bid() << endl;
-		d->nextContactId = item.bid();
-	}
-	
 	//no error checking for now
 	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Adding item " << item.toString() << endl;
 	d->SSIList.append( item );
@@ -535,6 +574,10 @@
 bool SSIManager::removeItem( const Oscar::SSI& item )
 {
 	d->SSIList.remove( item );
+	
+	if ( d->nextContactId > item.bid() )
+		d->nextContactId = item.bid();
+	
 	return true;
 }