void CMtfTestActionCompareAttachment::ExecuteActionL()
	{
	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionCompareAttachment);
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TMsvAttachmentId attachId = ObtainValueParameterL<TMsvAttachmentId>(TestCase(),ActionParameters().Parameter(2));
	HBufC* dataFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(3));

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CMsvStore* store = entry->ReadStoreL();
	CleanupStack::PushL(store);
	
	MMsvAttachmentManager& manager = store->AttachmentManagerL();
	
	RFile fileAttachment = manager.GetAttachmentFileL(attachId);
	CleanupClosePushL(fileAttachment);
	
	CompareFileL(fileAttachment, *dataFilePath);
	
	CleanupStack::PopAndDestroy(&fileAttachment);
	
	CleanupStack::PopAndDestroy(2, entry); // store, entry
	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionCompareAttachment);
	TestCase().ActionCompletedL(*this);
	}
void CMtfTestActionSmtpGetAttachmentFileFromId::RunTestActionL()
	{
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TMsvAttachmentId attachId = ObtainValueParameterL<TMsvAttachmentId>(TestCase(),ActionParameters().Parameter(2));
	HBufC* dataFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(3), NULL);

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
	CleanupStack::PushL(emailMsg);
	
	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
	
	RFile fileAttachment = manager.GetAttachmentFileL(attachId);
	CleanupClosePushL(fileAttachment);
	
	if( dataFilePath != NULL )
		{
		// check of contents of attachment file is requested
		CompareFileL(fileAttachment, *dataFilePath);
		}
	
	CleanupStack::PopAndDestroy(3, entry); // fileAttachment, emailMsg, entry
	}
void CMtfTestActionAddFileAttachmentByHandle::RunTestL()
	{
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	HBufC* paramFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(2));
	HBufC8* paramMimeType = ObtainParameterReferenceL<HBufC8>(TestCase(),ActionParameters().Parameter(3));

	iEntry = paramSession->GetEntryL(messageEntry);
	iStore = iEntry->EditStoreL();
	iAttachment = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
	iAttachment->SetMimeTypeL(*paramMimeType);
	
	RFile fileHandle = OpenFileL(paramSession->FileSession(), *paramFilePath);
	CleanupClosePushL(fileHandle);
	TFileName fileName;
	User::LeaveIfError(fileHandle.Name(fileName));
	iAttachment->SetAttachmentNameL(fileName);
	TInt fileSize = 0;
	User::LeaveIfError(fileHandle.Size(fileSize));
	iAttachment->SetSize(fileSize);
	
	MMsvAttachmentManager& manager = iStore->AttachmentManagerL();
	CActiveScheduler::Add(this);
	SetActive();
	manager.AddAttachmentL(fileHandle, iAttachment, iStatus);
	iAttachmentId = iAttachment->Id();
	iAttachment = NULL; // ownership passed to manager
	
	CleanupStack::Pop(&fileHandle);
	}
void CMtfTestActionGetAttachmentFileFromIndex::RunTestActionL()
	{
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TInt attachIndex = ObtainValueParameterL<TInt>(TestCase(),ActionParameters().Parameter(2));
	HBufC* dataFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(3), NULL);

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CMsvStore* store = entry->ReadStoreL();
	CleanupStack::PushL(store);
	
	MMsvAttachmentManager& manager = store->AttachmentManagerL();
	
	RFile fileAttachment = manager.GetAttachmentFileL(attachIndex);
	CleanupClosePushL(fileAttachment);
	
	if( dataFilePath != NULL )
		{
		// check of contents of attachment file is requested
		CompareFileL(fileAttachment, *dataFilePath);
		}
	
	CleanupStack::PopAndDestroy(3, entry); // fileAttachment, store, entry
	}
// methods from CSendAsEditUtils
void CSendAsTestEditUtils::LaunchEditorL(TMsvId /*aId*/, TRequestStatus& aStatus)
	{

	CDummyObserver* ob1 = new(ELeave) CDummyObserver;
	CleanupStack::PushL(ob1);

	CMsvSession* session = CMsvSession::OpenSyncL(*ob1);
	CleanupStack::PushL(session);

	CMsvEntry* cEntry = CMsvEntry::NewL(*session, KMsvDraftEntryId, 
		TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone,ETrue));
	CleanupStack::PushL(cEntry);

	CMsvEntrySelection* selection = cEntry->ChildrenL();
	CleanupStack::PushL(selection);

	CMsvEntry* cEntry2 = session->GetEntryL(selection->At(0));
	CleanupStack::PushL(cEntry2);

	TMsvEntry entry = cEntry2->Entry();
	entry.SetMtmData3(234567890); // Show we've been called by touching the TMsvEntry.
	cEntry2->ChangeL(entry);
	
	CleanupStack::PopAndDestroy(5, ob1); // cEntry2, selection, cEntry, session, ob1

	iUserStatus = &aStatus;
	aStatus = KRequestPending;
	// wait a few seconds before completing
	iEditTimer->After(KSendAsTestEditWaitTime);
	}
