email/pop3andsmtpmtm/imapservermtm/test/src/IMapClient.cpp
changeset 25 84d9eb65b26f
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
       
     1 // Copyright (c) 2005-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 "imapclient.h"
       
    17 
       
    18 _LIT(KLoopbackAddress,"127.0.0.1");
       
    19 //_LIT(KMSExchangeAddress,"10.22.64.6");
       
    20 
       
    21 void TDummySessionObserver::HandleSessionEventL(TMsvSessionEvent /*aEvent*/, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/)
       
    22 	{
       
    23 	}
       
    24  
       
    25 TImapAccount CActiveImapClient::GetAccount()
       
    26 	{
       
    27 	return iImapAccount;
       
    28 	}
       
    29 
       
    30 CActiveImapClient::CActiveImapClient() : CActive(EPriorityStandard)
       
    31 	{
       
    32 	}
       
    33 
       
    34 EXPORT_C CActiveImapClient* CActiveImapClient::NewL(MImapTestEventHandler* aOwner,TBool aFetchWholeMessage)
       
    35 	{
       
    36 	CActiveImapClient* self = NewLC(aOwner,aFetchWholeMessage);
       
    37 	CleanupStack::Pop(self);
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 EXPORT_C CActiveImapClient* CActiveImapClient::NewLC(MImapTestEventHandler* aOwner,TBool aFetchWholeMessage)
       
    42 	{
       
    43 	CActiveImapClient* self = new(ELeave) CActiveImapClient;
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL(aOwner,aFetchWholeMessage);
       
    46 	return self;
       
    47 	}
       
    48 	
       
    49 CActiveImapClient::~CActiveImapClient()
       
    50 	{
       
    51 	Cancel();
       
    52 	delete iMsvOperation;	
       
    53 	delete iSelection;
       
    54 	delete iClientMtm;
       
    55 	delete iClientRegistry;	
       
    56 	delete iSession;
       
    57 	delete iSessionObserver;
       
    58 	delete iAccounts;
       
    59 	}
       
    60 	
       
    61 	
       
    62 void CActiveImapClient::ConstructL(MImapTestEventHandler* aOwner,TBool aFetchWholeMessage)
       
    63 	{
       
    64 	CActiveScheduler::Add(this); 
       
    65 	iFetchWholeMessage=aFetchWholeMessage;
       
    66 	iOwner=aOwner;
       
    67 	//create an account
       
    68 	iAccounts = CEmailAccounts::NewL();	
       
    69 	CreateImapAccountL();
       
    70 	// Session observer. Needed to create a session
       
    71 	iSessionObserver = new (ELeave) TDummySessionObserver;
       
    72 	// Session. Needed to create a client registry.
       
    73 	iSession=CMsvSession::OpenSyncL(*iSessionObserver);
       
    74 	// Client registry. Needed to get the MTM component
       
    75 	iClientRegistry=CClientMtmRegistry::NewL(*iSession,KMsvDefaultTimeoutMicroSeconds32);
       
    76 	//get the client mtm
       
    77 	iClientMtm=iClientRegistry->NewMtmL(KUidMsgTypeIMAP4);	
       
    78 	
       
    79 	// Load the serial comms device drivers.  If this is not done,
       
    80 	// connecting via NT-RAS returns KErrNotFound (-1).
       
    81 	TInt driverErr;
       
    82 	driverErr=User::LoadPhysicalDevice(PDD_NAME);
       
    83 	if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists)
       
    84 		User::Leave(driverErr);
       
    85 	driverErr=User::LoadLogicalDevice(LDD_NAME);
       
    86 	if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists)
       
    87 		User::Leave(driverErr);
       
    88 	}
       
    89 	
       
    90 void CActiveImapClient::CreateImapAccountL()
       
    91 	{
       
    92 	//create objects and initialise with the defaults
       
    93 	CImImap4Settings* imap4Settings=new(ELeave)CImImap4Settings;
       
    94 	CleanupStack::PushL(imap4Settings);
       
    95 	CImSmtpSettings* smtpSettings=new(ELeave)CImSmtpSettings;
       
    96 	CleanupStack::PushL(smtpSettings);
       
    97 	CImIAPPreferences* imapIap = CImIAPPreferences::NewLC();
       
    98 	CImIAPPreferences* smtpIap = CImIAPPreferences::NewLC();
       
    99 	
       
   100 	iAccounts->PopulateDefaultImapSettingsL(*imap4Settings, *imapIap);
       
   101 	iAccounts->PopulateDefaultSmtpSettingsL(*smtpSettings, *smtpIap);
       
   102 	
       
   103 	//override some of the defaults
       
   104 	imap4Settings->SetPasswordL(_L8("davids"));
       
   105 	imap4Settings->SetLoginNameL(_L8("davids"));
       
   106 	imap4Settings->SetServerAddressL(KLoopbackAddress);
       
   107 	imap4Settings->SetFolderPathL(_L8(""));
       
   108 	imap4Settings->SetSynchronise(EUseLocal);
       
   109 	
       
   110 	//create the account
       
   111 	iImapAccount = iAccounts->CreateImapAccountL(_L("TestAccount"), *imap4Settings, *imapIap, EFalse);
       
   112 	TSmtpAccount smtpAccount;
       
   113 	smtpAccount = iAccounts->CreateSmtpAccountL(iImapAccount, *smtpSettings, *smtpIap, EFalse);
       
   114 
       
   115 	//clean up
       
   116 	CleanupStack::PopAndDestroy(smtpIap);
       
   117 	CleanupStack::PopAndDestroy(imapIap);	
       
   118 	CleanupStack::PopAndDestroy(smtpSettings);
       
   119 	CleanupStack::PopAndDestroy(imap4Settings); 
       
   120 	}
       
   121 
       
   122 void CActiveImapClient::StartL()
       
   123 	{
       
   124 	
       
   125 	//if we are to collect the whole email then the next operation will be to fetch it
       
   126 	iNextStep=(!iFetchWholeMessage? EDisconnect:EFetch);
       
   127 		
       
   128 	iSelection = new (ELeave) CMsvEntrySelection;
       
   129 	TPckg<MMsvImapConnectionObserver*> param(this);
       
   130 	//select the imap service entry
       
   131 	iSelection->AppendL(iImapAccount.iImapService);
       
   132 	//make the service entry the current context
       
   133 	iClientMtm->SwitchCurrentEntryL(iImapAccount.iImapService);
       
   134 	//sync the account
       
   135 	iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMConnectAndSyncCompleteAfterFullSync,*iSelection,param,iStatus);			
       
   136 	SetActive();
       
   137 	}
       
   138 	
       
   139 void CActiveImapClient::DoCancel()
       
   140 	{	
       
   141 	__ASSERT_DEBUG(iMsvOperation!=NULL, User::Panic(_L("CActiveImapClient"), -3));
       
   142 	iMsvOperation->Cancel();
       
   143 	}
       
   144 
       
   145 void CActiveImapClient::RunL()
       
   146 	{
       
   147 
       
   148 	TPckg<MMsvImapConnectionObserver*> param(this);
       
   149 	TImImap4GetMailInfo imap4GetMailInfo;
       
   150 	imap4GetMailInfo.iMaxEmailSize = KMaxTInt;
       
   151 	imap4GetMailInfo.iDestinationFolder = KMsvGlobalInBoxIndexEntryIdValue;
       
   152 	imap4GetMailInfo.iGetMailBodyParts = EGetImap4EmailBodyTextAndAttachments;
       
   153 			
       
   154 	TPckgBuf<TImImap4GetMailInfo> package(imap4GetMailInfo);
       
   155 	
       
   156 	CMsvEntry* inboxEntry=NULL;
       
   157 	CMsvEntry* imapService=NULL;
       
   158 	TMsvSelectionOrdering ordering;
       
   159 	
       
   160 	switch(iNextStep)
       
   161 		{
       
   162 		//connect and sync	
       
   163 		case EDisconnect:
       
   164 			iNextStep=EComplete;
       
   165 			delete iMsvOperation;
       
   166 			iMsvOperation=NULL;
       
   167 			iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMDisconnect,*iSelection,param,iStatus);	
       
   168 			SetActive();
       
   169 			break;	
       
   170 			
       
   171 		case EFetch:
       
   172 			iNextStep=EDisconnect;
       
   173 			// message in remote inbox					
       
   174 			//create a CMsvEntry for the service entry
       
   175 			imapService = CMsvEntry::NewL(*iSession, iImapAccount.iImapService,ordering);
       
   176 			CleanupStack::PushL(imapService);
       
   177 			//retrieve the inbox from the imap service entry
       
   178 			CMsvEntrySelection* msvEntrySelection;
       
   179 			msvEntrySelection=imapService->ChildrenL();
       
   180 			CleanupStack::PushL(msvEntrySelection);
       
   181 			//create a CMsvEntry for the inbox entry
       
   182 			inboxEntry = CMsvEntry::NewL(*iSession, (*msvEntrySelection)[0],ordering);
       
   183 			CleanupStack::PopAndDestroy(msvEntrySelection);
       
   184 			CleanupStack::PushL(inboxEntry);
       
   185 			//retrieve the message from the inboxEntry service entry
       
   186 			msvEntrySelection=inboxEntry->ChildrenL();
       
   187 			CleanupStack::PushL(msvEntrySelection);
       
   188 			//append the message entry to our selection
       
   189 			iSelection->AppendL((*msvEntrySelection)[0]);
       
   190 			//cleanup
       
   191 			CleanupStack::PopAndDestroy(msvEntrySelection);
       
   192 			CleanupStack::PopAndDestroy(inboxEntry);
       
   193 			CleanupStack::PopAndDestroy(imapService);
       
   194 
       
   195 	 
       
   196 			package = imap4GetMailInfo;
       
   197 			delete iMsvOperation;
       
   198 			iMsvOperation=NULL;
       
   199 			iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMCopyMailSelectionWhenAlreadyConnected, *iSelection, package, iStatus);	
       
   200 			SetActive();
       
   201 			break;
       
   202 		
       
   203 		case EComplete:
       
   204 			//inform the owning object that the process is complete
       
   205 			iOwner->HandleEvent(KErrNone);
       
   206 			break;
       
   207 			
       
   208 		default:
       
   209 			__ASSERT_DEBUG(0, User::Panic(_L("CActiveImapClient unknown state"), KErrUnknown));
       
   210 			break;			
       
   211 		}
       
   212 
       
   213 	}
       
   214 	
       
   215 	
       
   216 TInt CActiveImapClient::RunError(TInt aError)
       
   217 	{
       
   218 	iOwner->HandleEvent(aError);
       
   219 	return KErrNone;
       
   220 	}
       
   221 	
       
   222 void CActiveImapClient::HandleImapConnectionEvent(TImapConnectionEvent /*aConnectionState*/)
       
   223 	{
       
   224 	//this method does nothing
       
   225 	}
       
   226 
       
   227 
       
   228