jabbercontactpool.cpp 9.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 /*
  * jabbercontactpool.cpp
  *
  * Copyright (c) 2004 by Till Gerken <till@tantalo.net>
  * Copyright (c) 2006      by Olivier Goffart  <ogoffart at kde.org>
  *
  * Kopete    (c) by the Kopete developers  <kopete-devel@kde.org>
  *
  * *************************************************************************
  * *                                                                       *
  * * This program is free software; you can redistribute it and/or modify  *
  * * it under the terms of the GNU General Public License as published by  *
  * * the Free Software Foundation; either version 2 of the License, or     *
  * * (at your option) any later version.                                   *
  * *                                                                       *
  * *************************************************************************
  */

#include "jabbercontactpool.h"
20
#include "jabber_protocol_debug.h"
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

#include <kmessagebox.h>
#include <kopeteaccountmanager.h>
#include <kopetecontactlist.h>
#include "kopeteuiglobal.h"
#include "kopetemetacontact.h"

#include "jabberprotocol.h"
#include "jabberbasecontact.h"
#include "jabbercontact.h"
#include "jabbergroupcontact.h"
#include "jabbergroupmembercontact.h"
#include "jabberresourcepool.h"
#include "jabberaccount.h"
#include "jabbertransport.h"

JabberContactPool::JabberContactPool ( JabberAccount *account )
{

	mAccount = account;

}

JabberContactPool::~JabberContactPool ()
{
	qDeleteAll(mPool);
}

JabberContactPoolItem *JabberContactPool::findPoolItem ( const XMPP::RosterItem &contact )
{

	// see if the contact already exists
	foreach(JabberContactPoolItem *mContactItem, mPool)
	{
		if ( mContactItem->contact()->rosterItem().jid().full().toLower() == contact.jid().full().toLower() )
		{
			return mContactItem;
		}
	}

	return 0;

}

JabberContact *JabberContactPool::addContact ( const XMPP::RosterItem &contact, Kopete::MetaContact *metaContact, bool dirty )
{
	// see if the contact already exists
	JabberContactPoolItem *mContactItem = findPoolItem ( contact );
	if ( mContactItem)
	{
71
		qCDebug(JABBER_PROTOCOL_LOG) << "Updating existing contact " << contact.jid().full() << "   -  " <<   mContactItem->contact();
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

		// It exists, update it.
		mContactItem->contact()->updateContact ( contact );
		mContactItem->setDirty ( dirty );

		JabberContact *retval = dynamic_cast<JabberContact *>(mContactItem->contact ());

		if ( !retval )
		{
			KMessageBox::error ( Kopete::UI::Global::mainWidget (),
								 "Fatal error in the Jabber contact pool. Please restart Kopete and submit a debug log "
								 "of your session to http://bugs.kde.org.",
								 "Fatal Jabber Error" );
		}

		return retval;
	}

90
	qCDebug(JABBER_PROTOCOL_LOG) << "Adding new contact " << contact.jid().full();
91 92 93 94 95 96 97 98 99 100 101 102 103
	
	JabberTransport *transport=0l;
	QString legacyId;
	//find if the contact should be added to a transport.
	if(mAccount->transports().contains(contact.jid().domain()))
	{
		transport=mAccount->transports()[contact.jid().domain()];
		legacyId=transport->legacyId( contact.jid() );
	}
		
	// create new contact instance and add it to the dictionary
	JabberContact *newContact = new JabberContact ( contact, transport ? (Kopete::Account*)transport : (Kopete::Account*)mAccount, metaContact , legacyId );
	JabberContactPoolItem *newContactItem = new JabberContactPoolItem ( newContact );
Laurent Montel's avatar
Laurent Montel committed
104
	connect ( newContact, SIGNAL (contactDestroyed(Kopete::Contact*)), this, SLOT (slotContactDestroyed(Kopete::Contact*)) );
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	newContactItem->setDirty ( dirty );
	mPool.append ( newContactItem );

	return newContact;

}

