metadataengine/client/src/mderelationcondition.cpp
changeset 0 c53acadfccc6
child 40 910a23996aa0
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2009 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 #include "mderelationcondition.h"
       
    19 #include "mderelationdef.h"
       
    20 #include "mdelogiccondition.h"
       
    21 #include "mdscommoninternal.h"
       
    22 #include "mdcserializationbuffer.h"
       
    23 #include "mdcquery.h"
       
    24 
       
    25 CMdERelationCondition* CMdERelationCondition::NewL( TItemId aRelationId,
       
    26 		const RArray<TItemId>* aRelationIds, TRelationConditionSide aSide,
       
    27 		const CMdERelationDef* aRelationDef, 
       
    28 		const TMdEIntRange* aParameterRange )
       
    29 	{
       
    30 	CMdERelationCondition* self = CMdERelationCondition::NewLC(
       
    31 			aRelationId, aRelationIds, aSide, aRelationDef, aParameterRange );
       
    32     CleanupStack::Pop( self );
       
    33     return self;
       
    34 	}
       
    35 
       
    36 CMdERelationCondition* CMdERelationCondition::NewLC( TItemId aRelationId,
       
    37 		const RArray<TItemId>* aRelationIds, TRelationConditionSide aSide,
       
    38 		const CMdERelationDef* aRelationDef, 
       
    39 		const TMdEIntRange* aParameterRange )
       
    40 	{
       
    41 	CMdERelationCondition* self = new ( ELeave ) CMdERelationCondition( 
       
    42 			aRelationId, aSide, aRelationDef );
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL( aRelationIds, aParameterRange );
       
    45     return self;
       
    46 	}
       
    47 
       
    48 CMdERelationCondition::CMdERelationCondition( TItemId aRelationId, 
       
    49 		TRelationConditionSide aSide, const CMdERelationDef* aRelationDef )
       
    50 		: CMdECondition( EConditionTypeRelation ), iRelationId( aRelationId ),
       
    51 		  iRelationDef( aRelationDef ), iSide( aSide )
       
    52 	{
       
    53 	iRelationIds = NULL;
       
    54 	iParameterRange = NULL;
       
    55 	iRightCondition = NULL;
       
    56 	iLeftCondition = NULL;
       
    57 	}
       
    58 
       
    59 void CMdERelationCondition::ConstructL(const RArray<TItemId>* aRelationIds, 
       
    60 		const TMdEIntRange* aParameterRange)
       
    61     {
       
    62     // Base class construction.
       
    63     ConditionConstruct();
       
    64 
       
    65     if( aRelationIds )
       
    66     	{
       
    67     	iRelationIds = new (ELeave) RArray<TItemId>();
       
    68     	
       
    69     	const TInt relationIdCount = aRelationIds->Count();
       
    70     	iRelationIds->ReserveL( relationIdCount );
       
    71     	
       
    72     	for( TInt i = 0; i < relationIdCount; i++ )
       
    73     		{
       
    74     		iRelationIds->AppendL( (*aRelationIds)[i] );
       
    75     		}
       
    76     	}
       
    77 
       
    78     if( aParameterRange )
       
    79         {
       
    80         // Make a copy of the parameter range.
       
    81         iParameterRange = new (ELeave) TMdEIntRange(*aParameterRange);
       
    82         }
       
    83     }
       
    84 
       
    85 CMdERelationCondition::~CMdERelationCondition()
       
    86     {
       
    87    	delete iLeftCondition;
       
    88    	delete iRightCondition;
       
    89    	delete iParameterRange;
       
    90 	delete iLastModifiedDateRange;
       
    91 
       
    92 	if( iRelationIds )
       
    93 		{
       
    94 		iRelationIds->Close();
       
    95 		delete iRelationIds;
       
    96 		}
       
    97     }
       
    98 
       
    99 EXPORT_C TItemId CMdERelationCondition::RelationId() const
       
   100     {   
       
   101     return iRelationId;
       
   102     }
       
   103 
       
   104 EXPORT_C const RArray<TItemId>* CMdERelationCondition::RelationIds() const
       
   105 	{   
       
   106 	return iRelationIds;
       
   107 	}
       
   108 
       
   109 EXPORT_C const CMdERelationDef* CMdERelationCondition::RelationDef() const
       
   110     {   
       
   111     return iRelationDef;
       
   112     }
       
   113 
       
   114 EXPORT_C CMdELogicCondition& CMdERelationCondition::LeftL()
       
   115     {
       
   116     if ( !iLeftCondition )
       
   117     	{
       
   118     	iLeftCondition = CMdELogicCondition::NewL(ELogicConditionOperatorAnd);
       
   119     	}
       
   120     
       
   121     return *iLeftCondition;
       
   122     }
       
   123 
       
   124 EXPORT_C CMdELogicCondition& CMdERelationCondition::RightL()
       
   125     {
       
   126     if ( !iRightCondition )
       
   127     	{
       
   128     	iRightCondition = CMdELogicCondition::NewL(ELogicConditionOperatorAnd);
       
   129     	}
       
   130 
       
   131     return *iRightCondition;
       
   132     }
       
   133 
       
   134 TRelationConditionSide CMdERelationCondition::Side() const
       
   135 	{
       
   136 	return iSide;
       
   137 	}
       
   138 
       
   139 EXPORT_C void CMdERelationCondition::SetSide(TRelationConditionSide aSide)
       
   140     {
       
   141     iSide = aSide;
       
   142     }
       
   143 
       
   144 
       
   145 EXPORT_C const TMdEIntRange* CMdERelationCondition::ParameterRange() const
       
   146     {
       
   147     return iParameterRange;
       
   148     }
       
   149 
       
   150 EXPORT_C void CMdERelationCondition::SetGuid(const TInt64& aGuidHigh, const TInt64& aGuidLow)
       
   151 	{
       
   152 	iGuidHigh = aGuidHigh;
       
   153 	iGuidLow = aGuidLow;
       
   154 	}
       
   155 
       
   156 EXPORT_C TBool CMdERelationCondition::Guid(TInt64& aGuidHigh, TInt64& aGuidLow) const
       
   157 	{
       
   158 	if( iGuidHigh == 0 && iGuidLow == 0 )
       
   159 		{
       
   160 		return EFalse;
       
   161 		}
       
   162 	
       
   163 	aGuidHigh = iGuidHigh;
       
   164 	aGuidLow = iGuidLow;
       
   165 	return ETrue;
       
   166 	}
       
   167 
       
   168 EXPORT_C void CMdERelationCondition::SetLastModifiedDateRangeL(const TMdETimeRange& aLastModifiedDateRange)
       
   169 	{
       
   170 	if( !iLastModifiedDateRange )
       
   171 		{
       
   172 		iLastModifiedDateRange = new (ELeave) TMdETimeRange( aLastModifiedDateRange );
       
   173 		}
       
   174 	}
       
   175 
       
   176 EXPORT_C TBool CMdERelationCondition::LastModifiedDateRange(TMdETimeRange& aLastModifiedDateRange) const
       
   177 	{
       
   178 	if( iLastModifiedDateRange )
       
   179 		{		
       
   180 		aLastModifiedDateRange.SetType( iLastModifiedDateRange->Type() );
       
   181 		aLastModifiedDateRange.SetMin( iLastModifiedDateRange->Min() );
       
   182 		aLastModifiedDateRange.SetMax( iLastModifiedDateRange->Max() );
       
   183 		return ETrue;
       
   184 		}
       
   185 	
       
   186 	return EFalse;
       
   187 	}
       
   188 
       
   189 TUint32 CMdERelationCondition::InternalQueryOptimizationFlags(TUint32& aFlags)
       
   190 	{
       
   191 	aFlags |= EContainsRelationCondition;
       
   192 	return EContainsRelationCondition;
       
   193 	}
       
   194 
       
   195 TUint32 CMdERelationCondition::RequiredBufferSize() const
       
   196 	{
       
   197 	TUint32 bufferSize = sizeof( TMdCRelationCondition );
       
   198 
       
   199 	if( iRelationIds )
       
   200 		{
       
   201 		// count and IDs
       
   202 		bufferSize += CMdCSerializationBuffer::KRequiredSizeForTUint32 + 
       
   203 			iRelationIds->Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
   204 		}	
       
   205 
       
   206 	if( iGuidHigh != 0 || iGuidLow != 0 )
       
   207 		{
       
   208 		bufferSize += 2 * CMdCSerializationBuffer::KRequiredSizeForTInt64;
       
   209 		}
       
   210 
       
   211 	if ( iParameterRange )
       
   212 		{
       
   213 		bufferSize += iParameterRange->RequiredBufferSize();
       
   214 		}
       
   215 
       
   216 	if ( iLeftCondition && iLeftCondition->Count() > 0 )
       
   217 		{
       
   218 		bufferSize += iLeftCondition->RequiredBufferSize();
       
   219 		}
       
   220 
       
   221 	if ( iRightCondition && iRightCondition->Count() > 0  )
       
   222 		{
       
   223 		bufferSize += iRightCondition->RequiredBufferSize();
       
   224 		}
       
   225 
       
   226 	if( iLastModifiedDateRange )
       
   227 		{
       
   228 		bufferSize += iLastModifiedDateRange->RequiredBufferSize();
       
   229 		}
       
   230 
       
   231 	return bufferSize;
       
   232 	}
       
   233 
       
   234 void CMdERelationCondition::SerializeL(CMdCSerializationBuffer& aBuffer,
       
   235 		TMdCOffset& aFreespaceOffset) const
       
   236 	{
       
   237 	TMdCRelationCondition condition;
       
   238 
       
   239 	condition.iConditionType = iType;
       
   240 	condition.iNegated = iNegated;
       
   241 	condition.iObjectSide = iSide;
       
   242 
       
   243 	if( iRelationDef )
       
   244 		{
       
   245 		condition.iRelationDefId = iRelationDef->Id();
       
   246 		}
       
   247 	else
       
   248 		{
       
   249 		condition.iRelationDefId = KNoDefId;
       
   250 		}
       
   251 
       
   252 	condition.iRelationId = iRelationId;
       
   253 
       
   254 	// updated later, if relation IDs exists
       
   255 	condition.iRelationIds.iPtr.iCount = 0;
       
   256 	condition.iRelationIds.iPtr.iOffset = KNoOffset;
       
   257 	// updated later, if GUID exists
       
   258 	condition.iGuid = KNoOffset;
       
   259 	// updated later, if parameter range exists
       
   260 	condition.iParameterRange = KNoOffset;
       
   261 	// updated later, if left object condition exists
       
   262 	condition.iLeftObjectCondition = KNoOffset;
       
   263 	// updated later, if right object condition exists
       
   264 	condition.iRightObjectCondition = KNoOffset;
       
   265 	// updated later, if last modified date range exists
       
   266 	condition.iLastModifiedDateRange = KNoOffset;
       
   267 
       
   268 	const TUint32 conditionOffset = aBuffer.Position();
       
   269 
       
   270 	// get position after condition
       
   271 	aFreespaceOffset = conditionOffset + sizeof( TMdCRelationCondition );
       
   272 	aBuffer.PositionL( aFreespaceOffset );
       
   273 
       
   274 	if( iRelationIds )
       
   275 		{
       
   276 		condition.iRelationIds.iPtr.iCount = iRelationIds->Count();
       
   277 		condition.iRelationIds.iPtr.iOffset = aFreespaceOffset;
       
   278 		
       
   279 		const TInt relationIdCount = iRelationIds->Count();
       
   280 		for( TInt i = 0; i < relationIdCount; i++ )
       
   281 			{
       
   282 			const TItemId relationId = (*iRelationIds)[i];
       
   283 			aBuffer.InsertL( relationId );
       
   284 			}
       
   285 		
       
   286 		aFreespaceOffset = aBuffer.Position();
       
   287 		}
       
   288 	
       
   289 	if( iGuidHigh != 0 || iGuidLow != 0 )
       
   290 		{
       
   291 		condition.iGuid = aFreespaceOffset;
       
   292 
       
   293 		aBuffer.InsertL( iGuidHigh );
       
   294 		aBuffer.InsertL( iGuidLow );
       
   295 		
       
   296 		aFreespaceOffset = aBuffer.Position();
       
   297 		}
       
   298 
       
   299 	if ( iParameterRange )
       
   300 		{
       
   301 		condition.iParameterRange = aFreespaceOffset;
       
   302 
       
   303 		iParameterRange->SerializeL( aBuffer );
       
   304 
       
   305 		aFreespaceOffset = aBuffer.Position();
       
   306 		}
       
   307 
       
   308 	if ( iLeftCondition && iLeftCondition->Count() > 0  )
       
   309 		{
       
   310 		condition.iLeftObjectCondition = aFreespaceOffset;
       
   311 
       
   312 		iLeftCondition->SerializeL( aBuffer, aFreespaceOffset );
       
   313 
       
   314 		aFreespaceOffset = aBuffer.Position();
       
   315 		}
       
   316 
       
   317 	if ( iRightCondition && iRightCondition->Count() > 0  )
       
   318 		{
       
   319 		condition.iRightObjectCondition = aFreespaceOffset;
       
   320 
       
   321 		iRightCondition->SerializeL( aBuffer, aFreespaceOffset );
       
   322 
       
   323 		aFreespaceOffset = aBuffer.Position();
       
   324 		}
       
   325 
       
   326 	if ( iLastModifiedDateRange )
       
   327 		{
       
   328 		condition.iLastModifiedDateRange = aFreespaceOffset;
       
   329 
       
   330 		iLastModifiedDateRange->SerializeL( aBuffer );
       
   331 
       
   332 		aFreespaceOffset = aBuffer.Position();
       
   333 		}
       
   334 
       
   335 	aBuffer.PositionL( conditionOffset );
       
   336 	
       
   337 	condition.SerializeL( aBuffer );
       
   338 	
       
   339 	// move back to after sub condition
       
   340 	aBuffer.PositionL( aFreespaceOffset );
       
   341 	}