emailservices/emailframework/src/CFSMailBrandManagerImpl.cpp
changeset 20 ecc8def7944a
parent 18 578830873419
child 30 759dc5235cdb
equal deleted inserted replaced
18:578830873419 20:ecc8def7944a
    13 *
    13 *
    14 * Description:  ES Protocol Framework branding manager interface definition
    14 * Description:  ES Protocol Framework branding manager interface definition
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
    18 //<qmail>
    19 #include <nmcommonheaders.h>
    19 #include <nmcommonheaders.h>
       
    20 //</qmail>
       
    21 
    20 #include "emailtrace.h"
    22 #include "emailtrace.h"
    21 #include <FSMAILBRANDMANAGER.rsg>
    23 #include <FSMAILBRANDMANAGER.rsg>
    22 #include <barsread.h>
    24 #include <barsread.h>
    23 #include <bautils.h>
    25 #include <bautils.h>
       
    26 #include <mmf/common/mmfcontrollerpluginresolver.h> // CleanupResetAndDestroy
       
    27 // <gmail_brand_issue>
       
    28 #include <etelmm.h>
       
    29 #include <mmtsy_names.h>
       
    30 #include <startupdomainpskeys.h>
       
    31 #include <tzlocalizer.h> // CTzLocalizer
       
    32 #include <centralrepository.h>
       
    33 // </gmail_brand_issue>
    24 //<cmail>
    34 //<cmail>
    25 #include "CFSMailClient.h"
    35 #include "CFSMailClient.h"
    26 //</cmail>
    36 //</cmail>
    27 
    37 
    28 #include "CFSMailBrandManagerImpl.h"
    38 #include "CFSMailBrandManagerImpl.h"
    29 #include "CFSMailBrand.h"
    39 #include "CFSMailBrand.h"
    30 
    40 
    31 
    41 
    32 const TInt KBrandArrayGranularity = 5;
    42 const TInt KBrandArrayGranularity = 5;
    33 _LIT( KResourceFilePath,"\\resource\\fsmailbrandmanager.rsc" );
    43 _LIT( KResourceFilePath,"\\resource\\fsmailbrandmanager.rsc" );
       
    44 
       
    45 // The following are needed to convert "Gmail" brand name to "Google Mail"
       
    46 // in certain countries.
       
    47 // <gmail_brand_issue>
       
    48 
       
    49 _LIT( KBrandNameGmail, "Gmail" );
       
    50 _LIT( KBrandNameGoogleMail, "Google Mail" );
       
    51 
       
    52 _LIT( KMCCGermany, "262" );
       
    53 _LIT( KMCCUK1, "234" );
       
    54 _LIT( KMCCUK2, "235" );
       
    55 
       
    56 const TUint8 KGermanyTzId = 36;
       
    57 const TUint8 KUKTzId = 104;
       
    58 
       
    59 const TInt KMCCValueMaxLength = 3;
       
    60 
       
    61 #ifdef __WINS__
       
    62 LOCAL_C void RetrieveNextToken( TDes8& aContent, TDes& aToken )	
       
    63 	{
       
    64 	_LIT8( KComma, "," );
       
    65 	TInt pos = aContent.Find( KComma );
       
    66 	if ( pos != KErrNotFound ) 
       
    67 		{
       
    68 		aToken.Copy( aContent.MidTPtr( 0, pos ) );
       
    69 		aContent.Copy(
       
    70 		    aContent.RightTPtr( aContent.Length() - pos - 1 ) );
       
    71 		}
       
    72 	}
       
    73 #endif // __WINS__
       
    74 // </gmail_brand_issue>
    34 
    75 
    35 // -----------------------------------------------------------------------------
    76 // -----------------------------------------------------------------------------
    36 // CFSMailBrandManagerImpl::CFSMailBrandManagerImpl
    77 // CFSMailBrandManagerImpl::CFSMailBrandManagerImpl
    37 // -----------------------------------------------------------------------------
    78 // -----------------------------------------------------------------------------
    38 CFSMailBrandManagerImpl::CFSMailBrandManagerImpl(
    79 CFSMailBrandManagerImpl::CFSMailBrandManagerImpl(
   131     {
   172     {
   132     FUNC_LOG;
   173     FUNC_LOG;
   133     
   174     
   134     // list all mailboxes
   175     // list all mailboxes
   135     RPointerArray<CFSMailBox> mailBoxes;
   176     RPointerArray<CFSMailBox> mailBoxes;
   136     TInt rcode = iMailClient.ListMailBoxes( TFSMailMsgId(), mailBoxes );
   177     CleanupResetAndDestroyPushL( mailBoxes );
   137     if( rcode == KErrNone )
   178     iMailClient.ListMailBoxes( TFSMailMsgId(), mailBoxes );
   138         {
   179     // Check is there need to change the name of the mailbox
   139         // Check is there need to change the name of the mailbox
   180     TPtrC name = GetTextL( EFSMailboxName, aMailBoxId);
   140         TPtrC name = GetTextL( EFSMailboxName, aMailBoxId);
   181     // <gmail_brand_issue>
   141         if( name.Length() )
   182     VerifyMailAccountName( name );
       
   183     // </gmail_brand_issue>
       
   184     if( name.Length() )
       
   185         {
       
   186         // check duplicates
       
   187         TInt orderNumber(2);
       
   188         TBool duplicate(ETrue);
       
   189         HBufC* newName = HBufC::NewL(name.Length()+6);
       
   190         TPtrC brandedName = name;
       
   191         while(duplicate != EFalse)
   142             {
   192             {
   143             // check duplicates
   193             duplicate = EFalse;
   144             TInt orderNumber(2);
   194             for(TInt i = 0; i < mailBoxes.Count( );i++)
   145             TBool duplicate(ETrue);
       
   146             HBufC* newName = HBufC::NewL(name.Length()+6);
       
   147             TPtrC brandedName = name;
       
   148             while(duplicate != EFalse)
       
   149                 {
   195                 {
   150                 duplicate = EFalse;
   196                 if(brandedName == mailBoxes[i]->GetName() && 
   151                 for(TInt i = 0; i < mailBoxes.Count( );i++)
   197                    aMailBoxId != mailBoxes[i]->GetId( ))
   152                     {
   198                     {
   153                     if(brandedName == mailBoxes[i]->GetName() && 
   199                     duplicate = ETrue;
   154                        aMailBoxId != mailBoxes[i]->GetId( ))
   200                     newName->Des().Copy(name);
   155                         {
   201                     newName->Des().Append(' ');
   156                         duplicate = ETrue;
   202                     newName->Des().Append('(');
   157                         newName->Des().Copy(name);
   203                     newName->Des().AppendNum(orderNumber++);
   158                         newName->Des().Append(' ');
   204                     newName->Des().Append(')');
   159                         newName->Des().Append('(');
   205                     brandedName.Set(newName->Des());
   160                         newName->Des().AppendNum(orderNumber++);
   206                     break;
   161                         newName->Des().Append(')');
       
   162                         brandedName.Set(newName->Des());
       
   163                         break;
       
   164                         }
       
   165                     }
   207                     }
   166                 }
   208                 }
   167             iMailClient.SetMailboxName(aMailBoxId,brandedName);
       
   168             delete newName;
       
   169             }
   209             }
   170         }
   210         iMailClient.SetMailboxName(aMailBoxId,brandedName);
   171        mailBoxes.ResetAndDestroy();
   211         delete newName;
       
   212         }
       
   213     CleanupStack::PopAndDestroy( &mailBoxes );
   172     }
   214     }
   173 
   215 
   174 // -----------------------------------------------------------------------------
   216 // -----------------------------------------------------------------------------
   175 // CFSMailBrandManagerImpl::GetGraphicL
   217 // CFSMailBrandManagerImpl::GetGraphicL
   176 // -----------------------------------------------------------------------------
   218 // -----------------------------------------------------------------------------
   200 	
   242 	
   201 	return brand->GetGraphicL( aElement );
   243 	return brand->GetGraphicL( aElement );
   202 	}
   244 	}
   203 
   245 
   204 // -----------------------------------------------------------------------------
   246 // -----------------------------------------------------------------------------
       
   247 // CFSMailBrandManagerImpl::GetGraphicL
       
   248 // -----------------------------------------------------------------------------
       
   249 CGulIcon* CFSMailBrandManagerImpl::GetGraphicL(
       
   250     TFSBrandElement aElement, 
       
   251     const TDesC& aBrandId )
       
   252     {
       
   253     FUNC_LOG;
       
   254     CFSMailBrand* brand = FindMatchingBrandL( aBrandId );
       
   255     if ( brand == NULL )
       
   256         {
       
   257         return NULL;    
       
   258         }    
       
   259     return brand->GetGraphicL( aElement );
       
   260     }
       
   261     
       
   262 // -----------------------------------------------------------------------------
   205 // CFSMailBrandManagerImpl::GetGraphicIdsL
   263 // CFSMailBrandManagerImpl::GetGraphicIdsL
   206 // -----------------------------------------------------------------------------
   264 // -----------------------------------------------------------------------------
   207 TInt CFSMailBrandManagerImpl::GetGraphicIdsL(
   265 TInt CFSMailBrandManagerImpl::GetGraphicIdsL(
   208     TFSBrandElement aElement, 
   266     TFSBrandElement aElement, 
   209 	const TFSMailMsgId& aMailboxId,
   267 	const TFSMailMsgId& aMailboxId,
   227 
   285 
   228 	if ( brand == NULL )
   286 	if ( brand == NULL )
   229 	    {
   287 	    {
   230 	    return KErrNotFound;
   288 	    return KErrNotFound;
   231 	    }
   289 	    }
       
   290     return brand->GetGraphicIdsL( aElement, aIconIds );
       
   291     }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CFSMailBrandManagerImpl::GetGraphicIdsL
       
   295 // -----------------------------------------------------------------------------
       
   296 TInt CFSMailBrandManagerImpl::GetGraphicIdsL(
       
   297     TFSBrandElement aElement, 
       
   298     const TDesC& aBrandId,
       
   299     TDes& aIconIds  )
       
   300     {
       
   301     FUNC_LOG;
       
   302 
       
   303     CFSMailBrand* brand = FindMatchingBrandL( aBrandId );
       
   304     if ( brand == NULL )
       
   305         {
       
   306         return KErrNotFound;
       
   307         }
   232     return brand->GetGraphicIdsL( aElement, aIconIds );
   308     return brand->GetGraphicIdsL( aElement, aIconIds );
   233     }
   309     }
   234 
   310 
   235 // -----------------------------------------------------------------------------
   311 // -----------------------------------------------------------------------------
   236 // CFSMailBrandManagerImpl::GetTextL
   312 // CFSMailBrandManagerImpl::GetTextL
   334 // CFSMailBrandManagerImpl::FindMatchingBrandL
   410 // CFSMailBrandManagerImpl::FindMatchingBrandL
   335 // -----------------------------------------------------------------------------
   411 // -----------------------------------------------------------------------------
   336 CFSMailBrand* CFSMailBrandManagerImpl::FindMatchingBrandL( const TDesC& aBrandId )
   412 CFSMailBrand* CFSMailBrandManagerImpl::FindMatchingBrandL( const TDesC& aBrandId )
   337     {
   413     {
   338     FUNC_LOG;
   414     FUNC_LOG;
   339     TInt brandCount( iBrands->Count() );
   415     if( aBrandId.Length() )
   340     for ( TInt i( 0 ); i < brandCount; i++ )
   416         {
   341         {
   417         TInt brandCount( iBrands->Count() );
   342         if ( (*iBrands)[ i ]->IsMatching( aBrandId ) )
   418         for ( TInt i( 0 ); i < brandCount; i++ )
   343             return (*iBrands)[ i ];
   419             {
       
   420             if ( (*iBrands)[ i ]->IsMatching( aBrandId ) )
       
   421                 return (*iBrands)[ i ];
       
   422             }
   344         }
   423         }
   345     return NULL;
   424     return NULL;
   346     }
   425     }
   347 
   426 
   348 // -----------------------------------------------------------------------------
   427 // -----------------------------------------------------------------------------
   349 // CFSMailBrandManagerImpl::MailboxMatchingBrandIdL
   428 // CFSMailBrandManagerImpl::MailboxMatchingBrandIdL
   350 // -----------------------------------------------------------------------------
   429 // -----------------------------------------------------------------------------
   351 CFSMailBox* CFSMailBrandManagerImpl::MailboxMatchingBrandIdL(
   430 CFSMailBox* CFSMailBrandManagerImpl::MailboxMatchingBrandIdL(
   352     const TDesC& aBrandId ) const
   431     const TDesC& aBrandId ) const
   353     {
   432     {    
   354     FUNC_LOG;
       
   355     RPointerArray<CFSMailBox> mailboxes;
   433     RPointerArray<CFSMailBox> mailboxes;
   356     TInt outcome( KErrNone );
   434 
   357     
   435     CleanupResetAndDestroyPushL( mailboxes );
   358     outcome = iMailClient.ListMailBoxes( TFSMailMsgId(), mailboxes );
   436 
   359     if ( outcome != KErrNone )
   437     iMailClient.ListMailBoxes( TFSMailMsgId(), mailboxes );
   360         {
   438 
   361         mailboxes.ResetAndDestroy();
       
   362         User::Leave( outcome );
       
   363         }
       
   364     
       
   365     TInt mailboxCount( mailboxes.Count() );
   439     TInt mailboxCount( mailboxes.Count() );
   366     TInt mailboxIndexer( 0 );
   440     TInt mailboxIndexer( 0 );
   367     while ( mailboxIndexer < mailboxCount )
   441     while ( mailboxIndexer < mailboxCount )
   368         {
   442         {
   369         TDesC* brandId( NULL );
   443         TDesC* brandId( NULL );
   384             }
   458             }
   385         
   459         
   386         ++mailboxIndexer;
   460         ++mailboxIndexer;
   387         }
   461         }
   388         
   462         
   389     mailboxes.ResetAndDestroy();
   463     CleanupStack::PopAndDestroy( &mailboxes );
   390     User::Leave( KErrNotFound );
   464     User::Leave( KErrNotFound );
   391     return NULL; // To prevent warning
   465     return NULL; // To prevent warning
   392     }
   466     }
   393 
   467 
       
   468 // <gmail_brand_issue>
       
   469 // -----------------------------------------------------------------------------
       
   470 // CFSMailBrandManagerImpl::GetMCCValueL
       
   471 // -----------------------------------------------------------------------------
       
   472 void CFSMailBrandManagerImpl::GetMCCValueL( TDes& aMcc ) const
       
   473     {
       
   474     aMcc.Zero();
       
   475     
       
   476     TInt err = KErrNone;
       
   477 
       
   478 #ifndef __WINS__
       
   479 
       
   480     TBool isSimPresent = EFalse;
       
   481     
       
   482     TInt simStatus( 0 );
       
   483 
       
   484 	err = RProperty::Get( KPSUidStartup, KPSSimStatus, simStatus );
       
   485 
       
   486 	if ( err == KErrNone &&
       
   487 	        simStatus != ESimNotPresent &&
       
   488 	        simStatus != ESimNotSupported )
       
   489 	    {
       
   490 	    isSimPresent = ETrue;
       
   491 	    }
       
   492 
       
   493     if ( isSimPresent )
       
   494         {
       
   495         // We cannot let the method leave here
       
   496         TRAP( err, GetMCCValueFromSIML( aMcc ) );
       
   497 
       
   498         // If reading from SIM fails one time try again after 0.5 secs
       
   499         if ( err != KErrNone )
       
   500             {
       
   501             const TInt KHalfSecond = 500000;
       
   502 
       
   503             User::After( KHalfSecond );
       
   504             
       
   505             err = KErrNone;
       
   506 
       
   507             // We cannot let the method leave here
       
   508             TRAP( err, GetMCCValueFromSIML( aMcc ) );
       
   509             }
       
   510         }
       
   511 
       
   512 #else // __WINS__
       
   513 
       
   514     _LIT( KSIMInfo, "C:\\data\\Settings\\SIMInfo.txt" );
       
   515 
       
   516     RFs fs;
       
   517 
       
   518     User::LeaveIfError( fs.Connect() );
       
   519 
       
   520     RFile simFile;
       
   521 
       
   522     err = simFile.Open( fs, KSIMInfo(), EFileShareReadersOnly |
       
   523                                         EFileStream |
       
   524                                         EFileRead );
       
   525     if ( err == KErrNone )
       
   526         {
       
   527         TBuf8<100> content;
       
   528         TBuf<100> dummy;
       
   529 
       
   530         simFile.Read( content );
       
   531 
       
   532     	simFile.Close();
       
   533 
       
   534     	fs.Close();
       
   535 
       
   536         if ( content.Length() > 0 )
       
   537         	{
       
   538             RetrieveNextToken( content, dummy );
       
   539             RetrieveNextToken( content, dummy );
       
   540             RetrieveNextToken( content, dummy );
       
   541             RetrieveNextToken( content, dummy );
       
   542 
       
   543             RMobilePhone::TMobilePhoneSubscriberId subscriberId;
       
   544 
       
   545             RetrieveNextToken( content, subscriberId );
       
   546 
       
   547             if ( subscriberId.Length() >= KMCCValueMaxLength )
       
   548                 {
       
   549                 aMcc = subscriberId.Left( KMCCValueMaxLength );
       
   550                 }
       
   551         	}
       
   552         }
       
   553 
       
   554 #endif // __WINS__
       
   555 
       
   556     }
       
   557 
       
   558 // ----------------------------------------------------------------------------
       
   559 // CFSMailBrandManagerImpl::GetMCCValueFromSIML
       
   560 // ----------------------------------------------------------------------------
       
   561 // 
       
   562 void CFSMailBrandManagerImpl::GetMCCValueFromSIML( TDes& aMcc ) const
       
   563     {
       
   564     RTelServer telServer;
       
   565 
       
   566     CleanupClosePushL( telServer );
       
   567 
       
   568     User::LeaveIfError( telServer.Connect() );
       
   569 
       
   570     User::LeaveIfError( telServer.LoadPhoneModule( KMmTsyModuleName ) );
       
   571     
       
   572     RMobilePhone mobilePhone;
       
   573 
       
   574     CleanupClosePushL( mobilePhone );
       
   575 
       
   576     User::LeaveIfError( mobilePhone.Open( telServer, KMmTsyPhoneName ) );
       
   577     
       
   578     TRequestStatus status;
       
   579 
       
   580     RMobilePhone::TMobilePhoneSubscriberId subscriberId;       
       
   581 
       
   582     mobilePhone.GetSubscriberId( status, subscriberId );
       
   583     
       
   584     User::WaitForRequest( status );
       
   585 
       
   586     User::LeaveIfError( status.Int() );
       
   587     
       
   588     CleanupStack::PopAndDestroy( &mobilePhone );
       
   589 
       
   590     CleanupStack::PopAndDestroy( &telServer );
       
   591 
       
   592     if ( subscriberId.Length() >= KMCCValueMaxLength )
       
   593         {
       
   594         aMcc = subscriberId.Left( KMCCValueMaxLength );
       
   595         }
       
   596     }
       
   597 
       
   598 // ----------------------------------------------------------------------------
       
   599 // CFSMailBrandManagerImpl::GetCurrentCountryL
       
   600 // ----------------------------------------------------------------------------
       
   601 // 
       
   602 TUint8 CFSMailBrandManagerImpl::GetCurrentCountryL() const
       
   603     {
       
   604     CTzLocalizer* localizer = CTzLocalizer::NewLC();
       
   605 
       
   606     CTzLocalizedCity* city = localizer->GetFrequentlyUsedZoneCityL(
       
   607         CTzLocalizedTimeZone::ECurrentZone );
       
   608         
       
   609     CleanupStack::PushL( city );
       
   610 
       
   611     CTzLocalizedCityGroup* cityGroup = 
       
   612         localizer->GetCityGroupL( city->GroupId() );
       
   613 
       
   614     TUint8 countryId = cityGroup->Id();
       
   615     
       
   616     delete cityGroup;
       
   617     cityGroup = NULL;
       
   618     
       
   619     CleanupStack::PopAndDestroy( 2, localizer );
       
   620     
       
   621     return countryId;
       
   622     }
       
   623 
       
   624 // -----------------------------------------------------------------------------
       
   625 // CFSMailBrandManagerImpl::VerifyMailAccountName
       
   626 // -----------------------------------------------------------------------------
       
   627 void CFSMailBrandManagerImpl::VerifyMailAccountName(
       
   628         TPtrC& aBrandedName ) const
       
   629     {
       
   630     // Due to legal reasons we don't show brand name "Gmail" in Germany and UK
       
   631     if ( !aBrandedName.CompareF( KBrandNameGmail ) )
       
   632         {
       
   633         // First check timezone id
       
   634         TUint8 timeZone = 0;
       
   635         
       
   636         TRAPD( err, timeZone = GetCurrentCountryL() );
       
   637         
       
   638         if ( err == KErrNone && ( timeZone == KGermanyTzId ||
       
   639                                   timeZone == KUKTzId ) )
       
   640             {
       
   641             aBrandedName.Set( KBrandNameGoogleMail );
       
   642             }
       
   643         // Then if necessary check MCC
       
   644         else
       
   645             {
       
   646             TBuf<KMCCValueMaxLength> mcc;
       
   647 
       
   648             TRAPD( err2, GetMCCValueL( mcc ) );
       
   649             
       
   650             if ( err2 == KErrNone && ( mcc == KMCCGermany ||
       
   651                                        mcc == KMCCUK1 ||
       
   652                                        mcc == KMCCUK2 ) )
       
   653                 {
       
   654                 aBrandedName.Set( KBrandNameGoogleMail );
       
   655                 }
       
   656             }
       
   657 
       
   658         }
       
   659     }
       
   660 // </gmail_brand_issue>