vtuis/videotelui/src/commands/cvtuicommandpolicymanager.cpp
branchRCL_3
changeset 35 779871d1e4f4
parent 0 ed9695c8bcbe
equal deleted inserted replaced
34:f15ac8e65a02 35:779871d1e4f4
       
     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:  Command policy manager class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <cvtlogger.h>
       
    20 
       
    21 #include "cvtuicommandpolicymanager.h"
       
    22 #include "cvtuicmdcontext.h"
       
    23 #include "cvtuicmdvalidatorbase.h"
       
    24 #include "cvtuicmdvalidationactionbase.h"
       
    25 #include "cvtuicmdcustomvalidationactionbase.h"
       
    26 #include "cvtuiprimarycmdpolicy.h"
       
    27 #include "cvtuicommandmanager.h"
       
    28 
       
    29 // ======== LOCAL FUNCTIONS ========
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // Used in InsertInOrder() method to compare two instances of CVtUiCmdContext.
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 TInt ContextTypeCmpFunc( const CVtUiCmdContext& aV1,
       
    36     const CVtUiCmdContext& aV2 )
       
    37     {
       
    38     return static_cast< TInt >( aV1.ContextType() ) -
       
    39         static_cast< TInt >( aV2.ContextType() );
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // Used in FindInOrder() method to compare instances of CVtUiCmdContext versus
       
    44 // given key value.
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 TInt ContextTypeCmpFuncVsKey( const TVtUiCmdContextType* aKey,
       
    48     const CVtUiCmdContext& aValue )
       
    49     {
       
    50     return static_cast< TInt > ( *aKey ) -
       
    51         static_cast< TInt >( aValue.ContextType() );
       
    52     }
       
    53 
       
    54 // ======== MEMBER FUNCTIONS ========
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CVtUiCommandPolicyManager::NewL
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CVtUiCommandPolicyManager*
       
    61 CVtUiCommandPolicyManager::NewL( const CVtUiCommandManager& aCmdManager )
       
    62     {
       
    63     CVtUiCommandPolicyManager* self =
       
    64         new ( ELeave ) CVtUiCommandPolicyManager( aCmdManager );
       
    65     CleanupStack::PushL( self );
       
    66     self->ConstructL();
       
    67     CleanupStack::Pop(); // self
       
    68     return self;
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CVtUiCommandPolicyManager::~CVtUiCommandPolicyManager
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 CVtUiCommandPolicyManager::~CVtUiCommandPolicyManager()
       
    76     {
       
    77     __VTPRINTENTER( "CmdPolicyMgr.~" )
       
    78     iContexts.Reset();
       
    79     iValidators.Reset();
       
    80     delete iPrimaryCommandPolicy;
       
    81     __VTPRINTEXIT( "CmdPolicyMgr.~" )
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // CVtUiCommandPolicyManager::RegisterCommandContext
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 TInt CVtUiCommandPolicyManager::RegisterCommandContext(
       
    89     CVtUiCmdContext& aContext )
       
    90     {
       
    91     __VTPRINTENTER( "CmdPolicyMgr.RegisterCommandContext" )
       
    92     TLinearOrder< CVtUiCmdContext > cmpFunc( ::ContextTypeCmpFunc );
       
    93     TInt result( KErrNone );
       
    94     TInt index( iContexts.FindInOrder( &aContext, cmpFunc ) );
       
    95     if ( index == KErrNotFound )
       
    96         {
       
    97         TInt error( iContexts.InsertInOrder( &aContext, cmpFunc ) );
       
    98         if ( !error )
       
    99             {
       
   100             CommandContextRegistered( aContext );
       
   101             }
       
   102         result = error;
       
   103         }
       
   104     else
       
   105         {
       
   106         result = KErrAlreadyExists;
       
   107         }
       
   108     __VTPRINTEXITR( "CmdPolicyMgr.RegisterCommandContext %d", result )
       
   109     return result;
       
   110     }
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // CVtUiCommandPolicyManager::UnregisterCommandContext
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CVtUiCommandPolicyManager::UnregisterCommandContext(
       
   117     CVtUiCmdContext& aContext )
       
   118     {
       
   119     __VTPRINTENTER( "CmdPolicyMgr.UnregisterCommandContext" )
       
   120     TLinearOrder< CVtUiCmdContext > cmpFunc( ::ContextTypeCmpFunc );
       
   121     TInt index( iContexts.FindInOrder( &aContext, cmpFunc ) );
       
   122     if ( index != KErrNotFound )
       
   123         {
       
   124         iContexts.Remove( index );
       
   125         CommandContextUnregistered( aContext );
       
   126         }
       
   127     __VTPRINTEXIT( "CmdPolicyMgr.UnregisterCommandContext" )
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // CVtUiCommandPolicyManager::AddValidator
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 TInt CVtUiCommandPolicyManager::AddValidator(
       
   135     CVtUiCmdValidatorBase& aValidator )
       
   136     {
       
   137     __VTPRINTENTER( "CmdPolicyMgr.AddValidator" )
       
   138     TInt error( iValidators.Append( &aValidator ) );
       
   139     if ( !error )
       
   140         {
       
   141         ValidatorAdded( aValidator );
       
   142         }
       
   143     __VTPRINTEXITR( "CmdPolicyMgr.AddValidator %d", error )
       
   144     return error;
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // CVtUiCommandPolicyManager::RemoveValidator
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 void CVtUiCommandPolicyManager::RemoveValidator(
       
   152     CVtUiCmdValidatorBase& aValidator )
       
   153     {
       
   154     __VTPRINTENTER( "CmdPolicyMgr.RemoveValidator" )
       
   155     TInt index( iValidators.Find( &aValidator ) );
       
   156     if ( index != KErrNotFound )
       
   157         {
       
   158         iValidators.Remove( index );
       
   159         ValidatorRemoved( aValidator );
       
   160         }
       
   161     __VTPRINTEXIT( "CmdPolicyMgr.RemoveValidator" )
       
   162     }
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // CVtUiCommandPolicyManager::ValidateMenuItemsL
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 void CVtUiCommandPolicyManager::ValidateMenuItemsL(
       
   169     CVtUiCmdValidationActionBase& aAction )
       
   170     {
       
   171     __VTPRINTENTER( "CmdPolicyMgr.ValidateMenuItemsL" )
       
   172     ValidateL( EVtUiCmdContextTypeMenu, aAction );
       
   173     __VTPRINTEXIT( "CmdPolicyMgr.ValidateMenuItemsL" )
       
   174     }
       
   175 
       
   176 // ---------------------------------------------------------------------------
       
   177 // CVtUiCommandPolicyManager::ValidateSoftkeyItemsL
       
   178 // ---------------------------------------------------------------------------
       
   179 //
       
   180 void CVtUiCommandPolicyManager::ValidateSoftkeyItemsL(
       
   181     CVtUiCmdValidationActionBase& aAction )
       
   182     {
       
   183     __VTPRINTENTER( "CmdPolicyMgr.ValidateSoftkeyItemsL" )
       
   184     ValidateL( EVtUiCmdContextTypeSoftkey, aAction );
       
   185     __VTPRINTEXIT( "CmdPolicyMgr.ValidateSoftkeyItemsL" )
       
   186     }
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // CVtUiCommandPolicyManager::ValidateToolbarItemsL
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 void CVtUiCommandPolicyManager::ValidateToolbarItemsL(
       
   193     CVtUiCmdValidationActionBase& aAction )
       
   194     {
       
   195     __VTPRINTENTER( "CmdPolicyMgr.ValidateToolbarItemsL" )
       
   196     ValidateL( EVtUiCmdContextTypeToolbar, aAction );
       
   197     __VTPRINTEXIT( "CmdPolicyMgr.ValidateToolbarItemsL" )
       
   198     }
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // CVtUiCommandPolicyManager::CustomValidateL
       
   202 // ---------------------------------------------------------------------------
       
   203 //
       
   204 void CVtUiCommandPolicyManager::CustomValidateL(
       
   205     CVtUiCmdCustomValidationActionBase& aAction )
       
   206     {
       
   207     __VTPRINTENTER( "CmdPolicyMgr.CustomValidateL" )
       
   208     ValidateL( EVtUiCmdContextTypeExecution, aAction );
       
   209     __VTPRINTEXIT( "CmdPolicyMgr.CustomValidateL" )
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // CVtUiCommandPolicyManager::CVtUiCommandPolicyManager
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 CVtUiCommandPolicyManager::CVtUiCommandPolicyManager(
       
   217     const CVtUiCommandManager& aCmdManager ) : iCommandManager( aCmdManager )
       
   218     {
       
   219     __VTPRINTENTER( "CmdPolicyMgr.ctor" )
       
   220     __VTPRINTEXIT( "CmdPolicyMgr.ctor" )
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // CVtUiCommandPolicyManager::ConstructL
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 void CVtUiCommandPolicyManager::ConstructL()
       
   228     {
       
   229     __VTPRINTENTER( "CmdPolicyMgr.ConstructL" )
       
   230     iPrimaryCommandPolicy =
       
   231         CVtUiPrimaryCmdPolicy::NewL( iCommandManager.UiStates(),
       
   232         iCommandManager.AppUi() );
       
   233     User::LeaveIfError( AddValidator( *iPrimaryCommandPolicy ) );
       
   234     __VTPRINTEXIT( "CmdPolicyMgr.ConstructL" )
       
   235     }
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // CVtUiCommandPolicyManager::CommandContextRegistered
       
   239 // ---------------------------------------------------------------------------
       
   240 //
       
   241 void CVtUiCommandPolicyManager::CommandContextRegistered(
       
   242     CVtUiCmdContext& aContext )
       
   243     {
       
   244     __VTPRINTENTER( "CmdPolicyMgr.CommandContextRegistered" )
       
   245     for ( TInt i = iValidators.Count() - 1 ; i >= 0; i-- )
       
   246         {
       
   247         aContext.AddValidator( *iValidators[ i ] );
       
   248         }
       
   249     __VTPRINTEXIT( "CmdPolicyMgr.CommandContextRegistered" )
       
   250     }
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // CVtUiCommandPolicyManager::CommandContextUnregistered
       
   254 // ---------------------------------------------------------------------------
       
   255 //
       
   256 void CVtUiCommandPolicyManager::CommandContextUnregistered(
       
   257     CVtUiCmdContext& aContext )
       
   258     {
       
   259     __VTPRINTENTER( "CmdPolicyMgr.CommandContextUnregistered" )
       
   260     for ( TInt i = iValidators.Count() - 1 ; i >= 0; i-- )
       
   261         {
       
   262         aContext.RemoveValidator( *iValidators[ i ] );
       
   263         }
       
   264     __VTPRINTEXIT( "CmdPolicyMgr.CommandContextUnregistered" )
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // CVtUiCommandPolicyManager::ValidatorAdded
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 void CVtUiCommandPolicyManager::ValidatorAdded(
       
   272     CVtUiCmdValidatorBase& aValidator )
       
   273     {
       
   274     __VTPRINTENTER( "CmdPolicyMgr.ValidatorAdded" )
       
   275     for ( TInt i = iContexts.Count() - 1 ; i >= 0; i-- )
       
   276         {
       
   277         iContexts[ i ]->AddValidator( aValidator );
       
   278         }
       
   279     __VTPRINTEXIT( "CmdPolicyMgr.ValidatorAdded" )
       
   280     }
       
   281 
       
   282 // ---------------------------------------------------------------------------
       
   283 // CVtUiCommandPolicyManager::ValidatorRemoved
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 void CVtUiCommandPolicyManager::ValidatorRemoved(
       
   287     CVtUiCmdValidatorBase& aValidator )
       
   288     {
       
   289     __VTPRINTENTER( "CmdPolicyMgr.ValidatorRemoved" )
       
   290     for ( TInt i = iContexts.Count() - 1 ; i >= 0; i-- )
       
   291         {
       
   292         iContexts[ i ]->RemoveValidator( aValidator );
       
   293         }
       
   294     __VTPRINTEXIT( "CmdPolicyMgr.ValidatorRemoved" )
       
   295     }
       
   296 
       
   297 // ---------------------------------------------------------------------------
       
   298 // CVtUiCommandPolicyManager::FindCommandContextByType
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 CVtUiCmdContext* CVtUiCommandPolicyManager::FindCommandContextByType(
       
   302         TVtUiCmdContextType aContextType )
       
   303     {
       
   304     __VTPRINTENTER( "CmdPolicyMgr.FindCommandContextByType" )
       
   305     CVtUiCmdContext* result = NULL;
       
   306     TInt index( iContexts.FindInOrder( aContextType,
       
   307         ::ContextTypeCmpFuncVsKey ) );
       
   308     if ( index != KErrNotFound )
       
   309         {
       
   310         result = iContexts[ index ];
       
   311         }
       
   312     __VTPRINTEXIT( "CmdPolicyMgr.FindCommandContextByType" )
       
   313     return result;
       
   314     }
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // CVtUiCommandPolicyManager::ValidateL
       
   318 // ---------------------------------------------------------------------------
       
   319 //
       
   320 void CVtUiCommandPolicyManager::ValidateL( TVtUiCmdContextType aContextType,
       
   321     CVtUiCmdValidationActionBase& aAction )
       
   322     {
       
   323     __VTPRINTENTER( "CmdPolicyMgr.ValidateL" )
       
   324     CVtUiCmdContext* context =
       
   325         FindCommandContextByType( aContextType );
       
   326     if ( context )
       
   327         {
       
   328         aAction.BeginProcess();
       
   329         context->ValidateL( aAction );
       
   330         aAction.EndProcess();
       
   331         }
       
   332     __VTPRINTEXIT( "CmdPolicyMgr.ValidateL" )
       
   333     }
       
   334