imservices/instantmessagingcache/imcacheclient/src/cimcacheaccesseventhandler.cpp
changeset 0 e6b17d312c8b
child 1 b610b446cb01
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     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("CIMCacheEventHandler::RunL() EIMOperationUnRegistered") );
       
   125 			iContinueObserving = EFalse;	
       
   126 			break;	
       
   127 			}
       
   128 		default :
       
   129 		    {
       
   130 		    TRACE( T_LIT("CIMCacheEventHandler::RunL() default") );
       
   131 		    break;	
       
   132 		    }
       
   133 		}	
       
   134 
       
   135 	TRACE( T_LIT("CIMCacheAccessEventHandler::RunL() iContinueObserving iContinueObserving = %d") ,iContinueObserving);
       
   136 	// check need to continue observing to server
       
   137 	if( iContinueObserving )
       
   138 		{
       
   139 		iRegistrar.RegisterObserverToServerL( iStatus ,EIMCacheObserveMessageAccessRegister );
       
   140 		SetActive();
       
   141 		}
       
   142 	TRACE( T_LIT("CIMCacheAccessEventHandler::RunL() end") );
       
   143 	}
       
   144 // -----------------------------------------------------------------------------
       
   145 // CIMCacheAccessEventHandler::DoCancel
       
   146 // -----------------------------------------------------------------------------
       
   147 //	
       
   148 void CIMCacheAccessEventHandler::DoCancel()
       
   149 	{
       
   150 	TRACE( T_LIT("CIMCacheAccessEventHandler::DoCancel() ") );
       
   151 	if( IsActive() )
       
   152         {
       
   153         TRAP_IGNORE(iRegistrar.CancelRequestL( iStatus ,EIMCacheCancelRequest ));
       
   154         }
       
   155 	}
       
   156 
       
   157 // ---------------------------------------------------------
       
   158 // CIMCacheAccessEventHandler::RunError()
       
   159 // ---------------------------------------------------------
       
   160 //
       
   161 TInt CIMCacheAccessEventHandler::RunError( TInt /* aError */)
       
   162     {
       
   163     TRACE( T_LIT("CVIMPSTEngineCVListener::RunError() start"));
       
   164     return KErrNone;
       
   165     }
       
   166 // -----------------------------------------------------------------------------
       
   167 // CIMCacheAccessEventHandler::UnRegisterObserver
       
   168 // -----------------------------------------------------------------------------
       
   169 //		
       
   170 void CIMCacheAccessEventHandler::UnRegisterObserver()
       
   171 	{
       
   172 	TRACE( T_LIT("CIMCacheAccessEventHandler::UnRegisterObserver() begin") );
       
   173 	if( IsActive() )
       
   174 		{
       
   175 		TRACE( T_LIT("CIMCacheAccessEventHandler::UnRegisterObserver() active") );
       
   176 		TRAP_IGNORE(iRegistrar.UnRegisterObserverToServerL( iStatus ,EIMCacheObserveMessageAccessUnRegister ));
       
   177 		}
       
   178 	TRACE( T_LIT("CIMCacheAccessEventHandler::UnRegisterObserver() end") );	
       
   179 	}
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CIMCacheAccessEventHandler::GetBufferChatDataL
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CIMCacheAccessEventHandler::GetBufferChatDataL() 
       
   186     {
       
   187     TRACE( T_LIT("CIMCacheAccessEventHandler::GetBufferChatDataL() begin") );
       
   188    
       
   189     TBool more = ETrue;
       
   190     while(more)
       
   191         {
       
   192         TPtr8 bufferPtr = iRegistrar.GetBufferedDataL( EIMCacheGetBufferedPackets  );
       
   193         InternalizeChatDataL( bufferPtr , more );
       
   194         }
       
   195     TRACE( T_LIT("CIMCacheAccessEventHandler::GetBufferChatDataL() end") );
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CIMCacheAccessEventHandler::InternalizeChatDataL
       
   200 // -----------------------------------------------------------------------------     
       
   201 void CIMCacheAccessEventHandler::InternalizeChatDataL( TPtr8 aChatDataBuffer , TBool& aMore )
       
   202     {
       
   203     TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatDataL() Start") );
       
   204     if( 0 == aChatDataBuffer.Length())
       
   205           {
       
   206           TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatItemL() aChatDataBuffer is zeor, hence returning ") );
       
   207           return;
       
   208           }
       
   209 	RDesReadStream readAllStream ;
       
   210 	RDesReadStream readStream ;    
       
   211 
       
   212 	readAllStream.Open( aChatDataBuffer );
       
   213 	CleanupClosePushL(readAllStream);
       
   214 
       
   215 	TInt messageCount = readAllStream.ReadInt16L();
       
   216 
       
   217 	for( TInt i=0; i< messageCount; i++)
       
   218 		{
       
   219 		SIMCacheMessageData chatData =
       
   220 		    {
       
   221 		    TIMCacheMessageType(0),
       
   222 		    };
       
   223 		        
       
   224 		TInt size = readAllStream.ReadInt16L() ;
       
   225 		HBufC8* mBuffer  = HBufC8::NewLC( size );
       
   226 		TPtr8 mBufferPtr = mBuffer->Des();
       
   227 		        
       
   228 		readAllStream.ReadL( mBufferPtr , size );
       
   229 
       
   230 		readStream.Open( *mBuffer );        
       
   231 		CleanupClosePushL(readStream) ;
       
   232 
       
   233 		chatData.iMessageType   = static_cast<TIMCacheMessageType>( readStream.ReadInt16L() );
       
   234 		chatData.iMessagerType  = static_cast<TIMCacheMessagerType>( readStream.ReadInt16L() );
       
   235 		chatData.iContentType   = static_cast<TIMCacheContentType>( readStream.ReadInt16L() );
       
   236 		chatData.iTime          = static_cast<TReal64>( readStream.ReadReal64L() );
       
   237 
       
   238 		TInt buddyLength = readStream.ReadInt16L();
       
   239 		HBufC* buddy     = HBufC::NewLC( buddyLength );
       
   240 		TPtr buddyPtr    = buddy->Des();
       
   241 		readStream.ReadL( buddyPtr , buddyLength );
       
   242 		chatData.iBuddyId = buddy;
       
   243 
       
   244 		TInt textSize = readStream.ReadInt16L();
       
   245 		HBufC* text  = HBufC::NewLC( textSize );
       
   246 		TPtr textPtr = text->Des(); 
       
   247 		readStream.ReadL( textPtr , textSize);            
       
   248 		chatData.iText = text;
       
   249 		
       
   250 		iAccessHandler.HandleIMCacheEventL(EIMCacheUnreadMessage , &chatData);
       
   251 		
       
   252 		CleanupStack::PopAndDestroy(4);//close read stream, delete mBuffer, text, buddy 
       
   253 		}
       
   254 
       
   255 	aMore = static_cast<TBool>( readAllStream.ReadInt16L() );
       
   256 
       
   257 	CleanupStack::PopAndDestroy();//readAllStream.     
       
   258     
       
   259     TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatDataL() End") );
       
   260     }
       
   261   
       
   262 // -----------------------------------------------------------------------------
       
   263 // CIMCacheAccessEventHandler::GetBufferedChatItemL
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 void CIMCacheAccessEventHandler::GetBufferedChatItemL( TIMCacheEventType aEventType )
       
   267     {
       
   268     TRACE( T_LIT("CIMCacheAccessEventHandler::GetBufferedChatItemL() begin") );
       
   269     
       
   270     TPtr8 bufferPtr = iRegistrar.GetBufferedDataL( EIMCacheGetBufferedPackets );
       
   271     
       
   272     InternalizeChatItemL( aEventType, bufferPtr );
       
   273     
       
   274     TRACE( T_LIT("CIMCacheAccessEventHandler::GetBufferedChatItemL() end") );
       
   275     }
       
   276 // -----------------------------------------------------------------------------
       
   277 // CIMCacheAccessEventHandler::InternalizeChatItemL
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 void CIMCacheAccessEventHandler::InternalizeChatItemL( TIMCacheEventType aEventType, 
       
   281                                                  TPtr8 aContactBuffer )
       
   282     {
       
   283     TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatItemL() begin") );   
       
   284     if( 0 == aContactBuffer.Length())
       
   285            {
       
   286            TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatItemL() aChatDataBuffer is zeor, hence returning ") );
       
   287            return;
       
   288            }
       
   289     RDesReadStream readAllStream ;
       
   290     RDesReadStream readStream ;    
       
   291     
       
   292     readAllStream.Open( aContactBuffer );
       
   293     CleanupClosePushL(readAllStream);
       
   294     
       
   295     TInt messageCount = readAllStream.ReadInt16L();
       
   296     
       
   297     for( TInt i=0; i< messageCount; i++)
       
   298         {
       
   299         SIMCacheChatItem chatItem =
       
   300           {
       
   301           0,
       
   302           };
       
   303                 
       
   304         TInt size = readAllStream.ReadInt16L() ;
       
   305         HBufC8* mBuffer  = HBufC8::NewLC( size );
       
   306         TPtr8 mBufferPtr = mBuffer->Des();
       
   307                 
       
   308         readAllStream.ReadL( mBufferPtr , size );
       
   309         
       
   310         readStream.Open( *mBuffer );        
       
   311         CleanupClosePushL(readStream) ;
       
   312         
       
   313         chatItem.iServiceId  = static_cast<TInt>( readStream.ReadInt16L() );
       
   314         
       
   315         TInt textSize = readStream.ReadInt16L();
       
   316         HBufC* buddyId = HBufC::NewLC( textSize );
       
   317         TPtr buddyIdPtr = buddyId->Des(); 
       
   318         readStream.ReadL( buddyIdPtr , textSize);
       
   319         
       
   320         chatItem.iBuddyId = buddyId;
       
   321         
       
   322         iAccessHandler.HandleIMCacheEventL( aEventType , &chatItem );
       
   323       
       
   324         CleanupStack::PopAndDestroy(3);//buddyId , close read stream, delete mBuffer.      
       
   325                   
       
   326         }
       
   327     CleanupStack::PopAndDestroy();//readAllStream.    
       
   328     TRACE( T_LIT("CIMCacheAccessEventHandler::InternalizeChatItemL() End") );
       
   329     }
       
   330 
       
   331  
       
   332 //  end of file
       
   333