email/pop3andsmtpmtm/popservermtm/src/POPSTOPPOP.CPP
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2006-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 //
       
    15 
       
    16 #include <e32std.h>
       
    17 
       
    18 #include "POPSMBX.H"
       
    19 #include "POPS.H"
       
    20 #include "POPSOP.H" //CImPop3Operations
       
    21 
       
    22 // includes for IMCV stuff
       
    23 #include <imcvrecv.h>
       
    24 #include <logwrap.h>
       
    25 #include <logwraplimits.h>
       
    26 
       
    27 #include <bautils.h>
       
    28 #include <msvutils.h>
       
    29 #include <imcvutil.h>
       
    30 
       
    31 #include <imcm.rsg>
       
    32 
       
    33 #include "PopsTopPop.h"
       
    34 #include "POPS.H"
       
    35 #include "POPSOP.H" //CImPop3Operations
       
    36 #include "POPSMBX.H"
       
    37 
       
    38 #include "POPS.PAN" // imrc's own panic codes
       
    39 
       
    40 #include "mobilitytestmtmapi.h"
       
    41 
       
    42 // IMRC Panic function
       
    43 GLREF_C void Panic(TPopsPanic aPanic);
       
    44 
       
    45 CImPop3TopPopulate* CImPop3TopPopulate::NewL(const CMsvEntrySelection& aRemoteEntry, CMsvServerEntry& aLocalEntry, TInt aLimit, CImPop3Session* aPop3Session, RFs& anFs, CImLogMessage* aLogMessage, TBool aDisconnectedMode, RArray<TUint>& aRemoteSizes)
       
    46 	{
       
    47 	CImPop3TopPopulate* self = new (ELeave) CImPop3TopPopulate( aLocalEntry, aLimit, aPop3Session, anFs, aLogMessage, aDisconnectedMode, aRemoteSizes);
       
    48 
       
    49 	CleanupStack::PushL(self);
       
    50 	self->ConstructL(aRemoteEntry);
       
    51 	CleanupStack::Pop(self);
       
    52 	return self;
       
    53 	}
       
    54 
       
    55 CImPop3TopPopulate::~CImPop3TopPopulate()
       
    56 	{
       
    57 	iRemoteSizes.Close();
       
    58 	delete iTop;
       
    59 	delete iSource;
       
    60 	delete iRecvConverter;
       
    61 	}
       
    62 
       
    63 void CImPop3TopPopulate::StartL(TRequestStatus& aStatus)
       
    64 	{
       
    65 	iMsgCtr=0;
       
    66 	iSavedError = KErrNone;
       
    67 	// set up progress obj.
       
    68 	iProgress.iTotalMsgs=iSource->Count();
       
    69 	iProgress.iMsgsToProcess=iProgress.iTotalMsgs;
       
    70 	
       
    71 	// Do a quick tally on the size of messages which are to be copied / moved.
       
    72 	iProgress.iTotalSize = 0;
       
    73 	for(TInt i = 0; i < iProgress.iTotalMsgs; ++i)
       
    74 		{
       
    75 		iDestination.SetEntry(iSource->At(i));
       
    76 		if(!iDestination.Entry().Complete())
       
    77 			{
       
    78 			iProgress.iTotalSize += iRemoteSizes[i];
       
    79 			}
       
    80 		}
       
    81 	
       
    82 
       
    83 	if(iProgress.iTotalMsgs>0)
       
    84 		{
       
    85 		delete iTop;
       
    86 		iTop=NULL;
       
    87 		iTop=CImPop3Top::NewL(iPopSession,iRecvConverter,EFalse);
       
    88 		DoRetrieveL();
       
    89 		Queue(aStatus);
       
    90 		}
       
    91 	else
       
    92 		{
       
    93 		aStatus = KRequestPending;
       
    94 		TRequestStatus* pS=&aStatus;
       
    95 		User::RequestComplete(pS,KErrNone);
       
    96 		}
       
    97 	}
       
    98 
       
    99 // ****************************************************************************************** 
       
   100 // Resume function called by the POP Server MTM, once it has completed Migrating to new bearer
       
   101 // 
       
   102 // ******************************************************************************************	
       
   103 void CImPop3TopPopulate::ResumeL(CImPop3Session* aPopSession, TRequestStatus& aStatus)
       
   104 	{
       
   105 	iMigratingToNewBearer = EFalse;
       
   106 	iPopSession = aPopSession;
       
   107 
       
   108 	delete iRecvConverter;
       
   109 	iRecvConverter = NULL;
       
   110 	iRecvConverter = CImRecvConvert::NewL( iFs, &iDestination, KUidMsgTypePOP3, iServiceId);
       
   111 	iRecvConverter->SetCaf(*iPopSession->GetCafL(iFs));
       
   112 	
       
   113 	delete iTop;
       
   114 	iTop=NULL;
       
   115 	iTop=CImPop3Top::NewL(iPopSession,iRecvConverter,EFalse);
       
   116 	
       
   117 	// Set the MIME parser to point at the first message to be processed.
       
   118 	iRecvConverter->SetMsvId((*iSource)[iMsgCtr]);
       
   119 		
       
   120 	DoRetrieveL();
       
   121 	Queue(aStatus);
       
   122 	}	
       
   123 
       
   124 TPop3Progress CImPop3TopPopulate::Progress()
       
   125 	{
       
   126 	if(iPopTopPopState==EPopTopPopRetrieving)
       
   127 		{
       
   128 		// Get the Copy/Move Progress
       
   129 		iProgress.iBytesDone=iTop->Progress();
       
   130 		}
       
   131 	else if (iPopTopPopState == EPopTopPopLogging)
       
   132 		{
       
   133 		// We are doing Logging so the move/copy is complete
       
   134 		iProgress.iBytesDone = iProgress.iTotalBytes;
       
   135 		}
       
   136 	else
       
   137 		{
       
   138 		iProgress.iBytesDone=0;
       
   139 		iProgress.iTotalBytes=0;
       
   140 		}
       
   141 	return iProgress;
       
   142 	}
       
   143 
       
   144 CImPop3TopPopulate::CImPop3TopPopulate(CMsvServerEntry& aLocalEntry, TInt aLimit, CImPop3Session* aPop3Session, RFs& anFs, CImLogMessage* aLogMessage, TBool aDisconnectedMode, RArray<TUint>& aRemoteSizes)
       
   145 	: CMsgActive( KMsgPop3RefreshMailboxPriority ), iDestination(aLocalEntry), iPopSession(aPop3Session),iFs(anFs), iLogMessage(aLogMessage), iDisconnectedMode(aDisconnectedMode), iTopLimit(aLimit), iRemoteSizes(aRemoteSizes)
       
   146 	{
       
   147 	}
       
   148 
       
   149 void CImPop3TopPopulate::ConstructL( const CMsvEntrySelection& aRemoteSelection)
       
   150 	{
       
   151 	// make our copy of aRemoteEntry
       
   152 	iSource = aRemoteSelection.CopyL();
       
   153 	iServiceId = iDestination.Entry().Id();
       
   154 
       
   155 	// assume Entry passed in is set to service id (IMCV needs to set new entries to this)
       
   156 	iRecvConverter = CImRecvConvert::NewL( iFs, &iDestination, KUidMsgTypePOP3, iServiceId);
       
   157 	iRecvConverter->SetCaf(*iPopSession->GetCafL(iFs));
       
   158 	iPopTopPopState=EPopTopPopRetrieving;
       
   159 	iProcessComplete=EFalse;
       
   160 
       
   161 	// set recv conv. 
       
   162 	if (iSource->Count())
       
   163 		{
       
   164 		// Set the MIME parser to point at the first message to be populated.
       
   165 		iRecvConverter->SetMsvId((*iSource)[0]);
       
   166 		}	
       
   167 	CActiveScheduler::Add(this);	  // Add CImPop3CopyMove to scheduler's queue
       
   168 	}
       
   169 
       
   170 void CImPop3TopPopulate::DoRetrieveL()
       
   171 	{
       
   172 	iAlreadyComplete = EFalse;
       
   173 	iMessageMarkedForDelete = EFalse;
       
   174 	TRequestStatus* pS = &iStatus;
       
   175 	TMsvId msgId=(*iSource)[iMsgCtr];
       
   176 	// iDestination will later be changed back to the destination entry, if required
       
   177 	TInt err = iDestination.SetEntry(msgId);
       
   178 	TMsvEmailEntry entry = iDestination.Entry();
       
   179 	if(err != KErrNone)
       
   180         {
       
   181 		SetActive();
       
   182 		User::RequestComplete(pS,err);
       
   183         }
       
   184 	else if (entry.Operation() && (entry.DisconnectedOperation() == EDisconnectedDeleteOperation))
       
   185 		{
       
   186 		iMessageMarkedForDelete = ETrue;
       
   187 		SetActive();
       
   188 		User::RequestComplete(pS, KErrNone);
       
   189 		}
       
   190 	else if ((entry.Complete()))
       
   191 		{
       
   192 		iAlreadyComplete = ETrue;
       
   193 		SetActive();
       
   194 		User::RequestComplete(pS, KErrNone);
       
   195 		}
       
   196 	else
       
   197 		{
       
   198 		iProgress.iTotalBytes = iRemoteSizes[iMsgCtr];
       
   199 		// reset entry to destination context if the current entry is not meant to be populated
       
   200 		User::LeaveIfError(iDestination.SetEntry(iDestId));
       
   201 		iRetrieveMessage = ETrue;
       
   202 
       
   203 		//If the message is not found complete the request.
       
   204 		if (!iTop->SetMessageAndLines(msgId,iTopLimit)) 
       
   205 			{
       
   206 			iAlreadyComplete = ETrue;
       
   207 			SetActive();
       
   208 			User::RequestComplete(pS, KErrNone);
       
   209 			}
       
   210 		else
       
   211 			{
       
   212 			iCurrentMsgSize = iRemoteSizes[iMsgCtr];
       
   213 			iTop->StartL(iStatus);
       
   214 			SetActive();
       
   215 			MOBILITY_TEST_MTM_STATE(iServiceId, KMobilityTestMtmStatePopRetrieving);
       
   216 			}
       
   217 		}
       
   218 	}
       
   219 
       
   220 void CImPop3TopPopulate::RetrievalCompleteL()
       
   221 	{
       
   222 	iProcessComplete=ETrue;
       
   223 	}
       
   224 
       
   225 void CImPop3TopPopulate::DoRunL()
       
   226 	{
       
   227 	// If we have already have an error then we have just been logging the failure.
       
   228 	// The failure has been logged so we can leave now.
       
   229 	User::LeaveIfError(iSavedError);
       
   230 
       
   231 	// There was no error so handle the completion of either the logging or the retrieve operations.
       
   232 	TRAP(iSavedError, RunLProcessingL());
       
   233 
       
   234 	if (iSavedError != KErrNone)
       
   235 		// If an error has occured then we need to clean up the message and log the failure before bailing out.
       
   236 		{
       
   237 		if(iRetrieveMessage)
       
   238 			{
       
   239 			if ((!iAlreadyComplete) && (!iMessageMarkedForDelete))
       
   240 				{
       
   241 				TRAPD(err,iRecvConverter->MessageCompleteL());
       
   242 
       
   243 	 			if(err != KErrNone)
       
   244 					{
       
   245 					// something is wrong with the message delete and report back
       
   246 					err = iDestination.SetEntry(iDestId);
       
   247 					if (err == KErrNone)
       
   248 						{
       
   249 						err = iDestination.DeleteEntry(iTop->EntryId());
       
   250 						}
       
   251 					}
       
   252 				}
       
   253 			}
       
   254 
       
   255 		// The message is logged as 'failed' if iSavedError is not KErrNone.
       
   256 		LogFetchedMessageL();
       
   257 		}
       
   258 	}
       
   259 
       
   260 void CImPop3TopPopulate::DoComplete(TInt& /*aCompleteStatus*/)
       
   261 	{
       
   262 	}
       
   263 
       
   264 void CImPop3TopPopulate::DoCancel()
       
   265 	{
       
   266 	iPopSession->SetOpNotPending();
       
   267 
       
   268 	if(iPopTopPopState==EPopTopPopRetrieving)
       
   269 		{
       
   270 		iTop->Cancel();
       
   271 		}
       
   272 
       
   273 	CMsgActive::DoCancel();
       
   274 	}
       
   275 
       
   276 void CImPop3TopPopulate::RunLProcessingL()
       
   277 	{
       
   278 	switch (iPopTopPopState)
       
   279 		{
       
   280 		case EPopTopPopRetrieving:
       
   281 			{
       
   282 		    TInt amountLeft = 0;
       
   283 			if(iRetrieveMessage)
       
   284 				{
       
   285 				TBool partial=ETrue;
       
   286 				if (iTop->Progress()>=TUint(iCurrentMsgSize))
       
   287 					{
       
   288 					partial=EFalse;
       
   289 					}
       
   290 				
       
   291 				if ((!iAlreadyComplete) && (!iMessageMarkedForDelete))
       
   292 					{
       
   293 					iRecvConverter->MessageCompleteL(partial);
       
   294 					}
       
   295 				amountLeft=(iCurrentMsgSize)-iTop->Progress()+iRecvConverter->DeletedAttachmentSize();
       
   296 				if (amountLeft<0)
       
   297 					{
       
   298 					amountLeft=0;
       
   299 					}
       
   300 
       
   301 				TMsvId msgId=(*iSource)[iMsgCtr];
       
   302 				if(iDestination.SetEntry(msgId)==KErrNone)
       
   303 					{
       
   304 					TMsvEmailEntry header=iDestination.Entry();
       
   305 					TBool changed=EFalse;
       
   306 					if((header.Unread()))
       
   307 						{
       
   308 						header.SetNew(EFalse);
       
   309 						changed=ETrue;
       
   310 						}
       
   311 
       
   312 					if(partial!=EFalse)
       
   313 						{
       
   314 						// We have used TOP so the entry will be partial, unless we have got the whole thing.
       
   315 						header.SetPartialDownloaded(partial);
       
   316 						changed=ETrue;
       
   317 						}
       
   318 
       
   319 					if(changed != EFalse)
       
   320 						{
       
   321 						User::LeaveIfError(iDestination.ChangeEntry(header));
       
   322 						}						
       
   323 					if(iDestination.SetEntry(iDestId)!=KErrNone)						
       
   324 					{
       
   325 					User::Leave(KPop3ProblemWithRemotePopServer);
       
   326 					}
       
   327 					}
       
   328 
       
   329 				}
       
   330 
       
   331 			iRecvConverter->WritePartialFooterL(amountLeft);
       
   332 
       
   333 			iDestination.SetEntry(iDestId);	// make sure not trying to delete current context
       
   334 			LogFetchedMessageL();
       
   335 			}
       
   336 			break;
       
   337 		case EPopTopPopLogging:
       
   338 			// If the message has been logged then move on and download the next one.
       
   339 			{
       
   340 			--iProgress.iMsgsToProcess;
       
   341 			if(++iMsgCtr<iSource->Count())
       
   342 				{
       
   343 				// If we are migrating halt the operation here
       
   344 				if (iMigratingToNewBearer)
       
   345 					{			
       
   346 					// If we don't SetActive, the RunL will complete the request
       
   347 					return;
       
   348 					}
       
   349 				else
       
   350 					{
       
   351  					iRecvConverter->ResetL();
       
   352 					iRecvConverter->SetMsvId((*iSource)[iMsgCtr]);
       
   353 					DoRetrieveL(); //add it to the collection;
       
   354 					iPopTopPopState = EPopTopPopRetrieving;
       
   355 					}
       
   356 				}
       
   357 			}
       
   358 			break;
       
   359 		default:
       
   360 			break;
       
   361 		}
       
   362 	}
       
   363 
       
   364 //
       
   365 // Helper function to set up the iLogEntry object which is invoked after the copy/move.
       
   366 //
       
   367 void CImPop3TopPopulate::LogFetchedMessageL()
       
   368 	{
       
   369 	iPopTopPopState = EPopTopPopLogging;
       
   370 	if (iLogMessage)
       
   371 		{
       
   372 // Get the header information for the message that is to be copied or moved.
       
   373 		User::LeaveIfError(iDestination.SetEntry((*iSource)[iMsgCtr]));
       
   374 
       
   375 		// Get the 'Fetch' string from the logging string table.
       
   376 		TBuf<KLogMaxSharedStringLength> fetchString;
       
   377 		iLogMessage->GetString(fetchString, R_LOG_DIR_FETCHED);
       
   378 
       
   379 // Set up the log event.
       
   380 		iLogMessage->LogEvent().SetEventType(KLogMailEventTypeUid);
       
   381 
       
   382 		iLogMessage->LogEvent().SetDirection(fetchString);
       
   383 		iLogMessage->LogEvent().SetRemoteParty(iDestination.Entry().iDetails);
       
   384 		iLogMessage->LogEvent().SetSubject(iDestination.Entry().iDescription);
       
   385 		iLogMessage->LogEvent().SetLink(iDestId);
       
   386 
       
   387 		if (iSavedError != KErrNone)
       
   388 			{
       
   389 			TBuf<KLogMaxSharedStringLength> failedString;
       
   390 			iLogMessage->GetString(failedString, R_LOG_DEL_FAILED);
       
   391 			iLogMessage->LogEvent().SetStatus(failedString);
       
   392 			}
       
   393 
       
   394 // Run the iLogMessage operation.
       
   395 		iLogMessage->Start(iDestination.Entry().iError, iStatus);
       
   396 		SetActive();
       
   397 		}
       
   398 	else
       
   399 		{
       
   400 		TRequestStatus* status = &iStatus;
       
   401 		SetActive();
       
   402 		User::RequestComplete(status, KErrNone);
       
   403 		}
       
   404 	}
       
   405 
       
   406  
       
   407 // ****************************************************************************************** 
       
   408 // This is called by the POP Server MTM when it starts Migrating Bearer
       
   409 // 
       
   410 // ******************************************************************************************
       
   411 void CImPop3TopPopulate::Pause()
       
   412 	{
       
   413 	// Set the Migration flag
       
   414 	iMigratingToNewBearer = ETrue;
       
   415 	}
       
   416 
       
   417 // ****************************************************************************************** 
       
   418 // This is called by the POP Server MTM when it starts Migrating Bearer
       
   419 // 
       
   420 // ******************************************************************************************	
       
   421 void CImPop3TopPopulate::CancelAllowResume()
       
   422 	{
       
   423 	iMigratingToNewBearer = ETrue;
       
   424 	
       
   425 	// Cancel the copying of the current message  and decrement counters 
       
   426 	// so we can restart from this message onwards when we have migrated.
       
   427 	// Use the normal cancel, as we really need to cancel here.
       
   428 	Cancel();		
       
   429 	}