sysstatemgmt/syslangutil/ssmlangselcmd/src/ssmlangselcmd.cpp
changeset 0 4e1aa6a622a0
child 21 ccb4f6b3db21
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 * Implementation of CSsmLangSelCmd class.
       
    16 *
       
    17 */
       
    18 
       
    19 #include "ssmlangselcmd.h"
       
    20 #include "ssmmapperutility.h"
       
    21 #include "ssmalternativelanguages.h"
       
    22 #include "ssmlanguageloader.h"
       
    23 #include "syslangutilprivatecrkeys.h"
       
    24 #include "trace.h"
       
    25 
       
    26 #include <syslangutil.h>
       
    27 #include <CommonEngineDomainCRKeys.h>
       
    28 #include <centralrepository.h>
       
    29 
       
    30 
       
    31 /** Default granularity for list of installed languages. */
       
    32 const TInt KLanguageListGranularity( 4 );
       
    33 
       
    34 
       
    35 // ======== MEMBER FUNCTIONS ========
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // CSsmLangSelCmd::NewL
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CSsmLangSelCmd* CSsmLangSelCmd::NewL()
       
    42 	{
       
    43     FUNC_LOG;
       
    44     CSsmLangSelCmd* self = new ( ELeave ) CSsmLangSelCmd();
       
    45     CleanupStack::PushL( self );
       
    46     self->ConstructL();
       
    47     CleanupStack::Pop( self );
       
    48 	return self;	
       
    49 	}
       
    50 
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // CSsmLangSelCmd::~CSsmLangSelCmd
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CSsmLangSelCmd::~CSsmLangSelCmd()
       
    57     {
       
    58     FUNC_LOG;
       
    59     delete iLangList;
       
    60     delete iMapperUtility;
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // CSsmLangSelCmd::RunL
       
    65 //
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 void CSsmLangSelCmd::RunL()
       
    69     {
       
    70     FUNC_LOG;
       
    71     TInt errorCode = iStatus.Int();
       
    72     
       
    73     ERROR( errorCode, "CSsmLangSelCmd::RunL with error" );
       
    74 
       
    75     if ( iState == EQueryListSize )
       
    76         {
       
    77         if ( errorCode == KErrNone )
       
    78             {
       
    79             TRAP( errorCode, FetchLanguageListL() );
       
    80             ERROR( errorCode, "Failed to fetch language list" );
       
    81             }
       
    82         else
       
    83             {
       
    84             TryNextList();
       
    85             }
       
    86         }
       
    87     }
       
    88 
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // CSsmLangSelCmd::DoCancel
       
    92 //
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 void CSsmLangSelCmd::DoCancel()
       
    96     {
       
    97     FUNC_LOG;
       
    98     }
       
    99 
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CSsmLangSelCmd::Initialize
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 TInt CSsmLangSelCmd::Initialize( CSsmCustomCommandEnv* aCmdEnv )
       
   106     {
       
   107     FUNC_LOG;
       
   108 
       
   109     iEnv = aCmdEnv;
       
   110     TRAPD( errorCode, InitializeL() );
       
   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 
       
   169 // ---------------------------------------------------------------------------
       
   170 // CSsmLangSelCmd::Close
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 void CSsmLangSelCmd::Close()
       
   174     {
       
   175     FUNC_LOG;
       
   176     delete iValidLanguages;
       
   177     iValidLanguages = NULL;
       
   178     
       
   179     iAdaptation.Close();
       
   180     }
       
   181 
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // CSsmLangSelCmd::Release
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 void CSsmLangSelCmd::Release()
       
   188     {
       
   189     FUNC_LOG;
       
   190 
       
   191 	delete this;
       
   192     }
       
   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 
       
   283 // ---------------------------------------------------------------------------
       
   284 // CSsmLangSelCmd::PrepareNextList
       
   285 //
       
   286 // ---------------------------------------------------------------------------
       
   287 //
       
   288 void CSsmLangSelCmd::PrepareNextList()
       
   289     {
       
   290     FUNC_LOG;
       
   291     ASSERT_TRACE( iLangListPref >= EPrimaryLanguages &&
       
   292                   iLangListPref <= ETertiaryLanguages );
       
   293     INFO_1( "Trying preferred language list %d", iLangListPref );
       
   294 
       
   295     iState = EQueryListSize;
       
   296     iAdaptation.PrepareSimLanguages(
       
   297         static_cast<TSsmLanguageListPriority>( iLangListPref++ ),
       
   298         iLangListSize, iStatus );
       
   299     SetActive();
       
   300     }
       
   301 
       
   302 
       
   303 // ---------------------------------------------------------------------------
       
   304 // CSsmLangSelCmd::FetchLanguageListL
       
   305 //
       
   306 // ---------------------------------------------------------------------------
       
   307 //
       
   308 void CSsmLangSelCmd::FetchLanguageListL()
       
   309     {
       
   310     FUNC_LOG;
       
   311     INFO_1( "Required language list size = %d", iLangListSize() );
       
   312 
       
   313     iState = EQueryListContent;
       
   314     delete iLangList;
       
   315     iLangList = NULL;
       
   316     
       
   317     iLangList = iAdaptation.SimLanguagesArrayL( iLangListSize() );    
       
   318     
       
   319     TInt selectionResult = InterpretResult();
       
   320 
       
   321     if ( selectionResult > KErrNone )
       
   322         {
       
   323         StoreLanguageToCentRep( selectionResult );
       
   324         UseLanguage( selectionResult );
       
   325         }
       
   326     else
       
   327         {
       
   328         TryNextList();
       
   329         }
       
   330     }
       
   331 
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // CSsmLangSelCmd::InterpretResult
       
   335 //
       
   336 // ---------------------------------------------------------------------------
       
   337 //
       
   338 TInt CSsmLangSelCmd::InterpretResult()
       
   339     {
       
   340     FUNC_LOG;
       
   341 
       
   342     TInt selectedLanguage = KErrNotFound;
       
   343     TInt langCount( iLangListSize() );
       
   344     INFO_1( "Number of languages: %d", langCount );
       
   345 
       
   346     for ( TInt i = 0; i < langCount; i++ )
       
   347         {
       
   348         TInt lang = ( *iLangList )[i];
       
   349         INFO_1( "Language: %d", lang );
       
   350 
       
   351         TInt nearestMatch = ValidAlternative( lang );
       
   352         if ( nearestMatch > 0 ) // Not an error code
       
   353             {
       
   354             selectedLanguage = nearestMatch;
       
   355             break;
       
   356             }
       
   357         }
       
   358 
       
   359     INFO_1( "Selected language %d from list", selectedLanguage );
       
   360     return selectedLanguage;
       
   361     }
       
   362 
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // CSsmLangSelCmd::ValidAlternative
       
   366 //
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 TInt CSsmLangSelCmd::ValidAlternative( const TInt aLanguage ) const
       
   370     {
       
   371     FUNC_LOG;
       
   372 
       
   373     TInt retVal = KErrNotFound;
       
   374 
       
   375     if ( IsValid( aLanguage ) )
       
   376         {
       
   377         retVal = aLanguage;
       
   378         }
       
   379     else
       
   380         {
       
   381         for ( TInt i = 0; i < KSsmAlternativeLanguageTableSize; i++ )
       
   382             {
       
   383             if ( KAlternativeLanguageTable[i].iLang1 == aLanguage )
       
   384                 {
       
   385                 INFO_2( "Alternative %d found for language %d",
       
   386                     KAlternativeLanguageTable[i].iLang2, aLanguage );
       
   387 
       
   388                 if ( IsValid( KAlternativeLanguageTable[i].iLang2 ) )
       
   389                     {
       
   390                     retVal = KAlternativeLanguageTable[i].iLang2;
       
   391                     break;
       
   392                     }
       
   393                 }
       
   394             else if ( KAlternativeLanguageTable[i].iLang2 == aLanguage )
       
   395                 {
       
   396                 INFO_2( "Alternative %d found for language %d",
       
   397                     KAlternativeLanguageTable[i].iLang1, aLanguage );
       
   398 
       
   399                 if ( IsValid( KAlternativeLanguageTable[i].iLang1 ) )
       
   400                     {
       
   401                     retVal = KAlternativeLanguageTable[i].iLang1;
       
   402                     break;
       
   403                     }
       
   404                 }
       
   405             }
       
   406         }
       
   407 
       
   408     return retVal;
       
   409     }
       
   410 
       
   411 
       
   412 // ---------------------------------------------------------------------------
       
   413 // CSsmLangSelCmd::TryNextList
       
   414 //
       
   415 // ---------------------------------------------------------------------------
       
   416 //
       
   417 void CSsmLangSelCmd::TryNextList()
       
   418     {
       
   419     FUNC_LOG;
       
   420 
       
   421     if ( iLangListPref <= ETertiaryLanguages )
       
   422         {
       
   423         PrepareNextList();
       
   424         }
       
   425     else // All lists exhausted
       
   426         {
       
   427         TInt prevLang = GetLanguageFromCentRep();
       
   428         if ( IsValid( prevLang ) )
       
   429             {
       
   430             UseLanguage( prevLang );
       
   431             }
       
   432         else
       
   433             {
       
   434             INFO( "Previous language not valid, reverting to default" );
       
   435             UseLanguage( iDefaultLanguage );
       
   436             }
       
   437         }
       
   438     }
       
   439 
       
   440 
       
   441 // ---------------------------------------------------------------------------
       
   442 // CSsmLangSelCmd::StoreLanguageToCentRep
       
   443 //
       
   444 // ---------------------------------------------------------------------------
       
   445 //
       
   446 void CSsmLangSelCmd::StoreLanguageToCentRep( const TInt aLanguage )
       
   447     {
       
   448     FUNC_LOG;
       
   449     TUid mappedUid = iMapperUtility->CrUid( KCRUidSysLangUtil );
       
   450     CRepository* cenrep = NULL;
       
   451     TRAPD( errorCode, cenrep = CRepository::NewL( mappedUid ) );
       
   452     ERROR_1( errorCode, "Failed to initialize cen rep %d", errorCode );
       
   453     
       
   454     if ( KErrNone == errorCode )
       
   455         {
       
   456         errorCode = cenrep->Set( KSysLangUtilSimLanguage, aLanguage );
       
   457         ERROR_1( errorCode, "Failed to store language code to CentRep, %d", 
       
   458                                                                   errorCode );
       
   459         delete cenrep;
       
   460         }
       
   461     }
       
   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