Pop3Mail.cpp
上传用户:sempras
上传日期:2007-03-04
资源大小:821k
文件大小:9k
- /*
- * ============================================================================
- * Name : CPop3Mail from Pop3Mail.cpp
- * Part of : EmailExample
- * Created : 09/11/2003 by Forum Nokia
- * Implementation notes:
- * basic statemachine
- *
- * Version : 1.0
- * Copyright: Nokia Corporation
- * ============================================================================
- */
- #include <popcmtm.h> // for POP3
- #include "Pop3Mail.h"
- #include "MsvEmailUtils.h"
- // constant strings for info print messages used when fetching mail.
- _LIT(KIMConnect, "Connecting to mail server");
- _LIT(KIMFetch, "Fetching mail");
- _LIT(KIMDisconnect, "Disconnecting");
- _LIT(KIMComplete, "Operation complete");
- _LIT(KFetchTitle, "Fetch mail");
- _LIT(KFetchMessage, "Getting mail");
- // static creation. Doesn't leave a copy on the clean up stack
- CPop3Mail* CPop3Mail::NewL(TRequestStatus& aStatus,CMsvSession& aMsvSession)
- {
- CPop3Mail* self = new (ELeave) CPop3Mail(aStatus,aMsvSession);
- CleanupStack::PushL(self);
- self->ConstructL();
- CleanupStack::Pop(self);
- return self;
- }
- // constructor
- CPop3Mail::CPop3Mail(TRequestStatus& aStatus,CMsvSession& aMsvSession)
- : CActive(CActive::EPriorityStandard), iObserverStatus(aStatus), iMsvSession(aMsvSession)
- {
- // add to active scheduler
- CActiveScheduler::Add(this);
- }
- // 2nd stage construction
- void CPop3Mail::ConstructL()
- {
- // set intitial object state
- iState=EInitialising;
- // create an empty selection to used laster.
- iMsvSelection = new(ELeave) CMsvEntrySelection();
- iState = EReady;
- Queue();
- }
- // destructor
- CPop3Mail::~CPop3Mail()
- {
- if(iOperation)
- {
- iOperation->Cancel();
- }
- Cancel();
- delete iOperation;
- delete iDialog;
- delete iMsvSelection;
- delete iPop3Mtm;
- delete iPop3Utils;
- }
- // active obejcts RunL functions acts as main state handling routine
- void CPop3Mail::RunL()
- {
- // get a pointer to our observers status
- TRequestStatus* st = &iObserverStatus;
- switch(iState)
- {
- case EReady:
- // tell the observer that the obejct os ready for use
- User::RequestComplete(st,KErrNone);
- break;
- case EConnecting:
- // display connecting message and initiate connection
- User::InfoPrint(KIMConnect);
- ExecuteConnectL();
- break;
- case EGetRemote:
- // display fetching mail message and initiate remote mail fetch
- User::InfoPrint(KIMFetch);
- ExecuteFetchMailL();
- break;
- case EDisconnectRemote:
- // display disconnect message and initiate disconnect
- User::InfoPrint(KIMDisconnect);
- ExecuteDisconnetL();
- break;
- case EDisconnecting:
- // display disconnecting message, reset object and complete observers request
- User::InfoPrint(KIMComplete);
- delete iDialog;
- iDialog=NULL;
- iState = EReady;
- delete iOperation;
- iOperation=NULL;
- User::RequestComplete(st,KErrNone);
- break;
- case ECanceling:
- // cancel the any outstanding operations and reset object.
- iStatus = KRequestPending;
- iState= EReady;
- delete iOperation;
- iOperation=NULL;
- User::RequestComplete(st,KErrCancel);
- default:
- break;
- }
- }
- // Connect to remote mail box
- void CPop3Mail::ExecuteConnectL()
- {
- // reset our status
- iStatus = KRequestPending;
- TBuf8<1> null;
- // execute the pop3 connect command
- MailCommandL(KPOP3MTMConnect,null);
- iState=EGetRemote;
- SetActive();
- }
- // fetch remote mail
- void CPop3Mail::ExecuteFetchMailL()
- {
- // get the msventry for this service (first pop3 service)
- CMsvEntry* service = iMsvSession.GetEntryL(iServiceId);
- CleanupStack::PushL(service);
- // get any children of the service - these are the remote message
- CMsvEntrySelection* children = service->ChildrenL();
- CleanupStack::PushL(children);
- CleanupStack::PopAndDestroy(children);
- CleanupStack::PopAndDestroy(service);
- // reset our status
- iStatus = KRequestPending;
- // setup a mail info buffer
- TImPop3GetMailInfo Pop3GetMailInfo;
- // setting iMaxEmailSize to KMaxTUint tells messaging to get the headers only
- Pop3GetMailInfo.iMaxEmailSize = KMaxTUint;
- // setup which service to download the messages from
- Pop3GetMailInfo.iDestinationFolder = iServiceId;
- // package up the information
- TPckgBuf<TImPop3GetMailInfo> package(Pop3GetMailInfo);
- // execute a pop3 copy mail command
- MailCommandL(KPOP3MTMCopyAllMailWhenAlreadyConnected,package);
- // set new state
- iState=EDisconnectRemote;
- SetActive();
- }
- // disconnect from service
- void CPop3Mail::ExecuteDisconnetL()
- {
- // reset our status
- iStatus = KRequestPending;
- // execute a disconnect from service command
- TBuf8<1> null;
- MailCommandL(KPOP3MTMDisconnect,null);
- iState=EDisconnecting;
- SetActive();
- }
- // interface to engine for fetching remote mail
- void CPop3Mail::FetchRemoteMailL(TMsvId aMailId)
- {
- // panic client is object is not in EReady state.
- __ASSERT_DEBUG(iState==EReady,User::Invariant());
- iMailId=aMailId;
- // reset our status
- iObserverStatus = KRequestPending;
- // create cancel dialog
- CCknCancelDialog* dialog=CCknCancelDialog::NewL(&iDialog,this,KFetchTitle,KFetchMessage);
- // activate cancel dialog
- dialog->RunDlgLD();
- LoadMtmL();
- iState=EConnecting;
- // set active and complete our own request.
- Queue();
- }
- // executes pop3 mail commands
- void CPop3Mail::MailCommandL(TInt aCommand,TDes8& aParams)
- {
- // get the pop3 service id
- if(!iPop3Utils)
- {
- iPop3Utils = CMsvEmailUtils::NewL(iMsvSession);
- }
- TMsvId id = iPop3Utils->GetServiceIdL(KUidMsgTypePOP3);
- // make sure iServiceId is a remote type service
- if(iServiceId != KMsvLocalServiceIndexEntryId)
- {
- iMsvSelection->Reset();
- // append pop3 service entry id
- iMsvSelection->AppendL(id);
- // get the msventry for the service id
- CMsvEntry* service = iMsvSession.GetEntryL(iServiceId);
- CleanupStack::PushL(service);
- // make sure the mtm for the service is a pop3 type
- if(service->Entry().iMtm == KUidMsgTypePOP3)
- {
- // delete and reset any outstanding operation
- if(iOperation)
- {
- iOperation->Cancel();
- }
- delete iOperation;
- iOperation=NULL;
- iOperation=iPop3Mtm->InvokeAsyncFunctionL(aCommand,*iMsvSelection,aParams/*ptr*/,iStatus);
- }
- CleanupStack::PopAndDestroy(service);
- }
- else
- {
- // there is no POP3 mail service defined on the device.
- User::Leave(KErrNotFound);
- }
- }
- // load the pop3 mtm
- void CPop3Mail::LoadMtmL()
- {
- // get the id for the pop3 mtm
- if(!iPop3Utils)
- {
- iPop3Utils = CMsvEmailUtils::NewL(iMsvSession);
- }
- TMsvId id = iPop3Utils->GetServiceIdL(KUidMsgTypePOP3);
- // make sure that there is a pop3 mtm defined in the system
- if(id == KMsvRootIndexEntryId)
- {
- User::Leave(KErrNotFound);
- }
- TMsvEntry tmp;
- // get the entry
- User::LeaveIfError(iMsvSession.GetEntry(id,iServiceId,tmp));
- // create a pop3 mtm from the service id
- iPop3Mtm = iPop3Utils->InstantiatePopClientMtmL(iServiceId);
- }
- // called by the user pressing the cancel button
- void CPop3Mail::CancelOperation()
- {
- // the dialog has destroyed itself so set the pointer to null
- iDialog=NULL;
- // cancel the operation if one exists
- if(iOperation)
- {
- iOperation->Cancel();
- }
- iState=ECanceling;
- if(!IsActive())
- {
- SetActive();
- }
- }
- // msvsession callback handling funtion
- void CPop3Mail::HandleSessionEventL(TMsvSessionEvent aEvent,TAny* /*aArg1*/,TAny* /*aArg2*/,TAny* /*aArg3*/)
- {
- switch(aEvent)
- {
- case MMsvSessionObserver::EMsvServerReady:
- if(iState==EInitialising)
- {
- // the message server is now ready for use.
- // set our state to ready and queue the next state.
- iState = EReady;
- Queue();
- }
- break;
- case EMsvServerTerminated:
- // the server has terminated
- iState=EInitialising;
- break;
- case MMsvSessionObserver::EMsvEntriesCreated:
- case MMsvSessionObserver::EMsvEntriesChanged:
- case MMsvSessionObserver::EMsvEntriesDeleted:
- case MMsvSessionObserver::EMsvEntriesMoved:
- case MMsvSessionObserver::EMsvMtmGroupInstalled:
- case MMsvSessionObserver::EMsvMtmGroupDeInstalled:
- case MMsvSessionObserver::EMsvGeneralError:
- case MMsvSessionObserver::EMsvCloseSession:
- case MMsvSessionObserver::EMsvServerFailedToStart:
- case MMsvSessionObserver::EMsvCorruptedIndexRebuilt:
- case MMsvSessionObserver::EMsvMediaChanged:
- case MMsvSessionObserver::EMsvMediaUnavailable:
- case MMsvSessionObserver::EMsvMediaAvailable:
- case MMsvSessionObserver::EMsvMediaIncorrect:
- case MMsvSessionObserver::EMsvCorruptedIndexRebuilding:
- break;
- }
- }
- void CPop3Mail::DoCancel()
- {}
- // don't handle errors at the moment
- TInt CPop3Mail::RunError(TInt aError)
- {
- return aError;
- }
- // complete our own status.
- // this allows the active scheduler chance to service other active objects before
- // return back to our RunL
- void CPop3Mail::Queue()
- {
- if(!IsActive())
- {
- SetActive();
- }
- TRequestStatus* st= &iStatus;
- User::RequestComplete(st,KErrNone);
- }