email/pop3andsmtpmtm/imapservermtm/test/src/T_DEF062024_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 // IMAPClient.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "t_def062024_imapclient.h"
       
    19 
       
    20 _LIT(KLoopbackAddress,"127.0.0.1");
       
    21 
       
    22 void TDummySessionObserver::HandleSessionEventL(TMsvSessionEvent /*aEvent*/, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/)
       
    23 	{
       
    24 	}
       
    25  
       
    26 TImapAccount CActiveImapClient::GetAccount()
       
    27 	{
       
    28 	return iImapAccount;
       
    29 	}
       
    30 
       
    31 CActiveImapClient::CActiveImapClient(MImapTestEventHandler& aObserver)
       
    32  : CActive(EPriorityStandard), iObserver(aObserver)
       
    33 	{
       
    34 	CActiveScheduler::Add(this); 	
       
    35 	}
       
    36 
       
    37 EXPORT_C CActiveImapClient* CActiveImapClient::NewL(MImapTestEventHandler& aObserver)
       
    38 	{
       
    39 	CActiveImapClient* self = NewLC(aObserver);
       
    40 	CleanupStack::Pop(self);
       
    41 	return self;
       
    42 	}
       
    43 
       
    44 EXPORT_C CActiveImapClient* CActiveImapClient::NewLC(MImapTestEventHandler& aObserver)
       
    45 	{
       
    46 	CActiveImapClient* self = new(ELeave) CActiveImapClient(aObserver);
       
    47 	CleanupStack::PushL(self);
       
    48 	self->ConstructL();
       
    49 	return self;
       
    50 	}
       
    51 	
       
    52 CActiveImapClient::~CActiveImapClient()
       
    53 	{
       
    54 	Cancel();
       
    55 	delete iMsvOperation;	
       
    56 	delete iSelection;
       
    57 	delete iClientMtm;
       
    58 	delete iClientRegistry;	
       
    59 	delete iSession;
       
    60 	delete iSessionObserver;
       
    61 	delete iAccounts;
       
    62 	}
       
    63 	
       
    64 	
       
    65 void CActiveImapClient::ConstructL()
       
    66 	{	
       
    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(4,imap4Settings);  
       
   117 	}
       
   118 
       
   119 // Return iMessageId, the message ID of the email being fetched with its attachment
       
   120 TMsvId CActiveImapClient::GetMessageId()
       
   121 	{
       
   122 	return iMessageId;
       
   123 	}
       
   124 
       
   125 void CActiveImapClient::StartL()
       
   126 	{	
       
   127 	//we are to collect the whole email then the next operation will be to fetch it
       
   128 	iState = EFetch;
       
   129 	iSelection = new (ELeave) CMsvEntrySelection;
       
   130 	TPckg<MMsvImapConnectionObserver*> param(this);
       
   131 	//select the imap service entry
       
   132 	iSelection->AppendL(iImapAccount.iImapService);
       
   133 	//make the service entry the current context
       
   134 	iClientMtm->SwitchCurrentEntryL(iImapAccount.iImapService);
       
   135 	//sync the account
       
   136 	iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMConnectAndSyncCompleteAfterFullSync,*iSelection,param,iStatus);			
       
   137 	SetActive();
       
   138 	}
       
   139 	
       
   140 void CActiveImapClient::DoCancel()
       
   141 	{	
       
   142 	__ASSERT_DEBUG(iMsvOperation!=NULL, User::Panic(_L("CActiveImapClient"), -3));
       
   143 	iMsvOperation->Cancel();
       
   144 	}
       
   145 
       
   146 void CActiveImapClient::RunL()
       
   147 	{
       
   148 	User::LeaveIfError(iStatus.Int());
       
   149 	switch(iState)
       
   150 		{
       
   151 		//connect and sync	
       
   152 		case EDisconnect:
       
   153 			{
       
   154 			iState = EComplete;
       
   155 			TPckg<MMsvImapConnectionObserver*> param(this);
       
   156 			delete iMsvOperation;
       
   157 			iMsvOperation = NULL;
       
   158 			iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMDisconnect,*iSelection,param,iStatus);	
       
   159 			SetActive();
       
   160 			break;		
       
   161 			}
       
   162 		case EFetch:
       
   163 			{
       
   164 			iState = EDisconnect;
       
   165 			TImImap4GetMailInfo imap4GetMailInfo;
       
   166 			imap4GetMailInfo.iMaxEmailSize = KMaxTInt;
       
   167 			imap4GetMailInfo.iDestinationFolder = KMsvGlobalInBoxIndexEntryIdValue;
       
   168 			imap4GetMailInfo.iGetMailBodyParts = EGetImap4EmailBodyTextAndAttachments;					
       
   169 			TPckgBuf<TImImap4GetMailInfo> package(imap4GetMailInfo);			
       
   170 			CMsvEntry* inboxEntry = NULL;
       
   171 			CMsvEntry* imapService = NULL;
       
   172 			TMsvSelectionOrdering ordering;
       
   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->Reset();
       
   190 			//We store the message Id of the email we want to check attachment
       
   191 			iMessageId = (*msvEntrySelection)[0];
       
   192 			iSelection->AppendL(iMessageId);
       
   193 			//cleanup
       
   194 			CleanupStack::PopAndDestroy(msvEntrySelection);
       
   195 			CleanupStack::PopAndDestroy(inboxEntry);
       
   196 			CleanupStack::PopAndDestroy(imapService);
       
   197 
       
   198 			package = imap4GetMailInfo;
       
   199 			delete iMsvOperation;
       
   200 			iMsvOperation = NULL;
       
   201 			iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMPopulate, *iSelection, package, iStatus);	
       
   202 			SetActive();
       
   203 			break;
       
   204 			}
       
   205 		case EComplete:
       
   206 			{//inform the owning object that the process is complete
       
   207 			iObserver.TestComplete(KErrNone);
       
   208 			break;
       
   209 			}
       
   210 		default:
       
   211 			{
       
   212 			__ASSERT_DEBUG(0, User::Panic(_L("CActiveImapClient unknown state"), KErrUnknown));
       
   213 			break;			
       
   214 			}
       
   215 		}
       
   216 	}
       
   217 	
       
   218 TInt CActiveImapClient::RunError(TInt aError)
       
   219 	{
       
   220 	iObserver.TestComplete(aError);
       
   221 	return KErrNone;
       
   222 	}
       
   223 	
       
   224 void CActiveImapClient::HandleImapConnectionEvent(TImapConnectionEvent /*aConnectionState*/)
       
   225 	{
       
   226 	//this method does nothing
       
   227 	}
       
   228 
       
   229 
       
   230