libtelepathy/tsrc/src/telepathygabblefetchcontacts.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 Fetch contacts related 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 
       
    27 #include "telepathygabblefetchcontacts.h"
       
    28 #include "telepathygabbletestdata.h"
       
    29 
       
    30 
       
    31 
       
    32 
       
    33 
       
    34 //-----------------------------------------------------------------------------
       
    35 // function_name	: CTelepathyGabbleFetchContacts
       
    36 // description     	: constructor
       
    37 //----------------------------------------------------------------------------- 
       
    38 CTelepathyGabbleFetchContacts::CTelepathyGabbleFetchContacts()
       
    39 {
       
    40 }
       
    41 
       
    42 //-----------------------------------------------------------------------------
       
    43 // function_name	: ConstructL
       
    44 // description     	: constructor
       
    45 //----------------------------------------------------------------------------- 
       
    46 void CTelepathyGabbleFetchContacts::ConstructL()
       
    47 {
       
    48 	//do nothing
       
    49 }
       
    50 
       
    51 //-----------------------------------------------------------------------------
       
    52 // function_name	: NewL
       
    53 // description     	: constructor
       
    54 //----------------------------------------------------------------------------- 
       
    55 CTelepathyGabbleFetchContacts* CTelepathyGabbleFetchContacts::NewL()
       
    56     {
       
    57     
       
    58 	CTelepathyGabbleFetchContacts* self = new(ELeave) CTelepathyGabbleFetchContacts;
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();    
       
    61     CleanupStack::Pop();
       
    62     return self;
       
    63     }
       
    64 
       
    65 //-----------------------------------------------------------------------------
       
    66 // function_name	: ~CTelepathyGabbleFetchContacts
       
    67 // description     	: Destructor
       
    68 //----------------------------------------------------------------------------- 
       
    69 CTelepathyGabbleFetchContacts::~CTelepathyGabbleFetchContacts()
       
    70     {
       
    71 	
       
    72     }
       
    73     
       
    74 //-----------------------------------------------------------------------------
       
    75 // function_name	: test_request_roster
       
    76 // description     	: Function for fetching the contacts.
       
    77 //----------------------------------------------------------------------------- 
       
    78 TInt CTelepathyGabbleFetchContacts::test_request_roster( CTestData* aTestData )
       
    79 {
       
    80 	
       
    81 	DBusGProxy * group_iface_subscribe = aTestData->GetGroupSubscribeInterface();
       
    82 	
       
    83 	tp_chan_iface_group_get_all_members_async(group_iface_subscribe, get_roster_member_cb, aTestData);
       
    84 
       
    85   	//Run the mainloop
       
    86 	g_main_loop_run (aTestData->GetMainLoop());
       
    87 
       
    88 	//retrun some error code here	
       
    89 	if ( aTestData->GetTotalFetchCount() != KErrGeneral )
       
    90 	{
       
    91 		return KErrNone;
       
    92 	}
       
    93 	else
       
    94 	{
       
    95 		return KErrGeneral;
       
    96 	}
       
    97 }
       
    98 
       
    99 //-----------------------------------------------------------------------------
       
   100 // function_name	: get_roster_member_cb
       
   101 // description     	: callback for getting the roster member.
       
   102 //----------------------------------------------------------------------------- 
       
   103 void  CTelepathyGabbleFetchContacts::get_roster_member_cb( DBusGProxy* /*proxy*/,GArray* current_members, GArray* local_pending_members, GArray* remote_pending_members, GError* /*error*/, gpointer userdata ) 
       
   104 {
       
   105 	guint fetch_count=0;
       
   106 	guint fetch_loop_count=0;	
       
   107 	GArray* fetch_members = NULL;
       
   108 	guint i;
       
   109 	guint total_len =0;
       
   110 	CTestData* testData = static_cast<CTestData*> (userdata);
       
   111 
       
   112 	
       
   113 	total_len = current_members->len + local_pending_members->len + remote_pending_members->len;
       
   114 
       
   115 	testData->SetTotalFetchCount(total_len);
       
   116 	
       
   117 	if( !total_len )
       
   118 		{
       
   119 			g_main_loop_quit(testData->GetMainLoop());
       
   120 			return;		
       
   121 			
       
   122 		}
       
   123 	
       
   124 	fetch_members = g_array_new (FALSE, FALSE, sizeof (guint32));
       
   125 
       
   126 	
       
   127 	if( current_members->len > 0)
       
   128 	{
       
   129 		fetch_count = 0;
       
   130 		fetch_loop_count = (current_members->len) / KMaxContactFetchCount + ( (current_members->len % KMaxContactFetchCount)? 1 : 0);
       
   131 		
       
   132 		for( i=0; i<fetch_loop_count; i++ )
       
   133 		{
       
   134 				
       
   135 			g_array_remove_range(fetch_members,0,fetch_count);
       
   136 			
       
   137 			fetch_count = (current_members->len <= KMaxContactFetchCount)? current_members->len : KMaxContactFetchCount;
       
   138 			
       
   139 			g_array_append_vals (fetch_members,current_members->data,fetch_count);
       
   140 			
       
   141 			g_array_remove_range(current_members,0,fetch_count);
       
   142 			
       
   143 			//we will quit the main loop in inspect_handles_cb
       
   144 			tp_conn_inspect_handles_async( DBUS_G_PROXY(testData->GetTpConn()),
       
   145 					       TP_CONN_HANDLE_TYPE_CONTACT ,fetch_members,
       
   146 					       inspect_handles_cb,userdata);
       
   147 		}
       
   148 	}
       
   149 	
       
   150 	if( local_pending_members->len > 0)
       
   151 	{
       
   152 		fetch_count = 0;
       
   153 		
       
   154 		g_array_remove_range(fetch_members,0,fetch_members->len);
       
   155 		
       
   156 		fetch_loop_count = (local_pending_members->len) / KMaxContactFetchCount + ( (local_pending_members->len % KMaxContactFetchCount)? 1 : 0);
       
   157 		
       
   158 		for( i=0; i<fetch_loop_count; i++ )
       
   159 		{
       
   160 				
       
   161 			g_array_remove_range(fetch_members,0,fetch_count);
       
   162 			
       
   163 			fetch_count = (local_pending_members->len <= KMaxContactFetchCount)? local_pending_members->len : KMaxContactFetchCount;
       
   164 			
       
   165 			
       
   166 			g_array_append_vals (fetch_members,local_pending_members->data,fetch_count);
       
   167 			
       
   168 			g_array_remove_range(local_pending_members,0,fetch_count);
       
   169 			
       
   170 			tp_conn_inspect_handles_async( DBUS_G_PROXY(testData->GetTpConn()),
       
   171 					       TP_CONN_HANDLE_TYPE_CONTACT ,fetch_members,
       
   172 					       inspect_handles_cb,userdata);
       
   173 		}
       
   174 
       
   175 	}
       
   176 
       
   177 	if( remote_pending_members->len > 0)
       
   178 	{
       
   179 		fetch_count = 0;
       
   180 		g_array_remove_range(fetch_members,0,fetch_members->len);
       
   181 		fetch_loop_count = (remote_pending_members->len) / KMaxContactFetchCount + ( (remote_pending_members->len % KMaxContactFetchCount)? 1 : 0);
       
   182 		
       
   183 		for( i=0; i<fetch_loop_count; i++ )
       
   184 		{
       
   185 				
       
   186 			g_array_remove_range(fetch_members,0,fetch_count);
       
   187 			
       
   188 			fetch_count = (remote_pending_members->len <= KMaxContactFetchCount)? remote_pending_members->len : KMaxContactFetchCount;
       
   189 			
       
   190 			g_array_append_vals (fetch_members,remote_pending_members->data,fetch_count);
       
   191 			g_array_remove_range(remote_pending_members,0,fetch_count);
       
   192 			tp_conn_inspect_handles_async( DBUS_G_PROXY(testData->GetTpConn()),
       
   193 					       TP_CONN_HANDLE_TYPE_CONTACT ,fetch_members,
       
   194 			  	       inspect_handles_cb,userdata);	
       
   195 		}	
       
   196 	
       
   197 	}
       
   198 	return;
       
   199 	
       
   200 }
       
   201 
       
   202 //-----------------------------------------------------------------------------
       
   203 // function_name	: inspect_handles_cb
       
   204 // description     	: inspect the handle for the contact, return the contact handle 
       
   205 //----------------------------------------------------------------------------- 
       
   206 void CTelepathyGabbleFetchContacts::inspect_handles_cb( DBusGProxy* /*proxy*/,char **handles_names,
       
   207 														GError *error, gpointer userdata ) 
       
   208 {
       
   209     CTestData* testData = static_cast<CTestData*> (userdata);
       
   210     TInt last_index = testData->GetLastIndexInFetch(); 
       
   211     guint total_len = testData->GetTotalFetchCount();
       
   212     
       
   213 	if(!last_index)
       
   214 	{
       
   215 		//allmembers_names = g_new0 ( char *, total_len + 1);		
       
   216 	}
       
   217 	
       
   218 	if(!handles_names || error )
       
   219 		{
       
   220 		//error condition in inspect_handles_cb
       
   221 		testData->SetTotalFetchCount(KErrGeneral);
       
   222 		g_main_loop_quit(testData->GetMainLoop());
       
   223 		return;
       
   224 		}
       
   225 		
       
   226 	for( TInt i=0; handles_names[i]; i++ )
       
   227 		{
       
   228 		
       
   229 		//handle_names can be logged to the log file
       
   230 		last_index++;
       
   231 		}
       
   232 		
       
   233 	testData->SetLastIndexInFetch(last_index);
       
   234 	
       
   235 	if (last_index == total_len )
       
   236 		{
       
   237 			//inspect_handle_cb for all the contacts has been receieved.
       
   238 			//Quit the main loop.
       
   239 			
       
   240 			//Set the flag that inspect_handles_cb has been completed successfully.
       
   241 			g_main_loop_quit(testData->GetMainLoop());	
       
   242 		}
       
   243 	
       
   244 	return ;
       
   245 }
       
   246 //  End of File