void CMtfTestActionSmtpRemoveEntryAttachmentById::RunTestL()
	{
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TMsvAttachmentId attachId = ObtainValueParameterL<TMsvAttachmentId>(TestCase(),ActionParameters().Parameter(2));

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
	CleanupStack::PushL(emailMsg);
	
	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
	
	CMsvAttachment* attachInfo = manager.GetAttachmentInfoL(attachId);
	CleanupStack::PushL(attachInfo);
	
	// First ensure that the attachment is a file attachment
	if( attachInfo->Type() != CMsvAttachment::EMsvMessageEntry )
		{
		User::Leave(KErrGeneral);
		}
		
	// Get the linked file
	TMsvId msgId = attachInfo->EntryAttachmentId();
	CleanupStack::PopAndDestroy(attachInfo);
		
	// Remove the attachment
	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
	CleanupStack::PushL(waiter);
	manager.RemoveAttachmentL(attachId, waiter->iStatus);
	waiter->StartAndWait();
	User::LeaveIfError(waiter->Result());
	CleanupStack::PopAndDestroy(waiter);	
	
	CleanupStack::PopAndDestroy(2, entry); // email msg, entry

	// Ensure that the message entry still exists
	CMsvEntry* checkEntry = paramSession->GetEntryL(msgId);
	delete checkEntry;
	checkEntry = NULL;
	}
void CMtfTestActionSmtpModifyAttachmentInfo::ExecuteActionL()
	{
	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionSmtpModifyAttachmentInfo);
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TMsvAttachmentId attachmentId = ObtainValueParameterL<TMsvAttachmentId>(TestCase(),ActionParameters().Parameter(2));

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
	CleanupStack::PushL(emailMsg);

	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
	
	CMsvMimeHeaders* mimeHeaders = CMsvMimeHeaders::NewL();
	CleanupStack::PushL(mimeHeaders);

	mimeHeaders->SetContentDescriptionL(KTxtContentDescription());
	mimeHeaders->SetContentBaseL(KTxtContentBase());
	mimeHeaders->SetContentLocationL(KTxtContentLocation());
	mimeHeaders->SetContentIdL(KTxtContentId());
	mimeHeaders->SetContentTypeL(KTxtContentType());
	mimeHeaders->SetContentSubTypeL(KTxtContentSubType());
	mimeHeaders->SetContentDispositionL(KTxtContentDisposition());
	mimeHeaders->ContentTypeParams().AppendL(KTxtParam1());
	mimeHeaders->ContentTypeParams().AppendL(KTxtParam2());
	mimeHeaders->ContentTypeParams().AppendL(KTxtParam3());
	mimeHeaders->ContentDispositionParams().AppendL(KTxtParam4());
	mimeHeaders->ContentDispositionParams().AppendL(KTxtParam5());
	mimeHeaders->ContentDispositionParams().AppendL(KTxtParam6());
	mimeHeaders->XTypeParams().AppendL(KTxtParam7());
	mimeHeaders->XTypeParams().AppendL(KTxtParam8());
	mimeHeaders->XTypeParams().AppendL(KTxtParam9());

	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
	CleanupStack::PushL(waiter);
	CMsvAttachment* attachment = manager.GetAttachmentInfoL(attachmentId);
	CleanupStack::PushL(attachment);
	mimeHeaders->StoreL(*attachment);
	manager.ModifyAttachmentInfoL(attachment, waiter->iStatus);
	CleanupStack::Pop(attachment);
	waiter->StartAndWait();
	User::LeaveIfError(waiter->Result());

	CleanupStack::PopAndDestroy(2, mimeHeaders); // waiter, mimeHeaders
	
	CompareMimeHeadersL(*emailMsg, attachmentId);
	
	CleanupStack::PopAndDestroy(2, entry);//emailMsg, entry

	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionSmtpModifyAttachmentInfo);
	TestCase().ActionCompletedL(*this);
	}
