📄 internetemailengine.cpp
字号:
/*
* ============================================================================
* Name : CInternetEmailEngine from InternetEmailEngine.cpp
* Part of : InternetEmail
* Created : 25.10.2005 by Forum Nokia
* Implementation notes:
* This is the main class, the engine, of this application.
* class itself is basic state handler that derives from CActive
* and implemenents MMsvSessionObserver interface.
* Initial content was not generated by S60 AppWizard.
* Version : 1.1
* Copyright: Nokia Corporation 2004
* ============================================================================
*/
#include "InternetEmailEngine.h"
// ================= MEMBER FUNCTIONS =======================
//
// -------------------------------------------------
// CInternetEmailEngine* CInternetEmailEngine::NewL(
// MInternetEmailEngineObserver& aObserver )
// static creation
// --------------------------------------------------
//
CInternetEmailEngine* CInternetEmailEngine::NewL( MInternetEmailEngineObserver& aObserver )
{
CInternetEmailEngine* engine=new(ELeave) CInternetEmailEngine( aObserver );
CleanupStack::PushL(engine);
engine->ConstructL();
CleanupStack::Pop(engine);
return engine;
}
// ------------------------------------------------------------------
// CInternetEmailEngine::CInternetEmailEngine(
// MInternetEmailEngineObserver& aObserver )
// : CActive(CActive::EPriorityStandard), iObserver( aObserver )
// actual constructor. Creates ao with standard prioritory
// ------------------------------------------------------------------
//
CInternetEmailEngine::CInternetEmailEngine( MInternetEmailEngineObserver& aObserver )
: CActive(CActive::EPriorityStandard), iObserver( aObserver )
{
// add this object to the active scheduler
CActiveScheduler::Add(this);
}
// ---------------------------------------------
// CInternetEmailEngine::~CInternetEmailEngine()
// Frees reserved resources
// ---------------------------------------------
//
CInternetEmailEngine::~CInternetEmailEngine()
{
if( IsActive() )
{
Cancel();
}
if(iMsvOp)
{
iMsvOp->Cancel();
}
delete iMsvOp;
iMsvOp = NULL;
delete iEntry;
iEntry = NULL;
delete iMtm;
iMtm = NULL;
delete iRemoteEntries;
iRemoteEntries = NULL;
delete iUiReg;
iUiReg = NULL;
delete iClientReg;
iClientReg = NULL;
delete iMsvSession; //Session must be deleted last
iMsvSession = NULL;
}
// ----------------------------------
// CInternetEmailEngine::ConstructL()
// EPOC two phased constructor
// ----------------------------------
//
void CInternetEmailEngine::ConstructL()
{
// create and open a session towards messaging
// the open operation is asynchronous and HandleSessionEventL
// will be called when the open is complete
iMsvSession=CMsvSession::OpenAsyncL(*this);
// Default protocol values
iProtocolType=EProtocolNULL;
iProtocolUid=KUidMsgTypeSMTP;
iIsProtocolSet=EFalse;
iHasImapInbox=EFalse;
//for message handling
iServiceId=KMsvRootIndexEntryId;
iId=KMsvRootIndexEntryId;
iRemoteEntries = new(ELeave) CMsvEntrySelection();
// other member initalization
iState=EInitialising;
}
// ------------------------------------------
// CInternetEmailEngine::CompleteConstructL()
// Called when MsvServer session is ready
// ------------------------------------------
//
void CInternetEmailEngine::CompleteConstructL()
{
iClientReg = CClientMtmRegistry::NewL( *iMsvSession );
iUiReg = CMtmUiRegistry::NewL( *iMsvSession );
}
// --------------------------------------------------------
// void CInternetEmailEngine::RunL()
// The main statemachine
//
// Cornerstone i of this module.
// --------------------------------------------------------
//
void CInternetEmailEngine::RunL()
{
TBuf8<1> null;
switch( iState ) // separation between states
{
// ------------------------------------
// STATE: EConnecting
// usually opens internet connection
// ------------------------------------
case EConnecting:
{
if( iProtocolType==EProtocolPop3 )
{
MailCommandL(KPOP3MTMConnect, null);
}
else
{
//As KIMAP4MTMConnect, but, after connection to
//the IMAP service, starts a background synchronisation.
//The call completes when the connection occurs
//and the synchronisation starts.
MailCommandL(KIMAP4MTMConnectAndSynchronise, null);
}
iState=EFetching;
SetActive(); //returns to active scheduler which waits for our request
}
break;
// ------------------------------------
// STATE: EFetching
// handles the remote mail fetch
// ------------------------------------
case EFetching:
{
// SEPARATION BETWEEN IMAP AND POP
if( iProtocolType==EProtocolPop3 )
{
TImPop3GetMailInfo Pop3GetMailInfo; // setup a mail info buffer
// 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);
}
else
{
//Completes (with KErrNone) only when the background synchronisation
//has finished. It is used to ensure that no UI-initiated
//folder syncing is performed during the background synchronisation.
MailCommandL(KIMAP4MTMWaitForBackground,null);
}
iState=EDisconnecting;
SetActive(); //returns to active scheduler which waits for our request
}
break;
// ------------------------------------
// STATE: EDisconnecting
// Disconnects from remote mailbox
// note: state only for pop as
// it is only offline mode protocol
// ------------------------------------
case EDisconnecting:
{
// SEPARATION BETWEEN IMAP AND POP
if( iProtocolType==EProtocolPop3 )
{
MailCommandL(KPOP3MTMDisconnect,null); // execute a disconnect from service command
}
else
{
//Cancels any operations in progress
//and sends logout messages to server.
//As long as the background synchronisation,
//this will also run any pending delete operations queued while offline.
MailCommandL(KIMAP4MTMDisconnect,null);
}
iState=EDone;
SetActive();
}
break;
// ------------------------------------
// STATE: EDone
// Returns our machine to ready state
// ------------------------------------
case EDone:
{
UpdateRemoteCountL(); // updates model and informs observers
iState = EReady;
if(iMsvOp)
{
iMsvOp->Cancel();
}
delete iMsvOp;
iMsvOp=NULL;
}
break;
// ------------------------------------
// STATE: ECanceling
// waits for mailcommands to cancel
// ------------------------------------
case ECanceling:
{
UpdateRemoteCountL(); // updates model and informs observers
iState= EReady;
delete iMsvOp;
iMsvOp=NULL;
}
break;
default:
//EReady is default, EInitialising, EReadyButNeedsProtocol
//handled elsewhere
break;
}
}
// ----------------------------------------------------------------------------
// void CInternetEmailEngine::MailCommandL( const TInt aCommand,TDes8& aParams)
// Excecutes asynchronous calls for both protocols. Note the specific
// aSelection and aParams given to InvokeAsyncFunctionL (and later to
// TransferCommandL) define the functioncall.
//
// Cornerstone ii of this module.
// ----------------------------------------------------------------------------
void CInternetEmailEngine::MailCommandL( const TInt aCommand, TDes8& aParams)
{
// make sure iServiceId is a remote type service
if( iServiceId != KMsvLocalServiceIndexEntryId )
{
iRemoteEntries->Reset();
iRemoteEntries->AppendL(iServiceId); //aSelection[0] is used in many mtm commands
// get the msventry for the service id
CMsvEntry* service = iMsvSession->GetEntryL(iServiceId);
CleanupStack::PushL(service);
// make sure the mtm for the service is of approriate type
if(service->Entry().iMtm == iProtocolUid)
{
// delete and reset any outstanding operation
if(iMsvOp)
{
iMsvOp->Cancel();
}
delete iMsvOp;
iMsvOp=NULL;
iMsvOp=iMtm->InvokeAsyncFunctionL(aCommand,*iRemoteEntries,aParams/*ptr*/,iStatus);
if( iStatus != KRequestPending )
{
User::Leave( iStatus.Int() ); //leave if error
}
}
CleanupStack::PopAndDestroy(service);
}
else
{
User::Leave(KErrNotFound);
}
}
// -----------------------------------------------------------------------
// void CInternetEmailEngine::HandleSessionEventL(TMsvSessionEvent aEvent,
// TAny* /*aArg1*/,TAny* /*aArg2*/,TAny* /*aArg3*/ )
// Messaging callback from MMsvSessionObserver
//
// Cornerstone iii of this module.
// -----------------------------------------------------------------------
void CInternetEmailEngine::HandleSessionEventL(TMsvSessionEvent aEvent,
TAny* /*aArg1*/,TAny* /*aArg2*/,TAny* /*aArg3*/ )
{
switch(aEvent)
{
// the messaging server is ready
case MMsvSessionObserver::EMsvServerReady:
{
// ---------------------------------------------
// STATE: EInitialising
// Completes constructs and changes state when
// MsvServer session is ready
// ---------------------------------------------
if(iState==EInitialising)
{
iState=EReadyButNeedsProtocol;
CompleteConstructL();
}
}
break;
case MMsvSessionObserver::EMsvEntriesCreated:
case MMsvSessionObserver::EMsvEntriesChanged:
case MMsvSessionObserver::EMsvEntriesDeleted:
case MMsvSessionObserver::EMsvEntriesMoved:
//note: we could notify observers here to redraw if entry
//would be approriate type and we would need more robust ui
case MMsvSessionObserver::EMsvMtmGroupInstalled:
case MMsvSessionObserver::EMsvMtmGroupDeInstalled:
case MMsvSessionObserver::EMsvGeneralError:
case MMsvSessionObserver::EMsvServerFailedToStart:
case MMsvSessionObserver::EMsvCorruptedIndexRebuilt:
case MMsvSessionObserver::EMsvCloseSession:
case MMsvSessionObserver::EMsvServerTerminated:
case MMsvSessionObserver::EMsvMediaChanged:
case MMsvSessionObserver::EMsvMediaUnavailable:
case MMsvSessionObserver::EMsvMediaAvailable:
case MMsvSessionObserver::EMsvMediaIncorrect:
case MMsvSessionObserver::EMsvCorruptedIndexRebuilding:
break;
}
}
// ---------------------------------------------------------------
// void CInternetEmailEngine::DisplayMessageL( const TMsvId &aId )
// here we use the generic mtm ui (view) architecture
// and its views and dialogs to display messages in standard way.
//
// cornerstone iv: of this module.
// ---------------------------------------------------------------
//
void CInternetEmailEngine::DisplayMessageL( const TMsvId &aId )
{
// 1. construct the client MTM
TMsvEntry indexEntry;
TMsvId serviceId;
User::LeaveIfError( iMsvSession->GetEntry(aId, serviceId, indexEntry));
CBaseMtm* mtm = iClientReg->NewMtmL(indexEntry.iMtm);
CleanupStack::PushL(mtm);
// 2. construct the user interface MTM
CBaseMtmUi* uiMtm = iUiReg->NewMtmUiL(*mtm);
CleanupStack::PushL(uiMtm);
// 3. display the message
uiMtm->BaseMtm().SwitchCurrentEntryL(indexEntry.Id());
CMsvOperationWait* waiter=CMsvOperationWait::NewLC();
waiter->Start(); //we use synchronous waiter
CMsvOperation* op = uiMtm->OpenL(waiter->iStatus); //the main "async-sync" call
CleanupStack::PushL(op);
CActiveScheduler::Start(); //notice! nested active scheduler for modal operation
// 4. cleanup for example even members
CleanupStack::PopAndDestroy(4); // op,waiter, mtm, uimtm
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -