messagingappbase/msgavkon/muiu_internal/emailtools/src/MuiuEmailTools.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2002 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:  MuiuEmailTools implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32base.h>
       
    22 #include <cemailaccounts.h>         // CEmailAccounts
       
    23 #include <msvstd.h>                 // TEntry
       
    24 #include <msvids.h>                 // KMsvUnknownServiceIndexEntryId
       
    25 #include <msvstd.hrh>               // KUidMsvServiceEntryValue
       
    26 #include <miuthdr.h>                // TMsvEmailEntry
       
    27 #include <MtmExtendedCapabilities.hrh> // Capabilities
       
    28 #include <mtclreg.h>                // CClientMtmRegistry
       
    29 #include <mtclbase.h>               // CBaseMtm
       
    30 #include <mtuireg.h>
       
    31 #include <muiuemailtools.h>
       
    32 
       
    33 // EXTERNAL DATA STRUCTURES
       
    34 // EXTERNAL FUNCTION PROTOTYPES
       
    35 // CONSTANTS
       
    36 // MACROS
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 // MODULE DATA STRUCTURES
       
    39 // LOCAL FUNCTION PROTOTYPES
       
    40 // FORWARD DECLARATIONS
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // MuiuEmailTools::SetDefaultSendingMailboxL()
       
    46 // ----------------------------------------------------------------------------
       
    47 //
       
    48 EXPORT_C void MuiuEmailTools::SetDefaultSendingMailboxL(    
       
    49     const TMsvId aSendingMailboxId )
       
    50     {
       
    51     CEmailAccounts* account = CEmailAccounts::NewLC();
       
    52     
       
    53     // Store the id
       
    54 	TSmtpAccount sendingId;
       
    55 	account->GetSmtpAccountL( aSendingMailboxId, sendingId );
       
    56 	account->SetDefaultSmtpAccountL( sendingId );   		       
       
    57     
       
    58     CleanupStack::PopAndDestroy( account );        
       
    59     }
       
    60     
       
    61 // ----------------------------------------------------------------------------
       
    62 // MuiuEmailTools::DefaultSendingMailboxL()
       
    63 // ----------------------------------------------------------------------------
       
    64 //
       
    65 EXPORT_C TMsvId MuiuEmailTools::DefaultSendingMailboxL( 
       
    66     CMsvSession& aMsvSession,
       
    67     TUid& aMtmId )
       
    68     {        
       
    69     // In case of basic mail mtm's, force the smtp as a searchable mtm
       
    70     TMsvId serviceId = KMsvUnknownServiceIndexEntryId;
       
    71     
       
    72     // If Imap4, Pop3 or Smtp mailbox, use CEmailAccounts to check the
       
    73     // default service
       
    74     if ( MuiuEmailTools::IsMailMtm( aMtmId ) )
       
    75         {
       
    76         TInt result = KErrNone;
       
    77         TSmtpAccount smtpId;
       
    78         aMtmId = KSenduiMtmSmtpUid;
       
    79         
       
    80         // Solve the mystery of the default mailbox
       
    81         CEmailAccounts* account = CEmailAccounts::NewLC();
       
    82         TRAPD( error, result = account->DefaultSmtpAccountL( smtpId ) );                    
       
    83         if ( error != KErrNone || result != KErrNone )
       
    84             {
       
    85             // Getting the default mailbox didn't succeed, 
       
    86             // return unknown service
       
    87             smtpId.iSmtpService = KMsvUnknownServiceIndexEntryId;
       
    88             }
       
    89         
       
    90         CleanupStack::PopAndDestroy( account );
       
    91         account = NULL;
       
    92         
       
    93         serviceId = smtpId.iSmtpService;
       
    94         }
       
    95     else
       
    96         {
       
    97         serviceId = QueryDefaultServiceL( aMsvSession, aMtmId );
       
    98         }
       
    99     
       
   100     // After the service has been specified, make sure it is valid    
       
   101     if ( !ValidateMailboxL( aMsvSession, aMtmId, serviceId ) )
       
   102         {
       
   103         serviceId = KMsvUnknownServiceIndexEntryId;
       
   104         }    
       
   105         
       
   106     return serviceId;
       
   107     }
       
   108 
       
   109 // ----------------------------------------------------------------------------
       
   110 // MuiuEmailTools::FindFirstValidMailboxL()
       
   111 // ----------------------------------------------------------------------------
       
   112 //
       
   113 TMsvId MuiuEmailTools::FindFirstValidMailboxL( 
       
   114     CMsvSession& aMsvSession,
       
   115     const CMsvEntrySelection& aSelection,
       
   116     const TUid& aMtmId )
       
   117     {
       
   118     TMsvId serviceId = KMsvUnknownServiceIndexEntryId;
       
   119     
       
   120     // Prepare the mtm store and get the MtmUi of the desired mtmId
       
   121     CClientMtmRegistry* registry = CClientMtmRegistry::NewL( aMsvSession );
       
   122     CleanupStack::PushL( registry );
       
   123     CBaseMtm* mtm = registry->NewMtmL( aMtmId );
       
   124     CleanupStack::PushL( mtm );
       
   125     CMtmUiRegistry* uiRegistry = CMtmUiRegistry::NewL( aMsvSession );
       
   126     CleanupStack::PushL( uiRegistry );
       
   127     CBaseMtmUi* smtpMtmUi = uiRegistry->NewMtmUiL( *mtm );
       
   128     CleanupStack::PushL( smtpMtmUi );
       
   129     
       
   130     // Query from the mtm, if it is able to validate the service
       
   131     if ( QueryCapability( 
       
   132         KUidMsvMtmQuerySupportValidateSelection, *smtpMtmUi ) )
       
   133         {
       
   134         const TInt count = aSelection.Count();
       
   135         for ( TInt index = 0; 
       
   136               index < count && serviceId == KMsvUnknownServiceIndexEntryId; 
       
   137               index++ )
       
   138             {
       
   139             TMsvId mailbox = aSelection.At( index );
       
   140             serviceId = QueryMailboxValidityL( mailbox, *smtpMtmUi ) > 0 ?
       
   141                 mailbox : KMsvUnknownServiceIndexEntryId;
       
   142             }        
       
   143         }
       
   144         
       
   145     CleanupStack::PopAndDestroy( 4, registry ); // registry, mtm, uiRegistry, smtpMtmUi
       
   146     
       
   147     return serviceId;
       
   148     }
       
   149         
       
   150 // ----------------------------------------------------------------------------
       
   151 // MuiuEmailTools::IsMailMtm()
       
   152 // ----------------------------------------------------------------------------
       
   153 //
       
   154 EXPORT_C TBool MuiuEmailTools::IsMailMtm( 
       
   155     const TUid& aMtm,
       
   156     const TBool aAllowExtended )
       
   157     {                   
       
   158     // Basic mail mtm's   
       
   159     TBool basicMtms = 
       
   160         ( aMtm == KSenduiMtmSmtpUid ) || 
       
   161         ( aMtm == KSenduiMtmPop3Uid ) || 
       
   162         ( aMtm == KSenduiMtmImap4Uid );
       
   163     // extended mail mtm's
       
   164     TBool extendedMtms =
       
   165         ( aMtm == KSenduiMtmSyncMLEmailUid );
       
   166             
       
   167     // Returns ETrue, if the id is any of the following
       
   168     return basicMtms || ( aAllowExtended && extendedMtms );
       
   169     }     
       
   170 
       
   171 // ----------------------------------------------------------------------------
       
   172 // MuiuEmailTools::GetMailboxServiceId()
       
   173 // ----------------------------------------------------------------------------
       
   174 //
       
   175 EXPORT_C TInt MuiuEmailTools::GetMailboxServiceId(
       
   176     CMsvSession& aMsvSession,
       
   177     TMsvEntry& aMailbox,
       
   178     const TMsvId aMailboxId,    
       
   179     const TBool aGetSendingService )
       
   180     {
       
   181     // Get the entry based on mailbox id    
       
   182     TMsvId serviceId;            
       
   183     TInt error = aMsvSession.GetEntry( aMailboxId, serviceId, aMailbox );
       
   184     
       
   185     TInt32 mtm = aMailbox.iMtm.iUid;
       
   186     
       
   187     // Continue handling, if successful
       
   188     if ( error == KErrNone && 
       
   189          aMailbox.iType.iUid == KUidMsvServiceEntryValue && 
       
   190          IsMailMtm( aMailbox.iMtm ) )  
       
   191         {        
       
   192         // Get related entry, if:
       
   193         // Mailbox type is smtp and pop3 or imap4 is wanted OR
       
   194         // Mailbox type is pop3 or imap4 and smtp is wanted
       
   195         if ( aGetSendingService && mtm != KSenduiMtmSmtpUidValue || 
       
   196             !aGetSendingService && mtm == KSenduiMtmSmtpUidValue )
       
   197             {
       
   198             error = aMsvSession.GetEntry( aMailbox.iRelatedId, serviceId, aMailbox );
       
   199             }
       
   200         }
       
   201 
       
   202     return error;
       
   203     }    
       
   204 
       
   205 // ----------------------------------------------------------------------------
       
   206 // MuiuEmailTools::ValidateMailboxL()
       
   207 // ----------------------------------------------------------------------------
       
   208 //
       
   209 TBool MuiuEmailTools::ValidateMailboxL( 
       
   210     CMsvSession& aMsvSession,
       
   211     const TUid& aMtmId,
       
   212     const TMsvId aMailboxId )
       
   213     {
       
   214     // Prepare the mtm store and get the MtmUi of the desired mtmId
       
   215     TBool validity = EFalse;
       
   216 
       
   217     CClientMtmRegistry* registry = CClientMtmRegistry::NewL( aMsvSession );
       
   218     CleanupStack::PushL( registry );
       
   219     CBaseMtm* mtm = registry->NewMtmL( aMtmId );
       
   220     CleanupStack::PushL( mtm );
       
   221     CMtmUiRegistry* uiRegistry = CMtmUiRegistry::NewL( aMsvSession );
       
   222     CleanupStack::PushL( uiRegistry );
       
   223     CBaseMtmUi* smtpMtmUi = uiRegistry->NewMtmUiL( *mtm );
       
   224     CleanupStack::PushL( smtpMtmUi );
       
   225 
       
   226     // Query from the mtm, if it is able to validate the service
       
   227     if ( QueryCapability( 
       
   228         KUidMsvMtmQuerySupportValidateSelection, *smtpMtmUi ) )
       
   229         {
       
   230         validity = QueryMailboxValidityL( aMailboxId, *smtpMtmUi ) > 0;
       
   231         }
       
   232         
       
   233     CleanupStack::PopAndDestroy( 4, registry ); // registry, mtm, uiRegistry, smtpMtmUi
       
   234     
       
   235     return validity;
       
   236     }
       
   237     
       
   238 // ----------------------------------------------------------------------------
       
   239 // MuiuEmailTools::QueryCapability()
       
   240 // ----------------------------------------------------------------------------
       
   241 //
       
   242 TBool MuiuEmailTools::QueryCapability( const TInt aQuery, CBaseMtmUi& aMtmUi )
       
   243     {
       
   244     TInt response = EFalse;
       
   245     TUid capabilityId = TUid::Uid( aQuery ); 
       
   246     
       
   247     // Query the capability from the Mtm
       
   248     TInt error = aMtmUi.QueryCapability( capabilityId, response );
       
   249     
       
   250     // Result is ok, when no error has occurred
       
   251     return !error && response;
       
   252     }    
       
   253 
       
   254 // ----------------------------------------------------------------------------
       
   255 // MuiuEmailTools::QueryMailboxValidityL()
       
   256 // ----------------------------------------------------------------------------
       
   257 //
       
   258 TInt MuiuEmailTools::QueryMailboxValidityL( 
       
   259     const TMsvId aMailboxId,
       
   260     CBaseMtmUi& aMtmUi )
       
   261     {
       
   262     CMsvEntrySelection* selection = new ( ELeave ) CMsvEntrySelection();
       
   263     CleanupStack::PushL( selection );
       
   264     selection->AppendL( aMailboxId );
       
   265     TPckgBuf<TInt> parameter = EFalse;    
       
   266     
       
   267     // Invoke mtm's sync function
       
   268     aMtmUi.InvokeSyncFunctionL( 
       
   269         KUidMsvMtmQuerySupportValidateSelection, *selection, parameter );
       
   270 
       
   271     CleanupStack::PopAndDestroy( selection );
       
   272         
       
   273     return ( parameter() == KErrNone );        
       
   274     }  
       
   275     
       
   276     
       
   277 // ----------------------------------------------------------------------------
       
   278 // MuiuEmailTools::QueryDefaultServiceL()
       
   279 // ----------------------------------------------------------------------------
       
   280 //
       
   281 TMsvId MuiuEmailTools::QueryDefaultServiceL( 
       
   282     CMsvSession& aMsvSession, 
       
   283     const TUid& aMtmId )
       
   284     {
       
   285     TMsvId serviceId = KMsvUnknownServiceIndexEntryId;
       
   286     CClientMtmRegistry* registry = CClientMtmRegistry::NewL( aMsvSession );
       
   287     CleanupStack::PushL( registry );
       
   288     CBaseMtm* mtm = registry->NewMtmL( aMtmId );
       
   289     CleanupStack::PushL( mtm );
       
   290     TRAPD( error, serviceId = mtm->DefaultServiceL() );
       
   291     if( error != KErrNone )
       
   292         {
       
   293         serviceId = KMsvUnknownServiceIndexEntryId;
       
   294         }
       
   295         
       
   296     CleanupStack::PopAndDestroy( 2, registry ); 
       
   297     registry = NULL;
       
   298     mtm = NULL;
       
   299     
       
   300     return serviceId;
       
   301     }           
       
   302             
       
   303 // End of File