void CMtfTestActionSmtpAddLinkedAttachment::RunTestL()
	{
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	HBufC* paramFilePath   = ObtainParameterReferenceL<HBufC>(TestCase(),ActionParameters().Parameter(2));
	HBufC8* paramMimeType = ObtainParameterReferenceL<HBufC8>(TestCase(),ActionParameters().Parameter(3));

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
	CleanupStack::PushL(emailMsg);
	
	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
	CleanupStack::PushL(waiter);
	
	CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvLinkedFile);
	CleanupStack::PushL(attachment);
	
	attachment->SetMimeTypeL(*paramMimeType);
	
	TParse fileNameParser;
	User::LeaveIfError(fileNameParser.Set(*paramFilePath, NULL, NULL));
	attachment->SetAttachmentNameL(fileNameParser.NameAndExt());
	
	TEntry fileEntry;
	User::LeaveIfError(paramSession->FileSession().Entry( fileNameParser.FullName(), fileEntry));
	attachment->SetSize(fileEntry.iSize);

	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
	
	manager.AddLinkedAttachmentL(*paramFilePath, attachment, waiter->iStatus);
	CleanupStack::Pop(attachment); // ownership passed to manager
	waiter->StartAndWait();
	User::LeaveIfError(waiter->Result());
	CleanupStack::PopAndDestroy(waiter);
	
	attachment = NULL;
	
	TInt attachmentCount = manager.AttachmentCount();
	attachment = manager.GetAttachmentInfoL(attachmentCount - 1);
	CleanupStack::PushL(attachment);
	
	TMsvAttachmentId attachmentId = attachment->Id();
	
	CleanupStack::PopAndDestroy(attachment);
	
	CleanupStack::PopAndDestroy(2, entry); // emailMsg, entry
	
	StoreParameterL<TMsvAttachmentId>(TestCase(),attachmentId,ActionParameters().Parameter(4));
	}
/**
ConstructL()
Used to ensure that MMsvAttachmentManager is not destroyed until we want it to be.
To do this we need to preserve certain objects of CImEmailMessage, which if disappeared
would invalidate MMsvAttachmentManager. But this should be deleted in a controlled manner.

@param aSession
An object of type CMsvSession object.

@param aEntry
An object of type TMsvEntry used to get the entry ID. 
*/
void CAttachmentItem::ConstructL(CMsvSession& aSession, TMsvEntry aEntry)
	{
	iMsvEntry = aSession.GetEntryL(aEntry.Id());

	iMsvEntry->SetEntryL(aEntry.Id());
	iEmailMsg = CImEmailMessage::NewL(*iMsvEntry);
		
	CMsvOperationActiveSchedulerWait* waiter = CMsvOperationActiveSchedulerWait::NewLC();
 	iEmailMsg->GetAttachmentsListL(waiter->iStatus, aEntry.Id(),
								   CImEmailMessage::EAllAttachments, 
								   CImEmailMessage::EThisMessageOnly);
	waiter->Start();

	CleanupStack::PopAndDestroy(1, waiter); //waiter		
	}
// ---------------------------------------------------------------------------
// Second level constructor. Creates the data members to the heap.
// Creates also the folder CMsvEntry.
// ---------------------------------------------------------------------------
//
void CIpsPlgMsgIterator::ConstructL(        
    CMsvSession& aMsvSession,
    const TFSMailMsgId aFolderId,
    const RArray<TFSMailSortCriteria>& aSorting )
    {
    FUNC_LOG;
    iFolderEntry  = aMsvSession.GetEntryL( aFolderId.Id() );
    
    StoreSortCriteriaL(aSorting);
    iMsgSortKey   = new (ELeave) TIpsPlgMsgKey( *iFolderEntry, iSortingCriteria );
    iMsgSwapper   = new (ELeave) TIpsPlgMsgSwap( *iFolderEntry );
    iSortingOn    = 
        ( aSorting.Count() > 0 ) && ( aSorting[0].iField != EFSMailDontCare);
    iMsgMapper    = CIpsPlgMsgMapper::NewL( aMsvSession, iPlugin );
    iMsvSession   = &aMsvSession;
    }
