email/imap4mtm/imapmailstore/src/cmsvplainbodytextqueueentry.cpp
changeset 31 ebfee66fde93
parent 0 72b543305e3a
equal deleted inserted replaced
30:6a20128ce557 31:ebfee66fde93
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // CMSVPLAINBODYTEXTQUEENTRY.CPP
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "cmsvplainbodytextqueueentry.h"
       
    19 #include "cimaputils.h"
       
    20 #include "cimaplogger.h"
       
    21 
       
    22 #include <cmsvplainbodytext.h>
       
    23 
       
    24 const TInt KOutOfOrderChunks = 3;
       
    25 
       
    26 
       
    27 
       
    28 CPlainBodyChunkInfo* CPlainBodyChunkInfo::NewL(TInt aChunkNumber, HBufC8* aData)
       
    29 	{
       
    30 	CPlainBodyChunkInfo* self =	new(ELeave) CPlainBodyChunkInfo(aChunkNumber,aData);
       
    31 	CleanupStack::PushL(self);
       
    32 	self->ConstructL();
       
    33 	CleanupStack::Pop();
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 /**
       
    38 Constructor for CPlainBodyChunkInfo.
       
    39 */
       
    40 CPlainBodyChunkInfo::CPlainBodyChunkInfo(TInt aChunkNumber, HBufC8* aData)
       
    41 :iChunkNumber(aChunkNumber), iData(aData)
       
    42 	{
       
    43 	}
       
    44 
       
    45 void CPlainBodyChunkInfo::ConstructL()
       
    46 	{
       
    47 	
       
    48 	}
       
    49 
       
    50 /**
       
    51 Destructor for CPlainBodyChunkInfo.
       
    52 */	
       
    53 CPlainBodyChunkInfo::~CPlainBodyChunkInfo()
       
    54 	{
       
    55 	delete iData;
       
    56 	}
       
    57 
       
    58 /**
       
    59 Constructor.
       
    60 */
       
    61 CMsvPlainBodyTextQueueEntry::CMsvPlainBodyTextQueueEntry(TInt aTotalChunks, CImapMailStore& aParent, CMsvServerEntry& aServerEntry, CImapSettings& aImapSettings, CFetchBodyInfo& aFetchBodyInfo, TInt aLogId, MMailStoreObserver& aObserver, MChunkOutOfOrderObserver& aChunkObserver, TBool aBinaryCap)
       
    62 	:CQueueEntryBase(aFetchBodyInfo.PartId(), EPlainTextBody, aParent, aServerEntry, aObserver),
       
    63 	iTotalChunks(aTotalChunks),
       
    64 	iEncoding(aFetchBodyInfo.ContentTransferEncoding()),
       
    65 	iCharsetId(aFetchBodyInfo.CharsetId()),
       
    66 	iBodyPartRemainingSize(aFetchBodyInfo.BodyPartRemainingSize()),
       
    67 	iImapSettings(aImapSettings),
       
    68 	iLogId(aLogId),
       
    69 	iChunkObserver(aChunkObserver)
       
    70 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)		
       
    71 	, iBinaryCap(aBinaryCap)
       
    72 #endif
       
    73 	{
       
    74 	//to ignore compilation warnings
       
    75 	aBinaryCap = aBinaryCap;
       
    76 	}
       
    77 
       
    78 
       
    79 /**
       
    80 Destructor.
       
    81 */		
       
    82 CMsvPlainBodyTextQueueEntry::~CMsvPlainBodyTextQueueEntry()
       
    83 	{
       
    84 	delete iStoreUtilities;
       
    85 	iDataArray.ResetAndDestroy();
       
    86 	iDataArray.Close();
       
    87 	delete iPlainBodyText;
       
    88 	delete iStore;
       
    89 	delete iDecodedData;
       
    90 	}
       
    91 	
       
    92 /**
       
    93 NewL Factory functions.
       
    94 */
       
    95 CMsvPlainBodyTextQueueEntry* CMsvPlainBodyTextQueueEntry::NewL(TInt aTotalChunks, CImapMailStore& aParent, CMsvServerEntry& aServerEntry, CImapSettings& aImapSettings, CFetchBodyInfo& aFetchBodyInfo, TInt aLogId, MMailStoreObserver& aObserver, MChunkOutOfOrderObserver& aChunkObserver, TBool aBinaryCap)
       
    96 	{
       
    97 	CMsvPlainBodyTextQueueEntry* self=new(ELeave)CMsvPlainBodyTextQueueEntry(aTotalChunks, aParent, aServerEntry, aImapSettings, aFetchBodyInfo, aLogId, aObserver, aChunkObserver, aBinaryCap);
       
    98 	CleanupStack::PushL(self);
       
    99 	self->ConstructL();
       
   100 	CleanupStack::Pop();
       
   101 	return self;
       
   102 	}
       
   103 
       
   104 /**
       
   105 The Two Phase constructor
       
   106 */
       
   107 void CMsvPlainBodyTextQueueEntry::ConstructL()
       
   108 	{
       
   109 	BaseConstructL();
       
   110 	
       
   111 	// Get the default charset
       
   112 	CCnvCharacterSetConverter* characterConverter = CCnvCharacterSetConverter::NewL();
       
   113 	CleanupStack::PushL(characterConverter);
       
   114 	CImConvertCharconv* charConv = CImConvertCharconv::NewL(*characterConverter, CImapUtils::GetRef().Fs());
       
   115 	CleanupStack::PushL(charConv);
       
   116 
       
   117 	TUint defaultCharset =	charConv->SystemDefaultCharset();
       
   118 	CleanupStack::PopAndDestroy(2, characterConverter);
       
   119 	if(iCharsetId == KUidMsvCharsetNone)
       
   120 		{
       
   121 		iCharsetId = defaultCharset;	
       
   122 		}
       
   123 		
       
   124 	iStoreUtilities=CStoreUtilities::NewL(iEncoding,iCharsetId,CImapUtils::GetRef().Fs());
       
   125 	iServerEntry.SetEntry(iId);
       
   126 	iStore = iServerEntry.EditStoreL();
       
   127 	TInt is8Bit = iImapSettings.Store8BitData();
       
   128 	iPlainBodyText = iStore->InitialisePlainBodyTextForWriteL(is8Bit,iCharsetId,defaultCharset);
       
   129 	}
       
   130 	
       
   131 	
       
   132 
       
   133 /**
       
   134 Adds a chunk of body data to the data array and sorts the array
       
   135 @param aData 			the body data, ownership  is taken.
       
   136 @param aChunkNumber 	the order number in which the chunk makes up the whole data.
       
   137 @return TBool			indicates whether the FETCH command need to be sent to server
       
   138 						ETrue : Send the FETCH command.
       
   139 						EFalse: Do not send the FETCH command.
       
   140 */	
       
   141 	
       
   142 TBool CMsvPlainBodyTextQueueEntry::AddChunkL(HBufC8* aData, TInt aChunkNumber, TInt aExtraFetchRequestCount)
       
   143 	{
       
   144 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)			
       
   145 	if(iBinaryCap)
       
   146 		{	
       
   147 		iExtraFetchRequestCount=aExtraFetchRequestCount;
       
   148 		}
       
   149 #else
       
   150 	//to ignore compilation warnings
       
   151 	aExtraFetchRequestCount = aExtraFetchRequestCount;
       
   152 #endif
       
   153 	TBool sendCommand = ETrue;
       
   154 	__LOG_FORMAT((iLogId, "CMsvPlainBodyTextQueueEntry::AddChunkL chunk number = %d",aChunkNumber));
       
   155 	CleanupStack::PushL(aData);
       
   156 	//make sure the chunk is within range.
       
   157 	__ASSERT_DEBUG(aChunkNumber<iTotalChunks,TImapServerPanic::ImapPanic(TImapServerPanic::EMailStoreDataChunkOutOfRange));
       
   158 	//append the chunk
       
   159  	CPlainBodyChunkInfo* chunkInfo = CPlainBodyChunkInfo::NewL(aChunkNumber, aData);
       
   160   	CleanupStack::Pop(aData);
       
   161  	CleanupStack::PushL(chunkInfo);
       
   162  
       
   163  	TLinearOrder<CPlainBodyChunkInfo>compareChunks(CompareChunks); 	
       
   164  	iDataArray.InsertInOrderL(chunkInfo,compareChunks);
       
   165  
       
   166  	if(iDataArray.Count() >= KOutOfOrderChunks)
       
   167  		{
       
   168  		sendCommand = EFalse;
       
   169  		}
       
   170  		
       
   171     CleanupStack::Pop(chunkInfo);
       
   172     
       
   173     if(!IsActive())
       
   174 		{
       
   175   	  	CompleteSelf();	
       
   176 		}
       
   177 	return sendCommand;
       
   178 	}
       
   179 
       
   180 	
       
   181 /** 
       
   182 Used to sort chunks of data by chunk number.
       
   183 @param aChunkBodyInfo1 	the first chunk in the comparision
       
   184 @param aChunkBodyInfo2 	the second chunk in the comparision
       
   185 @return TInt			the difference between the first chunks number and the second chunks number.
       
   186 */	
       
   187 TInt CMsvPlainBodyTextQueueEntry::CompareChunks(const CPlainBodyChunkInfo& aChunkBodyInfo1, const CPlainBodyChunkInfo& aChunkBodyInfo2)
       
   188 	{
       
   189 	return aChunkBodyInfo1.iChunkNumber-aChunkBodyInfo2.iChunkNumber;	
       
   190 	}
       
   191 
       
   192 TInt CMsvPlainBodyTextQueueEntry::RunError(TInt aError)
       
   193 	{
       
   194 	return CQueueEntryBase::RunError(aError);
       
   195 	}
       
   196 	
       
   197 	
       
   198 	
       
   199 /**
       
   200 Uses CMsvPlainBodyText to asynchronousy store the requests data object.
       
   201 @param 
       
   202 @return
       
   203 */
       
   204 void CMsvPlainBodyTextQueueEntry::RunL()
       
   205 	{
       
   206 	User::LeaveIfError(iStatus.Int());
       
   207 	if(iReadyToRemoveChunk)
       
   208 		{
       
   209 		delete iDecodedData;
       
   210 		iDecodedData=NULL;
       
   211 		iChunkObserver.EnableSendFetch();
       
   212 		iReadyToRemoveChunk=EFalse;	
       
   213 		}
       
   214 
       
   215 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)		
       
   216 	if(iBinaryCap && iExtraFetchRequestCount > 0)
       
   217 		{
       
   218 		iTotalChunks-=iExtraFetchRequestCount;
       
   219 		//reset iExtraFetchRequestCount to zero
       
   220 		iExtraFetchRequestCount=0;
       
   221 		}
       
   222 #endif
       
   223 
       
   224 	//if we have appended all the chunks then store the data.
       
   225 	if(iNextExpectedChunk==iTotalChunks)
       
   226 		{
       
   227 		__LOG_FORMAT((iLogId, "CMsvPlainBodyTextQueueEntry::RunL Commiting body data..."));
       
   228 		iPlainBodyText->CommitL();
       
   229 		//call back to the observer as the operation is complete
       
   230 		StoreComplete(KErrNone);	
       
   231 		//request can be removed from the queue
       
   232 		iParent.RemoveFromQueueAndDelete(this);	
       
   233 		}
       
   234 	else if(iDataArray.Count()>0 && iDataArray[0]->iChunkNumber==iNextExpectedChunk )
       
   235 		{
       
   236 		++iNextExpectedChunk;
       
   237 		//is this the last chunk?
       
   238 		TBool lastChunk=(iNextExpectedChunk==iTotalChunks);
       
   239 		HBufC8* decodedData = NULL;
       
   240 #if (defined SYMBIAN_EMAIL_CAPABILITY_SUPPORT)		
       
   241 		if(iBinaryCap)
       
   242 			{
       
   243 			const TPtrC8& bodyData = *(iDataArray[0]->iData);
       
   244 			decodedData = HBufC8::NewL(bodyData.Length());
       
   245 			// Message body is downloaded using Fetch Binary and the data is decoded by the IMAP mail server
       
   246 			// Nothing to do, just copy data
       
   247 			decodedData->Des().Append(bodyData);						
       
   248 			}
       
   249 		else
       
   250 #endif
       
   251 			{
       
   252 			// create a new buffer, decodedData and push it to CleannupStack because AttachFooterInfoL will 
       
   253 			// pop decodedData first and pushes it  when it returns.
       
   254 			__LOG_FORMAT((iLogId, "CMsvPlainBodyTextQueueEntry::RunL Decoding..."));
       
   255 			decodedData = iStoreUtilities->DecodeL(*(iDataArray[0]->iData), lastChunk);				
       
   256 			}
       
   257 			
       
   258 		__ASSERT_DEBUG(decodedData!=NULL, TImapServerPanic::ImapPanic(TImapServerPanic::EMailStoreDecodeDataNull));					
       
   259 		CleanupStack::PushL(decodedData); 
       
   260 		
       
   261 		//if this is the last chunk and if was a partial download then add footer
       
   262 		if(iBodyPartRemainingSize && iNextExpectedChunk==iTotalChunks)
       
   263 			{
       
   264 			__LOG_FORMAT((iLogId, "CMsvPlainBodyTextQueueEntry::RunL AttachFooterInfoL..."));
       
   265 			iStoreUtilities->AttachFooterInfoL(iBodyPartRemainingSize, iImapSettings, decodedData);
       
   266 			}
       
   267 			
       
   268 		// pop decodedData since the ownership will be transferred to iDecodedData.
       
   269 		CleanupStack::Pop(decodedData);			
       
   270 		__ASSERT_DEBUG(iDecodedData == NULL, TImapServerPanic::ImapPanic(TImapServerPanic::EMailStoreDecodeDataNotNull));
       
   271 		
       
   272 		// transferring ownership to iDecodedData.
       
   273 		iDecodedData = decodedData;
       
   274 		
       
   275 		__LOG_FORMAT((iLogId, "CMsvPlainBodyTextQueueEntry::RunL StoreChunk..."));
       
   276 		iPlainBodyText->StoreChunkL(*iDecodedData, iStatus);
       
   277 		SetActive();
       
   278 		
       
   279 		//delete the origninal encoded chunk
       
   280 		CPlainBodyChunkInfo* chunkInfo=iDataArray[0];
       
   281 		iDataArray.Remove(0);
       
   282 		delete chunkInfo;	
       
   283 		iReadyToRemoveChunk=ETrue;
       
   284 		}
       
   285 	}