idlehomescreen/xmluicontroller/src/policyevaluator.cpp
changeset 0 f72a12da539e
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Policy evaluator
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "xnnodeappif.h"
       
    20 #include "xnproperty.h"
       
    21 #include "xnuiengineappif.h"
       
    22 #include "policyevaluator.h"
       
    23 #include "aipolicyelement.h"
       
    24 #include "aixmluiutils.h"
       
    25 #include "aistrcnv.h"
       
    26 #include "aixmluiconstants.h"
       
    27 
       
    28 namespace
       
    29     {
       
    30 
       
    31     void ReplaceCharacters( TDes8& aDes,
       
    32                             TChar aOriginal,
       
    33                             TChar aNew )
       
    34         {
       
    35         if ( aDes.Length() == 0 )
       
    36             {
       
    37             return;
       
    38             }
       
    39 
       
    40         TPtr8 des = aDes.MidTPtr(0);
       
    41         TInt pos = des.Locate( aOriginal );
       
    42 
       
    43         while ( pos != KErrNotFound )
       
    44             {
       
    45             des[ pos++ ] = aNew;
       
    46 
       
    47             if ( pos == des.Length() )
       
    48                 {
       
    49                 break;
       
    50                 }
       
    51 
       
    52             des.Set( des.MidTPtr( pos ) );
       
    53             pos = des.Locate( aOriginal );
       
    54             }
       
    55         }
       
    56 
       
    57     }
       
    58 
       
    59 using namespace AiXmlUiController;
       
    60 
       
    61 enum EAI2Policies
       
    62     {
       
    63     KAI2PolUnset = 0,
       
    64     KAI2PolShow,
       
    65     KAI2PolHide,
       
    66     };
       
    67 
       
    68 // ======== MEMBER FUNCTIONS ========
       
    69 
       
    70 CPolicyEvaluator::CPolicyEvaluator()
       
    71     {
       
    72     }
       
    73 
       
    74 
       
    75 void CPolicyEvaluator::ConstructL()
       
    76     {
       
    77     }
       
    78 
       
    79 
       
    80 CPolicyEvaluator* CPolicyEvaluator::NewL()
       
    81     {
       
    82     CPolicyEvaluator* self = new( ELeave ) CPolicyEvaluator;
       
    83     return self;
       
    84     }
       
    85 
       
    86 
       
    87 CPolicyEvaluator::~CPolicyEvaluator()
       
    88     {
       
    89     }
       
    90 
       
    91 void CPolicyEvaluator::EvaluateContentPolicyL( CXnNodeAppIf& aTarget,
       
    92                                                RAiPolicyElementArray& aPolicyArray )
       
    93     {
       
    94     EvaluatePolicyL( aTarget,
       
    95                      aPolicyArray,
       
    96                      AiUiDef::xml::policy::KContent );
       
    97     }
       
    98 
       
    99 void CPolicyEvaluator::EvaluateEmptyContentPolicyL( CXnNodeAppIf& aTarget,
       
   100                                                     RAiPolicyElementArray& aPolicyArray )
       
   101     {
       
   102     EvaluatePolicyL( aTarget,
       
   103                      aPolicyArray,
       
   104                      AiUiDef::xml::policy::KEmptyContent );
       
   105     }
       
   106 
       
   107 void CPolicyEvaluator::EvaluateVisibilityPolicyL( CXnNodeAppIf& aTarget,
       
   108                                                   RAiPolicyElementArray& aPolicyArray )
       
   109     {
       
   110     EvaluatePolicyL( aTarget,
       
   111                      aPolicyArray,
       
   112                      AiUiDef::xml::policy::KVisibilityPolicy );
       
   113     }
       
   114 
       
   115 void CPolicyEvaluator::EvaluateContentChangedPolicyL( CXnNodeAppIf& aTarget,
       
   116                                                   RAiPolicyElementArray& aPolicyArray )
       
   117     {
       
   118     EvaluatePolicyL( aTarget,
       
   119                      aPolicyArray,
       
   120                      AiUiDef::xml::policy::KContentChanged );
       
   121     }
       
   122 
       
   123 void CPolicyEvaluator::EvaluateResourcePolicyL( CXnNodeAppIf& aTarget,
       
   124                                                 CXnNodeAppIf& aResource,
       
   125                                                 RAiPolicyElementArray& aPolicyArray )
       
   126     {
       
   127     RPointerArray< CXnNodeAppIf > policyArray =
       
   128         FindPropertyElementL( aTarget,
       
   129                               AiUiDef::xml::policy::KResource );
       
   130 
       
   131     CleanupClosePushL( policyArray );
       
   132 
       
   133     aPolicyArray.ReserveL( aPolicyArray.Count() + policyArray.Count() );
       
   134 
       
   135     for ( TInt i = 0; i < policyArray.Count(); ++i )
       
   136         {
       
   137         CXnNodeAppIf* property = policyArray[ i ];
       
   138 
       
   139         // Get name property to lookup target ui element
       
   140         const TDesC8* name = PropertyValue( *property,
       
   141                                       AiUiDef::xml::property::KName );
       
   142                 
       
   143         CXnNodeAppIf* target = NULL;
       
   144         if( name )
       
   145             {
       
   146             // Lookup target ui element
       
   147             target = property->UiEngineL()->FindNodeByIdL( *name, property->Namespace() );
       
   148             }
       
   149 
       
   150         if ( !target )
       
   151             {
       
   152             // No target found. Continue with next property
       
   153             continue;
       
   154             }
       
   155 
       
   156         const TDesC8* value = PropertyValue( *property,
       
   157                                              AiUiDef::xml::property::KValue );
       
   158 
       
   159         if ( !value )
       
   160             {
       
   161             // No value found. Continue with next property
       
   162             continue;
       
   163             }
       
   164 
       
   165         // Check if resource matches condition
       
   166         const TDesC8* id = PropertyValue( aResource,
       
   167                                           XnPropertyNames::common::KId );
       
   168 
       
   169         if ( !id )
       
   170             {
       
   171             continue;
       
   172             }
       
   173 
       
   174         TInt conditionEnd = value->Locate( KRightParenthesis );
       
   175 
       
   176         if ( conditionEnd++ < 0 )
       
   177             {
       
   178             continue;
       
   179             }
       
   180 
       
   181         TPtrC8 condition( value->Left( conditionEnd ) );
       
   182         TPtrC8 cssValue( value->Mid( conditionEnd ) );
       
   183 
       
   184         HBufC8* condBuffer = condition.AllocL();
       
   185         TPtr8 condPtr = condBuffer->Des();
       
   186         ReplaceCharacters( condPtr, KLeftParenthesis, KWhiteSpace );
       
   187         ReplaceCharacters( condPtr, KComma, KWhiteSpace );
       
   188         ReplaceCharacters( condPtr, KRightParenthesis, KWhiteSpace );
       
   189 
       
   190         if ( MatchCondition( *id, condPtr ) )
       
   191             {
       
   192             // Append target and value.
       
   193             // This cannot fail because space has been reserved.
       
   194             aPolicyArray.Append( TAiPolicyElement( *target, cssValue ) );
       
   195             }
       
   196 
       
   197         delete condBuffer;
       
   198         }
       
   199 
       
   200     CleanupStack::PopAndDestroy( &policyArray );
       
   201     }
       
   202 
       
   203 void CPolicyEvaluator::EvaluatePolicyL( CXnNodeAppIf& aTarget,
       
   204                                         RAiPolicyElementArray& aPolicyArray,
       
   205                                         const TDesC8& aPolicyClass )
       
   206     {
       
   207     RPointerArray< CXnNodeAppIf > policyArray =
       
   208         FindPropertyElementL( aTarget,
       
   209                               aPolicyClass );
       
   210 
       
   211     CleanupClosePushL( policyArray );
       
   212 
       
   213     aPolicyArray.ReserveL( aPolicyArray.Count() + policyArray.Count() );
       
   214 
       
   215     for ( TInt i = 0; i < policyArray.Count(); ++i )
       
   216         {
       
   217         CXnNodeAppIf* property = policyArray[ i ];
       
   218 
       
   219         // Get name property to lookup target ui element
       
   220         const TDesC8* name = PropertyValue( *property,
       
   221                                       AiUiDef::xml::property::KName );
       
   222                 
       
   223         CXnNodeAppIf* target = NULL;
       
   224         if( name )
       
   225             {
       
   226             // Lookup target ui element
       
   227             target = property->UiEngineL()->FindNodeByIdL( *name, property->Namespace() );
       
   228             }
       
   229 
       
   230         if ( !target )
       
   231             {
       
   232             // No target found. Continue with next property
       
   233             continue;
       
   234             }
       
   235 
       
   236         const TDesC8* value = PropertyValue( *property,
       
   237                                              AiUiDef::xml::property::KValue );
       
   238 
       
   239         if ( !value )
       
   240             {
       
   241             // No value found. Continue with next property
       
   242             continue;
       
   243             }
       
   244 
       
   245         if( aPolicyClass == AiUiDef::xml::policy::KVisibilityPolicy )
       
   246             {
       
   247             TInt conditionEnd = value->Locate( KRightParenthesis );
       
   248 
       
   249             if ( conditionEnd++ < 0 )
       
   250                 {
       
   251                 continue;
       
   252                 }
       
   253 
       
   254             TPtrC8 condition( value->Left( conditionEnd ) );
       
   255             TPtrC8 cssValue( value->Mid( conditionEnd ) );
       
   256 
       
   257             HBufC8* condBuffer = condition.AllocLC();
       
   258             TPtr8 condPtr = condBuffer->Des();
       
   259             ReplaceCharacters( condPtr, KLeftParenthesis, KWhiteSpace );
       
   260             ReplaceCharacters( condPtr, KComma, KWhiteSpace );
       
   261             ReplaceCharacters( condPtr, KRightParenthesis, KWhiteSpace );
       
   262 
       
   263             if ( MatchVisibilityConditionL( condPtr, *property, aPolicyArray ) )
       
   264                 {
       
   265                 // Append target and value.
       
   266                 // This cannot fail because space has been reserved.
       
   267                 aPolicyArray.Append( TAiPolicyElement( *target, cssValue ) );
       
   268                 }
       
   269 
       
   270             CleanupStack::PopAndDestroy( condBuffer );
       
   271             }
       
   272         else
       
   273             {
       
   274             // Append target and value.
       
   275             // This cannot fail because space has been reserved.
       
   276             aPolicyArray.Append( TAiPolicyElement( *target, *value ) );
       
   277             }
       
   278         }
       
   279 
       
   280     CleanupStack::PopAndDestroy( &policyArray );
       
   281     }
       
   282 
       
   283 TBool CPolicyEvaluator::MatchCondition( const TDesC8& aId,
       
   284                                         const TDesC8& aCondition )
       
   285     {
       
   286     TBool negation = EFalse;
       
   287 
       
   288     TLex8 parser( aCondition );
       
   289 
       
   290     if ( parser.NextToken() == AiUiDef::xml::policy::KCondition )
       
   291         {
       
   292         // Condition found
       
   293         parser.SkipSpace();
       
   294 
       
   295         // check negation
       
   296         if ( parser.Get() == KNotOperator )
       
   297             {
       
   298             negation = ETrue;
       
   299             }
       
   300         else
       
   301             {
       
   302             parser.UnGet();
       
   303             }
       
   304 
       
   305         parser.SkipSpace();
       
   306 
       
   307         TBool found = EFalse;
       
   308 
       
   309         // Find id from the list. Stop when found or in the end of string.
       
   310         while ( !parser.Eos() )
       
   311             {
       
   312             if ( parser.NextToken() == aId )
       
   313                 {
       
   314                 found = ETrue;
       
   315                 break;
       
   316                 }
       
   317             }
       
   318 
       
   319         // Test if id matches the given condition
       
   320         return ( ( found && !negation ) || ( !found && negation ) ); // found XOR negation
       
   321         }
       
   322     else
       
   323         {
       
   324         return EFalse;
       
   325         }
       
   326 
       
   327     }
       
   328 
       
   329 TBool CPolicyEvaluator::MatchVisibilityConditionL( const TDesC8& aCondition,
       
   330                                                   CXnNodeAppIf& aProperty,
       
   331                                                   RAiPolicyElementArray& aPolicyArray )
       
   332     {
       
   333     TBool negation = EFalse;
       
   334     TBool andOperation = EFalse;
       
   335     TInt nodesVisible = 0; // for managing the end of 'or' op.
       
   336 
       
   337     TLex8 parser( aCondition );
       
   338 
       
   339     if ( parser.NextToken() == AiUiDef::xml::policy::KCondition )
       
   340         {
       
   341         // Condition found
       
   342         parser.SkipSpace();
       
   343 
       
   344         // check negation
       
   345         if ( parser.Get() == KNotOperator )
       
   346             {
       
   347             negation = ETrue;
       
   348             }
       
   349         else
       
   350             {
       
   351             parser.UnGet();
       
   352             }
       
   353 
       
   354         if ( parser.Get() == KAndOperator )
       
   355             {
       
   356             andOperation = ETrue;
       
   357             }
       
   358         else
       
   359             {
       
   360             parser.UnGet();
       
   361             }
       
   362 
       
   363         parser.SkipSpace();
       
   364 
       
   365         // Determine aCondition lists visibilities. Stop when:
       
   366         //     - Eos reached
       
   367         //     - First 'false' in 'and' op
       
   368         //     - First 'true' in 'or' op
       
   369         while ( !parser.Eos() )
       
   370             {
       
   371             TPtrC8 id( parser.NextToken() );
       
   372             CXnNodeAppIf* target = NULL;
       
   373             TBool nodeVisible = EFalse;
       
   374 
       
   375             TBool displayBlockSet = KAI2PolUnset;
       
   376             TBool visibilityVisibleSet = KAI2PolUnset;
       
   377 
       
   378             // Lookup target ui element
       
   379             target = aProperty.UiEngineL()->FindNodeByIdL( id, aProperty.Namespace() );
       
   380 
       
   381             if( target )
       
   382                 {
       
   383                 // first try to first the display or visibility property updates from policy array
       
   384                 for( TInt i = 0; i < aPolicyArray.Count(); ++i )
       
   385                     {
       
   386                     if( &(aPolicyArray[i].Target()) == target )
       
   387                         {
       
   388                         if( aPolicyArray[i].Name() == XnPropertyNames::style::common::KDisplay )
       
   389                             {
       
   390                             if( aPolicyArray[i].Value() ==
       
   391                                     XnPropertyNames::style::common::display::KBlock )
       
   392                                 {
       
   393                                 displayBlockSet = KAI2PolShow;
       
   394                                 }
       
   395                             else if( aPolicyArray[i].Value() ==
       
   396                                     XnPropertyNames::style::common::display::KNone )
       
   397                                 {
       
   398                                 displayBlockSet = KAI2PolHide;
       
   399                                 }
       
   400                             }
       
   401                         if( aPolicyArray[i].Name() == XnPropertyNames::style::common::KVisibility )
       
   402                             {
       
   403                             if( aPolicyArray[i].Value() ==
       
   404                                     XnPropertyNames::style::common::visibility::KVisible )
       
   405                                 {
       
   406                                 visibilityVisibleSet = KAI2PolShow;
       
   407                                 }
       
   408                             else if( aPolicyArray[i].Value() ==
       
   409                                     XnPropertyNames::style::common::visibility::KHidden )
       
   410                                 {
       
   411                                 visibilityVisibleSet = KAI2PolHide;
       
   412                                 }
       
   413                             }
       
   414                         }
       
   415                     }
       
   416 
       
   417                 HBufC* displayPropertyValue = NULL;
       
   418                 if( !displayBlockSet )
       
   419                     {
       
   420                     // Only check CSS if policy array did not set the value
       
   421                     displayPropertyValue = PropertyValueL(
       
   422                                             *target,
       
   423                                             XnPropertyNames::style::common::KDisplay );
       
   424                     CleanupStack::PushL( displayPropertyValue );
       
   425 
       
   426                     if( displayPropertyValue )
       
   427                         {
       
   428                         HBufC8* buf8 = NULL;
       
   429                         buf8 = AiUtility::CopyToBufferL( buf8, *displayPropertyValue );
       
   430                         if( buf8 )
       
   431                             {
       
   432                             if( *buf8 == XnPropertyNames::style::common::display::KBlock )
       
   433                                 {
       
   434                                 displayBlockSet = KAI2PolShow;
       
   435                                 }
       
   436                             else
       
   437                                 {
       
   438                                 displayBlockSet = KAI2PolHide;
       
   439                                 }
       
   440                             delete buf8;
       
   441                             }
       
   442                         }
       
   443                     CleanupStack::PopAndDestroy( displayPropertyValue );
       
   444                     }
       
   445 
       
   446                 if( !visibilityVisibleSet )
       
   447                     {
       
   448                     displayPropertyValue = PropertyValueL(
       
   449                                             *target,
       
   450                                             XnPropertyNames::style::common::KVisibility );
       
   451                     CleanupStack::PushL( displayPropertyValue );
       
   452 
       
   453                     if( displayPropertyValue )
       
   454                         {
       
   455                         // only if visiblity property is set we affect nodeVisible variable
       
   456                         HBufC8* buf8 = NULL;
       
   457                         buf8 = AiUtility::CopyToBufferL( buf8, *displayPropertyValue );
       
   458                         if( buf8 )
       
   459                             {
       
   460                             if( *buf8 == XnPropertyNames::style::common::visibility::KVisible )
       
   461                                 {
       
   462                                 visibilityVisibleSet = KAI2PolShow;
       
   463                                 }
       
   464                             else
       
   465                                 {
       
   466                                 visibilityVisibleSet = KAI2PolHide;
       
   467                                 }
       
   468                             delete buf8;
       
   469                             }
       
   470                         }
       
   471                     CleanupStack::PopAndDestroy( displayPropertyValue );
       
   472                     }
       
   473                 }
       
   474 
       
   475             // At least one value has to be 'not unset'
       
   476             // and neither can be 'hide'
       
   477             if( ( displayBlockSet || visibilityVisibleSet ) &&
       
   478                 ( ( displayBlockSet != KAI2PolHide ) &&
       
   479                   ( visibilityVisibleSet != KAI2PolHide ) ) )
       
   480                 {
       
   481                 nodeVisible = ETrue;
       
   482                 }
       
   483 
       
   484             if( nodeVisible )
       
   485                 {
       
   486                 ++nodesVisible;
       
   487                 if( !andOperation && !negation )
       
   488                     {
       
   489                     // 'Or' ends to first true
       
   490                     // 'Not and' ends to first true
       
   491                     return ETrue;
       
   492                     }
       
   493                 }
       
   494             else if( !nodeVisible && andOperation && !negation )
       
   495                 {
       
   496                 // 'And' ends to first false
       
   497                 return EFalse;
       
   498                 }
       
   499 
       
   500             parser.SkipSpace();
       
   501             }
       
   502         if( nodesVisible > 0 && !negation )
       
   503             {
       
   504             return ETrue;
       
   505             }
       
   506         if( nodesVisible == 0 && negation )
       
   507             {
       
   508             return ETrue;
       
   509             }
       
   510         }
       
   511     return EFalse;
       
   512     }
       
   513 
       
   514 // End of file.