voiceui/voiceuivoicerecognition/src/vuicverificationtrainingstate.cpp
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2006 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 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <StringLoader.h>
       
    21 #include <centralrepository.h>
       
    22 
       
    23 #include <nssvasctrainingparameters.h>
       
    24 
       
    25 #include <srsfdomaincrkeys.h>
       
    26 
       
    27 #include <vuivoicerecognition.rsg>
       
    28 
       
    29 #include "vuicstate.h"
       
    30 #include "vuicverificationtrainingstate.h"
       
    31 #include "vuictutorialstate.h"
       
    32 #include "vuicerrorstate.h"
       
    33 
       
    34 #include "vuicdatastorage.h"
       
    35 
       
    36 #include "vuicvoicerecogdialogimpl.h"
       
    37 
       
    38 #include "rubydebug.h"
       
    39 
       
    40 // Constants
       
    41 
       
    42 static const TInt KCommandIds[] = 
       
    43     {
       
    44     R_QAN_VC_VERIFICATION_SELECT,
       
    45     R_QAN_VC_VERIFICATION_OTHER,
       
    46     R_QAN_VC_VERIFICATION_CANCEL
       
    47     };
       
    48     
       
    49 // -----------------------------------------------------------------------------
       
    50 // CVerificationTrainingState::NewL
       
    51 // Two-phased constructor.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CVerificationTrainingState* CVerificationTrainingState::NewL( CDataStorage& aDataStorage, CUiModel& aUiModel )
       
    55     {
       
    56     CVerificationTrainingState* self = new (ELeave) CVerificationTrainingState( aDataStorage, aUiModel );
       
    57     CleanupStack::PushL( self );
       
    58     self->ConstructL();
       
    59     CleanupStack::Pop( self );
       
    60     return self;
       
    61     }       
       
    62     
       
    63 // Destructor       
       
    64 CVerificationTrainingState::~CVerificationTrainingState()
       
    65     {
       
    66     RUBY_DEBUG0( "CVerificationTrainingState::~CVerificationTrainingState START" );
       
    67     
       
    68     delete iVasDbManager;
       
    69     iContextManager = NULL;
       
    70     iTagManager = NULL;
       
    71     
       
    72     if ( iTags )
       
    73         {
       
    74         iTags->ResetAndDestroy();
       
    75         }
       
    76     delete iTags;
       
    77     
       
    78     delete iTrainingParameters;
       
    79     
       
    80     SaveLanguage();
       
    81     
       
    82     RUBY_DEBUG0( "CVerificationTrainingState::~CVerificationTrainingState EXIT" );
       
    83     }
       
    84     
       
    85 // ---------------------------------------------------------
       
    86 // CVerificationTrainingState::HandleEventL
       
    87 // ---------------------------------------------------------
       
    88 //       
       
    89 void CVerificationTrainingState::HandleEventL( TInt aEvent )
       
    90     {
       
    91     RUBY_DEBUG_BLOCK( "CVerificationTrainingState::HandleEventL" );
       
    92 
       
    93     CState* nextState = NULL;
       
    94     
       
    95     switch( aEvent )
       
    96         {
       
    97         case KErrNone:
       
    98         
       
    99             if ( iInternalState == EStarted )
       
   100                 {
       
   101                 nextState = this;
       
   102                 iInternalState = EContextDeleted;
       
   103                 }
       
   104             else if ( iInternalState == EContextDeleted )
       
   105                 {
       
   106                 nextState = this;
       
   107                 iInternalState = EContextReady;
       
   108                 }
       
   109             else if ( iInternalState == EContextReady )
       
   110                 {
       
   111                 --iExpectedCallbacks;
       
   112                 
       
   113                 if ( iExpectedCallbacks == KErrNone )
       
   114                     {
       
   115                     nextState = this;
       
   116                     iInternalState = ECommandsTrained;
       
   117                     }
       
   118                 }
       
   119             else
       
   120                 {
       
   121                 --iExpectedCallbacks;
       
   122                 
       
   123                 if ( iExpectedCallbacks == KErrNone )
       
   124                     {
       
   125                     nextState = CTutorialState::NewL( DataStorage(), UiModel() );
       
   126                     iInternalState = ECommandsSaved;
       
   127                     }
       
   128                 }
       
   129             break;
       
   130             
       
   131         default:
       
   132             
       
   133             nextState = CErrorState::NewL( DataStorage(), UiModel(), aEvent );
       
   134             break;
       
   135         }
       
   136 
       
   137     DataStorage().VoiceRecognitionImpl()->ChangeState( nextState );
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------
       
   141 // CVerificationTrainingState::ExecuteL
       
   142 // ---------------------------------------------------------
       
   143 //       
       
   144 void CVerificationTrainingState::ExecuteL()
       
   145     {
       
   146     RUBY_DEBUG_BLOCK( "CVerificationTrainingState::ExecuteL" );
       
   147     
       
   148     if ( iInternalState == ENotStarted )
       
   149         {
       
   150         iInternalState = EStarted;
       
   151         
       
   152         // Delete context if already exists
       
   153         if ( DataStorage().Context()->ContextName() == KVoiceVerificationContext )
       
   154             {
       
   155             if ( iContextManager->DeleteContext( DataStorage().VoiceRecognitionImpl(),
       
   156                                                  DataStorage().Context() ) != KErrNone )
       
   157                 {
       
   158                 RUBY_ERROR0( "CVerificationTrainingState::ExecuteL - DeleteContext call failed" );
       
   159                 HandleEventL( KErrGeneral );
       
   160                 }
       
   161             }
       
   162         else 
       
   163             {
       
   164             HandleEventL( KErrNone );
       
   165             }
       
   166         }
       
   167     else if ( iInternalState == EContextDeleted )
       
   168         {
       
   169         // Create new context
       
   170         MNssContext* vasContext = iContextManager->CreateContextL();
       
   171         DataStorage().SetContext( vasContext );
       
   172             
       
   173         vasContext->SetNameL( KVoiceVerificationContext );
       
   174         vasContext->SetGlobal( EFalse );
       
   175     
       
   176         if ( iContextManager->SaveContext( DataStorage().VoiceRecognitionImpl(),
       
   177                                            vasContext ) != KErrNone )
       
   178             {
       
   179             RUBY_ERROR0( "CVerificationTrainingState::ExecuteL - SaveContext call failed" );
       
   180             HandleEventL( KErrGeneral );
       
   181             }
       
   182         }
       
   183     else if ( iInternalState == EContextReady )
       
   184         { 
       
   185         TrainTagsL();        
       
   186         }
       
   187      else if ( iInternalState == ECommandsTrained )
       
   188         {
       
   189         SaveTagsL();
       
   190         }
       
   191     }
       
   192 
       
   193 // ---------------------------------------------------------
       
   194 // CVerificationTrainingState::CVerificationTrainingState
       
   195 // ---------------------------------------------------------
       
   196 //              
       
   197 CVerificationTrainingState::CVerificationTrainingState( CDataStorage& aDataStorage, CUiModel& aUiModel )
       
   198  : CState( aDataStorage, aUiModel ), iInternalState( ENotStarted ), iTags(),
       
   199    iExpectedCallbacks( 0 )
       
   200     {
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------
       
   204 // CVerificationTrainingState::ConstructL
       
   205 // ---------------------------------------------------------
       
   206 //           
       
   207 void CVerificationTrainingState::ConstructL()
       
   208     {
       
   209     RUBY_DEBUG_BLOCK( "CVerificationTrainingState::ConstructL" );
       
   210     
       
   211     CState::ConstructL();
       
   212     
       
   213     iVasDbManager = CNssVASDBMgr::NewL();
       
   214     iVasDbManager->InitializeL();
       
   215     iTagManager = iVasDbManager->GetTagMgr();
       
   216     iContextManager = iVasDbManager->GetContextMgr();   
       
   217     }
       
   218     
       
   219 // ---------------------------------------------------------
       
   220 // CVerificationTrainingState::CreateTagsL
       
   221 // ---------------------------------------------------------
       
   222 //  
       
   223 void CVerificationTrainingState::CreateTagsL()
       
   224     {
       
   225     RUBY_DEBUG_BLOCK( "CVerificationTrainingState::CreateTagsL" );
       
   226     
       
   227     iTags = new ( ELeave ) RPointerArray<MNssTag>;
       
   228  
       
   229     for ( int i = 0; i < KVerificationCommands; ++i )
       
   230         {
       
   231         iTags->Append( iTagManager->CreateTagL( DataStorage().Context() ) );
       
   232         }
       
   233     }
       
   234     
       
   235 // ---------------------------------------------------------
       
   236 // CVerificationTrainingState::ConstructTrainingParametersL
       
   237 // ---------------------------------------------------------
       
   238 //  
       
   239 void CVerificationTrainingState::ConstructTrainingParametersL()
       
   240     {
       
   241     RUBY_DEBUG_BLOCK( "CVerificationTrainingState::ConstructTrainingParametersL" );
       
   242     
       
   243     RArray<TLanguage>* languageArray = new ( ELeave ) RArray<TLanguage>;
       
   244     CleanupDeletePushL( languageArray ); // Protect [allocated on the heap] array itself
       
   245     CleanupClosePushL( *languageArray );  // Protect the array elements
       
   246     
       
   247     // Always generate a pronunciation in UI language
       
   248     User::LeaveIfError( languageArray->Append( User::Language() ) );
       
   249     
       
   250     iTrainingParameters = CNssTrainingParameters::NewL();
       
   251     
       
   252     iTrainingParameters->SetLanguages( languageArray );
       
   253     
       
   254     // For array elements pushed via CleanupClosePushL
       
   255     CleanupStack::Pop( languageArray );
       
   256     CleanupStack::Pop( languageArray ); 
       
   257     }
       
   258     
       
   259 // ---------------------------------------------------------
       
   260 // CVerificationTrainingState::TrainTagsL
       
   261 // ---------------------------------------------------------
       
   262 //  
       
   263 void CVerificationTrainingState::TrainTagsL()
       
   264     {
       
   265     RUBY_DEBUG_BLOCK( "CVerificationTrainingState::TrainTagsL" );
       
   266     
       
   267     CreateTagsL();
       
   268     
       
   269     ConstructTrainingParametersL();
       
   270     
       
   271     iExpectedCallbacks = KVerificationCommands;
       
   272     
       
   273     for ( int i = 0; i < KVerificationCommands; ++i )
       
   274         {
       
   275         HBufC* text = StringLoader::LoadLC( KCommandIds[ i ] );    
       
   276         
       
   277         MNssSpeechItem* speechItem = static_cast<MNssTag*>( (*iTags)[ i ] )->SpeechItem();
       
   278         speechItem->SetTextL( *text );
       
   279         
       
   280         CleanupStack::PopAndDestroy( text );
       
   281         
       
   282         MNssRRD* rrd = static_cast<MNssTag*>( (*iTags)[ i ] )->RRD();
       
   283         
       
   284         rrd->IntArray()->AppendL( i );
       
   285             
       
   286         MNssSpeechItem::TNssSpeechItemResult result = 
       
   287 		    speechItem->TrainTextL( DataStorage().VoiceRecognitionImpl(),
       
   288 		                            iTrainingParameters );
       
   289     
       
   290         if ( result != MNssSpeechItem::EVasErrorNone )
       
   291             {
       
   292             RUBY_ERROR1( "CVerificationTrainingState::TrainTagsL - Training start failed with the nss code [%d]", result );
       
   293 
       
   294             HandleEventL( KErrGeneral );
       
   295             }
       
   296         }
       
   297     }
       
   298     
       
   299 // ---------------------------------------------------------
       
   300 // CVerificationTrainingState::SaveTagsL
       
   301 // ---------------------------------------------------------
       
   302 //  
       
   303 void CVerificationTrainingState::SaveTagsL()
       
   304     {
       
   305     RUBY_DEBUG_BLOCK( "CVerificationTrainingState::SaveTagsL" );
       
   306     
       
   307     iExpectedCallbacks = KVerificationCommands;
       
   308     
       
   309     for ( int i = 0; i < KVerificationCommands; ++i )
       
   310         {
       
   311         TInt result = 
       
   312             iTagManager->SaveTag( DataStorage().VoiceRecognitionImpl(),
       
   313                                   static_cast<MNssTag*>( (*iTags)[ i ] ) );
       
   314         if ( result != KErrNone ) 
       
   315             {
       
   316             RUBY_ERROR1( "CVerificationTrainingState::SaveTagsL - Savetag start failed with the nss code [%d]", result );
       
   317 
       
   318             HandleEventL( KErrGeneral );
       
   319             }
       
   320         }
       
   321     }
       
   322         
       
   323 // ---------------------------------------------------------
       
   324 // CVerificationTrainingState::SaveLanguage
       
   325 // ---------------------------------------------------------
       
   326 //           
       
   327 void CVerificationTrainingState::SaveLanguage()
       
   328     {
       
   329     RUBY_DEBUG0( "CVerificationTrainingState::SaveLanguage START" );
       
   330     
       
   331     TRAP_IGNORE(
       
   332         CRepository* client = CRepository::NewLC( KCRUidSRSFSettings );    
       
   333         client->Set( KSRSFVoiceUiLanguage, User::Language() );        
       
   334         CleanupStack::PopAndDestroy( client ) );
       
   335     
       
   336     RUBY_DEBUG0( "CVerificationTrainingState::SaveLanguage EXIT" );
       
   337     }
       
   338     
       
   339 // End of File
       
   340