metadataengine/client/src/mdenotifierao.cpp
changeset 0 c53acadfccc6
child 14 646a02f170b9
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Notifier client side active object
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "mdenotifierao.h"
       
    21 
       
    22 #include "mdcresult.h"
       
    23 #include "mdeenginesession.h"
       
    24 #include "mdesessionimpl.h"
       
    25 #include "mdenamespacedef.h"
       
    26 #include "mdcserializationbuffer.h"
       
    27 #include "mdccommon.pan"
       
    28 
       
    29 // ========================= MEMBER FUNCTIONS ==================================
       
    30 
       
    31 CMdENotifierAO* CMdENotifierAO::NewL(
       
    32     CMdESessionImpl& aSessionImpl, RMdEEngineSession& aSession )
       
    33     {
       
    34     CMdENotifierAO* self = CMdENotifierAO::NewLC( aSessionImpl, aSession );
       
    35     CleanupStack::Pop( self );
       
    36     return self;
       
    37     }
       
    38 
       
    39 
       
    40 CMdENotifierAO* CMdENotifierAO::NewLC(
       
    41     CMdESessionImpl& aSessionImpl, RMdEEngineSession& aSession )
       
    42     {
       
    43     CMdENotifierAO* self =
       
    44         new ( ELeave ) CMdENotifierAO( aSessionImpl, aSession );
       
    45     CleanupStack::PushL( self );
       
    46     self->ConstructL();
       
    47     return self;
       
    48     }
       
    49 
       
    50 
       
    51 CMdENotifierAO::CMdENotifierAO(
       
    52     CMdESessionImpl& aSessionImpl, RMdEEngineSession& aSession )
       
    53     : CActive( CActive::EPriorityUserInput )
       
    54     , iSessionImpl( aSessionImpl )
       
    55     , iSession( aSession )
       
    56     {
       
    57     CActiveScheduler::Add( this );
       
    58     }
       
    59 
       
    60 void CMdENotifierAO::ConstructL()
       
    61     {
       
    62     }
       
    63 
       
    64 
       
    65 CMdENotifierAO::~CMdENotifierAO()
       
    66     {
       
    67     Cancel(); // Causes call to DoCancel()
       
    68     delete iDataBuffer;
       
    69     iIdArray.Close();
       
    70     iRelationItemArray.Close();
       
    71     }
       
    72 
       
    73 
       
    74 void CMdENotifierAO::RegisterL( TUint32 aType, TAny* aObserver,
       
    75     							CMdECondition* aCondition, CMdENamespaceDef& aNamespaceDef )
       
    76     {
       
    77     iObserver = aObserver;
       
    78     iType = aType;
       
    79     iNamespaceDefId = aNamespaceDef.Id();
       
    80 
       
    81 	CMdCSerializationBuffer* buffer = NULL;
       
    82 
       
    83 	if( aCondition )
       
    84 		{
       
    85    		buffer = CMdCSerializationBuffer::NewLC( aCondition->RequiredBufferSize() );
       
    86 
       
    87    		// only needed, because method needs it as parameter
       
    88    		TUint32 freespaceOffset = 0;
       
    89 
       
    90    		aCondition->SerializeL( *buffer, freespaceOffset );
       
    91 		}
       
    92 	else
       
    93 		{
       
    94 		// create empty serialized condition buffer
       
    95 		buffer = CMdCSerializationBuffer::NewLC( 0 );
       
    96 		}
       
    97 
       
    98    	iSession.DoRegisterL( Id(), iType, *buffer, iNamespaceDefId );
       
    99 	CleanupStack::PopAndDestroy( buffer );
       
   100 
       
   101     // listen for first event
       
   102     iSession.DoListen( Id(), &iResultSize, iStatus );
       
   103     SetActive();
       
   104     }
       
   105 
       
   106 TBool CMdENotifierAO::Match( TUint32 aType, TAny* aObserver, CMdENamespaceDef& aNamespaceDef )
       
   107     {
       
   108     if( aNamespaceDef.Id() != iNamespaceDefId )
       
   109     	{
       
   110     	return EFalse;
       
   111     	}
       
   112 
       
   113     if ( iObserver != aObserver )
       
   114     	{
       
   115     	return EFalse;
       
   116     	}
       
   117     
       
   118     return ( iType & aType );
       
   119     }
       
   120 
       
   121 TInt CMdENotifierAO::Id()
       
   122     {
       
   123     return (TInt)this;
       
   124     }
       
   125 
       
   126 void CMdENotifierAO::DoCancel()
       
   127     {
       
   128     TRAP_IGNORE( iSession.DoUnregisterL( Id() ) );
       
   129     // the current pending call will return with KErrCancel
       
   130     }
       
   131 
       
   132 void CMdENotifierAO::RunL()
       
   133     {
       
   134     const TInt status = iStatus.Int();
       
   135 
       
   136     if ( status >= KErrNone )
       
   137     	{
       
   138 	    if ( status & ( EObjectNotifyAdd | EObjectNotifyModify | EObjectNotifyRemove
       
   139 	    				| EObjectNotifyPresent | EObjectNotifyNotPresent
       
   140 	    				| ERelationNotifyAdd | ERelationNotifyModify | ERelationNotifyRemove
       
   141 	    				| ERelationNotifyPresent | ERelationNotifyNotPresent
       
   142 	    				| EEventNotifyAdd | EEventNotifyRemove ) )
       
   143 	    	{
       
   144 	    	if( !iDataBuffer )
       
   145 	    		{
       
   146 	    		iDataBuffer = CMdCSerializationBuffer::NewL( iResultSize() );
       
   147 	    		}
       
   148 	    	else if( iDataBuffer->Buffer().MaxSize() < iResultSize() )
       
   149 	    		{
       
   150 	    		delete iDataBuffer;
       
   151 	    		iDataBuffer = NULL;
       
   152 	    		iDataBuffer = CMdCSerializationBuffer::NewL( iResultSize() );
       
   153 	    		}
       
   154 	    	
       
   155 	    	if( iResultSize() )
       
   156 	    		{
       
   157 	        	iSession.DoGetDataL( *iDataBuffer, Id() ); // reads ids to the buffer
       
   158 	        	DecodeIdBufferL(); // decodes ids from the data buffer and puts them to iIdArray
       
   159 	        	
       
   160 	        	delete iDataBuffer;
       
   161 	        	iDataBuffer = NULL;
       
   162 	        	
       
   163 	            DoNotifyObserver(); // notifies the observer about the event with an array of ids
       
   164 	    		}
       
   165 	        iSession.DoListen( Id(), &iResultSize, iStatus );  // continue listening for events
       
   166 	        SetActive();
       
   167 	        }
       
   168 	    else if ( status & ( /*ERelationItemNotifyAdd | ERelationItemNotifyModify
       
   169 	                         |*/ ERelationItemNotifyRemove ) ) // a relation was removed
       
   170 	    	{
       
   171 	    	if( !iDataBuffer )
       
   172 	    		{
       
   173 	    		iDataBuffer = CMdCSerializationBuffer::NewL(iResultSize());
       
   174 	    		}
       
   175 	    	else if( iDataBuffer->Size() < (TUint32)iResultSize() )
       
   176 	    		{
       
   177 	    		delete iDataBuffer;
       
   178 	    		iDataBuffer = NULL;
       
   179 	    		iDataBuffer = CMdCSerializationBuffer::NewL(iResultSize());
       
   180 	    		}
       
   181 	    	
       
   182 	    	if(iResultSize())
       
   183 	    		{
       
   184 	        	iSession.DoGetDataL( *iDataBuffer, Id() ); // reads ids to the buffer
       
   185 	        	DecodeRelationItemBufferL(); // decodes ids from the data buffer and puts them to iIdArray
       
   186 	        	
       
   187 	        	delete iDataBuffer;
       
   188 	        	iDataBuffer = NULL;
       
   189 	        	
       
   190 	            DoNotifyObserver(); // notifies the observer about the event with an array of ids
       
   191 	    		}
       
   192 	        iSession.DoListen( Id(), &iResultSize, iStatus ); // continue listening for events
       
   193 	        SetActive();
       
   194 	        }
       
   195 	    else if ( status == ESchemaModify )  // schema has been modified
       
   196 	    	{
       
   197 	        DoNotifyObserver(); // notifies the observer about the event with an array of ids
       
   198 	        iSession.DoListen( Id(), &iResultSize, iStatus ); // continue listening for events
       
   199 	        SetActive();
       
   200 	    	}
       
   201     	}
       
   202     else
       
   203         {
       
   204         // error in notifier mechanism
       
   205         if( status != KErrServerTerminated )
       
   206         	{        
       
   207         	iSession.DoUnregisterL( Id() ); // no response expected
       
   208         	}
       
   209         else
       
   210         	{
       
   211         	iSessionImpl.NotifyError( status );
       
   212         	}
       
   213         iSessionImpl.NotifierInError( this );
       
   214         }
       
   215     }
       
   216 
       
   217 TInt CMdENotifierAO::RunError(TInt aError)
       
   218     {
       
   219     if( aError == KErrServerTerminated )
       
   220     	{
       
   221     	iSessionImpl.NotifyError( aError );
       
   222     	}
       
   223     
       
   224     // notifying observer failed - continue listening anyway.
       
   225     iSessionImpl.NotifierInError( this );
       
   226     return KErrNone;
       
   227     }
       
   228 
       
   229 void CMdENotifierAO::DoNotifyObserver()
       
   230     {
       
   231     if( !iObserver )
       
   232         {
       
   233         return;
       
   234         }
       
   235     const TInt status = iStatus.Int();
       
   236     switch( iType & status )
       
   237         {
       
   238         case EObjectNotifyAdd:
       
   239         	{
       
   240         	MMdEObjectObserver* obs = static_cast<MMdEObjectObserver*>( iObserver );
       
   241         	obs->HandleObjectNotification( iSessionImpl, ENotifyAdd, iIdArray );
       
   242             iIdArray.Reset();            
       
   243         	break;
       
   244         	}
       
   245         case EObjectNotifyModify:
       
   246         	{
       
   247         	MMdEObjectObserver* obs = static_cast<MMdEObjectObserver*>( iObserver );
       
   248         	obs->HandleObjectNotification( iSessionImpl, ENotifyModify, iIdArray );
       
   249             iIdArray.Reset();            
       
   250         	break;
       
   251         	}
       
   252         case EObjectNotifyRemove:
       
   253         	{
       
   254         	MMdEObjectObserver* obs = static_cast<MMdEObjectObserver*>( iObserver );
       
   255         	obs->HandleObjectNotification( iSessionImpl, ENotifyRemove, iIdArray );
       
   256             iIdArray.Reset();            
       
   257         	break;
       
   258         	}
       
   259 
       
   260         case EObjectNotifyPresent:
       
   261         	{
       
   262 	    	MMdEObjectPresentObserver* obs = static_cast<MMdEObjectPresentObserver*>( iObserver );
       
   263             obs->HandleObjectPresentNotification( iSessionImpl, ETrue, iIdArray );
       
   264             iIdArray.Reset();            
       
   265 	    	break;
       
   266         	}
       
   267         case EObjectNotifyNotPresent:
       
   268         	{
       
   269 	    	MMdEObjectPresentObserver* obs = static_cast<MMdEObjectPresentObserver*>( iObserver );
       
   270             obs->HandleObjectPresentNotification( iSessionImpl, EFalse, iIdArray );
       
   271             iIdArray.Reset();            
       
   272 	    	break;
       
   273         	}
       
   274     	
       
   275         case ERelationNotifyAdd:
       
   276         	{
       
   277 	        MMdERelationObserver* obs = static_cast<MMdERelationObserver*>( iObserver );
       
   278             obs->HandleRelationNotification( iSessionImpl, ENotifyAdd, iIdArray );
       
   279             iIdArray.Reset();            
       
   280 	        break;
       
   281         	}
       
   282         case ERelationNotifyModify:
       
   283         	{
       
   284 	        MMdERelationObserver* obs = static_cast<MMdERelationObserver*>( iObserver );
       
   285             obs->HandleRelationNotification( iSessionImpl, ENotifyModify, iIdArray );
       
   286             iIdArray.Reset();            
       
   287 	        break;
       
   288         	}
       
   289         case ERelationNotifyRemove:
       
   290         	{
       
   291 	        MMdERelationObserver* obs = static_cast<MMdERelationObserver*>( iObserver );
       
   292             obs->HandleRelationNotification( iSessionImpl, ENotifyRemove, iIdArray );
       
   293             iIdArray.Reset();            
       
   294 	        break;
       
   295         	}
       
   296 
       
   297         case ERelationNotifyPresent:
       
   298         	{
       
   299 	    	MMdERelationPresentObserver* obs = static_cast<MMdERelationPresentObserver*>( iObserver );
       
   300             obs->HandleRelationPresentNotification( iSessionImpl, ETrue, iIdArray );
       
   301             iIdArray.Reset();            
       
   302 	    	break;
       
   303         	}
       
   304         case ERelationNotifyNotPresent:
       
   305         	{
       
   306 	    	MMdERelationPresentObserver* obs = static_cast<MMdERelationPresentObserver*>( iObserver );
       
   307             obs->HandleRelationPresentNotification( iSessionImpl, EFalse, iIdArray );
       
   308             iIdArray.Reset();            
       
   309 	    	break;
       
   310         	}
       
   311         	
       
   312         case ERelationItemNotifyRemove:
       
   313         	{
       
   314         	MMdERelationItemObserver* obs = static_cast<MMdERelationItemObserver*>( iObserver );
       
   315             obs->HandleRelationItemNotification( iSessionImpl, ENotifyRemove, iRelationItemArray );
       
   316             iRelationItemArray.Reset();            
       
   317         	break;
       
   318         	}
       
   319 
       
   320         case EEventNotifyAdd:
       
   321         	{
       
   322             MMdEEventObserver* obs = static_cast<MMdEEventObserver*>( iObserver );
       
   323             obs->HandleEventNotification( iSessionImpl, ENotifyAdd, iIdArray);
       
   324             iIdArray.Reset();            
       
   325             break;
       
   326         	}
       
   327         case EEventNotifyRemove:
       
   328         	{
       
   329             MMdEEventObserver* obs = static_cast<MMdEEventObserver*>( iObserver );
       
   330             obs->HandleEventNotification( iSessionImpl, ENotifyRemove, iIdArray);
       
   331             iIdArray.Reset();            
       
   332             break;
       
   333         	}
       
   334     	
       
   335         case ESchemaModify:
       
   336         	{
       
   337             MMdESchemaObserver* obs = static_cast<MMdESchemaObserver*>( iObserver );
       
   338             obs->HandleSchemaModified();
       
   339         	break;
       
   340         	}
       
   341 
       
   342         default:
       
   343         	// no observer to call - this should be skipped on server side!
       
   344         	break;
       
   345         }
       
   346     }
       
   347 
       
   348 void CMdENotifierAO::DecodeIdBufferL()
       
   349 	{
       
   350 	// IDs are always stored in object IDs, 
       
   351 	// even if those are actually relation or event IDs
       
   352 	
       
   353 	iIdArray.Reset();
       
   354 	iDataBuffer->PositionL( KNoOffset );
       
   355 	const TMdCItemIds& itemIds = TMdCItemIds::GetFromBufferL( *iDataBuffer );
       
   356 	__ASSERT_DEBUG( iNamespaceDefId == itemIds.iNamespaceDefId, User::Panic( _L("Incorrect namespaceDef from returned items!"), KErrCorrupt ) );
       
   357 
       
   358     iDataBuffer->PositionL( itemIds.iObjectIds.iPtr.iOffset );
       
   359 	for( TUint32 i = 0; i < itemIds.iObjectIds.iPtr.iCount; ++i )
       
   360 		{
       
   361 		TItemId id;
       
   362 		iDataBuffer->ReceiveL( id );
       
   363 		iIdArray.AppendL( id );
       
   364 		}
       
   365 	}
       
   366 
       
   367 void CMdENotifierAO::DecodeRelationItemBufferL()
       
   368 	{
       
   369     iRelationItemArray.Reset();
       
   370 	iDataBuffer->PositionL( KNoOffset );
       
   371 	const TMdCItems& items = TMdCItems::GetFromBufferL( *iDataBuffer );
       
   372 	__ASSERT_DEBUG( iNamespaceDefId == items.iNamespaceDefId, User::Panic( _L("Incorrect namespaceDef from returned items!"), KErrCorrupt ) );
       
   373 
       
   374     CMdENamespaceDef& namespaceDef = iSessionImpl.GetNamespaceDefL( iNamespaceDefId );
       
   375     iDataBuffer->PositionL( items.iRelations.iPtr.iOffset );
       
   376     TMdERelation relation;
       
   377     for (TInt i = 0; i < items.iRelations.iPtr.iCount; ++i )
       
   378     	{
       
   379     	relation.DeSerializeL( *iDataBuffer, namespaceDef );
       
   380     	if ( relation.Id() )
       
   381     		{
       
   382     		iRelationItemArray.Append( relation );
       
   383     		}
       
   384     	}
       
   385 	}