srsf/nssvasapi/nssvascore/src/nssvasccontextsrsportal.cpp
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2003-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:  CNssContextSrsPortal provides SRS related functionalities for 
       
    15 *               context processing. A context is saved in the SRS DB by creating 
       
    16 *               model bank, lexicon, and grammar into the SRS.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "nssvasccontextsrsportal.h"
       
    23 #include "rubydebug.h"
       
    24 
       
    25 
       
    26 // ================= MEMBER FUNCTIONS =======================
       
    27 
       
    28 // ---------------------------------------------------------
       
    29 // CNssContextSrsPortal::CNssContextSrsPortal
       
    30 // C++ default constructor can NOT contain any code that
       
    31 // might leave.
       
    32 // ---------------------------------------------------------
       
    33 //
       
    34 CNssContextSrsPortal::CNssContextSrsPortal()
       
    35     {
       
    36     RUBY_DEBUG0( "CNssContextSrsPortal::CNssContextSrsPortal" );
       
    37     
       
    38     iTerminationState = CNssContextSrsPortal::EVASTERMINATION_NOT_PENDING;
       
    39     iState = EVASNone;
       
    40     iRecovery = EFalse;
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------
       
    44 // CNssContextSrsPortal::~CNssContextSrsPortal
       
    45 // Destructor
       
    46 // ---------------------------------------------------------
       
    47 //
       
    48 CNssContextSrsPortal::~CNssContextSrsPortal()
       
    49     {
       
    50     RUBY_DEBUG0( "CNssContextSrsPortal::~CNssContextSrsPortal" );
       
    51     
       
    52     DeleteSRS();
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------
       
    56 // CNssContextSrsPortal::MsruoEvent
       
    57 // interface implementation from MSpeechRecognitionUtilityObserver
       
    58 // ---------------------------------------------------------
       
    59 //
       
    60 void CNssContextSrsPortal::MsruoEvent( TUid aEvent, TInt aResult )
       
    61     {
       
    62     RUBY_DEBUG2( "CNssContextSrsPortal::MsruoEvent - Event=0x%x, Result=%d", aEvent.iUid, aResult );
       
    63     
       
    64     TInt err = KErrNone;
       
    65     switch ( aEvent.iUid )
       
    66         {
       
    67         case KUidAsrEventCreateGrammarVal:
       
    68             RUBY_DEBUG0( "CNssContextSrsPortal::MsruoEvent - CreateGrammar" );
       
    69 
       
    70             if ( aResult == KErrNone )
       
    71                 {
       
    72                 iContext->SetGrammarId( iGrammarId );
       
    73                 iState = EVasWaitForCommitChanges;  // wait for commit
       
    74                 iSrsDBEventHandler->HandleSaveSrsDBCompleted();
       
    75                 }
       
    76             else
       
    77                 {   
       
    78                 CleanUp( aResult );
       
    79                 }
       
    80             break;
       
    81             
       
    82             
       
    83         case KUidAsrEventCreateLexiconVal:
       
    84             RUBY_DEBUG0( "CNssContextSrsPortal::MsruoEvent - CreateLexicon" );
       
    85                  
       
    86             if (aResult == KErrNone)
       
    87                 {
       
    88                 iContext->SetLexiconId(iLexiconId);
       
    89                 iState = EVASCreateGrammar;
       
    90                 err = iSpeechRecognitionUtility->CreateGrammar(iGrammarId);
       
    91                 if(err != KErrNone)
       
    92                     {
       
    93                     CleanUp(aResult);
       
    94                     }
       
    95                 }
       
    96             else
       
    97                 {
       
    98                 CleanUp(aResult);
       
    99                 }
       
   100             break;
       
   101             
       
   102             
       
   103         case KUidAsrEventCreateModelBankVal:
       
   104             RUBY_DEBUG0( "CNssContextSrsPortal::MsruoEvent - CreateModelBank" );
       
   105 
       
   106             // Two states may trigger this message:
       
   107             //  * When creating the first context
       
   108             //  * When resetting to factory settings
       
   109             
       
   110             // "Reset factory settings"
       
   111             if ( iState == EVASResetCreateModelBank )
       
   112                 {
       
   113                 if ( aResult == KErrNone )
       
   114                     {
       
   115                     iState = EVasWaitForCommitChanges;      // wait for commit
       
   116                     iSrsDBEventHandler->HandleResetSrsDBCompleted();
       
   117                     }
       
   118                 else
       
   119                     {
       
   120                     CleanUp( aResult );
       
   121                     }
       
   122                 }
       
   123             else
       
   124                 {
       
   125                 
       
   126                 if ( aResult == KErrNone )
       
   127                     {
       
   128                     iContext->SetModelBankId(iModelBankId);
       
   129                     iState = EVASCreateLexicon;
       
   130                     err = iSpeechRecognitionUtility->CreateLexicon(iLexiconId);
       
   131                     if ( err != KErrNone )
       
   132                         {
       
   133                         CleanUp( aResult );
       
   134                         }
       
   135                     }
       
   136                 else
       
   137                     {
       
   138                     CleanUp( aResult );
       
   139                     }
       
   140                 }
       
   141             break;
       
   142             
       
   143             
       
   144         case KUidAsrEventRemoveGrammarVal:
       
   145             RUBY_DEBUG0( "CNssContextSrsPortal::MsruoEvent - RemoveGrammar" );
       
   146             
       
   147             if ( aResult == KErrNone || aResult == KErrNotFound )
       
   148                 {
       
   149                 iState = EVASRemoveLexicon;
       
   150                 err = iSpeechRecognitionUtility->RemoveLexicon( 
       
   151                                 (TSILexiconID) iContext->LexiconId() );
       
   152                 if ( err != KErrNone )
       
   153                     {
       
   154                     CleanUp( aResult );
       
   155                     }
       
   156                 }
       
   157             else
       
   158                 {
       
   159                 CleanUp( aResult );
       
   160                 }
       
   161             break;
       
   162             
       
   163             // This case may never being called.
       
   164         case KUidAsrEventRemoveLexiconVal:
       
   165             RUBY_DEBUG0( "CNssContextSrsPortal::MsruoEvent - RemoveLexicon" );
       
   166              
       
   167             if ( aResult == KErrNone || aResult == KErrNotFound )
       
   168                 {
       
   169                 iState = EVasWaitForCommitChanges;      // wait for commit
       
   170                 iSrsDBEventHandler->HandleDeleteSrsDBCompleted();
       
   171                 }
       
   172             else
       
   173                 {
       
   174                 CleanUp( aResult );
       
   175                 }
       
   176             break;
       
   177             
       
   178             
       
   179             // This case may never being called.
       
   180         case KUidAsrEventRemoveModelBankVal:
       
   181             RUBY_DEBUG0( "CNssContextSrsPortal::MsruoEvent - RemoveModelBank" );
       
   182              
       
   183             // "Reset factory settings": Remove speaker adapted model bank
       
   184             //                           and create a new one.
       
   185             if ( iState == EVASResetRemoveModelBank )
       
   186                 {
       
   187                 TInt createErr = KErrNone;
       
   188                 
       
   189                 if ( aResult == KErrNone )
       
   190                     {
       
   191                     iState = EVASResetCreateModelBank;
       
   192                     
       
   193                     createErr = iSpeechRecognitionUtility->CreateModelBank( *iResetModelBankId );
       
   194                     }
       
   195                 
       
   196                 // If RemoveModelBank failed, or CreateModelBank 5 lines age failed.
       
   197                 if ( aResult != KErrNone || createErr != KErrNone )
       
   198                     {
       
   199                     DeleteSRS();
       
   200                     iSrsDBEventHandler->HandleSrsDBError( 
       
   201                             MNssCoreSrsDBEventHandler::EVasRemoveModelBankFailed );
       
   202                     }
       
   203                 }
       
   204             else
       
   205                 {
       
   206                 // Legacy code from SDND - I don't know when this is invoked
       
   207                 if ( aResult == KErrNone || aResult == KErrNotFound )
       
   208                     {
       
   209                     iState = EVasWaitForCommitChanges;      // wait for commit
       
   210                     
       
   211                     DeleteSRS();
       
   212                     iSrsDBEventHandler->HandleDeleteSrsDBCompleted();	
       
   213                     }
       
   214                 else
       
   215                     {
       
   216                     CleanUp( aResult );
       
   217                     }
       
   218                 }
       
   219             break;
       
   220             
       
   221             
       
   222             
       
   223         default:
       
   224             RUBY_DEBUG0( "CNssContextSrsPortal::MsruoEvent - default" );
       
   225             CleanUp( aResult );
       
   226             break;
       
   227     }
       
   228 }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CNssContextSrsPortal::DoSrsDBResult
       
   232 // Method to process the SRS DB result.
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 MNssCoreSrsDBEventHandler::TNssSrsDBResult 
       
   236 CNssContextSrsPortal::DoSrsDBResult(TInt aResult)
       
   237     {
       
   238     RUBY_DEBUG1( "CNssContextSrsPortal::DoSrsDBResult() aResult = %d", aResult );
       
   239     
       
   240     MNssCoreSrsDBEventHandler::TNssSrsDBResult errorCode;
       
   241     
       
   242     switch (aResult)
       
   243         {
       
   244         case KErrDiskFull:
       
   245             RUBY_DEBUG0( "CNssContextSrsPortal::DoSrsDBResult - KErrDiskFull" );
       
   246             
       
   247             errorCode = MNssCoreSrsDBEventHandler::EVasDiskFull;
       
   248             break;
       
   249         case KErrNoMemory:
       
   250             RUBY_DEBUG0( "CNssContextSrsPortal::DoSrsDBResult - KErrNoMemory" );
       
   251             
       
   252             errorCode = MNssCoreSrsDBEventHandler::EVasNoMemory;
       
   253             break;
       
   254         default:
       
   255             {
       
   256             RUBY_DEBUG1( "CNssContextSrsPortal::DoSrsDBResult - default, state = %d", iState );
       
   257             
       
   258             switch ( iState )
       
   259                 {
       
   260                 case EVASCreateModelBank:
       
   261                     errorCode = MNssCoreSrsDBEventHandler::EVasCreateModelBankFailed;
       
   262                     break;
       
   263                 case EVASCreateLexicon:
       
   264                     errorCode = MNssCoreSrsDBEventHandler::EVasCreateLexiconFailed;
       
   265                     break;
       
   266                 case EVASCreateGrammar:
       
   267                     errorCode = MNssCoreSrsDBEventHandler::EVasCreateGrammarFailed;
       
   268                     break;
       
   269                 case EVASRemoveModelBank:
       
   270                     errorCode = MNssCoreSrsDBEventHandler::EVasRemoveModelBankFailed;
       
   271                     break;
       
   272                 case EVASRemoveLexicon:
       
   273                     errorCode = MNssCoreSrsDBEventHandler::EVasRemoveLexiconFailed;
       
   274                     break;
       
   275                 case EVASRemoveGrammar:
       
   276                     errorCode = MNssCoreSrsDBEventHandler::EVasRemoveGrammarFailed;
       
   277                     break;
       
   278                 default:
       
   279                     errorCode = MNssCoreSrsDBEventHandler::EVasDBOperationError;
       
   280                     break;
       
   281                 }
       
   282             }
       
   283         }
       
   284     
       
   285     return errorCode;
       
   286     }
       
   287 
       
   288 
       
   289 
       
   290 // ---------------------------------------------------------
       
   291 // CNssContextSrsPortal::BeginSaveContextL
       
   292 // save context to srs database
       
   293 // ---------------------------------------------------------
       
   294 //
       
   295 void CNssContextSrsPortal::BeginSaveContextL( CNssContext* aContext, 
       
   296                                               MNssCoreSrsDBEventHandler* aSrsDBEventHandler )
       
   297     {
       
   298     RUBY_DEBUG_BLOCK( "CNssContextSrsPortal::BeginSaveContextL" );
       
   299     
       
   300     iSrsDBEventHandler = aSrsDBEventHandler;
       
   301     iContext = aContext;
       
   302     
       
   303     // verify the SRS is NULL, if not return failure
       
   304     if ( iSpeechRecognitionUtility )
       
   305         {
       
   306         User::Leave( KErrGeneral );
       
   307         }
       
   308     
       
   309     RUBY_DEBUG0( "CNssContextSrsPortal::CREATE SRS" );
       
   310     
       
   311     iSpeechRecognitionUtility = CNssSiUtilityWrapper::NewL( *this, KNssVASApiUid );
       
   312     
       
   313     TInt error;
       
   314     iRecovery = EFalse;
       
   315     
       
   316     // if creating context first time, create model bank and lexicon
       
   317     if ( !aContext->ModelBankAndLexiconExist() ) 
       
   318         {
       
   319         iState = EVASCreateModelBank;
       
   320         error = iSpeechRecognitionUtility->CreateModelBank(iModelBankId);
       
   321         if ( error != KErrNone )
       
   322             {
       
   323             DeleteSRS();
       
   324             User::Leave( error );
       
   325             }
       
   326         }
       
   327     else // Create grammar and lexicon
       
   328         {
       
   329         iState = EVASCreateLexicon;
       
   330         error = iSpeechRecognitionUtility->CreateLexicon(iLexiconId);
       
   331         if ( error != KErrNone )
       
   332             {
       
   333             DeleteSRS();
       
   334             User::Leave( error );
       
   335             }
       
   336         }
       
   337     }
       
   338 
       
   339 // ---------------------------------------------------------
       
   340 // CNssContextSrsPortal::BeginDeleteContextL
       
   341 // deletes context from SRS. 
       
   342 // ---------------------------------------------------------
       
   343 //
       
   344 void CNssContextSrsPortal::BeginDeleteContextL(CNssContext* aContext, 
       
   345                                 MNssCoreSrsDBEventHandler* aSrsDBEventHandler)
       
   346     {
       
   347     RUBY_DEBUG_BLOCK( "CNssContextSrsPortal::BeginDeleteContextL" );
       
   348     
       
   349     iSrsDBEventHandler = aSrsDBEventHandler;
       
   350     iContext = aContext;
       
   351     iRecovery = EFalse;
       
   352     
       
   353     // verify the SRS is NULL, if not return failure
       
   354     if ( iSpeechRecognitionUtility )
       
   355         {
       
   356         User::Leave( KErrGeneral );
       
   357         }
       
   358 
       
   359     RUBY_DEBUG0( "CNssContextSrsPortal::CREATE SRS" );
       
   360      
       
   361     iSpeechRecognitionUtility = CNssSiUtilityWrapper::NewL( *this, KNssVASApiUid );
       
   362     iState = EVASRemoveGrammar;
       
   363     
       
   364     TInt error = iSpeechRecognitionUtility->RemoveGrammar( (TSIGrammarID)aContext->GrammarId() );
       
   365     if ( error != KErrNone )
       
   366         {
       
   367         DeleteSRS();
       
   368         User::Leave( error );
       
   369         }
       
   370     
       
   371     // Lexicon will be deleted when grammar deletion callback arrives
       
   372     }
       
   373 
       
   374 // ---------------------------------------------------------
       
   375 // CNssContextSrsPortal::CommitSaveContext
       
   376 // commit context changes from SRS. 
       
   377 // ---------------------------------------------------------
       
   378 //
       
   379 TInt CNssContextSrsPortal::CommitSaveContext()
       
   380     {
       
   381     RUBY_DEBUG0( "CNssContextSrsPortal::CommitSaveContext" );
       
   382     
       
   383     TInt ret( KErrNone );
       
   384     
       
   385     if ( iState == EVasWaitForCommitChanges )
       
   386         {
       
   387         TInt error = iSpeechRecognitionUtility->CommitChanges();
       
   388         
       
   389         if ( error != KErrNone )
       
   390             {
       
   391             DeleteSRS();
       
   392             ret = error;
       
   393             }
       
   394         }
       
   395     else
       
   396         {
       
   397         DeleteSRS();
       
   398         ret = KErrGeneral;
       
   399         }
       
   400     
       
   401     DeleteSRS();
       
   402     iState = EVASNone;      // reset the state
       
   403     
       
   404     return ret;	
       
   405     }
       
   406 
       
   407 // ---------------------------------------------------------
       
   408 // CNssContextSrsPortal::RollbackSaveContext
       
   409 // commit context changes from SRS. 
       
   410 // ---------------------------------------------------------
       
   411 //
       
   412 TInt CNssContextSrsPortal::RollbackSaveContext()
       
   413     {
       
   414     RUBY_DEBUG0( "CNssContextSrsPortal::RollbackSaveContext" );
       
   415 
       
   416     if (iState != EVasWaitForCommitChanges)
       
   417         {
       
   418         DeleteSRS();
       
   419         return KErrGeneral;
       
   420         }
       
   421     
       
   422     DeleteSRS();
       
   423     iState = EVASNone;      // reset the state
       
   424     return KErrNone;	
       
   425     }
       
   426 
       
   427 // ---------------------------------------------------------
       
   428 // CNssContextSrsPortal::DeleteSRS
       
   429 // Delete the SRS, and set it to NULL
       
   430 // ---------------------------------------------------------
       
   431 //
       
   432 void CNssContextSrsPortal::DeleteSRS()
       
   433     {
       
   434     RUBY_DEBUG0( "CNssContextSrsPortal::DeleteSRS" );
       
   435     
       
   436     if ( iSpeechRecognitionUtility )
       
   437         {
       
   438         RUBY_DEBUG0( "CNssContextSrsPortal::deleted SRS........" );        
       
   439         
       
   440         delete iSpeechRecognitionUtility; 
       
   441         iSpeechRecognitionUtility = NULL;
       
   442         
       
   443         RUBY_DEBUG0( "CNssContextSrsPortal::.....SRS just deleted" );
       
   444         }      
       
   445     }
       
   446 
       
   447 // ---------------------------------------------------------
       
   448 // CNssContextSrsPortal::CleanUp
       
   449 // Clean up after SRS operation fail
       
   450 // ---------------------------------------------------------
       
   451 //
       
   452 void CNssContextSrsPortal::CleanUp( TInt aResult )
       
   453     {
       
   454     RUBY_DEBUG0( "CNssContextSrsPortal::CleanUp" );
       
   455 
       
   456     DeleteSRS();
       
   457     
       
   458     MNssCoreSrsDBEventHandler::TNssSrsDBResult errorCode = DoSrsDBResult( aResult );
       
   459     iSrsDBEventHandler->HandleSrsDBError( errorCode );            
       
   460     }
       
   461 
       
   462 // ---------------------------------------------------------
       
   463 // CNssContextSrsPortal::Register
       
   464 // context portal keeps a count of number of contexts created
       
   465 // increment count
       
   466 // ---------------------------------------------------------
       
   467 //
       
   468 void CNssContextSrsPortal::Register()
       
   469     {
       
   470     iContextCount++;
       
   471     }
       
   472 
       
   473 // ---------------------------------------------------------
       
   474 // CNssContextSrsPortal::Deregister
       
   475 // decrement count
       
   476 // id count is zero, and context builder is deleted, delete itself (portal),
       
   477 // ---------------------------------------------------------
       
   478 //
       
   479 void CNssContextSrsPortal::Deregister()
       
   480     {
       
   481     iContextCount--;
       
   482     if ( iContextCount == 0 && iTerminationState == EVASTERMINATION_PENDING )
       
   483         {
       
   484         delete this;
       
   485         }
       
   486     }
       
   487 
       
   488 // ---------------------------------------------------------
       
   489 // CNssContextSrsPortal::ContextCount
       
   490 // returns context count
       
   491 // ---------------------------------------------------------
       
   492 //
       
   493 TInt CNssContextSrsPortal::ContextCount()
       
   494     {
       
   495     return iContextCount;
       
   496     }
       
   497 
       
   498 // ---------------------------------------------------------
       
   499 // CNssContextSrsPortal::SetTerminationState
       
   500 // sets termination state
       
   501 // ---------------------------------------------------------
       
   502 //
       
   503 void CNssContextSrsPortal::SetTerminationState( TNssTerminationState aState )
       
   504    {
       
   505 	iTerminationState = aState;
       
   506    }
       
   507 
       
   508 // ---------------------------------------------------------
       
   509 // CNssContextSrsPortal::BeginResetModelsL
       
   510 // sets termination state
       
   511 // ---------------------------------------------------------
       
   512 //
       
   513 void CNssContextSrsPortal::BeginResetModelsL( TSIModelBankID aAdaptedModels,
       
   514                                               TSIModelBankID& aNewModels,
       
   515                                               MNssCoreSrsDBEventHandler* aSrsDBEventHandler )
       
   516     {
       
   517     RUBY_DEBUG_BLOCK( "CNssContextSrsPortal::BeginSaveContextL" );
       
   518     
       
   519     iSrsDBEventHandler = aSrsDBEventHandler;
       
   520     
       
   521     // verify the SRS is NULL, if not return failure
       
   522     if ( iSpeechRecognitionUtility )
       
   523         {
       
   524         User::Leave( KErrGeneral );
       
   525         }
       
   526     
       
   527     RUBY_DEBUG0( "CNssContextSrsPortal::CREATE SRS" );
       
   528     
       
   529     iSpeechRecognitionUtility = CNssSiUtilityWrapper::NewL( *this, KNssVASApiUid );
       
   530     TInt error = KErrNone;
       
   531     iRecovery = EFalse;
       
   532     
       
   533     iState = EVASResetRemoveModelBank;
       
   534     error = iSpeechRecognitionUtility->RemoveModelBank( aAdaptedModels );
       
   535     
       
   536     if ( error != KErrNone )
       
   537         {
       
   538         DeleteSRS();
       
   539         User::Leave( error );
       
   540         }
       
   541     
       
   542     iResetModelBankId = &aNewModels;
       
   543     }
       
   544 
       
   545 //  End of File