void CMtfTestActionRemoveAllAttachments::ExecuteActionL()
	{
	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionRemoveAllAttachments);
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TInt attachIdFlag = ObtainValueParameterL<TInt>(TestCase(),ActionParameters().Parameter(2), 0);

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CMsvStore* store = entry->EditStoreL();
	CleanupStack::PushL(store);
	
	MMsvAttachmentManager& manager = store->AttachmentManagerL();
	
	TInt attachmentCount = manager.AttachmentCount();
		
	for ( ; attachmentCount > 0 ; attachmentCount-- )
	{
	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
	CleanupStack::PushL(waiter);
		
	if( attachIdFlag == 1 )
		{
		CMsvAttachment* attachInfo = manager.GetAttachmentInfoL(0);
		CleanupStack::PushL(attachInfo);
				
		manager.RemoveAttachmentL(attachInfo->Id(), waiter->iStatus);
		CleanupStack::PopAndDestroy(attachInfo);
		}
	else
		{
		manager.RemoveAttachmentL(0, waiter->iStatus);
		}
	waiter->StartAndWait();
	User::LeaveIfError(waiter->Result());
	CleanupStack::PopAndDestroy(waiter);
	}
	
	store->CommitL();
	
	CleanupStack::PopAndDestroy(2, entry); // store, entry

	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionRemoveAllAttachments);
	TestCase().ActionCompletedL(*this);
	}
// Get the TMsvEntry of the message whose TMsvId is passed from the client..
void CDummyServerSession::GetTMsvEntryL(const RMessage2& aMessage)
	{
	CDummyObserver* ob = new(ELeave) CDummyObserver;
	CleanupStack::PushL(ob);
	
	TPckgBuf<TMsvId> idBuf;
	idBuf = aMessage.Int0();
	
	CMsvSession* session = CMsvSession::OpenSyncL(*ob);
	CleanupStack::PushL(session);
	
	TMsvId entryId = idBuf();
    CMsvEntry* cEntry = session->GetEntryL(entryId);
	CleanupStack::PushL(cEntry); 
	
	TPckgBuf<TMsvEntry> pckg = cEntry->Entry();
	aMessage.WriteL(1,pckg);
	
	CleanupStack::PopAndDestroy(3,ob);//cEntry,session,ob
	}
/**
Stores the Unknown Push message in the message store.

Saves the message data in a new attachment file. This makes it more efficient for the UI
to read the data.

@param	aSession		
Message server session

@param	aParent	
ID of the parent for the new entry. It is the caller's responsibility to ensure 
that the parent ID is correct.

@return	
ID of the new message server entry.

@see	CPushMsgEntryBase::SaveL()
*/
EXPORT_C TMsvId CUnknownPushMsgEntry::SaveL(CMsvSession& aSession, TMsvId aParent)

	{
	CMsvEntry* entryPtr = aSession.GetEntryL(KMsvRootIndexEntryId);
	CleanupStack::PushL(entryPtr);

	// This tidies itself up
	TMsvId newEntry = CPushMsgEntryBase::SaveL(aSession, aParent);
	
#ifdef __MESSAGING_API_V2__
	TRAPD(
		saveError,
		TFileName fileName = ContentFileNameL();
		entryPtr->SetEntryL(newEntry);
		CMsvStore* store = entryPtr->EditStoreL();
		CleanupStack::PushL(store);
		SaveMessageBodyL(store->AttachmentManagerL(), fileName);
		store->CommitL();
		CleanupStack::PopAndDestroy(store);
		)
void CMtfTestActionGetEntry::ExecuteActionL()
{
    TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionAddGetEntry);
    if(TestCase().TestStepResult() == EPass)
    {
        CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
        TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));

        CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
        CleanupStack::PushL(entry);

        entry->SetEntryL(messageEntry);

        StoreParameterL<CMsvEntry>(TestCase(),*entry,ActionParameters().Parameter(2));
        CleanupStack::Pop(entry);

        TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionAddGetEntry);
    }
    TestCase().ActionCompletedL(*this);
}
void CMtfTestActionRemoveFileAttachmentWithDestroy::ExecuteActionL()
	{
	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionRemoveFileAttachmentWithDestroy);
	CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
	TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
	TMsvAttachmentId attachId = ObtainValueParameterL<TMsvAttachmentId>(TestCase(),ActionParameters().Parameter(2));

	CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
	CleanupStack::PushL(entry);
	
	CMsvStore* store = entry->EditStoreL();
	CleanupStack::PushL(store);
	
	MMsvAttachmentManager& manager = store->AttachmentManagerL();
	
	CMsvAttachment* attachInfo = manager.GetAttachmentInfoL(attachId);
	CleanupStack::PushL(attachInfo);
	
	// First ensure that the attachment is a file attachment
	if( attachInfo->Type() != CMsvAttachment::EMsvFile )
		{
		User::Leave(KErrGeneral);
		}
	
	CleanupStack::PopAndDestroy(attachInfo);
	
	CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
	CleanupStack::PushL(waiter);
	manager.RemoveAttachmentL(attachId, waiter->iStatus);
	waiter->StartAndWait();
	User::LeaveIfError(waiter->Result());
	CleanupStack::PopAndDestroy(waiter);	
	
	// destroy the store without commit
	CleanupStack::PopAndDestroy(2, entry); // store, entry
	
	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionRemoveFileAttachmentWithDestroy);
	TestCase().ActionCompletedL(*this); 
	}
