srsf/vcommandhandler/src/tagnameset.cpp
branchRCL_3
changeset 19 e36f3802f733
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:  Array of tags and corresponding spoken texts
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "rubydebug.h"
       
    20 #include "tagnameset.h"
       
    21 #include <nssvasmspeechitem.h>
       
    22 #include <nssvasmtag.h>
       
    23 #include <nssvasmtagmgr.h>
       
    24 
       
    25 _LIT( KTagNameSetPanic, "TNS" );
       
    26 
       
    27 CTagNameSet* CTagNameSet::NewLC()
       
    28     {
       
    29     CTagNameSet* self = new (ELeave) CTagNameSet;
       
    30     CleanupStack::PushL( self );
       
    31     return self;
       
    32     }
       
    33     
       
    34 CTagNameSet::~CTagNameSet()
       
    35     {
       
    36     iTags.ResetAndDestroy();
       
    37     iTexts.ResetAndDestroy();
       
    38     }
       
    39     
       
    40         
       
    41 /**
       
    42 * Adds a tag-text pair to be trained
       
    43 * @param aTag tag to be trained and saved. This objects takes ownership
       
    44 *        on the aTag
       
    45 * @param aTrainingText text to be recognized. This object doesn't
       
    46 *        take ownership on aTrainingText, but makes an own copy
       
    47 */
       
    48 void CTagNameSet::AppendL( const MNssTag* aTag, const TDesC& aTrainingText )
       
    49     {
       
    50     HBufC* text = aTrainingText.AllocL();
       
    51     iTexts.Append( text );
       
    52     iTags.Append( aTag );    
       
    53     }
       
    54 
       
    55 /**
       
    56 * Train and save to VAS all the stored tags. It is not specified what happens
       
    57 * to VAS if this method leaves
       
    58 * @param aParams
       
    59 * @param aTagMgr
       
    60 * @param aIgnoreErrors If ETrue, doesn't fail the whole operation if some
       
    61 *        individual trainings/savings failed. Trains and saves as much as
       
    62 *        possible
       
    63 */
       
    64 void CTagNameSet::TrainAndSaveL( const CNssTrainingParameters& aParams, 
       
    65                                  MNssTagMgr& aTagMgr, TBool aIgnoreErrors )
       
    66     {
       
    67     RUBY_DEBUG_BLOCK( "CTrainingPack::TrainAndSaveL" );
       
    68     iIgnoreErrors = aIgnoreErrors;
       
    69     RUBY_DEBUG1( "CTrainingPack::TrainAndSaveL Processing [%d] pairs", iTags.Count() );
       
    70     
       
    71     TrainTagsL( aParams );
       
    72     SaveTagsL( aTagMgr );
       
    73     }
       
    74     
       
    75 /**
       
    76 * Remove all the stored tags from VAS. It is not specified what happens
       
    77 * to VAS if this method leaves
       
    78 * @param aTagMgr
       
    79 * @param aIgnoreErrors If ETrue, doesn't fail the whole operation if some
       
    80 *        individual removals failed. Removes as many tags as
       
    81 *        possible
       
    82 */                            
       
    83 void CTagNameSet::UntrainL( MNssTagMgr& aTagMgr, TBool aIgnoreErrors )
       
    84     {
       
    85     RUBY_DEBUG_BLOCK("");
       
    86     iIgnoreErrors = aIgnoreErrors;
       
    87     RUBY_DEBUG1( "Removing [%d] tags from VAS", iTags.Count() );
       
    88     SetCallbacksExpected( iTags.Count() );
       
    89     
       
    90     if( iCallbacksExpected > 0 )
       
    91         {
       
    92        	for( TInt i = 0; i < iTags.Count(); i++ )
       
    93         	{
       
    94             DeleteTagL( aTagMgr, *iTags[i] );
       
    95     	    }  // for
       
    96         WaitUntilOperationsAreCompletedL();
       
    97         }
       
    98     }
       
    99 
       
   100 /**
       
   101 * @return Number of tag-name pairs stored
       
   102 */
       
   103 TInt CTagNameSet::Count() const
       
   104     {
       
   105     return iTags.Count();
       
   106     }
       
   107 
       
   108 /**
       
   109 * @param aIndex must be >= 0 and < Count()
       
   110 * @return Voice tag at the given position
       
   111 * @panic KErrArgument if aIndex is out of bounds
       
   112 */    
       
   113 const MNssTag& CTagNameSet::TagAt( TInt aIndex ) const
       
   114     {
       
   115     __ASSERT_ALWAYS( aIndex >= 0 && aIndex < iTags.Count(), 
       
   116                     User::Panic( KTagNameSetPanic, KErrArgument ));
       
   117     return *iTags[aIndex];
       
   118     }
       
   119 
       
   120 /**
       
   121 * @param aIndex must be >= 0 and < Count()
       
   122 * @return Recognition text at the given position
       
   123 * @panic KErrArgument if aIndex is out of bounds
       
   124 */        
       
   125 const TDesC& CTagNameSet::TextAt( TInt aIndex ) const
       
   126     {
       
   127     __ASSERT_ALWAYS( aIndex >= 0 && aIndex < iTexts.Count(), 
       
   128                     User::Panic( KTagNameSetPanic, KErrArgument ));
       
   129     return *iTexts[aIndex];
       
   130     }
       
   131 
       
   132 /**
       
   133 * Train all the stored tag-name pairs. Synchronous
       
   134 */
       
   135 void CTagNameSet::TrainTagsL( const CNssTrainingParameters& aParams )
       
   136     {
       
   137     __ASSERT_ALWAYS( iCallbacksExpected == 0, User::Leave( KErrInUse) );
       
   138     SetCallbacksExpected( Count() );
       
   139     if( iCallbacksExpected > 0 )
       
   140         {
       
   141         for( TInt i = 0; i < Count(); i++ )
       
   142             {
       
   143             TrainTagL( TagAt( i ), aParams, TextAt( i ) );
       
   144             }  // for
       
   145         WaitUntilOperationsAreCompletedL();
       
   146         }  // if
       
   147     }
       
   148 
       
   149 
       
   150 /**
       
   151 * Trains the voice tag. Asynchronous
       
   152 * @leave VAS error code
       
   153 * @todo Is it ok to mix system-wide codes with the TNssSpeechItemResult codes
       
   154 */
       
   155 void CTagNameSet::TrainTagL( const MNssTag& aTag, 
       
   156 		const CNssTrainingParameters& aParams, const TDesC& aSpokenText )
       
   157 	{
       
   158 	RUBY_DEBUG_BLOCKL( "CTagNameSet::TrainTagL" );
       
   159     RUBY_DEBUG1( "Training for spoken text [%S]", &aSpokenText )
       
   160 
       
   161     MNssSpeechItem* speechItem = const_cast<MNssTag&>(aTag).SpeechItem();
       
   162     speechItem->SetTextL( aSpokenText );
       
   163     MNssSpeechItem::TNssSpeechItemResult nssErr = 
       
   164 		speechItem->TrainTextL( this, const_cast<CNssTrainingParameters*>( &aParams ) );
       
   165     
       
   166     if ( nssErr != MNssSpeechItem::EVasErrorNone )
       
   167         {
       
   168         RUBY_ERROR1( "CTagNameSet::TrainTagL training start failed with the nss code [%d]", 
       
   169                     nssErr );
       
   170         if( iIgnoreErrors )
       
   171             {
       
   172             RUBY_DEBUG0( "CTagNameSet::TrainTagL ignoring error" );
       
   173             DecreaseCallbacksExpected();
       
   174             }
       
   175         else 
       
   176             {
       
   177             // Wait for started operations before leaving
       
   178             DecreaseCallbacksExpected();
       
   179             WaitUntilOperationsAreCompletedL();
       
   180             
       
   181             switch( nssErr )
       
   182                 {
       
   183                 case MNssSpeechItem::EVasInvalidParameter:
       
   184                     User::Leave( KErrArgument );
       
   185                 case MNssSpeechItem::EVasTrainFailed:
       
   186                     User::Leave( KErrGeneral );
       
   187                 default:
       
   188                     User::Leave( nssErr );
       
   189                 } 
       
   190             }        
       
   191         }
       
   192     RUBY_DEBUG0( "CTagNameSet::TrainTagL Tag training has been initiated" );
       
   193 	}			
       
   194 			
       
   195 
       
   196 /**
       
   197 * Train Complete Event - Voice tag training completed
       
   198 * @param aErrorCode KErrNone if training was successfull
       
   199 */       
       
   200 void CTagNameSet::HandleTrainComplete( TInt aErrorCode )
       
   201 	{
       
   202 	//RUBY_DEBUG0( "Start" );
       
   203 	ProcessCallback( aErrorCode );
       
   204 	//RUBY_DEBUG0( "End" );
       
   205 	}
       
   206 
       
   207 /**
       
   208 * Saves the trained voice tag to VAS. Synchronous
       
   209 */
       
   210 void CTagNameSet::SaveTagsL( MNssTagMgr& aTagManager )
       
   211 	{
       
   212 	RUBY_DEBUG_BLOCK( "CTagSaver::SaveTagsL" );
       
   213 	__ASSERT_ALWAYS( iCallbacksExpected == 0, User::Leave( KErrInUse) );
       
   214     SetCallbacksExpected( Count() );
       
   215     if( iCallbacksExpected > 0 )
       
   216         {
       
   217         for( TInt i = 0; i < Count(); i++ )
       
   218             {
       
   219             SaveTagL( aTagManager, TagAt( i ) );
       
   220             }  // for
       
   221             
       
   222         WaitUntilOperationsAreCompletedL();
       
   223         }  // if
       
   224 	}
       
   225 	
       
   226 
       
   227 /**
       
   228 * Saves the trained voice tag to VAS. 
       
   229 */
       
   230 void CTagNameSet::SaveTagL( MNssTagMgr& aTagManager, const MNssTag& aTag )
       
   231 	{
       
   232 	RUBY_DEBUG_BLOCKL( "CTagNameSet::SaveTagL" );
       
   233     TInt nssErr = 
       
   234             aTagManager.SaveTag( this, const_cast<MNssTag*>( &aTag ) );
       
   235     if( nssErr != KErrNone ) 
       
   236         {
       
   237         RUBY_ERROR1( "CTagNameSet::SaveTagL Savetag start failed with the nss code [%d]", nssErr );
       
   238         if( iIgnoreErrors )
       
   239             {
       
   240             RUBY_DEBUG0( "CTagNameSet::SaveTagL ignoring error" );
       
   241             DecreaseCallbacksExpected();
       
   242             }
       
   243         else 
       
   244             {
       
   245             // Wait for started operations before leaving
       
   246             DecreaseCallbacksExpected();
       
   247             WaitUntilOperationsAreCompletedL();
       
   248             
       
   249             User::Leave( KErrGeneral );
       
   250             }        
       
   251         }
       
   252     RUBY_DEBUG0( "CTagNameSet::SaveTagL Savetag sequence started" );
       
   253 
       
   254 	}
       
   255 
       
   256 /**
       
   257 * Callback to indicate that SaveTag sequence has been completed
       
   258 * @param aErrorCode KErrNone if saving of tag was successfull
       
   259 */
       
   260 void CTagNameSet::SaveTagCompleted( TInt aErrorCode )
       
   261 	{
       
   262 	RUBY_DEBUG0( "Start" );
       
   263     ProcessCallback( aErrorCode );
       
   264     RUBY_DEBUG0( "End" );
       
   265     }
       
   266 
       
   267 /**
       
   268 * Deletes the voice tag from VAS. Asynchronous
       
   269 * @leave VAS error codes
       
   270 */
       
   271 void CTagNameSet::DeleteTagL( MNssTagMgr& aTagManager, MNssTag& aTag )
       
   272     {
       
   273     User::LeaveIfError( aTagManager.DeleteTag( this, &aTag ) );
       
   274     }
       
   275 
       
   276 /**
       
   277 * Callback to indicate a tag was deleted successfully.
       
   278 * @param aErrorCode KErrNone if deletion of tag was successfull
       
   279 */
       
   280 void CTagNameSet::DeleteTagCompleted( TInt aErrorCode )
       
   281     {
       
   282     RUBY_DEBUG0( "Start" );
       
   283     ProcessCallback( aErrorCode );
       
   284     RUBY_DEBUG0( "End" );
       
   285     }
       
   286 
       
   287 /**
       
   288 * Handles the common part of the callback processing
       
   289 * Decrements the number of still expected callbacks and
       
   290 * handles the errors according to aIgnoreErrors from the 
       
   291 * public methods
       
   292 */
       
   293 void CTagNameSet::ProcessCallback( TInt aErrorCode )
       
   294     {
       
   295     //RUBY_DEBUG0( "Start" );
       
   296     if( iIgnoreErrors || aErrorCode == KErrNone )
       
   297         {
       
   298         if( DecreaseCallbacksExpected() == 0 )
       
   299             {
       
   300             StopAsyncWaitingOrPanic( KErrNone );
       
   301             }  
       
   302         }
       
   303     else
       
   304         {
       
   305         RUBY_ERROR1( "Callback reported error [%d] ", aErrorCode );
       
   306         if( DecreaseCallbacksExpected() == 0 )
       
   307             {
       
   308             StopAsyncWaitingOrPanic( aErrorCode );
       
   309             }
       
   310         }
       
   311     //RUBY_DEBUG0( "End" );    
       
   312     }
       
   313 
       
   314 /**
       
   315 * Sets the number of callbacks to expect
       
   316 */
       
   317 void CTagNameSet::SetCallbacksExpected (TUint aCallbacksExpected )
       
   318     {
       
   319     RUBY_DEBUG2( "Changing from [%d] to [%d]", iCallbacksExpected, aCallbacksExpected );
       
   320     iCallbacksExpected = aCallbacksExpected;
       
   321     }
       
   322 
       
   323 /**
       
   324 * Decreases the number of callbacks to expect by one
       
   325 * @return Number of callbacks expected after decreasing        
       
   326 */
       
   327 TUint CTagNameSet::DecreaseCallbacksExpected()
       
   328     {
       
   329     __ASSERT_ALWAYS( iCallbacksExpected > 0, User::Panic( KTagNameSetPanic, KErrNotReady ) );
       
   330     RUBY_DEBUG2( "Changing from [%d] to [%d]", iCallbacksExpected, iCallbacksExpected - 1 );
       
   331     return --iCallbacksExpected;
       
   332     }
       
   333 
       
   334 /**
       
   335 * Waits until all the expected callbacks are received
       
   336 * If no callbacks are expected returns immediately
       
   337 * @leave @see CAsyncWorker::WaitForAsyncCallbackL
       
   338 */
       
   339 void CTagNameSet::WaitUntilOperationsAreCompletedL()
       
   340     {
       
   341     RUBY_DEBUG_BLOCK( "" );
       
   342     if( iCallbacksExpected > 0 )
       
   343         {
       
   344         RUBY_DEBUG1( "[%d] callbacks expected. Waiting for them", iCallbacksExpected );
       
   345         WaitForAsyncCallbackL();
       
   346         }
       
   347     else 
       
   348         {
       
   349         RUBY_DEBUG0( "no callbacks expected. Returning immediately" );
       
   350         }
       
   351     }
       
   352 
       
   353 //End of file