imservices/instantmessagingcache/imcacheserver/src/ccacheserveractivehelper.cpp
changeset 0 e6b17d312c8b
child 17 e1ad3c803639
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:  active helper for server ,owns the main cache arrray 
       
    15 *
       
    16 */
       
    17 
       
    18  // INCLUDE FILES
       
    19 #include "ccacheserveractivehelper.h"
       
    20 
       
    21 #include "cimcachesession.h"
       
    22 #include <imcachedefs.h>
       
    23 #include "mimcachemessageheader.h"
       
    24 #include "cimcacheserver.h"
       
    25 #include "timcachemessagefactory.h"
       
    26 #include "cimcachemessageheader.h"
       
    27 #include "cimcachemessagebase.h"
       
    28 #include <ecom/ecom.h>
       
    29  //logs
       
    30 #include "imcachedebugtrace.h"
       
    31 // ---------------------------------------------------------------------------
       
    32 // CCacheServerActiveHelper::NewL()
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CCacheServerActiveHelper* CCacheServerActiveHelper::NewL(CIMCacheServer& aServer)
       
    36     {
       
    37     TRACE( T_LIT( "CCacheServerActiveHelper::NewL begin") );
       
    38     CCacheServerActiveHelper* self = new(ELeave) CCacheServerActiveHelper(aServer);
       
    39     CleanupStack::PushL( self );
       
    40     self->ConstructL( );
       
    41     CleanupStack::Pop( self );
       
    42     TRACE( T_LIT( "CCacheServerActiveHelper::NewL end") );
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // CCacheServerActiveHelper::~CCacheServerActiveHelper()
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CCacheServerActiveHelper::~CCacheServerActiveHelper( )
       
    51     {
       
    52     TRACE( T_LIT( "CCacheServerActiveHelper::~CCacheServerActiveHelper begin") );
       
    53       
       
    54     iHeaderArray.ResetAndDestroy();  
       
    55     //all items are there in iHeaderArray so no need to call ResetAndDestroy
       
    56     iPluginInfo.ResetAndDestroy();
       
    57     iPluginInfo.Close();
       
    58     TRACE( T_LIT( "CCacheServerActiveHelper::~CCacheServerActiveHelper end") );
       
    59     }
       
    60 
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CCacheServerActiveHelper::CCacheServerActiveHelper()
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CCacheServerActiveHelper::CCacheServerActiveHelper(CIMCacheServer& aServer ) 
       
    67                                                     :iServer(aServer )
       
    68                                                       
       
    69     {
       
    70     TRACE( T_LIT( "CCacheServerActiveHelper::CCacheServerActiveHelper begin") );
       
    71 	TRACE( T_LIT( "CCacheServerActiveHelper::CCacheServerActiveHelper end") );
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CCacheServerActiveHelper::ConstructL()
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 void CCacheServerActiveHelper::ConstructL()
       
    79     {
       
    80     TRACE( T_LIT( "CCacheServerActiveHelper::ConstructL begin") );
       
    81 	iMessageFactory = new ( ELeave ) TIMCacheMessageFactory;
       
    82 	LoadPluginL( );
       
    83 	TRACE( T_LIT( "CCacheServerActiveHelper::ConstructL end") );
       
    84     }
       
    85  
       
    86 // ---------------------------------------------------------------------------
       
    87 // CCacheServerActiveHelper::ResetHeaderInoformation()
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 void CCacheServerActiveHelper::ResetHeaderInoformation()
       
    91                                                       
       
    92     {
       
    93     TRACE( T_LIT( "CCacheServerActiveHelper::ResetHeaderInoformation begin") );
       
    94     TInt count = iHeaderArray.Count() ;
       
    95     for ( TInt i= 0; i< count; i++ )
       
    96 	    {
       
    97 	    MIMCacheMessageHeader* header = iHeaderArray[ i ];
       
    98 	    header->SetChatStarted(EFalse);
       
    99 	    }
       
   100    	TRACE( T_LIT( "CCacheServerActiveHelper::ResetHeaderInoformation end") );
       
   101     }
       
   102 // ---------------------------------------------------------
       
   103 // CCacheServerActiveHelper::GetUnreadMessageCountL
       
   104 // (other items were commented in a header).
       
   105 // ---------------------------------------------------------
       
   106 //
       
   107 void CCacheServerActiveHelper::GetUnreadMessageCountL(const RMessage2& aMessage )
       
   108 	{
       
   109 	TRACE( T_LIT( "CCacheServerActiveHelper::GetUnreadMessageCountL	start") );
       
   110 		// at zero place service id was appended
       
   111 	TInt sericeId = aMessage.Int0();
       
   112 	
       
   113 	// at index 1 sender was appended
       
   114 	TInt bufferSize( aMessage.GetDesLength( 1 ) );
       
   115 	HBufC* buddyId = HBufC::NewLC( bufferSize );
       
   116 	TPtr buddyIdPtr = buddyId->Des();
       
   117 	aMessage.ReadL( 1, buddyIdPtr );
       
   118 
       
   119 	TInt headerIndex = iMessageFactory->FindHeaderIndexL(iHeaderArray, sericeId, buddyIdPtr );
       
   120 	TInt unreadCount = 0;
       
   121 	if( headerIndex != KErrNotFound ) //if match is found
       
   122 		{
       
   123 		MIMCacheMessageHeader* newHeader = iHeaderArray[ headerIndex ];
       
   124 		
       
   125 		unreadCount = newHeader->UnreadMessageCount();
       
   126 	
       
   127 		}
       
   128 	TPckgC<TInt> pack( unreadCount );
       
   129    	aMessage.WriteL( 2, pack );
       
   130    		
       
   131     CleanupStack::PopAndDestroy(buddyId);
       
   132     TRACE( T_LIT( "CCacheServerActiveHelper::GetUnreadMessageCountL	end") );
       
   133 	}
       
   134 // ---------------------------------------------------------
       
   135 // CCacheServerActiveHelper::GetAllUnreadMessageCountL
       
   136 // (other items were commented in a header).
       
   137 // ---------------------------------------------------------
       
   138 //
       
   139 void CCacheServerActiveHelper::GetAllUnreadMessageCountL(const RMessage2& aMessage )
       
   140 	{
       
   141 	TRACE( T_LIT( "CCacheServerActiveHelper::GetAllUnreadMessageCountL	start") );
       
   142 		// at zero place service id was appended
       
   143 	TInt sericeId = aMessage.Int0();
       
   144 	
       
   145 	TInt unreadCount = 0;
       
   146 	
       
   147 	TInt headerCount = iHeaderArray.Count();
       
   148 	
       
   149 	for( TInt i=0; i< headerCount; i++ )
       
   150 		{
       
   151 		// note in this case reciepient is own user id 
       
   152 		MIMCacheMessageHeader* header = iHeaderArray[i];
       
   153 		if( header->ServiceId() == sericeId ) 	
       
   154 			{
       
   155 			unreadCount = unreadCount + header->UnreadMessageCount();	
       
   156 			}
       
   157 		}
       
   158 
       
   159 	TPckgC<TInt> pack( unreadCount );
       
   160    	aMessage.WriteL( 1, pack );
       
   161    		
       
   162       
       
   163     TRACE( T_LIT( "CCacheServerActiveHelper::GetAllUnreadMessageCountL	end") );
       
   164 	}
       
   165 // ---------------------------------------------------------
       
   166 // CCacheServerActiveHelper::StartNewConversationL
       
   167 // (other items were commented in a header).
       
   168 // ---------------------------------------------------------
       
   169 //
       
   170 void CCacheServerActiveHelper::StartNewConversationL( const RMessage2& aMessage )
       
   171 	{
       
   172 	TRACE( T_LIT( "CCacheServerActiveHelper::StartNewConversationL	start") );
       
   173 	// at zero place service id was appended
       
   174 	TInt sericeId = aMessage.Int0();
       
   175 	
       
   176 	// at index 1 sender was appended
       
   177 	TInt bufferSize( aMessage.GetDesLength( 1 ) );
       
   178 	HBufC* buddyId = HBufC::NewLC( bufferSize );
       
   179 	TPtr buddyIdPtr = buddyId->Des();
       
   180 	aMessage.ReadL( 1, buddyIdPtr );
       
   181 	
       
   182 	TInt messageCount = 0;
       
   183 	
       
   184 	MIMCacheMessageHeader* chatHeader = ChatHeaderL( sericeId, buddyIdPtr );
       
   185 	
       
   186 	if( chatHeader == NULL ) //if match is not found
       
   187 		{
       
   188 		TRACE( T_LIT( "CCacheServerActiveHelper::StartNewConversationL	Fresh conversation") );
       
   189 		chatHeader = iMessageFactory->CreateIMCacheMessageHeaderL(sericeId, buddyIdPtr );
       
   190 		// insert always on top
       
   191 		iHeaderArray.InsertL(chatHeader , 0);
       
   192 		
       
   193 		iActiveHeader = chatHeader;
       
   194 		
       
   195 		chatHeader->SetChatStarted(ETrue);
       
   196 		
       
   197 		PackAndNotifyEventL( EIMOperationChatStarted, sericeId, iActiveHeader , NULL );	
       
   198 		}
       
   199 	else  if( !chatHeader->IsChatStarted() ) //if match is found
       
   200 		{
       
   201 		TRACE( T_LIT( "CCacheServerActiveHelper::StartNewConversationL	existing conversation") );
       
   202 		
       
   203 		chatHeader->SetChatStarted(ETrue);
       
   204 		
       
   205 		iActiveHeader = chatHeader ;
       
   206 		
       
   207 		iActiveHeader->MarkAllRead();
       
   208 		
       
   209 		messageCount = iActiveHeader->MessageCount();
       
   210 
       
   211 		if( messageCount ) 
       
   212 			{
       
   213 			PackAndNotifyEventL(EIMOperationFetchAll, sericeId, iActiveHeader, NULL );
       
   214 			// still fetching is not completed, until EIMOperationCompleted 
       
   215 			}
       
   216 		TRACE( T_LIT( "CCacheServerActiveHelper::StartNewConversationL start fetching") );
       
   217 		}
       
   218 	else
       
   219 		{
       
   220 		iActiveHeader = chatHeader;
       
   221 		
       
   222 		iActiveHeader->MarkAllRead();
       
   223 		
       
   224 		PackAndNotifyEventL(EIMOperationUnreadChange, sericeId, iActiveHeader , NULL );
       
   225 		// once request is completed, mark as fetch completed
       
   226 		}
       
   227 	// if there are no unread messages then reset the status pane indicator.
       
   228 	TInt unreadCount = 0;
       
   229     TInt headerCount = iHeaderArray.Count();
       
   230     for( TInt i=0; i< headerCount; i++ )
       
   231         {
       
   232         // note in this case reciepient is own user id 
       
   233         MIMCacheMessageHeader* header = iHeaderArray[i];
       
   234         if( header->ServiceId() == sericeId )   
       
   235             {
       
   236             unreadCount = unreadCount + header->UnreadMessageCount();   
       
   237             }
       
   238         }
       
   239     if(!unreadCount)
       
   240         {
       
   241         PublishMessageInfoL(*buddyId,sericeId);
       
   242         }
       
   243     
       
   244 	CleanupStack::PopAndDestroy(buddyId);
       
   245 	
       
   246     TRACE( T_LIT( "CCacheServerActiveHelper::StartNewConversationL	end") );
       
   247 	}
       
   248 
       
   249 
       
   250 // ---------------------------------------------------------
       
   251 // CCacheServerActiveHelper::AppendReceiveMessageL
       
   252 // (other items were commented in a header).
       
   253 // ---------------------------------------------------------
       
   254 //
       
   255 void CCacheServerActiveHelper::AppendReceiveMessageL( const RMessage2 &aMessage )
       
   256 	{
       
   257 	TRACE( T_LIT( "CCacheServerActiveHelper::AppendReceiveMessageL	start") );
       
   258 	// at zero pace service id was apended
       
   259 	TInt sericeId = aMessage.Int0();
       
   260 	
       
   261 	// at index 1 sender was appended
       
   262 	TInt bufferSize( aMessage.GetDesLength( 1 ) );
       
   263 	HBufC* buddyId = HBufC::NewLC( bufferSize );
       
   264 	TPtr buddyIdPtr = buddyId->Des();
       
   265 	aMessage.ReadL( 1, buddyIdPtr );
       
   266 	
       
   267 	// at index 2 text was appended
       
   268 	bufferSize = aMessage.GetDesLength( 2 ) ;
       
   269 	HBufC* text = HBufC::NewLC( bufferSize );
       
   270 	TPtr textPtr = text->Des();
       
   271 	aMessage.ReadL( 2, textPtr );
       
   272 	
       
   273 	TRACE( T_LIT( "CCacheServerActiveHelper::AppendReceiveMessageL Msg %S"), &textPtr );
       
   274 	
       
   275 	MIMCacheMessageHeader* chatHeader = ChatHeaderL( sericeId, buddyIdPtr );
       
   276 	
       
   277 	CIMCacheMessageBase* newmessage = NULL;
       
   278 	      
       
   279 	if( chatHeader == NULL ) //if match is not found
       
   280 		{
       
   281 		TRACE( T_LIT( "CCacheServerActiveHelper::AppendReceiveMessageL	Fresh conversation") );
       
   282 		chatHeader = iMessageFactory->CreateIMCacheMessageHeaderL(sericeId, buddyIdPtr);
       
   283 		// insert always on top
       
   284 		iHeaderArray.InsertL(chatHeader , 0);
       
   285 		
       
   286 		newmessage = iMessageFactory->CreateCacheMessageL( textPtr );
       
   287 		newmessage->SetMessageType( EIMCMessagePTOP );
       
   288 		newmessage->SetMessagerType( EIMCMessageReceived );
       
   289 		
       
   290 		chatHeader->AppendMessageL(newmessage);
       
   291 		
       
   292 		PublishMessageInfoL(*buddyId,sericeId);
       
   293 		
       
   294 		// no notification so for this is for servicetab
       
   295 		// here need to notify for all session
       
   296 		// fresh conversation started
       
   297 				
       
   298 		PackAndNotifyEventL( EIMOperationChatAdded,	sericeId, chatHeader ,newmessage );		
       
   299 	
       
   300 		}
       
   301 	else  // chat found
       
   302 		{
       
   303 		TRACE( T_LIT( "CCacheServerActiveHelper::AppendReceiveMessageL	existing conversation") );
       
   304 		// this is called from network side 
       
   305 		// here we dont know about ui client wheather this active conversation is active or not. 
       
   306 			
       
   307 		newmessage = iMessageFactory->CreateCacheMessageL( textPtr );
       
   308 		newmessage->SetMessageType( EIMCMessagePTOP );
       
   309 		newmessage->SetMessagerType( EIMCMessageReceived );
       
   310 		chatHeader->AppendMessageL(newmessage);
       
   311 	
       
   312 		// notify all registeed session about this changed
       
   313 		// this has happened on active conversation
       
   314 				
       
   315 		if( chatHeader == iActiveHeader )
       
   316 			{
       
   317 			//cv tab is active notify it.
       
   318 			PackAndNotifyEventL( EIMOperationMessageAdded, sericeId, chatHeader, newmessage );
       
   319 			}
       
   320 		else 
       
   321 			{
       
   322 			PublishMessageInfoL(*buddyId,sericeId);
       
   323 			//cv tab is not active notify it.
       
   324 			PackAndNotifyEventL( EIMOperationUnreadMessage, sericeId, chatHeader , newmessage );	
       
   325 			}
       
   326 		}	
       
   327 	CleanupStack::PopAndDestroy(2);//text,buddyId
       
   328 	//	else TODO mark unread messagesinto buffer
       
   329 	TRACE( T_LIT( "CCacheServerActiveHelper::AppendReceiveMessageL	end") );
       
   330 	}
       
   331 // ---------------------------------------------------------
       
   332 // CCacheServerActiveHelper::AppendSendMessageL
       
   333 // (other items were commented in a header).
       
   334 // ---------------------------------------------------------
       
   335 //
       
   336 void CCacheServerActiveHelper::AppendSendMessageL( const RMessage2 &aMessage )
       
   337 	{
       
   338 	TRACE( T_LIT( "CCacheServerActiveHelper::AppendSendMessageL	start") );
       
   339 	
       
   340 	// at index 0 text was appended
       
   341 	TInt bufferSize = aMessage.GetDesLength( 0 ) ;
       
   342 	HBufC* text = HBufC::NewLC( bufferSize );
       
   343 	TPtr textPtr = text->Des();
       
   344 	aMessage.ReadL( 0, textPtr );
       
   345 	
       
   346 	TRACE( T_LIT( "CCacheServerActiveHelper::AppendSendMessageL Msg %S"), &textPtr );
       
   347 	
       
   348 	if( iActiveHeader )
       
   349 		{
       
   350 		CIMCacheMessageBase* message = iMessageFactory->CreateCacheMessageL( textPtr );
       
   351 		message->SetMessageType( EIMCMessagePTOP );
       
   352 		message->SetMessagerType( EIMCMessageSent );
       
   353 		message->SetRead();	
       
   354 		
       
   355 		iActiveHeader->AppendMessageL(message);
       
   356 		}
       
   357 	CleanupStack::PopAndDestroy(text); // text
       
   358 	
       
   359 	TRACE( T_LIT( "CCacheServerActiveHelper::AppendSendMessageL	end") );
       
   360 	}
       
   361 
       
   362 // ---------------------------------------------------------
       
   363 // CCacheServerActiveHelper::AppendMessageL
       
   364 // (other items were commented in a header).
       
   365 // ---------------------------------------------------------
       
   366 //
       
   367 void CCacheServerActiveHelper::AppendMessageL( const RMessage2 &aMessage )
       
   368 	{
       
   369 	TRACE( T_LIT( "CCacheServerActiveHelper::AppendMessageL	start") );
       
   370 	// this is get called from active conversation only.
       
   371 	// this append the normal information message , 
       
   372 	// please note, this is not sent/recieved message
       
   373 	// at 0 place text was appended
       
   374 	// this is get called from conversation only system emssage
       
   375 	// will get addded when buddy presence changes.
       
   376 	TInt sericeId = aMessage.Int0();
       
   377 	
       
   378 	// at index 1 sender was appended
       
   379 	TInt bufferSize( aMessage.GetDesLength( 1 ) );
       
   380 	HBufC* buddyId = HBufC::NewLC( bufferSize );
       
   381 	TPtr buddyIdPtr = buddyId->Des();
       
   382 	aMessage.ReadL( 1, buddyIdPtr );
       
   383 	
       
   384 	// at index 2 text was appended
       
   385 	bufferSize = aMessage.GetDesLength( 2 ) ;
       
   386 	HBufC* text = HBufC::NewLC( bufferSize );
       
   387 	TPtr textPtr = text->Des();
       
   388 	aMessage.ReadL( 2, textPtr );
       
   389 	
       
   390 	TRACE( T_LIT( "CCacheServerActiveHelper::AppendSendMessageL Msg %S"), &textPtr );
       
   391 	
       
   392 	MIMCacheMessageHeader* chatHeader = ChatHeaderL( sericeId, buddyIdPtr );
       
   393 	
       
   394 	if( chatHeader != NULL )
       
   395 		{
       
   396 		CIMCacheMessageBase* message = iMessageFactory->CreateCacheMessageL( textPtr );
       
   397 		message->SetMessageType( EIMCMessageSystem );
       
   398 		message->SetMessagerType( EIMCMessageOther );
       
   399 		message->SetRead();		
       
   400 		chatHeader->AppendMessageL(message);
       
   401 		}
       
   402 	CleanupStack::PopAndDestroy(2); //buddyId, text
       
   403 	
       
   404 	TRACE( T_LIT( "CCacheServerActiveHelper::AppendMessageL	end") );
       
   405 	}
       
   406 // ---------------------------------------------------------
       
   407 // CCacheServerActiveHelper::CloseConversationL
       
   408 // (other items were commented in a header).
       
   409 // ---------------------------------------------------------
       
   410 //
       
   411 void CCacheServerActiveHelper::CloseConversationL( const RMessage2 &aMessage )
       
   412     {
       
   413     TRACE( T_LIT( "CCacheServerActiveHelper::CloseConversationL  start") );
       
   414     // at zero pace service id was apended
       
   415     TInt sericeId = aMessage.Int0();
       
   416     
       
   417     // at index 1 sender was appended
       
   418     TInt bufferSize( aMessage.GetDesLength( 1 ) );
       
   419     HBufC* buddyId = HBufC::NewLC( bufferSize );
       
   420     TPtr buddyIdPtr = buddyId->Des();
       
   421     aMessage.ReadL( 1, buddyIdPtr );
       
   422 
       
   423     TInt headerIndex = iMessageFactory->FindHeaderIndexL(iHeaderArray, sericeId, buddyIdPtr ) ;
       
   424        
       
   425     if( headerIndex != KErrNotFound ) //if match is found
       
   426         {
       
   427         MIMCacheMessageHeader* msgHeader = iHeaderArray[headerIndex];
       
   428         iHeaderArray.Remove(headerIndex);
       
   429         if(msgHeader == iActiveHeader)
       
   430             {
       
   431             iActiveHeader = NULL;
       
   432             }
       
   433        
       
   434 		PackAndNotifyEventL( EIMOperationChatDeleted, sericeId, msgHeader, NULL ); 
       
   435 		 
       
   436         delete msgHeader;
       
   437         iHeaderArray.Compress();  
       
   438        		      
       
   439         }  
       
   440      CleanupStack::PopAndDestroy(buddyId); // buddyId
       
   441      
       
   442     TRACE( T_LIT( "CCacheServerActiveHelper::CloseConversationL  end") );
       
   443     }
       
   444 // ---------------------------------------------------------
       
   445 // CCacheServerActiveHelper::CloseAllConversationL
       
   446 // (other items were commented in a header).
       
   447 // ---------------------------------------------------------
       
   448 //
       
   449 void CCacheServerActiveHelper::CloseAllConversationL( TInt aServiceId )
       
   450     {
       
   451     TRACE( T_LIT( "CCacheServerActiveHelper::CloseAllConversationL  start") );
       
   452     TBool needToNotify = EFalse;
       
   453     for( TInt index=0; index<iHeaderArray.Count();index++ )
       
   454         {
       
   455         MIMCacheMessageHeader* msgHeader = iHeaderArray[index];
       
   456         if( msgHeader->ServiceId() == aServiceId )
       
   457             {
       
   458             iHeaderArray.Remove(index);
       
   459             if(msgHeader == iActiveHeader)
       
   460                 {
       
   461                 iActiveHeader = NULL;
       
   462                 }
       
   463             delete msgHeader;
       
   464             msgHeader = NULL;
       
   465             iHeaderArray.Compress();
       
   466             index = index-1;// start from same index again
       
   467             needToNotify = ETrue;
       
   468             }
       
   469         }
       
   470     if( needToNotify )
       
   471         {
       
   472         PackAndNotifyEventL( EIMOperationAllChatDeleted, aServiceId, NULL, NULL ); 
       
   473         }
       
   474     TRACE( T_LIT( "CCacheServerActiveHelper::CloseAllConversationL  End") );
       
   475     }
       
   476 // ---------------------------------------------------------
       
   477 // CCacheServerActiveHelper::DeactivateConversationL
       
   478 // (other items were commented in a header).
       
   479 // ---------------------------------------------------------
       
   480 //
       
   481 void CCacheServerActiveHelper::DeactivateConversationL()
       
   482 	{
       
   483 	TRACE( T_LIT( "CCacheServerActiveHelper::DeactivateConversationL	start") );
       
   484 	// this is get called from active conversation only.
       
   485 	if( iActiveHeader )
       
   486 		{
       
   487 		iActiveHeader->MarkAllRead();	
       
   488 		}
       
   489 	iActiveHeader = NULL;
       
   490 	TRACE( T_LIT( "CCacheServerActiveHelper::DeactivateConversationL	end") );
       
   491 	}
       
   492 
       
   493 // ---------------------------------------------------------
       
   494 // CCacheServerActiveHelper::CheckConversationExistL
       
   495 // (other items were commented in a header).
       
   496 // ---------------------------------------------------------
       
   497 //
       
   498 void CCacheServerActiveHelper::CheckConversationExistL( const RMessage2& aMessage )
       
   499 	{
       
   500 	TRACE( T_LIT( "CCacheServerActiveHelper::CheckConversationExistL	start") );
       
   501 	// at zero place service id was appended
       
   502 	TInt sericeId = aMessage.Int0();
       
   503 	
       
   504 	// at index 1 sender was appended
       
   505 	TInt bufferSize( aMessage.GetDesLength( 1 ) );
       
   506 	HBufC* buddyId = HBufC::NewLC( bufferSize );
       
   507 	TPtr buddyIdPtr = buddyId->Des();
       
   508 	aMessage.ReadL( 1, buddyIdPtr );
       
   509 	
       
   510 	TInt headerIndex = iMessageFactory->FindHeaderIndexL(iHeaderArray, sericeId, buddyIdPtr ) ;
       
   511 	
       
   512 	TBool conversationExist = EFalse;
       
   513 	
       
   514 	if( headerIndex != KErrNotFound ) //if match is found
       
   515 		{
       
   516 	    conversationExist = ETrue;
       
   517 		}
       
   518 	TPckgC<TBool> pack( conversationExist );
       
   519    	aMessage.WriteL(2, pack );
       
   520     		
       
   521 	CleanupStack::PopAndDestroy(buddyId); // buddyId
       
   522     
       
   523     TRACE( T_LIT( "CCacheServerActiveHelper::CheckConversationExistL	end") );
       
   524 	}
       
   525 
       
   526 // ---------------------------------------------------------
       
   527 // CCacheServerActiveHelper::PackAndNotifyEventL
       
   528 // (other items were commented in a header).
       
   529 // ---------------------------------------------------------
       
   530 //
       
   531 void CCacheServerActiveHelper::PackAndNotifyEventL( TIMCacheOperationsCode aNotification, 
       
   532                                                     TInt aServiceId,
       
   533 												    MIMCacheMessageHeader* aChatHeader /*=NULL*/, 
       
   534 												    CIMCacheMessageBase* aMessage /*=NULL*/)	
       
   535 	{
       
   536 	TRACE( T_LIT( "CCacheServerActiveHelper::PackAndNotifyEventL	start") );
       
   537 	
       
   538 	iServer.PackAndNotifyEventL( aNotification, aServiceId, aChatHeader, aMessage );
       
   539 	 
       
   540 	TRACE( T_LIT( "CCacheServerActiveHelper::PackAndNotifyEventL	end") );
       
   541 	}
       
   542 	
       
   543    
       
   544 // ---------------------------------------------------------
       
   545 // CCacheServerActiveHelper::GetServiceChatHeaderArrayL
       
   546 // (other items were commented in a header).
       
   547 // ---------------------------------------------------------
       
   548 //
       
   549 void CCacheServerActiveHelper::GetServiceChatHeaderArrayL(TInt aServiceId , RPointerArray<MIMCacheMessageHeader>& aServiceHeaderArray)
       
   550 	{
       
   551 	TRACE( T_LIT( "CCacheServerActiveHelper::GetServiceChatHeaderArrayL	start") );		
       
   552 	
       
   553    
       
   554     if( -1 == aServiceId )
       
   555         {
       
   556         aServiceHeaderArray = iHeaderArray;        
       
   557         }
       
   558     else
       
   559         {
       
   560         for( TInt i= 0 ; i < iHeaderArray.Count() ; i++ )
       
   561             {
       
   562             MIMCacheMessageHeader* header = iHeaderArray[i];
       
   563             if( header->ServiceId() == aServiceId )
       
   564                 {
       
   565                 aServiceHeaderArray.AppendL(header);
       
   566                 }
       
   567             }        
       
   568         }
       
   569     TRACE( T_LIT( "CCacheServerActiveHelper::GetServiceChatHeaderArrayL    end") );
       
   570 	}
       
   571 
       
   572 // ---------------------------------------------------------
       
   573 // CCacheServerActiveHelper::ChatHeaderL
       
   574 // (other items were commented in a header).
       
   575 // ---------------------------------------------------------
       
   576 MIMCacheMessageHeader* CCacheServerActiveHelper::ChatHeaderL(const TInt& aServiceId, const TDesC& aBuddyId )
       
   577     {
       
   578     MIMCacheMessageHeader* header = NULL;
       
   579     TInt headerCount = iHeaderArray.Count();
       
   580     
       
   581     for( TInt index = 0 ; index < headerCount ; index++ )
       
   582         {
       
   583         header = iHeaderArray[index];
       
   584         if( header )
       
   585             {
       
   586             if( ( aServiceId == header->ServiceId() ) && ( aBuddyId.CompareC( header->BuddyId() ) == 0 ) )  
       
   587                 {
       
   588                 break;   
       
   589                 }                
       
   590             }
       
   591         header = NULL;
       
   592         }
       
   593     return header;
       
   594     }
       
   595 // -----------------------------------------------------------------------------
       
   596 // CCacheServerActiveHelper::ChatHeaderAt
       
   597 // name of history data
       
   598 // -----------------------------------------------------------------------------
       
   599 //
       
   600 MIMCacheMessageHeader* CCacheServerActiveHelper::ChatHeaderAt( TInt aIndex ) 
       
   601 	{
       
   602 	if( aIndex >= ChatHeadersCount() )
       
   603 		{
       
   604 		return NULL;	
       
   605 		}
       
   606 	return iHeaderArray[aIndex];
       
   607 	}
       
   608 	
       
   609 // -----------------------------------------------------------------------------
       
   610 // CCacheServerActiveHelper::ChatHeadersCount
       
   611 // name of history data
       
   612 // -----------------------------------------------------------------------------
       
   613 //
       
   614 TInt CCacheServerActiveHelper::ChatHeadersCount() 
       
   615 	{
       
   616 	return iHeaderArray.Count();
       
   617 	}
       
   618 
       
   619 
       
   620 // -----------------------------------------------------------------------------
       
   621 // CCacheServerActiveHelper::PublishMessageInfoL()
       
   622 // name of history data
       
   623 // -----------------------------------------------------------------------------
       
   624 //
       
   625 void  CCacheServerActiveHelper::PublishMessageInfoL(const TDesC& aSenderId,TInt aServiceId )
       
   626     {
       
   627     // set/reset  the status pane indicator here
       
   628     // based on the unread count.
       
   629     // as we need to show the indicator only for the received messages
       
   630     TInt unreadCount = 0;
       
   631     TInt multipleSender = 0;
       
   632     TInt headerCount = iHeaderArray.Count();
       
   633     TRACE(T_LIT( "CCacheServerActiveHelper::PublishMessageInfoL headerCount -- %d"), headerCount);
       
   634     for( TInt i=0; i< headerCount; i++ )
       
   635         {
       
   636         // note in this case reciepient is own user id 
       
   637         MIMCacheMessageHeader* header = iHeaderArray[i];
       
   638         TRACE(T_LIT( "CCacheServerActiveHelper::PublishMessageInfoL UnreadMessageCount -- %d"), header->UnreadMessageCount());
       
   639         if( header->ServiceId() == aServiceId  && header->UnreadMessageCount() )   
       
   640             {
       
   641             unreadCount = unreadCount + header->UnreadMessageCount();  
       
   642 	        multipleSender ++; 
       
   643             }
       
   644         }
       
   645     TRACE(T_LIT( "CCacheServerActiveHelper::PublishMessageInfoL   multipleSender -- %d"), multipleSender);
       
   646     // Get the number of  plugins
       
   647     const TInt count = iPluginInfo.Count();
       
   648     // Loop though all the command handlers, If DynInitMenuPaneL leaves for one plugin, a plugin
       
   649     // error message will be displayed and the loop will continue with
       
   650     // the next command handler. If none of the plugins leave, there will
       
   651     // be only one TRAP used.
       
   652     for ( TInt index = 0; index < count; ++index )
       
   653         {
       
   654         //trap is required if one plugin leaves then it should continue with other plugins.
       
   655         TRAP_IGNORE(iPluginInfo[index]->Plugin().MessageInfoL(unreadCount,aServiceId,aSenderId,multipleSender));
       
   656         }
       
   657     }
       
   658  // -----------------------------------------------------------------------------
       
   659  // CCacheServerActiveHelper::LoadPluginL
       
   660  // -----------------------------------------------------------------------------
       
   661  //
       
   662  void CCacheServerActiveHelper::LoadPluginL( )
       
   663      {
       
   664      // plugininfo array, Owned
       
   665      RImplInfoPtrArray pluginInfo;
       
   666      iPluginInfo.ResetAndDestroy();
       
   667      REComSession::ListImplementationsL( KINSTANTMSGINTERFACEUID,pluginInfo );
       
   668      TInt pluginCount = pluginInfo.Count();
       
   669      for ( TInt pluginIndex(0); pluginIndex < pluginCount; ++pluginIndex )
       
   670          {
       
   671          TUid pluginUid = pluginInfo[pluginIndex]->ImplementationUid();
       
   672          // Creates the plugin and transfers ownership of the services
       
   673          // object to the plugin.
       
   674          CInstantMsgIndicator* plugin = NULL;
       
   675          TRAPD(error, plugin = CInstantMsgIndicator::CreateImplementationL( pluginUid ));
       
   676          
       
   677          if(KErrNone != error)
       
   678              {
       
   679              // handle the error here.
       
   680              // error loading the plugin
       
   681              }
       
   682          else
       
   683              {
       
   684              // plugin loaded sucessfully 
       
   685              //if its here its sure that plugin is not null;
       
   686              //add the plugin to the array
       
   687              CPluginInfo* newPlugin = new ( ELeave ) CCacheServerActiveHelper::CPluginInfo( plugin, pluginUid);    
       
   688              CleanupStack::PushL( newPlugin );
       
   689              iPluginInfo.AppendL( newPlugin );
       
   690              CleanupStack::Pop( newPlugin );
       
   691             
       
   692             }
       
   693          
       
   694          }
       
   695      pluginInfo.ResetAndDestroy();
       
   696      pluginInfo.Close();
       
   697      }
       
   698  // ----------------------------------------------------------------------------
       
   699  // CCacheServerActiveHelper::CPluginInfo::CPluginInfo(
       
   700  // C++ constructor
       
   701  // (other items were commented in a header).
       
   702  // ----------------------------------------------------------------------------
       
   703  //
       
   704  CCacheServerActiveHelper::CPluginInfo::CPluginInfo( 
       
   705          CInstantMsgIndicator* aPlugin,
       
   706                                      TUid aPluginUid )
       
   707      : iPlugin( aPlugin ),
       
   708        iPluginUid( aPluginUid )
       
   709      {
       
   710 
       
   711      }
       
   712  // ----------------------------------------------------------------------------
       
   713  // CCacheServerActiveHelper::CPluginInfo::~CPluginInfo(
       
   714  // C++ destructor
       
   715  // (other items were commented in a header).
       
   716  // ----------------------------------------------------------------------------
       
   717  //
       
   718  CCacheServerActiveHelper::CPluginInfo::~CPluginInfo() 
       
   719      {
       
   720       delete iPlugin;
       
   721      }
       
   722  // -----------------------------------------------------------------------------
       
   723  // CCacheServerActiveHelper::LoadPluginL
       
   724  // -----------------------------------------------------------------------------
       
   725  //
       
   726  CInstantMsgIndicator& CCacheServerActiveHelper::CPluginInfo::Plugin()
       
   727      {
       
   728       return *iPlugin;
       
   729      }   
       
   730 /// end of file