void CMtfTestActionAddEntryAttachment::RunTestL()
{
    CMsvSession* paramSession = ObtainParameterReferenceL<CMsvSession>(TestCase(),ActionParameters().Parameter(0));
    TMsvId messageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(1));
    TMsvId attachmentMessageEntry = ObtainValueParameterL<TMsvId>(TestCase(),ActionParameters().Parameter(2));

    CMsvEntry* entry = paramSession->GetEntryL(messageEntry);
    CleanupStack::PushL(entry);

    CMsvStore* store = entry->EditStoreL();
    CleanupStack::PushL(store);

    CMtfAsyncWaiter* waiter = CMtfAsyncWaiter::NewL();
    CleanupStack::PushL(waiter);

    CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvMessageEntry);
    CleanupStack::PushL(attachment);

    TMsvEntry attachmentEntry;
    TMsvId attachmentServiceEntry;
    User::LeaveIfError(paramSession->GetEntry(attachmentMessageEntry, attachmentServiceEntry, attachmentEntry));
    attachment->SetSize(attachmentEntry.iSize);

    MMsvAttachmentManager& manager = store->AttachmentManagerL();

    manager.AddEntryAsAttachmentL(attachmentMessageEntry, attachment, waiter->iStatus);
    CleanupStack::Pop(attachment); // ownership passed to manager
    waiter->StartAndWait();
    User::LeaveIfError(waiter->Result());
    CleanupStack::PopAndDestroy(waiter);

    TMsvAttachmentId attachmentId = attachment->Id();

    store->CommitL();

    CleanupStack::PopAndDestroy(2, entry); // store, entry

    StoreParameterL<TMsvAttachmentId>(TestCase(),attachmentId,ActionParameters().Parameter(3));
}
EXPORT_C void CBaseSmsActiveSocketWatcher::StoreMsgL(CSmsMessage* aSmsMessage, TBool aCheckForSID)
	{
	BIOWATCHERLOG(iWatcherLog.Printf(_L("Bio: StoreMsgL: %S"), &iBioMsgText));

	CleanupStack::PushL(aSmsMessage);

	CMsvSession* session = CMsvSession::OpenSyncL(*this);
	CleanupStack::PushL(session);

	BIOWATCHERLOG(LogMessageL(*aSmsMessage));
	// The trap error code is ignored here. We already have loaded the settings
	// in the SetupL method. Any catastrophic failure would be reported later
	// when the message store is attempted.
	TRAP_IGNORE(RestoreSettingsL(*session));

	PreStoreActionL(*session, *aSmsMessage);

	CMsvEntry* msvEntry = session->GetEntryL(KMsvGlobalInBoxIndexEntryId);
	CleanupStack::PushL(msvEntry);
	TInt systemDrive = RFs::GetSystemDrive();
  	TInt driveUnit = session->CurrentDriveL();
  	 
  	TVolumeInfo volumeInfo;
  	User::LeaveIfError(iFs.Volume(volumeInfo, driveUnit));
  	 
  	 
  	BIOWATCHERLOG(iWatcherLog.Printf(_L("BioNbs: driveUnit: %d "),driveUnit));
  	BIOWATCHERLOG(iWatcherLog.Printf(_L("BioNbs: volumeInfo  : %d"), volumeInfo.iFree));
  	BIOWATCHERLOG(iWatcherLog.Printf(_L("BioNbs: threshold level= : %d"), KSmsThresholdDiskSpaceValue));
 
 
  	//Check if  non-system drive has enough space to store the message
  	if (driveUnit != systemDrive ) 
  	  	{
  	  	BIOWATCHERLOG(iWatcherLog.Printf(_L8("Low Memory")));
  	  	
  	  	TInt value;
  	  	TInt err = RProperty::Get(KUidSystemCategory,KUidPSDiskSpaceMonitorKeyType, value);
  	  	
  	  	BIOWATCHERLOG(iWatcherLog.Printf(_L("RProperty Get Value: %d "),err));
		
		if (volumeInfo.iFree < KSmsThresholdDiskSpaceValue)
  	  	 	{
  	  	 	if(value == ESmsDiskSpaceAvailable)
  	  	 	    {
  	  	 	    User::LeaveIfError(RProperty::Set(KUidSystemCategory,KUidPSDiskSpaceMonitorKeyType, ESmsDiskSpaceFull));
  	  	 	    }
  	  	 	User::Leave(KErrDiskFull);
  	  	 	}
  	  	 else
  	  	 	{
  	  	 	if(value == ESmsDiskSpaceFull)
  	  	 	    {
  	  	 	    User::LeaveIfError(RProperty::Set(KUidSystemCategory,KUidPSDiskSpaceMonitorKeyType, ESmsDiskSpaceAvailable));
  	  	 	    }
  	  	 	}
  	  	}


	if( CanStoreMessage() )
		{
		TBool retainReplaceMessage = ETrue;
		ReplaceTypeMessageL(*msvEntry, *aSmsMessage, retainReplaceMessage);
		CleanupStack::Pop(3, aSmsMessage); 
		CleanupStack::PushL(session); //guaranteed not leave because of the 3 previous POPs
		CleanupStack::PushL(msvEntry);

		if( retainReplaceMessage )
			CreateMessageL(*msvEntry, aSmsMessage, aCheckForSID); //destroys the CSmsMessage
		else
			delete aSmsMessage; //destroy the CSmsMessage as CreateMessageL() would have done

		CleanupStack::PopAndDestroy(2, session);
		}
	else
		{
		CleanupStack::PopAndDestroy(3, aSmsMessage);
		BIOWATCHERLOG(iWatcherLog.Printf(_L8("Not Creating Message")));
		}
	}
