servicewidget/servicewidgetdatapublisher/src/cservicewidgetmessagehandler.cpp
changeset 0 5e5d6b214f4f
equal deleted inserted replaced
-1:000000000000 0:5e5d6b214f4f
       
     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:  message handler notification
       
    15  *                
       
    16  *
       
    17 */
       
    18 
       
    19  
       
    20 #include "cservicewidgetmessagehandler.h"
       
    21 #include "mservicewidgetobservers.h"
       
    22 #include "swpdebugtrace.h"
       
    23 //im cache
       
    24 #include  <cimcachefactory.h>
       
    25 #include  <mimcacheaccessor.h>
       
    26 #include <apgtask.h> 
       
    27 #include <coemain.h>
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CServiceWidgetMessageHandler::CServiceWidgetMessageHandler
       
    31 // ---------------------------------------------------------------------------
       
    32 // 
       
    33 CServiceWidgetMessageHandler::CServiceWidgetMessageHandler(
       
    34 						MServiceWidgetNewMessageObserver& aMessageObserver )
       
    35 	:iMessageObserver( aMessageObserver )
       
    36    {
       
    37    TRACE_SWP(TXT("CServiceWidgetMessageHandler::CServiceWidgetMessageHandler") );
       
    38    }
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // CServiceWidgetMessageHandler::NewL
       
    42 // ---------------------------------------------------------------------------
       
    43 // 
       
    44 CServiceWidgetMessageHandler* CServiceWidgetMessageHandler::NewL( 
       
    45 						MServiceWidgetNewMessageObserver& aMessageObserver,
       
    46 						TInt aServiceId, 
       
    47      				    const TDesC& aOwnUserId )
       
    48     {
       
    49     TRACE_SWP(TXT("CServiceWidgetMessageHandler::NewL() start") );
       
    50     CServiceWidgetMessageHandler* self = NewLC(aMessageObserver,
       
    51     									aServiceId,
       
    52     									aOwnUserId);
       
    53     CleanupStack::Pop(self);
       
    54     TRACE_SWP(TXT("CServiceWidgetMessageHandler::NewL() end") );
       
    55     return self;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // CServiceWidgetMessageHandler::NewLC
       
    60 // ---------------------------------------------------------------------------
       
    61 // 
       
    62 CServiceWidgetMessageHandler* CServiceWidgetMessageHandler::NewLC( 
       
    63 					   MServiceWidgetNewMessageObserver& aMessageObserver,
       
    64 						TInt aServiceId, 
       
    65 				 		const TDesC& aOwnUserId )
       
    66     {
       
    67 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::NewLC() start") );
       
    68     CServiceWidgetMessageHandler* self =
       
    69         new (ELeave) CServiceWidgetMessageHandler(aMessageObserver );
       
    70     CleanupStack::PushL(self);
       
    71     self->ConstructL( aOwnUserId ,aServiceId );
       
    72     TRACE_SWP(TXT("CServiceWidgetMessageHandler::NewLC() end") );
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CServiceWidgetMessageHandler::ConstructL
       
    78 // ---------------------------------------------------------------------------
       
    79 // 
       
    80 void CServiceWidgetMessageHandler::ConstructL( const TDesC& aOwnUserId, TInt aServiceId )
       
    81     {
       
    82 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::ConstructL() start") );
       
    83    	iIMCacheFactory = CIMCacheFactory::InstanceL(); 
       
    84 	iAccessor = iIMCacheFactory->CreateAccessorL( aServiceId, aOwnUserId );
       
    85 	iAccessor->RegisterObserverL(*this);
       
    86 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::ConstructL() end") );
       
    87 	}
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // CServiceWidgetMessageHandler::~CServiceWidgetMessageHandler
       
    91 // ---------------------------------------------------------------------------
       
    92 // 
       
    93 CServiceWidgetMessageHandler::~CServiceWidgetMessageHandler()
       
    94     {
       
    95     TRACE_SWP(TXT("CServiceWidgetMessageHandler::~CServiceWidgetMessageHandler() start") );
       
    96   	if( iIMCacheFactory )
       
    97 		{
       
    98 		if( iAccessor )
       
    99 		    {
       
   100 	    	iAccessor->UnRegisterObserver(*this);	
       
   101 	    	}
       
   102 		CIMCacheFactory::Release();  	
       
   103 		}
       
   104 	iSendersArray.ResetAndDestroy();
       
   105 	
       
   106 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::~CServiceWidgetMessageHandler() end") );
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CServiceWidgetMessageHandler::GetUnreadMessageCountL
       
   111 // (other items were commented in a header).
       
   112 // -----------------------------------------------------------------------------
       
   113 //	
       
   114 TInt CServiceWidgetMessageHandler::GetUnreadMessageCountL()
       
   115 	{
       
   116 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::GetUnreadMessageCountL()") );
       
   117 	return iAccessor->GetAllUnreadMessageCountL() ;
       
   118 	}
       
   119 //-----------------------------------------------------------------------------
       
   120 // CServiceWidgetMessageHandler::HandleIMCacheEventL
       
   121 // ( Other items commented in header )
       
   122 //-----------------------------------------------------------------------------  
       
   123 
       
   124 void CServiceWidgetMessageHandler::HandleIMCacheEventL(TIMCacheEventType aEventType, TAny* aChatMessage  )
       
   125 	{
       
   126 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::HandleIMCacheEventL() start") );
       
   127 	switch( aEventType )
       
   128 		{
       
   129 		case EIMCacheUnreadMessage :
       
   130 			{
       
   131 			TRACE_SWP(TXT("CServiceWidgetMessageHandler::HandleIMCacheEventL() EIMCacheUnreadMessage ") );
       
   132 			if (!aChatMessage)
       
   133 				{
       
   134 				return;	
       
   135 				}
       
   136 			SIMCacheMessageData* chatData = static_cast<SIMCacheMessageData*>( aChatMessage ) ;
       
   137 			// check that buddy id exists
       
   138 			if ( chatData->iBuddyId )
       
   139 			    {
       
   140 			    TPtrC buddyId = chatData->iBuddyId->Des();
       
   141 				TPtrC messageText = chatData->iText->Des();
       
   142 				if( !IsExistInSendersArrayL( buddyId ) ) // append only if not exist 
       
   143 					{
       
   144 					HBufC* sender = buddyId.AllocLC();
       
   145 					if( sender )
       
   146 						{
       
   147 						iSendersArray.InsertL( sender ,0 ); // ownership to array
       
   148 						CleanupStack::Pop(sender); //	
       
   149 						}
       
   150 					}
       
   151 				iMessageObserver.HandleNewMessageL( buddyId, messageText );
       
   152 			    }
       
   153 			break;	
       
   154 			}
       
   155 		case EIMCacheUnreadChange :
       
   156 			{
       
   157 			TRACE_SWP(TXT("CServiceWidgetMessageHandler::HandleIMCacheEventL() EIMCacheUnreadChange ") );
       
   158 			SynchronizeSendersArrayL();
       
   159 		    iMessageObserver.HandleCountChangedL();
       
   160 			break;	
       
   161 			}
       
   162 		default:
       
   163 			{
       
   164 			break;
       
   165 			}
       
   166 		}
       
   167 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::HandleIMCacheEventL() end ") );
       
   168 	}
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CServiceWidgetMessageHandler::GetUnreadSenderId
       
   172 // (other items were commented in a header).
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 const TDesC& CServiceWidgetMessageHandler::GetUnreadSenderId() 
       
   176 	{
       
   177 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::GetUnreadSenderId() start") );
       
   178 	if( iSendersArray.Count() )
       
   179 		{
       
   180 		TRACE_SWP(TXT("CServiceWidgetMessageHandler::GetUnreadSenderId() end") );
       
   181 		return *( iSendersArray[ 0 ] ); //  latest sender always on top
       
   182 		}
       
   183 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::GetUnreadSenderId() end") );
       
   184 	return KNullDesC;
       
   185 	}
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CServiceWidgetMessageHandler::IsSingleMessageSender
       
   189 // (other items were commented in a header).
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 TBool CServiceWidgetMessageHandler::IsSingleMessageSender() 
       
   193 	{
       
   194 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::IsSingleMessageSender()") );
       
   195 	return ( iSendersArray.Count() == 1 );
       
   196 	}
       
   197 	
       
   198 // -----------------------------------------------------------------------------
       
   199 // CServiceWidgetMessageHandler::IsExistInSendersArrayL
       
   200 // (other items were commented in a header).
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 TBool CServiceWidgetMessageHandler::IsExistInSendersArrayL( const TDesC& aNewSenderId ) 
       
   204 	{
       
   205 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::IsExistInSendersArrayL() start") );
       
   206 	TInt count = iSendersArray.Count();
       
   207 	for( TInt i=0; i<count; i++ )
       
   208 		{
       
   209 		HBufC* senderId = iSendersArray[i];
       
   210 		if( senderId && aNewSenderId.Compare( *senderId ) == 0 )	
       
   211 			{
       
   212 			TRACE_SWP(TXT("CServiceWidgetMessageHandler::IsExistInSendersArrayL() end") );
       
   213 			return  ETrue;	
       
   214 			}
       
   215 		}
       
   216 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::IsExistInSendersArrayL() end") );
       
   217 	return EFalse;
       
   218 	}
       
   219 // -----------------------------------------------------------------------------
       
   220 // CServiceWidgetMessageHandler::SynchronizeSendersArrayL
       
   221 // (other items were commented in a header).
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CServiceWidgetMessageHandler::SynchronizeSendersArrayL() 
       
   225 	{
       
   226 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::SynchronizeSendersArrayL() start") );
       
   227 	TInt count = iSendersArray.Count();
       
   228 	for( TInt i=0; i < count; i++ )
       
   229 		{
       
   230 		HBufC* senderId = iSendersArray[i];
       
   231 		if( senderId )	
       
   232 			{
       
   233 			TInt unreadCount = iAccessor->GetUnreadMessageCountL(*senderId  ) ;
       
   234 			if( !unreadCount )
       
   235 				{
       
   236 				iSendersArray.Remove( i );
       
   237 				delete 	senderId;
       
   238 				senderId = NULL;
       
   239 				iSendersArray.Compress();
       
   240 				break;
       
   241 				}
       
   242 			}
       
   243 		}
       
   244 	TRACE_SWP(TXT("CServiceWidgetMessageHandler::SynchronizeSendersArrayL() end") );
       
   245 	}		
       
   246 	
       
   247 // end of file
       
   248 
       
   249