meetingrequest/mrgui/mrfieldbuilderpluginextension/src/cesmrcontacthandler.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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:  ESMR Contact Handler for fetching contacts for attendee field
       
    15 *
       
    16 */
       
    17 
       
    18 #include "emailtrace.h"
       
    19 #include "cesmrcontacthandler.h"
       
    20 
       
    21 //INCLUDES
       
    22 #include <eikenv.h>
       
    23 #include <StringLoader.h>
       
    24 #include <s32mem.h>
       
    25 
       
    26 //Virtual Phonebook
       
    27 #include <CVPbkContactManager.h>
       
    28 #include <MVPbkContactOperationBase.h>
       
    29 #include <MVPbkStoreContact.h>
       
    30 #include <MVPbkFieldType.h>
       
    31 #include <VPbkEng.rsg>
       
    32 #include <CVPbkContactFieldIterator.h>
       
    33 #include <MVPbkContactFieldTextData.h>
       
    34 #include <MVPbkContactLink.h>
       
    35 
       
    36 //Remote Contact Lookup
       
    37 //<cmail>
       
    38 #include "cpbkxremotecontactlookupenv.h"
       
    39 #include <cntitem.h>
       
    40 #include <CPbk2SortOrderManager.h>
       
    41 
       
    42 // Aiw launcher
       
    43 #include <AiwCommon.hrh>
       
    44 #include <AiwContactSelectionDataTypes.h>
       
    45 #include <AiwServiceHandler.h>
       
    46 #include <AiwGenericParam.h>
       
    47 
       
    48 //FS Email framework
       
    49 #include "CFSMailBox.h"
       
    50 //</cmail>
       
    51 
       
    52 //MRUI
       
    53 #include <esmrgui.rsg>
       
    54 #include "cesmrcontactmanagerhandler.h"
       
    55 #include "cesmrclslistshandler.h"
       
    56 #include "mesmrcontacthandlerobserver.h"
       
    57 #include "cesmrclsitem.h"
       
    58 #include "esmrfieldbuilderdef.h"
       
    59 
       
    60 // Constants
       
    61 
       
    62 // Unnamed namespace for local definitions
       
    63 namespace{
       
    64 
       
    65 template<typename T> class CleanupResetAndDestroyClose
       
    66     {
       
    67     public:
       
    68         inline static void PushL( T& aRef );
       
    69     private:
       
    70         static void Close( TAny *aPtr );
       
    71     };
       
    72 
       
    73 template<typename T> inline void CleanupResetAndDestroyClose<T>::PushL( T& aRef )
       
    74     {
       
    75     CleanupStack::PushL( TCleanupItem( &Close, &aRef ) );
       
    76     }
       
    77 
       
    78 template<typename T> void CleanupResetAndDestroyClose<T>::Close( TAny *aPtr )
       
    79     {
       
    80     static_cast<T*>(aPtr)->ResetAndDestroy();
       
    81     static_cast<T*>(aPtr)->Close();
       
    82     }
       
    83 
       
    84 template<typename T> inline void CleanupResetAndDestroyClosePushL( T& aRef )
       
    85     {
       
    86     CleanupResetAndDestroyClose<T>::PushL( aRef );
       
    87     }
       
    88 
       
    89 }//namespace
       
    90 
       
    91 // ======== MEMBER FUNCTIONS ========
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CESMRContactHandler::NewL
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 CESMRContactHandler* CESMRContactHandler::NewL()
       
    98     {
       
    99     FUNC_LOG;
       
   100 
       
   101     CESMRContactHandler* self = CESMRContactHandler::NewLC();
       
   102     CleanupStack::Pop(self);
       
   103 
       
   104     return self;
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CESMRContactHandler::NewLC
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 CESMRContactHandler* CESMRContactHandler::NewLC()
       
   112     {
       
   113     FUNC_LOG;
       
   114     CESMRContactHandler* self = new (ELeave) CESMRContactHandler();
       
   115     CleanupStack::PushL(self);
       
   116     self->ConstructL();
       
   117     return self;
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CESMRContactHandler::CESMRContactHandler
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 CESMRContactHandler::CESMRContactHandler():
       
   125     iState(EContactHandlerIdle)
       
   126     {
       
   127     FUNC_LOG;
       
   128     //do nothing
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CESMRContactHandler::ConstructL
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CESMRContactHandler::ConstructL()
       
   136     {
       
   137     FUNC_LOG;
       
   138     iESMRStatic.ConnectL();
       
   139     CESMRContactManagerHandler& contactManagerHandler = iESMRStatic.ContactManagerHandlerL();
       
   140 
       
   141     if ( !contactManagerHandler.IsReady() )
       
   142         {
       
   143         contactManagerHandler.AddObserverL( this );
       
   144         }
       
   145 
       
   146     iContactManager = &contactManagerHandler.GetContactManager();
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CESMRContactHandler::ContactManagerReady
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 void CESMRContactHandler::ContactManagerReady()
       
   154     {
       
   155     FUNC_LOG;
       
   156     CESMRContactManagerHandler* contactManagerHandler = NULL;
       
   157     TRAPD( error, contactManagerHandler = &iESMRStatic.ContactManagerHandlerL());
       
   158     if ( error != KErrNone )
       
   159         {
       
   160         CEikonEnv::Static()->HandleError(error);// codescanner::eikonenvstatic
       
   161         }
       
   162     else
       
   163         {
       
   164         contactManagerHandler->RemoveObserver( this );
       
   165         }
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CESMRContactHandler::~CESMRContactHandler
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 CESMRContactHandler::~CESMRContactHandler()
       
   173     {
       
   174     FUNC_LOG;
       
   175     delete iClsListHandler;
       
   176     delete iServiceHandler;
       
   177     iLastnameFields.Reset();
       
   178     iFirstnameFields.Reset();
       
   179     iEmailFields.Reset();
       
   180     delete iSearchMatch;
       
   181     iContactObjects.ResetAndDestroy();
       
   182     delete iLinksSet;
       
   183     delete iLinkOperationFetch;
       
   184     delete iCurrentLink;
       
   185     iESMRStatic.Close();
       
   186     if(iESMRMailBoxStaticCalled)
       
   187         {
       
   188         iESMRMailBoxStatic.Close();
       
   189         }
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CESMRContactHandler::SearchMatchesL
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 void CESMRContactHandler::SearchMatchesL( const TDesC& aText,
       
   197                                           MESMRContactHandlerObserver* aObserver )
       
   198     {
       
   199     FUNC_LOG;
       
   200     if ( (iState == EContactHandlerIdle) ||
       
   201          (iState == EContactHandlerSearchMatches)  )
       
   202         {
       
   203         iState = EContactHandlerSearchMatches;
       
   204         iHandlerObserver = aObserver;
       
   205         
       
   206         //delayed launch of listhandler
       
   207         if(!iClsListHandler)
       
   208             {
       
   209             iClsListHandler = CESMRClsListsHandler::NewL(
       
   210                     CEikonEnv::Static()->FsSession(), // codescanner::eikonenvstatic
       
   211                     iContactManager );
       
   212             iClsListHandler->SetObserver( this );
       
   213             }
       
   214         
       
   215         iClsListHandler->SetCurrentMailboxL( &DelayedMailBoxL() );
       
   216         iClsListHandler->SearchMatchesL( aText );
       
   217         }
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CESMRContactHandler::RemoteLookupSupportedL
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 TBool CESMRContactHandler::RemoteLookupSupportedL()
       
   225     {
       
   226     FUNC_LOG;
       
   227     DelayedMailBoxL();
       
   228     return iRemoteLookupSupported;
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CESMRContactHandler::DelayedMailBoxL
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 CFSMailBox& CESMRContactHandler::DelayedMailBoxL()
       
   236     {
       
   237     FUNC_LOG;
       
   238     if(!iESMRMailBoxStaticCalled)
       
   239         {
       
   240         iESMRMailBoxStatic.ConnectL();
       
   241         iMailBox = &iESMRMailBoxStatic.DefaultFSMailBoxL();
       
   242         iRemoteLookupSupported = iMailBox->HasCapability( EFSMBoxCapaSupportsRCL );
       
   243         iESMRMailBoxStaticCalled = ETrue;
       
   244         }
       
   245     
       
   246     return *iMailBox;
       
   247     }
       
   248 // -----------------------------------------------------------------------------
       
   249 // CESMRContactHandler::GetContactManager
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 CVPbkContactManager* CESMRContactHandler::GetContactManager()
       
   253     {
       
   254     FUNC_LOG;
       
   255     return iContactManager;
       
   256     }
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // CESMRContactHandler::GetAddressesFromPhonebookL
       
   260 // -----------------------------------------------------------------------------
       
   261 //
       
   262 void CESMRContactHandler::GetAddressesFromPhonebookL( MESMRContactHandlerObserver* aObserver )
       
   263     {
       
   264     FUNC_LOG;
       
   265     if ( iState == EContactHandlerIdle )
       
   266         {
       
   267         if ( !aObserver )
       
   268             {
       
   269             //Request can't be fulfilled without observer
       
   270             return;
       
   271             }
       
   272         iHandlerObserver = aObserver;
       
   273         iState = EContactHandlerGetAddressesFromPhonebook;
       
   274         TUint fetchFlags = 0;
       
   275 
       
   276         iContactObjects.ResetAndDestroy();
       
   277 
       
   278         //start service handler and add the interests of this class
       
   279         if(!iServiceHandler)
       
   280             {
       
   281             iServiceHandler = CAiwServiceHandler::NewL();
       
   282             iServiceHandler->AttachL( R_CONTACT_SELECTION_INTEREST );
       
   283             }
       
   284         
       
   285         CAiwGenericParamList& inParamList = iServiceHandler->InParamListL();
       
   286         inParamList.AppendL(
       
   287                 TAiwGenericParam(
       
   288                         EGenericParamContactSelectionData,
       
   289                         TAiwVariant(TAiwMultipleItemSelectionDataV1Pckg(
       
   290                                 TAiwMultipleItemSelectionDataV1().
       
   291                                 SetAddressSelectType(EAiwEMailSelect).
       
   292                                 SetFlags( fetchFlags )))));
       
   293 
       
   294         iServiceHandler->ExecuteServiceCmdL(
       
   295                 KAiwCmdSelect,
       
   296                 inParamList,
       
   297                 iServiceHandler->OutParamListL(),
       
   298                 0,
       
   299                 this);
       
   300         }
       
   301     }
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CESMRContactHandler::ArrayUpdatedL
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 void CESMRContactHandler::ArrayUpdatedL( RPointerArray<CESMRClsItem>& aMatchingItems )
       
   308     {
       
   309     FUNC_LOG;
       
   310     switch ( iState )
       
   311             {
       
   312             case EContactHandlerSearchMatches:
       
   313                 if ( aMatchingItems.Count() == 1 )
       
   314                     {
       
   315                     if ( !iSearchMatch )
       
   316                         {
       
   317                         iSearchMatch = CESMRClsItem::NewL();
       
   318                         }
       
   319                     iSearchMatch->SetDisplayNameL( aMatchingItems[0]->DisplayName() );
       
   320                     iSearchMatch->SetEmailAddressL( aMatchingItems[0]->EmailAddress() );
       
   321                     }
       
   322                 else
       
   323                     {
       
   324                     delete iSearchMatch;
       
   325                     iSearchMatch = NULL;
       
   326                     }
       
   327 
       
   328                 // The ownership of the array content is transferred to observer.
       
   329                 iState = EContactHandlerIdle;
       
   330                 if ( iHandlerObserver )
       
   331                     {
       
   332                     iHandlerObserver->OperationCompleteL( ESearchContacts, &aMatchingItems );
       
   333                     }
       
   334                 break;
       
   335                 
       
   336             default:
       
   337                 break;
       
   338             }
       
   339     }
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // CESMRContactHandler::OperationErrorL
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 void CESMRContactHandler::OperationErrorL( TInt aErrorCode )
       
   346     {
       
   347     FUNC_LOG;
       
   348     if ( aErrorCode != KErrNone )
       
   349         {
       
   350         iState = EContactHandlerIdle;
       
   351         if ( iHandlerObserver )
       
   352             {
       
   353             iHandlerObserver->OperationErrorL( ESearchContacts, aErrorCode );
       
   354             }
       
   355         }
       
   356     }
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // CESMRContactHandler::VPbkSingleContactOperationComplete
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 void CESMRContactHandler::VPbkSingleContactOperationComplete(
       
   363         MVPbkContactOperationBase& aOperation,
       
   364         MVPbkStoreContact* aContact )
       
   365     {
       
   366     FUNC_LOG;
       
   367     TRAP_IGNORE( VPbkSingleContactOperationCompleteL( aOperation, aContact) );
       
   368     }
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CESMRContactHandler::VPbkSingleContactOperationFailed
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 void CESMRContactHandler::VPbkSingleContactOperationFailed(
       
   375         MVPbkContactOperationBase& aOperation, TInt aError )
       
   376     {
       
   377     FUNC_LOG;
       
   378     TRAP_IGNORE( VPbkSingleContactOperationFailedL( aOperation, aError ) );
       
   379     }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CESMRContactHandler::VPbkSingleContactOperationFailedL
       
   383 // -----------------------------------------------------------------------------
       
   384 //
       
   385 void CESMRContactHandler::VPbkSingleContactOperationFailedL(
       
   386         MVPbkContactOperationBase& /*aOperation*/, TInt aError )
       
   387     {
       
   388     FUNC_LOG;
       
   389     iState = EContactHandlerIdle;
       
   390     if ( iHandlerObserver )
       
   391         {
       
   392         iHandlerObserver->OperationErrorL( EGetAddressesFromPhonebook, aError );
       
   393         iHandlerObserver = NULL;
       
   394         }
       
   395     }
       
   396 
       
   397 // -----------------------------------------------------------------------------
       
   398 // CESMRContactHandler::HandleNotifyL
       
   399 // -----------------------------------------------------------------------------
       
   400 //
       
   401 TInt CESMRContactHandler::HandleNotifyL( TInt aCmdId, 
       
   402                                          TInt aEventId,
       
   403                                          CAiwGenericParamList& aEventParamList,
       
   404                                          const CAiwGenericParamList& /*aInParamList*/)
       
   405     {
       
   406     FUNC_LOG;
       
   407     TInt result = 0;
       
   408     if ( (aCmdId == KAiwCmdSelect) && 
       
   409          (iState == EContactHandlerGetAddressesFromPhonebook) &&
       
   410          (aEventId == KAiwEventCompleted))
       
   411         {
       
   412         TInt index = 0;
       
   413         const TAiwGenericParam* param = 
       
   414             aEventParamList.FindFirst(index, EGenericParamContactLinkArray);
       
   415         if (param)
       
   416             {
       
   417             TPtrC8 contactLinks = param->Value().AsData();
       
   418 
       
   419             iLinksSet = iContactManager->CreateLinksLC(contactLinks);
       
   420             CleanupStack::Pop();
       
   421             if ( iLinksSet->Count() )
       
   422                 {
       
   423                 iCurrentLink = &iLinksSet->At(0);
       
   424                 iLinkOperationFetch = iContactManager->RetrieveContactL( 
       
   425                         iLinksSet->At(0), *this );
       
   426                 }
       
   427             }
       
   428         }
       
   429     else if ( aEventId == KAiwEventError )
       
   430         {
       
   431         iState = EContactHandlerIdle;
       
   432         if ( iHandlerObserver )
       
   433             {
       
   434             //Notify caller of the error
       
   435             iHandlerObserver->OperationErrorL( ESearchContacts, KErrGeneral );
       
   436             iHandlerObserver = NULL;
       
   437             }
       
   438         }
       
   439     else if ( (aEventId == KAiwEventQueryExit) )
       
   440         {
       
   441         // We don't need to check outParams, 
       
   442         // or do anything special during the exit
       
   443         result = 1;
       
   444         }
       
   445     else if ( aEventId == KAiwEventOutParamCheck && aCmdId == KAiwCmdSelect )
       
   446         {
       
   447         // We want select this contact
       
   448         result = 1;
       
   449         }
       
   450     else if ( aEventId == KAiwEventCanceled )
       
   451         {
       
   452         iState = EContactHandlerIdle;
       
   453         if ( iHandlerObserver )
       
   454             {
       
   455             RPointerArray<CESMRClsItem> emptyItems;
       
   456             iHandlerObserver->OperationCompleteL( ESearchContacts, &emptyItems );
       
   457             emptyItems.Close();
       
   458             iHandlerObserver = NULL;
       
   459             }
       
   460         }
       
   461 
       
   462     return result;
       
   463     }
       
   464 
       
   465 // -----------------------------------------------------------------------------
       
   466 // CESMRContactHandler::GetContactFieldsL
       
   467 // -----------------------------------------------------------------------------
       
   468 //
       
   469 void CESMRContactHandler::GetContactFieldsL( RArray<TInt>& aFieldIds, RPointerArray<HBufC>& aNumbers,
       
   470                                                  MVPbkStoreContact* aContact)
       
   471     {
       
   472     FUNC_LOG;
       
   473     for (TInt i = 0; i < aFieldIds.Count(); i++ )
       
   474         {
       
   475         const MVPbkFieldType*  myContactDataField =
       
   476         iContactManager->FieldTypes().Find( aFieldIds[i] );
       
   477 
       
   478         CVPbkBaseContactFieldTypeIterator* itrNumber =
       
   479         CVPbkBaseContactFieldTypeIterator::NewLC( *myContactDataField,
       
   480                 aContact->Fields() );
       
   481 
       
   482         // Iterate through each of the data fields
       
   483         while ( itrNumber->HasNext() )
       
   484             {
       
   485             const MVPbkBaseContactField* field = itrNumber->Next();
       
   486 
       
   487             if ( (field->FieldData()).DataType() == EVPbkFieldStorageTypeText )
       
   488                 {
       
   489                 HBufC* toAppend = MVPbkContactFieldTextData::Cast(
       
   490                         field->FieldData() ).Text().AllocL();
       
   491 
       
   492                 aNumbers.Append( toAppend );
       
   493                 }
       
   494             }
       
   495         CleanupStack::PopAndDestroy( itrNumber );
       
   496         }
       
   497     }
       
   498 
       
   499 // -----------------------------------------------------------------------------
       
   500 // CESMRContactHandler::LaunchRemoteLookupL
       
   501 // -----------------------------------------------------------------------------
       
   502 //
       
   503 TBool CESMRContactHandler::LaunchRemoteLookupL( const TDesC& aQueryString,
       
   504                                                 TDes& aDisplayname, 
       
   505                                                 TDes& aEmailAddress )
       
   506     {
       
   507     FUNC_LOG;
       
   508     CPbkxRemoteContactLookupServiceUiContext::TResult result;
       
   509     DoRemoteLookupL( aQueryString, 
       
   510                      result,
       
   511                      CPbkxRemoteContactLookupServiceUiContext::EModeContactSelector);
       
   512 
       
   513     if ( result.iExitReason == CPbkxRemoteContactLookupServiceUiContext::TResult::EExitContactSelected )
       
   514         {
       
   515         GetNameAndEmail( aDisplayname, aEmailAddress, *(result.iSelectedContactItem) );
       
   516         return ETrue;
       
   517         }
       
   518 
       
   519     return EFalse;
       
   520     }
       
   521 
       
   522 // -----------------------------------------------------------------------------
       
   523 // CESMRContactHandler::GetLastSearchNameLC
       
   524 // -----------------------------------------------------------------------------
       
   525 //
       
   526 HBufC* CESMRContactHandler::GetLastSearchNameLC( const TDesC& aEmailAddress )
       
   527     {
       
   528     FUNC_LOG;
       
   529     HBufC* name(NULL);
       
   530     if ( iSearchMatch )
       
   531         {
       
   532         if ( !iSearchMatch->EmailAddress().CompareF( aEmailAddress ) )
       
   533             {
       
   534             if ( iSearchMatch->DisplayName() != KNullDesC )
       
   535                 {
       
   536                 name = HBufC::NewLC( iSearchMatch->DisplayName().Length() );
       
   537                 name->Des().Copy( iSearchMatch->DisplayName() );
       
   538                 }
       
   539             }
       
   540         }
       
   541 
       
   542     return name;
       
   543     }
       
   544 
       
   545 // -----------------------------------------------------------------------------
       
   546 // CESMRContactHandler::GetNameAndEmail
       
   547 // -----------------------------------------------------------------------------
       
   548 //
       
   549 void CESMRContactHandler::GetNameAndEmail( TDes& aName, TDes& aEmail, CContactItem& aItem )
       
   550     {
       
   551     FUNC_LOG;
       
   552     CContactItemFieldSet& fieldSet = aItem.CardFields();
       
   553     aName.Zero();
       
   554 
       
   555     RBuf familyName;
       
   556     RBuf givenName;
       
   557     TInt pos;
       
   558 
       
   559     pos = fieldSet.Find(KUidContactFieldFamilyName);
       
   560     if (pos >= 0)
       
   561         {
       
   562         CContactItemField& itemField=fieldSet[pos];
       
   563         if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
       
   564             {
       
   565             CContactTextField* textField = itemField.TextStorage();
       
   566             familyName.Create( textField->Text() );
       
   567             }
       
   568         }
       
   569     pos = fieldSet.Find(KUidContactFieldGivenName);
       
   570     if (pos >= 0)
       
   571         {
       
   572         CContactItemField& itemField=fieldSet[pos];
       
   573         if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
       
   574             {
       
   575             CContactTextField* textField = itemField.TextStorage();
       
   576             givenName.Create( textField->Text() );
       
   577             }
       
   578         }
       
   579     RBuf email;
       
   580     pos = fieldSet.Find(KUidContactFieldEMail);
       
   581     if (pos >= 0)
       
   582         {
       
   583         CContactItemField& itemField=fieldSet[pos];
       
   584         if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
       
   585             {
       
   586             CContactTextField* textField = itemField.TextStorage();
       
   587             email.Create( textField->Text() );
       
   588             aEmail.Copy( email );
       
   589             }
       
   590         }
       
   591     email.Close();
       
   592 
       
   593 
       
   594     if ( familyName.Length() )
       
   595         {
       
   596         aName.Append(givenName);
       
   597         aName.Trim();
       
   598         }
       
   599 
       
   600     if ( givenName.Length() )
       
   601         {
       
   602         aName.Append(' ');
       
   603         aName.Append(familyName);
       
   604         aName.Trim();
       
   605         }
       
   606 
       
   607     givenName.Close();
       
   608     familyName.Close();
       
   609     }
       
   610 
       
   611 // -----------------------------------------------------------------------------
       
   612 // CESMRContactHandler::DoRemoteLookupL
       
   613 // -----------------------------------------------------------------------------
       
   614 //
       
   615 void CESMRContactHandler::DoRemoteLookupL( const TDesC& aQueryString,
       
   616     CPbkxRemoteContactLookupServiceUiContext::TResult& aResult,
       
   617     CPbkxRemoteContactLookupServiceUiContext::TMode aContext )
       
   618     {
       
   619     FUNC_LOG;
       
   620     TUid protocolUid = TUid::Null();
       
   621     TUint accountId = 0;
       
   622     
       
   623     DelayedMailBoxL().GetRCLInfo( protocolUid, accountId );
       
   624     const TPbkxRemoteContactLookupProtocolAccountId KAccountId( protocolUid, accountId );
       
   625 
       
   626     CPbkxRemoteContactLookupEnv* env = CPbkxRemoteContactLookupEnv::NewL();
       
   627     CleanupStack::PushL( env );
       
   628 
       
   629     MPbkxRemoteContactLookupServiceUi* serviceUi = env->ServiceUiL();
       
   630 
       
   631     // If you want test with RCL dummyContactDatabase, KAccountId = serviceUi->DefaultAccountIdL();
       
   632     MPbkxRemoteContactLookupServiceUi::TContextParams params = { KAccountId, aContext };
       
   633 
       
   634     CPbkxRemoteContactLookupServiceUiContext* ctx( NULL );
       
   635     TRAPD( error, ctx = serviceUi->NewContextL( params ) );
       
   636 
       
   637     User::LeaveIfError( error );
       
   638 
       
   639     CleanupStack::PushL( ctx );
       
   640 
       
   641     ctx->ExecuteL( aQueryString, aResult );
       
   642 
       
   643     CleanupStack::PopAndDestroy( ctx );
       
   644     CleanupStack::PopAndDestroy( env );
       
   645     }
       
   646 
       
   647 // -----------------------------------------------------------------------------
       
   648 // CESMRContactHandler::VPbkSingleContactOperationCompleteL
       
   649 // -----------------------------------------------------------------------------
       
   650 //
       
   651 void CESMRContactHandler::VPbkSingleContactOperationCompleteL(
       
   652         MVPbkContactOperationBase& aOperation,
       
   653         MVPbkStoreContact* aContact )
       
   654     {
       
   655     if ( (iLinkOperationFetch == &aOperation) && iState == EContactHandlerGetAddressesFromPhonebook )
       
   656         {
       
   657         CleanupDeletePushL( aContact );
       
   658 
       
   659         //setup contact field arrays, only must be done on first result
       
   660         if( iEmailFields.Count() < 1 )
       
   661             {
       
   662             iEmailFields.Append( R_VPBK_FIELD_TYPE_EMAILGEN );
       
   663             iEmailFields.Append( R_VPBK_FIELD_TYPE_EMAILWORK );
       
   664             iEmailFields.Append( R_VPBK_FIELD_TYPE_EMAILHOME );
       
   665             iFirstnameFields.Append( R_VPBK_FIELD_TYPE_FIRSTNAME );
       
   666             iLastnameFields.Append( R_VPBK_FIELD_TYPE_LASTNAME );
       
   667             }
       
   668         
       
   669         RPointerArray<HBufC> firstname;
       
   670         CleanupResetAndDestroyClosePushL( firstname );
       
   671         RPointerArray<HBufC> lastname;
       
   672         CleanupResetAndDestroyClosePushL( lastname );
       
   673         RPointerArray<HBufC> emailAddresses;
       
   674         CleanupResetAndDestroyClosePushL( emailAddresses );
       
   675         GetContactFieldsL( iFirstnameFields, firstname, aContact );
       
   676         GetContactFieldsL( iLastnameFields, lastname, aContact );
       
   677 
       
   678         const TInt KMaxNameLength = 255;
       
   679         // Create display name, this will be used in UI.
       
   680         HBufC* displayname = HBufC::NewLC( KMaxNameLength );
       
   681         if ( firstname.Count() )
       
   682             {
       
   683             displayname->Des().Copy( *firstname[0] );
       
   684             }
       
   685         if ( lastname.Count() )
       
   686             {
       
   687             if ( firstname.Count() )
       
   688                 {
       
   689                 displayname->Des().Append( KEmptySpace );
       
   690                 }
       
   691             displayname->Des().Append( *lastname[0] );
       
   692             }
       
   693 
       
   694         // retrieve selected email address
       
   695         MVPbkStoreContactFieldCollection& fields = aContact->Fields();
       
   696         MVPbkBaseContactField* selectedField = fields.RetrieveField( *iCurrentLink );
       
   697         HBufC* selectedEmailAddress = MVPbkContactFieldTextData::Cast(
       
   698                         selectedField->FieldData() ).Text().AllocL();
       
   699         CleanupStack::PushL( selectedEmailAddress );
       
   700         
       
   701         // create contact object
       
   702         CESMRClsItem* item = CESMRClsItem::NewLC();
       
   703         item->SetDisplayNameL( displayname->Des() );
       
   704         item->SetEmailAddressL( *selectedEmailAddress );
       
   705         //check number of email addreses
       
   706         GetContactFieldsL( iEmailFields, emailAddresses, aContact );
       
   707         if ( emailAddresses.Count() > 1 )
       
   708             {
       
   709             item->SetMultipleEmails();
       
   710             }
       
   711         CleanupStack::Pop( item );
       
   712         iContactObjects.Append( item );
       
   713 
       
   714         CleanupStack::PopAndDestroy( selectedEmailAddress );
       
   715         selectedEmailAddress = NULL;
       
   716 
       
   717         CleanupStack::PopAndDestroy( displayname );
       
   718         CleanupStack::PopAndDestroy( &emailAddresses );
       
   719         CleanupStack::PopAndDestroy( &lastname );
       
   720         CleanupStack::PopAndDestroy( &firstname );
       
   721         CleanupStack::PopAndDestroy( aContact );
       
   722 
       
   723         TInt index = (iLinksSet && iCurrentLink ? iLinksSet->Find(*iCurrentLink) + 1 : 0);
       
   724         if (iLinksSet && index < iLinksSet->Count())
       
   725             {
       
   726             iCurrentLink = &iLinksSet->At(index);
       
   727             delete iLinkOperationFetch;
       
   728             iLinkOperationFetch = NULL;
       
   729             iLinkOperationFetch = iContactManager->RetrieveContactL(
       
   730                 *iCurrentLink, *this);
       
   731             }
       
   732         else
       
   733             {
       
   734             delete iLinkOperationFetch;
       
   735             iLinkOperationFetch = NULL;
       
   736             iCurrentLink = NULL;
       
   737             iState = EContactHandlerIdle;
       
   738             delete iLinksSet;
       
   739             iLinksSet = NULL;
       
   740 
       
   741             if ( iHandlerObserver )
       
   742                 {
       
   743                 iHandlerObserver->OperationCompleteL( EGetAddressesFromPhonebook, &iContactObjects );
       
   744                 iHandlerObserver = NULL;
       
   745                 }
       
   746             }
       
   747         }
       
   748     }
       
   749 
       
   750 //EOF
       
   751