/* Test to check the new API
** CopyStoreL(TInt aDrive, TMsvOp aOperationId, TRequestStatus& aStatus) 
*/
LOCAL_C void TestCopyStoreOperationL()
	{
	CMsgsTestUtils::SetDriveL(EDriveC);
    
	CSessionObserver* ob = new(ELeave)CSessionObserver;
	CleanupStack::PushL(ob);
       
	// Set session in observer
	CMsvSession* session = CMsvSession::OpenAsyncL(*ob);
	ob->iSession = session; 
	CleanupStack::PushL(session);
    
	CActiveScheduler::Start();
	test(ob->iType==MMsvSessionObserver::EMsvServerReady);
	
	//Create an entry in the mail
	
	CMsvEntry* entry=session->GetEntryL(KMsvDraftEntryId);
	CleanupStack::PushL(entry);
	// create entry to work under
	TMsvEntry folder;
	folder.iType = KUidMsvFolderEntry;
	folder.iMtm = KUidMsvLocalServiceMtm;
	folder.iServiceId = KMsvLocalServiceIndexEntryId;
	_LIT(KTestDescription,"A Description");
	_LIT(KTestDetails,"A Details");
	folder.iDescription.Set(KTestDescription);
	folder.iDetails.Set(KTestDetails);
	entry->CreateL(folder);
	entry->SetEntryL(folder.Id());
	CMsvStore* store=entry->EditStoreL();
	CleanupStack::PushL(store);
	RMsvWriteStream stream;
	stream.AssignLC(*store,TUid::Uid(0x1000));
	stream.WriteL(KTestDescription);
	stream.CommitL();
	store->CommitL();
	CleanupStack::PopAndDestroy(3);
            
	CTestActive* active = new(ELeave)CTestActive;
	CleanupStack::PushL(active);
     
	// Test Copy MailStore 
	active->StartL();
	CMsvOperation* operation = NULL;
	TDriveUnit unit =(EDriveD);
	operation = session->CopyStoreL(unit, active->iStatus);
	test(operation->Mtm() == KUidMsvServerMtm);
	CleanupStack::PushL(operation);
	
	CActiveScheduler::Start();
	
	//Retrieve progress 
	TPckgBuf<TMsvCopyProgress> package;
	package.Copy(operation->ProgressL());
	test(package().iError == KErrNone);
		
	//delete session;
	CleanupStack::PopAndDestroy(4);
		
	//Check that both the source and destination
	//MailStore directory structure is same
	
	CMsgsTestUtils::WaitForServerClose();
	
	CDir 		 *srcDir;
	CDir 	     *destDir;
	_LIT(intro, "Testing the directory structure");
	_LIT(KEntryname,"%S");
	
	theUtils->Printf(intro);
	
	TFileName dest = unit.Name();
	dest.Append(KMsvDefaultFolder2);

	User::LeaveIfError(theUtils->FileSession().GetDir(dest, KEntryAttDir|KEntryAttNormal, ESortByName, destDir));
	
	unit =EDriveC;
	TFileName src = unit.Name();
	src.Append(KMsvDefaultFolder2);

	User::LeaveIfError(theUtils->FileSession().GetDir(src, KEntryAttDir|KEntryAttNormal, ESortByName, srcDir));
	
	TInt counter=0;
	TEntry srcEntry;
	TEntry destEntry;

	while (counter<srcDir->Count())
		{
		srcEntry = (*srcDir)[counter];
		destEntry = (*destDir)[counter];
		//Check the sizes 
		test(srcEntry.iSize == destEntry.iSize);
		//Check the names
		test(srcEntry.iName == destEntry.iName);
		//Print out the names on the log
		theUtils->Printf(KEntryname,&(srcEntry.iName));
		counter++;
		}
		
	delete srcDir;
	delete destDir;
	
	}