JabberBaseContact *JabberContactPool::addGroupContact ( const XMPP::RosterItem &contact, bool roomContact, Kopete::MetaContact *metaContact, bool dirty )
{

	XMPP::RosterItem mContact ( roomContact ? contact.jid().userHost () : contact.jid().full() );

	// see if the contact already exists
	JabberContactPoolItem *mContactItem = findPoolItem ( mContact );
	if ( mContactItem)
	{
		if(mContactItem->contact()->inherits(roomContact ?
				 (const char*)("JabberGroupContact") : (const char*)("JabberGroupMemberContact") ) )
		{
			
125
			qCDebug(JABBER_PROTOCOL_LOG) << "Updating existing contact " << mContact.jid().full();
126 127 128 129 130 131 132 133 134 135 136 137
			
			// It exists, update it.
			mContactItem->contact()->updateContact ( mContact );
			mContactItem->setDirty ( dirty );
	
			//we must tell to the originating function that no new contact has been added
			return 0L;//mContactItem->contact ();
		}
		else
		{
			//this happen if we receive a MUC invitaiton:  when the invitaiton is received, it's from the muc itself
			//and then kopete will create a temporary contact for it. but it will not be a good contact.
138
			qCDebug(JABBER_PROTOCOL_LOG) << "Bad contact will be removed and re-added " << mContact.jid().full();
139 140 141 142 143 144 145 146 147 148 149 150
			Kopete::MetaContact *old_mc=mContactItem->contact()->metaContact();
			delete mContactItem->contact();
			mContactItem = 0L;
			if(old_mc->contacts().isEmpty() && old_mc!=metaContact)
			{
				Kopete::ContactList::self()->removeMetaContact( old_mc );
			}
			
		}

	}

151
	qCDebug(JABBER_PROTOCOL_LOG) << "Adding new contact " << mContact.jid().full();
152 153 154 155 156 157 158 159 160 161 162

	// create new contact instance and add it to the dictionary
	JabberBaseContact *newContact;

	if ( roomContact )
		newContact = new JabberGroupContact ( contact, mAccount, metaContact );
	else
		newContact = new JabberGroupMemberContact ( contact, mAccount, metaContact );

	JabberContactPoolItem *newContactItem = new JabberContactPoolItem ( newContact );

Laurent Montel's avatar
Laurent Montel committed
163
	connect ( newContact, SIGNAL (contactDestroyed(Kopete::Contact*)), this, SLOT (slotContactDestroyed(Kopete::Contact*)) );
164 165 166 167 168 169 170 171 172 173

	newContactItem->setDirty ( dirty );
	mPool.append ( newContactItem );

	return newContact;

}

void JabberContactPool::removeContact ( const XMPP::Jid &jid )
{
174
	qCDebug(JABBER_PROTOCOL_LOG) << "Removing contact " << jid.full();
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

	foreach(JabberContactPoolItem *mContactItem, mPool)
	{
		if ( mContactItem->contact()->rosterItem().jid().full().toLower() == jid.full().toLower() )
		{
			/*
			 * The following deletion will cause slotContactDestroyed()
			 * to be called, which will clean the up the list.
			 */
			if(mContactItem->contact())
			{
				Kopete::MetaContact *mc=mContactItem->contact()->metaContact();
				delete mContactItem->contact ();
				if(mc && mc->contacts().isEmpty())
				{
					Kopete::ContactList::self()->removeMetaContact(mc) ;
				}
			}
			return;
		}
	}

197
	qCDebug(JABBER_PROTOCOL_LOG) << "WARNING: No match found!";
198 199 200 201 202

}

