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