void CSMSSender::CreateSMSMessageL(const TDesC& aText, const TDesC& aAddress)
/**
	Prepare SMS specific objects ready to send via ESOCK
	@param aText buffer containing ascii contents of message to send
	@param aAddress buffer with telephone number of SMS receiver 
*/
	{
#ifndef __WINS__
	TSmsAddr smsAddr;
    smsAddr.SetSmsAddrFamily(ESmsAddrSendOnly);
    smsAddr.SetPort(smsAddr.Port() + 1);//ycf
    __LOGSTR_TOFILE("sockent bind");
	iSocket.Bind(smsAddr);

	CSmsBuffer* smsBuffer = CSmsBuffer::NewL();
	//CleanupStack::PushL(smsBuffer) is NOT used because CSmsMessage takes ownership of our buffer :-)
	CSmsMessage* smsMsg = CSmsMessage::NewL(iFs, CSmsPDU::ESmsSubmit, smsBuffer);
	CleanupStack::PushL(smsMsg);
	
	TSmsUserDataSettings smsSettings;
    smsSettings.SetAlphabet(TSmsDataCodingScheme::ESmsAlphabetUCS2);
	smsSettings.SetTextCompressed(EFalse);
	smsMsg->SetUserDataSettingsL(smsSettings);
	
	TBuf<KMaxAddressSize> toAddress;
	toAddress.Copy(aAddress);
	smsMsg->SetToFromAddressL(toAddress);

	//Get service centre address.
	// The method used here assumes the SMS settings are provisioned, which is true in known cases.
	// There are alternative partner-only APIs, however this allow this source to be kept public
	#ifdef EKA2
		CSmsSettings* smsSCSettings = CSmsSettings::NewL();
		CleanupStack::PushL(smsSCSettings);
    	CSmsAccount* smsAccount=CSmsAccount::NewLC();
    	smsAccount->LoadSettingsL(*smsSCSettings);
 		// index of the default service centre address for this service
 		TInt defIndex;
		User::LeaveIfError(defIndex = smsSCSettings->DefaultServiceCenter());
 		// Get the service center address
		CSmsServiceCenter&  scAddr = smsSCSettings->GetServiceCenter(defIndex);
	
		TPtrC theAddress=scAddr.Address();
		HBufC* serviceCentreAddress=HBufC::NewLC(theAddress.Length());
		*serviceCentreAddress=theAddress;
		smsMsg->SmsPDU().SetServiceCenterAddressL(*serviceCentreAddress);
		CleanupStack::PopAndDestroy(serviceCentreAddress);//
		CleanupStack::PopAndDestroy(smsAccount);
		CleanupStack::PopAndDestroy(smsSCSettings);	
				
	#else
		TMsvId		serviceId;
		CObserver* pObserver = new (ELeave) CObserver();
		CleanupStack::PushL(pObserver);
		CMsvSession* pSession = CMsvSession::OpenSyncL(*pObserver);
		CleanupStack::PushL(pSession);
		TSmsUtilities::ServiceIdL(*pSession, serviceId, KUidMsgTypeSMS);
		CMsvEntry* service = pSession->GetEntryL(serviceId);
		CleanupStack::PushL(service);
		CMsvStore* msvstore = service->ReadStoreL();
		CleanupStack::PushL(msvstore);
		CSmsSettings* smsSCSettings = CSmsSettings::NewL();
		CleanupStack::PushL(smsSCSettings);
		smsSCSettings->RestoreL(*msvstore);
		TInt defIndex;
		User::LeaveIfError(defIndex = smsSCSettings->DefaultSC());
		defIndex = smsSCSettings->DefaultSC();
 		// Get the default service center address
		CSmsNumber&  scAddr = smsSCSettings->SCAddress(defIndex);
		TPtrC theAddress=scAddr.Address();
		HBufC* serviceCentreAddress=HBufC::NewLC(theAddress.Length());
		*serviceCentreAddress=theAddress;
		smsMsg->SmsPDU().SetServiceCenterAddressL(*serviceCentreAddress);
		CleanupStack::PopAndDestroy(serviceCentreAddress);//
		CleanupStack::PopAndDestroy(smsSCSettings); //smsSettings
		CleanupStack::PopAndDestroy(msvstore);
		CleanupStack::PopAndDestroy(service);
		CleanupStack::PopAndDestroy(pSession);
		CleanupStack::PopAndDestroy(pObserver);		
	#endif
	
	//convert to wide
	HBufC* payload = HBufC::NewL(aText.Length());
	CleanupStack::PushL(payload);
	TPtr pPayload=payload->Des();
	pPayload.Copy(aText); //copy from narrow to wide and convert
	smsBuffer->InsertL(0, pPayload); //copies payload
	RSmsSocketWriteStream writeStream(iSocket);
	CleanupClosePushL(writeStream);
	writeStream << *smsMsg; // remember << operator _CAN_ leave
	__LOGSTR_TOFILE("write stream commit");
	writeStream.CommitL();
	CleanupStack::PopAndDestroy(&writeStream);
	CleanupStack::PopAndDestroy(2);//smsMsg, payload	
#endif
	}