void JabberContactPool::slotContactDestroyed ( Kopete::Contact *contact )
{
203
	qCDebug(JABBER_PROTOCOL_LOG) << "Contact deleted, collecting the pieces...";
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

	JabberBaseContact *jabberContact = static_cast<JabberBaseContact *>( contact ); 
	//WARNING  this ptr is not usable, we are in the Kopete::Contact destructor

	// remove contact from the pool
	foreach(JabberContactPoolItem *mContactItem, mPool)
	{
		if ( mContactItem->contact() == jabberContact )
		{
			JabberContactPoolItem *deletedItem = mPool.takeAt( mPool.indexOf(mContactItem) );
			delete deletedItem;

			break;
		}
	}

	// delete all resources for it
	if(contact->account()==(Kopete::Account*)(mAccount))
		mAccount->resourcePool()->removeAllResources ( XMPP::Jid ( contact->contactId() ) );
	else
	{
		//this is a legacy contact. we have no way to get the real Jid at this point, we can only guess it.
		QString contactId= contact->contactId().replace('@','%') + '@' + contact->account()->myself()->contactId();
		mAccount->resourcePool()->removeAllResources ( XMPP::Jid ( contactId ) ) ;
	}

}

void JabberContactPool::clear ()
{
234
	qCDebug(JABBER_PROTOCOL_LOG) << "Clearing the contact pool.";
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

	foreach(JabberContactPoolItem *mContactItem, mPool)
	{
		/*
		 * The following deletion will cause slotContactDestroyed()
		 * to be called, which will clean the up the list.
		 * NOTE: this is a very inefficient way to clear the list
		 */
		delete mContactItem->contact ();
	}

}

void JabberContactPool::setDirty ( const XMPP::Jid &jid, bool dirty )
{
250
	qCDebug(JABBER_PROTOCOL_LOG) << "Setting flag for " << jid.full() << " to " << dirty;
251 252 253 254 255 256 257 258 259 260

	foreach(JabberContactPoolItem *mContactItem, mPool)
	{
		if ( mContactItem->contact()->rosterItem().jid().full().toLower() == jid.full().toLower() )
		{
			mContactItem->setDirty ( dirty );
			return;
		}
	}

261
	qCDebug(JABBER_PROTOCOL_LOG) << "WARNING: No match found!";
262 263 264 265 266

}

void JabberContactPool::cleanUp ()
{
267
	qCDebug(JABBER_PROTOCOL_LOG) << "Cleaning dirty items from contact pool.";
268 269 270 271 272

	foreach(JabberContactPoolItem *mContactItem, mPool)
	{
		if ( mContactItem->dirty () )
		{
273
			qCDebug(JABBER_PROTOCOL_LOG) << "Removing dirty contact " << mContactItem->contact()->contactId ();
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

			/*
			 * The following deletion will cause slotContactDestroyed()
			 * to be called, which will clean the up the list.
			 */
			delete mContactItem->contact ();
		}
	}

}

JabberBaseContact *JabberContactPool::findExactMatch ( const XMPP::Jid &jid )
{

	foreach(JabberContactPoolItem *mContactItem, mPool)
	{
		if ( mContactItem->contact()->rosterItem().jid().full().toLower () == jid.full().toLower () )
		{
			return mContactItem->contact ();
		}
	}

	return 0L;

}

JabberBaseContact *JabberContactPool::findRelevantRecipient ( const XMPP::Jid &jid )
{

	foreach(JabberContactPoolItem *mContactItem, mPool)
	{
		if ( mContactItem->contact()->rosterItem().jid().full().toLower () == jid.userHost().toLower () )
		{
			return mContactItem->contact ();
		}
	}

	return 0L;

}

QList<JabberBaseContact*> JabberContactPool::findRelevantSources ( const XMPP::Jid &jid )
{
	QList<JabberBaseContact*> list;

	foreach(JabberContactPoolItem *mContactItem, mPool)
	{
		if ( mContactItem->contact()->rosterItem().jid().userHost().toLower () == jid.userHost().toLower () )
		{
			list.append ( mContactItem->contact () );
		}
	}

	return list;

}

JabberContactPoolItem::JabberContactPoolItem ( JabberBaseContact *contact )
{
	mDirty = true;
	mContact = contact;
}

JabberContactPoolItem::~JabberContactPoolItem ()
{
}

void JabberContactPoolItem::setDirty ( bool dirty )
{
	mDirty = dirty;
}

bool JabberContactPoolItem::dirty ()
{
	return mDirty;
}

JabberBaseContact *JabberContactPoolItem::contact ()
{
	return mContact;
}

356