sysstatemgmt/syslangutil/ssmlangselcmd/src/ssmlangselcmd.cpp
branchRCL_3
changeset 21 ccb4f6b3db21
parent 0 4e1aa6a622a0
child 22 8cb079868133
equal deleted inserted replaced
20:1ddbe54d0645 21:ccb4f6b3db21
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8 *
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    20 #include "ssmmapperutility.h"
    20 #include "ssmmapperutility.h"
    21 #include "ssmalternativelanguages.h"
    21 #include "ssmalternativelanguages.h"
    22 #include "ssmlanguageloader.h"
    22 #include "ssmlanguageloader.h"
    23 #include "syslangutilprivatecrkeys.h"
    23 #include "syslangutilprivatecrkeys.h"
    24 #include "trace.h"
    24 #include "trace.h"
       
    25 #include "ssmmapperutilityinternalpskeys.h"
    25 
    26 
    26 #include <syslangutil.h>
    27 #include <syslangutil.h>
    27 #include <CommonEngineDomainCRKeys.h>
    28 #include <CommonEngineDomainCRKeys.h>
    28 #include <centralrepository.h>
    29 #include <centralrepository.h>
    29 
    30 
    30 
    31 
    31 /** Default granularity for list of installed languages. */
    32 /** Default granularity for list of installed languages. */
    32 const TInt KLanguageListGranularity( 4 );
    33 const TInt KLanguageListGranularity( 4 );
    33 
    34 
       
    35 /** Any of the code set to zero means auto selection is enabled. */
       
    36 const TInt KSettingAutomatic = 0;
       
    37 
       
    38 /** The maximum number of regions to be appended to the hash set. */
       
    39 const TInt KMaxNumberOfRegions = 56;
       
    40 
       
    41 // ======== LOCAL FUNCTIONS ========
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // HashLangRegionMappingFunction
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 inline TUint32 HashLangRegionMappingFunction(const TLanguageRegion& aMapping)
       
    48     {
       
    49     return aMapping.iLanguage;
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // LangRegionMappingIdentityRelation
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 inline TBool LangRegionMappingIdentityRelation(const TLanguageRegion& aMapping1,
       
    57     const TLanguageRegion& aMapping2)
       
    58     {
       
    59     return (aMapping1.iLanguage == aMapping2.iLanguage);
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // RegionLangMappingIdentityRelation
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 inline TBool RegionLangMappingIdentityRelation(const TLanguageRegion& aMapping1,
       
    67     const TLanguageRegion& aMapping2)
       
    68     {
       
    69     return (aMapping1.iRegion == aMapping2.iRegion);
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CompareLanguageMapping
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 static TInt CompareLanguageMapping(TLanguageRegion const &aLanguage1, TLanguageRegion const &aLanguage2)
       
    77     {
       
    78     if( aLanguage1.iLanguage == aLanguage2.iLanguage )
       
    79         {
       
    80         return 0;
       
    81         }
       
    82     return ( aLanguage1.iLanguage < aLanguage2.iLanguage ) ? -1 : 1;
       
    83     }
    34 
    84 
    35 // ======== MEMBER FUNCTIONS ========
    85 // ======== MEMBER FUNCTIONS ========
    36 
    86 
    37 // ---------------------------------------------------------------------------
    87 // ---------------------------------------------------------------------------
    38 // CSsmLangSelCmd::NewL
    88 // CSsmLangSelCmd::NewL
    46     self->ConstructL();
    96     self->ConstructL();
    47     CleanupStack::Pop( self );
    97     CleanupStack::Pop( self );
    48 	return self;	
    98 	return self;	
    49 	}
    99 	}
    50 
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CSsmLangSelCmd::CSsmLangSelCmd
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 CSsmLangSelCmd::CSsmLangSelCmd() : CActive( EPriorityNormal ),
       
   106     iState( EQueryListNone ),
       
   107     iLangRegionMappingHashSet( &::HashLangRegionMappingFunction, &::LangRegionMappingIdentityRelation )
       
   108     {
       
   109     FUNC_LOG;
       
   110 
       
   111     CActiveScheduler::Add( this );
       
   112     }
    51 
   113 
    52 // ---------------------------------------------------------------------------
   114 // ---------------------------------------------------------------------------
    53 // CSsmLangSelCmd::~CSsmLangSelCmd
   115 // CSsmLangSelCmd::~CSsmLangSelCmd
    54 // ---------------------------------------------------------------------------
   116 // ---------------------------------------------------------------------------
    55 //
   117 //
    56 CSsmLangSelCmd::~CSsmLangSelCmd()
   118 CSsmLangSelCmd::~CSsmLangSelCmd()
    57     {
   119     {
    58     FUNC_LOG;
   120     FUNC_LOG;
       
   121     Cancel();
    59     delete iLangList;
   122     delete iLangList;
    60     delete iMapperUtility;
   123     delete iMapperUtility;
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // CSsmLangSelCmd::ConstructL
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 void CSsmLangSelCmd::ConstructL()
       
   131     {
       
   132     FUNC_LOG;
       
   133     iMapperUtility = CSsmMapperUtility::NewL();
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // CSsmLangSelCmd::Initialize
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140  TInt CSsmLangSelCmd::Initialize( CSsmCustomCommandEnv* aCmdEnv )
       
   141      {
       
   142      FUNC_LOG;
       
   143 
       
   144      iEnv = aCmdEnv;
       
   145      TRAPD( errorCode, InitializeL() );
       
   146      return errorCode;
       
   147      }
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // CSsmLangSelCmd::InitializeL
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 void CSsmLangSelCmd::InitializeL()
       
   154     {
       
   155     FUNC_LOG;
       
   156     ASSERT_TRACE( iEnv );
       
   157 
       
   158     iValidLanguages = new ( ELeave ) CArrayFixFlat<TInt>( KLanguageListGranularity );
       
   159     
       
   160     RFs* fsSession = &( const_cast<RFs&>( iEnv->Rfs() ) );
       
   161     
       
   162     //Get the list of installed languages
       
   163     TInt errorCode = SysLangUtil::GetInstalledLanguages( iValidLanguages,
       
   164                                                          fsSession );
       
   165 
       
   166     ERROR( errorCode, "Failed to get installed languages" );
       
   167     User::LeaveIfError( errorCode );
       
   168     
       
   169     //Initialize the Language and Region mapping hash set
       
   170     InitializeRegionMappingL();
       
   171 
       
   172     //Get the number of installed languages
       
   173     const TInt validLangCount = iValidLanguages->Count();
       
   174     TLinearOrder<TLanguageRegion> order(&CompareLanguageMapping);
       
   175     
       
   176     //Get the mapped regions for the installed languages
       
   177     for(TInt index = 0; index < validLangCount; ++index) 
       
   178         {
       
   179         TLanguageRegion region = iLangRegionMappingHashSet.FindL(TLanguageRegion(iValidLanguages->At(index)));
       
   180         INFO_2( "Found Region code = %d for language %d", region.iRegion, region.iLanguage );
       
   181         iRegionsArray.InsertInOrderL(region, order);
       
   182         }
       
   183 
       
   184     //Connect to Misc adaptation
       
   185     errorCode = iAdaptation.Connect();
       
   186     ERROR( errorCode, "Failed to connect to RSsmMiscAdaptation" );
       
   187     User::LeaveIfError( errorCode );
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------------------------
       
   191 // CSsmLangSelCmd::InitializeRegionMappingL
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 void CSsmLangSelCmd::InitializeRegionMappingL()
       
   195     {
       
   196     //Reserve the memory for the number of mappings to be appended
       
   197     iLangRegionMappingHashSet.ReserveL( KMaxNumberOfRegions );
       
   198 
       
   199     //Insert the Language - Region mapping
       
   200     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangEnglish,             ERegGBR ) );
       
   201     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangFrench,              ERegFRA ) );
       
   202     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangGerman,              ERegDEU ) );
       
   203     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangSpanish,             ERegESP ) );
       
   204     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangItalian,             ERegITA ) );
       
   205     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangSwedish,             ERegSWE ) );
       
   206     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangDanish,              ERegDNK ) );
       
   207     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangNorwegian,           ERegNOR ) );
       
   208     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangFinnish,             ERegFIN ) );
       
   209     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangAmerican,            ERegUSA ) );
       
   210     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangPortuguese,          ERegPRT ) );
       
   211     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangTurkish,             ERegTUR ) );
       
   212     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangIcelandic,           ERegISL ) );
       
   213     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangRussian,             ERegRUS ) );
       
   214     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangHungarian,           ERegHUN ) );
       
   215     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangDutch,               ERegNLD ) );
       
   216     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangBelgianFlemish,      ERegBEL ) );
       
   217     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangCzech,               ERegCZE ) );
       
   218     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangSlovak,              ERegSVK ) );
       
   219     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangPolish,              ERegPOL ) );
       
   220     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangSlovenian,           ERegSVN ) );
       
   221     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangTaiwanChinese,       ERegTWN ) );
       
   222     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangHongKongChinese,     ERegHKG ) );
       
   223     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangPrcChinese,          ERegCHN ) );
       
   224     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangJapanese,            ERegJPN ) );
       
   225     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangThai,                ERegTHA ) );
       
   226     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangArabic,              ERegARE ) );
       
   227     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangTagalog,             ERegPHL ) );
       
   228     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangBulgarian,           ERegBGR ) );
       
   229     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangCatalan,             ERegESP ) );
       
   230     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangCroatian,            ERegHRV ) );
       
   231     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangEstonian,            ERegEST ) );
       
   232     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangFarsi,               ERegIRN ) );
       
   233     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangCanadianFrench,      ERegCAN ) );
       
   234     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangGreek,               ERegGRC ) );
       
   235     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangHebrew,              ERegISR ) );
       
   236     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangHindi,               ERegIND ) );
       
   237     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangIndonesian,          ERegIDN ) );
       
   238     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangLatvian,             ERegLVA ) );
       
   239     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangLithuanian,          ERegLTU ) );
       
   240     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangMalay,               ERegMYS ) );
       
   241     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangBrazilianPortuguese, ERegBRA ) );
       
   242     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangRomanian,            ERegROU ) );
       
   243     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangSerbian,             ERegSCG ) );
       
   244     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangLatinAmericanSpanish, ERegMEX ));
       
   245     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangUkrainian,           ERegUKR ) );
       
   246     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangUrdu,                ERegPAK ) );
       
   247     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangVietnamese,          ERegVNM ) );
       
   248 #ifdef __E32LANG_H__
       
   249     // 5.0
       
   250     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangBasque,              ERegESP ) );
       
   251     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangGalician,            ERegESP ) );
       
   252 #endif //__E32LANG_H__
       
   253 #if !defined(__SERIES60_31__)
       
   254     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangEnglish_Apac,        ERegGBR ) );
       
   255     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangEnglish_Taiwan,      ERegTWN ) );
       
   256     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangEnglish_HongKong,    ERegHKG ) );
       
   257     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangEnglish_Prc,         ERegCHN ) );
       
   258     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangEnglish_Japan,       ERegJPN ) );
       
   259     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangEnglish_Thailand,    ERegTHA ) );
       
   260     iLangRegionMappingHashSet.InsertL( TLanguageRegion( ELangMalay_Apac,          ERegMYS ) );
       
   261 #endif //!defined(__SERIES60_31__)
       
   262     }
       
   263 
       
   264 // ---------------------------------------------------------------------------
       
   265 // CSsmLangSelCmd::Execute
       
   266 // ---------------------------------------------------------------------------
       
   267 //
       
   268 void CSsmLangSelCmd::Execute( const TDesC8& /*aParams*/, TRequestStatus& aRequest )
       
   269     {
       
   270     FUNC_LOG;
       
   271     
       
   272     ASSERT_TRACE( !iClientStatus );
       
   273     ASSERT_TRACE( !IsActive() );
       
   274     ASSERT_TRACE( iEnv );
       
   275 
       
   276     aRequest = KRequestPending;
       
   277     iClientStatus = &aRequest;
       
   278 
       
   279     RFs* fsSession = &( const_cast<RFs&>( iEnv->Rfs() ) );
       
   280     
       
   281     //Get the default language
       
   282     TInt errorCode = SysLangUtil::GetDefaultLanguage( iDefaultLanguage, fsSession );
       
   283     ERROR( errorCode, "Failed to get default language" );
       
   284 
       
   285     //Get the language selected
       
   286     TInt lastSelectedLang = GetLastSelectedLang();
       
   287     TInt lastSelectedRegion ( KErrNotFound );
       
   288     TInt lastSelectedCollation ( KErrNotFound );
       
   289     
       
   290     //Reset error code to KErrNotFound
       
   291     errorCode = KErrNotFound;
       
   292     
       
   293     if ( KSettingAutomatic == lastSelectedLang )
       
   294         {
       
   295         //Auto selection is enabled
       
   296         SetIndividualSettingsToCentRep( KSettingAutomatic, KSettingAutomatic );
       
   297         PrepareNextList();
       
   298         }
       
   299     else if ( ValidateAndGetSettings( lastSelectedLang, lastSelectedRegion, lastSelectedCollation ) )
       
   300         {
       
   301         //Validate and get the selected Language, Region and Collation code
       
   302         errorCode = UseLocale( lastSelectedLang , lastSelectedRegion, lastSelectedCollation );
       
   303         ERROR( errorCode, "Failed to load selected Language, Region and Collation" );
       
   304         }
       
   305 
       
   306     //Load the default Language, Region and Collation in case of loading selected codes failed 
       
   307     //and auto selection is disabled.
       
   308     if( KErrNone != errorCode && KSettingAutomatic != lastSelectedLang )
       
   309         {
       
   310         //Validate and use the default codes 
       
   311         errorCode = ValidateAndUseDefaultLanguage();
       
   312         ERROR( errorCode, "Failed to load default Language, Region and Collation" );
       
   313         }
       
   314     }
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // CSsmLangSelCmd::GetLastSelectedLang
       
   318 //
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 TInt CSsmLangSelCmd::GetLastSelectedLang()
       
   322     {
       
   323     FUNC_LOG;
       
   324 
       
   325     TInt value( 0 );
       
   326     TInt errorCode = iMapperUtility->CrValue( KCRUidCommonEngineKeys, 
       
   327                                               KGSDisplayTxtLang,
       
   328                                               value );
       
   329     ERROR( errorCode, "Failed read KGSDisplayTxtLang CenRep key with error= %d"  );       
       
   330     
       
   331     INFO_1( "Last selected language is %d", value );
       
   332     return value;
       
   333     }
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // CSsmLangSelCmd::SetIndividualSettingsToCentRep
       
   337 //
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 TInt CSsmLangSelCmd::SetIndividualSettingsToCentRep( const TInt aRegion, const TInt aCollation )
       
   341     {
       
   342     FUNC_LOG;
       
   343     CRepository* cenrep = NULL;
       
   344     TRAPD( errorCode, cenrep = CRepository::NewL( KCRUidCommonEngineKeys ) );
       
   345   
       
   346     if ( KErrNone == errorCode )
       
   347         {
       
   348         //Set the Region CR with the given value
       
   349         errorCode = cenrep->Set( KGSRegion, aRegion );
       
   350         ERROR( errorCode, "Failed to Set Region code to CentRep, %d"  );                
       
   351         }
       
   352     else
       
   353         {
       
   354         INFO_1( "Failed to initialize cen rep %d", errorCode );        
       
   355         }
       
   356 
       
   357     if ( KErrNone == errorCode )
       
   358         {
       
   359         //Set the Collation CR with the given value
       
   360         errorCode = cenrep->Set( KGSCollation, aCollation );
       
   361         ERROR( errorCode, "Failed to Set Collation code to CentRep, %d"  );               
       
   362         }
       
   363     delete cenrep;
       
   364     return errorCode;
       
   365     }
       
   366 
       
   367 // ---------------------------------------------------------------------------
       
   368 // CSsmLangSelCmd::ValidateAndGetSettings
       
   369 //
       
   370 // ---------------------------------------------------------------------------
       
   371 //
       
   372 TBool CSsmLangSelCmd::ValidateAndGetSettings( const TInt aLastSelectedLang, TInt& aLastSelectedRegion, TInt& aLastSelectedCollation )
       
   373     {
       
   374     TBool isValid( EFalse );
       
   375     //Validate and get the selected settings
       
   376 	TInt errorCode = KErrNone;
       
   377     TRAP( errorCode, isValid = ValidateAndGetSettingsL( aLastSelectedLang, aLastSelectedRegion, aLastSelectedCollation ) );
       
   378     ERROR_1( errorCode, "Validation failed for the selected Language, region and Collation with error %d", errorCode );
       
   379     return isValid;
       
   380     }
       
   381 
       
   382 // ---------------------------------------------------------------------------
       
   383 // CSsmLangSelCmd::ValidateAndGetSettingsL
       
   384 //
       
   385 // ---------------------------------------------------------------------------
       
   386 //
       
   387 TBool CSsmLangSelCmd::ValidateAndGetSettingsL( const TInt aLastSelectedLang, TInt& aLastSelectedRegion, TInt& aLastSelectedCollation )
       
   388     {
       
   389     TBool isValid( EFalse );
       
   390 
       
   391     //Get the selected Region and Collation 
       
   392     GetIndividualSettingsFromCentRepL( aLastSelectedRegion, aLastSelectedCollation );
       
   393 
       
   394     //Check whether the selected Language is valid or not
       
   395     if( IsValid( aLastSelectedLang ) )
       
   396         {
       
   397          if (KSettingAutomatic >= aLastSelectedRegion )
       
   398             {
       
   399             //Selected Region is Automatic. Get the Region associated 
       
   400             //with the Language being loaded
       
   401             if( ValidateAndGetMappedRegion( aLastSelectedLang, aLastSelectedRegion ) )
       
   402                 {
       
   403                 isValid = ETrue;
       
   404                 }
       
   405             }
       
   406          else if( IsRegionValidL( aLastSelectedRegion ) )
       
   407                 {
       
   408                 //Selected Region is a valid Region
       
   409                 isValid = ETrue;
       
   410                 }
       
   411             
       
   412          if ( KSettingAutomatic >= aLastSelectedCollation )
       
   413              {
       
   414              //Collation is not set. Use the language code as the collation code
       
   415              aLastSelectedCollation = aLastSelectedLang;
       
   416              }
       
   417         }
       
   418     
       
   419     return isValid;
       
   420     }
       
   421 
       
   422 // ---------------------------------------------------------------------------
       
   423 // CSsmLangSelCmd::IsValid
       
   424 //
       
   425 // ---------------------------------------------------------------------------
       
   426 //
       
   427 TBool CSsmLangSelCmd::IsValid( const TInt aLanguage ) const
       
   428     {
       
   429     FUNC_LOG;
       
   430     ASSERT_TRACE( iValidLanguages );
       
   431 
       
   432     TBool valid( EFalse );
       
   433     TKeyArrayFix keyProp( 0, ECmpTInt );
       
   434     TInt index( KErrNotFound );
       
   435     // Returns zero if element is found.
       
   436     if ( 0 == iValidLanguages->Find( aLanguage, keyProp, index ) )
       
   437         {
       
   438         valid = ETrue;
       
   439         }
       
   440     
       
   441     return valid;
       
   442     }
       
   443 
       
   444 // ---------------------------------------------------------------------------
       
   445 // CSsmLangSelCmd::GetLanguageFromCentRep
       
   446 //
       
   447 // ---------------------------------------------------------------------------
       
   448 //
       
   449 TInt CSsmLangSelCmd::GetLanguageFromCentRep()
       
   450     {
       
   451     FUNC_LOG;
       
   452 
       
   453     TInt lang = iDefaultLanguage;
       
   454     TInt errorCode = iMapperUtility->CrValue( KCRUidSysLangUtil,
       
   455                                               KSysLangUtilSimLanguage,
       
   456                                               lang );
       
   457     
       
   458     ERROR_1( errorCode, "Failed to read language code from CenRep %d", 
       
   459              errorCode );
       
   460     return lang;
       
   461     }
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // CSsmLangSelCmd::IsRegionValidL
       
   465 //
       
   466 // ---------------------------------------------------------------------------
       
   467 //
       
   468 TBool CSsmLangSelCmd::IsRegionValidL( const TInt aRegion ) const
       
   469     {
       
   470     FUNC_LOG;
       
   471     TInt index = iRegionsArray.FindL(TLanguageRegion(0, aRegion), &::RegionLangMappingIdentityRelation);
       
   472 
       
   473     return ETrue;
       
   474     }
       
   475 
       
   476 // ---------------------------------------------------------------------------
       
   477 // CSsmLangSelCmd::ValidRegionAndGetMappedLanguage
       
   478 //
       
   479 // ---------------------------------------------------------------------------
       
   480 //
       
   481 TBool CSsmLangSelCmd::ValidateAndGetMappedRegion( const TInt aLanguage, TInt& aRegion )
       
   482     {
       
   483     FUNC_LOG;
       
   484 
       
   485     TBool valid( EFalse );
       
   486     TLanguageRegion lang( aLanguage );
       
   487 
       
   488     //Find the Language - Region mapping for the given Language
       
   489     TInt index = iRegionsArray.Find( lang, &::LangRegionMappingIdentityRelation );
       
   490     if( 0 <= index )
       
   491         {
       
   492         //Set the Region associated with the given Language
       
   493         aRegion = iRegionsArray[index].iRegion;
       
   494         INFO_1( "ValidRegionAndGetMappedLanguage:: Found region %d", aRegion);
       
   495         valid = ETrue;
       
   496         }
       
   497     else
       
   498         {
       
   499         INFO_1( "ValidRegionAndGetMappedLanguage:: Not Found region for lang %d", aLanguage);    
       
   500         }
       
   501 
       
   502     return valid;
       
   503     }
       
   504 
       
   505 // ---------------------------------------------------------------------------
       
   506 // CSsmLangSelCmd::GetIndividualSettingsFromCentRepL
       
   507 //
       
   508 // ---------------------------------------------------------------------------
       
   509 //
       
   510 void CSsmLangSelCmd::GetIndividualSettingsFromCentRepL( TInt& aRegion, TInt& aCollation )
       
   511     {
       
   512     FUNC_LOG;
       
   513     CRepository* cenrep = CRepository::NewL( KCRUidCommonEngineKeys );
       
   514     CleanupStack::PushL( cenrep );
       
   515     
       
   516     //Get the last selected Region from central repository
       
   517     User::LeaveIfError(cenrep->Get( KGSRegion, aRegion));   
       
   518         
       
   519     //Get the last selected Collation from central repository
       
   520     User::LeaveIfError(cenrep->Get( KGSCollation, aCollation));    
       
   521     
       
   522     INFO_2( "Last selected region %d and collation %d", aRegion, aCollation );
       
   523     CleanupStack::PopAndDestroy( cenrep );
       
   524     }
       
   525 
       
   526 // ---------------------------------------------------------------------------
       
   527 // CSsmLangSelCmd::ValidateAndUseDefaultLanguage
       
   528 //
       
   529 // ---------------------------------------------------------------------------
       
   530 //
       
   531 TInt CSsmLangSelCmd::ValidateAndUseDefaultLanguage()
       
   532     {
       
   533     FUNC_LOG;
       
   534     TLanguageRegion langRegion;
       
   535     
       
   536     //Get the Region associated with default Language
       
   537     TRAPD( error, langRegion = iLangRegionMappingHashSet.FindL( TLanguageRegion( iDefaultLanguage ) ) );
       
   538     if( KErrNone == error )
       
   539        {
       
   540        //Load the settings for default Language
       
   541        error = UseLocale( iDefaultLanguage, langRegion.iRegion, iDefaultLanguage );
       
   542        ERROR_1( error, "Failed to load default language setting %d", error );
       
   543        }
       
   544     else
       
   545         {
       
   546         INFO_1( "Failed to find the Region associated with default Language %d", error );
       
   547         }
       
   548 
       
   549     if ( iClientStatus )
       
   550         {
       
   551         ERROR_1( error, "Language selection custom command completed with error %d", error );
       
   552         User::RequestComplete( iClientStatus, error );
       
   553         iClientStatus = NULL;
       
   554         }
       
   555 
       
   556     return error;
       
   557     }
       
   558 
       
   559 // ---------------------------------------------------------------------------
       
   560 // CSsmLangSelCmd::UseLocale
       
   561 //
       
   562 // ---------------------------------------------------------------------------
       
   563 //
       
   564 TInt CSsmLangSelCmd::UseLocale( const TInt aLang, const TInt aRegion, const TInt aCollation )
       
   565     {
       
   566     FUNC_LOG;
       
   567     TInt errorCode(KErrNotFound);
       
   568 
       
   569     //Load the given Language, Region and Collation DLLs
       
   570     errorCode = SsmLanguageLoader::LoadLocale( aLang, aRegion, aCollation );
       
   571 
       
   572     if( KErrNone != errorCode )
       
   573         {
       
   574         INFO_1( "Loading failed with error  %d", errorCode);
       
   575         }
       
   576     else if ( iClientStatus )
       
   577         {
       
   578         User::RequestComplete( iClientStatus, errorCode );
       
   579         iClientStatus = NULL;
       
   580         }
       
   581     return errorCode;
    61     }
   582     }
    62 
   583 
    63 // ---------------------------------------------------------------------------
   584 // ---------------------------------------------------------------------------
    64 // CSsmLangSelCmd::RunL
   585 // CSsmLangSelCmd::RunL
    65 //
   586 //
    84             TryNextList();
   605             TryNextList();
    85             }
   606             }
    86         }
   607         }
    87     }
   608     }
    88 
   609 
       
   610 // ---------------------------------------------------------------------------
       
   611 // CSsmLangSelCmd::UseLanguage
       
   612 //
       
   613 // ---------------------------------------------------------------------------
       
   614 //
       
   615 void CSsmLangSelCmd::UseLanguage( const TInt aLang )
       
   616     {
       
   617     FUNC_LOG;
       
   618     TInt errorCode = KErrNotFound;
       
   619     TInt region = KErrNotFound;
       
   620 
       
   621     //Validate and get associated Region
       
   622     if( ValidateAndGetMappedRegion( aLang, region ) )
       
   623         {
       
   624         errorCode = SsmLanguageLoader::LoadLocale( aLang, region, aLang );
       
   625         }
       
   626     else
       
   627         {
       
   628         INFO_1( "Validation failed for the language (auto) %d", aLang );
       
   629         }
       
   630 
       
   631     if( KErrNone != errorCode )
       
   632         {
       
   633         INFO_1( "Loading the auto settings failed with error %d", errorCode );
       
   634         
       
   635         //Use the default setting as the selected one failed
       
   636         errorCode = ValidateAndUseDefaultLanguage();
       
   637         }
       
   638     else if ( iClientStatus )
       
   639         {
       
   640         User::RequestComplete( iClientStatus, errorCode );
       
   641         iClientStatus = NULL;
       
   642         }
       
   643     }
       
   644 
       
   645 // ---------------------------------------------------------------------------
       
   646 // CSsmLangSelCmd::ExecuteCancel
       
   647 // ---------------------------------------------------------------------------
       
   648 //
       
   649 void CSsmLangSelCmd::ExecuteCancel()
       
   650     {
       
   651     FUNC_LOG;
       
   652 
       
   653     Cancel();
       
   654     }
    89 
   655 
    90 // ---------------------------------------------------------------------------
   656 // ---------------------------------------------------------------------------
    91 // CSsmLangSelCmd::DoCancel
   657 // CSsmLangSelCmd::DoCancel
    92 //
   658 //
    93 // ---------------------------------------------------------------------------
   659 // ---------------------------------------------------------------------------
    94 //
   660 //
    95 void CSsmLangSelCmd::DoCancel()
   661 void CSsmLangSelCmd::DoCancel()
    96     {
   662     {
    97     FUNC_LOG;
   663     FUNC_LOG;
    98     }
   664     
    99 
   665     //Cancel the pending request with misc adaptation
   100 
   666     if( EQueryListNone != iState )
   101 // ---------------------------------------------------------------------------
   667         {
   102 // CSsmLangSelCmd::Initialize
   668         iAdaptation.Cancel();
   103 // ---------------------------------------------------------------------------
   669         }
   104 //
   670     
   105 TInt CSsmLangSelCmd::Initialize( CSsmCustomCommandEnv* aCmdEnv )
   671     if ( iClientStatus )
   106     {
   672         {
   107     FUNC_LOG;
   673         User::RequestComplete( iClientStatus, KErrCancel );
   108 
   674         iClientStatus = NULL;
   109     iEnv = aCmdEnv;
   675         }
   110     TRAPD( errorCode, InitializeL() );
   676     }
   111     return errorCode;
       
   112     }
       
   113 
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // CSsmLangSelCmd::Execute
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 void CSsmLangSelCmd::Execute(
       
   120     const TDesC8& /*aParams*/,
       
   121     TRequestStatus& aRequest )
       
   122     {
       
   123     FUNC_LOG;
       
   124     
       
   125     ASSERT_TRACE( !iClientStatus );
       
   126     ASSERT_TRACE( !IsActive() );
       
   127     ASSERT_TRACE( iEnv );
       
   128 
       
   129     aRequest = KRequestPending;
       
   130     iClientStatus = &aRequest;
       
   131     
       
   132 
       
   133     RFs* fsSession = &( const_cast<RFs&>( iEnv->Rfs() ) );
       
   134     TInt errorCode = SysLangUtil::GetDefaultLanguage( iDefaultLanguage,
       
   135                                                       fsSession );
       
   136     ERROR( errorCode, "Failed to get default language" );
       
   137 
       
   138     TInt lastSelectedLang = GetLastSelectedLang();
       
   139     // Revert to automatic selection if fetching last used language
       
   140     // fails, last used language is not valid (and obviously when last
       
   141     // selection is automatic).
       
   142     if ( lastSelectedLang == 0 )
       
   143         {
       
   144         PrepareNextList();
       
   145         }
       
   146     else if ( IsValid( lastSelectedLang ) )
       
   147         {
       
   148         UseLanguage( lastSelectedLang );
       
   149         }
       
   150     else
       
   151         {
       
   152         UseLanguage( iDefaultLanguage );
       
   153         }    
       
   154     }
       
   155 
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // CSsmLangSelCmd::ExecuteCancel
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CSsmLangSelCmd::ExecuteCancel()
       
   162     {
       
   163     FUNC_LOG;
       
   164 
       
   165     Cancel();
       
   166     }
       
   167 
       
   168 
   677 
   169 // ---------------------------------------------------------------------------
   678 // ---------------------------------------------------------------------------
   170 // CSsmLangSelCmd::Close
   679 // CSsmLangSelCmd::Close
   171 // ---------------------------------------------------------------------------
   680 // ---------------------------------------------------------------------------
   172 //
   681 //
   173 void CSsmLangSelCmd::Close()
   682 void CSsmLangSelCmd::Close()
   174     {
   683     {
   175     FUNC_LOG;
   684     FUNC_LOG;
   176     delete iValidLanguages;
   685     delete iValidLanguages;
   177     iValidLanguages = NULL;
   686     iValidLanguages = NULL;
   178     
   687     iRegionsArray.Close();
       
   688     iLangRegionMappingHashSet.Close();
   179     iAdaptation.Close();
   689     iAdaptation.Close();
   180     }
   690     }
   181 
   691 
   182 
   692 
   183 // ---------------------------------------------------------------------------
   693 // ---------------------------------------------------------------------------
   188     {
   698     {
   189     FUNC_LOG;
   699     FUNC_LOG;
   190 
   700 
   191 	delete this;
   701 	delete this;
   192     }
   702     }
   193 // ---------------------------------------------------------------------------
       
   194 // CSsmLangSelCmd::CSsmLangSelCmd
       
   195 // ---------------------------------------------------------------------------
       
   196 //
       
   197 
       
   198 CSsmLangSelCmd::CSsmLangSelCmd()
       
   199   : CActive( EPriorityNormal )
       
   200     {
       
   201     FUNC_LOG;
       
   202 
       
   203     CActiveScheduler::Add( this );
       
   204     }
       
   205 
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // CSsmLangSelCmd::ConstructL
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void CSsmLangSelCmd::ConstructL()
       
   212     {
       
   213     iMapperUtility = CSsmMapperUtility::NewL();
       
   214     }
       
   215 
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // CSsmLangSelCmd::InitializeL
       
   219 // ---------------------------------------------------------------------------
       
   220 //
       
   221 void CSsmLangSelCmd::InitializeL()
       
   222     {
       
   223 
       
   224     FUNC_LOG;
       
   225     ASSERT_TRACE( iEnv );
       
   226     
       
   227     iValidLanguages = new ( ELeave ) CArrayFixFlat<TInt>( 
       
   228                                                     KLanguageListGranularity );
       
   229     RFs* fsSession = &( const_cast<RFs&>( iEnv->Rfs() ) );
       
   230     TInt errorCode = SysLangUtil::GetInstalledLanguages( iValidLanguages,
       
   231                                                          fsSession );
       
   232     
       
   233     ERROR( errorCode, "Failed to get installed languages" );
       
   234     User::LeaveIfError( errorCode );
       
   235     
       
   236     errorCode = iAdaptation.Connect();
       
   237     ERROR( errorCode, "Failed to connect to RSsmMiscAdaptation" );
       
   238     User::LeaveIfError( errorCode );
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // CSsmLangSelCmd::GetLastSelectedLang
       
   243 //
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 TInt CSsmLangSelCmd::GetLastSelectedLang()
       
   247     {
       
   248     FUNC_LOG;
       
   249 
       
   250     TInt value( 0 );
       
   251     TInt errorCode = iMapperUtility->CrValue( KCRUidCommonEngineKeys, 
       
   252                                               KGSDisplayTxtLang,
       
   253                                               value );
       
   254     
       
   255     ERROR( errorCode, "Failed read KGSDisplayTxtLang CenRep key" );
       
   256     INFO_1( "Last selected language = %d", value );
       
   257     return value;
       
   258     }
       
   259 
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // CSsmLangSelCmd::IsValid
       
   263 //
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 TBool CSsmLangSelCmd::IsValid( const TInt aLanguage ) const
       
   267     {
       
   268     FUNC_LOG;
       
   269     ASSERT_TRACE( iValidLanguages );
       
   270 
       
   271     TBool valid( EFalse );
       
   272     TKeyArrayFix keyProp( 0, ECmpTInt );
       
   273     TInt index( KErrNotFound );
       
   274     // Returns zero if element is found.
       
   275     if ( 0 == iValidLanguages->Find( aLanguage, keyProp, index ) )
       
   276         {
       
   277         valid = ETrue;
       
   278         }
       
   279     return valid;
       
   280     }
       
   281 
       
   282 
   703 
   283 // ---------------------------------------------------------------------------
   704 // ---------------------------------------------------------------------------
   284 // CSsmLangSelCmd::PrepareNextList
   705 // CSsmLangSelCmd::PrepareNextList
   285 //
   706 //
   286 // ---------------------------------------------------------------------------
   707 // ---------------------------------------------------------------------------
   326     else
   747     else
   327         {
   748         {
   328         TryNextList();
   749         TryNextList();
   329         }
   750         }
   330     }
   751     }
   331 
       
   332 
   752 
   333 // ---------------------------------------------------------------------------
   753 // ---------------------------------------------------------------------------
   334 // CSsmLangSelCmd::InterpretResult
   754 // CSsmLangSelCmd::InterpretResult
   335 //
   755 //
   336 // ---------------------------------------------------------------------------
   756 // ---------------------------------------------------------------------------
   435             UseLanguage( iDefaultLanguage );
   855             UseLanguage( iDefaultLanguage );
   436             }
   856             }
   437         }
   857         }
   438     }
   858     }
   439 
   859 
   440 
       
   441 // ---------------------------------------------------------------------------
   860 // ---------------------------------------------------------------------------
   442 // CSsmLangSelCmd::StoreLanguageToCentRep
   861 // CSsmLangSelCmd::StoreLanguageToCentRep
   443 //
   862 //
   444 // ---------------------------------------------------------------------------
   863 // ---------------------------------------------------------------------------
   445 //
   864 //
   457         ERROR_1( errorCode, "Failed to store language code to CentRep, %d", 
   876         ERROR_1( errorCode, "Failed to store language code to CentRep, %d", 
   458                                                                   errorCode );
   877                                                                   errorCode );
   459         delete cenrep;
   878         delete cenrep;
   460         }
   879         }
   461     }
   880     }
   462 
       
   463 
       
   464 // ---------------------------------------------------------------------------
       
   465 // CSsmLangSelCmd::GetLanguageFromCentRep
       
   466 //
       
   467 // ---------------------------------------------------------------------------
       
   468 //
       
   469 TInt CSsmLangSelCmd::GetLanguageFromCentRep()
       
   470     {
       
   471     FUNC_LOG;
       
   472 
       
   473     TInt lang = iDefaultLanguage;
       
   474     TInt errorCode = iMapperUtility->CrValue( KCRUidSysLangUtil,
       
   475                                               KSysLangUtilSimLanguage,
       
   476                                               lang );
       
   477     
       
   478     ERROR_1( errorCode, "Failed to read language code from CenRep %d", 
       
   479              errorCode );
       
   480     return lang;
       
   481     }
       
   482 
       
   483 
       
   484 // ---------------------------------------------------------------------------
       
   485 // CSsmLangSelCmd::UseLanguage
       
   486 //
       
   487 // ---------------------------------------------------------------------------
       
   488 //
       
   489 void CSsmLangSelCmd::UseLanguage( const TInt aLang )
       
   490     {
       
   491     FUNC_LOG;
       
   492 
       
   493     TInt errorCode = aLang;
       
   494     if ( aLang > KErrNone )
       
   495         {
       
   496         errorCode = SsmLanguageLoader::LoadLanguage( aLang );
       
   497         ERROR( errorCode, "Failed to load language" );
       
   498         }
       
   499 
       
   500     ERROR( errorCode, "Language selection failed" );
       
   501 
       
   502     if ( iClientStatus )
       
   503         {
       
   504         User::RequestComplete( iClientStatus, errorCode );
       
   505         }
       
   506     }
       
   507