imservices/instantmessagingcache/imcacheserver/src/cimcachesession.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 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 				if( packet->OperationCode() ==  EIMOperationFetchAll )
       
   245 					{
       
   246 					iObserverMessage.Complete( EIMOperationDataAvailable );
       
   247 					}
       
   248 				else
       
   249 					{
       
   250 					iObserverMessage.Complete( packet->OperationCode() );
       
   251 					}
       
   252 				iObserverActive = EFalse;
       
   253 				}
       
   254 			
       
   255 			break;
       
   256         	}
       
   257         case EIMCacheObserveMessageUpdateUnRegister :
       
   258 	        {
       
   259 	        TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheObserveMessageUpdateUnRegister excuted") );
       
   260 	        if( !iObserverActive )
       
   261         		{
       
   262         		TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer not active ") );
       
   263         		}
       
   264         	else
       
   265 	        	{
       
   266 	        	TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer active ") );
       
   267 				iObserverMessage.Complete( EIMOperationUnRegistered );
       
   268 				iObserverActive = EFalse;		
       
   269 	        	}
       
   270 	        
       
   271             break;	
       
   272 	        }
       
   273 	     case EIMCacheObserveMessageAccessRegister:
       
   274         	{
       
   275         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheObserveMessageAccessRegister excuted") );
       
   276         	if( iObserverActive )
       
   277         		{
       
   278         		TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer already active") );
       
   279         		// complete the old observer request with KErrCancel
       
   280         		iObserverMessage.Complete( KErrAlreadyExists );
       
   281         		}
       
   282         	iObserverMessage = aMessage;
       
   283         	iObserverActive = ETrue;
       
   284             msgNeedsToBeCompleted = EFalse;
       
   285 			 if( iPacketsArray.Count() )
       
   286 				{
       
   287 				CIMCacheBufferArray* packet = iPacketsArray[ 0 ] ;
       
   288 				if( packet->OperationCode() ==  EIMOperationFetchAll )
       
   289 					{
       
   290 					iObserverMessage.Complete( EIMOperationUnreadChange );
       
   291 					}
       
   292 				else
       
   293 					{
       
   294 					iObserverMessage.Complete( packet->OperationCode() );
       
   295 					}
       
   296 				iObserverActive = EFalse;
       
   297 				}
       
   298 	     	break;
       
   299         	}
       
   300          case EIMCacheObserveMessageAccessUnRegister :
       
   301 	        {
       
   302 	        TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheObserveMessageAccessUnRegister excuted") );
       
   303 	        if( !iObserverActive )
       
   304         		{
       
   305         		TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer not active ") );
       
   306         		}
       
   307         	else
       
   308 	        	{
       
   309 	        	TRACE( T_LIT("CIMCacheSession::DispatchMessageL observer active ") );
       
   310 				iObserverMessage.Complete( EIMOperationUnRegistered );
       
   311 				iObserverActive = EFalse;		
       
   312 	        	}
       
   313 	  
       
   314             break;	
       
   315 	        }
       
   316        
       
   317         case EIMCacheGetUnreadCount:
       
   318         	{
       
   319         	iHelper->GetUnreadMessageCountL(aMessage);
       
   320         	break;
       
   321         	}
       
   322         case EIMCacheGetAllUnreadCount:
       
   323         	{
       
   324         	iHelper->GetAllUnreadMessageCountL(aMessage);
       
   325         	break;
       
   326         	}
       
   327        	   // check for conversation 
       
   328         case EIMCacheIsConversationExist :
       
   329 	        {
       
   330 	        TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheIsConversationExist excuted ") );
       
   331 	       	iHelper->CheckConversationExistL(aMessage);
       
   332 	    	break;	
       
   333 	        }
       
   334         // Get what change happen        	
       
   335         case EIMCacheGetBufferedPackets:
       
   336         	{
       
   337         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheGetBufferedPackets excuted") );
       
   338         	if( iPacketsArray.Count() )
       
   339 				{
       
   340 				CIMCacheBufferArray* packet = iPacketsArray[ 0 ] ;
       
   341 				aMessage.WriteL(0, packet->PacketData() );
       
   342 				iPacketsArray.Remove(0);
       
   343 				delete packet;
       
   344 				packet = NULL;
       
   345 				iPacketsArray.Compress();
       
   346 				}
       
   347         	break;
       
   348         	}       
       
   349            	
       
   350         case EIMCacheGetChatList:
       
   351         	{
       
   352         	TRACE( T_LIT("CIMCacheSession::DispatchMessageL EIMCacheGetChatList executed") );
       
   353         	//0 serviceId 
       
   354         	//1 buffer
       
   355         	TInt serviceId = aMessage.Int0();       	
       
   356         	
       
   357         	ExternalizeChatItemL( EIMOperationFetchAll , serviceId );
       
   358         	
       
   359         	//aMessage.WriteL( 1, *iChatDataBuffer); 
       
   360         	TInt packetsCounts = iPacketsArray.Count();
       
   361         
       
   362         	if( packetsCounts )
       
   363 				{
       
   364 				CIMCacheBufferArray* packet = iPacketsArray[ 0 ] ;
       
   365 				aMessage.WriteL( 1, packet->PacketData() );	
       
   366 				iPacketsArray.Remove(0);
       
   367 				delete packet;
       
   368 				packet = NULL;
       
   369 				iPacketsArray.Compress();
       
   370 				}
       
   371             break;	
       
   372         	}
       
   373         case EIMCacheCancelRequest:
       
   374             {
       
   375             TRACE( T_LIT("CIMCacheSession::EIMCacheCancelRequest start"));
       
   376             if(iObserverActive)
       
   377                 {
       
   378                 TRACE( T_LIT("CIMCacheSession::EIMCacheCancelRequest Inside if"));
       
   379                 iObserverMessage.Complete( KErrCancel );
       
   380                 TRACE( T_LIT("CIMCacheSession::EIMCacheCancelRequest After iObserverMessage.Complete"));
       
   381                 iObserverActive = EFalse;   
       
   382                 }
       
   383             TRACE( T_LIT("CIMCacheSession::EIMCacheCancelRequest End"));
       
   384 
       
   385           
       
   386             break;
       
   387             }
       
   388         default:
       
   389             {
       
   390             TRACE( T_LIT("CIMCacheSession::DispatchMessageL default excuted") );
       
   391             User::Leave( KErrNotSupported );
       
   392             break;
       
   393             }
       
   394         }
       
   395 	TRACE( T_LIT("CIMCacheSession::DispatchMessageL	end"));
       
   396     return msgNeedsToBeCompleted;
       
   397    
       
   398     }
       
   399  //=======================================================================
       
   400 //==========================initialization ==================================
       
   401 //========================================================================
       
   402 
       
   403 // ---------------------------------------------------------
       
   404 // CIMCacheSession::InitSessionL
       
   405 // (other items were commented in a header).
       
   406 // ---------------------------------------------------------
       
   407 //
       
   408 void CIMCacheSession::InitSessionL(const RMessage2& aMessage )
       
   409 	{
       
   410     TRACE( T_LIT("CIMCacheSession::InitSessionL:start") );
       
   411     // at zero place service id was appended
       
   412     iServiceId = aMessage.Int0();
       
   413 	if( Server() )
       
   414         {
       
   415         iHelper = Server()->GetActiveHelper();
       
   416         }
       
   417     TRACE( T_LIT("CIMCacheSession::InitSessionL end") );
       
   418 	} 
       
   419 
       
   420 
       
   421 // ---------------------------------------------------------
       
   422 // CIMCacheSession::InitSessionL
       
   423 // (other items were commented in a header).
       
   424 // ---------------------------------------------------------
       
   425 //
       
   426 TInt CIMCacheSession::ServiceId() const
       
   427     {
       
   428     TRACE( T_LIT("CIMCacheSession::ServiceId:start") );
       
   429     TRACE( T_LIT("CIMCacheSession::ServiceId end") );
       
   430     return iServiceId;
       
   431     } 
       
   432 // ---------------------------------------------------------------------------
       
   433 // CIMCacheSession::ResetHeaderInoformation()
       
   434 // ---------------------------------------------------------------------------
       
   435 //
       
   436 void CIMCacheSession::ResetHeaderInoformation()
       
   437                                                       
       
   438     {
       
   439     TRACE( T_LIT( "CIMCacheSession::ResetHeaderInoformation begin") );
       
   440     if( iConvesationInitialized )
       
   441 	    {
       
   442 	    iHelper->ResetHeaderInoformation();	
       
   443 	    }
       
   444    	TRACE( T_LIT( "CIMCacheSession::ResetHeaderInoformation end") );
       
   445     }
       
   446  
       
   447  
       
   448 // ---------------------------------------------------------
       
   449 // CIMCacheSession::PackAndNotifyEventL
       
   450 // (other items were commented in a header).
       
   451 // ---------------------------------------------------------
       
   452 //
       
   453 void CIMCacheSession::PackAndNotifyEventL(TIMCacheOperationsCode aChangeType ,
       
   454 										  MIMCacheMessageHeader* aChatHeader /*=NULL*/, 
       
   455 									 	  CIMCacheMessageBase* aMessage /*=NULL*/ ) 
       
   456 
       
   457     {	
       
   458     TRACE( T_LIT( "CIMCacheSession::PackAndNotifyEventL() begin") );
       
   459 
       
   460     switch( aChangeType )
       
   461         {
       
   462         case EIMOperationFetchAll :
       
   463             {
       
   464             SendChatListDataL(aChangeType, aChatHeader);
       
   465             break;	
       
   466             }
       
   467         case EIMOperationUnreadChange :
       
   468             {
       
   469             if(  iObserverActive && iConvesationInitialized )
       
   470                 {
       
   471                 iObserverMessage.Complete( aChangeType );
       
   472                 iObserverActive = EFalse;		
       
   473                 }
       
   474             else if( iObserverActive && iAccessorInitialized  )
       
   475                 {
       
   476                 ExternalizeSingleChatItemL(aChangeType, aChatHeader );
       
   477                 iObserverMessage.Complete( aChangeType );
       
   478                 iObserverActive = EFalse;
       
   479                 }
       
   480             break;	
       
   481             }
       
   482         case EIMOperationUnreadMessage :
       
   483             {
       
   484             SendUnreadChangeNotificationL( aChatHeader, aMessage );
       
   485             break;	
       
   486             }
       
   487         case EIMOperationChatStarted :
       
   488             {
       
   489             if( iObserverActive && iConvesationInitialized )
       
   490                 {
       
   491                 iObserverMessage.Complete( aChangeType );
       
   492                 iObserverActive = EFalse;		
       
   493                 }
       
   494             else if( iObserverActive && iAccessorInitialized )
       
   495                 {
       
   496                 ExternalizeSingleChatItemL(aChangeType, aChatHeader );
       
   497                 iObserverMessage.Complete( aChangeType );
       
   498                 iObserverActive = EFalse;   
       
   499                 }
       
   500             break;	
       
   501             }
       
   502         case EIMOperationChatAdded :
       
   503             {
       
   504             if(  iConvesationInitialized )
       
   505                 {
       
   506                 ExternalizeSingleChatItemL(aChangeType, aChatHeader);
       
   507                 if( iObserverActive )
       
   508                     {
       
   509                     iObserverMessage.Complete( aChangeType );
       
   510                     iObserverActive = EFalse;	
       
   511                     }
       
   512                 }
       
   513             else if( iAccessorInitialized )
       
   514                 {
       
   515                 SendUnreadChangeNotificationL(aChatHeader, aMessage );
       
   516                 }
       
   517             break;	
       
   518             }
       
   519         case EIMOperationMessageAdded :
       
   520             {
       
   521             if( iConvesationInitialized )
       
   522                 {
       
   523                 ExternalizeSingleChatDataL(aChangeType, aChatHeader , aMessage );
       
   524                 if( iObserverActive )
       
   525                     {
       
   526                     iObserverMessage.Complete( aChangeType );
       
   527                     iObserverActive = EFalse;
       
   528                     }
       
   529                 }
       
   530             break;	
       
   531             }
       
   532         case EIMOperationChatDeleted :
       
   533             {
       
   534             if( ( iConvesationInitialized || iAccessorInitialized  ) && iObserverActive )
       
   535                 {
       
   536                 ExternalizeSingleChatItemL(aChangeType, aChatHeader );
       
   537                 }
       
   538             if( iObserverActive )
       
   539                 {
       
   540                 iObserverMessage.Complete( aChangeType );
       
   541                 iObserverActive = EFalse;	
       
   542                 }
       
   543             break;	
       
   544             }
       
   545         case EIMOperationAllChatDeleted:
       
   546             {
       
   547             if( ( iConvesationInitialized || iAccessorInitialized  ) && iObserverActive )
       
   548                 {
       
   549                 iObserverMessage.Complete( aChangeType );
       
   550                 iObserverActive = EFalse;   
       
   551                 }
       
   552             break;
       
   553             }
       
   554         case EIMOperationCompleted :
       
   555             {
       
   556             if( iObserverActive && iConvesationInitialized )
       
   557                 {
       
   558                 iObserverMessage.Complete( aChangeType );
       
   559                 iObserverActive = EFalse;		
       
   560                 }
       
   561             break;	
       
   562             }
       
   563 
       
   564         }
       
   565 
       
   566     }
       
   567 // ---------------------------------------------------------
       
   568 // CIMCacheSession::SendUnreadChangeNotificationL
       
   569 // (other items were commented in a header).
       
   570 // ---------------------------------------------------------
       
   571 //
       
   572 void CIMCacheSession::SendUnreadChangeNotificationL( MIMCacheMessageHeader* aMsgHeader, 
       
   573 												 	CIMCacheMessageBase* aMessage )	
       
   574 	{
       
   575 	// write the message into continuous memory stream
       
   576 	if( iAccessorInitialized  || 
       
   577 		(iConvesationInitialized && aMsgHeader->IsChatStarted()) )
       
   578 		{
       
   579 		ExternalizeSingleChatDataL(EIMOperationUnreadMessage, aMsgHeader, aMessage );
       
   580 		if( iObserverActive )
       
   581 			{
       
   582 			iObserverMessage.Complete( EIMOperationUnreadMessage );
       
   583 			iObserverActive = EFalse;	
       
   584 			}
       
   585 		}
       
   586 
       
   587 	TRACE( T_LIT( "CIMCacheSession::SendUnreadChangeNotificationL() end") );
       
   588 	}
       
   589 
       
   590 // ---------------------------------------------------------
       
   591 // CIMCacheSession::SendChatListDataL
       
   592 // (other items were commented in a header).
       
   593 // ---------------------------------------------------------
       
   594 //
       
   595 void CIMCacheSession::SendChatListDataL( TIMCacheOperationsCode aChangeType ,
       
   596 									     MIMCacheMessageHeader* aMsgHeader )
       
   597 
       
   598 	{
       
   599 	
       
   600 	TRACE( T_LIT( "CIMCacheSession::SendChatListChangeNotificationL() begin") );
       
   601    
       
   602 	// write the message into continuous memory stream
       
   603 	if( iConvesationInitialized )
       
   604 		{
       
   605 		ExternalizeChatDataL(aChangeType, aMsgHeader );
       
   606 		if( iObserverActive )
       
   607 			{
       
   608 			iObserverMessage.Complete( EIMOperationDataAvailable );
       
   609 			iObserverActive = EFalse;	
       
   610 			}
       
   611 		}
       
   612 	else if( iObserverActive && iAccessorInitialized  )
       
   613 		{
       
   614         ExternalizeSingleChatItemL(aChangeType, aMsgHeader );
       
   615 		iObserverMessage.Complete( EIMOperationUnreadChange );
       
   616 		iObserverActive = EFalse;		
       
   617 		}
       
   618 		
       
   619 	TRACE( T_LIT( "CIMCacheSession::SendChatListChangeNotificationL() end") );
       
   620 	
       
   621 	}
       
   622 
       
   623 // ---------------------------------------------------------
       
   624 // CIMCacheSession::ExternalizeSingleChatItemL
       
   625 // (other items were commented in a header).
       
   626 // ---------------------------------------------------------
       
   627 //
       
   628 void CIMCacheSession::ExternalizeSingleChatItemL( TIMCacheOperationsCode aChangeType, MIMCacheMessageHeader* aMsgHeader )
       
   629 	{
       
   630 
       
   631 	 TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() Start") );
       
   632 
       
   633     RPointerArray<HBufC8> bufferedHeaderArray;
       
   634     bufferedHeaderArray.Reset();
       
   635     
       
   636 
       
   637     TInt availableSize = KMaxSize - 2*sizeof(TInt);//messageCount + more.
       
   638     TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() availableSize ---- %d"),availableSize );
       
   639 
       
   640     TBool more = EFalse;
       
   641 
       
   642     if( aMsgHeader != NULL )
       
   643         {
       
   644         TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() inside if aMsgHeader") );
       
   645         TInt dataSize = sizeof( TInt )+ //for service id.               
       
   646                     sizeof( TInt )+ aMsgHeader->BuddyId().Size() ; // for buddyId length and buddyId.
       
   647 
       
   648         availableSize = availableSize - dataSize - sizeof( dataSize );
       
   649         TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() inside if aMsgHeader availableSize --- %d"), availableSize );
       
   650 
       
   651         if( availableSize > 0 )
       
   652             {
       
   653             HBufC8* tempBuf = GetPackedChatItemBufferL( aMsgHeader , dataSize );
       
   654             bufferedHeaderArray.AppendL( tempBuf );
       
   655             }
       
   656         }
       
   657 	    
       
   658 	    ExternalizeBufferedDataL(aChangeType, bufferedHeaderArray , KMaxSize , more );
       
   659 	    TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() after ExternalizeBufferedDataL") );
       
   660 	    bufferedHeaderArray.ResetAndDestroy(); // delete all buffered contacts.
       
   661 	    TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() after bufferedHeaderArray.resetanddestroy") );
       
   662 	    TRACE( T_LIT( "CIMCacheSession::ExternalizeSingleChatItemL() Start") );
       
   663 	}
       
   664 // ---------------------------------------------------------
       
   665 // CIMCacheSession::ExternalizeChatItemL
       
   666 // (other items were commented in a header).
       
   667 // ---------------------------------------------------------
       
   668 //
       
   669 void CIMCacheSession::ExternalizeChatItemL( TIMCacheOperationsCode aChangeType, 
       
   670 										    TInt aServiceId /*= KErrNotFound*/ )
       
   671 	{
       
   672 	
       
   673     TRACE( T_LIT( "CIMCacheSession::ExternalizeL() begin") );
       
   674     
       
   675    	TInt availableSize = KMaxSize - 2*sizeof(TInt);
       
   676    
       
   677    	RPointerArray<HBufC8> bufferedHeaderArray;
       
   678    	
       
   679 	TBool more = EFalse;
       
   680 	TInt i = 0;
       
   681     if( aChangeType == EIMOperationFetchAll )
       
   682         {
       
   683         RPointerArray<MIMCacheMessageHeader> tempHeaderArray;
       
   684     	iHelper->GetServiceChatHeaderArrayL( aServiceId , tempHeaderArray );
       
   685   
       
   686         TInt chatCount = tempHeaderArray.Count();
       
   687          
       
   688         for( i=0; i < chatCount; i++ )
       
   689 			{
       
   690 			MIMCacheMessageHeader* chatHeader = tempHeaderArray[ i ];
       
   691 
       
   692 			TInt dataSize = sizeof( TInt )+ //for service id.               
       
   693 			sizeof( TInt )+ chatHeader->BuddyId().Size() ; // for buddyId length and buddyId.
       
   694 
       
   695 			availableSize = availableSize - dataSize - sizeof( dataSize );
       
   696 
       
   697 			if( availableSize > 0 )
       
   698 				{
       
   699 				HBufC8* tempBuf = GetPackedChatItemBufferL( chatHeader , dataSize );
       
   700 				bufferedHeaderArray.AppendL( tempBuf );
       
   701 				}
       
   702 			else
       
   703 				{
       
   704 				more = ETrue;
       
   705 				ExternalizeBufferedDataL(aChangeType, bufferedHeaderArray , KMaxSize , more );
       
   706 				bufferedHeaderArray.ResetAndDestroy(); // delete all buffered contacts.
       
   707 				availableSize = KMaxSize - 2*sizeof(TInt);
       
   708 				//preserve information for next trial.
       
   709 				}
       
   710 			}
       
   711         if( i == chatCount )
       
   712             {
       
   713             more = EFalse;
       
   714             ExternalizeBufferedDataL(aChangeType, bufferedHeaderArray , KMaxSize , more );
       
   715             }
       
   716         }
       
   717  
       
   718     bufferedHeaderArray.ResetAndDestroy(); // delete all buffered contacts.
       
   719     
       
   720     TRACE( T_LIT( "CIMCacheSession::ExternalizeL() end") );
       
   721     }
       
   722 // ---------------------------------------------------------
       
   723 // CIMCacheSession::ExternalizeBufferedDataL
       
   724 // (other items were commented in a header).
       
   725 // ---------------------------------------------------------
       
   726 //
       
   727 void CIMCacheSession::ExternalizeBufferedDataL(TIMCacheOperationsCode aOperationCode,
       
   728 											   RPointerArray<HBufC8>& aArray , 
       
   729 											   TInt aTotalSize , 
       
   730 											   TBool aMore /*= EFalse*/)
       
   731 	{
       
   732 	TRACE( T_LIT( "CIMCacheSession::ExternalizeBufferedDataL() begin") );
       
   733 	TInt appendedMsgCount = aArray.Count(); 
       
   734 	CBufFlat* flatBuf = CBufFlat::NewL(aTotalSize);//( aTotalSize + ( sizeof(TInt) * appendedMsgCount ) + sizeof(TInt) );
       
   735 	CleanupStack::PushL( flatBuf );
       
   736 	RBufWriteStream writeAllStream ;
       
   737 	CleanupClosePushL( writeAllStream );
       
   738 
       
   739 	writeAllStream.Open( *flatBuf );
       
   740 	writeAllStream.WriteInt16L( appendedMsgCount );
       
   741 	
       
   742 	for(TInt i = 0; i< appendedMsgCount ; i++ )
       
   743 		{
       
   744 		HBufC8* buf =  aArray[i];
       
   745 		writeAllStream.WriteInt16L(buf->Length());
       
   746 		writeAllStream.WriteL(*buf);			
       
   747 		}
       
   748 	
       
   749 	writeAllStream.WriteInt16L( aMore );//write more information at end.
       
   750 	
       
   751 	writeAllStream.CommitL(); 
       
   752 
       
   753 	CIMCacheBufferArray* packet = CIMCacheBufferArray::NewL( flatBuf->Ptr(0) , aOperationCode );
       
   754 	iPacketsArray.Append(packet); // ownership to array
       
   755 	
       
   756 	CleanupStack::PopAndDestroy(); // writeAllStream
       
   757 	CleanupStack::PopAndDestroy(); // flatBuf
       
   758 	
       
   759 	TRACE( T_LIT( "CIMCacheSession::ExternalizeBufferedDataL() end") );
       
   760 	
       
   761 	}  	
       
   762  
       
   763 // ---------------------------------------------------------
       
   764 // CIMCacheSession::ExternalizeSingleChatDataL
       
   765 // (other items were commented in a header).
       
   766 // ---------------------------------------------------------
       
   767 void CIMCacheSession::ExternalizeSingleChatDataL(TIMCacheOperationsCode aChangeType, MIMCacheMessageHeader* aChatHeader, CIMCacheMessageBase* aMessage )
       
   768 	{
       
   769 	
       
   770 	TInt availableSize = KMaxSize - 2*sizeof(TInt);//messageCount + more.
       
   771 
       
   772 	TBool more = EFalse;
       
   773 
       
   774 	if( aMessage && aChatHeader )
       
   775 		{
       
   776 		if( aChangeType == EIMOperationMessageAdded )
       
   777 			{
       
   778 			aMessage->SetRead()	;
       
   779 			}
       
   780 		RPointerArray<HBufC8> bufferedChatArray;
       
   781 			
       
   782 		TInt dataSize = sizeof( aMessage->MessageType() )+
       
   783 		                sizeof( aMessage->MessagerType() )+
       
   784 		                sizeof( aMessage->ContentType() )+
       
   785 		                sizeof( aMessage->TimeStamp().Int64() )+  
       
   786 		                sizeof( TInt )+ aChatHeader->BuddyId().Size() + // for buddy  length and buddy name
       
   787 		                sizeof( TInt )+ aMessage->Text().Size() ; // for message length and message
       
   788 		             
       
   789 		availableSize = availableSize - dataSize - sizeof( dataSize );
       
   790 		if( availableSize > 0 )
       
   791 		    {
       
   792 		     HBufC8* messageBuffer = GetPackedChatDataBufferL( aMessage , aChatHeader , dataSize );
       
   793 		     bufferedChatArray.AppendL( messageBuffer );
       
   794 		    }
       
   795 		    
       
   796 		ExternalizeBufferedDataL(aChangeType, bufferedChatArray , KMaxSize , more );
       
   797 		bufferedChatArray.ResetAndDestroy();
       
   798 		}
       
   799 	}
       
   800 
       
   801 // ---------------------------------------------------------
       
   802 // CIMCacheSession::ExternalizeChatDataL
       
   803 // (other items were commented in a header).
       
   804 // ---------------------------------------------------------
       
   805 void CIMCacheSession::ExternalizeChatDataL(TIMCacheOperationsCode aChangeType ,MIMCacheMessageHeader* aChatHeader )
       
   806     {    
       
   807     RPointerArray<HBufC8> bufferedChatArray;
       
   808     bufferedChatArray.Reset();
       
   809     
       
   810     TInt availableSize = KMaxSize - 2*sizeof(TInt);//messageCount + more.
       
   811    
       
   812     TBool more = EFalse;
       
   813      
       
   814     if( aChangeType == EIMOperationFetchAll &&  aChatHeader )
       
   815         { 
       
   816         TInt messageCount = aChatHeader->MessageCount();
       
   817     	
       
   818    		TInt index = 0;
       
   819         for( index = 0 ; index < messageCount  ; index++ )
       
   820             {
       
   821             CIMCacheMessageBase* message = aChatHeader->GetMessage(index);
       
   822             TInt dataSize = sizeof( message->MessageType() )+
       
   823                             sizeof( message->MessagerType() )+
       
   824                             sizeof( message->ContentType() )+
       
   825                             sizeof( message->TimeStamp().Int64() )+  
       
   826                             sizeof( TInt )+ aChatHeader->BuddyId().Size() + // for buddy  length and buddy name
       
   827                             sizeof( TInt )+ message->Text().Size() ; // for message length and message
       
   828                          
       
   829             availableSize = availableSize - dataSize - sizeof( dataSize );
       
   830             if( availableSize > 0 )
       
   831                 {
       
   832                 // mark message read
       
   833     			 message->SetRead();
       
   834                  HBufC8* messageBuffer = GetPackedChatDataBufferL( message , aChatHeader , dataSize );
       
   835                  bufferedChatArray.AppendL( messageBuffer );
       
   836                  }
       
   837             else
       
   838                 {
       
   839                 //else set information for more and message nest to last sent message i.e index
       
   840                 more = ETrue;
       
   841                 ExternalizeBufferedDataL(aChangeType, bufferedChatArray , KMaxSize , more );
       
   842     			bufferedChatArray.ResetAndDestroy();  
       
   843     			availableSize = KMaxSize - 2*sizeof(TInt); //messageCount + more.  
       
   844                 index--;
       
   845                 }
       
   846             }
       
   847         if( index == messageCount )
       
   848 			{
       
   849 			more = EFalse;
       
   850 			ExternalizeBufferedDataL(aChangeType, bufferedChatArray , KMaxSize , more );
       
   851 			}
       
   852         }
       
   853      bufferedChatArray.ResetAndDestroy();    
       
   854     }
       
   855 // ---------------------------------------------------------
       
   856 // CIMCacheSession::GetPackedChatDataBufferL
       
   857 // (other items were commented in a header).
       
   858 // ---------------------------------------------------------
       
   859 HBufC8* CIMCacheSession::GetPackedChatDataBufferL(CIMCacheMessageBase* aMsg , MIMCacheMessageHeader* aActiveHeader , const TInt aSize)
       
   860     {
       
   861     TRACE( T_LIT( "CIMCacheSession::GetPackedChatDataBufferL() begin") );
       
   862     
       
   863 
       
   864     CBufFlat*   flatBuf = CBufFlat::NewL( aSize );
       
   865     CleanupStack::PushL( flatBuf );
       
   866 
       
   867     RBufWriteStream writeStream ;
       
   868     CleanupClosePushL( writeStream );
       
   869 
       
   870     writeStream.Open( *flatBuf );
       
   871 
       
   872     writeStream.WriteInt16L( aMsg->MessageType() );  
       
   873     writeStream.WriteInt16L( aMsg->MessagerType() );
       
   874     writeStream.WriteInt16L( aMsg->ContentType() );   
       
   875     writeStream.WriteReal64L( aMsg->TimeStamp().Int64() );
       
   876     writeStream.WriteInt16L( aActiveHeader->BuddyId().Length() );    
       
   877     writeStream.WriteL( aActiveHeader->BuddyId() );
       
   878     writeStream.WriteInt16L( aMsg->Text().Length() );    
       
   879     writeStream.WriteL( aMsg->Text() );
       
   880     
       
   881     writeStream.CommitL();
       
   882 
       
   883     HBufC8* tempBuffer = flatBuf->Ptr(0).AllocL();
       
   884     
       
   885     CleanupStack::PopAndDestroy(); // flatBuf
       
   886     CleanupStack::PopAndDestroy(); // writeStream
       
   887     
       
   888     TRACE( T_LIT( "CIMCacheSession::GetPackedChatDataBufferL() end") ); 
       
   889     return tempBuffer;
       
   890     }
       
   891 
       
   892 // ---------------------------------------------------------
       
   893 // CIMCacheSession::GetPackedChatItemBufferL
       
   894 // (other items were commented in a header).
       
   895 // ---------------------------------------------------------
       
   896 //
       
   897 HBufC8* CIMCacheSession::GetPackedChatItemBufferL(MIMCacheMessageHeader* aChatHeader, TInt& aSize)
       
   898     {
       
   899     TRACE( T_LIT( "CIMCacheSession::GetPackedChatItemBufferL() end") );
       
   900     
       
   901     TInt  serviceId   = aChatHeader->ServiceId();
       
   902     const TDesC& buddyId = aChatHeader->BuddyId();
       
   903              
       
   904      CBufFlat*   flatBuf = CBufFlat::NewL( aSize );
       
   905      CleanupStack::PushL( flatBuf );
       
   906      
       
   907      RBufWriteStream writeStream ;
       
   908      CleanupClosePushL( writeStream );
       
   909      writeStream.Open( *flatBuf );
       
   910         
       
   911      writeStream.WriteInt16L( serviceId );
       
   912      writeStream.WriteInt16L( buddyId.Length() );
       
   913      writeStream.WriteL( buddyId );       
       
   914      writeStream.CommitL();
       
   915      
       
   916      HBufC8* chatHeaderBuffer = flatBuf->Ptr(0).AllocL();
       
   917     
       
   918      CleanupStack::PopAndDestroy(2); // writeAllStream,flatBuf
       
   919      
       
   920      TRACE( T_LIT( "CIMCacheSession::GetPackedChatItemBufferL() end") );
       
   921      
       
   922      return chatHeaderBuffer;
       
   923     }
       
   924 
       
   925 
       
   926 // end of file