void XmlContactStorage_Test::testLoad()
{
	// TODO: Check plugin data.
	// TODO: Check more things.
	QString xmlFilename = QString( SRCDIR ) + QString("xmlcontactstorage_test_list.xml");

	Kopete::ContactListStorage *storage = new Kopete::XmlContactStorage( xmlFilename );
	storage->load();

	// Verify that the loading went well.
	QVERIFY( storage->isValid() );
	// Check that we have the correct numbers of groups and contacts.
	QCOMPARE( storage->groups().size(), 5 );
	QCOMPARE( storage->contacts().size(), 6 );

	// Verify that we parsed the correct group names.
	QStringList groupNameList = expectedGroupList();
	QStringList::ConstIterator expectedIt, expectedItEnd = groupNameList.constEnd();

	uint groupId = 1;
	Kopete::Group::List loadGroupList = storage->groups();
	Kopete::Group::List::ConstIterator it, itEnd = loadGroupList.constEnd();
	for(it = loadGroupList.constBegin(), expectedIt = groupNameList.constBegin();
		it != itEnd, expectedIt != expectedItEnd;
		++it, ++expectedIt)
	{
		Kopete::Group *group = (*it);
		QString groupName = group->displayName();
		QString expectedGroupName = (*expectedIt);
		QCOMPARE( groupName, expectedGroupName );
		QCOMPARE( group->groupId(), groupId++ );
	}

	// Verify that we parsed the correct contacts.
	QStringList contactList = expectedContactList();
	QStringList::ConstIterator contactIt, contactItEnd = contactList.constEnd();
	
	Kopete::MetaContact::List loadContactList = storage->contacts();
	Kopete::MetaContact::List::ConstIterator loadContactIt, loadContactItEnd = loadContactList.constEnd();
	for(loadContactIt = loadContactList.constBegin(), contactIt = contactList.constBegin();
		loadContactIt != loadContactItEnd, contactIt != contactItEnd;
		++loadContactIt, ++contactIt)
	{
		Kopete::MetaContact *contact = (*loadContactIt);
		QString expectedContactName = (*contactIt);

		QCOMPARE( contact->displayName(), expectedContactName );
		QCOMPARE( (int)contact->displayNameSource(), (int)Kopete::MetaContact::SourceCustom );
	}
}
bool JabberTransport::createContact (const QString & contactId,  Kopete::MetaContact * metaContact)
{
#if 0 //TODO
	// collect all group names
	QStringList groupNames;
	Kopete::GroupList groupList = metaContact->groups();
	for(Kopete::Group *group = groupList.first(); group; group = groupList.next())
		groupNames += group->displayName();

	XMPP::Jid jid ( contactId );
	XMPP::RosterItem item ( jid );
	item.setName ( metaContact->displayName () );
	item.setGroups ( groupNames );

	// this contact will be created with the "dirty" flag set
	// (it will get reset if the contact appears in the roster during connect)
	JabberContact *contact = contactPool()->addContact ( item, metaContact, true );

	return ( contact != 0 );
#endif
	return false;
}
void WlmContact::sync(unsigned int flags)
{
    if (!account ()->isConnected ())
        return;
    /* 
     * If the contact has changed groups, then we update the server.
     */
    if( !metaContact() )
        return;

    // Don't add the contact if it's myself.
    if(contactId() == account()->accountId())
        return;

    if ( (flags & Kopete::Contact::MovedBetweenGroup) == Kopete::Contact::MovedBetweenGroup )
    {
        Kopete::Group* newGroup = metaContact()->groups().first();
        WlmAccount* acc = qobject_cast<WlmAccount*>(account());
        if(!acc)
            return;

        // if this contact is not in the contact list, add it
        if(!acc->isOnServerSideList(contactId()))
        {
            acc->createContact(contactId(), metaContact());
            m_currentGroup = newGroup;
            return;
        }

        if(isDisabled())
        {
            // enable this contact and update on server side
            setDisabled(false,true);
        }

        if(newGroup == m_currentGroup)
            return;

        if(newGroup == Kopete::Group::topLevel())
        {
            acc->server ()->mainConnection->removeFromGroup (
                acc->groupToGroupId().value(m_currentGroup->displayName()).toLatin1().constData(),
                m_contactSerial.toLatin1().constData ());
            m_currentGroup = newGroup;
            return;
        }

        // if the group don't exist on server side, we create it
        if(!acc->groupToGroupId().contains(newGroup->displayName()))
        {
            acc->server ()->mainConnection->addGroup(newGroup->displayName().toUtf8().constData());
                return;
        }

        // if we have both groups on server side, just move this contact
        if(acc->groupToGroupId().contains(newGroup->displayName())
            && acc->groupToGroupId().contains(m_currentGroup->displayName()))
        {
            acc->server ()->mainConnection->removeFromGroup (
                acc->groupToGroupId().value(m_currentGroup->displayName()).toLatin1().constData(),
                m_contactSerial.toLatin1().constData ());
            acc->server ()->mainConnection->addToGroup (
                acc->groupToGroupId().value(newGroup->displayName()).toLatin1().constData(),
                m_contactSerial.toLatin1().constData ());
            m_currentGroup = newGroup;
            return;
        }
        if(m_currentGroup == Kopete::Group::topLevel() &&
                acc->groupToGroupId().contains(newGroup->displayName()))
        {
            acc->server ()->mainConnection->addToGroup (
                acc->groupToGroupId().value(newGroup->displayName()).toLatin1().constData(),
                m_contactSerial.toLatin1().constData ());
            m_currentGroup = newGroup;
            return;
        }
    }
}
void MeanwhileSession::syncContactsToServer()
{
    HERE;
    struct mwSametimeList *list = mwSametimeList_new();

    /* set up a fallback group for top-level contacts */
    struct mwSametimeGroup *topstgroup = mwSametimeGroup_new(list,
            mwSametimeGroup_DYNAMIC, "People");
    mwSametimeGroup_setOpen(topstgroup, true);

    const QHash<QString, Kopete::Contact *> contacts = account->contacts();
   // Q3DictIterator<Kopete::Contact> it(account->contacts());
    for(QHash<QString, Kopete::Contact *>::const_iterator it = contacts.constBegin();
            it != contacts.constEnd(); ++it ) {
        MeanwhileContact *contact = static_cast<MeanwhileContact *>(it.value());

        /* Find the group that the metacontact is in */
        Kopete::MetaContact *mc = contact->metaContact();
        if (!mc)
            continue;

        Kopete::Group *contactgroup = mc->groups().value(0);
        if (!contactgroup)
            continue;

        if (contactgroup->type() == Kopete::Group::Temporary)
            continue;

        struct mwSametimeGroup *stgroup;
        if (contactgroup->type() == Kopete::Group::TopLevel) {
            stgroup = topstgroup;
        } else  {
            /* find (or create) a matching sametime list group */
            stgroup = mwSametimeList_findGroup(list,
                        contactgroup->displayName().toUtf8().constData());
            if (!stgroup) {
                stgroup = mwSametimeGroup_new(list, mwSametimeGroup_DYNAMIC,
                        contactgroup->displayName().toUtf8().constData());
            }
            mwSametimeGroup_setOpen(stgroup, contactgroup->isExpanded());
            mwSametimeGroup_setAlias(stgroup,
                    contactgroup->pluginData(account->protocol(), "alias").toUtf8().constData());
        }

        QByteArray tmpMeanwhileId = contact->meanwhileId().toUtf8();
        /* now add the user (by IDBlock) */
        struct mwIdBlock id =
            { (gchar*)tmpMeanwhileId.constData(), 0 };
        struct mwSametimeUser *stuser = mwSametimeUser_new(stgroup,
                mwSametimeUser_NORMAL, &id);

        mwSametimeUser_setAlias(stuser, mc->displayName().toUtf8().constData());
    }

    /* store! */
    struct mwPutBuffer *buf = mwPutBuffer_new();
    struct mwStorageUnit *unit = mwStorageUnit_new(mwStore_AWARE_LIST);
    struct mwOpaque *opaque = mwStorageUnit_asOpaque(unit);

    mwSametimeList_put(buf, list);
    mwPutBuffer_finalize(opaque, buf);

    mwServiceStorage_save(storageService, unit, NULL, NULL, NULL);

    mwSametimeList_free(list);
}