metadataengine/client/src/mdesessionimpl.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Metadata engine client session implementation*
       
    15 */
       
    16 
       
    17 #include <etel3rdparty.h>
       
    18 #include <e32property.h> 
       
    19 
       
    20 #include "mdesessionimpl.h"
       
    21 #include "mdesessionstartupao.h"
       
    22 
       
    23 #include "mdcdef.h"
       
    24 #include "mdcitem.h"
       
    25 #include "mdcresult.h"
       
    26 #include "mdeobjectdef.h"
       
    27 #include "mderelationdef.h"
       
    28 #include "mdeeventdef.h"
       
    29 #include "mdeobject.h"
       
    30 #include "mderelation.h"
       
    31 #include "mdeevent.h"
       
    32 #include "mdepanic.h"
       
    33 #include "mdequeryimpl.h"
       
    34 #include "mdenotifierao.h"
       
    35 #include "mdeobjectdef.h"
       
    36 #include "mdenamespacedef.h"
       
    37 #include "mdccommon.pan"
       
    38 #include "mdedatabuffer.h"
       
    39 #include "mdcserializationbuffer.h"
       
    40 #include "mdequerycriteriaserialization.h"
       
    41 #include "mdelogiccondition.h"
       
    42 #include "mdeobjectcondition.h"
       
    43 #include "mdscommoninternal.h"
       
    44 
       
    45 RMdESessionAsyncRequest::RMdESessionAsyncRequest( TRequestType aRequestType, 
       
    46 	CMdCSerializationBuffer* aBuffer, CMdCSerializationBuffer& aResultBuffer,
       
    47 	TRequestStatus& aRequestStatus) : 
       
    48 	iRequestType(aRequestType), iBuffer(aBuffer), iResultBuffer(&aResultBuffer), 
       
    49 	iRequestStatus(&aRequestStatus)
       
    50 	{
       
    51 	*iRequestStatus = KRequestPending;
       
    52 	}
       
    53 
       
    54 void RMdESessionAsyncRequest::Close()
       
    55 	{
       
    56 	if (iBuffer)
       
    57 		{
       
    58 		delete iBuffer;
       
    59 		iBuffer = NULL;
       
    60 		}
       
    61 	}
       
    62 
       
    63 void CMdESessionAsyncHandler::AddRequest( CMdCSerializationBuffer* aBuffer,
       
    64 			CMdCSerializationBuffer& aResultBuffer, 
       
    65 			TRequestStatus& aRequestStatus )
       
    66 	{
       
    67 	RMdESessionAsyncRequest request = RMdESessionAsyncRequest( 
       
    68 			RMdESessionAsyncRequest::EAddRequest, 
       
    69 			aBuffer, aResultBuffer, aRequestStatus );
       
    70 
       
    71 	iRequests.Append(request);
       
    72 
       
    73 	if( !IsActive() )
       
    74 		{
       
    75 		iEngineSession.DoAddItemsAsync( *aBuffer, aResultBuffer, 
       
    76 				iStatus );
       
    77 		SetActive();
       
    78 		}
       
    79 	}
       
    80 
       
    81 void CMdESessionAsyncHandler::UpdateRequest( CMdCSerializationBuffer * aBuffer,
       
    82 			CMdCSerializationBuffer& aResultBuffer,
       
    83 			TRequestStatus& aRequestStatus )
       
    84 	{
       
    85 	RMdESessionAsyncRequest request = RMdESessionAsyncRequest( 
       
    86 			RMdESessionAsyncRequest::EUpdateRequest,
       
    87 			aBuffer, aResultBuffer, aRequestStatus);
       
    88 
       
    89 	iRequests.Append(request);
       
    90 
       
    91 	if( !IsActive() )
       
    92 		{
       
    93 		iEngineSession.DoUpdateItemsAsync(*aBuffer, aResultBuffer, 
       
    94 				iStatus);
       
    95 		SetActive();
       
    96 		}
       
    97 	}
       
    98 
       
    99 void CMdESessionAsyncHandler::RemoveRequest( CMdCSerializationBuffer* aBuffer,
       
   100 		CMdCSerializationBuffer& aResultBuffer,
       
   101 		TRequestStatus& aRequestStatus )
       
   102 	{
       
   103 	RMdESessionAsyncRequest request = RMdESessionAsyncRequest( 
       
   104 			RMdESessionAsyncRequest::ERemoveRequest, 
       
   105 			aBuffer, aResultBuffer, aRequestStatus);
       
   106 
       
   107 	iRequests.Append(request);
       
   108 
       
   109 	if( !IsActive() )
       
   110 		{
       
   111 		iEngineSession.DoRemoveItemsAsync( *aBuffer, aResultBuffer, 
       
   112 				iStatus );	
       
   113 		SetActive();
       
   114 		}
       
   115 	}
       
   116 
       
   117 CMdESessionAsyncHandler* CMdESessionAsyncHandler::NewL(CMdESessionImpl& aSession, 
       
   118 		RMdEEngineSession &aEngineSession)
       
   119 	{
       
   120 	CMdESessionAsyncHandler* self = CMdESessionAsyncHandler::NewLC(
       
   121 			aSession, aEngineSession);
       
   122 	CleanupStack::Pop(self);
       
   123 	return self;
       
   124 	}
       
   125 
       
   126 CMdESessionAsyncHandler* CMdESessionAsyncHandler::NewLC( CMdESessionImpl& aSession, 
       
   127 		RMdEEngineSession &aEngineSession )
       
   128 	{
       
   129 	CMdESessionAsyncHandler *self = new (ELeave) CMdESessionAsyncHandler(
       
   130 			aSession, aEngineSession);
       
   131 	CleanupStack::PushL(self);
       
   132 	self->ConstructL();
       
   133 	return self;
       
   134 	}
       
   135 
       
   136 CMdESessionAsyncHandler::~CMdESessionAsyncHandler()
       
   137 	{
       
   138 	Cancel();
       
   139 
       
   140 	const TInt count = iRequests.Count();
       
   141 
       
   142 	for (TInt i = 0; i < count; i++)
       
   143 		{
       
   144 		iRequests[i].Close();
       
   145 		}
       
   146 	iRequests.Close();		
       
   147 	}		
       
   148 
       
   149 void CMdESessionAsyncHandler::RunL()
       
   150 	{
       
   151 	//Remove first from array	
       
   152 	RMdESessionAsyncRequest& request = iRequests[0];
       
   153 	iRequests.Remove(0);
       
   154 
       
   155 	const TInt status = iStatus.Int();
       
   156 
       
   157 	User::RequestComplete( request.iRequestStatus, status );
       
   158 	request.Close();
       
   159 	
       
   160 	if (iRequests.Count() > 0)
       
   161 		{
       
   162 		request = iRequests[0];
       
   163 
       
   164 		iStatus = KRequestPending;
       
   165 		
       
   166 		switch( request.iRequestType )
       
   167 			{
       
   168 			case RMdESessionAsyncRequest::EAddRequest:
       
   169 				iEngineSession.DoAddItemsAsync( *(request.iBuffer),
       
   170 					*(request.iResultBuffer), iStatus);
       
   171 				break;
       
   172 			case RMdESessionAsyncRequest::EUpdateRequest:
       
   173 				iEngineSession.DoUpdateItemsAsync( *(request.iBuffer),
       
   174 					*(request.iResultBuffer), iStatus);
       
   175 				break;
       
   176 			case RMdESessionAsyncRequest::ERemoveRequest:
       
   177 				iEngineSession.DoRemoveItemsAsync( *(request.iBuffer),
       
   178 					*(request.iResultBuffer), iStatus);
       
   179 				break;
       
   180 			default:
       
   181 			    break;
       
   182 			}
       
   183 		SetActive();			
       
   184 		}						
       
   185 	}
       
   186 
       
   187 TInt CMdESessionAsyncHandler::RunError(TInt aError)
       
   188 	{
       
   189 	if( aError == KErrServerTerminated )
       
   190 		{
       
   191 		iSession.NotifyError( aError );
       
   192 		return KErrNone;
       
   193 		}
       
   194 	else
       
   195 		{
       
   196 		return aError;
       
   197 		}
       
   198 	}
       
   199 
       
   200 void CMdESessionAsyncHandler::DoCancel()
       
   201 	{
       
   202 	}
       
   203 
       
   204 CMdESessionAsyncHandler::CMdESessionAsyncHandler(CMdESessionImpl& aSession, 
       
   205 		RMdEEngineSession &aEngineSession)
       
   206 	: CActive( CActive::EPriorityStandard ), iSession( aSession ), 
       
   207 	iEngineSession(aEngineSession)
       
   208 	{
       
   209 	}
       
   210 
       
   211 void CMdESessionAsyncHandler::ConstructL()		
       
   212 	{
       
   213 	CActiveScheduler::Add(this);
       
   214 	}
       
   215 
       
   216 CMdESessionImpl::CMdESessionImpl(MMdESessionObserver& aObserver)
       
   217 	: iSessionStartupAO( NULL ), iSessionObserver(&aObserver), 
       
   218 	  iSchemaBuffer( NULL ), iAsyncHandler(NULL), iNextQueryId( 0 ),
       
   219 	  iSession( *this )
       
   220 	{
       
   221 	}
       
   222 
       
   223 CMdESessionImpl::~CMdESessionImpl()
       
   224 	{
       
   225 	Close();
       
   226 
       
   227 	delete iSchemaBuffer;
       
   228 
       
   229 	iNotifiers.ResetAndDestroy();
       
   230 	iNotifiers.Close();
       
   231 
       
   232 	iSession.Shutdown();
       
   233 	iSession.Close();
       
   234 	
       
   235 	iSchemaChunk.Close();
       
   236 	
       
   237 	iNamespaceDefs.ResetAndDestroy();
       
   238 	iNamespaceDefs.Close();
       
   239 	
       
   240 	delete iSessionStartupAO;
       
   241 		
       
   242 	delete iAsyncHandler;
       
   243 	}
       
   244 
       
   245 void CMdESessionImpl::ConstructL()
       
   246 	{
       
   247     iSessionStartupAO = CMdESessionStartupAO::NewL( *this, iSession );
       
   248     iAsyncHandler = CMdESessionAsyncHandler::NewL( *this, iSession );    
       
   249     }
       
   250 
       
   251 void CMdESessionImpl::Close()
       
   252 	{
       
   253 	iSchemaObserverArray.Reset();
       
   254 	iSchemaObserverArray.Close();
       
   255 	}
       
   256 
       
   257 TInt CMdESessionImpl::NamespaceDefCount() const
       
   258 	{
       
   259 	return iNamespaceDefs.Count();
       
   260 	}
       
   261 
       
   262 CMdENamespaceDef& CMdESessionImpl::NamespaceDefL( TInt aIndex )
       
   263 	{
       
   264 	return *iNamespaceDefs[aIndex];
       
   265 	}
       
   266 
       
   267 CMdENamespaceDef& CMdESessionImpl::GetNamespaceDefL( const TDesC& aName )
       
   268 	{
       
   269 	const TInt KNamespaceCount = iNamespaceDefs.Count();
       
   270 	for ( TInt i = 0; i < KNamespaceCount; ++i )
       
   271 		{
       
   272 		if ( !aName.Compare( iNamespaceDefs[i]->Name() ) )
       
   273 			{
       
   274 			return NamespaceDefL( i );
       
   275 			}
       
   276 		}
       
   277 
       
   278 	User::Leave( KErrNotFound );
       
   279 	return NamespaceDefL( -1 ); // never reached
       
   280 	}
       
   281 
       
   282 CMdENamespaceDef& CMdESessionImpl::GetNamespaceDefL(TDefId aId)
       
   283 	{
       
   284 	const TInt KNamespaceCount = iNamespaceDefs.Count();
       
   285 	for ( TInt i = 0; i < KNamespaceCount; ++i )
       
   286 		{
       
   287 		if ( iNamespaceDefs[i]->Id() == aId )
       
   288 			{
       
   289 			return NamespaceDefL( i );
       
   290 			}
       
   291 		}
       
   292 
       
   293 	User::Leave( KErrNotFound );
       
   294 	return NamespaceDefL( -1 ); // never reached
       
   295 	}
       
   296 
       
   297 CMdENamespaceDef& CMdESessionImpl::GetDefaultNamespaceDefL()
       
   298 	{
       
   299 	return GetNamespaceDefL( KDefaultNamespaceDefId );
       
   300 	}
       
   301 
       
   302 CMdEObject* CMdESessionImpl::NewObjectL( CMdEObjectDef& aDef, const TDesC& aUri, TUint32 aMediaId )
       
   303 	{
       
   304 	CMdEObject* object = NewObjectLC( aDef, aUri, aMediaId );
       
   305 	CleanupStack::Pop(object);
       
   306 	return object;
       
   307 	}
       
   308 
       
   309 CMdEObject* CMdESessionImpl::NewObjectLC( CMdEObjectDef& aDef, const TDesC& aUri, TUint32 aMediaId )
       
   310 	{
       
   311 	CMdEObject* object = CMdEObject::NewLC( aDef, aUri, aMediaId );
       
   312 	return object;
       
   313 	}
       
   314 
       
   315 void CMdESessionImpl::CommitObjectL(CMdEObject& aObject)
       
   316     {
       
   317 	// check state
       
   318 	// check that object is open for modifications
       
   319 	if (!aObject.OpenForModifications())
       
   320 		{
       
   321 		User::Leave( KErrMdENotLocked );
       
   322 		}
       
   323 
       
   324 	RPointerArray<CMdEInstanceItem> items;
       
   325 	CleanupClosePushL( items );
       
   326 	items.AppendL( &aObject );
       
   327 	UpdateItemsL( items );
       
   328 	CleanupStack::PopAndDestroy( &items );	
       
   329     }
       
   330 
       
   331 void CMdESessionImpl::CommitObjectsL(RPointerArray<CMdEObject>& aObjects)
       
   332     {
       
   333 	// check state
       
   334 	// check that object is open for modifications
       
   335 
       
   336     RPointerArray<CMdEInstanceItem> items;
       
   337     CleanupClosePushL( items );
       
   338 
       
   339     const TInt objectsCount = aObjects.Count();
       
   340     items.ReserveL( objectsCount );    
       
   341     for (TInt i = 0; i < objectsCount; ++i)
       
   342     	{
       
   343     	CMdEObject* obj = aObjects[i];
       
   344     	if ( !obj->OpenForModifications() )
       
   345     		{
       
   346     		User::Leave( KErrMdENotLocked );
       
   347     		}
       
   348 		items.Append( obj );
       
   349     	}
       
   350 
       
   351     UpdateItemsL(items);
       
   352 
       
   353     items.Reset();
       
   354     CleanupStack::PopAndDestroy( &items );
       
   355     }
       
   356 
       
   357 TItemId CMdESessionImpl::CancelObjectL(CMdEObject& aObject)
       
   358     {
       
   359     // check that object is open for modifications
       
   360     if( !aObject.OpenForModifications() || !aObject.BelongsToSession() )
       
   361     	{
       
   362     	User::Leave( KErrMdENotLocked );
       
   363     	}
       
   364     
       
   365 	CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( 
       
   366 			sizeof(TMdCItemIds) + 
       
   367 			CMdCSerializationBuffer::KRequiredSizeForTItemId );
       
   368 	
       
   369 	TMdCItemIds itemIds;
       
   370 	itemIds.iNamespaceDefId = aObject.Def().NamespaceDef().Id();
       
   371 	itemIds.iObjectIds.iPtr.iCount = 1;
       
   372 	itemIds.iObjectIds.iPtr.iOffset = sizeof(TMdCItemIds);
       
   373 	itemIds.SerializeL( *buffer );
       
   374 
       
   375 	buffer->InsertL( aObject.Id() );
       
   376 
       
   377     iSession.DoCancelObjectL( *buffer );
       
   378 
       
   379     TItemId result;
       
   380     buffer->PositionL( KNoOffset );
       
   381     itemIds.DeserializeL( *buffer );
       
   382 	buffer->PositionL( itemIds.iObjectIds.iPtr.iOffset );
       
   383 	buffer->ReceiveL( result );
       
   384 
       
   385 	CleanupStack::PopAndDestroy( buffer ); // buffer
       
   386 
       
   387 	if (result == KNoId)
       
   388 		{
       
   389 		User::Leave( KErrNotFound );
       
   390 		}
       
   391 
       
   392 	aObject.SetNotOpenForModifications();
       
   393 	return result;
       
   394     }
       
   395 
       
   396 CMdERelation* CMdESessionImpl::NewRelationLC( CMdERelationDef& aDef, TItemId aLeftObjectId, 
       
   397 										      TItemId aRightObjectId, TInt32 aParameter )
       
   398 	{
       
   399 	return CMdERelation::NewLC(aDef, aLeftObjectId, aRightObjectId, aParameter);
       
   400 	}
       
   401 
       
   402 CMdERelation* CMdESessionImpl::NewRelationL( CMdERelationDef& aDef, TItemId aLeftObjectId, 
       
   403 										     TItemId aRightObjectId, TInt32 aParameter )
       
   404 	{
       
   405 	CMdERelation* rel = NewRelationLC( aDef, aLeftObjectId, aRightObjectId, aParameter );
       
   406 	CleanupStack::Pop( rel );
       
   407 	return rel;
       
   408 	}
       
   409 
       
   410 
       
   411 CMdEEvent* CMdESessionImpl::NewEventLC(CMdEEventDef& aDef, TItemId aObjectId, TTime aTime, const TDesC* aSource, const TDesC* aParticipant)
       
   412 	{
       
   413 	return CMdEEvent::NewLC(aDef, aObjectId, aTime, aSource, aParticipant );
       
   414 	}
       
   415 
       
   416 CMdEEvent* CMdESessionImpl::NewEventL(CMdEEventDef& aDef, TItemId aObjectId, TTime aTime, const TDesC* aSource, const TDesC* aParticipant)
       
   417 	{
       
   418 	CMdEEvent* event = NewEventLC( aDef, aObjectId, aTime, aSource, aParticipant );
       
   419 	CleanupStack::Pop( event );
       
   420 	return event;
       
   421 	}
       
   422 	
       
   423 void CMdESessionImpl::AddSchemaObserverL(MMdESchemaObserver& aObserver)
       
   424 	{
       
   425 	CheckOpened();
       
   426 
       
   427 	CMdENamespaceDef& defaultNamespaceDef = GetDefaultNamespaceDefL();
       
   428 
       
   429 	TInt err = FindNotifier( ESchemaModify, &aObserver, defaultNamespaceDef );
       
   430 	
       
   431     if ( err != KErrNotFound )
       
   432     	{
       
   433     	if ( err >= 0 )
       
   434     		{
       
   435     		return;
       
   436     		}
       
   437     	User::LeaveIfError( err );
       
   438     	}
       
   439     
       
   440     CMdENotifierAO* notifier = CMdENotifierAO::NewLC( *this, iSession );
       
   441     notifier->RegisterL( ESchemaModify, &aObserver, NULL, defaultNamespaceDef );
       
   442     User::LeaveIfError(iNotifiers.Append( notifier ));
       
   443     CleanupStack::Pop( notifier );
       
   444 	}
       
   445 
       
   446 
       
   447 void CMdESessionImpl::RemoveSchemaObserverL(MMdESchemaObserver& aObserver)
       
   448 	{
       
   449 	CheckOpened();
       
   450 
       
   451 	CMdENamespaceDef& defaultNamespaceDef = GetDefaultNamespaceDefL();
       
   452 
       
   453     TInt index = FindNotifier( ESchemaModify, &aObserver, defaultNamespaceDef );
       
   454     if ( index != KErrNotFound )
       
   455         {
       
   456 	    iNotifiers[index]->Cancel();
       
   457 	    delete iNotifiers[index];
       
   458 	    iNotifiers[index] = NULL;
       
   459 	    iNotifiers.Remove( index );
       
   460         }
       
   461     else
       
   462     	{
       
   463        	User::Leave( KErrNotFound );
       
   464     	}
       
   465 	}
       
   466 
       
   467 void CMdESessionImpl::NotifySessionOpened(TInt aError)
       
   468 	{
       
   469 	__ASSERT_DEBUG(iSessionObserver != 0,
       
   470 				    TMdEPanic::Panic(TMdEPanic::EInternal));
       
   471 	if(!aError)
       
   472 		{
       
   473 		iSessionState = EMdESessionOpen;
       
   474 		}
       
   475 
       
   476     if( iSessionObserver )
       
   477         {
       
   478         iSessionObserver->HandleSessionOpened(*this, aError);
       
   479         }
       
   480 	}
       
   481 
       
   482 
       
   483 void CMdESessionImpl::NotifyError(TInt aError)
       
   484 	{
       
   485 	if(iSessionObserver)
       
   486 		{
       
   487 		iSessionObserver->HandleSessionError(*this, aError);
       
   488 		}
       
   489 	iSessionState = EMdESessionClosed;
       
   490 	}
       
   491 
       
   492 void CMdESessionImpl::LoadSchemaL()
       
   493 	{
       
   494 	DoLoadSchemaL();
       
   495 	}
       
   496 
       
   497 RMdEEngineSession& CMdESessionImpl::EngineSession()
       
   498 	{
       
   499 	return iSession;
       
   500 	}
       
   501 
       
   502 void CMdESessionImpl::DoLoadSchemaL()
       
   503     {
       
   504     TInt handle( 0 );
       
   505     iSession.DoLoadSchemaL( handle );
       
   506     
       
   507    	TBuf<32> name( KSchemaChunkName );
       
   508 	name.AppendNum( handle );
       
   509 	iSchemaChunk.Close();
       
   510 	User::LeaveIfError( iSchemaChunk.OpenGlobal( name, ETrue ) );
       
   511 
       
   512 	CMdCSerializationBuffer* schemaBuffer = 
       
   513 		CMdCSerializationBuffer::NewLC( iSchemaChunk.Base(), iSchemaChunk.Size() );
       
   514 
       
   515 	if ( schemaBuffer->Size() == 0 )
       
   516 		{
       
   517 		User::Leave( KErrNotFound );
       
   518 		}
       
   519 	else
       
   520 		{
       
   521 		CleanupStack::Pop( schemaBuffer );
       
   522 		}
       
   523 
       
   524 	delete iSchemaBuffer;
       
   525 
       
   526 	iSchemaBuffer = schemaBuffer;
       
   527 	
       
   528 	iNamespaceDefs.ResetAndDestroy();
       
   529 	
       
   530 	// initialize namespacedefs
       
   531 	const TMdCSchema& schema = TMdCSchema::GetFromBufferL(*iSchemaBuffer);
       
   532 	const TUint32 namespaceCount = schema.iNamespaceDefs.iPtr.iCount;
       
   533 	const TMdCOffset namespaceOffset = schema.iNamespaceDefs.iPtr.iOffset;
       
   534 
       
   535 	iNamespaceDefs.ReserveL( namespaceCount );	
       
   536 	for ( TUint32 i = 0; i < namespaceCount; ++i )
       
   537 		{
       
   538 		iSchemaBuffer->PositionL( namespaceOffset + i * sizeof(TMdCNamespaceDef) );
       
   539 		const TMdCNamespaceDef& namespaceDef = TMdCNamespaceDef::GetFromBufferL(*iSchemaBuffer);
       
   540 		iNamespaceDefs.AppendL( CMdENamespaceDef::NewL( *this, namespaceDef, *iSchemaBuffer ) );
       
   541 		}
       
   542     }
       
   543 
       
   544 void CMdESessionImpl::AddRelationDefL( const CMdENamespaceDef &aNamespaceDef, const TDesC &aName )
       
   545 	{
       
   546 	iSession.DoAddRelationDefL(aNamespaceDef.Id(), aName);
       
   547 	DoLoadSchemaL();
       
   548 	}
       
   549 
       
   550 void CMdESessionImpl::AddEventDefL( const CMdENamespaceDef &aNamespaceDef, const TDesC &aName )
       
   551 	{
       
   552 	iSession.DoAddEventDefL(aNamespaceDef.Id(), aName);
       
   553 	DoLoadSchemaL();
       
   554 	}
       
   555 
       
   556 /**
       
   557 * Get methods
       
   558 */
       
   559 CMdEObject* CMdESessionImpl::GetObjectL(CMdEObjectDef& aObjectDef, 
       
   560 	const TItemId aId, const TInt64 aGuidHigh, const TInt64 aGuidLow, const TDesC& aUri, 
       
   561 	TMdCQueryLockType aLocktype, TBool aIncludeFreetexts )
       
   562     {
       
   563     if(aUri == KNullDesC && aGuidHigh == 0 && aGuidLow == 0 && aId == KNoId)
       
   564     	{
       
   565     	User::Leave(KErrNotSupported);
       
   566     	}
       
   567         
       
   568     CMdENamespaceDef &namespacedef = aObjectDef.NamespaceDef();    
       
   569     
       
   570     CMdEObjectQuery* query = NewObjectQueryL(namespacedef,aObjectDef,NULL);
       
   571     CleanupStack::PushL(query);
       
   572     
       
   573     query->SetResultMode(EQueryResultModeItem);
       
   574         
       
   575     if(aId != KNoId)
       
   576     	{
       
   577     	CMdEObjectCondition& cond = query->Conditions().AddObjectConditionL( aId );
       
   578     	cond.SetConfidentialityLevel( EObjectConditionLevelIgnoreConfidentiality );
       
   579     	}
       
   580     else if(aGuidHigh != 0 && aGuidLow != 0)
       
   581     	{
       
   582     	CMdEObjectCondition& cond = query->Conditions().AddObjectConditionL( aGuidHigh, aGuidLow );
       
   583     	cond.SetConfidentialityLevel( EObjectConditionLevelIgnoreConfidentiality );
       
   584     	}
       
   585     else if(aUri != KNullDesC)
       
   586     	{
       
   587     	CMdEObjectCondition& cond = query->Conditions().AddObjectConditionL( EObjectConditionCompareUri, aUri );
       
   588     	cond.SetConfidentialityLevel( EObjectConditionLevelIgnoreConfidentiality );
       
   589     	}
       
   590     else
       
   591     	{
       
   592     	User::Leave( KErrArgument );
       
   593     	}
       
   594 	
       
   595 	TUint32 optimizationFlags = EContainsObjectCondition;
       
   596 	
       
   597 	if( aIncludeFreetexts )
       
   598 		{
       
   599 		optimizationFlags |= EContainsFreetextCondition;
       
   600 		}
       
   601 	
       
   602 	if( aLocktype == ELock )
       
   603 		{
       
   604 		optimizationFlags |= EContainsObjectLocking;
       
   605 		}
       
   606 	
       
   607 	CMdEQueryCriteriaSerialization* buf = CMdEQueryCriteriaSerialization::NewLC( 
       
   608 											   query->ResultMode(),
       
   609 											   query->Type(), 
       
   610     										   query->NamespaceDef(), 
       
   611     										   &aObjectDef, 
       
   612     										   NULL, 
       
   613     										   1, //Max 1  
       
   614     										   0, // 0 offset because it's not used now
       
   615     										   optimizationFlags , 
       
   616     										   query->Conditions(), 
       
   617     										   query->OrderRules(),
       
   618     										   NULL);    
       
   619 
       
   620 	CMdCSerializationBuffer* resbuf = iSession.DoFindSyncLC( 
       
   621 		query, 
       
   622 		*buf, aLocktype, 
       
   623 		KMdEQueryDefaultMaxCount );
       
   624 
       
   625 	RPointerArray<CMdEInstanceItem> items;
       
   626 	CleanupClosePushL( items );	
       
   627 
       
   628 	DeserializeQueryResultL( *resbuf, items );
       
   629 
       
   630 	CleanupStack::Pop( &items );
       
   631 
       
   632    	CleanupStack::PopAndDestroy( resbuf );
       
   633 	CleanupStack::PopAndDestroy( buf );
       
   634 	CleanupStack::PopAndDestroy( query );
       
   635 
       
   636 	const TInt itemsCount( items.Count() );
       
   637 	if( itemsCount== 1 )
       
   638 		{
       
   639 		CMdEInstanceItem* item = items[0];
       
   640 		
       
   641 #ifdef _DEBUG
       
   642 		if( !item || item->InstanceType() != EMdETypeObject )
       
   643 			{
       
   644 			User::Leave( KErrCorrupt );
       
   645 			}
       
   646 #endif
       
   647 		
       
   648 		items.Close();
       
   649 
       
   650 		return (CMdEObject*)item;
       
   651 		}
       
   652    	else if( itemsCount == 0 )
       
   653    		{
       
   654    		items.Close();
       
   655 
       
   656    		return NULL;
       
   657    		}
       
   658 
       
   659 #ifdef _DEBUG
       
   660    	else
       
   661    		{
       
   662    		items.ResetAndDestroy();
       
   663    		items.Close();
       
   664 
       
   665    		User::Leave( KErrCorrupt );
       
   666    		}
       
   667 #endif
       
   668 
       
   669 	return NULL; // <-- just to stop compile warnings!!
       
   670     }
       
   671 
       
   672 CMdEObject* CMdESessionImpl::GetObjectL( const TItemId aId, CMdENamespaceDef* aNamespaceDef )
       
   673 	{
       
   674 	CMdENamespaceDef* namespaceDef = GetNamespaceDefL( aNamespaceDef );
       
   675 	
       
   676 	CMdEObjectDef* objectDef = namespaceDef->GetObjectDefL( KBaseObjectDefId );
       
   677 	if ( !objectDef )
       
   678 		{
       
   679 		User::Leave( KErrNotFound );
       
   680 		}
       
   681 	return GetObjectL(*objectDef, aId, 0, 0, KNullDesC, EGet, EFalse);	
       
   682 	}
       
   683 	
       
   684 CMdEObject* CMdESessionImpl::GetFullObjectL( const TItemId aId, CMdENamespaceDef* aNamespaceDef )
       
   685 	{
       
   686 	TMdEObject object;
       
   687 
       
   688 	CheckObjectL( object, aId, aNamespaceDef );
       
   689 
       
   690 	if( object.NotPresent() || object.Removed() )
       
   691 		{
       
   692 		User::Leave( KErrNotFound );
       
   693 		}
       
   694 	
       
   695 	const CMdEObjectDef& objectDef = object.DefL();
       
   696 	
       
   697 	return GetObjectL( CONST_CAST( CMdEObjectDef&, objectDef ), aId, 0, 0, KNullDesC, EGet, ETrue );
       
   698 	}
       
   699 
       
   700 CMdEObject* CMdESessionImpl::GetObjectL( const TItemId aId, CMdEObjectDef& aObjectDef )
       
   701 	{
       
   702 	return GetObjectL(aObjectDef, aId, 0, 0, KNullDesC, EGet, EFalse);	
       
   703 	}
       
   704 
       
   705 CMdEObject* CMdESessionImpl::GetObjectL( const TInt64 aGuidHigh, const TInt64 aGuidLow, CMdENamespaceDef* aNamespaceDef )
       
   706 	{
       
   707 	CMdENamespaceDef* namespaceDef = GetNamespaceDefL( aNamespaceDef );
       
   708 
       
   709 	CMdEObjectDef* objectDef = namespaceDef->GetObjectDefL( KBaseObjectDefId );
       
   710 	if ( !objectDef )
       
   711 		{
       
   712 		User::Leave( KErrNotFound );
       
   713 		}
       
   714 	return GetObjectL(*objectDef, KNoId, aGuidHigh, aGuidLow, KNullDesC, EGet, EFalse);	
       
   715 	}
       
   716 
       
   717 CMdEObject* CMdESessionImpl::GetFullObjectL( const TInt64 aGuidHigh, const TInt64 aGuidLow, CMdENamespaceDef* aNamespaceDef )
       
   718 	{
       
   719 	CMdEObject* object = NULL;
       
   720 
       
   721 	object = GetObjectL( aGuidHigh, aGuidLow, aNamespaceDef );
       
   722 
       
   723 	if ( object )
       
   724 		{
       
   725 		CMdEObjectDef& objectDef = object->Def();
       
   726 		TItemId objId = object->Id();
       
   727 
       
   728 		delete object;
       
   729 		object = NULL;		
       
   730 		object = GetObjectL( objectDef, objId, 0, 0, KNullDesC, EGet, ETrue );
       
   731 		}
       
   732 
       
   733 	return object;
       
   734 	}
       
   735 
       
   736 CMdEObject* CMdESessionImpl::GetObjectL( const TInt64 aGuidHigh, const TInt64 aGuidLow, CMdEObjectDef& aObjectDef )
       
   737 	{
       
   738 	return GetObjectL(aObjectDef, KNoId, aGuidHigh, aGuidLow, KNullDesC, EGet, EFalse);	
       
   739 	}
       
   740 
       
   741 CMdEObject* CMdESessionImpl::OpenObjectL( const TItemId aId, CMdENamespaceDef* aNamespaceDef )
       
   742 	{
       
   743 	CMdENamespaceDef* namespaceDef = GetNamespaceDefL( aNamespaceDef );
       
   744 	
       
   745 	CMdEObjectDef* objectDef = namespaceDef->GetObjectDefL( KBaseObjectDefId );
       
   746 	if ( !objectDef )
       
   747 		{
       
   748 		User::Leave( KErrNotFound );
       
   749 		}
       
   750 
       
   751 	CMdEObject* object = GetObjectL(*objectDef, aId, 0, 0, KNullDesC, ELock, EFalse);
       
   752 	
       
   753 	if ( object && !object->OpenForModifications() )
       
   754 		{
       
   755 		delete object;
       
   756 		object = NULL;
       
   757 		User::Leave( KErrLocked );
       
   758 		}
       
   759 
       
   760 	return object;
       
   761 	}
       
   762 
       
   763 CMdEObject* CMdESessionImpl::OpenFullObjectL( const TItemId aId, CMdENamespaceDef* aNamespaceDef )
       
   764 	{
       
   765 	TMdEObject object;
       
   766 
       
   767 	CheckObjectL( object, aId, aNamespaceDef );
       
   768 
       
   769 	if( object.NotPresent() || object.Removed() )
       
   770 		{
       
   771 		User::Leave( KErrNotFound );
       
   772 		}
       
   773 
       
   774 	const CMdEObjectDef& objectDef = object.DefL();
       
   775 	
       
   776 	return GetObjectL( CONST_CAST( CMdEObjectDef&, objectDef ), aId, 0, 0, KNullDesC, ELock, ETrue );
       
   777 	}
       
   778 
       
   779 CMdEObject* CMdESessionImpl::OpenObjectL( const TItemId aId, CMdEObjectDef& aObjectDef )
       
   780     {
       
   781     CMdEObject* object = GetObjectL(aObjectDef, aId, 0, 0, KNullDesC, ELock, EFalse);
       
   782 
       
   783 	if( object && !object->OpenForModifications() )
       
   784 		{
       
   785 		delete object;
       
   786 		object = NULL;
       
   787 		User::Leave( KErrLocked );
       
   788 		}
       
   789 
       
   790 	return object;    
       
   791     }
       
   792 
       
   793 CMdEObject* CMdESessionImpl::OpenObjectL( const TInt64 aGuidHigh, const TInt64 aGuidLow, CMdENamespaceDef* aNamespaceDef )
       
   794 	{
       
   795 	CMdENamespaceDef* namespaceDef = GetNamespaceDefL( aNamespaceDef );
       
   796 	
       
   797 	CMdEObjectDef* objectDef = namespaceDef->GetObjectDefL( KBaseObjectDefId );
       
   798 	if ( !objectDef )
       
   799 		{
       
   800 		User::Leave( KErrNotFound );
       
   801 		}
       
   802 
       
   803 	CMdEObject* object = GetObjectL(*objectDef, KNoId, aGuidHigh, aGuidLow, KNullDesC, ELock, EFalse);
       
   804 
       
   805 	if( object && !object->OpenForModifications() )
       
   806 		{
       
   807 		delete object;
       
   808 		object = NULL;
       
   809 		User::Leave( KErrLocked );
       
   810 		}
       
   811 
       
   812 	return object;
       
   813 	}
       
   814 
       
   815 CMdEObject* CMdESessionImpl::OpenFullObjectL( const TInt64 aGuidHigh, const TInt64 aGuidLow, CMdENamespaceDef* aNamespaceDef )
       
   816 	{
       
   817 	CMdEObject* object = NULL;
       
   818 	
       
   819 	object = GetObjectL( aGuidHigh,  aGuidLow, aNamespaceDef );
       
   820 	
       
   821 	if( object )
       
   822 		{
       
   823 		CMdEObjectDef& objectDef = object->Def();
       
   824 		TItemId objId = object->Id();
       
   825 
       
   826 		delete object;
       
   827 		object = NULL;
       
   828 
       
   829 		object = GetObjectL( CONST_CAST( CMdEObjectDef&, objectDef ), objId, 0, 0, KNullDesC, ELock, ETrue );
       
   830 		}
       
   831 
       
   832 	return object;
       
   833 	}
       
   834 
       
   835 CMdEObject* CMdESessionImpl::OpenObjectL( const TInt64 aGuidHigh, const TInt64 aGuidLow, CMdEObjectDef& aObjectDef )
       
   836     {
       
   837     CMdEObject* object = GetObjectL(aObjectDef, KNoId, aGuidHigh, aGuidLow, KNullDesC, ELock, EFalse);
       
   838 
       
   839 	if( object && !object->OpenForModifications() )
       
   840 		{
       
   841 		delete object;
       
   842 		object = NULL;
       
   843 		User::Leave( KErrLocked );
       
   844 		}
       
   845 
       
   846 	return object;    
       
   847     }
       
   848 
       
   849 EXPORT_C CMdEObject* CMdESessionImpl::GetObjectL( const TDesC& aUri, CMdENamespaceDef* aNamespaceDef )
       
   850 	{
       
   851 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
   852 
       
   853 	if ( !aNamespaceDef )
       
   854 		{
       
   855 		namespaceDef = &GetDefaultNamespaceDefL();
       
   856 		}
       
   857 
       
   858 	CMdEObjectDef* objectDef = namespaceDef->GetObjectDefL( KBaseObjectDefId );
       
   859 	if ( !objectDef )
       
   860 		{
       
   861 		User::Leave( KErrNotFound );
       
   862 		}
       
   863 
       
   864 	return GetObjectL(*objectDef, KNoId, 0, 0, aUri, EGet, EFalse);
       
   865 	}
       
   866     
       
   867 CMdEObject* CMdESessionImpl::GetFullObjectL( const TDesC& aUri, CMdENamespaceDef* aNamespaceDef )
       
   868 	{
       
   869 	TMdEObject object;
       
   870 	
       
   871 	CheckObjectL( object, aUri, aNamespaceDef );
       
   872 	
       
   873 	if( object.NotPresent() || object.Removed() )
       
   874 		{
       
   875 		User::Leave( KErrNotFound );
       
   876 		}
       
   877 	
       
   878 	const CMdEObjectDef& objectDef = object.DefL();
       
   879 	TItemId objId = object.Id();
       
   880 	
       
   881 	return GetObjectL( CONST_CAST( CMdEObjectDef&, objectDef ), objId, 0, 0, KNullDesC, EGet, ETrue );
       
   882 	}
       
   883     
       
   884 CMdEObject* CMdESessionImpl::GetObjectL( const TDesC& aUri, CMdEObjectDef& aObjectDef )
       
   885     {
       
   886     return GetObjectL( aObjectDef, KNoId, 0, 0, aUri, EGet, EFalse );
       
   887     }
       
   888 
       
   889     
       
   890 CMdEObject* CMdESessionImpl::OpenObjectL( const TDesC& aUri, CMdENamespaceDef* aNamespaceDef )
       
   891 	{
       
   892 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
   893 	
       
   894 	if ( !aNamespaceDef )
       
   895 		{
       
   896 		namespaceDef = &GetDefaultNamespaceDefL();
       
   897 		}
       
   898 
       
   899 	CMdEObjectDef* objectDef = namespaceDef->GetObjectDefL( KBaseObjectDefId );
       
   900 	if ( !objectDef )
       
   901 		{
       
   902 		User::Leave( KErrNotFound );
       
   903 		}
       
   904 
       
   905 	CMdEObject* object = GetObjectL(*objectDef, KNoId, 0, 0, aUri, ELock, EFalse);
       
   906 
       
   907 	if( object && !object->OpenForModifications() )
       
   908 		{
       
   909 		delete object;
       
   910 		object = NULL;
       
   911 		User::Leave( KErrLocked );
       
   912 		}
       
   913 
       
   914 	return object;
       
   915 	}
       
   916 
       
   917 CMdEObject* CMdESessionImpl::OpenFullObjectL( const TDesC& aUri, CMdENamespaceDef* aNamespaceDef )
       
   918 	{
       
   919 	TMdEObject object;
       
   920 	
       
   921 	CheckObjectL( object, aUri, aNamespaceDef );
       
   922 	
       
   923 	if( object.NotPresent() || object.Removed() )
       
   924 		{
       
   925 		User::Leave( KErrNotFound );
       
   926 		}
       
   927 	
       
   928 	const CMdEObjectDef& objectDef = object.DefL();
       
   929 	TItemId objId = object.Id();
       
   930 
       
   931 	return GetObjectL( CONST_CAST( CMdEObjectDef&, objectDef ), objId, 0, 0, KNullDesC, ELock, ETrue );
       
   932 	}
       
   933 
       
   934 CMdEObject* CMdESessionImpl::OpenObjectL( const TDesC& aUri, CMdEObjectDef& aObjectDef )
       
   935     {
       
   936     CMdEObject* object = GetObjectL(aObjectDef, KNoId, 0, 0, aUri, ELock, EFalse);
       
   937 
       
   938 	if( object && !object->OpenForModifications() )
       
   939 		{
       
   940 		delete object;
       
   941 		object = NULL;
       
   942 		User::Leave( KErrLocked );
       
   943 		}
       
   944 
       
   945 	return object;
       
   946     }
       
   947 
       
   948 void CMdESessionImpl::CheckObjectL( TMdEObject& aObject, const TDesC& aUri, 
       
   949 		CMdENamespaceDef* aNamespaceDef )
       
   950 	{
       
   951 	CMdENamespaceDef& namespaceDef = *GetNamespaceDefL( aNamespaceDef );
       
   952 	
       
   953 	CMdCSerializationBuffer* object = 
       
   954 		CMdCSerializationBuffer::NewLC( aObject.RequiredBufferSize() );
       
   955 
       
   956 	iSession.DoCheckObjectL( *object, aUri, namespaceDef.Id() );
       
   957 
       
   958 	object->PositionL( KNoOffset );
       
   959 	aObject.DeSerializeL( *object, namespaceDef );
       
   960 
       
   961 	CleanupStack::PopAndDestroy( object );
       
   962 	}
       
   963 
       
   964 void CMdESessionImpl::CheckObjectL( TMdEObject& aObject, TItemId aId, 
       
   965 		CMdENamespaceDef* aNamespaceDef )
       
   966 	{
       
   967 	CMdENamespaceDef& namespaceDef = *GetNamespaceDefL( aNamespaceDef );
       
   968 
       
   969 	CMdCSerializationBuffer* object = 
       
   970 		CMdCSerializationBuffer::NewLC( aObject.RequiredBufferSize() );
       
   971 
       
   972 	iSession.DoCheckObjectL( *object, aId, namespaceDef.Id() );
       
   973 	
       
   974 	object->PositionL( KNoOffset );
       
   975 	aObject.DeSerializeL( *object, namespaceDef );
       
   976 	
       
   977 	CleanupStack::PopAndDestroy( object );
       
   978 	}
       
   979 
       
   980 void CMdESessionImpl::CheckObjectL( RArray<TMdEObject>& aObjects, 
       
   981 		const RArray<TItemId>& aIds, CMdENamespaceDef* aNamespaceDef )
       
   982 	{
       
   983 	CMdENamespaceDef& namespaceDef = *GetNamespaceDefL( aNamespaceDef );
       
   984 
       
   985 	const TUint32 idCount = (TUint32)aIds.Count();
       
   986 	
       
   987 	CMdCSerializationBuffer* objects = 
       
   988 		CMdCSerializationBuffer::NewLC( 
       
   989 				CMdCSerializationBuffer::KRequiredSizeForTUint32 + 
       
   990 				TMdEObject::RequiredBufferSize() * idCount );
       
   991 
       
   992 	CMdCSerializationBuffer* ids = 
       
   993 		CMdCSerializationBuffer::NewLC( 
       
   994 				CMdCSerializationBuffer::KRequiredSizeForTUint32 + 
       
   995 				CMdCSerializationBuffer::KRequiredSizeForTItemId * idCount );
       
   996 
       
   997 	ids->InsertL( idCount );
       
   998 	for( TUint32 i = 0; i < idCount; i++ )
       
   999 		{
       
  1000 		ids->InsertL( aIds[i] );
       
  1001 		}
       
  1002 
       
  1003 	iSession.DoCheckObjectL( *objects, *ids, namespaceDef.Id() );
       
  1004 
       
  1005 	objects->PositionL( KNoOffset );
       
  1006 
       
  1007 	TUint32 objectCount = 0;
       
  1008 	objects->ReceiveL( objectCount );
       
  1009 
       
  1010 	aObjects.ReserveL( objectCount );
       
  1011 
       
  1012 	for( TUint32 i = 0; i < objectCount; i++ )
       
  1013 		{
       
  1014 		aObjects.AppendL( TMdEObject() );
       
  1015 		aObjects[i].DeSerializeL( *objects, namespaceDef );
       
  1016 		}
       
  1017 	
       
  1018 	CleanupStack::PopAndDestroy( ids );
       
  1019 	CleanupStack::PopAndDestroy( objects );
       
  1020 	}
       
  1021 
       
  1022 CMdERelation* CMdESessionImpl::GetRelationL(TItemId aId, CMdENamespaceDef* aNamespaceDef)
       
  1023     {
       
  1024     CMdERelationQuery* query = NewRelationQueryL( *GetNamespaceDefL( aNamespaceDef ), NULL );
       
  1025     query->SetResultMode( EQueryResultModeItem );
       
  1026     CleanupStack::PushL( query );
       
  1027 
       
  1028     query->Conditions().AddRelationConditionL( aId );
       
  1029 
       
  1030 	CMdEQueryCriteriaSerialization* buf = CMdEQueryCriteriaSerialization::NewLC( 
       
  1031 											   query->ResultMode(),
       
  1032 											   query->Type(), 
       
  1033     										   query->NamespaceDef(), 
       
  1034     										   NULL, 
       
  1035     										   NULL, 
       
  1036     										   1, //Max 1  
       
  1037     										   0, // 0 offset because it's not used now
       
  1038     										   EContainsRelationCondition, 
       
  1039     										   query->Conditions(), 
       
  1040     										   query->OrderRules(),
       
  1041     										   NULL);
       
  1042 
       
  1043 
       
  1044 	CMdCSerializationBuffer* resbuf = iSession.DoFindSyncLC(
       
  1045 		query, 
       
  1046 		*buf, EGet, 
       
  1047 		KMdEQueryDefaultMaxCount);
       
  1048 
       
  1049 	RPointerArray<CMdEInstanceItem> items;
       
  1050 	CleanupClosePushL( items );
       
  1051 
       
  1052 	DeserializeQueryResultL( *resbuf, items );
       
  1053 
       
  1054 	CleanupStack::Pop( &items );
       
  1055 
       
  1056    	CleanupStack::PopAndDestroy(resbuf);
       
  1057 	CleanupStack::PopAndDestroy( buf );
       
  1058 	CleanupStack::PopAndDestroy( query );
       
  1059 
       
  1060 	const TInt itemsCount( items.Count() );
       
  1061 	if( itemsCount== 1 )
       
  1062 		{
       
  1063 		CMdEInstanceItem* item = items[0];
       
  1064 
       
  1065 #ifdef _DEBUG
       
  1066 		if ( !item || item->InstanceType() != EMdETypeRelation )
       
  1067 			{
       
  1068 			User::Leave( KErrCorrupt );
       
  1069 			}
       
  1070 #endif	
       
  1071 		
       
  1072 		items.Close();
       
  1073 			
       
  1074 		return (CMdERelation*)item;
       
  1075 		}
       
  1076    	else if( itemsCount == 0 )
       
  1077    		{
       
  1078    		items.Close();
       
  1079    		return NULL;
       
  1080    		}
       
  1081 #ifdef _DEBUG
       
  1082    	else
       
  1083    		{
       
  1084    		items.ResetAndDestroy();
       
  1085    		items.Close();
       
  1086    		
       
  1087    		User::Leave( KErrCorrupt );
       
  1088    		}
       
  1089 #endif
       
  1090 
       
  1091    	return NULL; // <-- just to stop compile warnings!!
       
  1092     }
       
  1093 
       
  1094 CMdEEvent* CMdESessionImpl::GetEventL(TItemId aId, 
       
  1095 		CMdENamespaceDef* aNamespaceDef)
       
  1096     {
       
  1097 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  1098 	
       
  1099 	if ( !aNamespaceDef )
       
  1100 		{
       
  1101 		namespaceDef = &GetDefaultNamespaceDefL();
       
  1102 		}
       
  1103     
       
  1104 	CMdEEventQuery * query = NewEventQueryL(*namespaceDef,NULL);
       
  1105     query->SetResultMode(EQueryResultModeItem);
       
  1106     CleanupStack::PushL(query);
       
  1107 
       
  1108 	query->Conditions().AddEventConditionL(aId);
       
  1109 	
       
  1110 	CMdEQueryCriteriaSerialization* buf = CMdEQueryCriteriaSerialization::NewLC( 
       
  1111 											   query->ResultMode(),
       
  1112 											   query->Type(), 
       
  1113     										   query->NamespaceDef(), 
       
  1114     										   NULL, 
       
  1115     										   NULL, 
       
  1116     										   1, //Max 1  
       
  1117     										   0, // 0 offset because it's not used now
       
  1118     										   EContainsEventCondition,
       
  1119     										   query->Conditions(), 
       
  1120     										   query->OrderRules(),
       
  1121     										   NULL);    
       
  1122 
       
  1123 	CMdCSerializationBuffer* resbuf = iSession.DoFindSyncLC(
       
  1124 			query, 
       
  1125 			*buf, EGet,
       
  1126 			KMdEQueryDefaultMaxCount);
       
  1127 
       
  1128 	RPointerArray<CMdEInstanceItem> items;
       
  1129 	CleanupClosePushL( items );
       
  1130 
       
  1131 	DeserializeQueryResultL( *resbuf, items );
       
  1132 
       
  1133 	CleanupStack::Pop( &items );
       
  1134 
       
  1135    	CleanupStack::PopAndDestroy(resbuf);
       
  1136 	CleanupStack::PopAndDestroy( buf );
       
  1137 	CleanupStack::PopAndDestroy( query );
       
  1138 
       
  1139 	const TInt itemsCount( items.Count() );
       
  1140 	if( itemsCount == 1 )
       
  1141 		{
       
  1142 		CMdEInstanceItem* item = items[0];
       
  1143 		
       
  1144 #ifdef _DEBUG
       
  1145 		if ( !item || item->InstanceType() != EMdETypeEvent )
       
  1146 			{
       
  1147 			User::Leave( KErrCorrupt );
       
  1148 			}
       
  1149 #endif
       
  1150 		
       
  1151 		items.Close();
       
  1152 			
       
  1153 		return (CMdEEvent*)item;
       
  1154 		}
       
  1155    	else if( itemsCount == 0 )
       
  1156    		{
       
  1157    		items.Close();
       
  1158    		
       
  1159    		return NULL;
       
  1160    		}
       
  1161 #ifdef _DEBUG
       
  1162 	else
       
  1163    		{
       
  1164    		items.ResetAndDestroy();
       
  1165    		items.Close();
       
  1166    		
       
  1167    		User::Leave( KErrCorrupt );
       
  1168    		}
       
  1169 #endif
       
  1170 	
       
  1171 	return NULL; // <-- just to stop compile warnings!!
       
  1172     }
       
  1173 
       
  1174 /**
       
  1175 * Remove methods
       
  1176 */
       
  1177 CMdCSerializationBuffer* CMdESessionImpl::RemoveCommonL( 
       
  1178 		CMdENamespaceDef& aNamespaceDef, const RArray<TItemId>* aObjects,
       
  1179 		const RArray<TItemId>* aEvents, const RArray<TItemId>* aRelations )
       
  1180 	{
       
  1181 	if ( !( (aObjects && aObjects->Count()) || 
       
  1182 			(aEvents && aEvents->Count()) || 
       
  1183 			(aRelations && aRelations->Count()) ) )
       
  1184 		{
       
  1185 		User::Leave( KErrArgument );
       
  1186 		}
       
  1187 
       
  1188 	TMdCItemIds itemIds;
       
  1189 	itemIds.iNamespaceDefId = aNamespaceDef.Id();
       
  1190 	itemIds.iObjectUris.iPtr.iCount = 0;
       
  1191 	itemIds.iObjectUris.iPtr.iOffset = KNoOffset;
       
  1192 
       
  1193 	// headerSize
       
  1194 	TUint32 bufferSize = sizeof(TMdCItemIds);
       
  1195 	
       
  1196 	if ( aObjects )
       
  1197 		{
       
  1198 		bufferSize += aObjects->Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  1199 		}
       
  1200 	if ( aEvents )
       
  1201 		{
       
  1202 		bufferSize += aEvents->Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  1203 		}
       
  1204 	if ( aRelations )
       
  1205 		{
       
  1206 		bufferSize += aRelations->Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  1207 		}
       
  1208 
       
  1209 	CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( bufferSize );
       
  1210 
       
  1211 	buffer->PositionL( sizeof(TMdCItemIds) );
       
  1212 
       
  1213 	// insert objects
       
  1214 	if ( aObjects )
       
  1215 		{
       
  1216 		const TInt count = aObjects->Count();
       
  1217 		itemIds.iObjectIds.iPtr.iCount = count;
       
  1218 		itemIds.iObjectIds.iPtr.iOffset = buffer->Position();
       
  1219 
       
  1220 		for ( TInt i = 0; i < count; ++i )
       
  1221 			{
       
  1222 			buffer->InsertL( (*aObjects)[i] );
       
  1223 			}
       
  1224 		}
       
  1225 	else
       
  1226 		{
       
  1227 		itemIds.iObjectIds.iPtr.iCount = 0;
       
  1228 		itemIds.iObjectIds.iPtr.iOffset = KNoOffset;
       
  1229 		}
       
  1230 	
       
  1231 	// insert events
       
  1232 	if ( aEvents )
       
  1233 		{
       
  1234 		const TInt count = aEvents->Count();
       
  1235 		itemIds.iEventIds.iPtr.iCount = count;
       
  1236 		itemIds.iEventIds.iPtr.iOffset = buffer->Position();
       
  1237 		
       
  1238 		for ( TInt i = 0; i < count; ++i )
       
  1239 			{
       
  1240 			buffer->InsertL( (*aEvents)[i] );
       
  1241 			}
       
  1242 		}
       
  1243 	else
       
  1244 		{
       
  1245 		itemIds.iEventIds.iPtr.iCount = 0;
       
  1246 		itemIds.iEventIds.iPtr.iOffset = KNoOffset;
       
  1247 		}
       
  1248 
       
  1249 	// insert relations
       
  1250 	if ( aRelations )
       
  1251 		{
       
  1252 		const TInt count = aRelations->Count();
       
  1253 		itemIds.iRelationIds.iPtr.iCount = count;
       
  1254 		itemIds.iRelationIds.iPtr.iOffset = buffer->Position();
       
  1255 		
       
  1256 		for ( TInt i = 0; i < count; ++i )
       
  1257 			{
       
  1258 			buffer->InsertL( (*aRelations)[i] );
       
  1259 			}
       
  1260 		}
       
  1261 	else
       
  1262 		{
       
  1263 		itemIds.iRelationIds.iPtr.iCount = 0;
       
  1264 		itemIds.iRelationIds.iPtr.iOffset = KNoOffset;
       
  1265 		}
       
  1266 
       
  1267 	// set up header correctly
       
  1268 	buffer->PositionL( KNoOffset );
       
  1269 	itemIds.SerializeL( *buffer );
       
  1270 
       
  1271 	CleanupStack::Pop( buffer );
       
  1272 	return buffer;
       
  1273 	}
       
  1274 
       
  1275 CMdCSerializationBuffer* CMdESessionImpl::RemoveCommonL( 
       
  1276 		CMdENamespaceDef& aNamespaceDef, 
       
  1277 		const RPointerArray<TDesC16>* aObjects,
       
  1278 		const RArray<TItemId>* aEvents, const RArray<TItemId>* aRelations )
       
  1279 	{
       
  1280 	if ( !( (aObjects && aObjects->Count()) || 
       
  1281 			(aEvents && aEvents->Count()) || 
       
  1282 			(aRelations && aRelations->Count()) ) )
       
  1283 		{
       
  1284 		User::Leave( KErrArgument );
       
  1285 		}
       
  1286 
       
  1287 	TMdCItemIds itemIds;
       
  1288 	itemIds.iNamespaceDefId = aNamespaceDef.Id();
       
  1289 	itemIds.iObjectIds.iPtr.iCount = 0;
       
  1290 	itemIds.iObjectIds.iPtr.iOffset = KNoOffset;
       
  1291 
       
  1292 	// headerSize
       
  1293 	TUint32 bufferSize = sizeof(TMdCItemIds);
       
  1294 
       
  1295 	if ( aObjects )
       
  1296 		{
       
  1297 		const TInt count = aObjects->Count();
       
  1298 		for ( TInt i = 0; i < count; ++i )
       
  1299 			{
       
  1300 			bufferSize += CMdCSerializationBuffer::RequiredSize( *((*aObjects)[i]) );
       
  1301 			}
       
  1302 		}
       
  1303 	if ( aEvents )
       
  1304 		{
       
  1305 		bufferSize += aEvents->Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  1306 		}
       
  1307 	if ( aRelations )
       
  1308 		{
       
  1309 		bufferSize += aRelations->Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  1310 		}
       
  1311 
       
  1312 	CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( bufferSize );
       
  1313 
       
  1314 	buffer->PositionL( sizeof(TMdCItemIds) );
       
  1315 
       
  1316 	// insert objects
       
  1317 	if ( aObjects )
       
  1318 		{
       
  1319 		const TInt count = aObjects->Count();
       
  1320 		itemIds.iObjectUris.iPtr.iCount = count;
       
  1321 		itemIds.iObjectUris.iPtr.iOffset = buffer->Position();
       
  1322 
       
  1323 		for ( TInt i = 0; i < count; ++i )
       
  1324 			{
       
  1325 			const TDesC& uri = *((*aObjects)[i]);
       
  1326 			HBufC* lcUri = HBufC::NewLC( uri.Length() );
       
  1327 			lcUri->Des().CopyLC( uri );
       
  1328 			buffer->InsertL( *lcUri );
       
  1329 			CleanupStack::PopAndDestroy( lcUri );
       
  1330 			}
       
  1331 		}
       
  1332 	else
       
  1333 		{
       
  1334 		itemIds.iObjectUris.iPtr.iCount = 0;
       
  1335 		itemIds.iObjectUris.iPtr.iOffset = KNoOffset;
       
  1336 		}
       
  1337 	
       
  1338 	// insert events
       
  1339 	if ( aEvents )
       
  1340 		{
       
  1341 		const TInt count = aEvents->Count();
       
  1342 		itemIds.iEventIds.iPtr.iCount = count;
       
  1343 		itemIds.iEventIds.iPtr.iOffset = buffer->Position();
       
  1344 		
       
  1345 		for ( TInt i = 0; i < count; ++i )
       
  1346 			{
       
  1347 			buffer->InsertL( (*aEvents)[i] );
       
  1348 			}
       
  1349 		}
       
  1350 	else
       
  1351 		{
       
  1352 		itemIds.iEventIds.iPtr.iCount = 0;
       
  1353 		itemIds.iEventIds.iPtr.iOffset = KNoOffset;
       
  1354 		}
       
  1355 
       
  1356 	// insert relations
       
  1357 	if ( aRelations )
       
  1358 		{
       
  1359 		const TInt count = aRelations->Count();
       
  1360 		itemIds.iRelationIds.iPtr.iCount = count;
       
  1361 		itemIds.iRelationIds.iPtr.iOffset = buffer->Position();
       
  1362 		
       
  1363 		for ( TInt i = 0; i < count; ++i )
       
  1364 			{
       
  1365 			buffer->InsertL( (*aRelations)[i] );
       
  1366 			}
       
  1367 		}
       
  1368 	else
       
  1369 		{
       
  1370 		itemIds.iRelationIds.iPtr.iCount = 0;
       
  1371 		itemIds.iRelationIds.iPtr.iOffset = KNoOffset;
       
  1372 		}
       
  1373 
       
  1374 	// set up header correctly
       
  1375 	buffer->PositionL( KNoOffset );
       
  1376 	itemIds.SerializeL( *buffer );
       
  1377 
       
  1378 	CleanupStack::Pop( buffer );
       
  1379 	return buffer;
       
  1380 	}
       
  1381 
       
  1382 TInt CMdESessionImpl::DeserializeIdsL( RMdEDataBuffer& aSerializedItemIds, 
       
  1383 		RArray<TItemId>* aResultObjects, RArray<TItemId>* aResultEvents, 
       
  1384 		RArray<TItemId>* aResultRelations )
       
  1385 	{
       
  1386 	CMdCSerializationBuffer* buffer = aSerializedItemIds.GetBufferLC();
       
  1387 	
       
  1388 	const TMdCItemIds& itemIds = TMdCItemIds::GetFromBufferL( *buffer );
       
  1389 
       
  1390     if ( itemIds.iObjectIds.iPtr.iCount > 0 && aResultObjects )
       
  1391     	{
       
  1392     	buffer->PositionL( itemIds.iObjectIds.iPtr.iOffset );
       
  1393     	TItemId objectId;
       
  1394     	aResultObjects->ReserveL( itemIds.iObjectIds.iPtr.iCount );
       
  1395     	for (TUint32 i = 0; i < itemIds.iObjectIds.iPtr.iCount; ++i)
       
  1396     		{
       
  1397     		buffer->ReceiveL( objectId );
       
  1398     		aResultObjects->AppendL( objectId );
       
  1399     		}
       
  1400     	}
       
  1401 
       
  1402     if ( itemIds.iEventIds.iPtr.iCount > 0 && aResultEvents )
       
  1403     	{
       
  1404     	buffer->PositionL( itemIds.iEventIds.iPtr.iOffset );
       
  1405     	TItemId eventId;
       
  1406     	aResultEvents->ReserveL( itemIds.iEventIds.iPtr.iCount );
       
  1407     	for (TUint32 i = 0; i < itemIds.iEventIds.iPtr.iCount; ++i)
       
  1408     		{
       
  1409     		buffer->ReceiveL( eventId );
       
  1410     		aResultEvents->AppendL( eventId );
       
  1411     		}
       
  1412     	}
       
  1413 
       
  1414     if ( itemIds.iRelationIds.iPtr.iCount > 0 && aResultRelations )
       
  1415     	{
       
  1416     	buffer->PositionL( itemIds.iRelationIds.iPtr.iOffset );
       
  1417     	TItemId relationId;
       
  1418     	aResultRelations->ReserveL( itemIds.iRelationIds.iPtr.iCount );
       
  1419     	for (TUint32 i = 0; i < itemIds.iRelationIds.iPtr.iCount; ++i)
       
  1420     		{
       
  1421     		buffer->ReceiveL( relationId );
       
  1422     		aResultRelations->AppendL( relationId );
       
  1423     		}
       
  1424     	}
       
  1425     
       
  1426     const TInt errorCode = itemIds.iErrorCode;
       
  1427     CleanupStack::PopAndDestroy( buffer );
       
  1428     
       
  1429     return errorCode;
       
  1430 	}
       
  1431 
       
  1432 TItemId CMdESessionImpl::RemoveObjectL( TItemId aId, 
       
  1433 		CMdENamespaceDef* aNamespaceDef )
       
  1434     {
       
  1435 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  1436 
       
  1437 	if ( !aNamespaceDef )
       
  1438 		{
       
  1439 		namespaceDef = &GetDefaultNamespaceDefL();
       
  1440 		}    
       
  1441 
       
  1442 	RArray<TItemId> removeIdArray;
       
  1443 	CleanupClosePushL( removeIdArray );
       
  1444 	RArray<TItemId> resultObjectArray;
       
  1445 	CleanupClosePushL( resultObjectArray );
       
  1446 	removeIdArray.AppendL( aId );
       
  1447 	User::LeaveIfError( RemoveObjectsL( removeIdArray, resultObjectArray, 
       
  1448 			namespaceDef ) );
       
  1449 	TItemId result = KNoId;
       
  1450 	if ( resultObjectArray.Count() )
       
  1451 		{
       
  1452 		result = resultObjectArray[0];
       
  1453 		}
       
  1454 	CleanupStack::PopAndDestroy( 2, &removeIdArray ); // resultObjectArray, removeIdArray
       
  1455 	return result;
       
  1456     }
       
  1457 
       
  1458 TItemId CMdESessionImpl::RemoveObjectL( const TDesC& aUri, 
       
  1459 		CMdENamespaceDef* aNamespaceDef )
       
  1460     {
       
  1461 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  1462 
       
  1463 	if ( !aNamespaceDef )
       
  1464 		{
       
  1465 		namespaceDef = &GetDefaultNamespaceDefL();
       
  1466 		}
       
  1467 
       
  1468 	RPointerArray<TDesC16> removeUriArray;
       
  1469 	CleanupClosePushL( removeUriArray );
       
  1470 	RArray<TItemId> resultObjectArray;
       
  1471 	CleanupClosePushL( resultObjectArray );
       
  1472 	removeUriArray.AppendL( &aUri );
       
  1473 	User::LeaveIfError( RemoveObjectsL( removeUriArray, resultObjectArray, 
       
  1474 			namespaceDef ) );
       
  1475 	TItemId result = KNoId;
       
  1476 	if ( resultObjectArray.Count() )
       
  1477 		{
       
  1478 		result = resultObjectArray[0];
       
  1479 		}
       
  1480 	CleanupStack::PopAndDestroy( 2, &removeUriArray ); // resultObjectArray, removeUriArray
       
  1481 	return result;
       
  1482     }
       
  1483 
       
  1484 TInt CMdESessionImpl::RemoveObjectsL( const RArray<TItemId>& aId, 
       
  1485 		RArray<TItemId>& aResult, CMdENamespaceDef* aNamespaceDef )
       
  1486 	{
       
  1487 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  1488 	
       
  1489 	if ( !aNamespaceDef )
       
  1490 		{
       
  1491 		namespaceDef = &GetDefaultNamespaceDefL();
       
  1492 		}
       
  1493 
       
  1494 	CMdCSerializationBuffer* buffer = RemoveCommonL( *namespaceDef, &aId, 
       
  1495 			NULL, NULL  );
       
  1496 	CleanupStack::PushL( buffer );
       
  1497 	CMdCSerializationBuffer* resultBuffer = CMdCSerializationBuffer::NewLC( 
       
  1498 			buffer->Size() );
       
  1499 	RMdEDataBuffer dataBuffer;
       
  1500 	dataBuffer.SetBufferL( resultBuffer );
       
  1501 	CleanupStack::Pop( resultBuffer );
       
  1502 	CleanupClosePushL( dataBuffer );
       
  1503 	
       
  1504     iSession.DoRemoveItemsL( *buffer, *resultBuffer );
       
  1505     TInt32 firstItemError = DeserializeIdsL( dataBuffer, &aResult );
       
  1506     CleanupStack::PopAndDestroy( 2, buffer ); // successfulBuffer, buffer
       
  1507     return firstItemError;
       
  1508 	}
       
  1509 
       
  1510 TInt CMdESessionImpl::RemoveObjectsL( const RPointerArray<TDesC>& aUri, 
       
  1511 		RArray<TItemId>& aResult, CMdENamespaceDef* aNamespaceDef )
       
  1512 	{
       
  1513 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  1514 	
       
  1515 	if ( !aNamespaceDef )
       
  1516 		{
       
  1517 		namespaceDef = &GetDefaultNamespaceDefL();
       
  1518 		}    
       
  1519 
       
  1520 	CMdCSerializationBuffer* buffer = RemoveCommonL( *namespaceDef, &aUri, NULL, NULL  );
       
  1521 	CleanupStack::PushL( buffer );
       
  1522     const TUint32 rbs = sizeof( TMdCItemIds )
       
  1523                         + ( aUri.Count() + 2 ) * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  1524 
       
  1525 	CMdCSerializationBuffer* resultBuffer = CMdCSerializationBuffer::NewLC( rbs );
       
  1526 	RMdEDataBuffer dataBuffer;
       
  1527 	dataBuffer.SetBufferL( resultBuffer );
       
  1528 	CleanupStack::Pop( resultBuffer );
       
  1529 	CleanupClosePushL( dataBuffer );
       
  1530 	
       
  1531     iSession.DoRemoveItemsL( *buffer, *resultBuffer );
       
  1532     TInt32 firstItemError = DeserializeIdsL( dataBuffer, &aResult );
       
  1533     CleanupStack::PopAndDestroy( &dataBuffer );
       
  1534     CleanupStack::PopAndDestroy( buffer );
       
  1535     return firstItemError;
       
  1536 	}
       
  1537 
       
  1538 void CMdESessionImpl::RemoveObjectsAsyncL( 
       
  1539 		const RArray<TItemId>& aId, TRequestStatus& aStatus, 
       
  1540 		RMdEDataBuffer& aSerializedObjectIds, 
       
  1541 		CMdENamespaceDef* aNamespaceDef )
       
  1542 	{
       
  1543 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  1544 
       
  1545 	if ( !aNamespaceDef )
       
  1546 		{
       
  1547 		namespaceDef = &GetDefaultNamespaceDefL();
       
  1548 		}
       
  1549 
       
  1550 	CMdCSerializationBuffer* buffer = RemoveCommonL( *namespaceDef, &aId, 
       
  1551 			NULL, NULL  );
       
  1552 	CleanupStack::PushL( buffer );
       
  1553 	CMdCSerializationBuffer* resultBuffer = CMdCSerializationBuffer::NewLC( 
       
  1554 			buffer->Size() );
       
  1555 	aSerializedObjectIds.SetBufferL( resultBuffer );
       
  1556     CleanupStack::Pop( resultBuffer );
       
  1557 	
       
  1558     CleanupStack::Pop( buffer );
       
  1559 
       
  1560     iAsyncHandler->RemoveRequest( buffer, *resultBuffer, aStatus );
       
  1561 	}
       
  1562 	
       
  1563 void CMdESessionImpl::RemoveObjectsAsyncL( 
       
  1564 		const RPointerArray<TDesC>& aUri, TRequestStatus& aStatus, 
       
  1565 		RMdEDataBuffer& aSerializedObjectIds, 
       
  1566 		CMdENamespaceDef* aNamespaceDef )
       
  1567 	{
       
  1568 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  1569 	
       
  1570 	if ( !aNamespaceDef )
       
  1571 		{
       
  1572 		namespaceDef = &GetDefaultNamespaceDefL();
       
  1573 		}    
       
  1574 
       
  1575 	CMdCSerializationBuffer* buffer = RemoveCommonL( *namespaceDef, &aUri, 
       
  1576 			NULL, NULL  );
       
  1577 	CleanupStack::PushL( buffer );
       
  1578     const TUint32 rbs = sizeof( TMdCItemIds )
       
  1579                         + ( aUri.Count() + 2 ) * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  1580 
       
  1581 	CMdCSerializationBuffer* resultBuffer = CMdCSerializationBuffer::NewLC( rbs );
       
  1582 	aSerializedObjectIds.SetBufferL( resultBuffer );
       
  1583 	CleanupStack::Pop( resultBuffer );
       
  1584 	CleanupStack::Pop( buffer );
       
  1585 
       
  1586     iAsyncHandler->RemoveRequest( buffer, *resultBuffer, aStatus );
       
  1587 	}
       
  1588 
       
  1589 TItemId CMdESessionImpl::RemoveRelationL(TItemId aId, 
       
  1590 		CMdENamespaceDef* aNamespaceDef)
       
  1591     {
       
  1592     RArray<TItemId> items;
       
  1593     CleanupClosePushL( items );
       
  1594     RArray<TItemId> successful;
       
  1595     CleanupClosePushL( successful );
       
  1596 
       
  1597     items.AppendL( aId );
       
  1598     User::LeaveIfError( RemoveRelationsL( items, successful, aNamespaceDef ) );
       
  1599 
       
  1600     TItemId result = KNoId;
       
  1601     if ( successful.Count() )
       
  1602     	{
       
  1603     	result = successful[0];
       
  1604     	}
       
  1605     CleanupStack::PopAndDestroy( 2, &items );
       
  1606     return result;
       
  1607     }
       
  1608 
       
  1609 TInt CMdESessionImpl::RemoveRelationsL(const RArray<TItemId>& aId, 
       
  1610 		RArray<TItemId>& aSuccessful, CMdENamespaceDef* aNamespaceDef)
       
  1611 	{
       
  1612 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  1613 
       
  1614 	if ( !aNamespaceDef )
       
  1615 		{
       
  1616 		namespaceDef = &GetDefaultNamespaceDefL();
       
  1617 		}
       
  1618 
       
  1619 	CMdCSerializationBuffer* buffer = RemoveCommonL( *namespaceDef, 
       
  1620 			(RArray<TItemId>*)NULL, NULL, &aId  );
       
  1621 	CleanupStack::PushL( buffer );
       
  1622 	CMdCSerializationBuffer* successfulBuffer = CMdCSerializationBuffer::NewLC(
       
  1623 			buffer->Size() );
       
  1624     iSession.DoRemoveItemsL( *buffer, *successfulBuffer );
       
  1625     
       
  1626 	RMdEDataBuffer dataBuffer;
       
  1627 	dataBuffer.SetBufferL( successfulBuffer );
       
  1628 	CleanupStack::Pop( successfulBuffer );
       
  1629 	CleanupClosePushL( dataBuffer );
       
  1630     
       
  1631     TInt firstItemError = DeserializeIdsL( dataBuffer, NULL, NULL, 
       
  1632     		&aSuccessful );
       
  1633     CleanupStack::PopAndDestroy( &dataBuffer ); // successfulBuffer, buffer
       
  1634     CleanupStack::PopAndDestroy( buffer ); // successfulBuffer, buffer
       
  1635     return firstItemError;
       
  1636 	}
       
  1637 
       
  1638 void CMdESessionImpl::RemoveRelationsAsyncL(
       
  1639 		const RArray<TItemId>& aId, TRequestStatus& aStatus, 
       
  1640 		RMdEDataBuffer& aSerializedRelationIds, 
       
  1641 		CMdENamespaceDef* aNamespaceDef)
       
  1642 	{
       
  1643 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  1644 
       
  1645 	if ( !aNamespaceDef )
       
  1646 		{
       
  1647 		namespaceDef = &GetDefaultNamespaceDefL();
       
  1648 		}
       
  1649 
       
  1650 	CMdCSerializationBuffer* buffer = RemoveCommonL( *namespaceDef, 
       
  1651 			(RArray<TItemId>*)NULL, NULL, &aId  );
       
  1652 	CleanupStack::PushL( buffer );
       
  1653 	CMdCSerializationBuffer* resultBuffer = CMdCSerializationBuffer::NewLC( 
       
  1654 			buffer->Size() );
       
  1655 	aSerializedRelationIds.SetBufferL( resultBuffer );
       
  1656     CleanupStack::Pop( resultBuffer );    
       
  1657     CleanupStack::Pop( buffer );
       
  1658 
       
  1659     iAsyncHandler->RemoveRequest( buffer, *resultBuffer, aStatus );    
       
  1660 	}
       
  1661 
       
  1662 /**
       
  1663 * Add methods
       
  1664 */
       
  1665 TItemId CMdESessionImpl::AddItemL( CMdEInstanceItem& aItem )
       
  1666 	{
       
  1667 	RPointerArray<CMdEInstanceItem> items;
       
  1668 	CleanupClosePushL( items );
       
  1669 	items.Append( &aItem );
       
  1670 	User::LeaveIfError( AddItemsL( items ) );
       
  1671 	CleanupStack::PopAndDestroy( &items );
       
  1672 	return aItem.Id();
       
  1673 	}
       
  1674 
       
  1675 CMdCSerializationBuffer* CMdESessionImpl::SerializeItemsL( 
       
  1676 		RPointerArray<CMdEInstanceItem>& aItems )
       
  1677     {
       
  1678     const TInt itemsCount = aItems.Count();
       
  1679 	if ( itemsCount == 0 )
       
  1680 		{
       
  1681 		User::Leave(KErrArgument);
       
  1682 		return NULL;
       
  1683 		}
       
  1684 
       
  1685     TInt requiredBufferSize = sizeof(TMdCItems);
       
  1686 
       
  1687 	// counting items and required buffer size
       
  1688     TMdCItems items;
       
  1689     items.iNamespaceDefId = KNoDefId;
       
  1690     items.iErrorCode = KErrNone;
       
  1691     items.iObjects.iPtr.iCount = 0;
       
  1692     items.iObjects.iPtr.iOffset = 0;
       
  1693     items.iRelations.iPtr.iCount = 0;
       
  1694     items.iRelations.iPtr.iOffset = 0;
       
  1695     items.iEvents.iPtr.iCount = 0;
       
  1696     items.iEvents.iPtr.iOffset = 0;
       
  1697 
       
  1698     for ( TInt i = 0; i < itemsCount; ++i )
       
  1699 		{
       
  1700         switch (aItems[i]->InstanceType())
       
  1701         	{
       
  1702 			case EMdETypeObject:
       
  1703 	        	{
       
  1704 	            requiredBufferSize += static_cast<CMdEObject*>(aItems[i])->RequiredBufferSize();
       
  1705 				++items.iObjects.iPtr.iCount;
       
  1706 				const TDefId nmspId = static_cast<CMdEObject*>(aItems[i])->Def().NamespaceDef().Id();
       
  1707 				if (items.iNamespaceDefId == KNoDefId)
       
  1708 					{
       
  1709 					items.iNamespaceDefId = nmspId;
       
  1710 					}
       
  1711 				else if ( items.iNamespaceDefId != nmspId )
       
  1712 					{
       
  1713 					User::Leave(KErrArgument);
       
  1714 					}
       
  1715 	            break;
       
  1716 	            }
       
  1717 	        case EMdETypeRelation:
       
  1718 	        	{
       
  1719 	            requiredBufferSize += static_cast<CMdERelation*>(aItems[i])->RequiredBufferSize();
       
  1720 				++items.iRelations.iPtr.iCount;
       
  1721 				const TDefId nmspId = static_cast<CMdERelation*>(aItems[i])->Def().NamespaceDef().Id();
       
  1722 				if (items.iNamespaceDefId == KNoDefId)
       
  1723 					{
       
  1724 					items.iNamespaceDefId = nmspId;
       
  1725 					}
       
  1726 				else if ( items.iNamespaceDefId != nmspId )
       
  1727 					{
       
  1728 					User::Leave(KErrArgument);
       
  1729 					}
       
  1730 	            break;
       
  1731 	            }
       
  1732 	        case EMdETypeEvent:
       
  1733 	        	{
       
  1734 	            requiredBufferSize += static_cast<CMdEEvent*>(aItems[i])->RequiredBufferSize();
       
  1735 				++items.iEvents.iPtr.iCount;
       
  1736 				const TDefId nmspId = static_cast<CMdEEvent*>(aItems[i])->Def().NamespaceDef().Id();
       
  1737 				if (items.iNamespaceDefId == KNoDefId)
       
  1738 					{
       
  1739 					items.iNamespaceDefId = nmspId;
       
  1740 					}
       
  1741 				else if ( items.iNamespaceDefId != nmspId )
       
  1742 					{
       
  1743 					User::Leave(KErrArgument);
       
  1744 					}
       
  1745 	            break;
       
  1746 	            }
       
  1747 	        default:
       
  1748 	        	{
       
  1749 	        	User::Leave(KErrArgument);
       
  1750 	        	}
       
  1751         	}
       
  1752 		}
       
  1753 
       
  1754 	CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( requiredBufferSize );
       
  1755 
       
  1756     // move after main header
       
  1757 	TMdCOffset freespaceOffset = sizeof( TMdCItems );
       
  1758 
       
  1759 	if (items.iObjects.iPtr.iCount)
       
  1760 		{
       
  1761 		// add objects header
       
  1762 		items.iObjects.iPtr.iOffset = freespaceOffset;
       
  1763 		freespaceOffset += items.iObjects.iPtr.iCount * sizeof( TMdCObject );
       
  1764 		}
       
  1765 	if (items.iEvents.iPtr.iCount)
       
  1766 		{
       
  1767 		// add events header
       
  1768 		items.iEvents.iPtr.iOffset = freespaceOffset;
       
  1769 		freespaceOffset += items.iEvents.iPtr.iCount * sizeof( TMdCEvent );
       
  1770 		}
       
  1771 	if (items.iRelations.iPtr.iCount)
       
  1772 		{
       
  1773 		// add relations header
       
  1774 		items.iRelations.iPtr.iOffset = freespaceOffset;
       
  1775 		freespaceOffset += items.iRelations.iPtr.iCount * sizeof( TMdCRelation );
       
  1776 		}
       
  1777 
       
  1778 	TUint32 objectCtr = 0;
       
  1779     TUint32 relationCtr = 0;
       
  1780     TUint32 eventCtr = 0;
       
  1781 	for ( TInt i = 0; i < itemsCount; ++i )
       
  1782 		{
       
  1783         const TUint32 actualPosition = buffer->Position();
       
  1784         switch (aItems[i]->InstanceType())
       
  1785         	{
       
  1786 			case EMdETypeObject:
       
  1787 	        	{
       
  1788 	            CMdEObject* object = static_cast<CMdEObject*>(aItems[i]);
       
  1789 	            // set right offset
       
  1790 	            buffer->PositionL( items.iObjects.iPtr.iOffset + objectCtr * sizeof(TMdCObject) );
       
  1791 	            freespaceOffset = object->SerializeL( *buffer, freespaceOffset );
       
  1792 	            ++objectCtr;
       
  1793 	            break;
       
  1794 	            }
       
  1795 	        case EMdETypeRelation:
       
  1796 	        	{
       
  1797 	            CMdERelation* relation = static_cast<CMdERelation*>(aItems[i]);
       
  1798 	            // set right offset
       
  1799 	            buffer->PositionL( items.iRelations.iPtr.iOffset + relationCtr * sizeof(TMdCRelation) );
       
  1800 	            freespaceOffset = relation->SerializeL( *buffer, freespaceOffset );
       
  1801 	            ++relationCtr;
       
  1802 	            break;
       
  1803 	            }
       
  1804 	        case EMdETypeEvent:
       
  1805 	        	{
       
  1806 	            CMdEEvent* event = static_cast<CMdEEvent*>(aItems[i]);
       
  1807 	            // set right offset
       
  1808 	            buffer->PositionL( items.iEvents.iPtr.iOffset + eventCtr * sizeof(TMdCEvent) );
       
  1809 	            freespaceOffset = event->SerializeL( *buffer, freespaceOffset );
       
  1810 	            ++eventCtr;
       
  1811 				break;
       
  1812 	            }
       
  1813 	        default:
       
  1814 	        	{
       
  1815 	        	User::Leave(KErrArgument);
       
  1816 	        	}
       
  1817         	}
       
  1818 		}
       
  1819 
       
  1820     // insert namespaceid
       
  1821 	buffer->PositionL( KNoOffset );
       
  1822 	items.SerializeL( *buffer );
       
  1823 
       
  1824     CleanupStack::Pop( buffer );
       
  1825     return buffer;
       
  1826     }
       
  1827 
       
  1828 void CMdESessionImpl::DeserializeQueryResultL( 
       
  1829 		CMdCSerializationBuffer& aBuffer, 
       
  1830 		RPointerArray<CMdEInstanceItem>& aItems )
       
  1831 	{
       
  1832 	const TMdCItems& items = TMdCItems::GetFromBufferL( aBuffer );
       
  1833 
       
  1834     CMdENamespaceDef& namespaceDef = GetNamespaceDefL( items.iNamespaceDefId );
       
  1835 
       
  1836     aItems.ReserveL( items.iObjects.iPtr.iCount + items.iEvents.iPtr.iCount 
       
  1837     		+ items.iRelations.iPtr.iCount );
       
  1838     
       
  1839 	if ( items.iObjects.iPtr.iCount > 0 )
       
  1840 		{
       
  1841 		for ( TUint32 i = 0; i < items.iObjects.iPtr.iCount; ++i )
       
  1842 			{
       
  1843 			aBuffer.PositionL( items.iObjects.iPtr.iOffset + i * sizeof(TMdCObject) );
       
  1844 			CMdEObject* object = CMdEObject::NewLC( this, aBuffer, 
       
  1845 					namespaceDef );
       
  1846 			aItems.AppendL( object );
       
  1847 			CleanupStack::Pop( object );
       
  1848 			}
       
  1849 		}
       
  1850 		
       
  1851 	if ( items.iEvents.iPtr.iCount > 0 )
       
  1852 		{
       
  1853 		for ( TUint32 i = 0; i < items.iEvents.iPtr.iCount; ++i )
       
  1854 			{
       
  1855 			aBuffer.PositionL( items.iEvents.iPtr.iOffset + i * sizeof(TMdCEvent) );
       
  1856 			CMdEEvent* event = CMdEEvent::NewLC( this, aBuffer, namespaceDef );
       
  1857 			aItems.AppendL( event );
       
  1858 			CleanupStack::Pop( event );
       
  1859 			}
       
  1860 		}
       
  1861 
       
  1862 	if ( items.iRelations.iPtr.iCount > 0 )
       
  1863 		{
       
  1864 		for ( TUint32 i = 0; i < items.iRelations.iPtr.iCount; ++i )
       
  1865 			{
       
  1866 			aBuffer.PositionL( items.iRelations.iPtr.iOffset + i * sizeof(TMdCRelation) );
       
  1867 			CMdERelation* relation = CMdERelation::NewLC( this, aBuffer, 
       
  1868 					namespaceDef );
       
  1869 			aItems.AppendL( relation );
       
  1870 			CleanupStack::Pop( relation );
       
  1871 			}
       
  1872 		}
       
  1873 	}
       
  1874 
       
  1875 TItemId CMdESessionImpl::AddObjectL( CMdEObject& aObject )
       
  1876     {
       
  1877     AddItemL(aObject);
       
  1878     return aObject.Id();
       
  1879     }
       
  1880 
       
  1881 TInt CMdESessionImpl::AddObjectsL( RPointerArray<CMdEObject>& aObjects )
       
  1882     {
       
  1883     const TInt firstObjectError = AddItemsL( 
       
  1884     		(RPointerArray<CMdEInstanceItem>&)aObjects );
       
  1885 
       
  1886     return firstObjectError;
       
  1887     }
       
  1888 
       
  1889 TItemId CMdESessionImpl::AddRelationL( CMdERelation& aRelation ) 
       
  1890     {
       
  1891 	return AddItemL( aRelation );
       
  1892     }
       
  1893 
       
  1894 TItemId CMdESessionImpl::UpdateRelationL( CMdERelation& aRelation ) 
       
  1895     {
       
  1896 	RPointerArray<CMdEInstanceItem> items;
       
  1897 	CleanupClosePushL( items );
       
  1898 	items.Append( &aRelation );
       
  1899 	User::LeaveIfError( UpdateItemsL( items ) );
       
  1900 	CleanupStack::PopAndDestroy( &items );
       
  1901 	return aRelation.Id();
       
  1902     }
       
  1903 
       
  1904 TInt CMdESessionImpl::AddItemsL( RPointerArray<CMdEInstanceItem>& aItems )
       
  1905     {    
       
  1906     CMdCSerializationBuffer *buffer = SerializeItemsL( aItems );
       
  1907     CleanupStack::PushL( buffer );
       
  1908     CMdCSerializationBuffer* resultBuf = CMdCSerializationBuffer::NewLC( 
       
  1909     		+ sizeof(TMdCItemIds)
       
  1910     		+ aItems.Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId );
       
  1911 	RMdEDataBuffer dataBuffer;
       
  1912 	dataBuffer.SetBufferL( resultBuf );
       
  1913 	CleanupStack::Pop( resultBuf );
       
  1914 	CleanupClosePushL( dataBuffer );    
       
  1915     
       
  1916     iSession.DoAddItemsL( *buffer, *resultBuf );
       
  1917     
       
  1918 	const TInt firstItemError = DeserializeItemsL( dataBuffer, aItems );
       
  1919 
       
  1920 	CleanupStack::PopAndDestroy( &dataBuffer );
       
  1921     CleanupStack::PopAndDestroy( buffer );
       
  1922     
       
  1923     return firstItemError;
       
  1924     }
       
  1925 
       
  1926 void CMdESessionImpl::AddItemsAsyncL( 
       
  1927 		RPointerArray<CMdEInstanceItem>& aItems, TRequestStatus& aStatus, 
       
  1928 		RMdEDataBuffer& aSerializedItemIds )
       
  1929     {    
       
  1930     CMdCSerializationBuffer* buffer = SerializeItemsL( aItems );
       
  1931     CleanupStack::PushL( buffer );
       
  1932 
       
  1933     CMdCSerializationBuffer* resultBuf = CMdCSerializationBuffer::NewLC( 
       
  1934     		+ sizeof(TMdCItemIds)
       
  1935     		+ aItems.Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId );
       
  1936     aSerializedItemIds.SetBufferL( resultBuf );
       
  1937     CleanupStack::Pop( resultBuf );    
       
  1938     CleanupStack::Pop( buffer );
       
  1939 
       
  1940     iAsyncHandler->AddRequest( buffer, *resultBuf, aStatus );
       
  1941     }
       
  1942 
       
  1943 TInt CMdESessionImpl::UpdateItemsL( RPointerArray<CMdEInstanceItem>& aItems )
       
  1944     {    
       
  1945     CMdCSerializationBuffer *buffer = SerializeItemsL( aItems );
       
  1946     CleanupStack::PushL( buffer );
       
  1947 
       
  1948     CMdCSerializationBuffer* resultBuf = CMdCSerializationBuffer::NewLC( 
       
  1949     		+ sizeof(TMdCItemIds)
       
  1950     		+ aItems.Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId );
       
  1951 
       
  1952 	RMdEDataBuffer dataBuffer;
       
  1953 	dataBuffer.SetBufferL( resultBuf );
       
  1954 	CleanupStack::Pop( resultBuf );
       
  1955 	CleanupClosePushL( dataBuffer );
       
  1956 
       
  1957     iSession.DoUpdateItemsL( *buffer, *resultBuf );
       
  1958 
       
  1959 	TInt firstItemError = DeserializeItemsL( dataBuffer, aItems );
       
  1960 
       
  1961 	CleanupStack::PopAndDestroy( &dataBuffer );
       
  1962     CleanupStack::PopAndDestroy( buffer );
       
  1963     return firstItemError;
       
  1964     }      
       
  1965 
       
  1966 void CMdESessionImpl::UpdateItemsAsyncL( 
       
  1967 		RPointerArray<CMdEInstanceItem>& aItems, TRequestStatus& aStatus,
       
  1968 		RMdEDataBuffer& aSerializedItemIds )
       
  1969     {
       
  1970     CMdCSerializationBuffer *buffer = SerializeItemsL( aItems );
       
  1971     CleanupStack::PushL( buffer );
       
  1972     CMdCSerializationBuffer* resultBuf = CMdCSerializationBuffer::NewLC( 
       
  1973     		+ sizeof(TMdCItemIds)
       
  1974     		+ aItems.Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId );
       
  1975     aSerializedItemIds.SetBufferL( resultBuf );
       
  1976 	CleanupStack::Pop( resultBuf );
       
  1977 	CleanupStack::Pop( buffer );
       
  1978 
       
  1979     iAsyncHandler->UpdateRequest( buffer, *resultBuf, aStatus );
       
  1980     }
       
  1981 
       
  1982 TInt CMdESessionImpl::DeserializeItemsL( RMdEDataBuffer& aSerializedItems, 
       
  1983 		RPointerArray<CMdEInstanceItem>& aItems )
       
  1984 	{
       
  1985 	CMdCSerializationBuffer* buffer = aSerializedItems.GetBufferLC();
       
  1986 
       
  1987 	const TMdCItemIds& itemIds = TMdCItemIds::GetFromBufferL( *buffer );
       
  1988 
       
  1989     TUint32 objectsIndex = 0;
       
  1990     TUint32 eventsIndex = 0;
       
  1991     TUint32 relationsIndex = 0;
       
  1992     TItemId id = 0;
       
  1993 
       
  1994     const TInt count = aItems.Count();
       
  1995     
       
  1996     if ( count != itemIds.iObjectIds.iPtr.iCount
       
  1997     		+ itemIds.iRelationIds.iPtr.iCount + itemIds.iEventIds.iPtr.iCount )
       
  1998     	{
       
  1999     	User::Leave( KErrArgument );
       
  2000     	}
       
  2001 
       
  2002     for ( TInt i = 0; i < count; ++i )
       
  2003     	{
       
  2004     	switch ( aItems[i]->InstanceType() )
       
  2005     		{
       
  2006     		case EMdETypeObject:
       
  2007     			buffer->PositionL( itemIds.iObjectIds.iPtr.iOffset
       
  2008 				        + objectsIndex * CMdCSerializationBuffer::KRequiredSizeForTItemId );
       
  2009 				buffer->ReceiveL( id );
       
  2010 				aItems[i]->SetId( id );
       
  2011 				aItems[i]->SetSession( *this );
       
  2012 				static_cast<CMdEObject*>(aItems[i])->ClearObject();
       
  2013 				++objectsIndex;
       
  2014     			break;
       
  2015 
       
  2016     		case EMdETypeEvent:
       
  2017     			buffer->PositionL( itemIds.iEventIds.iPtr.iOffset
       
  2018     					+ eventsIndex * CMdCSerializationBuffer::KRequiredSizeForTItemId );
       
  2019 				buffer->ReceiveL( id );
       
  2020 				aItems[i]->SetId( id );
       
  2021 				aItems[i]->SetSession( *this );
       
  2022 				++eventsIndex;
       
  2023     			break;
       
  2024 
       
  2025     		case EMdETypeRelation:
       
  2026     			buffer->PositionL( itemIds.iRelationIds.iPtr.iOffset
       
  2027     					+ relationsIndex * CMdCSerializationBuffer::KRequiredSizeForTItemId );
       
  2028     			buffer->ReceiveL( id );
       
  2029 				aItems[i]->SetId( id );
       
  2030 				aItems[i]->SetSession( *this );
       
  2031 				++relationsIndex;
       
  2032     			break;
       
  2033 
       
  2034     		default:
       
  2035     			User::Leave( KErrArgument );
       
  2036     			break;
       
  2037     		}
       
  2038     	}
       
  2039     
       
  2040     const TInt errorCode = itemIds.iErrorCode;
       
  2041     CleanupStack::PopAndDestroy( buffer );
       
  2042     
       
  2043     return errorCode;
       
  2044 	}
       
  2045 
       
  2046 TItemId CMdESessionImpl::AddEventL( CMdEEvent& aEvent )
       
  2047     {
       
  2048 	return AddItemL( aEvent );
       
  2049     }
       
  2050 
       
  2051 TItemId CMdESessionImpl::RemoveEventL( TItemId aId, 
       
  2052 		CMdENamespaceDef* aNamespaceDef )
       
  2053     {
       
  2054     RArray<TItemId> items;
       
  2055     CleanupClosePushL( items );
       
  2056     RArray<TItemId> successful;
       
  2057     CleanupClosePushL( successful );
       
  2058 
       
  2059     items.AppendL( aId );
       
  2060     User::LeaveIfError( RemoveEventsL( items, successful, aNamespaceDef ) );
       
  2061 
       
  2062     TItemId result = KNoId;
       
  2063     if ( successful.Count() > 0 )
       
  2064     	{
       
  2065     	result = successful[0];
       
  2066     	}
       
  2067 
       
  2068     CleanupStack::PopAndDestroy( &successful );
       
  2069     CleanupStack::PopAndDestroy( &items );
       
  2070 
       
  2071     return result;
       
  2072     }
       
  2073 
       
  2074 TInt CMdESessionImpl::RemoveEventsL( const RArray<TItemId>& aId, 
       
  2075 		RArray<TItemId>& aSuccessful, CMdENamespaceDef* aNamespaceDef )
       
  2076 	{
       
  2077 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  2078 
       
  2079 	if ( !aNamespaceDef )
       
  2080 		{
       
  2081 		namespaceDef = &GetDefaultNamespaceDefL();
       
  2082 		}
       
  2083 
       
  2084 	CMdCSerializationBuffer* buffer = RemoveCommonL( *namespaceDef, 
       
  2085 			(RArray<TItemId>*)NULL, &aId, NULL  );
       
  2086 	CleanupStack::PushL( buffer );
       
  2087 	CMdCSerializationBuffer* successfulBuffer = CMdCSerializationBuffer::NewLC(
       
  2088 			buffer->Size() );
       
  2089 	
       
  2090 	RMdEDataBuffer dataBuffer;
       
  2091 	dataBuffer.SetBufferL( successfulBuffer );
       
  2092 	CleanupStack::Pop( successfulBuffer );
       
  2093 	CleanupClosePushL( dataBuffer );
       
  2094 	
       
  2095     iSession.DoRemoveItemsL( *buffer, *successfulBuffer );
       
  2096     TInt firstItemError = DeserializeIdsL( dataBuffer, NULL, 
       
  2097     		&aSuccessful );
       
  2098 
       
  2099     CleanupStack::PopAndDestroy( &dataBuffer );
       
  2100     CleanupStack::PopAndDestroy( buffer );
       
  2101     
       
  2102     return firstItemError;
       
  2103 	}    
       
  2104 	
       
  2105 void CMdESessionImpl::RemoveEventsAsyncL( 
       
  2106 		const RArray<TItemId>& aId, TRequestStatus& aStatus, 
       
  2107 		RMdEDataBuffer& aSerializedEventIds, 
       
  2108 		CMdENamespaceDef* aNamespaceDef )
       
  2109 	{
       
  2110 	CMdENamespaceDef* namespaceDef = aNamespaceDef;
       
  2111 	
       
  2112 	if ( !aNamespaceDef )
       
  2113 		{
       
  2114 		namespaceDef = &GetDefaultNamespaceDefL();
       
  2115 		}
       
  2116 
       
  2117 	CMdCSerializationBuffer* buffer = RemoveCommonL( *namespaceDef, 
       
  2118 			(RArray<TItemId>*)NULL, &aId, NULL  );
       
  2119 	CleanupStack::PushL( buffer );
       
  2120 	CMdCSerializationBuffer* resultBuffer = CMdCSerializationBuffer::NewLC( 
       
  2121 			buffer->Size() );
       
  2122 	aSerializedEventIds.SetBufferL( resultBuffer );
       
  2123     CleanupStack::Pop( resultBuffer );    
       
  2124 	
       
  2125     CleanupStack::Pop( buffer );
       
  2126 
       
  2127     iAsyncHandler->RemoveRequest( buffer, *resultBuffer, aStatus );    
       
  2128 	}
       
  2129 	
       
  2130 
       
  2131 // Query 
       
  2132 
       
  2133 CMdEObjectQuery* CMdESessionImpl::NewObjectQueryL(
       
  2134 		CMdENamespaceDef& aNamespaceDef, CMdEObjectDef& aObjectDef, 
       
  2135 		MMdEQueryObserver* aObserver)
       
  2136     {
       
  2137     CMdEObjectQueryImpl* query = CMdEObjectQueryImpl::NewLC( *this, 
       
  2138     		aNamespaceDef, aObjectDef, NULL, iSession );
       
  2139     if( aObserver )
       
  2140         {
       
  2141         query->AddObserverL( *aObserver );
       
  2142         }
       
  2143     CleanupStack::Pop( query );
       
  2144 
       
  2145     query->SetQueryId( iNextQueryId );
       
  2146     iNextQueryId++;
       
  2147     
       
  2148     return query;
       
  2149     }
       
  2150 
       
  2151 CMdEObjectQuery* CMdESessionImpl::NewObjectQueryL(
       
  2152 		CMdEObjectDef& aObjectDef, RPointerArray<CMdEObjectDef>* aObjectDefs, 
       
  2153 		MMdEQueryObserver* aObserver)
       
  2154 	{
       
  2155 	CleanupStack::PushL( aObjectDefs );
       
  2156 
       
  2157 	if( !aObjectDefs || ( aObjectDefs->Count() <= 0 ) )
       
  2158 		{
       
  2159 		User::Leave( KErrArgument );
       
  2160 		}
       
  2161 	
       
  2162     CMdEObjectQueryImpl* query = CMdEObjectQueryImpl::NewLC( *this, 
       
  2163     		aObjectDef.NamespaceDef(), aObjectDef, aObjectDefs, iSession );
       
  2164     if( aObserver )
       
  2165         {
       
  2166         query->AddObserverL( *aObserver );
       
  2167         }
       
  2168     CleanupStack::Pop( query );
       
  2169 
       
  2170     CleanupStack::Pop( aObjectDefs );
       
  2171 
       
  2172     query->SetQueryId( iNextQueryId );
       
  2173     iNextQueryId++;
       
  2174     
       
  2175     return query;
       
  2176 	}
       
  2177 
       
  2178 CMdERelationQuery* CMdESessionImpl::NewRelationQueryL(
       
  2179 		CMdENamespaceDef& aNamespaceDef, MMdEQueryObserver* aObserver)
       
  2180     {
       
  2181     CMdERelationQueryImpl* query = CMdERelationQueryImpl::NewLC( *this,
       
  2182     		aNamespaceDef, iSession );
       
  2183 
       
  2184     if( aObserver )
       
  2185         {
       
  2186         query->AddObserverL( *aObserver );
       
  2187         }
       
  2188     CleanupStack::Pop( query );
       
  2189 
       
  2190     query->SetQueryId( iNextQueryId );
       
  2191     iNextQueryId++;
       
  2192     
       
  2193     return query;
       
  2194     }
       
  2195 
       
  2196 CMdEEventQuery* CMdESessionImpl::NewEventQueryL(
       
  2197 		CMdENamespaceDef& aNamespaceDef, MMdEQueryObserver* aObserver)
       
  2198     {
       
  2199     CMdEEventQueryImpl* query = CMdEEventQueryImpl::NewLC( *this,
       
  2200     		aNamespaceDef, iSession );
       
  2201 
       
  2202     if( aObserver )
       
  2203         {
       
  2204         query->AddObserverL( *aObserver );
       
  2205         }
       
  2206     CleanupStack::Pop( query );
       
  2207 
       
  2208     query->SetQueryId( iNextQueryId );
       
  2209     iNextQueryId++;
       
  2210     
       
  2211     return query;
       
  2212     }
       
  2213 
       
  2214 
       
  2215 // Observer handling
       
  2216 void CMdESessionImpl::AddObjectObserverL( MMdEObjectObserver& aObserver,
       
  2217     									  CMdELogicCondition* aCondition, 
       
  2218      									  TUint32 aNotificationType,
       
  2219     									  CMdENamespaceDef* aNamespaceDef )
       
  2220     {
       
  2221     CleanupStack::PushL( aCondition );
       
  2222     
       
  2223     // if condition is given, check that it is correct type
       
  2224 	if( aCondition && ( EConditionTypeLogic != aCondition->Type() ) )
       
  2225 		{
       
  2226 		User::Leave( KErrArgument );
       
  2227 		}
       
  2228 
       
  2229 	// if namespace is not given get default namespace definition
       
  2230     CMdENamespaceDef* namespaceDef = NULL;
       
  2231     if ( !aNamespaceDef )
       
  2232     	{
       
  2233     	namespaceDef = &GetDefaultNamespaceDefL();
       
  2234     	}
       
  2235     else
       
  2236     	{
       
  2237     	namespaceDef = aNamespaceDef;
       
  2238     	}
       
  2239 
       
  2240 	TUint32 type = 0;
       
  2241 	if ( aNotificationType & ENotifyAdd )
       
  2242 		{
       
  2243 		type |= EObjectNotifyAdd;
       
  2244 		}
       
  2245 	if ( aNotificationType & ENotifyModify )
       
  2246 		{
       
  2247 		type |= EObjectNotifyModify;
       
  2248 		}
       
  2249 	if ( aNotificationType & ENotifyRemove )
       
  2250 		{
       
  2251 		type |= EObjectNotifyRemove;
       
  2252 		}
       
  2253 
       
  2254     TInt err = FindNotifier( type, &aObserver, *namespaceDef );
       
  2255     
       
  2256     if ( err != KErrNotFound )
       
  2257     	{
       
  2258     	if ( err >= 0 )
       
  2259     		{
       
  2260     		err = KErrAlreadyExists;
       
  2261     		}
       
  2262     	User::LeaveIfError( err );
       
  2263     	}
       
  2264 
       
  2265     CMdENotifierAO* notifier = CMdENotifierAO::NewLC( *this, iSession );
       
  2266     notifier->RegisterL( type, &aObserver, aCondition, *namespaceDef );
       
  2267 
       
  2268     CleanupStack::Pop( notifier );
       
  2269     iNotifiers.Append( notifier );
       
  2270     
       
  2271     CleanupStack::PopAndDestroy( aCondition );
       
  2272     }
       
  2273     
       
  2274 void CMdESessionImpl::AddObjectPresentObserverL(
       
  2275 		MMdEObjectPresentObserver& aObserver)
       
  2276 	{
       
  2277     CMdENamespaceDef& namespaceDef = GetDefaultNamespaceDefL();
       
  2278 
       
  2279     TInt err = FindNotifier( 
       
  2280     		EObjectNotifyPresent, &aObserver, namespaceDef );
       
  2281     
       
  2282     if ( err != KErrNotFound )
       
  2283     	{
       
  2284     	if ( err >= 0 )
       
  2285     		{
       
  2286     		err = KErrAlreadyExists;
       
  2287     		}
       
  2288     	User::LeaveIfError( err );
       
  2289     	}
       
  2290 
       
  2291     CMdENotifierAO* notifier = CMdENotifierAO::NewLC( *this, iSession );
       
  2292     notifier->RegisterL( EObjectNotifyPresent | EObjectNotifyNotPresent,
       
  2293     		&aObserver, NULL, namespaceDef );
       
  2294 
       
  2295     CleanupStack::Pop( notifier );
       
  2296     iNotifiers.Append( notifier );
       
  2297 	}
       
  2298 
       
  2299 void CMdESessionImpl::AddRelationObserverL( MMdERelationObserver& aObserver,
       
  2300     										CMdECondition* aCondition, 
       
  2301     										TUint32 aNotificationType,
       
  2302     										CMdENamespaceDef* aNamespaceDef )
       
  2303     {
       
  2304     CleanupStack::PushL( aCondition );
       
  2305     
       
  2306     // if condition is given, check that it is correct type
       
  2307 	if( aCondition && ( EConditionTypeRelation != aCondition->Type() ) )
       
  2308 		{
       
  2309 		User::Leave( KErrArgument );
       
  2310 		}
       
  2311 
       
  2312 	// if namespace is not given get default namespace definition
       
  2313     CMdENamespaceDef* namespaceDef = NULL;
       
  2314     if ( !aNamespaceDef )
       
  2315     	{
       
  2316     	namespaceDef = &GetDefaultNamespaceDefL();
       
  2317     	}
       
  2318     else
       
  2319     	{
       
  2320     	namespaceDef = aNamespaceDef;
       
  2321     	}
       
  2322 
       
  2323 	TUint32 type = 0;
       
  2324 	if ( aNotificationType & ENotifyAdd )
       
  2325 		{
       
  2326 		type |= ERelationNotifyAdd;
       
  2327 		}
       
  2328 	if ( aNotificationType & ENotifyModify )
       
  2329 		{
       
  2330 		type |= ERelationNotifyModify;
       
  2331 		}
       
  2332 	if ( aNotificationType & ENotifyRemove )
       
  2333 		{
       
  2334 		type |= ERelationNotifyRemove;
       
  2335 		}
       
  2336 
       
  2337 	TInt err = FindNotifier( type, &aObserver, *namespaceDef );
       
  2338 
       
  2339     if ( err != KErrNotFound )
       
  2340     	{
       
  2341     	if ( err >= 0 )
       
  2342     		{
       
  2343     		err = KErrAlreadyExists;
       
  2344     		}
       
  2345     	User::LeaveIfError( err );
       
  2346     	}
       
  2347     
       
  2348     CMdENotifierAO* notifier = CMdENotifierAO::NewLC( *this, iSession );
       
  2349     notifier->RegisterL( type, &aObserver, aCondition, *namespaceDef );
       
  2350 
       
  2351     CleanupStack::Pop( notifier );
       
  2352     iNotifiers.Append( notifier );
       
  2353     
       
  2354     CleanupStack::PopAndDestroy( aCondition );
       
  2355     }
       
  2356 
       
  2357 void CMdESessionImpl::AddRelationItemObserverL( 
       
  2358 		MMdERelationItemObserver& aObserver, CMdECondition* aCondition, 
       
  2359 		TUint32 aNotificationType, CMdENamespaceDef* aNamespaceDef )
       
  2360 	{
       
  2361     CleanupStack::PushL( aCondition );
       
  2362     
       
  2363 	// if condition is given, check that it is correct type
       
  2364 	if( aCondition && ( EConditionTypeRelation != aCondition->Type() ) )
       
  2365 		{
       
  2366 		User::Leave( KErrArgument );
       
  2367 		}
       
  2368 	
       
  2369 	// if namespace is not given get default namespace definition
       
  2370 	CMdENamespaceDef* namespaceDef = NULL;
       
  2371 	if ( !aNamespaceDef )
       
  2372 		{
       
  2373 		namespaceDef = &GetDefaultNamespaceDefL();
       
  2374 		}
       
  2375 	else
       
  2376 		{
       
  2377 		namespaceDef = aNamespaceDef;
       
  2378 		}
       
  2379 	
       
  2380 	TUint32 type = 0;
       
  2381 	if ( aNotificationType & ENotifyAdd )
       
  2382 		{		
       
  2383 		User::Leave( KErrNotSupported );
       
  2384 		}
       
  2385 	if ( aNotificationType & ENotifyModify )
       
  2386 		{		
       
  2387 		User::Leave( KErrNotSupported );
       
  2388 		}
       
  2389 	if ( aNotificationType & ENotifyRemove )
       
  2390 		{
       
  2391 		type |= ERelationItemNotifyRemove;
       
  2392 		}
       
  2393 	
       
  2394 	TInt err = FindNotifier( type, &aObserver, *namespaceDef );
       
  2395 	
       
  2396 	if ( err != KErrNotFound )
       
  2397 		{
       
  2398 		if ( err >= 0 )
       
  2399 			{
       
  2400 			err = KErrAlreadyExists;
       
  2401 			}
       
  2402 		User::LeaveIfError( err );
       
  2403 		}
       
  2404 	
       
  2405 	CMdENotifierAO* notifier = CMdENotifierAO::NewLC( *this, iSession );
       
  2406 	notifier->RegisterL( type, &aObserver, aCondition, *namespaceDef );
       
  2407 	
       
  2408 	CleanupStack::Pop( notifier );
       
  2409 	iNotifiers.Append( notifier );
       
  2410 	
       
  2411     CleanupStack::PopAndDestroy( aCondition );
       
  2412 	}
       
  2413 
       
  2414 
       
  2415 void CMdESessionImpl::AddRelationPresentObserverL(
       
  2416 		MMdERelationPresentObserver& aObserver)
       
  2417 	{
       
  2418     CMdENamespaceDef& namespaceDef = GetDefaultNamespaceDefL();
       
  2419 
       
  2420     TInt err = FindNotifier( 
       
  2421     		ERelationNotifyPresent | ERelationNotifyNotPresent,
       
  2422     		&aObserver, namespaceDef );
       
  2423     
       
  2424     if ( err != KErrNotFound )
       
  2425     	{
       
  2426     	if ( err >= 0 )
       
  2427     		{
       
  2428     		err = KErrAlreadyExists;
       
  2429     		}
       
  2430     	User::LeaveIfError( err );
       
  2431     	}
       
  2432     
       
  2433     CMdENotifierAO* notifier = CMdENotifierAO::NewLC( *this, iSession );
       
  2434     notifier->RegisterL( ERelationNotifyPresent | ERelationNotifyNotPresent,
       
  2435     		&aObserver, NULL, namespaceDef );
       
  2436 
       
  2437     CleanupStack::Pop( notifier );
       
  2438     iNotifiers.Append( notifier );
       
  2439 	}
       
  2440 
       
  2441 void CMdESessionImpl::AddEventObserverL( MMdEEventObserver& aObserver,
       
  2442     									 CMdECondition* aCondition, 
       
  2443     									 TUint32 aNotificationType,
       
  2444     									 CMdENamespaceDef* aNamespaceDef )
       
  2445     {
       
  2446     CleanupStack::PushL( aCondition );
       
  2447     
       
  2448     // if condition is given, check that it is correct type
       
  2449 	if( aCondition && ( EConditionTypeEvent != aCondition->Type() ) )
       
  2450 		{
       
  2451 		User::Leave( KErrArgument );
       
  2452 		}
       
  2453 
       
  2454 	if ( aNotificationType & ENotifyModify )
       
  2455 		{
       
  2456 		User::Leave( KErrNotSupported );
       
  2457 		}
       
  2458 
       
  2459 	// if namespace is not given get default namespace definition
       
  2460     CMdENamespaceDef* namespaceDef = NULL;
       
  2461     if ( !aNamespaceDef )
       
  2462     	{
       
  2463     	namespaceDef = &GetDefaultNamespaceDefL();
       
  2464     	}
       
  2465     else
       
  2466     	{
       
  2467     	namespaceDef = aNamespaceDef;
       
  2468     	}
       
  2469 
       
  2470 	TUint32 type = 0;
       
  2471 	if ( aNotificationType & ENotifyAdd )
       
  2472 		{
       
  2473 		type |= EEventNotifyAdd;
       
  2474 		}
       
  2475 	if ( aNotificationType & ENotifyRemove )
       
  2476 		{
       
  2477 		type |= EEventNotifyRemove;
       
  2478 		}
       
  2479 	
       
  2480     TInt err = FindNotifier( type, &aObserver, *namespaceDef );
       
  2481     
       
  2482     if ( err != KErrNotFound )
       
  2483     	{
       
  2484     	if ( err >= 0 )
       
  2485     		{
       
  2486     		err = KErrAlreadyExists;
       
  2487     		}
       
  2488     	User::LeaveIfError( err );
       
  2489     	}
       
  2490 
       
  2491     CMdENotifierAO* notifier = CMdENotifierAO::NewLC( *this, iSession );
       
  2492     notifier->RegisterL( type, &aObserver, aCondition, *namespaceDef );
       
  2493 
       
  2494     CleanupStack::Pop( notifier );
       
  2495     iNotifiers.Append( notifier );
       
  2496     
       
  2497     CleanupStack::PopAndDestroy( aCondition );
       
  2498     }
       
  2499 
       
  2500 void CMdESessionImpl::RemoveObjectObserverL( 
       
  2501 		MMdEObjectObserver& aObserver, CMdENamespaceDef* aNamespaceDef )
       
  2502     {
       
  2503 	// if namespace is not given get default namespace definition
       
  2504     CMdENamespaceDef* namespaceDef = NULL;
       
  2505     if ( !aNamespaceDef )
       
  2506     	{
       
  2507     	namespaceDef = &GetDefaultNamespaceDefL();
       
  2508     	}
       
  2509     else
       
  2510     	{
       
  2511     	namespaceDef = aNamespaceDef;
       
  2512     	}
       
  2513 
       
  2514     TInt index = FindNotifier( 
       
  2515     		EObjectNotifyAdd | EObjectNotifyModify | EObjectNotifyRemove,
       
  2516     		&aObserver, *namespaceDef );
       
  2517     if ( index != KErrNotFound )
       
  2518         {
       
  2519     	iNotifiers[index]->Cancel();
       
  2520     	delete iNotifiers[index];
       
  2521     	iNotifiers[index] = NULL;
       
  2522     	iNotifiers.Remove( index );
       
  2523         }
       
  2524     else
       
  2525     	{
       
  2526     	User::Leave( KErrNotFound );
       
  2527     	}
       
  2528     }
       
  2529 
       
  2530 void CMdESessionImpl::RemoveObjectPresentObserverL(
       
  2531 		MMdEObjectPresentObserver& aObserver)
       
  2532 	{
       
  2533 	// if namespace is not given get default namespace definition
       
  2534     CMdENamespaceDef& namespaceDef = GetDefaultNamespaceDefL();
       
  2535 
       
  2536     TInt index = FindNotifier( EObjectNotifyPresent | EObjectNotifyNotPresent,
       
  2537     		&aObserver, namespaceDef );
       
  2538     if ( index != KErrNotFound )
       
  2539         {
       
  2540     	iNotifiers[index]->Cancel();
       
  2541     	delete iNotifiers[index];
       
  2542     	iNotifiers[index] = NULL;
       
  2543     	iNotifiers.Remove( index );
       
  2544         }
       
  2545     else
       
  2546     	{
       
  2547     	User::Leave( KErrNotFound );
       
  2548     	}
       
  2549 	}
       
  2550 
       
  2551 void CMdESessionImpl::RemoveRelationObserverL( 
       
  2552 		MMdERelationObserver& aObserver, CMdENamespaceDef* aNamespaceDef )
       
  2553     {
       
  2554 	// if namespace is not given get default namespace definition
       
  2555     CMdENamespaceDef* namespaceDef = NULL;
       
  2556     if ( !aNamespaceDef )
       
  2557     	{
       
  2558     	namespaceDef = &GetDefaultNamespaceDefL();
       
  2559     	}
       
  2560     else
       
  2561     	{
       
  2562     	namespaceDef = aNamespaceDef;
       
  2563     	}
       
  2564 
       
  2565     TInt index = FindNotifier( 
       
  2566     		ERelationNotifyAdd | ERelationNotifyModify | ERelationNotifyRemove,
       
  2567     		&aObserver, *namespaceDef );
       
  2568     if ( index != KErrNotFound )
       
  2569         {
       
  2570     	iNotifiers[index]->Cancel();
       
  2571     	delete iNotifiers[index];
       
  2572     	iNotifiers[index] = NULL;
       
  2573     	iNotifiers.Remove( index );
       
  2574         }
       
  2575     else
       
  2576     	{
       
  2577     	User::Leave( KErrNotFound );
       
  2578     	}
       
  2579    }
       
  2580 
       
  2581 void CMdESessionImpl::RemoveRelationItemObserverL( 
       
  2582 		MMdERelationItemObserver& aObserver, CMdENamespaceDef* aNamespaceDef )
       
  2583     {
       
  2584 	// if namespace is not given get default namespace definition
       
  2585     CMdENamespaceDef* namespaceDef = NULL;
       
  2586     if ( !aNamespaceDef )
       
  2587     	{
       
  2588     	namespaceDef = &GetDefaultNamespaceDefL();
       
  2589     	}
       
  2590     else
       
  2591     	{
       
  2592     	namespaceDef = aNamespaceDef;
       
  2593     	}
       
  2594 
       
  2595     TInt index = FindNotifier( 
       
  2596     		/*ERelationItemNotifyAdd | ERelationItemNotifyModify |*/ 
       
  2597     		ERelationItemNotifyRemove,
       
  2598     		&aObserver, *namespaceDef );
       
  2599     if ( index != KErrNotFound )
       
  2600         {
       
  2601     	iNotifiers[index]->Cancel();
       
  2602     	delete iNotifiers[index];
       
  2603     	iNotifiers[index] = NULL;
       
  2604     	iNotifiers.Remove( index );
       
  2605         }
       
  2606     else
       
  2607     	{
       
  2608     	User::Leave( KErrNotFound );
       
  2609     	}
       
  2610    }
       
  2611 
       
  2612 void CMdESessionImpl::RemoveRelationPresentObserverL(
       
  2613 		MMdERelationPresentObserver& aObserver)
       
  2614 	{
       
  2615 	// if namespace is not given get default namespace definition
       
  2616     CMdENamespaceDef& namespaceDef = GetDefaultNamespaceDefL();
       
  2617 
       
  2618     TInt index = FindNotifier( 
       
  2619     		ERelationNotifyPresent | ERelationNotifyNotPresent,
       
  2620     		&aObserver, namespaceDef );
       
  2621     if ( index != KErrNotFound )
       
  2622         {
       
  2623     	iNotifiers[index]->Cancel();
       
  2624     	delete iNotifiers[index];
       
  2625     	iNotifiers[index] = NULL;
       
  2626     	iNotifiers.Remove( index );
       
  2627         }
       
  2628     else
       
  2629     	{
       
  2630     	User::Leave( KErrNotFound );
       
  2631     	}
       
  2632 	}
       
  2633 
       
  2634 void CMdESessionImpl::RemoveEventObserverL( 
       
  2635 		MMdEEventObserver& aObserver, CMdENamespaceDef* aNamespaceDef )
       
  2636     {
       
  2637 	// if namespace is not given get default namespace definition
       
  2638     CMdENamespaceDef* namespaceDef = NULL;
       
  2639     if ( !aNamespaceDef )
       
  2640     	{
       
  2641     	namespaceDef = &GetDefaultNamespaceDefL();
       
  2642     	}
       
  2643     else
       
  2644     	{
       
  2645     	namespaceDef = aNamespaceDef;
       
  2646     	}
       
  2647     
       
  2648     TInt index = FindNotifier( EEventNotifyAdd | EEventNotifyRemove,
       
  2649     		&aObserver, *namespaceDef );
       
  2650     if ( index != KErrNotFound )
       
  2651         {
       
  2652 	    iNotifiers[index]->Cancel();
       
  2653 	    delete iNotifiers[index];
       
  2654 	    iNotifiers[index] = NULL;
       
  2655 	    iNotifiers.Remove( index );
       
  2656         }
       
  2657     else
       
  2658     	{
       
  2659     	User::Leave( KErrNotFound );
       
  2660     	}
       
  2661     }
       
  2662 
       
  2663 TInt CMdESessionImpl::FindNotifier( TUint32 aNotifyType, TAny* aObserver, 
       
  2664 									CMdENamespaceDef& aNamespaceDef )
       
  2665     {
       
  2666     const TInt notifiersCount = iNotifiers.Count();
       
  2667     for( TInt i = 0; i < notifiersCount; ++i )
       
  2668         {
       
  2669         if ( iNotifiers[i]->Match( aNotifyType, aObserver, aNamespaceDef ) )
       
  2670         	{
       
  2671         	return i;
       
  2672         	}
       
  2673         }
       
  2674     return KErrNotFound;
       
  2675     }
       
  2676 
       
  2677 void CMdESessionImpl::NotifierInError( CMdENotifierAO* aNotifier )
       
  2678     {
       
  2679     TInt index = iNotifiers.Find( aNotifier );
       
  2680     delete aNotifier;
       
  2681     iNotifiers.Remove( index );
       
  2682     }
       
  2683 
       
  2684 void CMdESessionImpl::ImportSchemaL( const TDesC& aFileName )
       
  2685     {
       
  2686     iSession.DoImportSchemaL( aFileName );
       
  2687     DoLoadSchemaL();  
       
  2688     }
       
  2689     
       
  2690 TInt CMdESessionImpl::ImportMetadataL( const TDesC& aFileName )
       
  2691     {
       
  2692     return iSession.DoImportMetadataL( aFileName );
       
  2693     }
       
  2694 
       
  2695 void CMdESessionImpl::ImportMetadata( const TDesC& aFileName, 
       
  2696 		TPckgBuf<TInt>& aResult, TRequestStatus& aStatus )
       
  2697 	{
       
  2698 	return iSession.DoImportMetadata( aFileName, aResult, aStatus );
       
  2699 	}
       
  2700 
       
  2701 CMdCSerializationBuffer* CMdESessionImpl::ExportCommonL( 
       
  2702 		const CMdENamespaceDef* aNamespaceDef,
       
  2703 		const RPointerArray<CMdEObjectDef>* aObjectDefs,
       
  2704         const RPointerArray<CMdERelationDef>* aRelationDefs,
       
  2705         const RPointerArray<CMdEEventDef>* aEventDefs )
       
  2706 	{
       
  2707 	// headerSize
       
  2708 	TUint32 bufferSize = sizeof(TMdCItemIds);
       
  2709 	
       
  2710 	TMdCItemIds itemIds;
       
  2711 	if ( aNamespaceDef )
       
  2712 		{
       
  2713 		itemIds.iNamespaceDefId = aNamespaceDef->Id();
       
  2714 		}
       
  2715 	else
       
  2716 		{
       
  2717 		itemIds.iNamespaceDefId = KNoDefId;
       
  2718 		}
       
  2719 	itemIds.iObjectUris.iPtr.iCount = 0;
       
  2720 	itemIds.iObjectUris.iPtr.iOffset = KNoOffset;
       
  2721 	itemIds.iObjectIds.iPtr.iCount = 0;
       
  2722 	itemIds.iObjectIds.iPtr.iOffset = KNoOffset;
       
  2723 	itemIds.iEventIds.iPtr.iCount = 0;
       
  2724 	itemIds.iEventIds.iPtr.iOffset = KNoOffset;
       
  2725 	itemIds.iRelationIds.iPtr.iCount = 0;
       
  2726 	itemIds.iRelationIds.iPtr.iOffset = KNoOffset;
       
  2727 
       
  2728 	if ( !aNamespaceDef || (!aObjectDefs && !aRelationDefs && !aEventDefs) )
       
  2729 		{
       
  2730 		CMdCSerializationBuffer* buffer = 
       
  2731 				CMdCSerializationBuffer::NewLC( bufferSize );
       
  2732 		itemIds.SerializeL( *buffer );
       
  2733 		CleanupStack::Pop( buffer );
       
  2734 		return buffer;
       
  2735 		}
       
  2736 
       
  2737 	if ( aObjectDefs )
       
  2738 		{
       
  2739 		bufferSize += aObjectDefs->Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  2740 		}
       
  2741 	if ( aEventDefs )
       
  2742 		{
       
  2743 		bufferSize += aEventDefs->Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  2744 		}
       
  2745 	if ( aRelationDefs )
       
  2746 		{
       
  2747 		bufferSize += aRelationDefs->Count() * CMdCSerializationBuffer::KRequiredSizeForTItemId;
       
  2748 		}
       
  2749 
       
  2750 	CMdCSerializationBuffer* buffer = 
       
  2751 			CMdCSerializationBuffer::NewLC( bufferSize );
       
  2752 
       
  2753 	buffer->PositionL( sizeof(TMdCItemIds) );
       
  2754 
       
  2755 	// insert objects
       
  2756 	if ( aObjectDefs )
       
  2757 		{
       
  2758 		itemIds.iObjectIds.iPtr.iOffset = buffer->Position();
       
  2759 		itemIds.iObjectIds.iPtr.iCount = aObjectDefs->Count();
       
  2760 		
       
  2761 		for ( TInt i = 0; i < itemIds.iObjectIds.iPtr.iCount; ++i )
       
  2762 			{
       
  2763 			buffer->InsertL( (*aObjectDefs)[i]->Id() );
       
  2764 			}
       
  2765 		}
       
  2766 
       
  2767 	// insert events
       
  2768 	if ( aEventDefs )
       
  2769 		{
       
  2770 		itemIds.iEventIds.iPtr.iOffset = buffer->Position();
       
  2771 		itemIds.iEventIds.iPtr.iCount = aEventDefs->Count();
       
  2772 
       
  2773 		for ( TInt i = 0; i < itemIds.iEventIds.iPtr.iCount; ++i )
       
  2774 			{
       
  2775 			buffer->InsertL( (*aEventDefs)[i]->Id() );
       
  2776 			}
       
  2777 		}
       
  2778 
       
  2779 	// insert relations
       
  2780 	if ( aRelationDefs )
       
  2781 		{
       
  2782 		itemIds.iRelationIds.iPtr.iOffset = buffer->Position();
       
  2783 		itemIds.iRelationIds.iPtr.iCount = aRelationDefs->Count();
       
  2784 
       
  2785 		for ( TInt i = 0; i < itemIds.iRelationIds.iPtr.iCount; ++i )
       
  2786 			{
       
  2787 			buffer->InsertL( (*aRelationDefs)[i]->Id() );
       
  2788 			}
       
  2789 		}
       
  2790 
       
  2791 	// set up header correctly
       
  2792 	buffer->PositionL( KNoOffset );
       
  2793 	itemIds.SerializeL( *buffer );
       
  2794 
       
  2795 	CleanupStack::Pop( buffer );
       
  2796 	return buffer;
       
  2797 	}
       
  2798 
       
  2799 
       
  2800 void CMdESessionImpl::ExportMetadataL( const TDesC& aFileName,
       
  2801     	const CMdENamespaceDef* aNamespaceDef,
       
  2802 	    const RPointerArray<CMdEObjectDef>* aObjectDefs,
       
  2803         const RPointerArray<CMdERelationDef>* aRelationDefs,
       
  2804         const RPointerArray<CMdEEventDef>* aEventDefs )
       
  2805     {
       
  2806 	CMdCSerializationBuffer* buffer = ExportCommonL( 
       
  2807 			aNamespaceDef, aObjectDefs, aRelationDefs, aEventDefs );
       
  2808 	CleanupStack::PushL( buffer );
       
  2809 
       
  2810     // Export
       
  2811     iSession.DoExportMetadataL( aFileName, *buffer );
       
  2812        
       
  2813     // Cleanup
       
  2814 	CleanupStack::PopAndDestroy( buffer );
       
  2815     }
       
  2816 
       
  2817 void CMdESessionImpl::ExportMetadataL( const TDesC& aFileName, 
       
  2818 		TRequestStatus& aStatus, RMdEDataBuffer& aBuffer, 
       
  2819         const CMdENamespaceDef* aNamespaceDef, 
       
  2820 	    const RPointerArray<CMdEObjectDef>* aObjectDefs, 
       
  2821         const RPointerArray<CMdERelationDef>* aRelationDefs, 
       
  2822         const RPointerArray<CMdEEventDef>* aEventDefs )
       
  2823     {
       
  2824 	CMdCSerializationBuffer* buffer = ExportCommonL( 
       
  2825 			aNamespaceDef, aObjectDefs, aRelationDefs, aEventDefs );
       
  2826 	CleanupStack::PushL( buffer );
       
  2827 
       
  2828     // Export
       
  2829     iSession.DoExportMetadataL( aFileName, *buffer, aStatus );
       
  2830 
       
  2831     aBuffer.SetBufferL( buffer );
       
  2832     CleanupStack::Pop( buffer );
       
  2833     }
       
  2834 
       
  2835 void CMdESessionImpl::GetSchemaVersionL(
       
  2836 		TInt& aMajorVersion, TInt& aMinorVersion)
       
  2837 	{
       
  2838 	return iSession.DoGetSchemaVersionL( aMajorVersion, aMinorVersion );
       
  2839 	}
       
  2840 
       
  2841 void CMdESessionImpl::SetObjectToPresentByGuidL( 
       
  2842 		const TInt64& aGuidHigh, const TInt64& aGuidLow )
       
  2843 	{
       
  2844 	return iSession.DoSetObjectToPresentByGuidL( aGuidHigh, aGuidLow );
       
  2845 	}
       
  2846 
       
  2847 void CMdESessionImpl::CheckOpened() const
       
  2848 	{
       
  2849 	__ASSERT_ALWAYS(iSessionState == EMdESessionOpen,
       
  2850 				    TMdEPanic::Panic(TMdEPanic::ESessionOpenInProgress));
       
  2851 	}
       
  2852 
       
  2853 void CMdESessionImpl::GetCountL( CMdCSerializationBuffer* aBuffer, 
       
  2854 		TUint32& aResult )
       
  2855 	{
       
  2856 	const TMdCItemCounts& itemCounts = TMdCItemCounts::GetFromBufferL( *aBuffer );
       
  2857 
       
  2858 	if( itemCounts.iObjects ) 
       
  2859 	    {
       
  2860 	    aResult = itemCounts.iObjects;
       
  2861 	    __ASSERT_DEBUG( ( itemCounts.iEvents == 0 ) && ( itemCounts.iRelations == 0 ), MMdCCommon::Panic( KErrCorrupt ) );
       
  2862         }
       
  2863 	
       
  2864 	if( itemCounts.iEvents ) 
       
  2865 	    {
       
  2866 	    aResult = itemCounts.iEvents;
       
  2867 	    __ASSERT_DEBUG( ( itemCounts.iObjects == 0 ) && ( itemCounts.iRelations == 0 ), MMdCCommon::Panic( KErrCorrupt ) );
       
  2868 	    }
       
  2869 
       
  2870 	if( itemCounts.iRelations ) 
       
  2871 	    {
       
  2872 	    aResult = itemCounts.iRelations;
       
  2873 	    __ASSERT_DEBUG( ( itemCounts.iObjects == 0 ) && ( itemCounts.iEvents == 0 ), MMdCCommon::Panic( KErrCorrupt ) );
       
  2874 	    }
       
  2875 	}
       
  2876 
       
  2877 void CMdESessionImpl::GetItemIdL( CMdCSerializationBuffer* aBuffer, 
       
  2878 		RArray<TItemId>& aIdArray )
       
  2879 	{
       
  2880 	const TMdCItemIds& itemIds = TMdCItemIds::GetFromBufferL( *aBuffer );
       
  2881 
       
  2882 	if( itemIds.iObjectIds.iPtr.iCount > 0 ) 
       
  2883 	    {
       
  2884 	    aBuffer->PositionL( itemIds.iObjectIds.iPtr.iOffset );
       
  2885 	    }
       
  2886 	
       
  2887 	if( itemIds.iRelationIds.iPtr.iCount > 0 ) 
       
  2888 	    {
       
  2889 	    aBuffer->PositionL( itemIds.iRelationIds.iPtr.iOffset );
       
  2890 	    }
       
  2891 
       
  2892 	if( itemIds.iEventIds.iPtr.iCount > 0 ) 
       
  2893 	    {
       
  2894 	    aBuffer->PositionL( itemIds.iEventIds.iPtr.iOffset );
       
  2895 	    }	
       
  2896 
       
  2897 	const TInt count = itemIds.iObjectIds.iPtr.iCount + itemIds.iRelationIds.iPtr.iCount
       
  2898 		+ itemIds.iEventIds.iPtr.iCount;
       
  2899     aIdArray.ReserveL( count );
       
  2900 	for( TUint32 i = 0; i < count; i++ )
       
  2901 	    {
       
  2902 	    TItemId id;
       
  2903 	    aBuffer->ReceiveL( id );
       
  2904 	    aIdArray.AppendL( id );
       
  2905 	    }
       
  2906 	}
       
  2907 
       
  2908 void CMdESessionImpl::GetDistinctValuesL( CMdCSerializationBuffer& aBuffer, 
       
  2909 		CDesCArray& aResults )
       
  2910 	{
       
  2911 	const TMdCItemIds& itemIds = TMdCItemIds::GetFromBufferL( aBuffer );
       
  2912 	aBuffer.PositionL( itemIds.iObjectUris.iPtr.iOffset );
       
  2913 	for ( TUint32 i = 0; i < itemIds.iObjectUris.iPtr.iCount; ++i )
       
  2914 		{
       
  2915 		TPtrC16 value = aBuffer.ReceivePtr16L();
       
  2916 		aResults.AppendL( value );
       
  2917 		}
       
  2918 	}
       
  2919 
       
  2920 CMdENamespaceDef* CMdESessionImpl::GetNamespaceDefL( 
       
  2921 		CMdENamespaceDef* aNamespaceDef )
       
  2922 	{
       
  2923     if ( aNamespaceDef )
       
  2924     	{
       
  2925     	return aNamespaceDef;
       
  2926     	}
       
  2927     else
       
  2928     	{
       
  2929     	return &GetDefaultNamespaceDefL();
       
  2930     	}
       
  2931 	}