void CBBListenerImpl::ConstructL()
{
	CALLSTACKITEM_N(_CL("CBBListenerImpl"), _CL("ConstructL"));

	iCurrentBuf=CXmlBufExternalizer::NewL(2048);
	iFreeBuf=CXmlBufExternalizer::NewL(2048);
	iSerializedData=HBufC8::NewL(2048);

	iTimer=CTimeOut::NewL(*this);
	iTimer->Wait(KInitialAckInterval);

	iFactory=BBDataFactory();
	iTuple=new (ELeave) CBBTuple(iFactory);
	iToAck=CList<TUint>::NewL();

	iSeenDb=CDb::NewL(AppContext(), _L("REMOTE_IDS"), EFileWrite);
	iSeenIds=CSeenIds::NewL(iSeenDb->Db());

	ConnectL();
	SetFilterL();
	iGetState=EWaitForNotify;
	
	CActiveScheduler::Add(this);
}
void CBBListenerImpl::CheckedRunL()
{
	CALLSTACKITEM_N(_CL("CBBListenerImpl"), _CL("CheckedRunL"));

#ifdef __WINS__
	//User::Leave(KErrGeneral);
#endif

	Log(_L("CheckedRunL()"));

	{
		if (iStatus.Int()!=KErrNone && iStatus.Int()!=EDeleteNotification) {
			if (iStatus.Int()==KClientBufferTooSmall) {
				iSerializedData->Des().Zero();
				iSerializedData=iSerializedData->ReAllocL(iSerializedData->Des().MaxLength()*2);
				iAsyncErrorCount=0;
				GetOrWaitL();
				return;
			} else if (iStatus.Int()==KErrNotFound) {
				if (iGetState==EWaitForNotify) {
					User::Leave(KErrNotFound);
				} else if (iGetState==EGettingListener) {
					GetLoca();
				} else if (iGetState==EGettingLoca) {
					SetFilterL();
					WaitForNotify();
				}
				return;
			} 
			if (iAsyncErrorCount>5) User::Leave(iStatus.Int());
			++iAsyncErrorCount;
			ConnectL();
			SetFilterL();
			WaitForNotify();
			return;
		}
	}

	if (iFullArgs.iTupleName==KListenerStop) {
		TRequestStatus s;
		iBBClient.Delete(iFullArgs.iId, s);
		User::WaitForRequest(s);
		if (iUnackedCount>0 || iSent==0) {
			iPendingStop=ETrue;
		} else {
			if (!iStopTimer) iStopTimer=CTimeOut::NewL(*this);
			iStopTimer->Wait(10);
		}
		WaitForNotify();
		return;
	}
	MBBData* d=0;
	if (iStatus!=EDeleteNotification) {
		RDesReadStream rs(*iSerializedData);
		CleanupClosePushL(rs);	
		TTypeName read_type=TTypeName::IdFromStreamL(rs);
		{
			d=iFactory->CreateBBDataL(read_type, KEvent, iFactory);
			CleanupPushBBDataL(d);
		}
		{
			d->InternalizeL(rs);
		}
		CleanupStack::Pop();
		CleanupStack::PopAndDestroy();
	}

	{
		iTuple->iData.SetValue(d);
		iTuple->iTupleMeta.iModuleUid()=iFullArgs.iTupleName.iModule.iUid;
		iTuple->iTupleMeta.iModuleId()=iFullArgs.iTupleName.iId;
		iTuple->iTupleMeta.iSubName=iFullArgs.iSubName;
		if ( iFullArgs.iTupleName == KLastKnownGpsTuple ) {
			iTuple->iTupleId()=0;
		} else {
			iTuple->iTupleId()=iFullArgs.iId;
		}
		iTuple->iExpires()=iFullArgs.iLeaseExpires;
		
		iFreeBuf->Zero();
		iTuple->IntoXmlL(iFreeBuf);
		iFreeBuf->Characters(_L("\n"));
	}

	iUnackedCount++;
	if (iReadyToWrite) {
		iWaiting[0]=iFreeBuf;
		WriteL();
		GetOrWaitL();
	} else {
		iWaiting[1]=iFreeBuf;
		iPendingWrite=ETrue;
	}
}
void CTestContactsPBAPExport::ExportContactsL()
	{
	TInt err = KErrNone;
	// Retrieve the file name to which contact item is to be exported
   	RFs fsSession;
	RFileWriteStream writeStream;

	// connect to file system
	User::LeaveIfError(fsSession.Connect());
	CleanupClosePushL(fsSession);

	GetInputFromIni();

   	// Makes one or more directories.
   	fsSession.MkDirAll(iExportTo);

	// Replaces a single file with another
	User::LeaveIfError(writeStream.Replace(fsSession, iExportTo, EFileWrite));

	INFO_PRINTF1(_L("Exporting Contact....."));

	// Existing database
   	TPtrC databaseFile(_L("C:contactDb.cdb"));

	CContactDatabase* dBase = NULL;
	CContactIdArray* idArray = NULL;

	// Open the existing database
	dBase = CContactDatabase::OpenL(databaseFile);
	CleanupStack::PushL(dBase);

	// Create Utility class object, to export the contact from database
	CTestStep* self = static_cast<CTestStep*>(this);
	iExportObj = new(ELeave) CContactsPBAPExportUtilityClass(self);

	SetFilterL();

	CCntFilter* exportFilter = CCntFilter::NewL();
	CleanupStack::PushL(exportFilter);

	// Get all the contacts from the database to export
	exportFilter->SetContactFilterTypeCard(ETrue);
	dBase->FilterDatabaseL(*exportFilter);
	idArray = exportFilter->iIds;
	CleanupStack::PushL(idArray);

	if(iDamageDb)
		{
		#ifdef _DEBUG
		#ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
		TRAPD(err1,dBase->DamageDatabaseL(0x666));
		if(err1 == KErrNone)
			{
			TRAPD(err,iExportObj->ExportvCardL(dBase, iStandard, idArray, writeStream, iContactFilter));
			INFO_PRINTF2(_L("Err:%d"),err);
			if(err == KErrNotReady)
				{
				SetTestStepResult(EPass);
				}
			else
				{
				SetTestStepResult(EFail);
				}
			if(dBase->IsDamaged())
				{
				dBase->RecoverL();
				}
			}
		else
			{
			INFO_PRINTF2(_L("Could not damage database Err:"),err1);
			}
		#else
			SetTestStepResult(EPass);
		#endif
		#endif
		}
    else
	    {
	    if(iInvalidFileSystem)
		    {
		    #ifdef _DEBUG
		    fsSession.SetErrorCondition(KErrNotReady);
		    TRAPD(err,iExportObj->ExportvCardL(dBase, iStandard, idArray, writeStream, iContactFilter));
			if(err == KErrNotReady)
				{
				SetTestStepResult(EPass);
				}
			else
				{
				SetTestStepResult(EFail);
				}
			fsSession.SetErrorCondition(KErrNone);
			#endif
			}
	    else
			{
			if(!iSetOOM)
				{
				if(idArray->Count() > 0)
					{
					for(TInt i=0; i<idArray->Count() ; i++)
						{
						TInt dCount = dBase->CountL();
						if(i>=dCount)
							{
							break;
							}

						// temporary array used to export one contact at a time
						CContactIdArray* tempIdArray = CContactIdArray::NewL();
						CleanupStack::PushL(tempIdArray);
						tempIdArray->AddL((*idArray)[i]);
						TRAPD(err,iExportObj->ExportvCardL(dBase, iStandard, tempIdArray, writeStream, iContactFilter));

						if(err != KErrNone )
							{
							if(err != KErrNotFound)
								{
								SetTestStepError(err);
								}
							}

						CleanupStack::PopAndDestroy(tempIdArray);
						}
					}
				else
					{
					if(idArray->Count()==0)
						{
						TRAPD(err,iExportObj->ExportvCardL(dBase, iStandard, idArray, writeStream, iContactFilter));
						if(err != KErrNone)
							{
							SetTestStepError(err);
							}
						}
					}

				}
			else
				{
				TInt tryCount = 1;
				for ( ;; )
					{
					__UHEAP_SETFAIL(RHeap::EDeterministic, tryCount);
					TRAP(err, iExportObj->ExportvCardL(dBase, iStandard, idArray, writeStream, iContactFilter));

					if ( err == KErrNone )
						{
						__UHEAP_RESET;
						INFO_PRINTF1(_L("OOM testing of CContactDatabase::ExportSelectedContactsL Api is done"));
						break;
						}
					if ( err != KErrNoMemory )
						{
						INFO_PRINTF2(_L("The unexpected error code is:%d"),err);
						SetTestStepResult(EFail);
						break;
						}
					__UHEAP_SETFAIL(RHeap::ENone, 0);
					tryCount++;
					}
				}
			}
	    }

	CleanupStack::Pop(idArray);
	CleanupStack::PopAndDestroy(exportFilter);

	INFO_PRINTF1(_L("Exported Contact"));
	writeStream.CommitL();
	writeStream.Close();

	INFO_PRINTF2(_L("Total number of contacts in database %d "), dBase->CountL());

	// Cleanup
	CleanupStack::PopAndDestroy(dBase);
    CleanupStack::PopAndDestroy(&fsSession);
	}