imservices/instantmessagingcache/imcacheserver/src/cimcachesession.cpp
changeset 0 e6b17d312c8b
child 2 89455bbf90d8
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 session class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 // local includes
       
    21 #include "cimcachesession.h"
       
    22 
       
    23 #include "cimcacheserver.h"
       
    24 #include "ccacheserveractivehelper.h"
       
    25 #include "cimcachemessagebase.h"
       
    26 #include "mimcachemessageheader.h"
       
    27 // logs
       
    28 #include "imcachedebugtrace.h"
       
    29 #include "cimcachebufferarray.h"
       
    30 
       
    31 // ==============================================================
       
    32 // ======================== SESSION =============================
       
    33 // ==============================================================
       
    34 
       
    35 // ---------------------------------------------------------
       
    36 // CIMCacheSession::NewL
       
    37 // Two-phased constructor.
       
    38 // ---------------------------------------------------------
       
    39 //
       
    40 CIMCacheSession* CIMCacheSession::NewL()
       
    41     {
       
    42     TRACE( T_LIT("CIMCacheSession::NewL begin") );
       
    43     CIMCacheSession* self = new( ELeave ) CIMCacheSession(  );
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop( self );
       
    47     TRACE( T_LIT("CIMCacheSession::NewL end") );
       
    48     return self;
       
    49     }
       
    50 
       
    51 // ---------------------------------------------------------
       
    52 // CIMCacheSession::NewL
       
    53 // Symbian OS default constructor can leave.
       
    54 // ---------------------------------------------------------
       
    55 //
       
    56 void CIMCacheSession::ConstructL()
       
    57 	{
       
    58 	//This T-class is heap-allocated only because we do not want to 
       
    59 	//include T-class header in header file of imcachesession.
       
    60 	}
       
    61 
       
    62 // ---------------------------------------------------------
       
    63 // CIMCacheSession::NewL
       
    64 // destructor
       
    65 // ---------------------------------------------------------
       
    66 //
       
    67 CIMCacheSession::~CIMCacheSession()
       
    68     {
       
    69     TRACE( T_LIT("CIMCacheSession[%d]::~CIMCacheSession() start"), this );
       
    70     if(iObserverActive)
       
    71         {
       
    72         iObserverMessage.Complete( KErrCancel );
       
    73         iObserverActive = EFalse;   
       
    74         }
       
    75     if( Server() )
       
    76         {
       
    77         Server()->SessionDied( this );
       
    78         }
       
    79      iPacketsArray.ResetAndDestroy();
       
    80      
       
    81      ResetHeaderInoformation();	
       
    82      
       
    83      TRACE( T_LIT("CIMCacheSession::~CIMCacheSession() end "));
       
    84      }
       
    85 
       
    86 
       
    87 // ---------------------------------------------------------
       
    88 // CIMCacheSession::NewL
       
    89 // C++ default constructor can NOT contain any code, that
       
    90 // might leave.
       
    91 // ---------------------------------------------------------
       
    92 //
       
    93 CIMCacheSession::CIMCacheSession():
       
    94 iOpenChatFetchCompleted( EFalse )
       
    95 	{
       
    96  	TRACE( T_LIT("CIMCacheSession::CIMCacheSession"));
       
    97     }
       
    98 
       
    99 
       
   100 // ---------------------------------------------------------
       
   101 // CIMCacheSession::CreateL
       
   102 // (other items were commented in a header).
       
   103 // ---------------------------------------------------------
       
   104 //
       
   105 void CIMCacheSession::CreateL()
       
   106     {
       
   107     TRACE( T_LIT("CIMCacheSession[%d]::CreateL()"), this );
       
   108     Server()->SessionCreatedL( this );
       
   109     TRACE( T_LIT( "CIMCacheSession::CreateL end") );
       
   110     }
       
   111 
       
   112 // ---------------------------------------------------------
       
   113 // CIMCacheSession::ServiceL
       
   114 // (other items were commented in a header).
       
   115 // ---------------------------------------------------------
       
   116 //
       
   117 void CIMCacheSession::ServiceL( const RMessage2 &aMessage )
       
   118     {
       
   119 	TRACE( T_LIT("CIMCacheSession::ServiceL	, begin"));
       
   120     if( DispatchMessageL( aMessage ) )
       
   121         {
       
   122         TRACE( T_LIT("CIMCacheSession::ServiceL inside  DispatchMessageL"));
       
   123         if( !iMessageCompleted  )
       
   124         	{
       
   125             TRACE( T_LIT("CIMCacheSession::ServiceL inside  !iMessageCompleted"));
       
   126         	aMessage.Complete( KErrNone );
       
   127         	}
       
   128         }
       
   129     TRACE( T_LIT("CIMCacheSession::ServiceL	, end"));
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------
       
   133 // CIMCacheSession::ServiceError
       
   134 // (other items were commented in a header).
       
   135 // ---------------------------------------------------------
       
   136 //
       
   137 void CIMCacheSession::ServiceError( const RMessage2& aMessage,
       
   138                                       TInt aError )
       
   139     {
       
   140     TRACE( T_LIT("CIMCacheSession::ServiceError	, begin"));
       
   141     aMessage.Complete( aError );
       
   142     TRACE( T_LIT("CIMCacheSession::ServiceError	, end"));
       
   143     }
       
   144 
       
   145 
       
   146 // ---------------------------------------------------------
       
   147 // CIMCacheSession::DispatchMessageL
       
   148 // (other items were commented in a header).
       
   149 // ---------------------------------------------------------
       
   150 //
       
   151 TBool CIMCacheSession::DispatchMessageL( const RMessage2& aMessage )
       
   152     {
       
   153     TRACE( T_LIT("CIMCacheSession[%d]::DispatchMessageL() %d"), this, aMessage.Function() );
       
   154     
       
   155 	iMessageCompleted = EFalse;
       
   156     TBool msgNeedsToBeCompleted = ETrue;
       
   157     
       
   158 	  switch( aMessage.Function() )
       
   159         {
       
   160         // initialization operations
       
   161         case EIMCacheInitUpdate :
       
   162 	        {
       
   163 	        TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheInitUpdate") );
       
   164 	        InitSessionL( aMessage );
       
   165 	       	break;	
       
   166 	        }
       
   167 	     // initialization operations
       
   168         case EIMCacheInitAccess :
       
   169 	        {
       
   170 	        TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheInitAccess") );
       
   171 	        InitSessionL( aMessage );
       
   172 	        iAccessorInitialized = ETrue;
       
   173         	break;	
       
   174 	        }
       
   175 	     // initialization operations
       
   176         case EIMCacheInitConvesation :
       
   177 	        {
       
   178 	        TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheInitAccess") );
       
   179 	        InitSessionL( aMessage );
       
   180 	        iConvesationInitialized = ETrue;
       
   181         	break;	
       
   182 	        }
       
   183 	     // update right operations
       
   184         case EIMCacheStartNewConversation :
       
   185         	{
       
   186         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheStartNewConversation excuted") );
       
   187         	iHelper->StartNewConversationL(aMessage);
       
   188         	break;
       
   189         	}
       
   190       	case EIMCacheAppendSendMessage :
       
   191         	{
       
   192         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheAppendSendMessage excuted") );
       
   193         	iHelper->AppendSendMessageL(aMessage);
       
   194         	break;
       
   195         	}
       
   196         case EIMCacheAppendMessage :
       
   197         	{
       
   198         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheAppendMessage excuted") );
       
   199         	iHelper->AppendMessageL(aMessage);
       
   200         	break;
       
   201         	}
       
   202         case EIMCacheAppendReceiveMessage:
       
   203         	{
       
   204         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheAppendReceiveMessage excuted") );
       
   205         	iHelper->AppendReceiveMessageL(aMessage);
       
   206         	break;
       
   207         	}
       
   208         case EIMCacheCloseConversation :
       
   209             {
       
   210             TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheCloseConversation excuted") );
       
   211             iHelper->CloseConversationL(aMessage);
       
   212             break;
       
   213             }
       
   214         case EIMCacheCloseAllConversation :
       
   215             {
       
   216             TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheCloseAllConversation excuted") );
       
   217             iHelper->CloseAllConversationL(iServiceId);
       
   218             break;
       
   219             }
       
   220         case EIMCacheDeactivateConversation:
       
   221         	{
       
   222         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheDeactivateConversation excuted") );
       
   223         	iHelper->DeactivateConversationL();
       
   224         
       
   225         	break;
       
   226         	}
       
   227        	 
       
   228         case EIMCacheObserveMessageUpdateRegister:
       
   229         	{
       
   230         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheObserveMessageUpdateRegister excuted") );
       
   231         	if( iObserverActive )
       
   232         		{
       
   233         		TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer already active") );
       
   234         		// complete the old observer request with KErrCancel
       
   235         		iObserverMessage.Complete( KErrAlreadyExists );
       
   236         		}
       
   237         	iObserverMessage = aMessage;
       
   238         	iObserverActive = ETrue;
       
   239            	msgNeedsToBeCompleted = EFalse;
       
   240            	// check fetching still needed or not if needed pack and send again 
       
   241 			if( iPacketsArray.Count() )
       
   242 				{
       
   243 				CIMCacheBufferArray* packet = iPacketsArray[ 0 ] ;
       
   244 				iObserverMessage.Complete( packet->OperationCode() );
       
   245 				iObserverActive = EFalse;
       
   246 				}
       
   247 			
       
   248 			break;
       
   249         	}
       
   250         case EIMCacheObserveMessageUpdateUnRegister :
       
   251 	        {
       
   252 	        TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheObserveMessageUpdateUnRegister excuted") );
       
   253 	        if( !iObserverActive )
       
   254         		{
       
   255         		TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer not active ") );
       
   256         		}
       
   257         	else
       
   258 	        	{
       
   259 	        	TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer active ") );
       
   260 				iObserverMessage.Complete( EIMOperationUnRegistered );
       
   261 				iObserverActive = EFalse;		
       
   262 	        	}
       
   263 	        
       
   264             break;	
       
   265 	        }
       
   266 	     case EIMCacheObserveMessageAccessRegister:
       
   267         	{
       
   268         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheObserveMessageAccessRegister excuted") );
       
   269         	if( iObserverActive )
       
   270         		{
       
   271         		TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer already active") );
       
   272         		// complete the old observer request with KErrCancel
       
   273         		iObserverMessage.Complete( KErrAlreadyExists );
       
   274         		}
       
   275         	iObserverMessage = aMessage;
       
   276         	iObserverActive = ETrue;
       
   277             msgNeedsToBeCompleted = EFalse;
       
   278 			 if( iPacketsArray.Count() )
       
   279 				{
       
   280 				CIMCacheBufferArray* packet = iPacketsArray[ 0 ] ;
       
   281 				iObserverMessage.Complete( packet->OperationCode() );
       
   282 				iObserverActive = EFalse;
       
   283 				}
       
   284 	     	break;
       
   285         	}
       
   286          case EIMCacheObserveMessageAccessUnRegister :
       
   287 	        {
       
   288 	        TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheObserveMessageAccessUnRegister excuted") );
       
   289 	        if( !iObserverActive )
       
   290         		{
       
   291         		TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer not active ") );
       
   292         		}
       
   293         	else
       
   294 	        	{
       
   295 	        	TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer active ") );
       
   296 				iObserverMessage.Complete( EIMOperationUnRegistered );
       
   297 				iObserverActive = EFalse;		
       
   298 	        	}
       
   299 	  
       
   300             break;	
       
   301 	        }
       
   302        
       
   303         case EIMCacheGetUnreadCount:
       
   304         	{
       
   305         	iHelper->GetUnreadMessageCountL(aMessage);
       
   306         	break;
       
   307         	}
       
   308         case EIMCacheGetAllUnreadCount:
       
   309         	{
       
   310         	iHelper->GetAllUnreadMessageCountL(aMessage);
       
   311         	break;
       
   312         	}
       
   313        	   // check for conversation 
       
   314         case EIMCacheIsConversationExist :
       
   315 	        {
       
   316 	        TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheIsConversationExist excuted ") );
       
   317 	       	iHelper->CheckConversationExistL(aMessage);
       
   318 	    	break;	
       
   319 	        }
       
   320         // Get what change happen        	
       
   321         case EIMCacheGetBufferedPackets:
       
   322         	{
       
   323         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheGetBufferedPackets excuted") );
       
   324         	if( iPacketsArray.Count() )
       
   325 				{
       
   326 				CIMCacheBufferArray* packet = iPacketsArray[ 0 ] ;
       
   327 				aMessage.WriteL(0, packet->PacketData() );
       
   328 				iPacketsArray.Remove(0);
       
   329 				delete packet;
       
   330 				packet = NULL;
       
   331 				iPacketsArray.Compress();
       
   332 				}
       
   333         	break;
       
   334         	}       
       
   335            	
       
   336         case EIMCacheGetChatList:
       
   337         	{
       
   338         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheGetChatList executed") );
       
   339         	//0 serviceId 
       
   340         	//1 buffer
       
   341         	TInt serviceId = aMessage.Int0();       	
       
   342         	
       
   343         	ExternalizeChatItemL( EIMOperationFetchAll , serviceId );
       
   344         	
       
   345         	//aMessage.WriteL( 1, *iChatDataBuffer); 
       
   346         	TInt packetsCounts = iPacketsArray.Count();
       
   347         
       
   348         	if( packetsCounts )
       
   349 				{
       
   350 				CIMCacheBufferArray* packet = iPacketsArray[ 0 ] ;
       
   351 				aMessage.WriteL( 1, packet->PacketData() );	
       
   352 				iPacketsArray.Remove(0);
       
   353 				delete packet;
       
   354 				packet = NULL;
       
   355 				iPacketsArray.Compress();
       
   356 				}
       
   357             break;	
       
   358         	}
       
   359         case EIMCacheCancelRequest:
       
   360             {
       
   361             TRACE( T_LIT("CIMCacheSession::EIMCacheCancelRequest start"));
       
   362             if(iObserverActive)
       
   363                 {
       
   364                 TRACE( T_LIT("CIMCacheSession::EIMCacheCancelRequest Inside if"));
       
   365                 iObserverMessage.Complete( KErrCancel );
       
   366                 TRACE( T_LIT("CIMCacheSession::EIMCacheCancelRequest After iObserverMessage.Complete"));
       
   367                 iObserverActive = EFalse;   
       
   368                 }
       
   369             TRACE( T_LIT("CIMCacheSession::EIMCacheCancelRequest End"));
       
   370 
       
   371           
       
   372             break;
       
   373             }
       
   374         default:
       
   375             {
       
   376             TRACE( T_LIT("CIMCacheSession::DispatchMessageL default excuted") );
       
   377             User::Leave( KErrNotSupported );
       
   378             break;
       
   379             }
       
   380         }
       
   381 	TRACE( T_LIT("CIMCacheSession::DispatchMessageL	end"));
       
   382     return msgNeedsToBeCompleted;
       
   383    
       
   384     }
       
   385  //=======================================================================
       
   386 //==========================initialization ==================================
       
   387 //========================================================================
       
   388 
       
   389 // ---------------------------------------------------------
       
   390 // CIMCacheSession::InitSessionL
       
   391 // (other items were commented in a header).
       
   392 // ---------------------------------------------------------
       
   393 //
       
   394 void CIMCacheSession::InitSessionL(const RMessage2& aMessage )
       
   395 	{
       
   396     TRACE( T_LIT("CIMCacheSession::InitSessionL:start") );
       
   397     // at zero place service id was appended
       
   398     iServiceId = aMessage.Int0();
       
   399 	if( Server() )
       
   400         {
       
   401         iHelper = Server()->GetActiveHelper();
       
   402         }
       
   403     TRACE( T_LIT("CIMCacheSession::InitSessionL end") );
       
   404 	} 
       
   405 
       
   406 
       
   407 // ---------------------------------------------------------
       
   408 // CIMCacheSession::InitSessionL
       
   409 // (other items were commented in a header).
       
   410 // ---------------------------------------------------------
       
   411 //
       
   412 TInt CIMCacheSession::ServiceId() const
       
   413     {
       
   414     TRACE( T_LIT("CIMCacheSession::ServiceId:start") );
       
   415     TRACE( T_LIT("CIMCacheSession::ServiceId end") );
       
   416     return iServiceId;
       
   417     } 
       
   418 // ---------------------------------------------------------------------------
       
   419 // CIMCacheSession::ResetHeaderInoformation()
       
   420 // ---------------------------------------------------------------------------
       
   421 //
       
   422 void CIMCacheSession::ResetHeaderInoformation()
       
   423                                                       
       
   424     {
       
   425     TRACE( T_LIT( "CIMCacheSession::ResetHeaderInoformation begin") );
       
   426     if( iConvesationInitialized )
       
   427 	    {
       
   428 	    iHelper->ResetHeaderInoformation();	
       
   429 	    }
       
   430    	TRACE( T_LIT( "CIMCacheSession::ResetHeaderInoformation end") );
       
   431     }
       
   432  
       
   433  
       
   434 // ---------------------------------------------------------
       
   435 // CIMCacheSession::PackAndNotifyEventL
       
   436 // (other items were commented in a header).
       
   437 // ---------------------------------------------------------
       
   438 //
       
   439 void CIMCacheSession::PackAndNotifyEventL(TIMCacheOperationsCode aChangeType ,
       
   440 										  MIMCacheMessageHeader* aChatHeader /*=NULL*/, 
       
   441 									 	  CIMCacheMessageBase* aMessage /*=NULL*/ ) 
       
   442 
       
   443     {	
       
   444     TRACE( T_LIT( "CIMCacheSession::PackAndNotifyEventL() begin") );
       
   445 
       
   446     switch( aChangeType )
       
   447         {
       
   448         case EIMOperationFetchAll :
       
   449             {
       
   450             SendChatListDataL(aChangeType, aChatHeader);
       
   451             break;	
       
   452             }
       
   453         case EIMOperationUnreadChange :
       
   454             {
       
   455             if(  iObserverActive && iConvesationInitialized )
       
   456                 {
       
   457                 iObserverMessage.Complete( aChangeType );
       
   458                 iObserverActive = EFalse;		
       
   459                 }
       
   460             else if( iObserverActive && iAccessorInitialized  )
       
   461                 {
       
   462                 ExternalizeSingleChatItemL(aChangeType, aChatHeader );
       
   463                 iObserverMessage.Complete( aChangeType );
       
   464                 iObserverActive = EFalse;
       
   465                 }
       
   466             break;	
       
   467             }
       
   468         case EIMOperationUnreadMessage :
       
   469             {
       
   470             SendUnreadChangeNotificationL( aChatHeader, aMessage );
       
   471             break;	
       
   472             }
       
   473         case EIMOperationChatStarted :
       
   474             {
       
   475             if( iObserverActive && iConvesationInitialized )
       
   476                 {
       
   477                 iObserverMessage.Complete( aChangeType );
       
   478                 iObserverActive = EFalse;		
       
   479                 }
       
   480             else if( iObserverActive && iAccessorInitialized )
       
   481                 {
       
   482                 ExternalizeSingleChatItemL(aChangeType, aChatHeader );
       
   483                 iObserverMessage.Complete( aChangeType );
       
   484                 iObserverActive = EFalse;   
       
   485                 }
       
   486             break;	
       
   487             }
       
   488         case EIMOperationChatAdded :
       
   489             {
       
   490             if(  iConvesationInitialized )
       
   491                 {
       
   492                 ExternalizeSingleChatItemL(aChangeType, aChatHeader);
       
   493                 if( iObserverActive )
       
   494                     {
       
   495                     iObserverMessage.Complete( aChangeType );
       
   496                     iObserverActive = EFalse;	
       
   497                     }
       
   498                 }
       
   499             else if( iAccessorInitialized )
       
   500                 {
       
   501                 SendUnreadChangeNotificationL(aChatHeader, aMessage );
       
   502                 }
       
   503             break;	
       
   504             }
       
   505         case EIMOperationMessageAdded :
       
   506             {
       
   507             if( iConvesationInitialized )
       
   508                 {
       
   509                 ExternalizeSingleChatDataL(aChangeType, aChatHeader , aMessage );
       
   510                 if( iObserverActive )
       
   511                     {
       
   512                     iObserverMessage.Complete( aChangeType );
       
   513                     iObserverActive = EFalse;
       
   514                     }
       
   515                 }
       
   516             break;	
       
   517             }
       
   518         case EIMOperationChatDeleted :
       
   519             {
       
   520             if( ( iConvesationInitialized || iAccessorInitialized  ) && iObserverActive )
       
   521                 {
       
   522                 ExternalizeSingleChatItemL(aChangeType, aChatHeader );
       
   523                 }
       
   524             if( iObserverActive )
       
   525                 {
       
   526                 iObserverMessage.Complete( aChangeType );
       
   527                 iObserverActive = EFalse;	
       
   528                 }
       
   529             break;	
       
   530             }
       
   531         case EIMOperationAllChatDeleted:
       
   532             {
       
   533             if( ( iConvesationInitialized || iAccessorInitialized  ) && iObserverActive )
       
   534                 {
       
   535                 iObserverMessage.Complete( aChangeType );
       
   536                 iObserverActive = EFalse;   
       
   537                 }
       
   538             break;
       
   539             }
       
   540         case EIMOperationCompleted :
       
   541             {
       
   542             if( iObserverActive && iConvesationInitialized )
       
   543                 {
       
   544                 iObserverMessage.Complete( aChangeType );
       
   545                 iObserverActive = EFalse;		
       
   546                 }
       
   547             break;	
       
   548             }
       
   549 
       
   550         }
       
   551 
       
   552     }
       
   553 // ---------------------------------------------------------
       
   554 // CIMCacheSession::SendUnreadChangeNotificationL
       
   555 // (other items were commented in a header).
       
   556 // ---------------------------------------------------------
       
   557 //
       
   558 void CIMCacheSession::SendUnreadChangeNotificationL( MIMCacheMessageHeader* aMsgHeader, 
       
   559 												 	CIMCacheMessageBase* aMessage )	
       
   560 	{
       
   561 	// write the message into continuous memory stream
       
   562 	if( iAccessorInitialized  || 
       
   563 		iConvesationInitialized && aMsgHeader->IsChatStarted() )
       
   564 		{
       
   565 		ExternalizeSingleChatDataL(EIMOperationUnreadMessage, aMsgHeader, aMessage );
       
   566 		if( iObserverActive )
       
   567 			{
       
   568 			iObserverMessage.Complete( EIMOperationUnreadMessage );
       
   569 			iObserverActive = EFalse;	
       
   570 			}
       
   571 		}
       
   572 
       
   573 	TRACE( T_LIT( "CIMCacheSession::SendUnreadChangeNotificationL() end") );
       
   574 	}
       
   575 
       
   576 // ---------------------------------------------------------
       
   577 // CIMCacheSession::SendChatListDataL
       
   578 // (other items were commented in a header).
       
   579 // ---------------------------------------------------------
       
   580 //
       
   581 void CIMCacheSession::SendChatListDataL( TIMCacheOperationsCode aChangeType ,
       
   582 									     MIMCacheMessageHeader* aMsgHeader )
       
   583 
       
   584 	{
       
   585 	
       
   586 	TRACE( T_LIT( "CIMCacheSession::SendChatListChangeNotificationL() begin") );
       
   587    
       
   588 	// write the message into continuous memory stream
       
   589 	if( iConvesationInitialized )
       
   590 		{
       
   591 		ExternalizeChatDataL(aChangeType, aMsgHeader );
       
   592 		if( iObserverActive )
       
   593 			{
       
   594 			iObserverMessage.Complete( EIMOperationDataAvailable );
       
   595 			iObserverActive = EFalse;	
       
   596 			}
       
   597 		}
       
   598 	else if( iObserverActive && iAccessorInitialized  )
       
   599 		{
       
   600 		iObserverMessage.Complete( EIMOperationUnreadChange );
       
   601 		iObserverActive = EFalse;		
       
   602 		}
       
   603 		
       
   604 	TRACE( T_LIT( "CIMCacheSession::SendChatListChangeNotificationL() end") );
       
   605 	
       
   606 	}
       
   607 
       
   608 // ---------------------------------------------------------
       
   609 // CIMCacheSession::ExternalizeSingleChatItemL
       
   610 // (other items were commented in a header).
       
   611 // ---------------------------------------------------------
       
   612 //
       
   613 void CIMCacheSession::ExternalizeSingleChatItemL( TIMCacheOperationsCode aChangeType, MIMCacheMessageHeader* aMsgHeader )
       
   614 	{
       
   615 
       
   616 	 TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() Start") );
       
   617 
       
   618     RPointerArray<HBufC8> bufferedHeaderArray;
       
   619     bufferedHeaderArray.Reset();
       
   620     
       
   621 
       
   622     TInt availableSize = KMaxSize - 2*sizeof(TInt);//messageCount + more.
       
   623     TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() availableSize ---- %d"),availableSize );
       
   624 
       
   625     TBool more = EFalse;
       
   626 
       
   627     if( aMsgHeader != NULL )
       
   628         {
       
   629         TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() inside if aMsgHeader") );
       
   630         TInt dataSize = sizeof( TInt )+ //for service id.               
       
   631                     sizeof( TInt )+ aMsgHeader->BuddyId().Size() ; // for buddyId length and buddyId.
       
   632 
       
   633         availableSize = availableSize - dataSize - sizeof( dataSize );
       
   634         TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() inside if aMsgHeader availableSize --- %d"), availableSize );
       
   635 
       
   636         if( availableSize > 0 )
       
   637             {
       
   638             HBufC8* tempBuf = GetPackedChatItemBufferL( aMsgHeader , dataSize );
       
   639             bufferedHeaderArray.AppendL( tempBuf );
       
   640             }
       
   641         }
       
   642 	    
       
   643 	    ExternalizeBufferedDataL(aChangeType, bufferedHeaderArray , KMaxSize , more );
       
   644 	    TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() after ExternalizeBufferedDataL") );
       
   645 	    bufferedHeaderArray.ResetAndDestroy(); // delete all buffered contacts.
       
   646 	    TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() after bufferedHeaderArray.resetanddestroy") );
       
   647 	    TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() Start") );
       
   648 	}
       
   649 // ---------------------------------------------------------
       
   650 // CIMCacheSession::ExternalizeChatItemL
       
   651 // (other items were commented in a header).
       
   652 // ---------------------------------------------------------
       
   653 //
       
   654 void CIMCacheSession::ExternalizeChatItemL( TIMCacheOperationsCode aChangeType, 
       
   655 										    TInt aServiceId /*= KErrNotFound*/ )
       
   656 	{
       
   657 	
       
   658     TRACE( T_LIT( "CIMCacheSession::ExternalizeL() begin") );
       
   659     
       
   660    	TInt availableSize = KMaxSize - 2*sizeof(TInt);
       
   661    
       
   662    	RPointerArray<HBufC8> bufferedHeaderArray;
       
   663    	
       
   664 	TBool more = EFalse;
       
   665 	TInt i = 0;
       
   666     if( aChangeType == EIMOperationFetchAll )
       
   667         {
       
   668         RPointerArray<MIMCacheMessageHeader> tempHeaderArray;
       
   669     	iHelper->GetServiceChatHeaderArrayL( aServiceId , tempHeaderArray );
       
   670   
       
   671         TInt chatCount = tempHeaderArray.Count();
       
   672          
       
   673         for( i=0; i < chatCount; i++ )
       
   674 			{
       
   675 			MIMCacheMessageHeader* chatHeader = tempHeaderArray[ i ];
       
   676 
       
   677 			TInt dataSize = sizeof( TInt )+ //for service id.               
       
   678 			sizeof( TInt )+ chatHeader->BuddyId().Size() ; // for buddyId length and buddyId.
       
   679 
       
   680 			availableSize = availableSize - dataSize - sizeof( dataSize );
       
   681 
       
   682 			if( availableSize > 0 )
       
   683 				{
       
   684 				HBufC8* tempBuf = GetPackedChatItemBufferL( chatHeader , dataSize );
       
   685 				bufferedHeaderArray.AppendL( tempBuf );
       
   686 				}
       
   687 			else
       
   688 				{
       
   689 				more = ETrue;
       
   690 				ExternalizeBufferedDataL(aChangeType, bufferedHeaderArray , KMaxSize , more );
       
   691 				bufferedHeaderArray.ResetAndDestroy(); // delete all buffered contacts.
       
   692 				availableSize = KMaxSize - 2*sizeof(TInt);
       
   693 				//preserve information for next trial.
       
   694 				}
       
   695 			}
       
   696         if( i == chatCount )
       
   697             {
       
   698             more = EFalse;
       
   699             ExternalizeBufferedDataL(aChangeType, bufferedHeaderArray , KMaxSize , more );
       
   700             }
       
   701         }
       
   702  
       
   703     bufferedHeaderArray.ResetAndDestroy(); // delete all buffered contacts.
       
   704     
       
   705     TRACE( T_LIT( "CIMCacheSession::ExternalizeL() end") );
       
   706     }
       
   707 // ---------------------------------------------------------
       
   708 // CIMCacheSession::ExternalizeBufferedDataL
       
   709 // (other items were commented in a header).
       
   710 // ---------------------------------------------------------
       
   711 //
       
   712 void CIMCacheSession::ExternalizeBufferedDataL(TIMCacheOperationsCode aOperationCode,
       
   713 											   RPointerArray<HBufC8>& aArray , 
       
   714 											   TInt aTotalSize , 
       
   715 											   TBool aMore /*= EFalse*/)
       
   716 	{
       
   717 	TRACE( T_LIT( "CIMCacheSession::ExternalizeBufferedDataL() begin") );
       
   718 	TInt appendedMsgCount = aArray.Count(); 
       
   719 	CBufFlat* flatBuf = CBufFlat::NewL(aTotalSize);//( aTotalSize + ( sizeof(TInt) * appendedMsgCount ) + sizeof(TInt) );
       
   720 	CleanupStack::PushL( flatBuf );
       
   721 	RBufWriteStream writeAllStream ;
       
   722 	CleanupClosePushL( writeAllStream );
       
   723 
       
   724 	writeAllStream.Open( *flatBuf );
       
   725 	writeAllStream.WriteInt16L( appendedMsgCount );
       
   726 	
       
   727 	for(TInt i = 0; i< appendedMsgCount ; i++ )
       
   728 		{
       
   729 		HBufC8* buf =  aArray[i];
       
   730 		writeAllStream.WriteInt16L(buf->Length());
       
   731 		writeAllStream.WriteL(*buf);			
       
   732 		}
       
   733 	
       
   734 	writeAllStream.WriteInt16L( aMore );//write more information at end.
       
   735 	
       
   736 	writeAllStream.CommitL(); 
       
   737 
       
   738 	CIMCacheBufferArray* packet = CIMCacheBufferArray::NewL( flatBuf->Ptr(0) , aOperationCode );
       
   739 	iPacketsArray.Append(packet); // ownership to array
       
   740 	
       
   741 	CleanupStack::PopAndDestroy(); // writeAllStream
       
   742 	CleanupStack::PopAndDestroy(); // flatBuf
       
   743 	
       
   744 	TRACE( T_LIT( "CIMCacheSession::ExternalizeBufferedDataL() end") );
       
   745 	
       
   746 	}  	
       
   747  
       
   748 // ---------------------------------------------------------
       
   749 // CIMCacheSession::ExternalizeSingleChatDataL
       
   750 // (other items were commented in a header).
       
   751 // ---------------------------------------------------------
       
   752 void CIMCacheSession::ExternalizeSingleChatDataL(TIMCacheOperationsCode aChangeType, MIMCacheMessageHeader* aChatHeader, CIMCacheMessageBase* aMessage )
       
   753 	{
       
   754 	
       
   755 	TInt availableSize = KMaxSize - 2*sizeof(TInt);//messageCount + more.
       
   756 
       
   757 	TBool more = EFalse;
       
   758 
       
   759 	if( aMessage && aChatHeader )
       
   760 		{
       
   761 		if( aChangeType == EIMOperationMessageAdded )
       
   762 			{
       
   763 			aMessage->SetRead()	;
       
   764 			}
       
   765 		RPointerArray<HBufC8> bufferedChatArray;
       
   766 			
       
   767 		TInt dataSize = sizeof( aMessage->MessageType() )+
       
   768 		                sizeof( aMessage->MessagerType() )+
       
   769 		                sizeof( aMessage->ContentType() )+
       
   770 		                sizeof( aMessage->TimeStamp().Int64() )+  
       
   771 		                sizeof( TInt )+ aChatHeader->BuddyId().Size() + // for buddy  length and buddy name
       
   772 		                sizeof( TInt )+ aMessage->Text().Size() ; // for message length and message
       
   773 		             
       
   774 		availableSize = availableSize - dataSize - sizeof( dataSize );
       
   775 		if( availableSize > 0 )
       
   776 		    {
       
   777 		     HBufC8* messageBuffer = GetPackedChatDataBufferL( aMessage , aChatHeader , dataSize );
       
   778 		     bufferedChatArray.AppendL( messageBuffer );
       
   779 		    }
       
   780 		    
       
   781 		ExternalizeBufferedDataL(aChangeType, bufferedChatArray , KMaxSize , more );
       
   782 		bufferedChatArray.ResetAndDestroy();
       
   783 		}
       
   784 	}
       
   785 
       
   786 // ---------------------------------------------------------
       
   787 // CIMCacheSession::ExternalizeChatDataL
       
   788 // (other items were commented in a header).
       
   789 // ---------------------------------------------------------
       
   790 void CIMCacheSession::ExternalizeChatDataL(TIMCacheOperationsCode aChangeType ,MIMCacheMessageHeader* aChatHeader )
       
   791     {    
       
   792     RPointerArray<HBufC8> bufferedChatArray;
       
   793     bufferedChatArray.Reset();
       
   794     
       
   795     TInt availableSize = KMaxSize - 2*sizeof(TInt);//messageCount + more.
       
   796    
       
   797     TBool more = EFalse;
       
   798      
       
   799     if( aChangeType == EIMOperationFetchAll &&  aChatHeader )
       
   800         { 
       
   801         TInt messageCount = aChatHeader->MessageCount();
       
   802     	
       
   803    		TInt index = 0;
       
   804         for( index = 0 ; index < messageCount  ; index++ )
       
   805             {
       
   806             CIMCacheMessageBase* message = aChatHeader->GetMessage(index);
       
   807             TInt dataSize = sizeof( message->MessageType() )+
       
   808                             sizeof( message->MessagerType() )+
       
   809                             sizeof( message->ContentType() )+
       
   810                             sizeof( message->TimeStamp().Int64() )+  
       
   811                             sizeof( TInt )+ aChatHeader->BuddyId().Size() + // for buddy  length and buddy name
       
   812                             sizeof( TInt )+ message->Text().Size() ; // for message length and message
       
   813                          
       
   814             availableSize = availableSize - dataSize - sizeof( dataSize );
       
   815             if( availableSize > 0 )
       
   816                 {
       
   817                 // mark message read
       
   818     			 message->SetRead();
       
   819                  HBufC8* messageBuffer = GetPackedChatDataBufferL( message , aChatHeader , dataSize );
       
   820                  bufferedChatArray.AppendL( messageBuffer );
       
   821                  }
       
   822             else
       
   823                 {
       
   824                 //else set information for more and message nest to last sent message i.e index
       
   825                 more = ETrue;
       
   826                 ExternalizeBufferedDataL(aChangeType, bufferedChatArray , KMaxSize , more );
       
   827     			bufferedChatArray.ResetAndDestroy();  
       
   828     			availableSize = KMaxSize - 2*sizeof(TInt); //messageCount + more.  
       
   829                 index--;
       
   830                 }
       
   831             }
       
   832         if( index == messageCount )
       
   833 			{
       
   834 			more = EFalse;
       
   835 			ExternalizeBufferedDataL(aChangeType, bufferedChatArray , KMaxSize , more );
       
   836 			}
       
   837         }
       
   838      bufferedChatArray.ResetAndDestroy();    
       
   839     }
       
   840 // ---------------------------------------------------------
       
   841 // CIMCacheSession::GetPackedChatDataBufferL
       
   842 // (other items were commented in a header).
       
   843 // ---------------------------------------------------------
       
   844 HBufC8* CIMCacheSession::GetPackedChatDataBufferL(CIMCacheMessageBase* aMsg , MIMCacheMessageHeader* aActiveHeader , const TInt aSize)
       
   845     {
       
   846     TRACE( T_LIT( "CIMCacheSession::GetPackedChatDataBufferL() begin") );
       
   847     
       
   848 
       
   849     CBufFlat*   flatBuf = CBufFlat::NewL( aSize );
       
   850     CleanupStack::PushL( flatBuf );
       
   851 
       
   852     RBufWriteStream writeStream ;
       
   853     CleanupClosePushL( writeStream );
       
   854 
       
   855     writeStream.Open( *flatBuf );
       
   856 
       
   857     writeStream.WriteInt16L( aMsg->MessageType() );  
       
   858     writeStream.WriteInt16L( aMsg->MessagerType() );
       
   859     writeStream.WriteInt16L( aMsg->ContentType() );   
       
   860     writeStream.WriteReal64L( aMsg->TimeStamp().Int64() );
       
   861     writeStream.WriteInt16L( aActiveHeader->BuddyId().Length() );    
       
   862     writeStream.WriteL( aActiveHeader->BuddyId() );
       
   863     writeStream.WriteInt16L( aMsg->Text().Length() );    
       
   864     writeStream.WriteL( aMsg->Text() );
       
   865     
       
   866     writeStream.CommitL();
       
   867 
       
   868     HBufC8* tempBuffer = flatBuf->Ptr(0).AllocL();
       
   869     
       
   870     CleanupStack::PopAndDestroy(); // flatBuf
       
   871     CleanupStack::PopAndDestroy(); // writeStream
       
   872     
       
   873     TRACE( T_LIT( "CIMCacheSession::GetPackedChatDataBufferL() end") ); 
       
   874     return tempBuffer;
       
   875     }
       
   876 
       
   877 // ---------------------------------------------------------
       
   878 // CIMCacheSession::GetPackedChatItemBufferL
       
   879 // (other items were commented in a header).
       
   880 // ---------------------------------------------------------
       
   881 //
       
   882 HBufC8* CIMCacheSession::GetPackedChatItemBufferL(MIMCacheMessageHeader* aChatHeader, TInt& aSize)
       
   883     {
       
   884     TRACE( T_LIT( "CIMCacheSession::GetPackedChatItemBufferL() end") );
       
   885     
       
   886     TInt  serviceId   = aChatHeader->ServiceId();
       
   887     const TDesC& buddyId = aChatHeader->BuddyId();
       
   888              
       
   889      CBufFlat*   flatBuf = CBufFlat::NewL( aSize );
       
   890      CleanupStack::PushL( flatBuf );
       
   891      
       
   892      RBufWriteStream writeStream ;
       
   893      CleanupClosePushL( writeStream );
       
   894      writeStream.Open( *flatBuf );
       
   895         
       
   896      writeStream.WriteInt16L( serviceId );
       
   897      writeStream.WriteInt16L( buddyId.Length() );
       
   898      writeStream.WriteL( buddyId );       
       
   899      writeStream.CommitL();
       
   900      
       
   901      HBufC8* chatHeaderBuffer = flatBuf->Ptr(0).AllocL();
       
   902     
       
   903      CleanupStack::PopAndDestroy(2); // writeAllStream,flatBuf
       
   904      
       
   905      TRACE( T_LIT( "CIMCacheSession::GetPackedChatItemBufferL() end") );
       
   906      
       
   907      return chatHeaderBuffer;
       
   908     }
       
   909 
       
   910 
       
   911 // end of file