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::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::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; }