libtelepathy/tsrc/src/telepathygabblesendmessage.cpp
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /*
       
     2 * Copyright (c) 2008 - 2009 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 the License "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:    Used for Send/Receive Test Cases
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20  
       
    21 //Include files
       
    22 #include <e32err.h>
       
    23 #include <e32const.h>
       
    24 #include <e32base.h>
       
    25 #include <glib.h>
       
    26 #include <StifLogger.h>
       
    27 #include "telepathygabbleutils.h"
       
    28 #include "telepathygabblesendmessage.h"
       
    29 #include "telepathygabbletestdata.h"
       
    30 
       
    31 
       
    32 
       
    33 
       
    34 //-----------------------------------------------------------------------------
       
    35 // function_name	: CTelepathyGabbleSendRecvMessage
       
    36 // description     	: constructor
       
    37 //----------------------------------------------------------------------------- 
       
    38 
       
    39 CTelepathyGabbleSendRecvMessage::CTelepathyGabbleSendRecvMessage()
       
    40 {
       
    41 }
       
    42 
       
    43 //-----------------------------------------------------------------------------
       
    44 // function_name	: ConstructL
       
    45 // description     	: constructor
       
    46 //----------------------------------------------------------------------------- 
       
    47 void CTelepathyGabbleSendRecvMessage::ConstructL()
       
    48 {
       
    49 	
       
    50 }
       
    51 
       
    52 //-----------------------------------------------------------------------------
       
    53 // function_name	: NewL
       
    54 // description     	: NewL
       
    55 //----------------------------------------------------------------------------- 
       
    56 CTelepathyGabbleSendRecvMessage* CTelepathyGabbleSendRecvMessage::NewL()
       
    57     {
       
    58 	CTelepathyGabbleSendRecvMessage* self = new(ELeave) CTelepathyGabbleSendRecvMessage;
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();    
       
    61     CleanupStack::Pop();
       
    62     return self;
       
    63     }
       
    64 
       
    65 //-----------------------------------------------------------------------------
       
    66 // function_name	:  ~CTelepathyGabbleSendRecvMessage
       
    67 // description     	: Destructor
       
    68 //----------------------------------------------------------------------------- 
       
    69 CTelepathyGabbleSendRecvMessage::~CTelepathyGabbleSendRecvMessage()
       
    70     {
       
    71     }
       
    72     	
       
    73 
       
    74 //-----------------------------------------------------------------------------
       
    75 // function_name	: SendMessage
       
    76 // description     	: To send a message
       
    77 //----------------------------------------------------------------------------- 
       
    78 TInt CTelepathyGabbleSendRecvMessage::SendMessage( const char *contact_id, 
       
    79 													const char *message,
       
    80 													CTestData* aTestData )
       
    81 {
       
    82 	TpChan* text_chan = NULL; //for send msg
       
    83 
       
    84 	TInt contact_handle;
       
    85 	GArray *handles = NULL;
       
    86 	GError* error = NULL;
       
    87 	DBusGProxy  *text_iface = NULL;
       
    88 
       
    89 	gpointer userdata = NULL; 
       
    90 	
       
    91 	
       
    92 	//get the contact handle.
       
    93 	contact_handle =  CTelepathyGabbleUtils::get_handle_from_contact(contact_id,aTestData);
       
    94 	
       
    95 	if ( contact_handle == KErrNotFound )
       
    96 		{
       
    97 		return CTestData::EMessageSendErr;	
       
    98 		}
       
    99 
       
   100 	text_chan = (TpChan*)g_hash_table_find(
       
   101 								   aTestData->GetTextChannelsHT(),
       
   102 							      (GHRFunc) text_channels_find_func,
       
   103 							      &contact_handle );	 
       
   104 	      
       
   105 	//text_chan for the particular contact does not exist.
       
   106 	//New text_chan must be created.						      
       
   107 	if (text_chan == NULL)	
       
   108 		{ 
       
   109 		text_chan = tp_conn_new_channel(
       
   110 							(aTestData->GetDBusConnection()),
       
   111 							(aTestData->GetTpConn()),CONNMGR_BUS,TP_IFACE_CHANNEL_TYPE_TEXT,
       
   112 							TP_CONN_HANDLE_TYPE_CONTACT,contact_handle, TRUE );	
       
   113 
       
   114 		//Insert the new text_chan to the hash table.							
       
   115 
       
   116 		g_hash_table_insert(
       
   117 				(aTestData->GetTextChannelsHT()),
       
   118 				g_strdup(dbus_g_proxy_get_path(DBUS_G_PROXY(text_chan))),text_chan);
       
   119 					
       
   120 		}
       
   121 
       
   122 	text_iface = tp_chan_get_interface(text_chan,TELEPATHY_CHAN_IFACE_TEXT_QUARK);	
       
   123 	
       
   124 	
       
   125 	if( aTestData->GetTestType() == CTestData::ESendErr ||
       
   126 		aTestData->GetTestType() == CTestData::EReceive )
       
   127 		{
       
   128 		//Register the callback to get the error
       
   129 		//when the recipeint_id is invalid.
       
   130 		dbus_g_proxy_connect_signal(text_iface, "SendError",
       
   131 					G_CALLBACK(SendError_cb),
       
   132 						aTestData, NULL);	
       
   133 		}
       
   134 
       
   135 	if( aTestData->GetTestType() == CTestData::EReceive )
       
   136 		{
       
   137 		//Register for the receive message callback here.
       
   138 	 	dbus_g_proxy_connect_signal(text_iface, "Received",
       
   139 					(G_CALLBACK(receivedMessage_cb)),
       
   140 					aTestData, NULL);	
       
   141 		}
       
   142 
       
   143  	
       
   144 	
       
   145 	//This callback tells whether the message is sent to the server or not.
       
   146 	
       
   147 	tp_chan_type_text_send_async( text_iface, TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, 
       
   148 	message,sendmessage_cb, aTestData); 
       
   149 	
       
   150 	
       
   151 	// user_data = this i.e. the class pointer  
       
   152 
       
   153 	//Run the mainloop
       
   154 	g_main_loop_run (aTestData->GetMainLoop());
       
   155 
       
   156 
       
   157 	//return the Message_sent that was updated in the callback sendmessage_cb	
       
   158 	return aTestData->GetSendRecvMsgState();
       
   159 }
       
   160 
       
   161 //-----------------------------------------------------------------------------
       
   162 // function_name	: ReceiveMessage
       
   163 // description     	: receive message
       
   164 //----------------------------------------------------------------------------- 
       
   165 TInt CTelepathyGabbleSendRecvMessage::ReceiveMessage(   const char *contact_id,
       
   166 														const char *message,
       
   167 														CTestData* aTestData )
       
   168 {
       
   169 	TpChan* text_chan = NULL; //for send msg
       
   170 	DBusGProxy *text_iface;
       
   171 
       
   172 	guint contact_handle;
       
   173 	
       
   174 	
       
   175 	 
       
   176 	//send msg 1
       
   177 	//For Automation, Do the send message to yourself
       
   178 	//own contact_id received from the cfg file.
       
   179 	TInt msg_status = SendMessage(contact_id,message,aTestData);
       
   180 	if( msg_status == CTestData::EMessageNotSent )
       
   181 		{
       
   182 			aTestData->GetStifLogger()->Log( _L("ReceiveMessage: Msg is not sent successfully \n") );
       
   183 			
       
   184 		    if( aTestData->GetTestType() == CTestData::ERecvBlank )
       
   185 		    	g_main_loop_quit(aTestData->GetMainLoop()); 
       
   186 		    
       
   187 			return CTestData::EMessageNotSent;
       
   188 		}
       
   189 	 
       
   190 
       
   191  	/*contact_handle =  CTelepathyGabbleUtils::get_handle_from_contact(contact_id,aTestData);
       
   192  	
       
   193  	text_chan = (TpChan*)g_hash_table_find(
       
   194  					(aTestData->GetTextChannelsHT()),
       
   195 				      (GHRFunc) text_channels_find_func,
       
   196 				      &contact_handle );	
       
   197     text_iface = tp_chan_get_interface(text_chan ,
       
   198 					   TELEPATHY_CHAN_IFACE_TEXT_QUARK);
       
   199 					   
       
   200  	//Register for the receive message callback here.
       
   201  	dbus_g_proxy_connect_signal(text_iface, "Received",
       
   202 				(G_CALLBACK(receivedMessage_cb)),
       
   203 				aTestData, NULL);
       
   204  	
       
   205  	//send msg 2
       
   206  	//Do the send message to yourself
       
   207  	
       
   208 	msg_status == CTestData::EMessageNotSent )
       
   209 		{
       
   210 			return CTestData::EMessageNotSent;
       
   211 		}*/
       
   212 	aTestData->GetStifLogger()->Log( _L("after : register receivedMessage_cb \n") );
       
   213 	if (aTestData->GetTestType() == CTestData::EReceive && 
       
   214 			aTestData->GetSendRecvMsgState() != CTestData::EMessageSendErr )
       
   215 	{
       
   216 		//there are two callbacks now.
       
   217 		//receivedMessage_cb and sendmessage_cb.
       
   218 		
       
   219 		//Run the mainloop again as it was quit in the callback sendmessage_cb.
       
   220 		//the message_state is not yet MESSAGE_RECEIVED.
       
   221 		//we need to start the main loop again to get the receivedmessage_cb.
       
   222 		aTestData->GetStifLogger()->Log( _L("start mainloop if state not as EMESSAGE_RCVD \n") );
       
   223  		g_main_loop_run (aTestData->GetMainLoop());
       
   224  		
       
   225  	}
       
   226   return aTestData->GetSendRecvMsgState();
       
   227 }
       
   228 
       
   229 //-----------------------------------------------------------------------------
       
   230 // function_name	: sendmessage_cb
       
   231 // description     	: Callback for Send Message
       
   232 //----------------------------------------------------------------------------- 
       
   233 void CTelepathyGabbleSendRecvMessage::sendmessage_cb( DBusGProxy* /*proxy*/, GError* /*error*/, gpointer userdata ) 
       
   234 {
       
   235 	CTestData* testdata = static_cast<CTestData*> (userdata);
       
   236 	
       
   237 	testdata->GetStifLogger()->Log( _L("inside sendmessage_cb \n") );
       
   238 		
       
   239  	//This is the test case for sending message to invalid userid.
       
   240  	if ( testdata->GetTestType() != CTestData::ESendErr )
       
   241  		
       
   242  	{
       
   243  	    	    
       
   244 		testdata->SetSendRecvMsgState(CTestData::EMessageSent);
       
   245 		testdata->GetStifLogger()->Log( _L("sendmessage_cb::msg sent successfully \n") );
       
   246  
       
   247  		//Quit the main loop , just for now
       
   248  		g_main_loop_quit(testdata->GetMainLoop()); 
       
   249  	}
       
   250  	else if( testdata->GetTestType() == CTestData::ERecvBlank )
       
   251  	{
       
   252  	  	
       
   253  		//Quit the main loop , just for now
       
   254  		testdata->GetStifLogger()->Log( _L("sendmessage_cb::test case is for blank msg sending \n") );
       
   255  		g_main_loop_quit(testdata->GetMainLoop()); 	
       
   256  	}
       
   257  	
       
   258  
       
   259  	//otherwise send message error test case, dont quit.
       
   260 
       
   261 }
       
   262 
       
   263 //-----------------------------------------------------------------------------
       
   264 // function_name	: receivedMessage_cb
       
   265 // description     	: Callback for receive message
       
   266 //----------------------------------------------------------------------------- 
       
   267 void CTelepathyGabbleSendRecvMessage::receivedMessage_cb (  DBusGProxy* /*proxy*/,
       
   268 								guint            /* message_id */,
       
   269 							   	guint            /* timestamp */,
       
   270 							    guint           /* from_handle */,
       
   271 							    guint           /* message_type */,
       
   272 							    guint           /* message_flags */,
       
   273 							    gchar           * /*message_body */,
       
   274 							    gpointer		user_data )
       
   275 {
       
   276 	//GArray  *message_ids;
       
   277 	//GArray  *handles = NULL;
       
   278 	CTestData* testdata = static_cast<CTestData*> (user_data);
       
   279 	
       
   280 	
       
   281 	/*message_ids = g_array_new (FALSE, FALSE, sizeof (guint));
       
   282 
       
   283 	g_array_append_val (message_ids, message_id);
       
   284 
       
   285 	g_array_free (message_ids, TRUE);
       
   286 	
       
   287 	handles = g_array_new (FALSE, FALSE, sizeof (guint32));
       
   288 	
       
   289 	g_array_append_val (handles, from_handle);	
       
   290 	
       
   291 	g_array_free (handles, TRUE);*/
       
   292 	testdata->GetStifLogger()->Log( _L("receivedMessage_cb::test case is for receive msg \n") );
       
   293 	
       
   294 	if( testdata->GetTestType() == CTestData::EReceive )
       
   295 		{
       
   296 		testdata->SetSendRecvMsgState(CTestData::EMessageRecvd);
       
   297 		
       
   298 		//Quit the main loop once the receive message callback is received. 	
       
   299 	 	g_main_loop_quit(testdata->GetMainLoop());
       
   300 			
       
   301 		}
       
   302 	
       
   303 
       
   304 	
       
   305 }
       
   306 
       
   307 //-----------------------------------------------------------------------------
       
   308 // function_name	: text_channels_find_func
       
   309 // description     	: find whether the text channel 
       
   310 //										is existing given the hash table pointer
       
   311 //----------------------------------------------------------------------------- 
       
   312 gboolean CTelepathyGabbleSendRecvMessage::text_channels_find_func(	gchar* /*key*/,
       
   313 									TpChan *text_channel_ht_ptr,
       
   314 									guint *contact_handle)
       
   315 {
       
   316 	if ((text_channel_ht_ptr->handle == *contact_handle))
       
   317 	{
       
   318 		return TRUE;
       
   319 	}//else
       
   320 	return FALSE;
       
   321 } 
       
   322 
       
   323 void CTelepathyGabbleSendRecvMessage::SendError_cb (  DBusGProxy* /*proxy*/,
       
   324 								guint            error,
       
   325 							   	guint            /*timestamp*/,
       
   326 							    guint            /*message_type*/,
       
   327 							    gchar*           /*message_body*/,
       
   328 							    gpointer		user_data )
       
   329 {
       
   330 	
       
   331 	//Get the testdata
       
   332 	CTestData* testdata = static_cast<CTestData*> (user_data);
       
   333 	
       
   334 	FILE* fp;
       
   335 		
       
   336 	fp = fopen("c:\\testlogs.txt","a");
       
   337 	if(fp)
       
   338 	{
       
   339 		fprintf(fp,"error  code is %u", error);
       
   340 		fprintf(fp,"\n");
       
   341 		fclose(fp);
       
   342 	}
       
   343 
       
   344 	testdata->GetStifLogger()->Log( _L("inside SendError_cb \n") );
       
   345 	
       
   346 
       
   347 	testdata->SetSendRecvMsgState(CTestData::EMessageSendErr);		
       
   348   	//quit the main loop
       
   349   	g_main_loop_quit(testdata->GetMainLoop());	
       
   350 }