imservices/ossprotocoladaptation/src/search/csearchrequest.cpp
branchRCL_3
changeset 14 7797b2f86d2b
parent 13 b6f2a363adf7
child 16 cfe5eb8bb9ca
equal deleted inserted replaced
13:b6f2a363adf7 14:7797b2f86d2b
     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:  Search implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <string.h>
       
    20 #include <stdlib.h>
       
    21 
       
    22 #include <ximpprotocolconnectionhost.h>
       
    23 #include <ximpobjectfactory.h>
       
    24 #include <searchelement.h>
       
    25 #include <searchobjectfactory.h>
       
    26 #include <ximpobjectcollection.h>
       
    27 #include <searchinfo.h>
       
    28 #include <searchobjectfactory.h>
       
    29 #include <protocolsearchdatahost.h>
       
    30 #include <protocolsearchfeaturedatahost.h>
       
    31 #include <ximpidentity.h>
       
    32 
       
    33 #include "stringutils.h"
       
    34 
       
    35 #include "msg_enums.h"
       
    36 #include "waittimer.h"
       
    37 #include "msgliterals.h"
       
    38 #include "ossprotocoladaptutils.h"
       
    39 #include "csearchrequest.h"
       
    40 #include "mossprotocolconnectionmanager.h"
       
    41 #include "ossprotocolpluginlogger.h"
       
    42 
       
    43 #include "cossprotocolconnectionmanager.h"
       
    44 
       
    45 
       
    46 // search  strings  used 
       
    47 const char* FirstName ="first_name";
       
    48 const char* LastName ="last_name";
       
    49 const char* UserId ="user_id";
       
    50 const char* Jid ="jid";
       
    51 
       
    52 // ======== MEMBER FUNCTIONS ========
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CSearchrequest::CSearchrequest
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CSearchrequest::CSearchrequest(	MOSSProtocolConnectionManager& aConnMan,
       
    59 									TXIMPRequestId aRequestId ) :
       
    60     CActive( EPriorityNormal ),
       
    61     iRequestId( aRequestId ),
       
    62     iConnMan( aConnMan )
       
    63     {
       
    64     LOGGER ( TXT("::CSearchrequest Start") );
       
    65     CActiveScheduler::Add( this );
       
    66     
       
    67     LOGGER ( TXT("::CSearchrequest End") );
       
    68     }
       
    69 
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CSearchrequest::ConstructL
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 void CSearchrequest::ConstructL()
       
    76     {
       
    77     LOGGER ( TXT("CSearchrequest::ConstructL Start-End") );
       
    78     
       
    79 	iSearchId=NULL;
       
    80     }
       
    81 
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CSearchrequest::NewLC
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CSearchrequest* CSearchrequest::NewLC(
       
    88     MOSSProtocolConnectionManager& aConnMan,
       
    89     TXIMPRequestId aRequestId )
       
    90     {
       
    91     LOGGER ( TXT("CSearchrequest::NewLC Start") );
       
    92     
       
    93      CSearchrequest* self = new( ELeave ) 
       
    94     			CSearchrequest( aConnMan, aRequestId );
       
    95     CleanupStack::PushL( self );
       
    96     self->ConstructL();
       
    97     
       
    98     LOGGER ( TXT("CSearchrequest::NewLC End") );
       
    99     return self;
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // CSearchrequest::~CSearchrequest
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 CSearchrequest::~CSearchrequest()
       
   107     {
       
   108     LOGGER ( TXT("::~CSearchrequest Start") );
       
   109     delete iSearchId;
       
   110     iSearchId=NULL;
       
   111     
       
   112 	CActive::Cancel();
       
   113     
       
   114     LOGGER ( TXT("::~CSearchrequest End") );
       
   115     }
       
   116 
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CSearchrequest::DoCancel()
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 void CSearchrequest::DoCancel()
       
   123     {
       
   124     LOGGER ( TXT("CSearchrequest::DoCancel Start") );
       
   125     LOGGER ( TXT("CSearchrequest::DoCancel End") );
       
   126     }
       
   127 
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // CSearchrequest::RunL()
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 void CSearchrequest::RunL()
       
   134 	{
       
   135 	LOGGER ( TXT("CSearchrequest::RunL Start") );
       
   136 	User::LeaveIfError ( iStatus.Int() );
       
   137 
       
   138 	int len = MAX_SIZE_DATA;
       
   139 	char* pResponse = NULL, *pOrigResponse = NULL;;
       
   140 	char* key = NULL; 
       
   141 
       
   142 	MXIMPObjectFactory& objFact = iConnMan.HandleToHost().ObjectFactory();
       
   143 	MSearchObjectFactory& searchObjFact = iConnMan.GetSearchHost().SearchObjectFactory();
       
   144 	MXIMPIdentity* identity = objFact.NewIdentityLC();
       
   145 	MXIMPObjectCollection* fieldlist = objFact.NewObjectCollectionLC();
       
   146 
       
   147   	TInt datacount = iConnMan.DataHandler().SearchDataCount();
       
   148  	
       
   149  	while( datacount )
       
   150 		{
       
   151 		LOGGER ( TXT("CONTACT NO : %d"), datacount );	
       
   152 		pOrigResponse =pResponse = iConnMan.DataHandler().ResponseL ( 5 );
       
   153 		CleanupStack::PushL(pOrigResponse) ;
       
   154 		MSearchInfo* searchInfo = searchObjFact.NewSearchInfoLC();
       
   155 		pResponse += sizeof ( message_hdr_resp );
       
   156 		len = strlen(pResponse) + 1;
       
   157 		while( !( *(pResponse) == '\0' && *(pResponse + 1) == '\0' ))
       
   158 			{
       
   159 			len = strlen(pResponse) + 1;
       
   160 			key = ( char* ) User::AllocLC(len);
       
   161 			strcpy(key,pResponse);
       
   162 			pResponse += len;
       
   163 			len = strlen(pResponse) + 1;
       
   164 			if( strcmp(key,FirstName) == 0)
       
   165 				{
       
   166 				LOGGER ( TXT("FIRSTNAME :") );
       
   167 			    ParseAndSetL(pResponse,len,*searchInfo, EFirstName);
       
   168 				}
       
   169 			else if( strcmp(key,LastName) == 0 )
       
   170 				{
       
   171 				LOGGER ( TXT("LASTNAME :"));
       
   172 				ParseAndSetL(pResponse,len,*searchInfo, ELastName);
       
   173 				}
       
   174 			else if( strcmp(key,Jid) == 0 )
       
   175 				{
       
   176 				LOGGER ( TXT("JID :") );
       
   177 				ParseAndSetL(pResponse,len,*searchInfo, EEmailAddress);
       
   178 				}
       
   179 			pResponse += len;
       
   180 			CleanupStack::PopAndDestroy ();// key			
       
   181 			}
       
   182 		fieldlist->AddObjectL ( searchInfo );	
       
   183 		CleanupStack::Pop();//searchInfo
       
   184 		CleanupStack::PopAndDestroy(pOrigResponse) ;//pOrigResponse	
       
   185 		datacount = iConnMan.DataHandler().SearchDataCount();
       
   186 		}
       
   187 	iConnMan.DataHandler().ResponseL ( 5 ); // this removes the operation from the array
       
   188 	identity->SetIdentityL(*iSearchId );
       
   189 	iConnMan.GetSearchHost().SearchFeatureDataAccess().HandleSearchResultsL ( identity,fieldlist );
       
   190 	iConnMan.HandleToHost().HandleRequestCompleted ( iRequestId, KErrNone );
       
   191 	CleanupStack::Pop ( 2 );//identity,fieldlist
       
   192 	delete this;
       
   193 
       
   194 	LOGGER ( TXT("CSearchrequest::RunL End") );
       
   195 	}
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // CSearchrequest::RunError
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 TInt CSearchrequest::RunError( TInt  aError )
       
   202     {
       
   203     LOGGER ( TXT("CSearchrequest::RunError : %d  Start"), aError );
       
   204     
       
   205     iConnMan.HandleToHost().HandleRequestCompleted( iRequestId, aError );
       
   206     TRAP_IGNORE(iConnMan.DataHandler().ResponseL ( 5 ));	 // this removes the operation from the array
       
   207     delete this;
       
   208 	LOGGER ( TXT("CSearchrequest::RunError End") );
       
   209     return KErrNone;
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // CSearchrequest::SearchL
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 void CSearchrequest::SearchL( const MXIMPIdentity& aSearchId,
       
   217 							  const RPointerArray< MSearchElement >& aObjs,
       
   218 							  TInt  /*aSearchLimit*/)
       
   219 	{
       
   220     LOGGER ( TXT("CSearchrequest::SearchL Start") );
       
   221     char *smsg = NULL;
       
   222 	char *tmpmsg = NULL;
       
   223 	message_hdr_req msgHdr = {0,};
       
   224 	int userLen = 0;
       
   225 	int totalLen = 0;
       
   226 	smsg = ( char* ) User::AllocLC(MAX_MSG_SIZE);
       
   227 	memset ( smsg, '\0', MAX_MSG_SIZE );
       
   228 	msgHdr.message_type = ESearch;
       
   229 	memcpy ( smsg, &msgHdr, sizeof ( message_hdr_req ) );
       
   230 	
       
   231 	HBufC* keyStr = NULL;//HBufC::NewL( 1024 );
       
   232 	
       
   233 	CSearchKeyStore& keyStore = iConnMan.GetSearchKeyStore();
       
   234 	
       
   235 	totalLen += sizeof( message_hdr_req );
       
   236 
       
   237 	TInt elementCount=aObjs.Count();	
       
   238 	
       
   239 	for (TInt i=0; i< elementCount; ++i)
       
   240 		{
       
   241 		const MSearchElement* searchElement = aObjs[i];
       
   242 		
       
   243 			switch( searchElement->GetSearchKey() )
       
   244 			{
       
   245 			case EUserFirstName:
       
   246 				{
       
   247 				keyStr = keyStore.GetLabelFromEnumL( EUserFirstName );
       
   248 				
       
   249 				break;	
       
   250 				}
       
   251 	            
       
   252 			case EUser:
       
   253 				{
       
   254 				keyStr = keyStore.GetLabelFromEnumL( EUser );
       
   255 				
       
   256 				break;	
       
   257 				}
       
   258 	            
       
   259 			case EUserLastName:
       
   260 				{
       
   261 				keyStr = keyStore.GetLabelFromEnumL( EUserLastName );
       
   262 				
       
   263 				break;	
       
   264 				}
       
   265 				
       
   266 			case EUserMiddleName:
       
   267 				{
       
   268 				keyStr = keyStore.GetLabelFromEnumL( EUserMiddleName );
       
   269 				
       
   270 				break;	
       
   271 				}
       
   272 				
       
   273 			case EUserFullName:
       
   274 				{
       
   275 				keyStr = keyStore.GetLabelFromEnumL( EUserFullName );
       
   276 				
       
   277 				break;	
       
   278 				}
       
   279 				
       
   280 			case EUserCountry:
       
   281 				{
       
   282 				keyStr = keyStore.GetLabelFromEnumL( EUserCountry );
       
   283 				
       
   284 				break;	
       
   285 				}
       
   286 				
       
   287 			case EUserCity:
       
   288 				{
       
   289 				keyStr = keyStore.GetLabelFromEnumL( EUserCity );
       
   290 				
       
   291 				break;	
       
   292 				}
       
   293 				
       
   294 			case EBirthday:
       
   295 				{
       
   296 				keyStr = keyStore.GetLabelFromEnumL( EBirthday );
       
   297 				
       
   298 				break;	
       
   299 				}
       
   300 				
       
   301 			case EState:
       
   302 				{
       
   303 				keyStr = keyStore.GetLabelFromEnumL( EState );
       
   304 				
       
   305 				break;	
       
   306 				}
       
   307 				
       
   308 			case EUserEmailAddress:
       
   309 				{
       
   310 				keyStr = keyStore.GetLabelFromEnumL( EUserEmailAddress );
       
   311 				
       
   312 				break;	
       
   313 				}
       
   314 				
       
   315 			case EUserNickName:
       
   316 				{
       
   317 				keyStr = keyStore.GetLabelFromEnumL( EUserNickName );
       
   318 				
       
   319 				break;	
       
   320 				}
       
   321 				
       
   322 			case EOrganisation:
       
   323 				{
       
   324 				keyStr = keyStore.GetLabelFromEnumL( EOrganisation );
       
   325 				
       
   326 				break;	
       
   327 				}
       
   328 				
       
   329 			case EOrganisationUnit:
       
   330 				{
       
   331 				keyStr = keyStore.GetLabelFromEnumL( EOrganisationUnit );
       
   332 				
       
   333 				break;	
       
   334 				}
       
   335 	            
       
   336 			case  EReadLabel: 
       
   337 				{
       
   338 				//If key is not supported(case of not localized string)
       
   339 				keyStr = searchElement->GetSearchLabel().Identity().AllocL();
       
   340 				
       
   341 				break;	
       
   342 				}
       
   343 			default:
       
   344 				{
       
   345 				//If any other case just continue with next one
       
   346 				continue;					
       
   347 				}
       
   348 			}	
       
   349 		
       
   350 		if ( NULL == keyStr )
       
   351 			{
       
   352 			continue;	
       
   353 			}
       
   354 		//Add key
       
   355 		CleanupStack::PushL( keyStr );
       
   356 		
       
   357 		charFormatData tmpmsgData = OssProtocolAdapUtils::ConvertTUint16ToCharLC( *keyStr );
       
   358 	    tmpmsg = tmpmsgData.data;
       
   359 	    userLen = tmpmsgData.dataSize;
       
   360 			 
       
   361 		 if ( totalLen + userLen > MAX_MSG_SIZE ) 
       
   362 		    	{
       
   363 				User::Leave(KErrArgument);		
       
   364 		    	}
       
   365 
       
   366 		memcpy ( smsg + totalLen, tmpmsg, userLen );
       
   367 
       
   368     	totalLen += userLen;
       
   369 		
       
   370 		//Add value
       
   371 		
       
   372 			
       
   373 		
       
   374 		charFormatData tmpmsgDataValue = OssProtocolAdapUtils::ConvertTUint16ToCharLC( 
       
   375 					searchElement->GetSearchId().Identity() );
       
   376 					
       
   377 	    tmpmsg = tmpmsgDataValue.data;
       
   378 	    userLen = tmpmsgDataValue.dataSize;
       
   379 			 
       
   380 		 if ( totalLen + userLen > MAX_MSG_SIZE ) 
       
   381 		    	{
       
   382 				User::Leave(KErrArgument);		
       
   383 		    	}
       
   384 
       
   385 		memcpy ( smsg + totalLen, tmpmsg, userLen );
       
   386 
       
   387     	totalLen += userLen;
       
   388     	
       
   389     	CleanupStack::PopAndDestroy ( 3 );//For keyStr, tmpmsgData, tmpmsgDataValue
       
   390     	
       
   391 		} // end  : for loop
       
   392 	iSendId = iConnMan.DataHandler().SendDataL ( iStatus, smsg, totalLen++ );
       
   393 
       
   394 	delete iSearchId;	
       
   395 	iSearchId=NULL;
       
   396     iSearchId=aSearchId.Identity().AllocL();
       
   397 	// signal the scheduler
       
   398 	SetActive();
       
   399 	CleanupStack::PopAndDestroy ( 1 );//smsg
       
   400 	LOGGER ( TXT("CSearchrequest::SearchL End") );
       
   401 		
       
   402 	}
       
   403 	
       
   404 // ---------------------------------------------------------------------------
       
   405 // CSearchrequest::SearchUtil
       
   406 // ---------------------------------------------------------------------------
       
   407 //
       
   408 void CSearchrequest::ParseAndSetL(char* aResponse, TInt /*alen*/, MSearchInfo& aSearchInfo, TInfoType aKeytype) 
       
   409 	{
       
   410     char* keyvalue =  aResponse;
       
   411 	HBufC16* key_value = OssProtocolAdapUtils::ConvertCharToTUint16LC( keyvalue );
       
   412 	aSearchInfo.SetFieldInfoL(*key_value,aKeytype);
       
   413 	CleanupStack::PopAndDestroy( key_value );
       
   414 	}
       
   415 	
       
   416 // End of file
       
   417