meetingrequestengines/mrmailboxutils/src/cmrutilsmsvhelper.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Provides Class methods for handling message entries and using CMsvSession
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "cmrutilsmsvhelper.h"
       
    21 #include "cmrmailboxinfo.h"
       
    22 #include <SenduiMtmUids.h>
       
    23 #include <cemailaccounts.h>
       
    24 #include <smtpset.h>
       
    25 #include <imapset.h>
       
    26 #include <pop3set.h>
       
    27 #include <MuiuMsvUiServiceUtilities.h>
       
    28 #include <MeetingRequestIds.hrh>
       
    29 #include <mtclbase.h>
       
    30 #include <mtclreg.h>
       
    31 #include <utf.h>
       
    32 
       
    33 // ========================= MEMBER FUNCTIONS ================================
       
    34 
       
    35 // ----------------------------------------------------------------------------
       
    36 // CMRUtilsMsvHelper::NewL
       
    37 // Symbian two-phased constructor.
       
    38 // (other items were commented in a header).
       
    39 // ----------------------------------------------------------------------------
       
    40 //
       
    41 CMRUtilsMsvHelper* CMRUtilsMsvHelper::NewL(
       
    42     CMsvSession* aSession,
       
    43     MMsvSessionObserver& aParentObserver )
       
    44 	{
       
    45 	CMRUtilsMsvHelper* self =
       
    46 	    new( ELeave ) CMRUtilsMsvHelper( aSession, aParentObserver );
       
    47 	CleanupStack::PushL( self );
       
    48 	self->ConstructL();
       
    49 	CleanupStack::Pop();
       
    50 	return self;
       
    51 	}
       
    52 
       
    53 // ----------------------------------------------------------------------------
       
    54 // CMRUtilsMsvHelper::CMRUtilsMsvHelper
       
    55 // Default constructor
       
    56 // ----------------------------------------------------------------------------
       
    57 //
       
    58 CMRUtilsMsvHelper::CMRUtilsMsvHelper(
       
    59     CMsvSession* aSession,
       
    60     MMsvSessionObserver& aParentObserver )
       
    61     : iParentObserver( aParentObserver ),
       
    62       iSession( aSession ),
       
    63       iOwnSession( aSession ? EFalse : ETrue ),
       
    64       iEmailAddr(NULL)
       
    65     {
       
    66     }
       
    67 
       
    68 // ----------------------------------------------------------------------------
       
    69 // CMRUtilsMsvHelper::~CMRUtilsMsvHelper
       
    70 // Destructor.
       
    71 // ----------------------------------------------------------------------------
       
    72 //
       
    73 CMRUtilsMsvHelper::~CMRUtilsMsvHelper()
       
    74     {
       
    75     iMRMailBoxes.ResetAndDestroy();
       
    76     delete iMtmReg;
       
    77     if ( iOwnSession )
       
    78         {
       
    79         if(iSession)
       
    80         {
       
    81            delete iSession; 
       
    82         }
       
    83         }
       
    84     else if ( iSession )
       
    85         { // just remove observer
       
    86         iSession->RemoveObserver( *this );
       
    87         }
       
    88     delete iEmailSettings;
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CMRUtilsMsvHelper::ConstructL
       
    93 // Symbian OS default constructor
       
    94 // ----------------------------------------------------------------------------
       
    95 //
       
    96 void CMRUtilsMsvHelper::ConstructL()
       
    97     {
       
    98     if ( iSession )
       
    99         { // if existing session was given as construction parameter,
       
   100           // then add observer manually
       
   101         iSession->AddObserverL( *this );
       
   102         }   
       
   103     // Create session if doesn't exist yet: 
       
   104     EnsureSessionExistsL();
       
   105     
       
   106     FindMRMailBoxesL();
       
   107     }
       
   108 
       
   109 // ----------------------------------------------------------------------------
       
   110 // CMRUtilsMsvHelper::MRMailBoxes
       
   111 // returns array of mailbox.
       
   112 // ----------------------------------------------------------------------------
       
   113 //
       
   114 const RPointerArray<CMRMailBoxInfo>& CMRUtilsMsvHelper::MRMailBoxes() const
       
   115     {
       
   116     return iMRMailBoxes;
       
   117     }
       
   118     
       
   119 // ----------------------------------------------------------------------------
       
   120 // CMRUtilsMsvHelper::MRMailBoxInfoL
       
   121 // returns mailbox info
       
   122 // ----------------------------------------------------------------------------
       
   123 //
       
   124 const CMRMailBoxInfo* CMRUtilsMsvHelper::MRMailBoxInfoL(
       
   125     TInt aEntryId ) const
       
   126     {
       
   127     CMRMailBoxInfo* info( NULL );
       
   128     TInt count( iMRMailBoxes.Count() );
       
   129     for ( TInt i( 0 ); i < count; ++i )
       
   130         {
       
   131         if ( iMRMailBoxes[i]->EntryId() == aEntryId )
       
   132             {
       
   133             info = iMRMailBoxes[i];
       
   134             break;
       
   135             }
       
   136         }
       
   137     return info;
       
   138     }
       
   139     
       
   140 // TODO: siirrä tämä utilsimpliin
       
   141 // ----------------------------------------------------------------------------
       
   142 // CMRUtilsMsvHelper::ListMRMailBoxesL
       
   143 // lists all the mailboxes available.
       
   144 // ----------------------------------------------------------------------------
       
   145 //
       
   146 void CMRUtilsMsvHelper::ListMRMailBoxesL(
       
   147     RArray<CMRMailboxUtils::TMailboxInfo>& aMailBoxes )
       
   148     {
       
   149     EnsureSessionExistsL();
       
   150     aMailBoxes.Reset();
       
   151 
       
   152     TInt count( iMRMailBoxes.Count() );
       
   153     for ( TInt i( 0 ); i < count; ++i )
       
   154         {
       
   155         CMRMailboxUtils::TMailboxInfo info;
       
   156         info.iName.Set( iMRMailBoxes[i]->Name() );
       
   157         info.iEmailAddress.Set( iMRMailBoxes[i]->Address() );
       
   158         info.iMtmUid = iMRMailBoxes[i]->MtmUid();
       
   159         info.iEntryId = iMRMailBoxes[i]->EntryId();
       
   160         aMailBoxes.AppendL( info );
       
   161         }
       
   162     }
       
   163 
       
   164 // ----------------------------------------------------------------------------
       
   165 // CMRUtilsMsvHelper::ExtensionL
       
   166 // extension API.
       
   167 // ----------------------------------------------------------------------------
       
   168 //
       
   169 TAny* CMRUtilsMsvHelper::ExtensionL( TUid /*aExtensionUid*/ )
       
   170     {
       
   171 	return NULL;
       
   172     }
       
   173 
       
   174 // ----------------------------------------------------------------------------
       
   175 // CMRUtilsMsvHelper::MsvSessionL
       
   176 // returns session.
       
   177 // ----------------------------------------------------------------------------
       
   178 //
       
   179 CMsvSession& CMRUtilsMsvHelper::MsvSessionL()
       
   180     {
       
   181     EnsureSessionExistsL();
       
   182     return *iSession;
       
   183     }
       
   184     
       
   185 // ----------------------------------------------------------------------------
       
   186 // CMRUtilsMsvHelper::EMailAddressL 
       
   187 // ----------------------------------------------------------------------------
       
   188 //
       
   189 //TBuf<KMREmailAddrLength> CMRUtilsMsvHelper::EMailAddressL( TMsvEntry& entry )
       
   190 void  CMRUtilsMsvHelper::EMailAddressL( TMsvEntry& entry )
       
   191     {
       
   192     EnsureSessionExistsL();
       
   193    // TBuf<KMREmailAddrLength> emailAddr;
       
   194     
       
   195     if ( entry.iMtm.iUid == KSenduiMtmImap4UidValue ||
       
   196          entry.iMtm.iUid == KSenduiMtmPop3UidValue ||
       
   197          entry.iMtm.iUid == KSenduiMtmSyncMLEmailUidValue )
       
   198         {        
       
   199         // Read e-mail address from SMTP settings:
       
   200         TMsvId smtpId = CorrespondingSmtpServiceL( entry );
       
   201         CImSmtpSettings* smtpSettings = SmtpSettingsLC( smtpId );
       
   202        iEmailAddr = smtpSettings->EmailAddress();
       
   203         CleanupStack::PopAndDestroy( smtpSettings );
       
   204         }
       
   205     else if ( IsMRCapableL( entry.iMtm ) )
       
   206         { // 3rd party mtm types -> query from client mtm
       
   207         CBaseMtm* mtm = iMtmReg->NewMtmL( entry.iMtm );
       
   208         CleanupStack::PushL( mtm );
       
   209         CMsvEntrySelection* selection = new( ELeave ) CMsvEntrySelection();
       
   210         CleanupStack::PushL( selection );
       
   211         selection->AppendL( entry.Id() );
       
   212         RBuf8 addrData;
       
   213         CleanupClosePushL( addrData );
       
   214         addrData.CreateL( KMREmailAddrLength );        
       
   215         mtm->InvokeSyncFunctionL( KMTMMailAddressRequestFunctionId,
       
   216                                   *selection,
       
   217                                   addrData );
       
   218         if ( addrData.Length() > 0 )
       
   219             {
       
   220             User::LeaveIfError( CnvUtfConverter::ConvertToUnicodeFromUtf8(
       
   221                 iEmailAddr, addrData ) );
       
   222             }        
       
   223         CleanupStack::PopAndDestroy( 3 ); // addrData, selection, mtm
       
   224         }
       
   225     else
       
   226         { // we can't resolve email address for other types, and that's fine
       
   227         User::Leave( KErrNotSupported );
       
   228         }
       
   229 
       
   230    // return emailAddr;
       
   231     }
       
   232 
       
   233 // ----------------------------------------------------------------------------
       
   234 // CMRUtilsMsvHelper::CorrespondingSmtpServiceL
       
   235 // returns the smtp id.
       
   236 // ----------------------------------------------------------------------------
       
   237 //
       
   238 TMsvId CMRUtilsMsvHelper::CorrespondingSmtpServiceL( TMsvEntry& entry ) const
       
   239     {
       
   240     TMsvId smtpId( KMsvNullIndexEntryId );
       
   241 
       
   242     switch ( entry.iMtm.iUid )
       
   243         {
       
   244         case KSenduiMtmImap4UidValue: // flow through
       
   245         case KSenduiMtmPop3UidValue:
       
   246             {
       
   247             // In these cases smtp entry is available in iRelatedId:
       
   248             smtpId = entry.iRelatedId;
       
   249             break;
       
   250             }
       
   251         case KSenduiMtmSmtpUidValue:
       
   252         case KSenduiMtmSyncMLEmailUidValue:
       
   253             {
       
   254             // In these cases we already know the msvid for the smtp settings
       
   255             // (for syncml there is also smtp settings!):
       
   256             smtpId = entry.Id();
       
   257             break;
       
   258             }
       
   259         default:
       
   260             {
       
   261             User::Leave( KErrNotSupported );
       
   262             break;
       
   263             }
       
   264         }
       
   265     return smtpId;
       
   266     }
       
   267 
       
   268 // ----------------------------------------------------------------------------
       
   269 // CMRUtilsMsvHelper::SmtpSettingsLC
       
   270 // returns smtp settings.
       
   271 // ----------------------------------------------------------------------------
       
   272 //
       
   273 CImSmtpSettings* CMRUtilsMsvHelper::SmtpSettingsLC( TMsvId aSmtpService )
       
   274     {
       
   275     
       
   276 // TODO: onko tässä lazyssa järkeä?    
       
   277     if ( !iEmailSettings )
       
   278         { // Lazy instantiation of iEmailSettings
       
   279         iEmailSettings = CEmailAccounts::NewL();
       
   280         }
       
   281         
       
   282     TSmtpAccount smtpAccount;
       
   283     iEmailSettings->GetSmtpAccountL( aSmtpService, smtpAccount );
       
   284     CImSmtpSettings* smtpSettings = new( ELeave ) CImSmtpSettings();
       
   285     CleanupStack::PushL( smtpSettings );
       
   286     iEmailSettings->LoadSmtpSettingsL( smtpAccount, *smtpSettings );
       
   287     return smtpSettings;
       
   288     }
       
   289 
       
   290 // ----------------------------------------------------------------------------
       
   291 // CMRUtilsMsvHelper::IsMRCapableL
       
   292 // returns ETrue if MRCapable, EFalse otherwise.
       
   293 // ----------------------------------------------------------------------------
       
   294 //
       
   295 
       
   296 TBool CMRUtilsMsvHelper::IsMRCapableL( TUid aMtmUid )
       
   297     {
       
   298     TBool retVal( EFalse );
       
   299     if ( aMtmUid.iUid == KSenduiMtmImap4UidValue ||
       
   300          aMtmUid.iUid == KSenduiMtmPop3UidValue ||
       
   301          aMtmUid.iUid == KSenduiMtmSyncMLEmailUidValue )
       
   302         {
       
   303         retVal = ETrue;
       
   304         }
       
   305     else if ( aMtmUid.iUid == KSenduiMtmSmtpUidValue )
       
   306         { // don't consider smtp's here, corresponding imap/pop boxes only
       
   307         retVal = EFalse;
       
   308         }
       
   309     else
       
   310         {        
       
   311         // we can create and destroy mtm without performance loss since registry
       
   312         // unloads them only if unused for a period of time
       
   313         CBaseMtm* mtm = iMtmReg->NewMtmL( aMtmUid );
       
   314         CleanupStack::PushL( mtm );
       
   315         TInt dummyResponse( 0 );
       
   316         TInt result( mtm->QueryCapability(
       
   317             TUid::Uid( KMtmUiSupportMeetingRequest ), dummyResponse ) );
       
   318         CleanupStack::PopAndDestroy( mtm );        
       
   319         retVal = ( ( result == KErrNone ) ? ETrue : EFalse );
       
   320         }
       
   321     return retVal;
       
   322     }
       
   323 
       
   324 // ----------------------------------------------------------------------------
       
   325 // CMRUtilsMsvHelper::EnsureSessionExistsL
       
   326 // ensures the session existense, creates if not exists.
       
   327 // ----------------------------------------------------------------------------
       
   328 //
       
   329 void CMRUtilsMsvHelper::EnsureSessionExistsL()
       
   330     {
       
   331     if ( !iSession )
       
   332         {
       
   333         iSession = CMsvSession::OpenSyncL( *this );
       
   334         iOwnSession = ETrue;
       
   335         }
       
   336     if ( !iMtmReg )
       
   337         {
       
   338         iMtmReg = CClientMtmRegistry::NewL( *iSession );        
       
   339         }
       
   340     }
       
   341 
       
   342 // ----------------------------------------------------------------------------
       
   343 // CMRUtilsMsvHelper::HandleSessionEventL
       
   344 // ----------------------------------------------------------------------------
       
   345 //
       
   346 void CMRUtilsMsvHelper::HandleSessionEventL(
       
   347     TMsvSessionEvent aEvent,
       
   348     TAny* aArg1,
       
   349     TAny* aArg2,
       
   350     TAny* aArg3 )
       
   351     {
       
   352     switch ( aEvent )
       
   353         {
       
   354         case EMsvCloseSession:
       
   355         case EMsvServerTerminated:
       
   356             {
       
   357             delete iMtmReg;
       
   358             iMtmReg = NULL;
       
   359             
       
   360             if ( iOwnSession )
       
   361                 {
       
   362                 delete iSession;
       
   363                 }
       
   364             iSession = NULL; // New session constructed only if needed
       
   365             break;
       
   366             }
       
   367         case EMsvEntriesCreated:
       
   368             {
       
   369             CMsvEntrySelection* entries =
       
   370                 static_cast<CMsvEntrySelection*>( aArg1 );            
       
   371             if ( AddToListIfMRMailBoxesL( *entries ) )
       
   372                 { // at least MR mailbox created
       
   373                 iParentObserver.HandleSessionEventL( aEvent,
       
   374                                                      aArg1,
       
   375                                                      aArg2,
       
   376                                                      aArg3 );                
       
   377                 }
       
   378             break;
       
   379             }            
       
   380         case EMsvEntriesChanged:        
       
   381         case EMsvEntriesDeleted:        
       
   382             {
       
   383             CMsvEntrySelection* entries =
       
   384                 static_cast<CMsvEntrySelection*>( aArg1 );        
       
   385             if ( ContainsMRMailBoxEntryL( *entries ) )
       
   386                 {
       
   387                 // Note: in case of entry deletion it is important that
       
   388                 // iMRMailBoxes array is refreshed only after evaluating
       
   389                 // ContainsMRMailBoxEntryL()
       
   390                 FindMRMailBoxesL();                
       
   391                 iParentObserver.HandleSessionEventL( aEvent,
       
   392                                                      aArg1,
       
   393                                                      aArg2,
       
   394                                                      aArg3 );
       
   395                 }
       
   396             break;
       
   397             }            
       
   398         default:
       
   399             {
       
   400             // ignore other events
       
   401             break;
       
   402             }
       
   403         }
       
   404     }
       
   405 
       
   406 // ----------------------------------------------------------------------------
       
   407 // CMRUtilsMsvHelper::ContainsMRMailBoxEntryL
       
   408 // ----------------------------------------------------------------------------
       
   409 //
       
   410 TBool CMRUtilsMsvHelper::ContainsMRMailBoxEntryL(
       
   411     const CMsvEntrySelection& aEntries ) const
       
   412     {
       
   413     TBool retVal( EFalse );
       
   414     
       
   415     TInt count( iMRMailBoxes.Count() );
       
   416     for ( TInt i( 0 ); i < count; ++i )
       
   417         {
       
   418         if ( aEntries.Find( iMRMailBoxes[i]->EntryId() ) != KErrNotFound )
       
   419             {
       
   420             retVal = ETrue;
       
   421             break;
       
   422             }
       
   423         }
       
   424     return retVal;
       
   425     }
       
   426     
       
   427 // ----------------------------------------------------------------------------
       
   428 // CMRUtilsMsvHelper::FindMRMailBoxesL
       
   429 // ----------------------------------------------------------------------------
       
   430 //
       
   431 void CMRUtilsMsvHelper::FindMRMailBoxesL()
       
   432     {    
       
   433     EnsureSessionExistsL();    
       
   434     iMRMailBoxes.ResetAndDestroy();
       
   435     // list also hidden (3rd party) accounts -> 2nd parameter == ETrue
       
   436     CMsvEntrySelection* allMailBoxes =
       
   437         MsvUiServiceUtilities::GetListOfAccountsL( *iSession, ETrue );
       
   438     CleanupStack::PushL( allMailBoxes );
       
   439     AddToListIfMRMailBoxesL( *allMailBoxes );
       
   440     CleanupStack::PopAndDestroy( allMailBoxes );
       
   441     }
       
   442 
       
   443 // ----------------------------------------------------------------------------
       
   444 // CMRUtilsMsvHelper::AddToListIfMRMailBoxesL
       
   445 // ----------------------------------------------------------------------------
       
   446 //
       
   447 TBool CMRUtilsMsvHelper::AddToListIfMRMailBoxesL(
       
   448     const CMsvEntrySelection& aEntries )
       
   449     {
       
   450     TBool retVal( EFalse );
       
   451     TInt count( aEntries.Count() );
       
   452     for ( TInt i( 0 ); i < count; ++i )
       
   453         {
       
   454         TBool result( EFalse );
       
   455         // don't care if individual mailboxes cause some problems
       
   456         TRAP_IGNORE( result = AddToListIfMRMailBoxL( aEntries.At( i ) ) );
       
   457         if ( result )
       
   458             { // retVal can only go from EFalse to ETrue, not other way
       
   459             retVal = ETrue;
       
   460             }
       
   461         }
       
   462     return retVal;
       
   463     }
       
   464 
       
   465 // ----------------------------------------------------------------------------
       
   466 // CMRUtilsMsvHelper::AddToListIfMRMailBoxL
       
   467 // ----------------------------------------------------------------------------
       
   468 //
       
   469 TBool CMRUtilsMsvHelper::AddToListIfMRMailBoxL( TMsvId aEntryId )
       
   470     {    
       
   471     TBool retVal( EFalse );
       
   472     EnsureSessionExistsL();
       
   473     TMsvEntry entry;
       
   474     TMsvId dummyService;
       
   475     
       
   476     CMRMailBoxInfo* info( NULL );
       
   477     TInt result = iSession->GetEntry( aEntryId, dummyService, entry );    
       
   478 
       
   479     if ( result == KErrNone &&
       
   480          entry.iType == KUidMsvServiceEntry &&        
       
   481          IsMRCapableL( entry.iMtm ) )
       
   482         {        
       
   483          EMailAddressL( entry ) ;
       
   484         if ( MsvUiServiceUtilities::IsValidEmailAddressL( iEmailAddr ) )
       
   485             {            
       
   486             info = CMRMailBoxInfo::NewL( entry.iDetails,
       
   487                                          iEmailAddr,
       
   488                                          entry.iMtm,
       
   489                                          aEntryId );    
       
   490             CleanupStack::PushL( info );
       
   491             iMRMailBoxes.AppendL( info ); // ownership transferred
       
   492             retVal = ETrue;
       
   493             CleanupStack::Pop( info );
       
   494             }
       
   495         }
       
   496         
       
   497     return retVal;
       
   498     }
       
   499 
       
   500     
       
   501 // End of file