policymanagement/policyengine/policyengineserver/src/ElementBase.cpp
changeset 0 b497e44ab2fc
child 73 ae69c2e8bc34
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2000 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: Implementation of policymanagement components
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 
       
    21 #include "ElementBase.h"
       
    22 #include "PolicyStorage.h"
       
    23 #include "PolicyParser.h"
       
    24 #include "XACMLconstants.h"
       
    25 #include "elements.h"
       
    26 #include "ErrorCodes.h"
       
    27 #include "debug.h"
       
    28 #include "PolicyEngineServer.h"
       
    29 
       
    30 #include <e32std.h>
       
    31 
       
    32 // CONSTANTS
       
    33 // MACROS
       
    34 // DATA TYPES
       
    35 // FUNCTION PROTOTYPES
       
    36 // FORWARD DECLARATIONS
       
    37 // CLASS DECLARATION
       
    38 
       
    39 // ----------------------------------------------------------------------------------------
       
    40 // ElementHelper namespace contains utility functions for element handling
       
    41 // ----------------------------------------------------------------------------------------
       
    42 
       
    43 namespace ElementHelper
       
    44 {
       
    45 	// ----------------------------------------------------------------------------------------
       
    46 	// ElementHelper::DecodeEmptyElement 
       
    47 	// ----------------------------------------------------------------------------------------
       
    48 
       
    49 	HBufC8 * DecodeEmptyElement( const TDesC8& aElementName)
       
    50 	{
       
    51 		//needed extra space in description
       
    52 		const TInt KExtraSpace = 4;
       
    53 		
       
    54 		//create cbuf (return value)
       
    55 		HBufC8 * hbuf = HBufC8::NewL( aElementName.Length() + KExtraSpace);
       
    56 		TPtr8 ptr = hbuf->Des();
       
    57 		
       
    58 		//append empty element format
       
    59 		ptr.Append('<');
       
    60 		ptr.Append( aElementName);
       
    61 		ptr.Append(_L(" />"));
       
    62 
       
    63 		return hbuf;		
       
    64 	}
       
    65 
       
    66 	// ----------------------------------------------------------------------------------------
       
    67 	// ElementHelper::CreateNewElementL Create new element, element type is identified by aElementId
       
    68 	// ----------------------------------------------------------------------------------------
       
    69 
       
    70 	CElementBase * CreateNewElementL( const TUint32& aElementId)
       
    71 	{
       
    72 		//decode element type
       
    73 		TInt type = aElementId & 0xff;
       
    74 		CElementBase * element = 0;
       
    75 	
       
    76 		//create empty element
       
    77 		switch ( type)
       
    78 		{
       
    79 			case EPolicySet:
       
    80 				element = CPolicySet::NewL();
       
    81 				break;
       
    82 			case EPolicy:
       
    83 				element = CPolicy::NewL();
       
    84 				break;
       
    85 			case ETarget:
       
    86 				element = CTarget::NewL();
       
    87 				break;
       
    88 			case ERule:	
       
    89 				element = CRule::NewL();
       
    90 				break;
       
    91 			case ESubjects:
       
    92 				element = CSubjects::NewL();
       
    93 				break;
       
    94 			case ESubject:
       
    95 				element = CSubject::NewL();
       
    96 				break;
       
    97 			case ESubjectAttributeDesignator:
       
    98 				element = CSubjectAttributeDesignator::NewL();
       
    99 				break;
       
   100 			case ESubjectMatch:
       
   101 				element = CMatchObject::NewL( ESubjectMatch);
       
   102 			break;
       
   103 			case EActions:
       
   104 				element = CActions::NewL();
       
   105 				break;
       
   106 			case EAction:
       
   107 				element = CAction::NewL();
       
   108 				break;
       
   109 			case EActionAttributeDesignator:
       
   110 				element = CActionAttributeDesignator::NewL();
       
   111 				break;
       
   112 			case EActionMatch:
       
   113 				element = CMatchObject::NewL( EActionMatch);
       
   114 			break;
       
   115 			case EResources:
       
   116 				element = CResources::NewL();
       
   117 				break;
       
   118 			case EResource:
       
   119 				element = CResource::NewL();
       
   120 				break;
       
   121 			case EResourceAttributeDesignator:
       
   122 				element = CResourceAttributeDesignator::NewL();
       
   123 				break;
       
   124 			case EResourceMatch:
       
   125 				element = CMatchObject::NewL( EResourceMatch);
       
   126 			break;
       
   127 			case EEnvironments:
       
   128 				element = CEnvironment::NewL();
       
   129 				break;
       
   130 			case EEnvironment:
       
   131 				element = CEnvironment::NewL();
       
   132 				break;
       
   133 			case EEnvironmentAttributeDesignator:
       
   134 				element = CEnvironmentAttributeDesignator::NewL();
       
   135 				break;
       
   136 			case EEnvironmentMatch:
       
   137 				element = CMatchObject::NewL( EEnvironmentMatch);
       
   138 			break;
       
   139 			case EDescription:
       
   140 				element = CDescription::NewL();
       
   141 			break;
       
   142 			case ECondition:
       
   143 				element = CCondition::NewL();
       
   144 				break;
       
   145 			case EApply:
       
   146 				element = CApply::NewL();
       
   147 				break;
       
   148 			default:
       
   149 				User::Panic(PolicyExecutionPanic, KErrCorrupt);	
       
   150 			break;
       
   151 		}
       
   152 	
       
   153 		//set id and storage reference
       
   154 		element->SetId( aElementId);
       
   155 		
       
   156 		return element;
       
   157 	}
       
   158 	
       
   159 	// ----------------------------------------------------------------------------------------
       
   160 	// ElementHelper::CompareElements Compare function for RArray 
       
   161 	// ----------------------------------------------------------------------------------------
       
   162 
       
   163 	TInt CompareElements( CElementBase* const& aElement1, CElementBase* const& aElement2)
       
   164 	{
       
   165 		if ( aElement1->GetId() == aElement2->GetId()) return 0;
       
   166 		return ( aElement1->GetId() < aElement2->GetId() ? -1 : 1 );
       
   167 	}
       
   168 }
       
   169 
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CElementBase::CElementBase()
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 
       
   176 CElementBase::CElementBase()
       
   177 	: iElementState( ENewElement), iSaveType( EOwnElement), 
       
   178 	  iCreateExternalId( EFalse), iExternalId( NULL), iParentId( 0), iElementId( 0), 
       
   179 	  iReferenceCount( KNotInElementCache)
       
   180 {
       
   181 }
       
   182 
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CElementBase::CElementBase()
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 
       
   189 CElementBase::CElementBase( const TUint32& aElementId )
       
   190 	: iElementState( ENotLoaded), iSaveType( EOwnElement), iCreateExternalId( EFalse), iExternalId( NULL), iParentId( 0), 
       
   191 	  iElementId( aElementId), iReferenceCount( KNotInElementCache)
       
   192 {
       
   193 }
       
   194 
       
   195 
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CElementBase::~CElementBase()
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 
       
   202 CElementBase::~CElementBase()
       
   203 {
       
   204 	//delete all containers in the element list
       
   205 	for (TInt i(0); i < iElements.Count(); i++)
       
   206 	{
       
   207 		TElementContainer * container = iElements[i];
       
   208 		
       
   209 		//delete element only when it isn't in policy storage
       
   210 		if ( container->iDeleteWithElement)
       
   211 		{
       
   212 			delete container->iElement;
       
   213 		}
       
   214 		
       
   215 		//allways delete container
       
   216 		delete container;
       
   217 	}
       
   218 
       
   219 	iElements.Close();
       
   220 }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CElementBase::ElementType()
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 
       
   227 TNativeElementTypes CElementBase::ElementType()
       
   228 {
       
   229 	return iElementType;
       
   230 }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CElementBase::ElementType()
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 TNativeElementTypes CElementBase::SubElementType( const TInt& aIndex, const TInt& aExpectedCount)
       
   237 {
       
   238 	//check that aIndex is valid
       
   239 	if ( aIndex >= iElements.Count())
       
   240 	{
       
   241 		return (TNativeElementTypes)KErrNotFound;
       
   242 	}
       
   243 	
       
   244 	//Check also expected count if parameter is given (positive)
       
   245 	if ( aExpectedCount >= 0 && aExpectedCount != iElements.Count())
       
   246 	{
       
   247 		return (TNativeElementTypes)KErrNotFound;
       
   248 	}
       
   249 	
       
   250 	//return type of element
       
   251 	return iElements[ aIndex]->iElement->ElementType();
       
   252 }
       
   253 
       
   254 
       
   255 // -----------------------------------------------------------------------------
       
   256 // CElementBase::SetId()
       
   257 // -----------------------------------------------------------------------------
       
   258 //
       
   259 
       
   260 void CElementBase::SetId( const TUint32& aElementId)
       
   261 {
       
   262 	iElementId = aElementId;	
       
   263 }
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CElementBase::CreateId()
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 
       
   270 void CElementBase::CreateIdL()
       
   271 {
       
   272 	//if element doesn't have id, get new id from policy storage
       
   273 	if ( iElementId == 0)
       
   274 	{
       
   275 		iElementId = CPolicyStorage::PolicyStorage()->CreateIdL( iElementType);
       
   276 	}
       
   277 	
       
   278 	//create ids also for child elements
       
   279 /*	for ( TInt i(0); i < iElements.Count(); i++)
       
   280 	{
       
   281 		TElementContainer * container = iElements[i];
       
   282 		container->iElement->CreateIds( aPolicyStorage);
       
   283 	}*/
       
   284 }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CElementBase::GetId()
       
   288 // -----------------------------------------------------------------------------
       
   289 //
       
   290 		
       
   291 TUint32 CElementBase::GetId() const 
       
   292 {
       
   293 	return iElementId;
       
   294 }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CElementBase::AddParentIdL()
       
   298 // -----------------------------------------------------------------------------
       
   299 //
       
   300 
       
   301 void CElementBase::AddParentIdL( TUint32 iMotherId)
       
   302 {
       
   303 	//parent id is saved only for rules, policysets and policies
       
   304 	if ( iElementType == ERule || iElementType == EPolicySet || iElementType == EPolicy )
       
   305 	{
       
   306 /*		//create new parant element list if list doesn't exist
       
   307 		if ( !iParentElements )
       
   308 		{
       
   309 			iParentElements	= new (ELeave) RIdList;
       
   310 		}
       
   311 	
       
   312 		//add parent to list
       
   313 		iParentElements->AppendL( iMotherId);
       
   314 */
       
   315 		iParentId = iMotherId;
       
   316 	}
       
   317 }
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 // CElementBase::CheckNewParentId()
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 /*
       
   324 TBool CElementBase::CheckNewParentId( TUint32 iMotherId)
       
   325 {
       
   326 	if (!iParentElements)
       
   327 	{
       
   328 		return ETrue;
       
   329 	}
       
   330 	
       
   331 	for ( TInt i = 0; i < iParentElements->Count(); i++)
       
   332 	{
       
   333 		if ( (*iParentElements)[i] == iMotherId)
       
   334 		{
       
   335 			return EFalse;
       
   336 		}
       
   337 	}
       
   338 	
       
   339 	return ETrue;	
       
   340 }*/
       
   341 
       
   342 // -----------------------------------------------------------------------------
       
   343 // CElementBase::DeleteElementL()
       
   344 // -----------------------------------------------------------------------------
       
   345 //
       
   346 
       
   347 void CElementBase::DeleteElementL()
       
   348 {
       
   349 	__ASSERT_ALWAYS ( iElementId || iSaveType == ESubElement, User::Panic(PolicyStoragePanic, KErrGeneral));
       
   350 	
       
   351 	//if element is subelement which is saved part of parent element
       
   352 	if ( iSaveType == ESubElement) return;
       
   353 	
       
   354 	//remove element
       
   355 	CPolicyStorage::PolicyStorage()->DeleteElementL( iElementId);	
       
   356 }
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // CElementBase::DeleteMarkRecursiveL()
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 void CElementBase::DeleteMarkRecursiveL()
       
   363 {
       
   364 	iElementState = EDeletedEditableElement;
       
   365 
       
   366 	for ( TInt i(0); i < iElements.Count(); i++)
       
   367 	{
       
   368 		//get element from list, add parent and save it
       
   369 		CElementBase * element = iElements[i]->iElement;
       
   370 		
       
   371 		//check and load element (to editable cache)
       
   372 		TElementReserver reserver( element);
       
   373 		CPolicyStorage::PolicyStorage()->CheckElementL( element);	
       
   374 		
       
   375 		element->DeleteMarkRecursiveL();
       
   376 		
       
   377 		reserver.Release();
       
   378 	}
       
   379 }
       
   380 
       
   381 
       
   382 
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 // CElementBase::RemoveChild()
       
   386 // -----------------------------------------------------------------------------
       
   387 //
       
   388 
       
   389 TInt CElementBase::RemoveChild( const TUint32& aChildId)
       
   390 {
       
   391 	TInt err = KErrNotFound;
       
   392 
       
   393 	//delete also child elements
       
   394 	for ( TInt i(0); i < iElements.Count(); i++)
       
   395 	{
       
   396 		//get element from list, add parent and save it
       
   397 		TElementContainer * container = iElements[i];
       
   398 		__ASSERT_ALWAYS ( container, User::Panic(PolicyStoragePanic, KErrGeneral));
       
   399 		
       
   400 		if ( container->iElement->iElementId == aChildId)
       
   401 		{
       
   402 			//delete container and remove it from child list
       
   403 			delete iElements[i];
       
   404 			iElements.Remove( i);
       
   405 			err = KErrNone;
       
   406 			break;
       
   407 		}
       
   408 	}		
       
   409 	
       
   410 	return err;
       
   411 }
       
   412 
       
   413 
       
   414 // -----------------------------------------------------------------------------
       
   415 // CElementBase::CreateExternalId()
       
   416 // -----------------------------------------------------------------------------
       
   417 //
       
   418 void CElementBase::CreateExternalId()
       
   419 {
       
   420 	iCreateExternalId = ETrue;
       
   421 }
       
   422 
       
   423 
       
   424 // -----------------------------------------------------------------------------
       
   425 // CElementBase::SaveElementL()
       
   426 // -----------------------------------------------------------------------------
       
   427 //
       
   428 
       
   429 TInt CElementBase::SaveElementL( TBool aRecursive)
       
   430 {
       
   431 	
       
   432 
       
   433 	//check id
       
   434 	if ( (iElementId == 0 && iSaveType == EOwnElement )|| iCreateExternalId)
       
   435 	{
       
   436 		if ( iElementId == 0)
       
   437 		{
       
   438 			iElementId = CPolicyStorage::PolicyStorage()->CreateIdL( iElementType);
       
   439 		}
       
   440 	}
       
   441 	
       
   442 	//if aResursive, make recursive save also for childs
       
   443 	if ( aRecursive )
       
   444 	{
       
   445 		for ( TInt i(0); i < iElements.Count(); i++)
       
   446 		{
       
   447 			//get element from list, add parent and save it
       
   448 			TElementContainer * container = iElements[i];
       
   449 			if ( iSaveType == EOwnElement)
       
   450 			{
       
   451 				container->iElement->AddParentIdL( GetId());
       
   452 			}
       
   453 			container->iElement->SaveElementL( ETrue);
       
   454 		}
       
   455 	}
       
   456 
       
   457 	TInt err(KErrNone);
       
   458 
       
   459 	//Save element, if element is subelement it is already saved by parent element
       
   460 	if ( iSaveType == EOwnElement)
       
   461 	{
       
   462 		//decode element, use id mode where only child id is saved to description
       
   463 		HBufC8 * description = DecodeElementL( ENative, EIdMode);
       
   464 		CleanupStack::PushL( description);
       
   465 	
       
   466 		//id parent element is defined
       
   467 		if ( iParentId )
       
   468 		{
       
   469 			//create buffer for parent mapping
       
   470 			HBufC8 * temp = HBufC8::NewL( description->Length() + (KIdLength + 1));		
       
   471 			TPtr8 ptr = temp->Des();
       
   472 		
       
   473 			//add original description
       
   474 			ptr.Append( *description);
       
   475 			
       
   476 			//and parent mapping
       
   477 			ptr.Append( KParentListStartMark);
       
   478 			ptr.AppendNum( iParentId);
       
   479 		
       
   480 			//delete old description and push old in cleanup stack
       
   481 			CleanupStack::PopAndDestroy( description);			//old description
       
   482 			CleanupStack::PushL( temp);
       
   483 			description = temp;
       
   484 		}
       
   485 
       
   486 		//write policy to database
       
   487 		CPolicyStorage::PolicyStorage()->SaveElementL( iElementId, description);
       
   488 
       
   489 		CleanupStack::PopAndDestroy( description);
       
   490 	}
       
   491 
       
   492 	return err;
       
   493 }
       
   494 	
       
   495 // -----------------------------------------------------------------------------
       
   496 // CElementBase::SelectCorrectValue()
       
   497 // -----------------------------------------------------------------------------
       
   498 //
       
   499 
       
   500 const TDesC8& CElementBase::SelectCorrectValue( const TLanguageSelector &aLanguage, const TDesC8& aOriginalValue ) const
       
   501 {
       
   502 	if ( aLanguage == ENative )
       
   503 	{
       
   504 		return aOriginalValue;
       
   505 	}
       
   506 
       
   507 	return CPolicyParser::ConvertValues( ENative, aOriginalValue);
       
   508 }
       
   509 	
       
   510 	
       
   511 // -----------------------------------------------------------------------------
       
   512 // CElementBase::DecodeElementOrIdL()
       
   513 // -----------------------------------------------------------------------------
       
   514 //
       
   515 		
       
   516 HBufC8 * CElementBase::DecodeElementOrIdL( const TLanguageSelector &aLanguage, CElementBase * aElement, const TDecodeMode &aMode)
       
   517 {		
       
   518 	HBufC8 * buffer = NULL;
       
   519 	
       
   520 	//if full mode, decode full element. In id mode only id list is decoded
       
   521 	if ( aMode == EFullMode)
       
   522 	{
       
   523 		buffer = aElement->DecodeElementL( aLanguage, EFullMode);
       
   524 	}
       
   525 	else 
       
   526 	{
       
   527 		if ( aElement->iSaveType == ESubElement )
       
   528 		{
       
   529 			buffer = aElement->DecodeElementL( aLanguage, aMode);
       
   530 		}
       
   531 		else
       
   532 		{
       
   533 			buffer = aElement->DecodeIdL();
       
   534 		}
       
   535 	}
       
   536 		
       
   537 	
       
   538 	return buffer;
       
   539 }
       
   540 
       
   541 // -----------------------------------------------------------------------------
       
   542 // CElementBase::DecodeIdL()
       
   543 // -----------------------------------------------------------------------------
       
   544 //
       
   545 
       
   546 HBufC8 * CElementBase::DecodeIdL()
       
   547 {
       
   548 	//decoding format
       
   549 	_LIT8( DecodeFormat,"|%d");
       
   550 	
       
   551 	//create buffer for id 
       
   552 	const TInt KIdPtrLength = 10;
       
   553 	HBufC8 * hbuf = HBufC8::NewLC(KIdPtrLength);
       
   554 	
       
   555 	//format it
       
   556 	hbuf->Des().Format( DecodeFormat, iElementId);
       
   557 	
       
   558 	CleanupStack::Pop();
       
   559 	return hbuf;
       
   560 }
       
   561 
       
   562 // -----------------------------------------------------------------------------
       
   563 // CElementBase::AddToElementListL()
       
   564 // -----------------------------------------------------------------------------
       
   565 //
       
   566 
       
   567 void CElementBase::AddToElementListL( CElementBase * aElement, TBool aDeleteWithElement)
       
   568 {	
       
   569 	//create new container for element
       
   570 	TElementContainer * container = new (ELeave) TElementContainer;
       
   571 
       
   572 		
       
   573 	//fill container and apppend it to list
       
   574 	container->iElement = aElement;
       
   575 	container->iDeleteWithElement = aDeleteWithElement;
       
   576 	
       
   577 	iElements.AppendL( container);
       
   578 }
       
   579 
       
   580 // -----------------------------------------------------------------------------
       
   581 // CElementBase::DescriptionL()
       
   582 // -----------------------------------------------------------------------------
       
   583 //
       
   584 
       
   585 HBufC8 * CElementBase::DescriptionL()
       
   586 {
       
   587 	//try find description element from element list
       
   588 	for (TInt i(0); i < iElements.Count(); i++)
       
   589 	{
       
   590 		TElementContainer * container = iElements[i];
       
   591 		//reserve element from storage
       
   592 		TElementReserver elementReserver( container->iElement);
       
   593 		
       
   594 		if ( container->iElement->iElementType == EDescription )
       
   595 		{		
       
   596 			//check element and read description
       
   597 			CPolicyStorage::PolicyStorage()->CheckElementL( container->iElement);
       
   598 			CDescription * description = (CDescription*) container->iElement;
       
   599 			elementReserver.Release();
       
   600 			return  description->DescriptionText();
       
   601 		}
       
   602 		
       
   603 		elementReserver.Release();
       
   604 	}
       
   605 	
       
   606 	return NULL;
       
   607 }
       
   608 
       
   609 // -----------------------------------------------------------------------------
       
   610 // CElementBase::GetChildListLengthL()
       
   611 // -----------------------------------------------------------------------------
       
   612 //
       
   613 
       
   614 TInt CElementBase::GetChildListLengthL()
       
   615 {
       
   616 	TInt size = 0;
       
   617 
       
   618 	//policies, rules, policysets are elements which are shown external child list
       
   619 	for (TInt i(0); i < iElements.Count(); i++)
       
   620 	{
       
   621 		CElementBase * element = iElements[i]->iElement;
       
   622 		TElementReserver elementReserver( element);
       
   623 		
       
   624 		if ( element->iElementType == ERule || element->iElementType == EPolicy ||
       
   625 			 element->iElementType == EPolicySet )
       
   626 		{
       
   627 			//check element and read external id length
       
   628 			CPolicyStorage::PolicyStorage()->CheckElementL( element);
       
   629 			size += element->ExternalId()->Length() + 1;
       
   630 		}
       
   631 		
       
   632 		elementReserver.Release();
       
   633 	}
       
   634 	
       
   635 	return size;
       
   636 }
       
   637 
       
   638 // -----------------------------------------------------------------------------
       
   639 // CElementBase::GetChildListL()
       
   640 // -----------------------------------------------------------------------------
       
   641 //
       
   642 
       
   643 void CElementBase::GetChildListL( TDes8& aChilds)
       
   644 {
       
   645 	//policies, rules, policysets are elements which are shown external child list
       
   646 	for (TInt i(0); i < iElements.Count(); i++)
       
   647 	{
       
   648 		CElementBase * element = iElements[i]->iElement;
       
   649 		TElementReserver elementReserver( element);
       
   650 		
       
   651 		//add child ids to one list one after one
       
   652 		if ( element->iElementType == ERule || element->iElementType == EPolicy ||
       
   653 			 element->iElementType == EPolicySet )
       
   654 		{
       
   655 			CPolicyStorage::PolicyStorage()->CheckElementL( element);
       
   656 			aChilds.Append( *(element->ExternalId()));
       
   657 			aChilds.Append( KMessageDelimiterChar);
       
   658 		}
       
   659 		
       
   660 		elementReserver.Release();
       
   661 	}
       
   662 }
       
   663 
       
   664 // -----------------------------------------------------------------------------
       
   665 // CElementBase::GetChildElementCountL()
       
   666 // -----------------------------------------------------------------------------
       
   667 //
       
   668 
       
   669 TInt CElementBase::GetChildElementCountL()
       
   670 {
       
   671 	TInt count(0);
       
   672 	
       
   673 	//policies, rules, policysets are elements which are shown external child list
       
   674 	for (TInt i(0); i < iElements.Count(); i++)
       
   675 	{
       
   676 		CElementBase * element = iElements[i]->iElement;
       
   677 		TElementReserver elementReserver( element);
       
   678 		
       
   679 		if ( element->iElementType == ERule || element->iElementType == EPolicy ||
       
   680 			 element->iElementType == EPolicySet )
       
   681 		{
       
   682 			count++;
       
   683 		}
       
   684 	}
       
   685 	
       
   686 	return count;
       
   687 }
       
   688 
       
   689 
       
   690 // -----------------------------------------------------------------------------
       
   691 // CElementBase::CheckAllElementsL()
       
   692 // -----------------------------------------------------------------------------
       
   693 //
       
   694 	
       
   695 void CElementBase::CheckAllElementsL()
       
   696 {
       
   697 	//Make element check for all elements in contaneiner list
       
   698 	for (TInt i(0); i < iElements.Count(); i++)
       
   699 	{
       
   700 		CPolicyStorage::PolicyStorage()->CheckElementL( iElements[i]->iElement);
       
   701 	}
       
   702 }
       
   703 
       
   704 // -----------------------------------------------------------------------------
       
   705 // CElementBase::CheckAllElementsL()
       
   706 // -----------------------------------------------------------------------------
       
   707 //
       
   708 
       
   709 TInt CElementBase::ElementCount()
       
   710 {
       
   711 	return iElements.Count();	
       
   712 }
       
   713 
       
   714 // -----------------------------------------------------------------------------
       
   715 // CElementBase::CheckAllElementsL()
       
   716 // -----------------------------------------------------------------------------
       
   717 //
       
   718 
       
   719 CElementBase * CElementBase::Element( TInt aIndex)
       
   720 {
       
   721 	return iElements[ aIndex]->iElement;
       
   722 }
       
   723 
       
   724 // -----------------------------------------------------------------------------
       
   725 // CElementBase::ReleaseElement()
       
   726 // -----------------------------------------------------------------------------
       
   727 //
       
   728 
       
   729 void CElementBase::ReleaseElement()
       
   730 {
       
   731 	//this operation is only for cache elements
       
   732 	if ( iReferenceCount == KNotInElementCache) return;
       
   733 	
       
   734 	iReferenceCount--;
       
   735 	
       
   736 //	RDEBUG_3(_L("Policy Engine: Decrease element %d reference count (%d)"), iElementId, iReferenceCount);
       
   737 
       
   738 	//reference count must be equal or greater than zero
       
   739 	__ASSERT_ALWAYS ( iReferenceCount >= 0, User::Panic(PolicyStoragePanic, KErrGeneral));
       
   740 	
       
   741 }
       
   742 
       
   743 // -----------------------------------------------------------------------------
       
   744 // CElementBase::ReserveElement()
       
   745 // -----------------------------------------------------------------------------
       
   746 //
       
   747 
       
   748 
       
   749 void CElementBase::ReserveElement()
       
   750 {
       
   751 	//this operation is only for cache elements
       
   752 	if ( iReferenceCount == KNotInElementCache) return;
       
   753 
       
   754 	iReferenceCount++;
       
   755 	
       
   756 //	RDEBUG_3(_L("Policy Engine: Increase element %d reference count (%d)"), iElementId, iReferenceCount);	
       
   757 }
       
   758 
       
   759 // -----------------------------------------------------------------------------
       
   760 // CElementBase::Container()
       
   761 // -----------------------------------------------------------------------------
       
   762 //
       
   763 
       
   764 
       
   765 RElementContainer* CElementBase::Container()
       
   766 {
       
   767 	return &iElements;
       
   768 }
       
   769 
       
   770 // -----------------------------------------------------------------------------
       
   771 // CElementBase::IdentificateElement()
       
   772 // -----------------------------------------------------------------------------
       
   773 //
       
   774 
       
   775 		
       
   776 TBool CElementBase::IdentificateElement( const TDesC8& aElementName )
       
   777 {
       
   778 	return (iElementName == aElementName);
       
   779 }
       
   780 
       
   781 // -----------------------------------------------------------------------------
       
   782 // CElementBase::AbsoluteTarget()
       
   783 // -----------------------------------------------------------------------------
       
   784 //
       
   785 /*
       
   786 
       
   787 void CElementBase::AbsoluteTargetL( TUnionTarget * target)
       
   788 {
       
   789 	//assert allways when element is not rule, policy or policyset
       
   790 	__ASSERT_ALWAYS ( iElementType == ERule || iElementType == EPolicySet || iElementType == EPolicy, User::Panic(PolicyStoragePanic, KErrGeneral));
       
   791 
       
   792 	//add target definitions to one target union from parent list, resolve absolute target for them
       
   793 	if ( iParentElements)
       
   794 	{
       
   795 		for ( TInt i(0); i < iParentElements->Count(); i++ )
       
   796 		{
       
   797 			//get element from storage and check it
       
   798 			CElementBase * element = iPolicyStorage->GetElementL( (*iParentElements)[i]);
       
   799 			TElementReserver elementReserver( element);
       
   800 		
       
   801 			//no need for type check, bacause all parent elements are correct type
       
   802 			iPolicyStorage->CheckElementL( element);
       
   803 			element->AbsoluteTargetL( target);
       
   804 			
       
   805 			elementReserver.Release();
       
   806 		}
       
   807 	}
       
   808 	
       
   809 	//add element target to targer union
       
   810 	for (TInt i(0); i < iElements.Count(); i++)
       
   811 	{
       
   812 		CElementBase * element = iElements[i]->iElement;
       
   813 		TElementReserver elementReserver( element);
       
   814 			
       
   815 		if ( element->iElementType == ETarget )
       
   816 		{
       
   817 			//add target definitions to one target union
       
   818 			iPolicyStorage->CheckElementL( element);
       
   819 			target->AddTargetElementsL( element);	
       
   820 		}
       
   821 
       
   822 		elementReserver.Release();
       
   823 	}
       
   824 	
       
   825 }
       
   826 */
       
   827 // -----------------------------------------------------------------------------
       
   828 // CElementBase::AddAttributeL()
       
   829 // -----------------------------------------------------------------------------
       
   830 //
       
   831 
       
   832 			
       
   833 void CElementBase::AddAttributeL( CPolicyParser *aParser, const TDesC8& aName, const TDesC8& /*aValue*/)
       
   834 {
       
   835 	//unexpected attribute
       
   836 	aParser->HandleErrorL( ParserErrors::UnexpectedAttribute, aName);
       
   837 }
       
   838 
       
   839 // -----------------------------------------------------------------------------
       
   840 // CElementBase::AddContentL()
       
   841 // -----------------------------------------------------------------------------
       
   842 //
       
   843 
       
   844 
       
   845 void CElementBase::AddContentL( CPolicyParser *aParser, const TDesC8& aName)
       
   846 {
       
   847 	//unexpected content
       
   848 	aParser->HandleErrorL( ParserErrors::UnexpectedContent, aName);
       
   849 }
       
   850 
       
   851 // -----------------------------------------------------------------------------
       
   852 // CElementBase::AddElementL()
       
   853 // -----------------------------------------------------------------------------
       
   854 //
       
   855 
       
   856 void CElementBase::AddElementL( CPolicyParser *aParser, CElementBase * /*aElement*/)
       
   857 {
       
   858 	//unexpected element
       
   859 	aParser->HandleErrorL( ParserErrors::UnexpectedElement, aParser->ActiveElementName());
       
   860 }
       
   861 
       
   862 // -----------------------------------------------------------------------------
       
   863 // CElementBase::Match()
       
   864 // -----------------------------------------------------------------------------
       
   865 //
       
   866 
       
   867 
       
   868 TMatchResponse CElementBase::MatchL( CPolicyProcessor* /*aPolicyProcessor*/)
       
   869 {
       
   870  	User::Panic(PolicyExecutionPanic, KErrCorrupt);
       
   871  	TMatchResponse response = EIndeterminate;
       
   872  	return response;
       
   873 }
       
   874 
       
   875 
       
   876 // -----------------------------------------------------------------------------
       
   877 // CElementBase::ApplyValueL()
       
   878 // -----------------------------------------------------------------------------
       
   879 //
       
   880 
       
   881 CAttributeValue* CElementBase::ApplyValueL( CPolicyProcessor* /*aPolicyProcessor*/)
       
   882 {
       
   883  	User::Panic(PolicyExecutionPanic, KErrCorrupt);
       
   884  	return NULL;
       
   885 }
       
   886 
       
   887 // -----------------------------------------------------------------------------
       
   888 // CElementBase::DecodeElementL()
       
   889 // -----------------------------------------------------------------------------
       
   890 //
       
   891 
       
   892 HBufC8 * CElementBase::DecodeElementL( const TLanguageSelector &/*aLanguage*/, const TDecodeMode &/*aMode*/  )
       
   893 {
       
   894 	User::Panic(PolicyExecutionPanic, KErrCorrupt);
       
   895 	return NULL;
       
   896 }
       
   897 
       
   898 // -----------------------------------------------------------------------------
       
   899 // CElementBase::AddIdElementL()
       
   900 // -----------------------------------------------------------------------------
       
   901 //
       
   902 
       
   903 void CElementBase::AddIdElementL( CElementBase * /*aElement*/)
       
   904 {
       
   905 	User::Panic(PolicyExecutionPanic, KErrCorrupt);
       
   906 }
       
   907 
       
   908 // -----------------------------------------------------------------------------
       
   909 // CElementBase::ValidElement()
       
   910 // -----------------------------------------------------------------------------
       
   911 //
       
   912 
       
   913 TBool CElementBase::ValidElement()
       
   914 {
       
   915 	User::Panic(PolicyExecutionPanic, KErrCorrupt);
       
   916 	return ETrue;
       
   917 }
       
   918 
       
   919 // -----------------------------------------------------------------------------
       
   920 // CElementBase::CheckAllElementsL()
       
   921 // -----------------------------------------------------------------------------
       
   922 //
       
   923 
       
   924 HBufC8 * CElementBase::ExternalId() const 
       
   925 {
       
   926 	return iExternalId;
       
   927 }
       
   928 
       
   929 
       
   930 // -----------------------------------------------------------------------------
       
   931 // CElementBase::GetElementsListLengthL()
       
   932 // -----------------------------------------------------------------------------
       
   933 //
       
   934 
       
   935 TInt CElementBase::GetElementsListLengthL( TElementType aType )
       
   936 {
       
   937 	TInt size = 0;
       
   938 
       
   939 	if ( iExternalId &&
       
   940 	   ((iElementType == ERule && aType == ERules ) ||
       
   941 	    (iElementType == EPolicy && aType == EPolicies ) ||
       
   942 	    (iElementType == EPolicySet && aType == EPolicySets )))
       
   943 	{
       
   944 		size = iExternalId->Length() + 1;
       
   945 	}
       
   946 
       
   947 	//policies, rules, policysets are elements which are shown external child list
       
   948 	for (TInt i(0); i < iElements.Count(); i++)
       
   949 	{
       
   950 		CElementBase * element = iElements[i]->iElement;
       
   951 		TElementReserver elementReserver( element);
       
   952 		
       
   953 		if ( element->iElementType == ERule || 
       
   954 			 element->iElementType == EPolicy ||
       
   955 			 element->iElementType == EPolicySet )
       
   956 		{
       
   957 			//check element and read external id length
       
   958 			CPolicyStorage::PolicyStorage()->CheckElementL( element);
       
   959 			size += element->GetElementsListLengthL( aType);
       
   960 		}
       
   961 		
       
   962 		elementReserver.Release();
       
   963 	}
       
   964 	
       
   965 	return size;
       
   966 }
       
   967 
       
   968 // -----------------------------------------------------------------------------
       
   969 // CElementBase::GetElementsListL()
       
   970 // -----------------------------------------------------------------------------
       
   971 //
       
   972 
       
   973 void CElementBase::GetElementsListL( TElementType aType, TDes8& aChilds)
       
   974 {
       
   975 	if ( iExternalId &&
       
   976 	   ((iElementType == ERule && aType == ERules ) ||
       
   977 	    (iElementType == EPolicy && aType == EPolicies ) ||
       
   978 	    (iElementType == EPolicySet && aType == EPolicySets )))
       
   979 	{
       
   980 		aChilds.Append( *iExternalId);
       
   981 		aChilds.Append( KMessageDelimiterChar);
       
   982 	}
       
   983 
       
   984 	//policies, rules, policysets are elements which are shown external child list
       
   985 	for (TInt i(0); i < iElements.Count(); i++)
       
   986 	{
       
   987 		CElementBase * element = iElements[i]->iElement;
       
   988 		TElementReserver elementReserver( element);
       
   989 		
       
   990 		//add child ids to one list one after one
       
   991 		if ( element->iElementType == ERule || 
       
   992 			 element->iElementType == EPolicy ||
       
   993 			 element->iElementType == EPolicySet )
       
   994 		{
       
   995 			CPolicyStorage::PolicyStorage()->CheckElementL( element);
       
   996 			element->GetElementsListL( aType, aChilds);
       
   997 		}
       
   998 		
       
   999 		elementReserver.Release();
       
  1000 	}
       
  1001 }
       
  1002 
       
  1003 // -----------------------------------------------------------------------------
       
  1004 // CElementBase::FindAttributesL()
       
  1005 // -----------------------------------------------------------------------------
       
  1006 //
       
  1007 void CElementBase::FindAttributesL( TNativeElementTypes aAttributeType, RElementArray& aValuesList)
       
  1008 {
       
  1009 	if ( iElementType == aAttributeType)
       
  1010 	{
       
  1011 		aValuesList.AppendL( this);
       
  1012 	}
       
  1013 	else
       
  1014 	{
       
  1015 		for ( TInt i = 0; i < iElements.Count(); i++)
       
  1016 		{
       
  1017 			CElementBase * element = iElements[i]->iElement;
       
  1018 			TNativeElementTypes type = element->ElementType();
       
  1019 	
       
  1020 			if ( type == ERule ||
       
  1021 				 type == ETarget ||
       
  1022 				 type == EPolicy ||
       
  1023 				 type == EPolicySet ||
       
  1024 				 type == ESubject ||
       
  1025 				 type == ESubjects ||
       
  1026 				 type == EAction ||
       
  1027 				 type == EActions ||
       
  1028 				 type == EResource ||
       
  1029 				 type == EResources ||
       
  1030 				 type == EEnvironment ||
       
  1031 				 type == EEnvironment ||
       
  1032 				 type == ESubjectMatch ||
       
  1033 				 type == EActionMatch ||
       
  1034 				 type == EResourceMatch ||
       
  1035 				 type == EEnvironmentMatch )
       
  1036 			{
       
  1037 				TElementReserver reserve( element);
       
  1038 				CPolicyStorage::PolicyStorage()->CheckElementL( element);		
       
  1039 				
       
  1040 				element->FindAttributesL( aAttributeType, aValuesList);
       
  1041 				
       
  1042 				reserve.Release();
       
  1043 			}
       
  1044 		}	
       
  1045 	}
       
  1046 }