imservices/instantmessagingcache/imcacheclient/src/cimcacheupdater.cpp
changeset 0 e6b17d312c8b
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:  write right class implemetation
       
    15 *
       
    16 */
       
    17 
       
    18 //  INCLUDE FILES
       
    19 #include "cimcacheupdater.h"
       
    20 
       
    21 #include <imcachedefs.h>
       
    22 // logs
       
    23 #include "imcachedebugtrace.h"
       
    24 
       
    25 #include <e32base.h>
       
    26 #include <s32mem.h>
       
    27 
       
    28 //Received message maximum length is 400 character if received message is 
       
    29 //longer than 400 character truncate it to 400 character
       
    30 //This must always be in sync with imcvuiapp KReceiveMsgMaxLength
       
    31 const TInt KReceiveMsgMaxLength = 400; 
       
    32 _LIT(KPercentage, "%");
       
    33 // -----------------------------------------------------------------------------
       
    34 // CIMCacheUpdater::NewL()
       
    35 // Two-phased constructor.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CIMCacheUpdater* CIMCacheUpdater::NewL(TInt aServiceId,
       
    39 											const TDesC& aSenderId ,TBool aRegistrationNeeded )
       
    40     {
       
    41     TRACE( T_LIT("CIMCacheUpdater::NewL begin") );
       
    42     CIMCacheUpdater* self = new ( ELeave ) CIMCacheUpdater( aServiceId ) ;
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL( aSenderId, aRegistrationNeeded );
       
    45     CleanupStack::Pop( self );  //self
       
    46     TRACE( T_LIT("CIMCacheUpdater::NewL end") );
       
    47     return self;
       
    48     }
       
    49 // -----------------------------------------------------------------------------
       
    50 // CIMCacheUpdater::ConstructL()
       
    51 // Symbian OS default constructor can leave.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 void CIMCacheUpdater::ConstructL( const TDesC& aSenderId ,TBool aRegistrationNeeded )
       
    55     {
       
    56     TRACE( T_LIT("CIMCacheUpdater::ConstructL begin") );
       
    57     iUserId = aSenderId.AllocL();
       
    58     User::LeaveIfError( iClient.Connect() );
       
    59     EIMCacheOperations opration = EIMCacheInitUpdate;
       
    60     if( aRegistrationNeeded )
       
    61 	    {
       
    62 	    opration = EIMCacheInitConvesation;	
       
    63 	    }
       
    64     iClient.StartTransactionL(opration, iServiceId, *iUserId );
       
    65 	TRACE( T_LIT("CIMCacheUpdater::ConstructL end") );
       
    66     }
       
    67 // -----------------------------------------------------------------------------
       
    68 // CIMCacheUpdater::~CIMCacheUpdater()
       
    69 // Destructor
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 CIMCacheUpdater::~CIMCacheUpdater()
       
    73     {
       
    74     TRACE( T_LIT("CIMCacheUpdater::~CIMCacheUpdater begin") );
       
    75     delete iUserId;
       
    76     //do not close the client session here. since there is also accessor class which is closing the session
       
    77     //moved this part of code which is common to both udpater and Accessor i.e, client ~CIMCacheClient()
       
    78     //iClient.Close();
       
    79     TRACE( T_LIT("CIMCacheUpdater::~CIMCacheUpdater end") );
       
    80     }
       
    81 // -----------------------------------------------------------------------------
       
    82 // CIMCacheUpdater::CIMCacheUpdater()
       
    83 // C++ default constructor can NOT contain any code, that
       
    84 // might leave.
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CIMCacheUpdater::CIMCacheUpdater(TInt aServiceId )
       
    88 	:iServiceId( aServiceId )
       
    89 	{
       
    90 	TRACE( T_LIT("CIMCacheUpdater::CIMCacheUpdater ") );
       
    91 	}
       
    92 
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CIMCacheUpdater::StartNewConversationL()
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 void CIMCacheUpdater::StartNewConversationL(const TDesC& aBuddyId )
       
    99 	{
       
   100 	TRACE( T_LIT("CIMCacheUpdater::StartNewConversationL begin") );
       
   101 	if( !aBuddyId.Length() )
       
   102 		{
       
   103 		User::Leave( KErrArgument )	;
       
   104 		}
       
   105 	iClient.StartTransactionL(EIMCacheStartNewConversation, iServiceId , aBuddyId  );
       
   106 	
       
   107 	TRACE( T_LIT("CIMCacheUpdater::StartNewConversationL end") );
       
   108 	}
       
   109 	
       
   110 // -----------------------------------------------------------------------------
       
   111 // CIMCacheUpdater::AppendReceiveMessageL()
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CIMCacheUpdater::AppendReceiveMessageL( const TDesC& aBuddyId, 
       
   115 											 const TDesC& aText )
       
   116     {
       
   117     TRACE( T_LIT("CIMCacheUpdater::AppendReceiveMessageL begin") );	
       
   118     if( !aBuddyId.Length() )
       
   119         {
       
   120         User::Leave( KErrArgument )	;
       
   121         }
       
   122     TInt len = aText.Length();
       
   123     HBufC* bigmsg = NULL;
       
   124     bigmsg = HBufC::NewLC(KReceiveMsgMaxLength + 1) ; // 1 for  %
       
   125     if( bigmsg )
       
   126         {
       
   127         TRACE( T_LIT("Inside if( bigmsg )") );
       
   128         TPtr bigmsgPtr = bigmsg->Des();
       
   129         //If message is more than 400 character take only first 400 character
       
   130         //rest of the message will be lost
       
   131         bigmsgPtr.Append( aText.Left( KReceiveMsgMaxLength ) ); 
       
   132         if( len > KReceiveMsgMaxLength)
       
   133             {
       
   134             //append % as 401st character to identify on UI that it is a 
       
   135             //long message which is truncated        
       
   136             bigmsgPtr.Append(KPercentage); 
       
   137             }
       
   138 
       
   139         iClient.StartTransactionL(EIMCacheAppendReceiveMessage, iServiceId, aBuddyId , bigmsgPtr );
       
   140 
       
   141         CleanupStack::PopAndDestroy(bigmsg); // bigmsg
       
   142         }
       
   143 	TRACE( T_LIT("CIMCacheUpdater::AppendReceiveMessageL end") );
       
   144 	}
       
   145 // -----------------------------------------------------------------------------
       
   146 // CIMCacheUpdater::AppendSendMessageL()
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 void CIMCacheUpdater::AppendSendMessageL(const TDesC& aText )
       
   150 	{
       
   151   	TRACE( T_LIT("CIMCacheUpdater::AppendSendMessageL begin") );
       
   152   	
       
   153 	iClient.StartTransactionL( EIMCacheAppendSendMessage , aText);	
       
   154 	TRACE( T_LIT("CIMCacheUpdater::AppendSendMessageL end") );
       
   155 	}
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CIMCacheUpdater::AppendMessageL()
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CIMCacheUpdater::AppendMessageL(const TDesC& aBuddyId, const TDesC& aText )
       
   162 	{
       
   163   	TRACE( T_LIT("CIMCacheUpdater::AppendMessageL begin") );
       
   164 	if( !aBuddyId.Length() )
       
   165 		{
       
   166 		User::Leave( KErrArgument )	;
       
   167 		}
       
   168 	iClient.StartTransactionL(EIMCacheAppendMessage, iServiceId, aBuddyId, aText);	
       
   169 	TRACE( T_LIT("CIMCacheUpdater::AppendMessageL end") );
       
   170 	}
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CIMCacheUpdater::CloseConversation()
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 void CIMCacheUpdater::CloseConversationL( const TDesC& aBuddyId )
       
   177     {
       
   178     TRACE( T_LIT("CIMCacheUpdater::CloseConversationL begin") ); 
       
   179     
       
   180 	if( !aBuddyId.Length() )
       
   181 		{
       
   182 		User::Leave( KErrArgument )	;
       
   183 		}
       
   184     iClient.StartTransactionL( EIMCacheCloseConversation,
       
   185 								iServiceId, 
       
   186 								aBuddyId                                
       
   187 								);
       
   188     TRACE( T_LIT("CIMCacheUpdater::CloseConversationL end") );
       
   189    }
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CIMCacheUpdater::CloseConversation()
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 void CIMCacheUpdater::CloseAllConversationL()
       
   196     {
       
   197     TRACE( T_LIT("CIMCacheUpdater::CloseAllConversationL begin") ); 
       
   198     iClient.StartTransactionL( EIMCacheCloseAllConversation );
       
   199     TRACE( T_LIT("CIMCacheUpdater::CloseAllConversationL end") );
       
   200     }
       
   201 // -----------------------------------------------------------------------------
       
   202 // CIMCacheUpdater::DeactivateConversationL()
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 TInt CIMCacheUpdater::DeactivateConversationL( )
       
   206 	{
       
   207 	TRACE( T_LIT("CIMCacheUpdater::DeactivateConversationL begin") );
       
   208 	
       
   209 	TInt error = iClient.StartTransactionL( EIMCacheDeactivateConversation );
       
   210 								 
       
   211 	TRACE( T_LIT("CIMCacheUpdater::DeactivateAllConversationsL end") );
       
   212 	return error;
       
   213 	}
       
   214 // -----------------------------------------------------------------------------
       
   215 // CIMCacheUpdater::ServiceId()
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 TInt CIMCacheUpdater::ServiceId() const 
       
   219 	{
       
   220 	return iServiceId;	
       
   221 	}
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CIMCacheAccess::RegisterObserverL()
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 void CIMCacheUpdater::RegisterObserverL( MIMCacheEventHandler& aObserver  )
       
   228 	{
       
   229 	TRACE( T_LIT("CIMCacheAccess::RegisterObserverL begin") );
       
   230 	// update case ETrue says CV is active
       
   231 	// cch need not to register
       
   232 	iClient.RegisterUpdateObserverL( aObserver  );
       
   233 	TRACE( T_LIT("CIMCacheAccess::RegisterObserverL end") );
       
   234 	}
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CIMCacheAccess::UnRegisterObserverL()
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 void CIMCacheUpdater::UnRegisterObserver( MIMCacheEventHandler& aObserver )
       
   241 	{
       
   242 	TRACE( T_LIT("CIMCacheAccess::UnRegisterObserverL begin") );
       
   243 	iClient.UnRegisterUpdateObserver( aObserver );
       
   244 	TRACE( T_LIT("CIMCacheAccess::UnRegisterObserverL end") );
       
   245 	}	
       
   246   
       
   247 // -----------------------------------------------------------------------------
       
   248 // CIMCacheAccess::GetChatListL()
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 RArray<SIMCacheChatItem> CIMCacheUpdater::GetChatListL( const TInt aServiceId  )
       
   252 	{
       
   253 	TRACE( T_LIT("CIMCacheUpdater::GetChatListL") );	
       
   254 	RArray<SIMCacheChatItem> chatListArray;//array to hold chat item
       
   255 	TBool more = EFalse;
       
   256 	TPtr8 bufferPtr =  iClient.GetChatListL( aServiceId );
       
   257 	InternalizeChatListL( chatListArray, bufferPtr, more );
       
   258 	
       
   259 	while( more)
       
   260 		{
       
   261 		bufferPtr = iClient.GetBufferedDataL( EIMCacheGetBufferedPackets );
       
   262 		InternalizeChatListL( chatListArray, bufferPtr, more );
       
   263 		}
       
   264 	return chatListArray;
       
   265 	}
       
   266     
       
   267 // -----------------------------------------------------------------------------
       
   268 // CIMCacheUpdater::InternalizeChatListL
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void CIMCacheUpdater::InternalizeChatListL(RArray<SIMCacheChatItem>& aChatArray, TPtr8 aChatListBuffer ,TBool& aMore )
       
   272     {
       
   273     TRACE( T_LIT("CIMCacheUpdater::InternalizeChatListL() begin") );   
       
   274     
       
   275     RDesReadStream readAllStream ;
       
   276     RDesReadStream readStream ;    
       
   277     
       
   278     readAllStream.Open( aChatListBuffer );
       
   279     CleanupClosePushL(readAllStream);
       
   280     
       
   281     TInt messageCount = readAllStream.ReadInt16L();
       
   282     
       
   283     for( TInt i=0; i< messageCount; i++)
       
   284         {
       
   285         SIMCacheChatItem chatItem =
       
   286           {
       
   287           0,
       
   288           };
       
   289                 
       
   290         TInt size = readAllStream.ReadInt16L() ;
       
   291         HBufC8* mBuffer  = HBufC8::NewLC( size );
       
   292         TPtr8 mBufferPtr = mBuffer->Des();
       
   293                 
       
   294         readAllStream.ReadL( mBufferPtr , size );
       
   295         
       
   296         readStream.Open( *mBuffer );        
       
   297         CleanupClosePushL(readStream) ;
       
   298         
       
   299         chatItem.iServiceId  = static_cast<TInt>( readStream.ReadInt16L() );
       
   300         
       
   301         TInt textSize = readStream.ReadInt16L();
       
   302         HBufC* buddyId = HBufC::NewLC( textSize );
       
   303         TPtr buddyIdPtr = buddyId->Des(); 
       
   304         readStream.ReadL( buddyIdPtr , textSize);
       
   305         
       
   306         chatItem.iBuddyId = buddyId;
       
   307         CleanupStack::Pop(buddyId); // ownership to structure variable
       
   308         aChatArray.Insert(chatItem, 0);
       
   309         CleanupStack::PopAndDestroy(2);//close read stream, delete mBuffer.                
       
   310         }
       
   311     aMore = static_cast<TBool>( readAllStream.ReadInt16L() );
       
   312     
       
   313     CleanupStack::PopAndDestroy();//readAllStream.    
       
   314     
       
   315     TRACE( T_LIT("CIMCacheUpdater::InternalizeChatListL() End") );
       
   316     }
       
   317 
       
   318 //  END OF FILE