@ -62,6 +62,9 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const
QVariant result ;
Item * item = static_cast < Item * > ( index . internalPointer ( ) ) ;
if ( item - > type = = Item : : reference ) {
item = static_cast < Reference * > ( item ) - > dereference ( ) ;
}
switch ( role ) {
case Qt : : DisplayRole :
{
@ -430,7 +433,8 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
{
Item * parent ;
Account * acc ;
Contact * sample = 0 ;
Contact * contact ;
Reference * ref = 0 ;
ElId id ( account , jid ) ;
{
@ -442,13 +446,18 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
acc = itr - > second ;
}
for ( std : : multimap < ElId , Contact * > : : iterator itr = contacts . lower_bound ( id ) , eItr = contacts . upper_bound ( id ) ; itr ! = eItr ; + + itr ) {
sample = itr - > second ; //need to find if this contact is already added somewhere
break ; //so one iteration is enough
{
std : : map < ElId , Contact * > : : iterator itr = contacts . find ( id ) ;
if ( itr = = contacts . end ( ) ) {
contact = new Contact ( acc , jid , data ) ;
contacts . insert ( std : : make_pair ( id , contact ) ) ;
} else {
contact = itr - > second ;
}
}
if ( group = = " " ) { //this means this contact is already added somewhere and there is no sense to add it ungrouped
if ( sample ! = 0 ) {
if ( group = = " " ) {
if ( acc - > getContact ( jid ) ! = - 1 ) {
qDebug ( ) < < " An attempt to add a contact " < < jid < < " to the ungrouped contact set of account " < < account < < " for the second time, skipping " ;
return ;
} else {
@ -464,39 +473,26 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
parent = itr - > second ;
for ( int i = 0 ; i < parent - > childCount ( ) ; + + i ) { //checking if the contact is already added to that group
Item * item = parent - > child ( i ) ;
if ( item - > type = = Item : : contact ) {
Contact * ca = static_cast < Contact * > ( item ) ;
if ( ca - > getJid ( ) = = jid ) {
qDebug ( ) < < " An attempt to add a contact " < < jid < < " to the group " < < group < < " for the second time, skipping " ;
return ;
}
}
if ( parent - > getContact ( jid ) ! = - 1 ) {
qDebug ( ) < < " An attempt to add a contact " < < jid < < " to the group " < < group < < " for the second time, skipping " ;
return ;
}
for ( int i = 0 ; i < acc - > childCount ( ) ; + + i ) { //checking if that contact is among ugrouped
Item * item = acc - > child ( i ) ;
if ( item - > type = = Item : : contact ) {
Contact * ca = static_cast < Contact * > ( item ) ;
if ( ca - > getJid ( ) = = jid ) {
qDebug ( ) < < " An attempt to add a already existing contact " < < jid < < " to the group " < < group < < " , contact will be moved from ungrouped contacts of " < < account ;
parent - > appendChild ( ca ) ;
return ;
}
}
int refIndex = acc - > getContact ( jid ) ;
if ( refIndex ! = - 1 ) { //checking if that contact is among ugrouped
qDebug ( ) < < " An attempt to add a already existing contact " < < jid
< < " to the group " < < group
< < " , contact will be moved from ungrouped contacts of " < < account ;
ref = static_cast < Reference * > ( acc - > child ( refIndex ) ) ;
acc - > removeChild ( refIndex ) ;
}
}
Contact * contact ;
if ( sample = = 0 ) {
contact = new Contact ( jid , data ) ;
} else {
contact = sample - > copy ( ) ;
if ( ref = = 0 ) {
ref = new Reference ( contact ) ;
}
contacts . insert ( std : : make_pair ( id , contact ) ) ;
parent - > appendChild ( contact ) ;
parent - > appendChild ( ref ) ;
}
void Models : : Roster : : removeGroup ( const QString & account , const QString & name )
@ -513,33 +509,23 @@ void Models::Roster::removeGroup(const QString& account, const QString& name)
parent - > removeChild ( row ) ;
std : : deque < Contact * > toInsert ;
std : : deque < Reference * > toInsert ;
for ( int i = 0 ; item - > childCount ( ) > 0 ; + + i ) {
Contact * cont = static_cast < Contact * > ( item - > child ( 0 ) ) ;
Reference * ref = static_cast < Reference * > ( item - > child ( 0 ) ) ;
item - > removeChild ( 0 ) ;
std : : multimap < ElId , Contact * > : : iterator cBeg = contacts . lower_bound ( { account , cont - > getJid ( ) } ) ;
std : : multimap < ElId , Contact * > : : iterator cEnd = contacts . upper_bound ( { account , cont - > getJid ( ) } ) ;
int count = std : : distance ( cBeg , cEnd ) ;
if ( count > 1 ) {
for ( ; cBeg ! = cEnd ; + + count , + + cBeg ) {
if ( cBeg - > second = = cont ) {
delete cont ;
contacts . erase ( cBeg ) ;
break ;
}
}
Contact * cont = static_cast < Contact * > ( ref - > dereference ( ) ) ;
if ( cont - > referencesCount ( ) = = 1 ) {
toInsert . push_back ( ref ) ;
} else {
toInsert . push_back ( cont ) ;
delete ref ;
}
}
if ( toInsert . size ( ) > 0 ) {
Account * acc = accounts . find ( " account " ) - > second ;
for ( std : : deque < Contact * > : : size_type i = 0 ; i < toInsert . size ( ) ; + + i ) {
Contact * cont = toInsert [ i ] ;
acc - > appendChild ( cont ) ; //TODO optimisation
acc - > appendChild ( toInsert [ i ] ) ; //TODO optimisation
}
}
@ -550,19 +536,18 @@ void Models::Roster::removeGroup(const QString& account, const QString& name)
void Models : : Roster : : changeContact ( const QString & account , const QString & jid , const QMap < QString , QVariant > & data )
{
ElId id ( account , jid ) ;
std : : multimap < ElId , Contact * > : : iterator cBeg = contacts . lower_bound ( id ) ;
std : : multimap < ElId , Contact * > : : iterator cEnd = contacts . upper_bound ( id ) ;
std : : map < ElId , Contact * > : : iterator cItr = contacts . find ( id ) ;
for ( ; cBeg ! = cEnd ; + + cBeg ) {
if ( cItr ! = contacts . end ( ) ) {
for ( QMap < QString , QVariant > : : const_iterator itr = data . begin ( ) , end = data . end ( ) ; itr ! = end ; + + itr ) {
cBeg - > second - > update ( itr . key ( ) , itr . value ( ) ) ;
cItr - > second - > update ( itr . key ( ) , itr . value ( ) ) ;
}
}
std : : map < ElId , Room * > : : iterator rItr = rooms . find ( id ) ;
if ( rItr ! = rooms . end ( ) ) {
for ( QMap < QString , QVariant > : : const_iterator itr = data . begin ( ) , end = data . end ( ) ; itr ! = end ; + + itr ) {
rItr - > second - > update ( itr . key ( ) , itr . value ( ) ) ;
} else {
std : : map < ElId , Room * > : : iterator rItr = rooms . find ( id ) ;
if ( rItr ! = rooms . end ( ) ) {
for ( QMap < QString , QVariant > : : const_iterator itr = data . begin ( ) , end = data . end ( ) ; itr ! = end ; + + itr ) {
rItr - > second - > update ( itr . key ( ) , itr . value ( ) ) ;
}
}
}
}
@ -570,18 +555,13 @@ void Models::Roster::changeContact(const QString& account, const QString& jid, c
void Models : : Roster : : changeMessage ( const QString & account , const QString & jid , const QString & id , const QMap < QString , QVariant > & data )
{
ElId elid ( account , jid ) ;
std : : multimap < ElId , Contact * > : : iterator cBeg = contacts . lower_bound ( elid ) ;
std : : multimap < ElId , Contact * > : : iterator cEnd = contacts . upper_bound ( elid ) ;
std : : map < ElId , Contact * > : : iterator cItr = contacts . find ( elid ) ;
for ( ; cBeg ! = cEnd ; + + cBeg ) {
for ( QMap < QString , QVariant > : : const_iterator itr = data . begin ( ) , end = data . end ( ) ; itr ! = end ; + + itr ) {
cBeg - > second - > changeMessage ( id , data ) ;
}
}
std : : map < ElId , Room * > : : iterator rItr = rooms . find ( elid ) ;
if ( rItr ! = rooms . end ( ) ) {
for ( QMap < QString , QVariant > : : const_iterator itr = data . begin ( ) , end = data . end ( ) ; itr ! = end ; + + itr ) {
if ( cItr ! = contacts . end ( ) ) {
cItr - > second - > changeMessage ( id , data ) ;
} else {
std : : map < ElId , Room * > : : iterator rItr = rooms . find ( elid ) ;
if ( rItr ! = rooms . end ( ) ) {
rItr - > second - > changeMessage ( id , data ) ;
}
}
@ -590,25 +570,26 @@ void Models::Roster::changeMessage(const QString& account, const QString& jid, c
void Models : : Roster : : removeContact ( const QString & account , const QString & jid )
{
ElId id ( account , jid ) ;
std : : multimap < ElId , Contact * > : : iterator cBeg = contacts . lower_bound ( id ) ;
std : : multimap < ElId , Contact * > : : iterator cEnd = contacts . upper_bound ( id ) ;
std : : multimap < ElId , Contact * > : : iterator cpBeg = cBeg ;
std : : map < ElId , Contact * > : : iterator itr = contacts . find ( id ) ;