LOCAL_C void DoExecuteL()
	{
	CActiveScheduler* activeScheduler = new (ELeave) CActiveScheduler();
	CleanupStack::PushL(activeScheduler);
	CActiveScheduler::Install(activeScheduler);
	
	// Create Session
	CSessionObserver* sessionObserver;
	sessionObserver = new (ELeave) CSessionObserver;
	CleanupStack::PushL(sessionObserver);
		
	CMsvSession* session = CMsvSession::OpenSyncL(*sessionObserver);
	CleanupStack::PushL(session);
	
	// Set the message entry to global Outbox folder
	CMsvEntry* msvEntry = session->GetEntryL(KMsvGlobalOutBoxIndexEntryId);
	CleanupStack::PushL(msvEntry);
	
	// Get the selection of Pigeon messages present in Outbox
	CMsvEntrySelection* selection = msvEntry->ChildrenWithMtmL(KUidMsgTypePigeon);
	CleanupStack::PushL(selection);
	
	// Schedule it to send immediately - use default settings.
	CMsvScheduleSettings* settings = CMsvScheduleSettings::NewL();
	CleanupStack::PushL(settings);

	RScheduler scheduler;
	TSchedulerItemRef ref;
	
	CMsvScheduleSend::ConnectAndRegisterL(scheduler, *settings);
	CleanupClosePushL(scheduler);
	
	TInt selCount = selection->Count();
	
	for( TInt index = 0; index < selCount; ++index )
		{
		msvEntry->SetEntryL(selection->At(index));
			
		if(msvEntry != NULL)
			{
			TMsvEntry entry = msvEntry->Entry();
					
			ref.iHandle = KErrNotFound;
			
			FindOrCreateScheduleL(scheduler, entry.iDate, *settings, ref);

			User::LeaveIfError(scheduler.DisableSchedule(ref.iHandle));
		
			TMsvSchedulePackage package;
			package.iPollProgress = 3000000;
			package.iCommandId = ESendScheduledL;
			package.iId = selection->At(index);
				
			TTaskInfo info;

			CMsvScheduleSend::ScheduleEntryL(scheduler, ref, package, info);
		
			User::LeaveIfError(scheduler.EnableSchedule(ref.iHandle));
			}
		}
	CleanupStack::PopAndDestroy(7, activeScheduler);
	}