imservices/ossprotocoladaptation/src/im/creceivemessagehandler.cpp
changeset 0 e6b17d312c8b
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     1 /*
       
     2 * Copyright (c) 2007-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:  implementation of receive message handler
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <ximpprotocolconnectionhost.h>
       
    20 #include "creceivemessagehandler.h"
       
    21 #include "mossprotocolconnectionmanager.h"
       
    22 #include "ossprotocolpluginlogger.h"
       
    23 #include "waittimer.h"
       
    24 #include "cossprotocolconnectionmanager.h"
       
    25 #include <ximpobjectfactory.h>
       
    26 #include <ximpobjectcollection.h>
       
    27 #include <protocolpresentitygroupsdatahost.h>
       
    28 #include <presentitygroupinfo.h>
       
    29 #include <presentitygroupmemberinfo.h>
       
    30 #include <ximpidentity.h>
       
    31 #include <protocolpresencepublishingdatahost.h>
       
    32 #include <protocolpresencedatahost.h>
       
    33 #include <presenceobjectfactory.h>
       
    34 #include <presencegrantrequestinfo.h>
       
    35 #include <personpresenceinfo.h>
       
    36 #include <servicepresenceinfo.h>
       
    37 #include <devicepresenceinfo.h>
       
    38 #include <presenceinfo.h>
       
    39 #include <presenceinfofield.h>
       
    40 #include <presenceinfofieldcollection.h>
       
    41 #include <presenceinfofieldvalueenum.h>
       
    42 #include <presenceinfofieldvaluetext.h>
       
    43 #include <ximpcontext.h>
       
    44 #include <ximpclient.h>
       
    45 #include <devicepresenceinfo.h>
       
    46 #include <protocolpresencewatchingdatahost.h>
       
    47 #include <presencegrantrequestinfo.h>
       
    48 #include <protocolpresenceauthorizationdatahost.h>
       
    49 #include <e32des16.h>
       
    50 #include <imconversationinfo.h>
       
    51 #include <imobjectfactory.h>
       
    52 #include <protocolimdatahost.h>
       
    53 #include <protocolimconversationdatahost.h>
       
    54 #include <glib/gprintf.h>
       
    55 #include <string.h>
       
    56 #include <sys/types.h>
       
    57 #include <stdio.h>
       
    58 #include <stdlib.h>
       
    59 #include <glib/gstrfuncs.h>
       
    60 #include "ossprotocoladaptutils.h"
       
    61 
       
    62 
       
    63 // ======== MEMBER FUNCTIONS ========
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CReceiveMessageHandler::CReceiveMessageHandler
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CReceiveMessageHandler::CReceiveMessageHandler (
       
    70     MOSSProtocolConnectionManager& aConnMan ) :
       
    71 
       
    72 		CActive ( EPriorityNormal ),
       
    73 		iConnMan ( aConnMan )
       
    74 	{
       
    75 	LOGGER ( TXT ( "::CReceiveMessageHandler Start" ) );
       
    76 	CActiveScheduler::Add ( this );
       
    77 
       
    78 	LOGGER ( TXT ( "::CReceiveMessageHandler End" ) );
       
    79 	}
       
    80 
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CReceiveMessageHandler::ConstructL
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 void CReceiveMessageHandler::ConstructL()
       
    87 	{
       
    88 	LOGGER ( TXT ( "CReceiveMessageHandler::ConstructL Start-End" ) );
       
    89 
       
    90 
       
    91 	}
       
    92 
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // CReceiveMessageHandler::NewL
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 CReceiveMessageHandler* CReceiveMessageHandler::NewL (
       
    99     MOSSProtocolConnectionManager& aConnMan )
       
   100 	{
       
   101 	LOGGER ( TXT ( "CReceiveMessageHandler::NewL Start" ) );
       
   102 
       
   103 	CReceiveMessageHandler* self = new ( ELeave )
       
   104 	CReceiveMessageHandler ( aConnMan );
       
   105 	CleanupStack::PushL ( self );
       
   106 	self->ConstructL();
       
   107 	CleanupStack::Pop ( self );
       
   108 
       
   109 	LOGGER ( TXT ( "CReceiveMessageHandler::NewL End" ) );
       
   110 	return self;
       
   111 	}
       
   112 
       
   113 
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // CReceiveMessageHandler::~CReceiveMessageHandler
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 CReceiveMessageHandler::~CReceiveMessageHandler()
       
   120 	{
       
   121 	LOGGER ( TXT ( "::~CReceiveMessageHandler Start" ) );
       
   122 
       
   123 	Cancel();
       
   124 
       
   125 	LOGGER ( TXT ( "::~CReceiveMessageHandler End" ) );
       
   126 	}
       
   127 
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // CReceiveMessageHandler::DoCancel()
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 void CReceiveMessageHandler::DoCancel()
       
   134 	{
       
   135 	LOGGER ( TXT ( "CReceiveMessageHandler::DoCancel Start" ) );
       
   136 
       
   137 	iConnMan.DataHandler().CancelListeningMsg();
       
   138 
       
   139 	LOGGER ( TXT ( "CReceiveMessageHandler::DoCancel End" ) );
       
   140 	}
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // CReceiveMessageHandler::RunL()
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void CReceiveMessageHandler::RunL()
       
   147 	{
       
   148 	LOGGER ( TXT ( "CReceiveMessageHandler::RunL Start" ) );
       
   149 
       
   150 	// something is coming from the xmpp server
       
   151 	User::LeaveIfError ( iStatus.Int() );
       
   152 	
       
   153 	TRAPD( error, ProcessIncomingDataL() );
       
   154     if ( error != KErrNone )
       
   155     {
       
   156     	Cancel();
       
   157     }
       
   158    
       
   159     // start listening again for next incoming message
       
   160 	StartListeningL();
       
   161 
       
   162 	LOGGER ( TXT ( "CReceiveMessageHandler::RunL End" ) );
       
   163 	}
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // CReceiveMessageHandler::ProcessIncomingDataL()
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CReceiveMessageHandler::ProcessIncomingDataL()
       
   170 	{
       
   171 	LOGGER ( TXT ( "CReceiveMessageHandler::ProcessIncomingDataL Start" ) );
       
   172 	
       
   173 	char* pResponse = iConnMan.DataHandler().ResponseL ( 1 );
       
   174 	CleanupStack::PushL( pResponse );
       
   175 	
       
   176 	//-----Response format--------
       
   177 	//|------|------/0|-------/0|
       
   178 	//|header|sender/0|message/0|
       
   179 	
       
   180 	//parse sender.
       
   181 	char* sender = pResponse + sizeof( message_hdr_resp );
       
   182 	HBufC16 *sendername = OssProtocolAdapUtils::ConvertCharToTUint16LC(sender); 
       
   183 	
       
   184 	//parse message.
       
   185 	char* recvdmessage = pResponse + sizeof( message_hdr_resp ) + strlen(sender) + 1;
       
   186     HBufC16 *message = OssProtocolAdapUtils::ConvertCharToTUint16LC(recvdmessage);
       
   187 		
       
   188 	MXIMPObjectFactory& objFact = iConnMan.HandleToHost().ObjectFactory();
       
   189 	MImObjectFactory& imObjFact = iConnMan.GetImHost().ImObjectFactory();
       
   190 	MXIMPIdentity* identity = objFact.NewIdentityLC();
       
   191 	MImConversationInfo* convinfo = imObjFact.NewImConversationInfoLC();
       
   192 	
       
   193 	identity->SetIdentityL ( *sendername );
       
   194 	convinfo->SetMessageIdL ( identity );
       
   195 	convinfo->SetTextMessageL ( *message );   //SetTextMessageL(const TDesC16& aMessage )
       
   196 	iConnMan.GetImHost().ConversationDataHost().HandleNewTextMessageL ( convinfo );
       
   197 	
       
   198 	CleanupStack::Pop( 2 ); //convinfo,identity
       
   199 	CleanupStack::PopAndDestroy( 3 ); //message,sendername,pResponse. 
       
   200 	
       
   201 	LOGGER ( TXT ( "CReceiveMessageHandler::ProcessIncomingDataL End" ) );
       
   202 	}
       
   203 
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // CReceiveMessageHandler::RunError
       
   207 // ---------------------------------------------------------------------------
       
   208 //
       
   209 TInt CReceiveMessageHandler::RunError ( TInt  aError )
       
   210 	{
       
   211 	LOGGER ( TXT ( "CReceiveMessageHandler::RunError Start %d" ), aError );
       
   212 
       
   213 	// restart listening incoming data
       
   214 	iConnMan.DataHandler().ListenIncomingMsg ( iStatus );
       
   215 	SetActive();
       
   216 
       
   217 	LOGGER ( TXT ( "CReceiveMessageHandler::RunError End" ) );
       
   218 	return KErrNone;
       
   219 	}
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // CReceiveMessageHandler::StartListeningL
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 void CReceiveMessageHandler::StartListeningL()
       
   226 	{
       
   227 	LOGGER ( TXT ( "CReceiveMessageHandler::StartListeningL Start" ) );
       
   228 
       
   229 	// Ready to receive notification from the server
       
   230 	TInt count = 0;
       
   231 
       
   232 	// signal the scheduler
       
   233 
       
   234 	if ( ! ( this->IsActive() ) )
       
   235 		{
       
   236 		count = iConnMan.DataHandler().ListenIncomingMsg ( iStatus );
       
   237 		SetActive();
       
   238 		}
       
   239 
       
   240 	for ( TInt x ( 0 ) ;  x < count ; ++x )
       
   241 		{
       
   242 		ProcessIncomingDataL();
       
   243 		}
       
   244 
       
   245 	LOGGER ( TXT ( "CReceiveMessageHandler::StartListeningL End" ) );
       
   246 	}
       
   247 
       
   248 
       
   249 
       
   250 // End of file