imservices/instantmessagingcache/imcacheclient/src/cimcacheaccesseventhandler.cpp
changeset 51 61fad867f68e
equal deleted inserted replaced
-1:000000000000 51:61fad867f68e
       
     1 /*
       
     2 * Copyright (c) 2008 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:  cache message change observer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 
       
    21 #include "cimcacheaccesseventhandler.h"
       
    22 #include "mimcacheclient.h"
       
    23 #include "mimcacheeventhandler.h"
       
    24 // logs
       
    25 #include "imcachedebugtrace.h"
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CIMCacheAccessEventHandler::NewL
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 CIMCacheAccessEventHandler* CIMCacheAccessEventHandler::NewL( 
       
    32 								MIMCacheClient& aRegistrar, 
       
    33 								MIMCacheEventHandler& aHandler )
       
    34     {
       
    35     TRACE( T_LIT("CIMCacheAccessEventHandler::NewL() begin") );
       
    36 	CIMCacheAccessEventHandler* self = new ( ELeave ) CIMCacheAccessEventHandler( aRegistrar ,aHandler ) ;
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop( self );  //self
       
    40 	TRACE( T_LIT("CIMCacheAccessEventHandler::NewL() end") );
       
    41     return self;
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CIMCacheAccessEventHandler::ConstructL
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 void CIMCacheAccessEventHandler::ConstructL()
       
    49     {
       
    50     TRACE( T_LIT("CIMCacheAccessEventHandler::ConstructL() begin") );
       
    51     iContinueObserving = ETrue ;
       
    52    	CActiveScheduler::Add( this );
       
    53 	iRegistrar.RegisterObserverToServerL( iStatus ,EIMCacheObserveMessageAccessRegister );
       
    54 	SetActive();
       
    55 	TRACE( T_LIT("CIMCacheAccessEventHandler::ConstructL() end") );
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CIMCacheAccessEventHandler::~CIMCacheAccessEventHandler
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CIMCacheAccessEventHandler::~CIMCacheAccessEventHandler()
       
    63 	{
       
    64 	TRACE( T_LIT("CIMCacheAccessEventHandler::~CIMCacheAccessEventHandler() begin") );
       
    65 	Cancel();
       
    66 	TRACE( T_LIT("CIMCacheAccessEventHandler::~CIMCacheAccessEventHandler() end") );
       
    67 	}
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CIMCacheAccessEventHandler::CIMCacheAccessEventHandler(
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CIMCacheAccessEventHandler::CIMCacheAccessEventHandler(
       
    74 								 MIMCacheClient& aRegistrar , 
       
    75 								 MIMCacheEventHandler& aHandler) :
       
    76 	CActive( CActive::EPriorityIdle ),
       
    77   	iRegistrar( aRegistrar ),
       
    78   	iAccessHandler( aHandler )
       
    79  	{
       
    80  	TRACE( T_LIT("CIMCacheAccessEventHandler::CIMCacheAccessEventHandler()") );
       
    81 	}
       
    82 // -----------------------------------------------------------------------------
       
    83 // CIMCacheAccessEventHandler::RunL
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CIMCacheAccessEventHandler::RunL()
       
    87 	{
       
    88 	TRACE( T_LIT("CIMCacheAccessEventHandler::RunL() begin") );
       
    89 	TRACE( T_LIT("CIMCacheAccessEventHandler::RunL() iStatus code = "),iStatus.Int()  );
       
    90 	
       
    91 	switch( iStatus.Int() )
       
    92 		{
       
    93 		case EIMOperationUnreadMessage :
       
    94 		    {
       
    95 		    TRACE( T_LIT("CIMCacheAccessEventHandler::RunL() EIMOperationUnreadMessage") );
       
    96 			GetBufferChatDataL();	            
       
    97 		    break;
       
    98 		    }
       
    99 	    case EIMOperationUnreadChange :
       
   100 	        {
       
   101 	        // message data available get the data   
       
   102 	        GetBufferedChatItemL(EIMCacheUnreadChange);  
       
   103 	        break;
       
   104 	        }
       
   105 	    case EIMOperationChatStarted :
       
   106 	        {
       
   107 	        // message data available get the data   
       
   108 	        GetBufferedChatItemL(EIMCacheChatStarted);  
       
   109 	        break;  
       
   110 	        }  
       
   111 		case EIMOperationChatDeleted :
       
   112 			{
       
   113 			// message data available get the data   
       
   114             GetBufferedChatItemL(EIMCacheChatClosed );  
       
   115 			break;	
       
   116 			} 
       
   117 	   case EIMOperationAllChatDeleted:
       
   118 		    {
       
   119 		    iAccessHandler.HandleIMCacheEventL( EIMCacheAllChatClosed, NULL );
       
   120 		    break;
       
   121 		    }
       
   122        case EIMOperationUnRegistered:
       
   123             {
       
   124             TRACE(T_LIT(
       
   125                     "CIMCacheEventHandler::RunL() EIMOperationUnRegistered"));
       
   126             iContinueObserving = EFalse;
       
   127             break;
       
   128             }
       
   129         case KErrServerTerminated:
       
   130             {
       
   131             // This case handles when the server terminated status is set.
       
   132             // This is scenario is typically scene when imcache server is uninstalled.
       
   133             UnRegisterObserver();
       
   134             DoCancel();
       
   135             break;
       
   136             }
       
   137 		default :
       
   138 		    {
       
   139 		    TRACE( T_LIT("CIMCacheEventHandler::RunL() default") );
       
   140 		    break;	
       
   141 		    }
       
   142 		}	
       
   143 
       
   144 	TRACE( T_LIT("CIMCacheAccessEventHandler::RunL() iContinueObserving iContinueObserving = %d") ,iContinueObserving);
       
   145 		// Comment: cancel operation is changes as synchronous. iContinueObserving from event
       
   146 	// can not really be trusted since complete code might be different if leave has occured 
       
   147 	// for example. Cancel is handled here as generic cancel.
       
   148 	// check need to continue observing to server
       
   149 	if( iContinueObserving && KErrCancel != iStatus.Int() )
       
   150 		{
       
   151 		iRegistrar.RegisterObserverToServerL( iStatus ,EIMCacheObserveMessageAccessRegister );
       
   152 		SetActive();
       
   153 		}
       
   154 	TRACE( T_LIT("CIMCacheAccessEventHandler::RunL() end") );
       
   155 	}
       
   156 // -----------------------------------------------------------------------------
       
   157 // CIMCacheAccessEventHandler::DoCancel
       
   158 // -----------------------------------------------------------------------------
       
   159 //	
       
   160 void CIMCacheAccessEventHandler::DoCancel()
       
   161 	{
       
   162 	TRACE( T_LIT("CIMCacheAccessEventHandler::DoCancel() ") );
       
   163 		// Comment: set to EFalse here since we can not trust other changes. See
       
   164 	// comments in RunL
       
   165 	iContinueObserving = EFalse;
       
   166 	if( IsActive() )
       
   167         {
       
   168         TRAP_IGNORE(iRegistrar.CancelRequestL( iStatus ,EIMCacheCancelRequest ));
       
   169         }
       
   170 	}
       
   171 
       
   172 // ---------------------------------------------------------
       
   173 // CIMCacheAccessEventHandler::RunError()
       
   174 // ---------------------------------------------------------
       
   175 //
       
   176 TInt CIMCacheAccessEventHandler::RunError( TInt /* aError */)
       
   177     {
       
   178     TRACE( T_LIT("CVIMPSTEngineCVListener::RunError() start"));
       
   179     return KErrNone;
       
   180     }
       
   181 // -----------------------------------------------------------------------------
       
   182 // CIMCacheAccessEventHandler::UnRegisterObserver
       
   183 // -----------------------------------------------------------------------------
       
   184 //		
       
   185 void CIMCacheAccessEventHandler::UnRegisterObserver()
       
   186 	{
       
   187 	TRACE( T_LIT("CIMCacheAccessEventHandler::UnRegisterObserver() begin") );
       
   188 	if( IsActive() )
       
   189 		{
       
   190 		TRACE( T_LIT("CIMCacheAccessEventHandler::UnRegisterObserver() active") );
       
   191 		TRAP_IGNORE(iRegistrar.UnRegisterObserverToServerL( iStatus ,EIMCacheObserveMessageAccessUnRegister ));
       
   192 		}
       
   193 	TRACE( T_LIT("CIMCacheAccessEventHandler::UnRegisterObserver() end") );	
       
   194 	}
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CIMCacheAccessEventHandler::GetBufferChatDataL
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void CIMCacheAccessEventHandler::GetBufferChatDataL() 
       
   201     {
       
   202     TRACE( T_LIT("CIMCacheAccessEventHandler::GetBufferChatDataL() begin") );
       
   203    
       
   204     TBool more = ETrue;
       
   205     while(more)
       
   206         {
       
   207         TPtr8 bufferPtr = iRegistrar.GetBufferedDataL( EIMCacheGetBufferedPackets  );
       
   208         InternalizeChatDataL( bufferPtr , more );
       
   209         }
       
   210     TRACE( T_LIT("CIMCacheAccessEventHandler::GetBufferChatDataL() end") );
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CIMCacheAccessEventHandler::InternalizeChatDataL
       
   215 // -----------------------------------------------------------------------------     
       
   216 void CIMCacheAccessEventHandler::InternalizeChatDataL( TPtr8 aChatDataBuffer , TBool& aMore )
       
   217     {
       
   218     TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatDataL() Start") );
       
   219     if( 0 == aChatDataBuffer.Length())
       
   220           {
       
   221           TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatItemL() aChatDataBuffer is zeor, hence returning ") );
       
   222           return;
       
   223           }
       
   224 	RDesReadStream readAllStream ;
       
   225 	RDesReadStream readStream ;    
       
   226 
       
   227 	readAllStream.Open( aChatDataBuffer );
       
   228 	CleanupClosePushL(readAllStream);
       
   229 
       
   230 	TInt messageCount = readAllStream.ReadInt16L();
       
   231 
       
   232 	for( TInt i=0; i< messageCount; i++)
       
   233 		{
       
   234 		SIMCacheMessageData chatData =
       
   235 		    {
       
   236 		    TIMCacheMessageType(0),
       
   237 		    };
       
   238 		        
       
   239 		TInt size = readAllStream.ReadInt16L() ;
       
   240 		HBufC8* mBuffer  = HBufC8::NewLC( size );
       
   241 		TPtr8 mBufferPtr = mBuffer->Des();
       
   242 		        
       
   243 		readAllStream.ReadL( mBufferPtr , size );
       
   244 
       
   245 		readStream.Open( *mBuffer );        
       
   246 		CleanupClosePushL(readStream) ;
       
   247 
       
   248 		chatData.iMessageType   = static_cast<TIMCacheMessageType>( readStream.ReadInt16L() );
       
   249 		chatData.iMessagerType  = static_cast<TIMCacheMessagerType>( readStream.ReadInt16L() );
       
   250 		chatData.iContentType   = static_cast<TIMCacheContentType>( readStream.ReadInt16L() );
       
   251 		chatData.iTime          = static_cast<TReal64>( readStream.ReadReal64L() );
       
   252 
       
   253 		TInt buddyLength = readStream.ReadInt16L();
       
   254 		HBufC* buddy     = HBufC::NewLC( buddyLength );
       
   255 		TPtr buddyPtr    = buddy->Des();
       
   256 		readStream.ReadL( buddyPtr , buddyLength );
       
   257 		chatData.iBuddyId = buddy;
       
   258 
       
   259 		TInt textSize = readStream.ReadInt16L();
       
   260 		HBufC* text  = HBufC::NewLC( textSize );
       
   261 		TPtr textPtr = text->Des(); 
       
   262 		readStream.ReadL( textPtr , textSize);            
       
   263 		chatData.iText = text;
       
   264 		
       
   265 		iAccessHandler.HandleIMCacheEventL(EIMCacheUnreadMessage , &chatData);
       
   266 		
       
   267 		CleanupStack::PopAndDestroy(4);//close read stream, delete mBuffer, text, buddy 
       
   268 		}
       
   269 
       
   270 	aMore = static_cast<TBool>( readAllStream.ReadInt16L() );
       
   271 
       
   272 	CleanupStack::PopAndDestroy();//readAllStream.     
       
   273     
       
   274     TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatDataL() End") );
       
   275     }
       
   276   
       
   277 // -----------------------------------------------------------------------------
       
   278 // CIMCacheAccessEventHandler::GetBufferedChatItemL
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 void CIMCacheAccessEventHandler::GetBufferedChatItemL( TIMCacheEventType aEventType )
       
   282     {
       
   283     TRACE( T_LIT("CIMCacheAccessEventHandler::GetBufferedChatItemL() begin") );
       
   284     
       
   285     TPtr8 bufferPtr = iRegistrar.GetBufferedDataL( EIMCacheGetBufferedPackets );
       
   286     
       
   287     InternalizeChatItemL( aEventType, bufferPtr );
       
   288     
       
   289     TRACE( T_LIT("CIMCacheAccessEventHandler::GetBufferedChatItemL() end") );
       
   290     }
       
   291 // -----------------------------------------------------------------------------
       
   292 // CIMCacheAccessEventHandler::InternalizeChatItemL
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CIMCacheAccessEventHandler::InternalizeChatItemL( TIMCacheEventType aEventType, 
       
   296                                                  TPtr8 aContactBuffer )
       
   297     {
       
   298     TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatItemL() begin") );   
       
   299     if( 0 == aContactBuffer.Length())
       
   300            {
       
   301            TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatItemL() aChatDataBuffer is zeor, hence returning ") );
       
   302            return;
       
   303            }
       
   304     RDesReadStream readAllStream ;
       
   305     RDesReadStream readStream ;    
       
   306     
       
   307     readAllStream.Open( aContactBuffer );
       
   308     CleanupClosePushL(readAllStream);
       
   309     
       
   310     TInt messageCount = readAllStream.ReadInt16L();
       
   311     
       
   312     for( TInt i=0; i< messageCount; i++)
       
   313         {
       
   314         SIMCacheChatItem chatItem =
       
   315           {
       
   316           0,
       
   317           };
       
   318                 
       
   319         TInt size = readAllStream.ReadInt16L() ;
       
   320         HBufC8* mBuffer  = HBufC8::NewLC( size );
       
   321         TPtr8 mBufferPtr = mBuffer->Des();
       
   322                 
       
   323         readAllStream.ReadL( mBufferPtr , size );
       
   324         
       
   325         readStream.Open( *mBuffer );        
       
   326         CleanupClosePushL(readStream) ;
       
   327         
       
   328         chatItem.iServiceId  = static_cast<TInt>( readStream.ReadInt16L() );
       
   329         
       
   330         TInt textSize = readStream.ReadInt16L();
       
   331         HBufC* buddyId = HBufC::NewLC( textSize );
       
   332         TPtr buddyIdPtr = buddyId->Des(); 
       
   333         readStream.ReadL( buddyIdPtr , textSize);
       
   334         
       
   335         chatItem.iBuddyId = buddyId;
       
   336         
       
   337         iAccessHandler.HandleIMCacheEventL( aEventType , &chatItem );
       
   338       
       
   339         CleanupStack::PopAndDestroy(3);//buddyId , close read stream, delete mBuffer.      
       
   340                   
       
   341         }
       
   342     CleanupStack::PopAndDestroy();//readAllStream.    
       
   343     TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatItemL() End") );
       
   344     }
       
   345 
       
   346  
       
   347 //  end of file
       
   348