emailuis/emailui/src/FreestyleEmailUiContactHandler.cpp
changeset 0 8466d47a6819
child 2 5253a20d2a1e
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     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:  Contanct handler implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include <AknQueryDialog.h>
       
    21 #include <aknlists.h>
       
    22 #include <StringLoader.h>
       
    23 
       
    24 
       
    25 //Virtual Phonebook 
       
    26 #include <CVPbkContactManager.h>
       
    27 #include <CVPbkContactStoreUriArray.h>
       
    28 #include <VPbkContactStoreUris.h>       // VPbkContactStoreUris
       
    29 #include <TVPbkContactStoreUriPtr.h>    // TVPbkContactStoreUriPtr
       
    30 #include <MVPbkContactOperationBase.h>
       
    31 #include <MVPbkStoreContact.h>
       
    32 #include <MVPbkFieldType.h>
       
    33 #include <VPbkEng.rsg>
       
    34 #include <CVPbkContactFieldIterator.h>
       
    35 #include <MVPbkContactFieldTextData.h>
       
    36 #include <MVPbkContactStoreList.h>
       
    37 
       
    38 
       
    39 // Phonebook 2
       
    40 #include <CPbk2SortOrderManager.h>
       
    41 #include <MPbk2ContactNameFormatter.h>
       
    42 #include <Pbk2ContactNameFormatterFactory.h>        
       
    43 #include <Pbk2ViewId.hrh>
       
    44 #include <CPbk2ViewState.h>
       
    45 
       
    46 // Send UI
       
    47 #include <sendui.h>
       
    48 #include <SendUiConsts.h>
       
    49 #include <CMessageData.h>
       
    50 
       
    51 #include <aiwdialdataext.h>
       
    52 #include <MVPbkContactLink.h>
       
    53 
       
    54 //Remote Contact Lookup
       
    55 //<cmail>
       
    56 #include "cpbkxremotecontactlookupenv.h"
       
    57 #include "tpbkxremotecontactlookupprotocolaccountid.h"
       
    58 #include "cpbkxremotecontactlookupserviceuicontext.h"
       
    59 #include <cntitem.h>
       
    60 
       
    61 // <cmail> SF path
       
    62 #include <MPbkGlobalSetting.h> // Global setting data 
       
    63 #include <PbkGlobalSettingFactory.h>
       
    64 // </cmail>
       
    65 
       
    66 // Aiw launcher
       
    67 #include <AiwDialDataTypes.h>
       
    68 #include <AiwContactAssignDataTypes.h>
       
    69 #include <AiwContactSelectionDataTypes.h>
       
    70 #include <AiwServiceHandler.h>
       
    71 
       
    72 // FS Email framework
       
    73 #include "CFSMailBox.h"
       
    74 //</cmail>
       
    75 
       
    76 #include <FreestyleEmailUi.rsg>
       
    77 
       
    78 #include "FreestyleEmailUiContactHandler.h"
       
    79 #include "FreestyleEmailUiCLSListsHandler.h"
       
    80 #include "FreestyleEmailUiUtilities.h"
       
    81 #include "FreestyleEmailUiLiterals.h"
       
    82 #include "FreestyleEmailUiContactHandlerObserver.h"
       
    83 #include "FreestyleEmailUiCLSItem.h"
       
    84 
       
    85 //Constants
       
    86 const TInt KInternetCallPreferred = 1;
       
    87 
       
    88 
       
    89 CFSEmailUiContactHandler* CFSEmailUiContactHandler::NewL( RFs& aSession )
       
    90     {
       
    91     FUNC_LOG;
       
    92     CFSEmailUiContactHandler* self = CFSEmailUiContactHandler::NewLC( aSession );
       
    93     CleanupStack::Pop(self);
       
    94     return self;
       
    95     }
       
    96 
       
    97 CFSEmailUiContactHandler* CFSEmailUiContactHandler::NewLC( RFs& aSession )
       
    98     {
       
    99     FUNC_LOG;
       
   100     CFSEmailUiContactHandler* self = new (ELeave) CFSEmailUiContactHandler( aSession );
       
   101     CleanupStack::PushL(self);
       
   102     self->ConstructL( aSession );
       
   103     return self;
       
   104     }
       
   105 
       
   106 CFSEmailUiContactHandler::~CFSEmailUiContactHandler()
       
   107     {
       
   108     FUNC_LOG;
       
   109 	delete iContactForMsgCreation;
       
   110 	if (iPersistentSetting)
       
   111         {
       
   112         iPersistentSetting->Close();
       
   113         delete iPersistentSetting;
       
   114         }          
       
   115     delete iClsListHandler;
       
   116     delete iContactManager;
       
   117     delete iServiceHandler;
       
   118     
       
   119     ResetFieldIds();
       
   120      
       
   121     delete iPreviousEmailAddress;
       
   122     delete iSearchMatch;
       
   123    
       
   124     iContactObjects.ResetAndDestroy();
       
   125 
       
   126     delete iLinksSet;
       
   127     delete iLinkOperation;
       
   128     delete iLinkOperationFetch;
       
   129     delete iCurrentLink;
       
   130     delete iLastClsItem;
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CFSEmailUiContactHandler::AddToContactL
       
   135 // -----------------------------------------------------------------------------
       
   136 void CFSEmailUiContactHandler::AddToContactL( const TDesC& aDes, 
       
   137                                                     TContactUpdateType aUpdateType,
       
   138                                                     TAddToContactsType aAddToContactsType,
       
   139                                                     MFSEmailUiContactHandlerObserver* aObserver )
       
   140     {
       
   141     FUNC_LOG;
       
   142     if ( iState == EContactHandlerIdle ) //Check that there are no other request on going
       
   143     	{
       
   144     	iState = EContactHandlerAddToContacts;
       
   145     	iHandlerObserver = aObserver; //Can be null
       
   146     	TUint assignFlags = 0;
       
   147     	if ( aAddToContactsType == EContactHandlerCreateNewContact )
       
   148     		{
       
   149     		// In case of EUpdateExisting, we don't have to do anything as default is updateexisting
       
   150     		assignFlags = AiwContactAssign::ECreateNewContact;
       
   151     		}
       
   152 
       
   153     	CAiwGenericParamList& inParamList =
       
   154     	iServiceHandler->InParamListL();
       
   155 
       
   156     	inParamList.AppendL(
       
   157     			TAiwGenericParam(
       
   158     					EGenericParamContactAssignData,
       
   159     					TAiwVariant(AiwContactAssign::TAiwSingleContactAssignDataV1Pckg(
       
   160     							AiwContactAssign::TAiwSingleContactAssignDataV1().SetFlags(assignFlags)))));
       
   161     	
       
   162     	TGenericParamIdValue updateType;
       
   163     	if ( aUpdateType == EContactUpdateEmail )
       
   164     		{
       
   165     		updateType = EGenericParamEmailAddress;
       
   166     		}
       
   167     	else // EContactUpdateNumber
       
   168     		{
       
   169     		updateType = EGenericParamPhoneNumber;
       
   170     		}
       
   171     	
       
   172     	inParamList.AppendL(
       
   173     			TAiwGenericParam(
       
   174     					updateType,
       
   175     					TAiwVariant(aDes)
       
   176     			));
       
   177 
       
   178     	//Async call, CallBack is HandleNotify
       
   179     	iServiceHandler->ExecuteServiceCmdL(
       
   180     			KAiwCmdAssign,
       
   181     			inParamList,
       
   182     			iServiceHandler->OutParamListL(),
       
   183     			0,
       
   184     			this);
       
   185     	}
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CFSEmailUiContactHandler::AddToContactsQueryL
       
   190 // -----------------------------------------------------------------------------
       
   191 TBool CFSEmailUiContactHandler::AddtoContactsQueryL( TAddToContactsType& aType )
       
   192 	{
       
   193     FUNC_LOG;
       
   194 	// create popup list dialog asking new/update
       
   195 	// Costruct listbox and popup list
       
   196 	CEikTextListBox* listbox = new ( ELeave ) CAknSinglePopupMenuStyleListBox();
       
   197 	CleanupStack::PushL( listbox );
       
   198 
       
   199 	CAknPopupList* popupList = CAknPopupList::NewL( listbox, 
       
   200 			R_AVKON_SOFTKEYS_SELECT_BACK );
       
   201 
       
   202 	CleanupStack::PushL( popupList );
       
   203 	listbox->ConstructL( popupList, CEikListBox::ELeftDownInViewRect );
       
   204 
       
   205 	listbox->CreateScrollBarFrameL(ETrue);
       
   206 	listbox->ScrollBarFrame()->SetScrollBarVisibilityL(
       
   207 			CEikScrollBarFrame::EOff,
       
   208 			CEikScrollBarFrame::EAuto );
       
   209 
       
   210     // Get list item array and put all removable theme names in it.
       
   211     MDesCArray* itemList = listbox->Model()->ItemTextArray();
       
   212     CDesCArray* listitems = static_cast< CDesCArray* >( itemList );
       
   213 
       
   214     // Load title for popup list.
       
   215     HBufC* resource = StringLoader::LoadLC( R_FREESTYLE_EMAIL_UI_VIEWER_ADD_TO_CONTACTS );
       
   216     popupList->SetTitleL( *resource );
       
   217     CleanupStack::PopAndDestroy( resource );            
       
   218 
       
   219 	// Load and append actual selection lines
       
   220     resource = StringLoader::LoadLC( R_FREESTYLE_EMAIL_UI_VIEWER_CREATE_NEW_CONTACT );
       
   221     listitems->AppendL( *resource );
       
   222     CleanupStack::PopAndDestroy( resource );            
       
   223 
       
   224     resource = StringLoader::LoadLC( R_FREESTYLE_EMAIL_UI_VIEWER_UPDATE_EXISTING );
       
   225     listitems->AppendL( *resource );
       
   226     CleanupStack::PopAndDestroy( resource );            
       
   227     
       
   228     TBool returnValue = EFalse;
       
   229     // Execute popup list.
       
   230     if ( popupList->ExecuteLD() )
       
   231     	{
       
   232     	if ( !listbox->CurrentItemIndex() )
       
   233     		{
       
   234     		aType = EContactHandlerCreateNewContact;
       
   235     		}
       
   236     	else
       
   237     		{
       
   238     		//UpdateExisting is "default" so no flags are needed
       
   239     		aType = EContactHandlerUpdateExisting;
       
   240     		}
       
   241     	returnValue = ETrue; //User pressed OK
       
   242     	}
       
   243     CleanupStack::Pop( popupList ); 
       
   244 	CleanupStack::PopAndDestroy( listbox );	
       
   245 
       
   246 	return returnValue;
       
   247 	}
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // CFSEmailUiContactHandler::FindAndCallToContactByEmailL
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 void CFSEmailUiContactHandler::FindAndCallToContactByEmailL(
       
   254     const TDesC& aEmailAddress, CFSMailBox* aMailBox,
       
   255     MFSEmailUiContactHandlerObserver* aObserver, TBool aUseSenderText )
       
   256     {
       
   257     FUNC_LOG;
       
   258     if ( iOpenComplete && (iState == EContactHandlerIdle) ) //If contact stores all ready
       
   259         {
       
   260         
       
   261         iState = EContactHandlerCallToContactByEmail;
       
   262         iHandlerObserver = aObserver;
       
   263         iUseSenderText = aUseSenderText;
       
   264         iMailBox = aMailBox;
       
   265        
       
   266         FindContactLinkL( aEmailAddress );
       
   267           
       
   268         }
       
   269     }
       
   270 
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CFSEmailUiContactHandler::FindAndCreateMsgToContactByEmailL
       
   274 // -----------------------------------------------------------------------------
       
   275 void CFSEmailUiContactHandler::FindAndCreateMsgToContactByEmailL( const TDesC& aEmailAddress,
       
   276 															 	  CFSMailBox* aMailBox )
       
   277     {
       
   278     FUNC_LOG;
       
   279     if ( iOpenComplete && (iState == EContactHandlerIdle) ) //If contact stores all ready
       
   280     	{
       
   281     	
       
   282     	iState = EContactHandlerCreateMessage;
       
   283      	iMailBox = aMailBox;     	
       
   284 
       
   285      	FindContactLinkL( aEmailAddress );
       
   286     	
       
   287     	}
       
   288     }
       
   289 
       
   290 
       
   291 	
       
   292 // ---------------------------------------------------------------------------
       
   293 // Searches for the contact if necessary and shows the contact details.
       
   294 // ---------------------------------------------------------------------------
       
   295 //
       
   296 void CFSEmailUiContactHandler::ShowContactDetailsL( const TDesC& aAddress, 
       
   297     TContactUpdateType /*aType*/, MFSEmailUiContactHandlerObserver* aObserver )
       
   298     {
       
   299     FUNC_LOG;
       
   300     iHandlerObserver = aObserver;
       
   301     iState = EContactHandlerShowContactDetails;
       
   302 
       
   303     FindContactLinkL( aAddress );
       
   304     
       
   305     }
       
   306 
       
   307 
       
   308 void CFSEmailUiContactHandler::MakeAiwCallL(MVPbkContactLink* aContactLink,
       
   309 											const TDesC& aContactNumber,
       
   310 											TBool aVoipOverride )
       
   311     {
       
   312     FUNC_LOG;
       
   313     CAiwDialDataExt* dialData = CAiwDialDataExt::NewLC();
       
   314     dialData->SetWindowGroup( CCoeEnv::Static()->RootWin().Identifier() );
       
   315    
       
   316     HBufC8* linkBuffer(NULL);
       
   317     
       
   318     if( aContactLink )
       
   319     	{
       
   320     	linkBuffer = aContactLink->PackLC();
       
   321     	dialData->SetContactLinkL( *linkBuffer );
       
   322    		// Contact link exists, check if voip is preferred
       
   323 	   	// Create iPersistentSetting here only when needed for the first time
       
   324 		if ( !iPersistentSetting )
       
   325 			{
       
   326 		    iPersistentSetting = 
       
   327 		        PbkGlobalSettingFactory::CreatePersistentSettingL();
       
   328 		    iPersistentSetting->
       
   329 		        ConnectL(MPbkGlobalSetting::ERichCallSettingsCategory);		
       
   330 			} 
       
   331 		if ( aVoipOverride || SelectBetweenCsAndVoip() == EAiwVoIPCall )
       
   332 			{
       
   333 		    dialData->SetCallType( CAiwDialData::EAIWVoiP );      	  	
       
   334 			}
       
   335         // <cmail> video call
       
   336         else if ( iVideoCall )
       
   337             {
       
   338             iVideoCall = EFalse;
       
   339             dialData->SetCallType( CAiwDialData::EAIWForcedVideo );
       
   340             }
       
   341         // </cmail>
       
   342 		else
       
   343 			{
       
   344 		    dialData->SetCallType( CAiwDialData::EAIWVoice );      	  	
       
   345 			} 							
       
   346     	}
       
   347     else
       
   348     	{
       
   349 		if ( aVoipOverride )
       
   350 			{
       
   351 		    dialData->SetCallType( CAiwDialData::EAIWVoiP );      	  	
       
   352 			}
       
   353         // <cmail> video call
       
   354         else if ( iVideoCall ) 
       
   355             {
       
   356             iVideoCall = EFalse;
       
   357             dialData->SetCallType( CAiwDialData::EAIWForcedVideo );
       
   358             }
       
   359         // </cmail>
       
   360 		else
       
   361 			{
       
   362 		    dialData->SetCallType( CAiwDialData::EAIWVoice );      	  	
       
   363 			}   
       
   364     	dialData->SetPhoneNumberL( aContactNumber );
       
   365     	}
       
   366     
       
   367     CAiwGenericParamList& inParamList = iServiceHandler->InParamListL();
       
   368     dialData->FillInParamListL( inParamList );    
       
   369     if( aContactLink )
       
   370     	{
       
   371     	CleanupStack::PopAndDestroy( linkBuffer );
       
   372     	}
       
   373     
       
   374     CleanupStack::PopAndDestroy( dialData );
       
   375     
       
   376     iServiceHandler->ExecuteServiceCmdL(
       
   377         KAiwCmdCall,
       
   378         inParamList,
       
   379         iServiceHandler->OutParamListL(),
       
   380         0,
       
   381         NULL );
       
   382     if ( iHandlerObserver )
       
   383     	{
       
   384     	RPointerArray<CFSEmailUiClsItem> emptyArray;
       
   385     	iHandlerObserver->OperationCompleteL( EFindAndCallToContactByEmailL, emptyArray );
       
   386     	emptyArray.Close();
       
   387     	iHandlerObserver = NULL;
       
   388     	}
       
   389     iState = EContactHandlerIdle;
       
   390     } 
       
   391 
       
   392 
       
   393 TInt CFSEmailUiContactHandler::SelectBetweenCsAndVoip() const
       
   394     {
       
   395     FUNC_LOG;
       
   396     // Default to voice call
       
   397     TInt ret = EAiwVoice;
       
   398     
       
   399 	TInt preferred = KErrNotFound;
       
   400     if ( iPersistentSetting )
       
   401     	{
       
   402 	    iPersistentSetting->Get
       
   403     	    (MPbkGlobalSetting::EPreferredTelephony, preferred);	
       
   404     	}
       
   405    
       
   406     // VoIP type is preferred only if the setting is,
       
   407     // KInternetCallPreferred. In other cases, like when the setting
       
   408     // is not found, voice call is preferred.
       
   409     switch (preferred)
       
   410         {
       
   411         case KInternetCallPreferred:
       
   412             {
       
   413             ret = EAiwVoIPCall;
       
   414             break;
       
   415             }
       
   416         default:
       
   417             {
       
   418             break;
       
   419             }
       
   420         }
       
   421     return ret;
       
   422     }
       
   423 
       
   424 // ---------------------------------------------------------------------------
       
   425 // Searches contacts matching the given address
       
   426 // ---------------------------------------------------------------------------
       
   427 //
       
   428 void CFSEmailUiContactHandler::SearchMatchesL( const TDesC& aText, 
       
   429     MFSEmailUiContactHandlerObserver* aObserver, CFSMailBox* aMailBox )
       
   430     {
       
   431     FUNC_LOG;
       
   432     if ( (iState == EContactHandlerIdle) ||
       
   433     	 (iState == EContactHandlerSearchMatches)  )
       
   434     	{
       
   435     	iState = EContactHandlerSearchMatches;
       
   436     	iHandlerObserver = aObserver;
       
   437     	
       
   438     	iClsListHandler->SetCurrentMailboxL( aMailBox );
       
   439     	
       
   440     	//Async call, CallBack is ArrayUpdatedL (when error: OperationErrorL)
       
   441     	iClsListHandler->SearchMatchesL( aText );
       
   442     	}
       
   443     }
       
   444 
       
   445 TBool CFSEmailUiContactHandler::IsLanguageSupportedL()
       
   446     {
       
   447     FUNC_LOG;
       
   448     return iClsListHandler->IsLanguageSupportedL();
       
   449     }
       
   450 
       
   451 CVPbkContactManager* CFSEmailUiContactHandler::GetContactManager()
       
   452 	{
       
   453     FUNC_LOG;
       
   454 	return iContactManager;
       
   455 	}
       
   456 
       
   457 
       
   458 void CFSEmailUiContactHandler::GetAddressesFromPhonebookL(
       
   459     MFSEmailUiContactHandlerObserver* aObserver )
       
   460 	{
       
   461     FUNC_LOG;
       
   462 	if ( iState == EContactHandlerIdle )
       
   463 		{
       
   464 		if ( !aObserver )
       
   465 			{
       
   466 			//Request can't be fulfilled without observer
       
   467 			return;
       
   468 			}
       
   469 		iHandlerObserver = aObserver;
       
   470 		iState = EContactHandlerGetAddressesFromPhonebook;
       
   471 		TUint fetchFlags = 0;
       
   472 
       
   473 		iContactObjects.ResetAndDestroy();
       
   474 
       
   475 		CAiwGenericParamList& inParamList = iServiceHandler->InParamListL();
       
   476 		inParamList.AppendL(
       
   477 				TAiwGenericParam(
       
   478 						EGenericParamContactSelectionData,
       
   479 						TAiwVariant(TAiwMultipleItemSelectionDataV1Pckg(
       
   480 								TAiwMultipleItemSelectionDataV1().
       
   481 								SetAddressSelectType(EAiwEMailSelect).
       
   482 								SetFlags( fetchFlags )))));
       
   483 
       
   484 
       
   485 		//Async call, CallBack is HandleNotify
       
   486 		iServiceHandler->ExecuteServiceCmdL(
       
   487 				KAiwCmdSelect,
       
   488 				inParamList,
       
   489 				iServiceHandler->OutParamListL(),
       
   490 				0,
       
   491 				this);	
       
   492 		}
       
   493 	}
       
   494 
       
   495 // <cmail> video call
       
   496 // ---------------------------------------------------------------------------
       
   497 // Enables/disables video call.
       
   498 // ---------------------------------------------------------------------------
       
   499 //
       
   500 void CFSEmailUiContactHandler::SetVideoCall( TBool aState )
       
   501     {
       
   502     iVideoCall = aState;
       
   503     }
       
   504 // </cmail>
       
   505 
       
   506 /////////////////////////////////////////////////////////////////////////////
       
   507 // CFSEmailUiContactHandler::GetSmsAddressFromPhonebookAndSendL
       
   508 //
       
   509 /////////////////////////////////////////////////////////////////////////////
       
   510 void CFSEmailUiContactHandler::GetSmsAddressFromPhonebookAndSendL( MVPbkContactLink* aContactLink )
       
   511 	{
       
   512     FUNC_LOG;
       
   513 	if ( iState == EContactHandlerIdle )
       
   514 		{
       
   515 		iState = EContactHandlerGetSmsAddressFromPhonebook;
       
   516 
       
   517  		CAiwGenericParamList& inParamList = iServiceHandler->InParamListL();
       
   518  		// Set data type select
       
   519  	    TAiwSingleItemSelectionDataV3 dataSelect;
       
   520         dataSelect.SetAddressSelectType( EAiwPhoneNumberSelect );
       
   521         TAiwSingleItemSelectionDataV3Pckg dataPckg( dataSelect );
       
   522         inParamList.AppendL( TAiwGenericParam( EGenericParamContactSelectionData, 
       
   523                            TAiwVariant( dataPckg )));
       
   524  	 		
       
   525  		// Append contact links 		
       
   526        	CVPbkContactLinkArray* links = CVPbkContactLinkArray::NewLC();
       
   527         links->AppendL( aContactLink ); // Clone ownership is transferred    
       
   528         HBufC8* packedLinks = links->PackLC();          
       
   529         inParamList.AppendL( TAiwGenericParam( EGenericParamContactLinkArray,
       
   530                           TAiwVariant(*packedLinks)));
       
   531         CleanupStack::PopAndDestroy( packedLinks ); 
       
   532         CleanupStack::PopAndDestroy( links ); 
       
   533 
       
   534         //Async call, CallBack is HandleNotify
       
   535 		iServiceHandler->ExecuteServiceCmdL(
       
   536 				KAiwCmdSelect,
       
   537 				inParamList,
       
   538 				iServiceHandler->OutParamListL(),
       
   539 				0,
       
   540 				this);	
       
   541 		}
       
   542 	}
       
   543 
       
   544 /////////////////////////////////////////////////////////////////////////////
       
   545 // CFSEmailUiContactHandler::GetMmsAddressFromPhonebookAndSendL
       
   546 //
       
   547 /////////////////////////////////////////////////////////////////////////////
       
   548 void CFSEmailUiContactHandler::GetMmsAddressFromPhonebookAndSendL( MVPbkContactLink* aContactLink,
       
   549 														    TBool aIsVoiceMessage )
       
   550 	{
       
   551     FUNC_LOG;
       
   552 	if ( iState == EContactHandlerIdle )
       
   553 		{
       
   554 		if ( aIsVoiceMessage )
       
   555 			{
       
   556 			iState = EContactHandlerGetVoiceMsgAddressFromPhonebook;		
       
   557 			}
       
   558 		else
       
   559 			{
       
   560 			iState = EContactHandlerGetMmsAddressFromPhonebook;			
       
   561 			}
       
   562 
       
   563  		CAiwGenericParamList& inParamList = iServiceHandler->InParamListL();
       
   564  		// Set data type select
       
   565  	    TAiwSingleItemSelectionDataV3 dataSelect;
       
   566         dataSelect.SetAddressSelectType( EAiwMMSSelect );
       
   567         TAiwSingleItemSelectionDataV3Pckg dataPckg( dataSelect );
       
   568         inParamList.AppendL( TAiwGenericParam( EGenericParamContactSelectionData, 
       
   569                            TAiwVariant( dataPckg )));
       
   570  	 		
       
   571  		// Append contact links 				
       
   572        	CVPbkContactLinkArray* links = CVPbkContactLinkArray::NewLC();
       
   573         links->AppendL( aContactLink ); // Clone ownership is transferred
       
   574 	    HBufC8* packedLinks = links->PackLC();          
       
   575         inParamList.AppendL( TAiwGenericParam( EGenericParamContactLinkArray,
       
   576                           TAiwVariant(*packedLinks)));
       
   577         CleanupStack::PopAndDestroy( packedLinks ); 
       
   578         CleanupStack::PopAndDestroy( links );
       
   579 
       
   580         //Async call, CallBack is HandleNotify
       
   581 		iServiceHandler->ExecuteServiceCmdL(
       
   582 				KAiwCmdSelect,
       
   583 				inParamList,
       
   584 				iServiceHandler->OutParamListL(),
       
   585 				0,
       
   586 				this);	
       
   587 		}
       
   588 	}
       
   589 
       
   590 /////////////////////////////////////////////////////////////////////////////
       
   591 // CFSEmailUiContactHandler::ArrayUpdatedL
       
   592 //
       
   593 /////////////////////////////////////////////////////////////////////////////
       
   594 void CFSEmailUiContactHandler::ArrayUpdatedL(
       
   595     const RPointerArray<CFSEmailUiClsItem>& aMatchingItems )
       
   596     {
       
   597     FUNC_LOG;
       
   598 
       
   599     switch ( iState )
       
   600         {
       
   601 		case EContactHandlerCreateMessage:
       
   602 			CreateMessageL( aMatchingItems );	
       
   603 			break;
       
   604 		case EContactHandlerCallToContactByEmail:
       
   605 			if ( aMatchingItems.Count() )
       
   606 				{
       
   607 				delete iLastClsItem;
       
   608 				iLastClsItem = NULL;
       
   609 				iLastClsItem = aMatchingItems[0]->CloneLC();
       
   610 				CleanupStack::Pop();
       
   611 				}
       
   612 			HandleCallL( aMatchingItems );
       
   613 			break;
       
   614 		case EContactHandlerShowContactDetails:
       
   615 			ShowDetailsL( aMatchingItems );
       
   616 			break;
       
   617 		case EContactHandlerSearchMatches: 
       
   618 			if ( aMatchingItems.Count() == 1 )
       
   619 				{
       
   620 				if ( !iSearchMatch )
       
   621 					{
       
   622 					iSearchMatch = CFSEmailUiClsItem::NewL();			
       
   623 					}
       
   624 				iSearchMatch->SetDisplayNameL( aMatchingItems[0]->DisplayName() );
       
   625 				iSearchMatch->SetEmailAddressL( aMatchingItems[0]->EmailAddress() );
       
   626 				}
       
   627 			else
       
   628 			    {
       
   629 			    delete iSearchMatch;
       
   630 			    iSearchMatch = NULL;
       
   631 			    }
       
   632 	
       
   633 			// The ownership of the array content is transferred to observer.
       
   634 			iState = EContactHandlerIdle;
       
   635 			if ( iHandlerObserver )
       
   636 				{
       
   637 				iHandlerObserver->OperationCompleteL( ESearchContacts, aMatchingItems );
       
   638 				}
       
   639 			break;
       
   640 		default:
       
   641 			break;
       
   642         }
       
   643 
       
   644     }
       
   645 
       
   646 void CFSEmailUiContactHandler::OperationErrorL( TInt aErrorCode )
       
   647 	{
       
   648     FUNC_LOG;
       
   649 	if ( iHandlerObserver )
       
   650 		{
       
   651         // KerrNotReady --> caching in progress, KErrNotFound --> caching not started yet
       
   652         if ( (aErrorCode == KErrNotReady) || (aErrorCode == KErrNotFound) )
       
   653             {
       
   654             iCachingInProgressError = ETrue;
       
   655             iState = EContactHandlerIdle;
       
   656             iHandlerObserver->OperationErrorL( ESearchContacts, aErrorCode );
       
   657             }
       
   658         else if ( aErrorCode == KErrNone )
       
   659             {
       
   660             // handle only if caching was in progress
       
   661             if ( iCachingInProgressError )
       
   662                 {
       
   663                 iCachingInProgressError = EFalse;
       
   664                 iState = EContactHandlerIdle;
       
   665 		iHandlerObserver->OperationErrorL( ESearchContacts, aErrorCode );
       
   666 		}
       
   667 	}
       
   668         else
       
   669             {
       
   670             iState = EContactHandlerIdle;
       
   671             iHandlerObserver->OperationErrorL( ESearchContacts, aErrorCode );
       
   672             }
       
   673         }
       
   674     else
       
   675         {
       
   676         iState = EContactHandlerIdle;
       
   677         }
       
   678     }
       
   679 
       
   680 
       
   681 void CFSEmailUiContactHandler::VPbkSingleContactOperationComplete(
       
   682 	    MVPbkContactOperationBase& aOperation,
       
   683 	    MVPbkStoreContact* aContact )
       
   684 	{
       
   685     FUNC_LOG;
       
   686 	TRAP_IGNORE( VPbkSingleContactOperationCompleteL( aOperation, aContact) );
       
   687 	}
       
   688 
       
   689 void CFSEmailUiContactHandler::VPbkSingleContactOperationCompleteL(
       
   690 		    MVPbkContactOperationBase& aOperation,
       
   691 		    MVPbkStoreContact* aContact )
       
   692 	{
       
   693     FUNC_LOG;
       
   694 	
       
   695 	if ( (iLinkOperationFetch == &aOperation) && iState == EContactHandlerGetAddressesFromPhonebook )
       
   696 		{
       
   697 		CleanupDeletePushL( aContact );
       
   698 		
       
   699 		RPointerArray<HBufC> firstname;
       
   700 		CleanupResetAndDestroyClosePushL( firstname );
       
   701 		RPointerArray<HBufC> lastname;
       
   702 		CleanupResetAndDestroyClosePushL( lastname );
       
   703 		RPointerArray<HBufC> emailAddresses;			
       
   704 		CleanupResetAndDestroyClosePushL( emailAddresses );
       
   705 		GetContactFieldsL( iFirstnameFields, firstname, aContact );
       
   706 		GetContactFieldsL( iLastnameFields, lastname, aContact );
       
   707 	
       
   708 		// Create display name, this will be used in UI.
       
   709 		TInt dispNameLength = 1;
       
   710 		if ( firstname.Count() ) dispNameLength += firstname[0]->Length();
       
   711 		if ( lastname.Count() ) dispNameLength += lastname[0]->Length();
       
   712 		HBufC* displayname = HBufC::NewLC( dispNameLength );
       
   713 		
       
   714 		if ( firstname.Count() )
       
   715 			{
       
   716 			displayname->Des().Copy( *firstname[0] );
       
   717 			displayname->Des().Append( KSpace );
       
   718 			}
       
   719 		if ( lastname.Count() )
       
   720 			{
       
   721 			displayname->Des().Append( *lastname[0] );
       
   722 			}
       
   723 		
       
   724 		// retrieve selected email address
       
   725 		MVPbkStoreContactFieldCollection& fields = aContact->Fields();
       
   726 		MVPbkBaseContactField* selectedField = fields.RetrieveField( *iCurrentLink );
       
   727 		HBufC* selectedEmailAddress = MVPbkContactFieldTextData::Cast( 
       
   728 						selectedField->FieldData() ).Text().AllocLC();
       
   729 
       
   730         //check number of email addreses
       
   731         GetContactFieldsL( iEmailFields, emailAddresses, aContact );
       
   732 		// create contact object
       
   733 		CFSEmailUiClsItem* item = CFSEmailUiClsItem::NewLC();
       
   734         
       
   735         if ( emailAddresses.Count() > 1 )
       
   736             {
       
   737             item->SetMultipleEmails( ETrue );
       
   738             }
       
   739 
       
   740 		item->SetDisplayNameL( displayname->Des() );
       
   741 		item->SetEmailAddressL( *selectedEmailAddress );
       
   742 		CleanupStack::Pop( item );
       
   743 		iContactObjects.Append( item );
       
   744 
       
   745 		CleanupStack::PopAndDestroy( selectedEmailAddress );
       
   746 		selectedEmailAddress = NULL;
       
   747 
       
   748 		
       
   749 		CleanupStack::PopAndDestroy( displayname );
       
   750 		CleanupStack::PopAndDestroy( &emailAddresses );
       
   751 		CleanupStack::PopAndDestroy( &lastname );
       
   752 		CleanupStack::PopAndDestroy( &firstname );
       
   753 		CleanupStack::PopAndDestroy( aContact );
       
   754 		
       
   755 		// Get index of Next ContactLink if there's no LinkSet
       
   756 		// or iCurrenLink index is set to 0
       
   757 		TInt index = (iLinksSet && iCurrentLink ? iLinksSet->Find
       
   758 				(*iCurrentLink) + 1 : 0);
       
   759 		
       
   760 		if (iLinksSet && index < iLinksSet->Count())
       
   761 		    {
       
   762 		    iCurrentLink = &iLinksSet->At(index);
       
   763 		    delete iLinkOperationFetch;
       
   764 		    iLinkOperationFetch = NULL;
       
   765 
       
   766 		    //Async operation, callback VPbkSingleContactOperationCompleteL
       
   767 		    //Error situations: VPbkSingleContactOperationFailed
       
   768 		    iLinkOperationFetch = iContactManager->RetrieveContactL(
       
   769 		        *iCurrentLink, *this);
       
   770 		    }
       
   771 		else 
       
   772 		    {
       
   773 		    delete iLinkOperationFetch; 
       
   774 		    iLinkOperationFetch = NULL;
       
   775 
       
   776 		    iCurrentLink = NULL;
       
   777 		    iState = EContactHandlerIdle;
       
   778 		    delete iLinksSet;
       
   779 		    iLinksSet = NULL;
       
   780 
       
   781 		    if ( iHandlerObserver )
       
   782 		    	{
       
   783 		    	iHandlerObserver->OperationCompleteL( EGetAddressesFromPhonebook, iContactObjects );
       
   784 		    	iContactObjects.ResetAndDestroy();
       
   785 		    	iHandlerObserver = NULL;
       
   786 		    	}
       
   787 			}
       
   788 		}
       
   789 	
       
   790 	else if ( (iLinkOperationFetch == &aOperation) && (iState == EContactHandlerCallToContactByEmail ) )
       
   791 	{	
       
   792 		CleanupDeletePushL( aContact );
       
   793 	
       
   794 		RPointerArray<HBufC> phonenumbers;
       
   795 		CleanupResetAndDestroyClosePushL( phonenumbers );
       
   796 		GetContactFieldsL( iPhoneNumberFields, phonenumbers, aContact );
       
   797 		TInt phoneNumbersAvailable = phonenumbers.Count();
       
   798 		CleanupStack::PopAndDestroy( &phonenumbers );
       
   799 		
       
   800 		if ( phoneNumbersAvailable )
       
   801 			{
       
   802 			if ( CallQueryL( iLastClsItem->DisplayName() ) )
       
   803 				{
       
   804 				MakeAiwCallL( iLastClsItem->ContactLink(), KNullDesC );
       
   805 				}
       
   806 			else
       
   807 				{
       
   808 				iState = EContactHandlerIdle;
       
   809 				}
       
   810 			}
       
   811 		else 
       
   812 			{
       
   813 	        if ( IsRemoteLookupSupported() )
       
   814 	            {
       
   815 	            RemoteContactQueryL();
       
   816 	            }
       
   817 	        else
       
   818 	            {
       
   819 	            TFsEmailUiUtility::ShowErrorNoteL( R_FREESTYLE_EMAIL_UI_VIEWER_NO_PHONE_NUMBER );					
       
   820 	            }
       
   821 			iState = EContactHandlerIdle;
       
   822 			}
       
   823 		
       
   824 		CleanupStack::PopAndDestroy( aContact );
       
   825 	}
       
   826 	// Addition to get contact for message creation.
       
   827 	else if ( (iLinkOperationFetch == &aOperation) && ( iMsgCreationHelperState == EContactHandlerGetSmsAddressFromPhonebook || 
       
   828 		    iMsgCreationHelperState == EContactHandlerGetMmsAddressFromPhonebook || iMsgCreationHelperState == EContactHandlerGetVoiceMsgAddressFromPhonebook ) )
       
   829 		{
       
   830 		if ( iContactForMsgCreation )
       
   831 			{
       
   832 			delete iContactForMsgCreation;
       
   833 			iContactForMsgCreation = NULL;
       
   834 			}
       
   835 		// Check that we have a contact for message creatiun
       
   836 		if ( aContact )
       
   837 			{
       
   838 			// State is now idle.
       
   839 			iState = EContactHandlerIdle;
       
   840 			// Store contact
       
   841 			iContactForMsgCreation = aContact;
       
   842 			// Create clonelink for address selection
       
   843 	        MVPbkContactLink* cloneLink = iCurrentLink->CloneLC();  
       
   844 	   		CleanupStack::Pop();
       
   845 			switch ( iMsgCreationHelperState )
       
   846 				{
       
   847 				case EContactHandlerGetSmsAddressFromPhonebook:
       
   848 					// Select SMS address, sending is done when callback completes
       
   849 					GetSmsAddressFromPhonebookAndSendL( cloneLink );
       
   850 					break;
       
   851 				case EContactHandlerGetMmsAddressFromPhonebook:
       
   852 					// Select MMS address, sending is done when callback completes
       
   853 					GetMmsAddressFromPhonebookAndSendL( cloneLink, EFalse );	
       
   854 					break;
       
   855 				case EContactHandlerGetVoiceMsgAddressFromPhonebook:
       
   856 					// Select Voice MSG address, sending is done when callback completes
       
   857 					GetMmsAddressFromPhonebookAndSendL( cloneLink, ETrue );	
       
   858 					break;
       
   859 				default:
       
   860 					iState = EContactHandlerIdle;
       
   861 					iMsgCreationHelperState = EContactHandlerIdle;
       
   862 					break;
       
   863 				}		
       
   864 			}	
       
   865 		else
       
   866 			{
       
   867 			iMsgCreationHelperState = EContactHandlerIdle;
       
   868 			iState = EContactHandlerIdle;
       
   869 			}
       
   870 		}
       
   871 	
       
   872 	}
       
   873 
       
   874 void CFSEmailUiContactHandler::VPbkSingleContactOperationFailed(
       
   875 	    MVPbkContactOperationBase& aOperation, TInt aError )
       
   876 	{
       
   877     FUNC_LOG;
       
   878 	TRAP_IGNORE( VPbkSingleContactOperationFailedL( aOperation, aError ) );
       
   879 	}
       
   880 
       
   881 void CFSEmailUiContactHandler::VPbkSingleContactOperationFailedL(
       
   882 	    MVPbkContactOperationBase& /*aOperation*/, TInt aError )
       
   883 	{
       
   884     FUNC_LOG;
       
   885 	iState = EContactHandlerIdle;
       
   886 	if ( iHandlerObserver )
       
   887 		{
       
   888 		iHandlerObserver->OperationErrorL( EFindAndCallToContactByEmailL, aError );
       
   889 		iHandlerObserver = NULL;
       
   890 		}
       
   891 	}
       
   892 
       
   893 
       
   894 void CFSEmailUiContactHandler::OpenComplete()
       
   895 	{
       
   896     FUNC_LOG;
       
   897 	if ( iStoreReady )
       
   898 		{
       
   899 		iOpenComplete = ETrue;
       
   900 		}
       
   901 	}
       
   902 
       
   903 void CFSEmailUiContactHandler::StoreReady(MVPbkContactStore& /*aContactStore*/)
       
   904 	{
       
   905     FUNC_LOG;
       
   906 	iStoreReady = ETrue;
       
   907 	}
       
   908 
       
   909 
       
   910 void CFSEmailUiContactHandler::StoreUnavailable(MVPbkContactStore& /*aContactStore*/, TInt /*aReason*/)
       
   911 	{
       
   912     FUNC_LOG;
       
   913 	iStoreReady = EFalse;
       
   914 	}
       
   915 
       
   916 
       
   917 void CFSEmailUiContactHandler::HandleStoreEventL( MVPbkContactStore& /*aContactStore*/, 
       
   918                 								  TVPbkContactStoreEvent /*aStoreEvent*/)
       
   919 	{
       
   920     FUNC_LOG;
       
   921 	
       
   922 	}
       
   923 
       
   924 // -----------------------------------------------------------------------------
       
   925 // CFSEmailUiContactHandler::HandleNotifyL
       
   926 // -----------------------------------------------------------------------------
       
   927 TInt CFSEmailUiContactHandler::HandleNotifyL( TInt aCmdId, TInt aEventId,
       
   928 	                CAiwGenericParamList& aEventParamList,
       
   929 	                const CAiwGenericParamList& /*aInParamList*/)
       
   930 	{
       
   931     FUNC_LOG;
       
   932 	TInt result = 0;
       
   933 	if ( (aCmdId == KAiwCmdAssign) && (iState == EContactHandlerAddToContacts) &&
       
   934 		 (aEventId == KAiwEventCompleted) )
       
   935 		{
       
   936 		// Add to contacts function is completed
       
   937 		iState = EContactHandlerIdle;
       
   938 		if ( iHandlerObserver )
       
   939 			{
       
   940 			RPointerArray<CFSEmailUiClsItem> emptyItems;
       
   941 			iHandlerObserver->OperationCompleteL( EAddToContactL, emptyItems );
       
   942 			emptyItems.Close();
       
   943 			iHandlerObserver = NULL;
       
   944 			}
       
   945 		
       
   946 		}
       
   947 	else if ( (aCmdId == KAiwCmdSelect) && (iState == EContactHandlerGetAddressesFromPhonebook) &&
       
   948 			  (aEventId == KAiwEventCompleted))
       
   949 		{
       
   950 
       
   951 		TInt index = 0;
       
   952 		const TAiwGenericParam* param =
       
   953 		aEventParamList.FindFirst(index, EGenericParamContactLinkArray);
       
   954 		if (param)
       
   955 			{
       
   956 			TPtrC8 contactLinks = param->Value().AsData();
       
   957 
       
   958 			iLinksSet = iContactManager->CreateLinksLC(contactLinks);
       
   959 			CleanupStack::Pop();
       
   960 			if ( iLinksSet->Count() )
       
   961 				{
       
   962 				iCurrentLink = &iLinksSet->At(0);
       
   963 				//Async operation, callback VPbkSingleContactOperationCompleteL
       
   964 				//Error situations: VPbkSingleContactOperationFailed
       
   965 				iLinkOperationFetch = iContactManager->RetrieveContactL( iLinksSet->At(0), *this );
       
   966 				}
       
   967 
       
   968 			iContactManager->ContactStoresL().OpenAllL( *this );
       
   969 			}
       
   970 
       
   971 		}
       
   972 	else if ( (aCmdId == KAiwCmdSelect) && (iState == EContactHandlerGetSmsAddressFromPhonebook) &&
       
   973 			  (aEventId == KAiwEventCompleted))
       
   974 		{
       
   975 		iState = EContactHandlerIdle;
       
   976 		SendMessageL( aEventParamList,  EContactHandlerGetSmsAddressFromPhonebook );
       
   977 		}
       
   978 	else if ( (aCmdId == KAiwCmdSelect) && (iState == EContactHandlerGetMmsAddressFromPhonebook) &&
       
   979 			  (aEventId == KAiwEventCompleted))
       
   980 		{
       
   981 		iState = EContactHandlerIdle;
       
   982 		// Send SMS to contact address
       
   983 		SendMessageL( aEventParamList,  EContactHandlerGetMmsAddressFromPhonebook );
       
   984 		}
       
   985 	else if ( (aCmdId == KAiwCmdSelect) && (iState == EContactHandlerGetVoiceMsgAddressFromPhonebook) &&
       
   986 			  (aEventId == KAiwEventCompleted))
       
   987 		{
       
   988 		iState = EContactHandlerIdle;
       
   989 		// Send Voice MSG to contact address
       
   990 		SendMessageL( aEventParamList,  EContactHandlerGetVoiceMsgAddressFromPhonebook );
       
   991 		}
       
   992 	
       
   993 	//Notify caller of the error
       
   994 	else if ( aEventId == KAiwEventError )
       
   995 		{
       
   996 		iState = EContactHandlerIdle;
       
   997 		if ( iHandlerObserver )
       
   998 			{
       
   999 			iHandlerObserver->OperationErrorL( CurrentCommand(), KErrGeneral );
       
  1000 			iHandlerObserver = NULL;
       
  1001 			}
       
  1002 		}
       
  1003 
       
  1004 	// We don't need to check outParams, or do anything special during the exit
       
  1005 	else if ( (aEventId == KAiwEventQueryExit) )
       
  1006 		{
       
  1007 		result = ETrue;
       
  1008 		}
       
  1009 	
       
  1010 	else if ( aEventId == KAiwEventOutParamCheck )
       
  1011 		{
       
  1012 		result = 1;
       
  1013 		}
       
  1014 	else if ( aEventId == KAiwEventCanceled )
       
  1015 		{
       
  1016 		iState = EContactHandlerIdle;
       
  1017 		if ( iHandlerObserver )
       
  1018 			{
       
  1019 			RPointerArray<CFSEmailUiClsItem> emptyItems;
       
  1020 			iHandlerObserver->OperationCompleteL( CurrentCommand(), emptyItems );
       
  1021 			emptyItems.Close();
       
  1022 			iHandlerObserver = NULL;
       
  1023 			}
       
  1024 		}
       
  1025 
       
  1026     return result;
       
  1027     }
       
  1028 
       
  1029 
       
  1030 CFSEmailUiContactHandler::CFSEmailUiContactHandler( RFs& aSession ): 
       
  1031 	iState(EContactHandlerIdle),
       
  1032     iHandlerObserver(NULL),
       
  1033 	iOpenComplete(EFalse),
       
  1034 	iStoreReady(EFalse),
       
  1035 	iUseSenderText(EFalse),
       
  1036 	iFs( aSession ),
       
  1037 	iCachingInProgressError(EFalse),
       
  1038     // <cmail> video call
       
  1039     iVideoCall( EFalse )
       
  1040     // </cmail>
       
  1041     {
       
  1042     FUNC_LOG;
       
  1043     }
       
  1044 
       
  1045 void CFSEmailUiContactHandler::ConstructL( RFs& aSession )
       
  1046     {
       
  1047     FUNC_LOG;
       
  1048     CVPbkContactStoreUriArray* uriArray = CVPbkContactStoreUriArray::NewL();    
       
  1049     CleanupStack::PushL( uriArray );
       
  1050     TVPbkContactStoreUriPtr phoneMemoryUri( VPbkContactStoreUris::DefaultCntDbUri() );  
       
  1051     uriArray->AppendL( phoneMemoryUri );       //default contact database store uri
       
  1052     iContactManager = CVPbkContactManager::NewL( *uriArray );
       
  1053 
       
  1054     CleanupStack::PopAndDestroy( uriArray );
       
  1055 
       
  1056     // Open all the contact stores
       
  1057     iContactManager->ContactStoresL().OpenAllL( *this );
       
  1058     
       
  1059     iClsListHandler = CFSEmailUiClsListsHandler::NewL( aSession, iContactManager );
       
  1060     iClsListHandler->SetObserver( this );
       
  1061     iServiceHandler = CAiwServiceHandler::NewL();
       
  1062     
       
  1063     iServiceHandler->AttachL( R_FSE_CONTACT_SELECTION_INTEREST );
       
  1064     iServiceHandler->AttachL( R_FSE_CONTACT_ASSIGNMENT_INTEREST );
       
  1065     iServiceHandler->AttachL( R_FSE_CALL_INTEREST );
       
  1066 
       
  1067     FormatFieldIds();
       
  1068     }
       
  1069 
       
  1070 
       
  1071 void CFSEmailUiContactHandler::HandleCallL(
       
  1072     const RPointerArray<CFSEmailUiClsItem>& aMatchingItems )
       
  1073     {
       
  1074     FUNC_LOG;
       
  1075    
       
  1076     if ( aMatchingItems.Count() == 0 )
       
  1077         {
       
  1078         if ( IsRemoteLookupSupported() )
       
  1079             {
       
  1080             RemoteContactQueryL();
       
  1081             }
       
  1082         else
       
  1083             {
       
  1084             TFsEmailUiUtility::ShowErrorNoteL( R_FREESTYLE_EMAIL_UI_VIEWER_NO_PHONE_NUMBER );					
       
  1085             }
       
  1086         // <cmail> video call flag needs to be cleared
       
  1087         iVideoCall = EFalse;
       
  1088         // </cmail>
       
  1089         iState = EContactHandlerIdle;
       
  1090         }				
       
  1091     else if ( aMatchingItems.Count() > 0 ) // Call to one directly
       
  1092         {
       
  1093         // Create contact item in which to copy number or address, async operation.
       
  1094         delete iCurrentLink;
       
  1095         iCurrentLink = NULL;
       
  1096 
       
  1097         iCurrentLink = aMatchingItems[0]->ContactLink()->CloneLC();
       
  1098         CleanupStack::Pop();
       
  1099         if ( iLinkOperationFetch )
       
  1100         	{
       
  1101         	delete iLinkOperationFetch;
       
  1102         	iLinkOperationFetch = NULL;
       
  1103         	}
       
  1104         
       
  1105         //Async operation, callback VPbkSingleContactOperationCompleteL
       
  1106         //Error situations: VPbkSingleContactOperationFailed
       
  1107         iLinkOperationFetch = iContactManager->RetrieveContactL
       
  1108         (*iCurrentLink, *this);	
       
  1109         
       
  1110         }
       
  1111     }
       
  1112 
       
  1113 void CFSEmailUiContactHandler::CreateMessageL( const RPointerArray<CFSEmailUiClsItem>& aMatchingItems )
       
  1114 	{
       
  1115     FUNC_LOG;
       
  1116 	iState = EContactHandlerIdle;
       
  1117 	
       
  1118 	if ( aMatchingItems.Count() == 0 )
       
  1119 		{
       
  1120         // <cmail>
       
  1121         /*if ( IsRemoteLookupSupported() )
       
  1122             {
       
  1123             TInt answer = TFsEmailUiUtility::ShowConfirmationQueryL( R_FREESTYLE_EMAIL_FETCH_FROM_REMOTE_QUERY );
       
  1124             if ( answer )
       
  1125                 {
       
  1126                 LaunchRemoteLookupWithQueryL( *iMailBox, *iPreviousEmailAddress );
       
  1127                 }
       
  1128             }
       
  1129         else
       
  1130             {
       
  1131             // No email address found and no RCL available, send MMS to email addres
       
  1132             // using utilities class. MMS Service is validated in utilities class.
       
  1133             TFsEmailUiUtility::ShowCreateMessageQueryL( *iPreviousEmailAddress, ETrue );
       
  1134             }*/
       
  1135         // phone number not found for email address in contacts - 
       
  1136         // no Text message available - subset of Multimedia and Voice message is displayed
       
  1137         TFsEmailUiUtility::ShowCreateMessageQueryL( *iPreviousEmailAddress, ETrue );
       
  1138         // </cmail>
       
  1139 		iState = EContactHandlerIdle;
       
  1140 		}
       
  1141 	else if ( aMatchingItems.Count() > 0 ) 
       
  1142 		{
       
  1143 		CSendUi* sendUi = CSendUi::NewLC();
       
  1144 
       
  1145 		CArrayFix<TUid>* showedServicesUidArray = new( ELeave ) CArrayFixFlat<TUid>(4);
       
  1146 		CleanupStack::PushL( showedServicesUidArray );
       
  1147 
       
  1148 		CDesCArrayFlat* array = new ( ELeave ) CDesCArrayFlat( 2 );
       
  1149 		CleanupStack::PushL( array );
       
  1150 
       
  1151 		TSendingCapabilities noCapabilities(0,0,0);
       
  1152 		
       
  1153 		if( sendUi->ValidateServiceL( KSenduiMtmSmsUid, noCapabilities ) )
       
  1154 			{
       
  1155 			HBufC* textMessage = StringLoader::LoadLC( R_FREESTYLE_EMAIL_UI_VIEWER_TEXT_MESSAGE );
       
  1156 		    array->AppendL( *textMessage );
       
  1157 			CleanupStack::PopAndDestroy( textMessage );				
       
  1158 			showedServicesUidArray->AppendL( KSenduiMtmSmsUid );
       
  1159 			}
       
  1160 		if( sendUi->ValidateServiceL( KSenduiMtmMmsUid, noCapabilities ) )
       
  1161 			{
       
  1162 			HBufC* multimediaMessage = StringLoader::LoadLC( R_FREESTYLE_EMAIL_UI_VIEWER_MULTIMEDIA_MESSAGE );
       
  1163 		    array->AppendL( *multimediaMessage );
       
  1164 			CleanupStack::PopAndDestroy( multimediaMessage );				
       
  1165 			showedServicesUidArray->AppendL( KSenduiMtmMmsUid );
       
  1166 			}
       
  1167 		if( sendUi->ValidateServiceL( KSenduiMtmAudioMessageUid, noCapabilities ) )
       
  1168 			{
       
  1169 			HBufC* voiceMessage = StringLoader::LoadLC( R_FREESTYLE_EMAIL_UI_VIEWER_VOICE_MESSAGE );
       
  1170 		    array->AppendL( *voiceMessage );
       
  1171 			CleanupStack::PopAndDestroy( voiceMessage );				
       
  1172 			showedServicesUidArray->AppendL( KSenduiMtmAudioMessageUid );
       
  1173 			}
       
  1174 
       
  1175 		TInt selectedOption;
       
  1176 		CAknListQueryDialog* dlg = new ( ELeave ) CAknListQueryDialog( &selectedOption );
       
  1177 		dlg->PrepareLC( R_MAIL_VIEWER_CREATE_MESSAGE_DIALOG  );
       
  1178 					
       
  1179 		dlg->SetItemTextArray( array );
       
  1180 		dlg->SetOwnershipType( ELbmDoesNotOwnItemArray );	
       
  1181 		TInt ret = dlg->RunLD();
       
  1182 		CleanupStack::PopAndDestroy( array );
       
  1183 		CleanupStack::PopAndDestroy( showedServicesUidArray );
       
  1184 		CleanupStack::PopAndDestroy( sendUi );		
       
  1185 	
       
  1186 		// Continue sending process if query is not cancelled
       
  1187 		if( ret )
       
  1188 			{
       
  1189 			switch ( selectedOption )
       
  1190 				{
       
  1191 				case 0:
       
  1192 					// Select SMS address, sending is done when callback completes
       
  1193 					iMsgCreationHelperState = EContactHandlerGetSmsAddressFromPhonebook;	
       
  1194 					break;
       
  1195 				case 1:
       
  1196 					// Select MMS address, sending is done when callback completes
       
  1197 					iMsgCreationHelperState = EContactHandlerGetMmsAddressFromPhonebook;	
       
  1198 					break;
       
  1199 				case 2:
       
  1200 					// Select Voice MSG address, sending is done when callback completes
       
  1201 					iMsgCreationHelperState = EContactHandlerGetVoiceMsgAddressFromPhonebook;	
       
  1202 					break;
       
  1203 				default:
       
  1204 					iMsgCreationHelperState = EContactHandlerIdle;
       
  1205 					break;
       
  1206 				}
       
  1207 			// Create contact item in which to copy number or address, async operation.
       
  1208 			delete iCurrentLink;
       
  1209 			iCurrentLink = NULL;
       
  1210 			
       
  1211 			iCurrentLink = aMatchingItems[0]->ContactLink()->CloneLC();
       
  1212 			CleanupStack::Pop();
       
  1213 			if ( iLinkOperationFetch )
       
  1214 				{
       
  1215 				delete iLinkOperationFetch;
       
  1216 				iLinkOperationFetch = NULL;
       
  1217 				}
       
  1218 			
       
  1219 			//Async operation, callback VPbkSingleContactOperationCompleteL
       
  1220 			//Error situations: VPbkSingleContactOperationFailed
       
  1221 			iLinkOperationFetch = iContactManager->RetrieveContactL
       
  1222 			(*iCurrentLink, *this);											
       
  1223 			}
       
  1224 		}
       
  1225 	}
       
  1226 
       
  1227 // -----------------------------------------------------------------------------
       
  1228 // CFSEmailUiContactHandler::SendMessageL
       
  1229 // -----------------------------------------------------------------------------
       
  1230 void CFSEmailUiContactHandler::SendMessageL( CAiwGenericParamList& aEventParamList, TInt aServiceType )
       
  1231 	{
       
  1232     FUNC_LOG;
       
  1233 	TInt paramIndex(0);
       
  1234 	iState = EContactHandlerIdle;
       
  1235 
       
  1236 	const TAiwGenericParam* param = 
       
  1237             aEventParamList.FindFirst( paramIndex, EGenericParamContactLinkArray );
       
  1238     if ( param )
       
  1239         {
       
  1240         TPtrC8 packedLinks = param->Value().AsData();
       
  1241         if ( packedLinks.Length() > 0 )
       
  1242             {      
       
  1243 	     	CSendUi* sendUi = CSendUi::NewLC();
       
  1244     		CMessageData* messageData = CMessageData::NewLC();		 
       
  1245             MVPbkContactLinkArray* linkArray = GetContactManager()->CreateLinksLC(packedLinks);
       
  1246             MVPbkStoreContactField* field = iContactForMsgCreation->Fields().RetrieveField(linkArray->At(0));
       
  1247             MVPbkContactFieldData& data = field->FieldData();
       
  1248             if (data.DataType() == EVPbkFieldStorageTypeText)
       
  1249                 {
       
  1250                 const TDesC& dataText = MVPbkContactFieldTextData::Cast(data).Text();			
       
  1251 		     	// Get contact alias to message data if there is name data
       
  1252                 //RFs& rFs = CEikonEnv::Static()->FsSession();
       
  1253 		        //User::LeaveIfError(rFs.Connect());    
       
  1254 			    CPbk2SortOrderManager* sortOrderManager = 
       
  1255 			    		CPbk2SortOrderManager::NewL(iContactManager->FieldTypes(), &iFs);
       
  1256 			    MPbk2ContactNameFormatter* nameFormatter = 
       
  1257 			    		Pbk2ContactNameFormatterFactory::CreateL(iContactManager->FieldTypes(), *sortOrderManager, &iFs);		     
       
  1258 		     	HBufC* nameBuffer = nameFormatter->GetContactTitleL( iContactForMsgCreation->Fields(), 0);
       
  1259 		     	
       
  1260 		     	delete sortOrderManager;
       
  1261 		     	sortOrderManager = NULL;
       
  1262 		     	delete nameFormatter;     
       
  1263 		     	nameFormatter = NULL;
       
  1264 		        
       
  1265 		     	//rFs.Close();
       
  1266 		        if ( nameBuffer )
       
  1267 		            {
       
  1268 		            CleanupStack::PushL( nameBuffer );
       
  1269 		            messageData->AppendToAddressL( dataText, *nameBuffer );
       
  1270 		            CleanupStack::PopAndDestroy( nameBuffer );
       
  1271 		            }
       
  1272 		        else
       
  1273 		            {
       
  1274 					messageData->AppendToAddressL( dataText );	
       
  1275 		            }	
       
  1276 		        // Send selected message							
       
  1277 				TSendingCapabilities noCapabilities(0,0,0);
       
  1278 				switch ( aServiceType )
       
  1279 					{				
       
  1280 					case EContactHandlerGetSmsAddressFromPhonebook:
       
  1281 						{
       
  1282 						if( sendUi->ValidateServiceL( KSenduiMtmSmsUid, noCapabilities ) )
       
  1283 							{
       
  1284 							sendUi->CreateAndSendMessageL( KSenduiMtmSmsUid, messageData, KNullUid, EFalse );							
       
  1285 							}
       
  1286 						}					
       
  1287 						break;
       
  1288 					case EContactHandlerGetMmsAddressFromPhonebook:
       
  1289 						{
       
  1290 						if( sendUi->ValidateServiceL( KSenduiMtmMmsUid, noCapabilities ) )
       
  1291 							{
       
  1292 							sendUi->CreateAndSendMessageL( KSenduiMtmMmsUid, messageData, KNullUid, EFalse );							
       
  1293 							}
       
  1294 						}
       
  1295 						break;
       
  1296 					case EContactHandlerGetVoiceMsgAddressFromPhonebook:
       
  1297 						{
       
  1298 						if( sendUi->ValidateServiceL( KSenduiMtmAudioMessageUid, noCapabilities ) )
       
  1299 							{
       
  1300 							sendUi->CreateAndSendMessageL( KSenduiMtmAudioMessageUid, messageData, KNullUid, EFalse );							
       
  1301 							}
       
  1302 						}
       
  1303 		 				break;
       
  1304 					default:
       
  1305 						break;			
       
  1306 					}
       
  1307                 }	
       
  1308             CleanupStack::PopAndDestroy( 3 ); // Sendui, messageData and linkArray.
       
  1309             }
       
  1310         }
       
  1311 	}
       
  1312 // ---------------------------------------------------------------------------
       
  1313 // CFSEmailUiContactHandler::IsRemoteLookupSupported
       
  1314 // ---------------------------------------------------------------------------
       
  1315 //
       
  1316 TBool CFSEmailUiContactHandler::IsRemoteLookupSupported()
       
  1317     {
       
  1318     FUNC_LOG;
       
  1319     TBool supported = iMailBox &&
       
  1320         iMailBox->HasCapability( EFSMBoxCapaSupportsRCL );
       
  1321     return supported;
       
  1322     }
       
  1323 
       
  1324 
       
  1325 void CFSEmailUiContactHandler::GetContactFieldsL( RArray<TInt>& aFieldIds,
       
  1326     RPointerArray<HBufC>& aNumbers, MVPbkStoreContact* aContact)
       
  1327 	{
       
  1328     FUNC_LOG;
       
  1329 	for (TInt i = 0; i < aFieldIds.Count(); i++ )
       
  1330 		{
       
  1331 		const MVPbkFieldType*  myContactDataField = 
       
  1332 		iContactManager->FieldTypes().Find( aFieldIds[i] );
       
  1333 
       
  1334 		CVPbkBaseContactFieldTypeIterator* itrNumber = 
       
  1335 		CVPbkBaseContactFieldTypeIterator::NewLC( *myContactDataField, 
       
  1336 				aContact->Fields() );	
       
  1337 
       
  1338 		// Iterate through each of the data fields
       
  1339 		while ( itrNumber->HasNext() )
       
  1340 			{
       
  1341 			const MVPbkBaseContactField* field = itrNumber->Next();
       
  1342 
       
  1343 			if ( (field->FieldData()).DataType() == EVPbkFieldStorageTypeText )
       
  1344 				{
       
  1345 				HBufC* toAppend = MVPbkContactFieldTextData::Cast(
       
  1346 						field->FieldData() ).Text().AllocL();
       
  1347 				
       
  1348 				aNumbers.Append( toAppend );					
       
  1349 				}
       
  1350 			}
       
  1351 		CleanupStack::PopAndDestroy( itrNumber ); 
       
  1352 		}
       
  1353 	}
       
  1354 
       
  1355 // ---------------------------------------------------------------------------
       
  1356 // Activates phonebook view to show details of give contact
       
  1357 // ---------------------------------------------------------------------------
       
  1358 //
       
  1359 void CFSEmailUiContactHandler::ShowDetailsL(
       
  1360     const RPointerArray<CFSEmailUiClsItem>& aMatchingItems )
       
  1361     {
       
  1362     FUNC_LOG;
       
  1363     if ( aMatchingItems.Count() &&
       
  1364         aMatchingItems[0]->ContactLink() )
       
  1365         {
       
  1366         MVPbkContactLink* contact = aMatchingItems[0]->ContactLink();
       
  1367         CPbk2ViewState* pbk2ViewParam = CPbk2ViewState::NewLC();
       
  1368         pbk2ViewParam->SetFocusedContact( contact->CloneLC() );
       
  1369         CleanupStack::Pop(); // Cloned contact
       
  1370         pbk2ViewParam->SetFocusedFieldIndex( 3 );
       
  1371         HBufC8* paramBuf = pbk2ViewParam->PackLC();
       
  1372 
       
  1373         // Make view id with Phonebook2's app UID3 and Contact Info View's id
       
  1374         const TVwsViewId viewId( TUid::Uid(0x101f4cce),
       
  1375             TUid::Uid(EPbk2ContactInfoViewId) );
       
  1376 
       
  1377         // Activate the view
       
  1378         CEikonEnv::Static()->AppUi()->ActivateViewL( viewId,
       
  1379             CPbk2ViewState::Uid(), *paramBuf );
       
  1380 
       
  1381         // Cleanup
       
  1382         CleanupStack::PopAndDestroy( paramBuf );
       
  1383         CleanupStack::PopAndDestroy( pbk2ViewParam );
       
  1384         }
       
  1385     else
       
  1386         {
       
  1387         TFsEmailUiUtility::ShowErrorNoteL(
       
  1388             R_FREESTYLE_EMAIL_UI_VIEWER_NO_EMAIL_ADDRESS, EFalse );
       
  1389         }
       
  1390 
       
  1391     iState = EContactHandlerIdle;
       
  1392     }
       
  1393 
       
  1394 // ---------------------------------------------------------------------------
       
  1395 // CFSEmailUiContactHandler::FindContactLinkL
       
  1396 // Searches contacts matching with the given email address
       
  1397 // ---------------------------------------------------------------------------
       
  1398 //
       
  1399 void CFSEmailUiContactHandler::FindContactLinkL( const TDesC& aDes )
       
  1400     {
       
  1401     FUNC_LOG;
       
  1402 
       
  1403     // Replace email address to match with the new search
       
  1404     SetPreviousEmailAddressL( aDes );
       
  1405 
       
  1406     // Get contact links matching the given address
       
  1407     iClsListHandler->SetCurrentMailboxL( NULL );
       
  1408     iClsListHandler->SearchMatchesL( aDes );
       
  1409     }
       
  1410 
       
  1411 void CFSEmailUiContactHandler::FormatFieldIds()
       
  1412 	{
       
  1413     FUNC_LOG;
       
  1414 	iEmailFields.Append( R_VPBK_FIELD_TYPE_EMAILGEN );
       
  1415 	iEmailFields.Append( R_VPBK_FIELD_TYPE_EMAILWORK );
       
  1416 	iEmailFields.Append( R_VPBK_FIELD_TYPE_EMAILHOME );
       
  1417 	
       
  1418 	iFirstnameFields.Append( R_VPBK_FIELD_TYPE_FIRSTNAME );
       
  1419 	iLastnameFields.Append( R_VPBK_FIELD_TYPE_LASTNAME );
       
  1420 	
       
  1421 	iPhoneNumberFields.Append( R_VPBK_FIELD_TYPE_LANDPHONEHOME );
       
  1422 	iPhoneNumberFields.Append( R_VPBK_FIELD_TYPE_MOBILEPHONEWORK );
       
  1423 	iPhoneNumberFields.Append( R_VPBK_FIELD_TYPE_MOBILEPHONEHOME );
       
  1424 	iPhoneNumberFields.Append( R_VPBK_FIELD_TYPE_LANDPHONEWORK );
       
  1425 	iPhoneNumberFields.Append( R_VPBK_FIELD_TYPE_LANDPHONEGEN );
       
  1426 	iPhoneNumberFields.Append( R_VPBK_FIELD_TYPE_MOBILEPHONEGEN );
       
  1427 	
       
  1428 	}
       
  1429 
       
  1430 void CFSEmailUiContactHandler::ResetFieldIds()
       
  1431 	{
       
  1432     FUNC_LOG;
       
  1433 	iLastnameFields.Reset();
       
  1434 	iFirstnameFields.Reset();
       
  1435 	iEmailFields.Reset();
       
  1436 	iPhoneNumberFields.Reset();
       
  1437 	}
       
  1438 
       
  1439 
       
  1440 // -----------------------------------------------------------------------------
       
  1441 // CFSEmailUiContactHandler::LaunchRemoteLookupL
       
  1442 // -----------------------------------------------------------------------------
       
  1443 TBool CFSEmailUiContactHandler::GetNameAndEmailFromRemoteLookupL( CFSMailBox& aMailBox,
       
  1444     const TDesC& aQueryString, TDes& aDisplayname, TDes& aEmailAddress )
       
  1445 	{
       
  1446     FUNC_LOG;
       
  1447 
       
  1448 	CPbkxRemoteContactLookupServiceUiContext::TResult result;
       
  1449 	DoRemoteLookupL( aMailBox, aQueryString, result, 
       
  1450 			         CPbkxRemoteContactLookupServiceUiContext::EModeContactSelector);
       
  1451 
       
  1452 	if ( result.iExitReason == 
       
  1453 		 CPbkxRemoteContactLookupServiceUiContext::TResult::EExitContactSelected )
       
  1454 		{
       
  1455 		GetNameAndEmail( aDisplayname, aEmailAddress, *(result.iSelectedContactItem) );
       
  1456 		return ETrue;
       
  1457 		} 
       
  1458 
       
  1459 	return EFalse;
       
  1460 	}
       
  1461 
       
  1462 // -----------------------------------------------------------------------------
       
  1463 // CFSEmailUiContactHandler::LaunchRemoteLookupL
       
  1464 // -----------------------------------------------------------------------------
       
  1465 void CFSEmailUiContactHandler::LaunchRemoteLookupL( CFSMailBox& aMailBox )
       
  1466 	{
       
  1467     FUNC_LOG;
       
  1468 
       
  1469 	CPbkxRemoteContactLookupServiceUiContext::TResult result;
       
  1470 	DoRemoteLookupL( aMailBox, KNullDesC , result, 
       
  1471 				     CPbkxRemoteContactLookupServiceUiContext::EModeNormal );
       
  1472 
       
  1473 	}
       
  1474 
       
  1475 // -----------------------------------------------------------------------------
       
  1476 // CFSEmailUiContactHandler::LaunchRemoteLookupL
       
  1477 // -----------------------------------------------------------------------------
       
  1478 HBufC* CFSEmailUiContactHandler::GetNameAndNumberFromRemoteLookupL( CFSMailBox& aMailBox, const TDesC& aQuery, 
       
  1479 													  RBuf& aPhoneNumber )
       
  1480 	{
       
  1481     FUNC_LOG;
       
  1482 
       
  1483 	CPbkxRemoteContactLookupServiceUiContext::TResult result;
       
  1484 	DoRemoteLookupL( aMailBox, aQuery , result, 
       
  1485 				     CPbkxRemoteContactLookupServiceUiContext::EModeContactSelector );
       
  1486 
       
  1487 	HBufC* displayName = NULL;
       
  1488 	
       
  1489 	if ( result.iExitReason == 
       
  1490 	CPbkxRemoteContactLookupServiceUiContext::TResult::EExitContactSelected )
       
  1491 		{
       
  1492 		displayName = GetPhoneNumberAndNameL( aPhoneNumber, *(result.iSelectedContactItem) );
       
  1493 		} 
       
  1494 	return displayName;
       
  1495 	}
       
  1496 
       
  1497 void CFSEmailUiContactHandler::LaunchRemoteLookupWithQueryL( CFSMailBox& aMailBox, const TDesC& aQuery )
       
  1498 	{
       
  1499     FUNC_LOG;
       
  1500 
       
  1501 	CPbkxRemoteContactLookupServiceUiContext::TResult result;
       
  1502 	DoRemoteLookupL( aMailBox, aQuery , result, 
       
  1503 					     CPbkxRemoteContactLookupServiceUiContext::EModeExistingCriteria );
       
  1504 
       
  1505 	}
       
  1506 
       
  1507 HBufC* CFSEmailUiContactHandler::GetLastSearchNameL( const TDesC& aEmailAddress )
       
  1508 	{
       
  1509     FUNC_LOG;
       
  1510 	HBufC* name(NULL);
       
  1511 	if ( iSearchMatch )
       
  1512 		{
       
  1513 		if ( !iSearchMatch->EmailAddress().CompareF( aEmailAddress ) )
       
  1514 			{
       
  1515 			if ( iSearchMatch->DisplayName() != KNullDesC )
       
  1516 				{
       
  1517 				name = iSearchMatch->DisplayName().AllocL();
       
  1518 				}
       
  1519 			}
       
  1520 		}
       
  1521 	return name;
       
  1522 	}
       
  1523 
       
  1524 void CFSEmailUiContactHandler::Reset()
       
  1525 	{
       
  1526     FUNC_LOG;
       
  1527 	iContactObjects.ResetAndDestroy();
       
  1528 	iState = EContactHandlerIdle;
       
  1529 	delete iLinksSet;
       
  1530 	iLinksSet = NULL;
       
  1531 	delete iLinkOperation;
       
  1532 	iLinkOperation = NULL;
       
  1533 	delete iLinkOperationFetch;
       
  1534 	iLinkOperationFetch = NULL;
       
  1535 	iHandlerObserver = NULL;
       
  1536 	}
       
  1537 
       
  1538 
       
  1539 // ---------------------------------------------------------------------------
       
  1540 // Gets the name and email address from given contact item
       
  1541 // ---------------------------------------------------------------------------
       
  1542 //
       
  1543 void CFSEmailUiContactHandler::GetNameAndEmail( TDes& aName, TDes& aEmail,
       
  1544     CContactItem& aItem ) const
       
  1545 	{
       
  1546     FUNC_LOG;
       
  1547 	CContactItemFieldSet& fieldSet = aItem.CardFields();
       
  1548 	aName.Zero();
       
  1549 
       
  1550 	RBuf familyName;
       
  1551 	RBuf givenName;
       
  1552 	TInt pos;
       
  1553 
       
  1554 	pos = fieldSet.Find(KUidContactFieldFamilyName);
       
  1555 	if (pos >= 0)
       
  1556 		{
       
  1557 		CContactItemField& itemField=fieldSet[pos];
       
  1558 		if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
       
  1559 			{
       
  1560 			CContactTextField* textField = itemField.TextStorage();
       
  1561 			familyName.Create( textField->Text() );
       
  1562 			}				
       
  1563 		}
       
  1564 	pos = fieldSet.Find(KUidContactFieldGivenName);
       
  1565 	if (pos >= 0)
       
  1566 		{
       
  1567 		CContactItemField& itemField=fieldSet[pos];
       
  1568 		if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
       
  1569 			{
       
  1570 			CContactTextField* textField = itemField.TextStorage();
       
  1571 			givenName.Create( textField->Text() );
       
  1572 			}				
       
  1573 		}	
       
  1574 	RBuf email;
       
  1575 	pos = fieldSet.Find(KUidContactFieldEMail);
       
  1576 	if (pos >= 0)
       
  1577 		{
       
  1578 		CContactItemField& itemField=fieldSet[pos];
       
  1579 		if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
       
  1580 			{
       
  1581 			CContactTextField* textField = itemField.TextStorage();
       
  1582 			email.Create( textField->Text() );
       
  1583 			aEmail.Copy( email );
       
  1584 			}				
       
  1585 		}
       
  1586 	email.Close();
       
  1587 
       
  1588 
       
  1589 	if ( familyName.Length() )
       
  1590 		{
       
  1591 		aName.Append(givenName);
       
  1592 		aName.Trim();
       
  1593 		}
       
  1594 
       
  1595 	if ( givenName.Length() )
       
  1596 		{
       
  1597 		aName.Append(' ');
       
  1598 		aName.Append(familyName);
       
  1599 		aName.Trim();
       
  1600 		}
       
  1601 
       
  1602 	givenName.Close();
       
  1603 	familyName.Close();
       
  1604 	}
       
  1605 
       
  1606 // ---------------------------------------------------------------------------
       
  1607 // Gets the phone number from given contact item
       
  1608 // ---------------------------------------------------------------------------
       
  1609 //
       
  1610 HBufC* CFSEmailUiContactHandler::GetPhoneNumberAndNameL( RBuf& aPhoneNumber, CContactItem& aItem ) const
       
  1611 	{
       
  1612     FUNC_LOG;
       
  1613 	TInt pos;
       
  1614 	CContactItemFieldSet& fieldSet = aItem.CardFields();
       
  1615 
       
  1616 	pos = fieldSet.Find(KUidContactFieldPhoneNumber);
       
  1617 	if (pos >= 0)
       
  1618 		{
       
  1619 		CContactItemField& itemField=fieldSet[pos];
       
  1620 		if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
       
  1621 			{
       
  1622 			CContactTextField* textField = itemField.TextStorage();
       
  1623 			//aPhoneNumber.Create( textField->Text()->Length() );
       
  1624 			//aPhoneNumber.Copy( textField->Text() );
       
  1625 			aPhoneNumber.Create( textField->Text() );
       
  1626 			}				
       
  1627 		}
       
  1628 	
       
  1629 	HBufC* firstname = NULL;
       
  1630 	
       
  1631 	pos = fieldSet.Find( KUidContactFieldGivenName );
       
  1632 	if ( pos >= 0 )
       
  1633 		{
       
  1634 		CContactItemField& itemField=fieldSet[pos];
       
  1635 		if ( !itemField.IsHidden() && !itemField.IsDisabled() )
       
  1636 			{
       
  1637 			CContactTextField* textField = itemField.TextStorage();
       
  1638 			firstname = textField->Text().AllocLC();
       
  1639 			}				
       
  1640 		}
       
  1641 	if ( !firstname )
       
  1642 	    {
       
  1643 	    firstname = KNullDesC().AllocLC();
       
  1644 	    }
       
  1645 
       
  1646 	HBufC* lastname = NULL;
       
  1647 	
       
  1648 	pos = fieldSet.Find( KUidContactFieldFamilyName );
       
  1649 	if ( pos >= 0 )
       
  1650 		{
       
  1651 		CContactItemField& itemField=fieldSet[pos];
       
  1652 		if ( !itemField.IsHidden() && !itemField.IsDisabled() )
       
  1653 			{
       
  1654 			CContactTextField* textField = itemField.TextStorage();
       
  1655 			lastname = textField->Text().AllocLC();
       
  1656 			}				
       
  1657 		}
       
  1658 	if ( !lastname )
       
  1659 	    {
       
  1660 	    lastname = KNullDesC().AllocLC();
       
  1661 	    }
       
  1662 	
       
  1663 	HBufC* name = TFsEmailUiUtility::CreateDisplayNameLC( *firstname, *lastname, KNullDesC );
       
  1664 	CleanupStack::Pop( name );
       
  1665 	CleanupStack::PopAndDestroy( lastname );
       
  1666 	CleanupStack::PopAndDestroy( firstname );
       
  1667 	
       
  1668 	return name;
       
  1669 	}
       
  1670 
       
  1671 // ---------------------------------------------------------------------------
       
  1672 // CFSEmailUiContactHandler::DoRemoteLookupL
       
  1673 // ---------------------------------------------------------------------------
       
  1674 //
       
  1675 void CFSEmailUiContactHandler::DoRemoteLookupL( CFSMailBox& aMailBox,
       
  1676     const TDesC& aQueryString,
       
  1677 	CPbkxRemoteContactLookupServiceUiContext::TResult& aResult,
       
  1678 	CPbkxRemoteContactLookupServiceUiContext::TMode aContext )
       
  1679 	{
       
  1680     FUNC_LOG;
       
  1681    	TUid protocolUid = TUid::Null();
       
  1682 	TUint accountId = 0;
       
  1683 	aMailBox.GetRCLInfo( protocolUid, accountId );
       
  1684 	const TPbkxRemoteContactLookupProtocolAccountId KAccountId(
       
  1685 		protocolUid, accountId );
       
  1686 		
       
  1687 
       
  1688 	CPbkxRemoteContactLookupEnv* env = CPbkxRemoteContactLookupEnv::NewL();
       
  1689 	CleanupStack::PushL( env );
       
  1690 
       
  1691 	MPbkxRemoteContactLookupServiceUi* serviceUi = env->ServiceUiL();
       
  1692 
       
  1693 	// If you want test with RCL dummyContactDatabase, KAccountId = serviceUi->DefaultAccountIdL();
       
  1694 	MPbkxRemoteContactLookupServiceUi::TContextParams params = { KAccountId, aContext };
       
  1695 
       
  1696 	CPbkxRemoteContactLookupServiceUiContext* ctx( NULL );
       
  1697 	ctx = serviceUi->NewContextL( params );
       
  1698 
       
  1699 	CleanupStack::PushL( ctx ); 
       
  1700 
       
  1701 	ctx->ExecuteL( aQueryString, aResult );
       
  1702 
       
  1703 	CleanupStack::PopAndDestroy( ctx );
       
  1704 	CleanupStack::PopAndDestroy( env );
       
  1705 
       
  1706 	}
       
  1707 
       
  1708 
       
  1709 // ---------------------------------------------------------------------------
       
  1710 // Displays call query
       
  1711 // ---------------------------------------------------------------------------
       
  1712 //
       
  1713 TBool CFSEmailUiContactHandler::CallQueryL(
       
  1714     const TDesC& aDisplayName )
       
  1715 	{
       
  1716     FUNC_LOG;
       
  1717 	TInt answer = 0;
       
  1718 	
       
  1719 	if ( aDisplayName.Length() )
       
  1720 	    {
       
  1721 	    if ( iUseSenderText )
       
  1722 	    	{
       
  1723 	    	answer = TFsEmailUiUtility::ShowConfirmationQueryL( R_FREESTYLE_EMAIL_CALL_SENDER_QUERY, 
       
  1724 															aDisplayName );
       
  1725 	    	}
       
  1726 	    else
       
  1727 	    	{
       
  1728 	    	answer = TFsEmailUiUtility::ShowConfirmationQueryL( R_FREESTYLE_EMAIL_UI_VIEWER_CALL_HOTSPOT, 
       
  1729 															    aDisplayName );
       
  1730 	    	}
       
  1731 	    }
       
  1732 	else // Show basic note if display name is not available.
       
  1733 	    {
       
  1734 	    _LIT(KEmpty, "");
       
  1735 	    // Create dialog
       
  1736 	    CFSEmailUiCallDialog* queryNote = new ( ELeave ) CFSEmailUiCallDialog();
       
  1737 	    CleanupStack::PushL( queryNote ); 
       
  1738         // Createa and set text
       
  1739 	    HBufC* querytext = StringLoader::LoadLC( R_FREESTYLE_EMAIL_UI_VIEWER_CALL_HOTSPOT, KEmpty );       
       
  1740 	    TPtr pointer = querytext->Des();
       
  1741 	    _LIT(KLineBreak, "\n");
       
  1742         AknTextUtils::StripCharacters( pointer, KLineBreak );
       
  1743 	    queryNote->SetPromptL( *querytext );       
       
  1744 	    CleanupStack::PopAndDestroy( querytext );
       
  1745         CleanupStack::Pop( queryNote );
       
  1746         // Show note.
       
  1747         answer = queryNote->ExecuteLD( R_FSEMAIL_QUERY_DIALOG );
       
  1748 	    }
       
  1749 	
       
  1750 	return answer;
       
  1751 	}
       
  1752 
       
  1753 
       
  1754 
       
  1755 // ---------------------------------------------------------------------------
       
  1756 // Creates copy of given CLS items and appends them to given array.
       
  1757 // ---------------------------------------------------------------------------
       
  1758 //
       
  1759 void CFSEmailUiContactHandler::CopyItemsL(
       
  1760     const RPointerArray<CFSEmailUiClsItem>& aItems,
       
  1761     RPointerArray<CFSEmailUiClsItem>& aArray )
       
  1762     {
       
  1763     FUNC_LOG;
       
  1764     for ( TInt ii = 0; ii < aItems.Count(); ++ii )
       
  1765         {
       
  1766         CFSEmailUiClsItem* clone = aItems[ii]->CloneLC();
       
  1767         aArray.AppendL( clone );
       
  1768         CleanupStack::Pop( clone );
       
  1769         }
       
  1770     }
       
  1771 
       
  1772 // ---------------------------------------------------------------------------
       
  1773 // Sets the address used for previous contact search
       
  1774 // ---------------------------------------------------------------------------
       
  1775 //
       
  1776 void CFSEmailUiContactHandler::SetPreviousEmailAddressL( const TDesC& aAddress )
       
  1777     {
       
  1778     FUNC_LOG;
       
  1779     delete iPreviousEmailAddress;
       
  1780     iPreviousEmailAddress = NULL;
       
  1781     iPreviousEmailAddress = aAddress.AllocL();
       
  1782     }
       
  1783 
       
  1784 void CFSEmailUiContactHandler::RemoteContactQueryL()
       
  1785 	{
       
  1786 	FUNC_LOG;
       
  1787 	TInt answer = TFsEmailUiUtility::ShowConfirmationQueryL( R_FREESTYLE_EMAIL_FETCH_FROM_REMOTE_QUERY );
       
  1788 	if ( answer )	
       
  1789 		{
       
  1790 		LaunchRemoteLookupWithQueryL( *iMailBox, *iPreviousEmailAddress );
       
  1791 		}
       
  1792 	}
       
  1793 
       
  1794 TContactHandlerCmd CFSEmailUiContactHandler::CurrentCommand()
       
  1795 	{
       
  1796     FUNC_LOG;
       
  1797 	switch (iState)
       
  1798 		{
       
  1799 		case EContactHandlerAddToContacts:
       
  1800 			return EAddToContactL;
       
  1801 		case EContactHandlerSearchMatches:
       
  1802 			return ESearchContacts;
       
  1803 		case EContactHandlerShowContactDetails:
       
  1804 			return EShowContactDetailsL;
       
  1805 		case EContactHandlerGetAddressesFromPhonebook:
       
  1806 			return EGetAddressesFromPhonebook;
       
  1807 		case EContactHandlerCallToContactByEmail:
       
  1808 			return ECallToContact;
       
  1809 		case EContactHandlerCreateMessage:
       
  1810 			return ECreateMessage;
       
  1811 		default:
       
  1812 			break;
       
  1813 		}
       
  1814 	return EIdle;
       
  1815 	}
       
  1816 
       
  1817 void CFSEmailUiContactHandler::ClearObservers()
       
  1818     {
       
  1819     iHandlerObserver = NULL;
       
  1820     }
       
  1821 	
       
  1822