- /*
- * ============================================================================
- * Name : CMsvEmailUtils from MsvEmailUtils.cpp
- * Part of : EmailExample
- * Created : 09/11/2003 by Forum Nokia
- * Implementation notes:
- * Common helpers for different internet email protocols
- *
- * Version : 1.0
- * Copyright: Nokia Corporation
- * ============================================================================
- */
- #include <mtclreg.h> //for CClientMtmRegistry
- #include <popcmtm.h> //for POP commands
- #include <impcmtm.h> //for IMAP commmands
- #include "MsvEmailUtils.h"
- // static creation. Removes object from clean up stack
- CMsvEmailUtils* CMsvEmailUtils::NewL(CMsvSession& aSession)
- {
- CMsvEmailUtils* self = NewLC(aSession);
- CleanupStack::Pop(self);
- return self;
- }
- // static creation. Leaves object on clean up stack
- CMsvEmailUtils* CMsvEmailUtils::NewLC(CMsvSession& aSession)
- {
- CMsvEmailUtils* self = new (ELeave)CMsvEmailUtils(aSession);
- CleanupStack::PushL(self);
- self->ConstructL();
- return self;
- }
- CMsvEmailUtils::CMsvEmailUtils(CMsvSession& aSession):
- iMsvSession(aSession)
- {}
- CMsvEmailUtils::~CMsvEmailUtils()
- {
- delete iMtmReg;
- }
- void CMsvEmailUtils::ConstructL()
- {}
- // find the messaging type ID of the given service UID
- TMsvId CMsvEmailUtils::GetServiceIdL(TUid aType)
- {
- return FindServiceL(aType);
- }
- // find the messaging type ID of the given folder then service UID
- TMsvId CMsvEmailUtils::GetFolderThenServiceIdL(TUid aType)
- {
- return FindFolderThenServiceL(aType);
- }
- // create an imap4 client mtm
- CImap4ClientMtm* CMsvEmailUtils::InstantiateImapClientMtmL(TMsvId aService)
- {
- // make sure we have created the mtm registry.
- if (!iMtmReg)
- {
- iMtmReg = CClientMtmRegistry::NewL(iMsvSession);
- }
- // create a imap4 client mtm
- CImap4ClientMtm* newMtm = static_cast<CImap4ClientMtm*>(iMtmReg->NewMtmL(KUidMsgTypeIMAP4));
- CleanupStack::PushL(newMtm);
- // get the entry associated with the given servive ID
- CMsvEntry* entry = iMsvSession.GetEntryL(aService);
- CleanupStack::PushL(entry);
- // set the entry as the current entry
- // mtm takes ownership of the entry
- newMtm->SetCurrentEntryL(entry);
- CleanupStack::Pop(entry);
- CleanupStack::Pop(newMtm);
- return newMtm;
- }
- // create a pop3 client mtm
- CPop3ClientMtm* CMsvEmailUtils::InstantiatePopClientMtmL(TMsvId aService)
- {
- // make sure we have created the mtm registry.
- if (!iMtmReg)
- {
- iMtmReg = CClientMtmRegistry::NewL(iMsvSession);
- }
- // create a pop3 client mtm
- CPop3ClientMtm* newMtm = static_cast<CPop3ClientMtm*>(iMtmReg->NewMtmL(KUidMsgTypePOP3));
- CleanupStack::PushL(newMtm);
- // get the entry associated with the given servive ID
- CMsvEntry* entry = iMsvSession.GetEntryL(aService);
- CleanupStack::PushL(entry);
- // set the entry as the current entry
- // mtm takes ownership of the entry
- newMtm->SetCurrentEntryL(entry);
- CleanupStack::Pop(entry);
- CleanupStack::Pop(newMtm);
- return newMtm;
- }
- // this function will return the first folder of first
- // service of given type if and only if folder exists
- // else it returns first service entry id or if not exists root id
- TMsvId CMsvEmailUtils::FindFolderThenServiceL(TUid aType)
- {
- // select the root index to start the search
- CMsvEntry* currentEntry = iMsvSession.GetEntryL(KMsvRootIndexEntryId);
- CleanupStack::PushL(currentEntry);
- // don't sort the entries
- currentEntry->SetSortTypeL(TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone, ETrue));
- TMsvId rc = KMsvRootIndexEntryId;
- TInt count=currentEntry->Count();
- // loop for every child entry of the root index
- for(TInt i = 0;i<count;i++)
- {
- const TMsvEntry& child = (*currentEntry)[i];
- // is the current child the same type as the type we are looking for ?
- if (child.iMtm == aType)
- {
- // selects first entry as index entry
- CMsvEntry* firstEntry = iMsvSession.GetEntryL(child.Id());
- CleanupStack::PushL(firstEntry);
- TInt innercount=firstEntry->Count();
- if( innercount )
- { //if has childs == folders take first
- const TMsvEntry& folder = (*firstEntry)[0];
- rc=folder.Id();
- }
- else
- {
- rc=child.Id();
- }
- CleanupStack::PopAndDestroy(firstEntry);
- break;
- }
- }//for
- CleanupStack::PopAndDestroy(currentEntry);
- // return the service id of the type if found.
- // otherwise return the root index
- return rc;
- }
- // find the message id of a service given a UID
- // this function will return the first service id for given type
- TMsvId CMsvEmailUtils::FindServiceL(TUid aType)
- {
- // select the root index to start the search
- CMsvEntry* currentEntry = iMsvSession.GetEntryL(KMsvRootIndexEntryId);
- CleanupStack::PushL(currentEntry);
- // don't sort the entries
- currentEntry->SetSortTypeL(TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone, ETrue));
- TMsvId rc = KMsvRootIndexEntryId;
- TInt count=currentEntry->Count();
- // loop for every child entry of the root index
- for(TInt i = 0;i<count;i++)
- {
- const TMsvEntry& child = (*currentEntry)[i];
- // is the current child the same type as the type we are looking for ?
- if (child.iMtm == aType)
- {
- // selects first entry as index entry
- rc=child.Id();
- break;
- }
- }//for
- CleanupStack::PopAndDestroy(currentEntry);
- // return the service id of the type if found.
- // otherwise return the root index
- return rc;
- }