srsf/vcommandhandler/src/contextprovider.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:  Provides access to the VAS context
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "rubydebug.h"
       
    20 #include "contextprovider.h"
       
    21 
       
    22 #include <nssvasmcontextmgr.h>
       
    23 #include <nssvasmcontext.h>
       
    24 #include "vcommandinternalapi.h"
       
    25 
       
    26 _LIT( KContextProviderPanic, "VCP" );
       
    27 
       
    28 CContextProvider* CContextProvider::NewL( CNssVASDBMgr& aVasDbManager )
       
    29 	{
       
    30 	CContextProvider* self = CContextProvider::NewLC( aVasDbManager );
       
    31 	CleanupStack::Pop( self );
       
    32 	return self;
       
    33 	}
       
    34 	
       
    35 CContextProvider* CContextProvider::NewLC( CNssVASDBMgr& aVasDbManager )
       
    36 	{
       
    37 	CContextProvider* self = new (ELeave) CContextProvider;
       
    38 	CleanupStack::PushL( self );
       
    39 	// No part of initialization can leave -> no ConstructL()
       
    40 	self->iContextManager = aVasDbManager.GetContextMgr();
       
    41 	return self;
       
    42 	}
       
    43 	
       
    44 CContextProvider::~CContextProvider()
       
    45 	{
       
    46 	delete iSaveContextWaiter; // if any
       
    47 	delete iVasContext;  // if any
       
    48 	}
       
    49 	
       
    50 /**
       
    51 * Returns the VCommand VAS context. If it doesn't exists,
       
    52 * it is being created. client is responsible for the context deletion
       
    53 * 
       
    54 * @todo If support for different contexts is needed, 
       
    55 * 1) Rename the method into GetContextLC;
       
    56 * 2) add the optional
       
    57 * argument "const TDesC& aContextName = KVoiceCommandContext"
       
    58 */
       
    59 MNssContext* CContextProvider::GetVCommandContextLC()
       
    60 	{
       
    61 	RUBY_DEBUG0( "CContextProvider::GetVCommandContextL start" );
       
    62     __ASSERT_ALWAYS( !iVasContext, User::Leave( KErrNotReady ) );
       
    63     if( iContextManager->GetContextList( this ) != KErrNone )
       
    64         {
       
    65         RUBY_DEBUG0( "CContextProvider::GetVCommandContextL No context in the VAS yet. Creating one" );
       
    66         return CreateVCommandContextLC();
       
    67         }
       
    68     else
       
    69         {
       
    70         RUBY_DEBUG0( "CContextProvider::GetVCommandContextL List of contexts requested" );
       
    71         }
       
    72         TRAPD ( err, WaitForAsyncCallbackL(); );
       
    73         if ( err )
       
    74             {
       
    75             iContextManager->CancelGetContext();
       
    76             User::Leave( err );
       
    77             }
       
    78         MNssContext* result = iVasContext;
       
    79         iVasContext = NULL;        
       
    80         CleanupDeletePushL( result );
       
    81         
       
    82         RUBY_DEBUG0( "CContextProvider::GetVCommandContextL end" );
       
    83         return result;
       
    84 	}
       
    85 
       
    86 /**
       
    87 * Creates VCommand context. To be called during the GetVCommandContextLC
       
    88 * sequence
       
    89 */	
       
    90 MNssContext* CContextProvider::CreateVCommandContextLC()
       
    91 	{
       
    92 	RUBY_DEBUG0( "CContextProvider::CreateVCommandContextLC start" );
       
    93 	__ASSERT_ALWAYS( !iVasContext, User::Leave( KErrNotReady ) );
       
    94     iVasContext = iContextManager->CreateContextL();
       
    95     iVasContext->SetNameL( KVoiceCommandContext );
       
    96     iVasContext->SetGlobal( ETrue );
       
    97     
       
    98     if ( iContextManager->SaveContext( this, iVasContext ) != 
       
    99                             KErrNone )
       
   100         {
       
   101         RUBY_ERROR0( "CContextProvider::CreateVCommandContextLC SaveContext call failed" );
       
   102         User::Leave( KErrAbort );
       
   103         }
       
   104     else 
       
   105         {
       
   106         RUBY_DEBUG0( "CContextProvider::CreateVCommandContextLC SaveContext requested" );
       
   107         }
       
   108         
       
   109     // We cannot use the inherited WaitForAsyncCallback. 
       
   110     // It might already be waiting from GetVCommandContextLC
       
   111     /** 
       
   112      * @todo Consider transforming WaitForAsyncCallback from wait/released into 
       
   113      *        wait_counter
       
   114      */
       
   115     iSaveContextWaiter = CAsyncWorker::NewL();
       
   116     iSaveContextWaiter->WaitForAsyncCallbackL();
       
   117     delete iSaveContextWaiter;
       
   118     iSaveContextWaiter = NULL;
       
   119     MNssContext* result = iVasContext;
       
   120     iVasContext = NULL;
       
   121     CleanupDeletePushL( result );
       
   122 
       
   123     RUBY_DEBUG0( "CContextProvider::CreateVCommandContextLC end" );
       
   124     return result;
       
   125 	}	
       
   126 				
       
   127 // From MNssGetContextClient
       
   128 /**
       
   129 * Callback to indicate GetContext successed.
       
   130 * client has to delete the context after using the context.
       
   131 * @since 2.0
       
   132 * @param aContext
       
   133 * @param aErrorCode KErrNone if getting of context was successfull
       
   134 * @return None
       
   135 */
       
   136 void CContextProvider::GetContextCompleted( MNssContext* /*aContext*/,
       
   137                                             TInt /*aErrorCode*/ )
       
   138 	{
       
   139 	RUBY_ERROR0( "CContextProvider::GetContextCompleted Should never be called" );
       
   140 	__ASSERT_ALWAYS( EFalse, User::Panic( KContextProviderPanic, KErrNotSupported ) );
       
   141 	}
       
   142 
       
   143 /**
       
   144 * Callback to indicate GetContext successed.
       
   145 * client has to ResetAndDestroy() after using the list.
       
   146 * @since 2.0
       
   147 * @param aContextList A list of contexts. 
       
   148 * @param aErrorCode KErrNone if getting of context list was successfull
       
   149 * @return None
       
   150 */
       
   151 void CContextProvider::GetContextListCompleted(
       
   152     MNssContextListArray *aContextList, TInt aErrorCode )
       
   153 	{
       
   154 	// No single L function called => TRAPD( DoGetContextListCompletedL() ) not needed
       
   155     RUBY_DEBUG0( "CContextProvider::GetContextListCompleted start" );
       
   156     
       
   157     if( aErrorCode == KErrNone )
       
   158         {
       
   159         TInt err = KErrNone;
       
   160 
       
   161         if ( iVasContext )
       
   162             {
       
   163             RUBY_ERROR0( "CContextProvider::GetContextListCompleted iVasContext is not NULL" );
       
   164             err = KErrAlreadyExists;
       
   165             }
       
   166        
       
   167         if( !aContextList )
       
   168             {
       
   169             RUBY_ERROR0( "CContextProvider::GetContextListCompleted empty context list" );
       
   170             err = KErrArgument;
       
   171             }
       
   172         
       
   173         if( err == KErrNone )  // guarding conditions are ok
       
   174             {
       
   175             RUBY_DEBUG1( "CContextProvider::GetContextListCompleted Iterating over [%d] contexts",
       
   176                         aContextList->Count() );
       
   177             for( TInt i ( 0 ); i < aContextList->Count(); i++ ) 
       
   178                 {
       
   179                 if ( aContextList->At(i)->ContextName() == KVoiceCommandContext )
       
   180                     {
       
   181                     iVasContext = aContextList->At( i );
       
   182                     }
       
   183                 else
       
   184                     {
       
   185                     delete aContextList->At( i );
       
   186                     }
       
   187                 }  // for
       
   188             aContextList->Reset();
       
   189             delete aContextList;
       
   190         
       
   191         	if( !iVasContext ) 
       
   192     	        {
       
   193     	        RUBY_DEBUG0( "CContextProvider::GetContextListCompleted COMMAND context not found" );
       
   194     	        TRAP( err,
       
   195     	                iVasContext = CreateVCommandContextLC();
       
   196     	        		CleanupStack::Pop( iVasContext );
       
   197     	        		);
       
   198     	        if( err != KErrNone )
       
   199     	        	{
       
   200     	        	RUBY_ERROR1( "CContextProvider::GetContextListCompleted \
       
   201     	        				 Failed to create the VCommandContext", err );
       
   202     	        	}
       
   203     	        }
       
   204             }  // if guarding conditions are ok
       
   205             
       
   206         if( err != KErrNone )
       
   207         	{
       
   208         	RUBY_ERROR1( "CContextProvider::GetContextListCompleted failed with [%d]", err );
       
   209         	// Whenever error is returned, post-WaitForAsynchCallbackL code has no chance
       
   210             // to clean the iVasContext;
       
   211             delete iVasContext;
       
   212             iVasContext = NULL;
       
   213         	}
       
   214             
       
   215         StopAsyncWaitingOrPanic( err );
       
   216         }
       
   217         
       
   218     else // aErrorCode is not KErrNone
       
   219         {
       
   220         RUBY_ERROR1( "CContextProvider::GetContextCompleted error. Error code [%d] ", aErrorCode );
       
   221         
       
   222         // Whenever error is returned, post-WaitForAsynchCallbackL code has no chance
       
   223         // to clean the iVasContext;
       
   224         delete iVasContext;
       
   225         iVasContext = NULL;
       
   226         StopAsyncWaitingOrPanic( aErrorCode );       
       
   227         }
       
   228     
       
   229     RUBY_DEBUG0( "CContextProvider::GetContextListCompleted end" );     
       
   230    	}
       
   231 
       
   232 // From MNssSaveContextClient
       
   233 
       
   234 /**
       
   235 * Callback to indicate SaveContext successed.
       
   236 * @since 2.0
       
   237 * @param aErrorCode KErrNone if saving of context was successfull
       
   238 * @return None
       
   239 */
       
   240 void CContextProvider::SaveContextCompleted( TInt aErrorCode )
       
   241 	{
       
   242 	RUBY_DEBUG0( "CContextProvider::SaveContextCompleted start" );
       
   243 	
       
   244 	if( aErrorCode == KErrNone )
       
   245 	    {
       
   246 	    iSaveContextWaiter->StopAsyncWaitingOrPanic( KErrNone );    
       
   247 	    }
       
   248     else
       
   249         {
       
   250         RUBY_ERROR1( "CContextProvider::SaveContextCompleted error. Error code [%d] ", aErrorCode );
       
   251         
       
   252         // Whenever error is returned, post-WaitForAsynchCallbackL code has no chance
       
   253         // to clean the iVasContext;
       
   254         delete iVasContext;
       
   255         iVasContext = NULL;
       
   256         iSaveContextWaiter->StopAsyncWaitingOrPanic( aErrorCode );        
       
   257         }
       
   258     
       
   259     RUBY_DEBUG0( "CContextProvider::SaveContextCompleted end" );
       
   260 	}
       
   261 
       
   262 //End of file