messagingappbase/msgeditor/appuisrc/MsgCheckNames.cpp
branchRCL_3
changeset 27 7fdbb852d323
equal deleted inserted replaced
26:ebe688cedc25 27:7fdbb852d323
       
     1 /*
       
     2 * Copyright (c) 2002 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:   Alias name matching functionality.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "MsgCheckNames.h"
       
    22 
       
    23 #include <eiklbv.h>
       
    24 #include <aknlistquerydialog.h>
       
    25 #include <CPbkContactEngine.h>       // CPbkContactEngine
       
    26 #include <CPbkIdleFinder.h>          // CPbkIdleFinder
       
    27 #include <CPbkContactItem.h>         // Phonebook Contact
       
    28 #include <cntdef.h>                  //CheckNames
       
    29 #include <CPbkSmsAddressSelect.h>    //CheckNames
       
    30 #include <CPbkEmailAddressSelect.h>  //CheckNames
       
    31 #include <CPbkMmsAddressSelect.h>    //CheckNames
       
    32 #include <MsgAddressControl.h>       // CMsgRecipientArray/CheckNames
       
    33 
       
    34 #include <MsgEditorAppUi.rsg>        // resource identifiers
       
    35 
       
    36 #include <VPbkContactStoreUris.h>
       
    37 #include <TVPbkFieldVersitProperty.h>
       
    38 #include <MVPbkContactFieldTextData.h>
       
    39 #include <MVPbkStoreContact.h>
       
    40 #include <RVPbkContactFieldDefaultPriorities.h>
       
    41 #include <TVPbkContactStoreUriPtr.h>
       
    42 #include <CPbk2StoreConfiguration.h>
       
    43 #include <MVPbkContactLink.h>
       
    44 #include <CVPbkFieldTypeRefsList.h>
       
    45 #include <CVPbkContactStoreUriArray.h>
       
    46 
       
    47 #include <CVPbkFieldTypeSelector.h>
       
    48 #include <VPbkContactViewFilterBuilder.h>
       
    49 #include <AiwContactSelectionDataTypes.h>
       
    50 
       
    51 #include <TVPbkWordParserCallbackParam.h>
       
    52 #include <AiwContactSelectionDataTypes.h>
       
    53 #include <RVPbkContactFieldDefaultPriorities.h>
       
    54 
       
    55 #include <featmgr.h>
       
    56 
       
    57 // MACROS
       
    58 _LIT( KItemStart, "1\t" );    //For creation of checkbox list
       
    59 
       
    60 const TUint KLatinCharEnd = 0xFF;
       
    61 
       
    62 _LIT( KPanicText, "Check names -panic" );
       
    63 
       
    64 
       
    65 // ============================= LOCAL FUNCTIONS ===============================
       
    66 
       
    67 // ---------------------------------------------------------
       
    68 // FindWordSplitterL
       
    69 // ---------------------------------------------------------
       
    70 //
       
    71 TInt FindWordSplitterL( TAny* aParams )
       
    72 	{
       
    73 	TVPbkWordParserCallbackParam* parser = 
       
    74         static_cast<TVPbkWordParserCallbackParam*>( aParams );
       
    75         
       
    76     const TText* ptr = parser->iStringToParse->Ptr();
       
    77 	const TText* end = ptr + parser->iStringToParse->Length();
       
    78 
       
    79 	const TText* startOfWord=NULL;
       
    80 	FOREVER
       
    81 		{
       
    82 		if ( ptr==end || TChar(*ptr).IsSpace() )
       
    83 			{
       
    84 			if ( startOfWord )
       
    85 				{
       
    86 				TPtrC addWord( startOfWord,ptr - startOfWord );
       
    87 				parser->iWordArray->AppendL( addWord );
       
    88 				startOfWord = NULL;
       
    89 				}
       
    90 			if ( ptr == end )
       
    91                 {
       
    92 				break;
       
    93                 }
       
    94 			}
       
    95 		else if ( !startOfWord )
       
    96             {
       
    97 			startOfWord = ptr;
       
    98             }
       
    99 		ptr++;
       
   100 		}
       
   101 	return( KErrNone );
       
   102 	}
       
   103 
       
   104 
       
   105 
       
   106 // ============================ MEMBER FUNCTIONS ===============================
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CMsgCheckNames::CMsgCheckNames
       
   110 //
       
   111 // C++ default constructor can NOT contain any code, that
       
   112 // might leave.
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 CMsgCheckNames::CMsgCheckNames()
       
   116     {
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CMsgCheckNames::ConstructL
       
   121 //
       
   122 // Symbian 2nd phase constructor can leave.
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CMsgCheckNames::ConstructL()
       
   126     {
       
   127     iFoundContacts = CVPbkContactLinkArray::NewL();
       
   128   
       
   129     iStoreConfiguration = CPbk2StoreConfiguration::NewL();
       
   130     
       
   131     CVPbkContactStoreUriArray* storeArray = iStoreConfiguration->SearchStoreConfigurationL();
       
   132     CleanupStack::PushL( storeArray );
       
   133            
       
   134     iCommonUtils = CContactMatcher::NewL( &CCoeEnv::Static()->FsSession() );//parameter for RFs&
       
   135     iCommonUtils->OpenStoreL( *storeArray );
       
   136     iServiceHandler = CAiwServiceHandler::NewL();
       
   137     iServiceHandler->AttachL ( R_CHECKNAMES_FETCH_INTEREST );
       
   138     
       
   139     iStoreConfiguration->AddObserverL( *this );
       
   140     
       
   141     CleanupStack::PopAndDestroy( storeArray );
       
   142     
       
   143     FeatureManager::InitializeLibL();
       
   144     
       
   145     if ( FeatureManager::FeatureSupported( KFeatureIdChinese ) )
       
   146         {
       
   147         iUseAdvancedMatch = ETrue;
       
   148         }
       
   149     
       
   150     FeatureManager::UnInitializeLib();
       
   151     }
       
   152 
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CMsgCheckNames::NewL
       
   156 //
       
   157 // Two-phased constructor.
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 EXPORT_C CMsgCheckNames* CMsgCheckNames::NewL()
       
   161     {
       
   162     CMsgCheckNames* self = CMsgCheckNames::NewLC();
       
   163     CleanupStack::Pop( self );
       
   164 
       
   165     return self;
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CMsgCheckNames::NewLC
       
   170 //
       
   171 // Factory method
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 EXPORT_C CMsgCheckNames* CMsgCheckNames::NewLC()
       
   175     {
       
   176 	CMsgCheckNames* self = new (ELeave) CMsgCheckNames();	
       
   177 	CleanupStack::PushL(self);
       
   178 
       
   179 	self->ConstructL();
       
   180 	
       
   181 	return self;
       
   182     }
       
   183     
       
   184 // -----------------------------------------------------------------------------
       
   185 // CMsgCheckNames::~CMsgCheckNames
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 EXPORT_C CMsgCheckNames::~CMsgCheckNames()
       
   189     {
       
   190     delete iFoundContacts;
       
   191     delete iCommonUtils;
       
   192     
       
   193     if ( iWait.IsStarted() )
       
   194         {
       
   195         iWait.AsyncStop();         
       
   196         }
       
   197         
       
   198     delete iServiceHandler;
       
   199     delete iStoreConfiguration;
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CMsgCheckNames::FindAndCheckByNameL
       
   204 //
       
   205 // Finds the contact items that match the given search string, checks if they
       
   206 // are valid and if there are more than one valid matches shows them in a dialog
       
   207 // where user can select one or more of the contacts. If shudown flag is set 
       
   208 // leaves with KLeaveExit code to make the application to shutdown
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 EXPORT_C TBool CMsgCheckNames::FindAndCheckByNameL( const TDesC& aText, 
       
   212                                                     TMsgAddressSelectType aAddressSelectType,
       
   213                                                     CMsgRecipientArray& aRecipientArray, 
       
   214                                                     TInt aIndex )
       
   215     {
       
   216     TBool returnValue = EFalse;
       
   217     
       
   218     TAiwAddressSelectType addressType = ConvertToPbk2Type( aAddressSelectType );
       
   219     
       
   220     CVPbkFieldTypeRefsList* fieldTypes = CVPbkFieldTypeRefsList::NewL();
       
   221     CleanupStack::PushL( fieldTypes );
       
   222 
       
   223     DefineSearchFieldsL( *fieldTypes ); 
       
   224 
       
   225     iFoundContacts->ResetAndDestroy();
       
   226 
       
   227     //search the given string from the fields
       
   228     CDesCArray* wordArray = SplitFindStringL( aText );
       
   229     CleanupStack::PushL( wordArray );
       
   230     TCallBack findParser( FindWordSplitterL );
       
   231     
       
   232     iCommonUtils->MatchDataL( *wordArray, *fieldTypes, *iFoundContacts, findParser );
       
   233         
       
   234     CleanupStack::PopAndDestroy( wordArray );
       
   235     
       
   236     // If simple matching did not find any result and advanced flag is
       
   237     // on perform advanced matching.
       
   238     if ( iUseAdvancedMatch &&
       
   239          iFoundContacts &&
       
   240          iFoundContacts->Count() == 0 &&
       
   241          UseAdvancedMatch( aText ) )
       
   242         {
       
   243         PerformAdvancedMatchL( aText, fieldTypes );
       
   244         }
       
   245      // Coverty fix, Forward NULL , http://ousrv057/cov.cgi?cid=35690 
       
   246     TInt contactAmount = 0;  
       
   247     if(iFoundContacts)
       
   248         {
       
   249         contactAmount = iFoundContacts->Count();
       
   250       	}
       
   251 
       
   252     // Offer the user a list of matching contacts
       
   253     if ( contactAmount > 1 )
       
   254         {                         
       
   255         CDesCArray* items = new ( ELeave ) CDesCArrayFlat(4);
       
   256         CleanupStack::PushL( items );
       
   257        
       
   258         CVPbkContactLinkArray* helperArray =  CVPbkContactLinkArray::NewL();
       
   259         CleanupStack::PushL( helperArray );
       
   260 
       
   261         //go through every contact, search the names for them and insert into items array
       
   262         for ( TInt i = 0; i<contactAmount; i++ )
       
   263             {
       
   264             MVPbkStoreContact* storeContact = NULL;
       
   265             iCommonUtils->GetStoreContactL( iFoundContacts->At(i), &storeContact ); 
       
   266             storeContact->PushL();
       
   267             
       
   268             if ( KErrNotFound != iCommonUtils->ContactHasFieldOfTypeL( addressType, *storeContact ) ) 
       
   269                 {
       
   270                 MVPbkStoreContactFieldCollection& fieldCollection = storeContact->Fields();
       
   271                 
       
   272                 HBufC* contactName = iCommonUtils->GetNameL( fieldCollection );
       
   273                 if ( contactName )
       
   274                     {
       
   275                     contactName = contactName->ReAllocL( contactName->Des().Length()+2 );
       
   276                     CleanupStack::PushL( contactName );
       
   277                 
       
   278                     TPtr contactNamePtr = contactName->Des();
       
   279 
       
   280                     /*
       
   281                      * Fix the split contact name issue::ECYU-7FPC93.
       
   282                      * Replace any enter-key characters with space characters.
       
   283                      * Start
       
   284                      */
       
   285                     
       
   286                     TInt enterKeyPos;
       
   287                     TChar enterChar( CEditableText::EParagraphDelimiter );
       
   288                     TChar spaceChar( CEditableText::ESpace );
       
   289                     TInt nameLength = contactNamePtr.Length();
       
   290                     
       
   291                     enterKeyPos = contactNamePtr.Locate(enterChar);
       
   292                     while( enterKeyPos != KErrNotFound && enterKeyPos < nameLength )
       
   293                         {
       
   294                         contactNamePtr[enterKeyPos] = spaceChar;
       
   295                         enterKeyPos = contactNamePtr.Locate(enterChar);
       
   296                         }
       
   297                     // Fix: End
       
   298                     // We need to add the checkbox
       
   299                     contactNamePtr.Insert( 0, KItemStart );
       
   300             
       
   301                     // Pay attention to the order of the items
       
   302                     TInt pos = items->InsertIsqAllowDuplicatesL( contactNamePtr, ECmpCollated ); 
       
   303                     CleanupStack::PopAndDestroy( contactName );
       
   304                 
       
   305                     const MVPbkContactLink& preservedLink = iFoundContacts->At( i );
       
   306                 
       
   307                     MVPbkContactLink* link = preservedLink.CloneLC();
       
   308                     helperArray->InsertL( link, pos );
       
   309                 
       
   310                     CleanupStack::Pop();
       
   311                     }
       
   312                 }
       
   313             
       
   314             CleanupStack::PopAndDestroy( storeContact );
       
   315             }
       
   316             
       
   317         iFoundContacts->ResetAndDestroy();
       
   318         
       
   319         if ( items->Count() )
       
   320             {
       
   321             CListBoxView::CSelectionIndexArray* matchingRecipients = 
       
   322                             new ( ELeave ) CArrayFixFlat<TInt>( contactAmount );
       
   323             CleanupStack::PushL( matchingRecipients );
       
   324             
       
   325             CAknListQueryDialog* dlg = new (ELeave) CAknListQueryDialog( matchingRecipients );
       
   326             dlg->PrepareLC( R_MEB_CHECKNAMES_QUERY );
       
   327             dlg->SetItemTextArray( items );
       
   328             dlg->SetOwnershipType( ELbmDoesNotOwnItemArray );
       
   329 
       
   330             if ( dlg->RunLD() )
       
   331                 {
       
   332                 // We must now read which one / ones of the contacts the user selected
       
   333                 const TInt recipientCount = matchingRecipients->Count();
       
   334                 MVPbkContactLink* link = NULL;
       
   335                 
       
   336                 for ( TInt i = 0; i < recipientCount; ++i )
       
   337                     {
       
   338                     // Copying entries
       
   339                     link = helperArray->At( matchingRecipients->At( i ) ).CloneLC();
       
   340                     
       
   341                     // Inserts entry, shifts original items
       
   342                     iFoundContacts->InsertL( link, i );
       
   343                     CleanupStack::Pop();
       
   344                     } 
       
   345                 }
       
   346             CleanupStack::PopAndDestroy(); // matchingRecipients
       
   347             }
       
   348         CleanupStack::PopAndDestroy( 2, items);//helperArray
       
   349         }
       
   350 
       
   351     CleanupStack::PopAndDestroy( fieldTypes ); // fieldtypes
       
   352     
       
   353     if ( iFoundContacts->Count() > 0 )  
       
   354         {
       
   355         ExecuteSingleItemFetchL( 
       
   356                 TAiwSingleItemSelectionDataV1().SetAddressSelectType( addressType ) );
       
   357         
       
   358         CAknInputBlock* inputBlock = CAknInputBlock::NewCancelHandlerLC( this );
       
   359         
       
   360         iWait.Start(); // Make this whole op look like synchronous
       
   361         
       
   362         CleanupStack::PopAndDestroy( inputBlock );
       
   363         
       
   364         if ( iShutdown )
       
   365             {
       
   366             User::Leave( KLeaveExit );
       
   367             }
       
   368         
       
   369         if ( iFoundContacts->Count() > 0 )//the process might have been cancelled
       
   370             {
       
   371             PutContactsToArrayL( aRecipientArray, aIndex );
       
   372             returnValue = ETrue;    
       
   373             }
       
   374         }
       
   375         
       
   376     //return false if no contacts        
       
   377     return returnValue;
       
   378     }
       
   379 
       
   380 // ----------------------------------------------------------------------------
       
   381 // CMsgCheckNames::FetchRecipientsL
       
   382 //
       
   383 // Fetches the recipients, and searches the address from the selected recipients
       
   384 // Starts input blocker to "eat" all key press events to editor during
       
   385 // phonebook operations. If shudown flag is set leaves with KLeaveExit code
       
   386 // to make the application to shutdown
       
   387 // ----------------------------------------------------------------------------    
       
   388 //
       
   389 EXPORT_C void CMsgCheckNames::FetchRecipientsL( CMsgRecipientArray& aRecipientList,
       
   390                                                 TMsgAddressSelectType aAddressSelectType )
       
   391     {
       
   392     TAiwAddressSelectType addressType = ConvertToPbk2Type( aAddressSelectType );
       
   393     
       
   394     // Construct empty filter
       
   395     CVPbkFieldTypeSelector* contactViewFilter =
       
   396         CVPbkFieldTypeSelector::NewL( iCommonUtils->FieldTypes() );
       
   397     CleanupStack::PushL( contactViewFilter );
       
   398 
       
   399     // Append the filter object with suitable criteria            
       
   400     if ( addressType ==  EAiwEMailSelect )            
       
   401         {
       
   402         VPbkContactViewFilterBuilder::BuildContactViewFilterL( *contactViewFilter, 
       
   403                                                                EVPbkContactViewFilterEmail,
       
   404                                                                iCommonUtils->GetContactManager() );
       
   405         }
       
   406             
       
   407     else if ( addressType ==  EAiwMMSSelect )
       
   408         {
       
   409         VPbkContactViewFilterBuilder::BuildContactViewFilterL( *contactViewFilter, 
       
   410                                                                EVPbkContactViewFilterPhoneNumber,
       
   411                                                                iCommonUtils->GetContactManager() );
       
   412                     
       
   413         VPbkContactViewFilterBuilder::BuildContactViewFilterL( *contactViewFilter, 
       
   414                                                                EVPbkContactViewFilterEmail,
       
   415                                                                iCommonUtils->GetContactManager() );
       
   416         }
       
   417             
       
   418     else
       
   419         {
       
   420         VPbkContactViewFilterBuilder::BuildContactViewFilterL( *contactViewFilter, 
       
   421                                                                EVPbkContactViewFilterPhoneNumber,
       
   422                                                                iCommonUtils->GetContactManager() );
       
   423         }
       
   424     
       
   425     iFoundContacts->ResetAndDestroy();
       
   426     
       
   427     TAiwMultipleItemSelectionDataV1 selectionData;
       
   428     selectionData.SetFetchFilter( contactViewFilter );
       
   429     selectionData.SetAddressSelectType( addressType );
       
   430             
       
   431     ExecuteMultipleItemFetchL( selectionData );
       
   432     
       
   433     CleanupStack::PopAndDestroy( contactViewFilter );
       
   434     
       
   435     CAknInputBlock* inputBlock = CAknInputBlock::NewCancelHandlerLC( this );
       
   436     
       
   437     iWait.Start();
       
   438     
       
   439     if ( iFoundContacts->Count() > 0 )
       
   440         {
       
   441         // The process might have been cancelled in which case there
       
   442         // will not be any contacts.
       
   443         PutContactsToArrayL( aRecipientList, -1 );
       
   444         }
       
   445     
       
   446     CleanupStack::PopAndDestroy( inputBlock );    
       
   447     
       
   448     if ( iShutdown )
       
   449         {
       
   450         User::Leave( KLeaveExit );
       
   451         }
       
   452     }
       
   453 
       
   454 // ----------------------------------------------------------------------------
       
   455 // CMsgCheckNames::FetchRecipientsL
       
   456 //
       
   457 // Fetches the recipients. If shudown flag is set leaves with KLeaveExit code
       
   458 // to make the application to shutdown
       
   459 // ----------------------------------------------------------------------------    
       
   460 //
       
   461 EXPORT_C void CMsgCheckNames::FetchRecipientsL( CVPbkContactLinkArray& aContactLinkArray,
       
   462                                                 TMsgAddressSelectType /*aAddressSelectType*/ )
       
   463     {
       
   464     ExecuteMultipleEntryFetchL( TAiwMultipleEntrySelectionDataV2().SetFetchFilter( NULL ).SetFlags( 0 ) );
       
   465     
       
   466     CAknInputBlock* inputBlock = CAknInputBlock::NewCancelHandlerLC( this );
       
   467     
       
   468     iWait.Start();  // Wait for the user to check off the names.
       
   469     
       
   470     CleanupStack::PopAndDestroy( inputBlock );
       
   471     
       
   472     if ( iShutdown )
       
   473         {
       
   474         User::Leave( KLeaveExit );
       
   475         }
       
   476     
       
   477     if ( iFoundContacts->Count() )
       
   478         {
       
   479         for ( TInt n = 0; n < iFoundContacts->Count(); n++ )
       
   480             {
       
   481             aContactLinkArray.AppendL( iFoundContacts->At( n ).CloneLC() );
       
   482             CleanupStack::Pop();    // Cloned link.
       
   483             }
       
   484         }
       
   485     }
       
   486     
       
   487 // ----------------------------------------------------
       
   488 // CMsgCheckNames::GetAlias
       
   489 // ----------------------------------------------------
       
   490 //
       
   491  
       
   492 EXPORT_C void CMsgCheckNames::GetAliasL(const TDesC& aNumber,TDes& aAlias,TInt aMaxLength)
       
   493     {                           
       
   494          // Match contacts to the from address
       
   495         CVPbkContactLinkArray* linkArray = CVPbkContactLinkArray::NewLC();
       
   496         iCommonUtils->MatchPhoneNumberL(aNumber,7,CVPbkPhoneNumberMatchStrategy::EVPbkStopOnFirstMatchFlag,*linkArray );
       
   497         TInt tmpcnt = linkArray->Count();
       
   498         if ( linkArray->Count() > 0 )
       
   499            {
       
   500             MVPbkStoreContact* contact;
       
   501             iCommonUtils->GetStoreContactL(linkArray->At(0), &contact);
       
   502             contact->PushL();
       
   503          
       
   504             MVPbkStoreContactFieldCollection& coll = contact->Fields();       
       
   505             HBufC* alias = iCommonUtils->GetNameL( coll );                        
       
   506             
       
   507             if ( alias )
       
   508                 {
       
   509                  if ( alias->Length() > aMaxLength )
       
   510                     {
       
   511                     aAlias = alias->Left( aMaxLength );
       
   512                     }        
       
   513                 else
       
   514                     {
       
   515                     aAlias = *alias;
       
   516                     }
       
   517                 delete alias;
       
   518                 alias = NULL;
       
   519                 }                           
       
   520             CleanupStack::PopAndDestroy( contact ); // contact
       
   521             }
       
   522         else            
       
   523            {
       
   524            aAlias = KNullDesC();
       
   525            }
       
   526         CleanupStack::PopAndDestroy( 1); //  linkArray, ,        
       
   527     }
       
   528 
       
   529 // ----------------------------------------------------------------------------
       
   530 // CMsgCheckNames::HandleNotifyL
       
   531 // ----------------------------------------------------------------------------
       
   532 //
       
   533 TInt CMsgCheckNames::HandleNotifyL( TInt aCmdId,
       
   534                                     TInt aEventId,
       
   535                                     CAiwGenericParamList& aEventParamList,
       
   536                                     const CAiwGenericParamList& /*aInParamList*/ )
       
   537     {
       
   538     // the trapd is to enable stopping the iWait in case of a leave
       
   539     // trapping a lot of code to make future extensions easier
       
   540     __ASSERT_ALWAYS( iFoundContacts, User::Panic(KPanicText,0) );
       
   541     
       
   542     TInt retVal = KErrNone;
       
   543     
       
   544     TRAPD( err, 
       
   545         {
       
   546         iFoundContacts->ResetAndDestroy();
       
   547         if ( aEventId == KAiwEventCompleted )
       
   548             {
       
   549             TInt index = 0;
       
   550             const TAiwGenericParam* param =
       
   551                 aEventParamList.FindFirst( index, EGenericParamContactLinkArray );
       
   552             if ( param )
       
   553                 {
       
   554                 TPtrC8 contactLinks = param->Value().AsData();
       
   555                 if ( contactLinks.Length() ) // If the event was cancelled
       
   556                     {
       
   557                     CVPbkContactLinkArray* links = 
       
   558                             CVPbkContactLinkArray::NewLC( contactLinks, 
       
   559                                                           iCommonUtils->GetContactStoresL() );
       
   560                     CleanupStack::Pop(); //links
       
   561                     
       
   562                     // Replace iFoundContacts
       
   563                     delete iFoundContacts;
       
   564                     iFoundContacts = links;                    
       
   565                     }
       
   566                 else
       
   567                     {
       
   568                     retVal = KErrArgument;    
       
   569                     }
       
   570                 }
       
   571             else 
       
   572                 {
       
   573                 retVal = KErrArgument;
       
   574                 }  
       
   575             }
       
   576         else if ( aEventId == KAiwEventOutParamCheck )
       
   577             {
       
   578             if ( aCmdId == KAiwCmdSelect )
       
   579                 {
       
   580                 retVal = 1;//this value going back to aiw-component to inform param to be ok
       
   581                 }
       
   582             }
       
   583 
       
   584         else 
       
   585             {
       
   586             retVal = KErrCompletion;
       
   587             }
       
   588             
       
   589         if ( retVal != 1 && iWait.IsStarted() )
       
   590             {
       
   591             iWait.AsyncStop();         
       
   592             }
       
   593         } );
       
   594 
       
   595     if ( err != KErrNone )
       
   596         {
       
   597         if ( iWait.IsStarted() )
       
   598             {
       
   599             iWait.AsyncStop();         
       
   600             }
       
   601         User::Leave( err );
       
   602         }
       
   603         
       
   604     return retVal;        
       
   605     }                    
       
   606     
       
   607 
       
   608 // ----------------------------------------------------
       
   609 // CMsgCheckNames::ExecuteMultipleEntryFetchL
       
   610 //
       
   611 // Uses aiw to fetch contact(s) from vpbk
       
   612 // ----------------------------------------------------
       
   613 //   
       
   614 void CMsgCheckNames::ExecuteMultipleEntryFetchL( TAiwMultipleEntrySelectionDataV2 aData )
       
   615     {
       
   616     CAiwGenericParamList& inParamList = iServiceHandler->InParamListL();
       
   617     inParamList.AppendL( TAiwGenericParam( 
       
   618                                 EGenericParamContactSelectionData,
       
   619                                 TAiwVariant( TAiwMultipleEntrySelectionDataV2Pckg( aData ) ) ) );
       
   620             
       
   621     iServiceHandler->ExecuteServiceCmdL( KAiwCmdSelect, 
       
   622                                          inParamList, 
       
   623                                          iServiceHandler->OutParamListL(),
       
   624                                          0,
       
   625                                          this );            
       
   626     }
       
   627 
       
   628 // ----------------------------------------------------------------------------
       
   629 // CMsgCheckNames::ExecuteSingleItemFetchL
       
   630 // ----------------------------------------------------------------------------
       
   631 //
       
   632 void CMsgCheckNames::ExecuteSingleItemFetchL( TAiwSingleItemSelectionDataV1 aData )
       
   633     {
       
   634     RVPbkContactFieldDefaultPriorities defaultPriorities;
       
   635     CleanupClosePushL( defaultPriorities );    
       
   636     
       
   637     SetDefaultPrioritiesL( aData.AddressSelectType(), defaultPriorities );
       
   638     
       
   639     aData.SetDefaultPriorities( defaultPriorities );
       
   640         
       
   641     CAiwGenericParamList& inParamList = iServiceHandler->InParamListL();    
       
   642     inParamList.AppendL( TAiwGenericParam( 
       
   643                                 EGenericParamContactSelectionData,
       
   644                                 TAiwVariant( TAiwSingleItemSelectionDataV1Pckg( aData ) ) ) );
       
   645 
       
   646     // the validity of iFoundContacts is checkeck by the calling function
       
   647     HBufC8* packedLinks = iFoundContacts->PackLC();
       
   648         
       
   649     inParamList.AppendL( TAiwGenericParam( EGenericParamContactLinkArray,
       
   650                                            TAiwVariant( *packedLinks ) ) );
       
   651 
       
   652     iServiceHandler->ExecuteServiceCmdL( KAiwCmdSelect, 
       
   653                                          inParamList, 
       
   654                                          iServiceHandler->OutParamListL(),
       
   655                                          0,
       
   656                                          this );
       
   657     CleanupStack::PopAndDestroy( 2, &defaultPriorities ); 
       
   658     }
       
   659 
       
   660 // ----------------------------------------------------------------------------
       
   661 // CMsgCheckNames::ExecuteMultipleItemFetchL
       
   662 // ----------------------------------------------------------------------------
       
   663 //
       
   664 void CMsgCheckNames::ExecuteMultipleItemFetchL( TAiwMultipleItemSelectionDataV1& aData )
       
   665     {
       
   666     RVPbkContactFieldDefaultPriorities defaultPriorities;
       
   667     CleanupClosePushL( defaultPriorities );    
       
   668     
       
   669     SetDefaultPrioritiesL( aData.AddressSelectType(), defaultPriorities );
       
   670     
       
   671     aData.SetDefaultPriorities( defaultPriorities );
       
   672         
       
   673     CAiwGenericParamList& inParamList = iServiceHandler->InParamListL();    
       
   674     inParamList.AppendL( TAiwGenericParam( 
       
   675                                 EGenericParamContactSelectionData,
       
   676                                 TAiwVariant( TAiwMultipleItemSelectionDataV1Pckg( aData ) ) ) );
       
   677 
       
   678     // the validity of iFoundContacts is checkeck by the calling function
       
   679     HBufC8* packedLinks = iFoundContacts->PackLC();
       
   680         
       
   681     inParamList.AppendL( TAiwGenericParam( EGenericParamContactLinkArray,
       
   682                                            TAiwVariant( *packedLinks ) ) );
       
   683 
       
   684     iServiceHandler->ExecuteServiceCmdL( KAiwCmdSelect, 
       
   685                                          inParamList, 
       
   686                                          iServiceHandler->OutParamListL(),
       
   687                                          0,
       
   688                                          this );
       
   689     CleanupStack::PopAndDestroy( 2, &defaultPriorities ); 
       
   690     }
       
   691     
       
   692 // ----------------------------------------------------------------------------
       
   693 // CMsgCheckNames::PutContactsToArrayL
       
   694 // ----------------------------------------------------------------------------
       
   695 //
       
   696 void CMsgCheckNames::PutContactsToArrayL( CMsgRecipientArray& aRecipientArray, TInt aIndex )
       
   697     {
       
   698     __ASSERT_ALWAYS( iFoundContacts, User::Panic(KPanicText,0) );
       
   699     //delete the original entry, if there was one        
       
   700     if ( aIndex > -1 && 
       
   701          aIndex < aRecipientArray.Count() )
       
   702         {
       
   703         delete aRecipientArray.At( aIndex );
       
   704         aRecipientArray.Delete( aIndex );   
       
   705         }
       
   706     else
       
   707         {
       
   708         aIndex = 0;
       
   709         }
       
   710         
       
   711     MVPbkStoreContact* storeContact = NULL;
       
   712     MVPbkStoreContactField* selectedField = NULL;
       
   713     TInt recipientsNotHandled = iFoundContacts->Count();
       
   714        
       
   715     for ( TInt i = 0; recipientsNotHandled > i; i++ )
       
   716         {
       
   717         const MVPbkContactLink& changedContactLink = iFoundContacts->At( i );
       
   718         iCommonUtils->GetStoreContactL( changedContactLink, &storeContact ); 
       
   719         storeContact->PushL();
       
   720         MVPbkStoreContactFieldCollection& fieldCollection = storeContact->Fields();  
       
   721 
       
   722         selectedField = fieldCollection.RetrieveField( iFoundContacts->At(i) );
       
   723 
       
   724         if ( selectedField )//gets the selected field and writes it to the reciItem
       
   725             {
       
   726             CMsgRecipientItem* reciItem = CMsgRecipientItem::NewLC( KNullDesC, KNullDesC ); 
       
   727             
       
   728             // Set field data
       
   729             const MVPbkContactFieldTextData* textData =
       
   730                 &MVPbkContactFieldTextData::Cast( selectedField->FieldData() );
       
   731                 
       
   732             reciItem->SetAddressL( textData->Text() );
       
   733             
       
   734             HBufC* contactName = iCommonUtils->GetNameL( fieldCollection );
       
   735             if ( contactName )
       
   736                 {
       
   737                 reciItem->SetNameL( contactName->Des() );
       
   738                 delete contactName;
       
   739                 }
       
   740                 
       
   741             reciItem->SetContactLink( &changedContactLink );
       
   742             aRecipientArray.InsertL( aIndex, reciItem );
       
   743             
       
   744             CleanupStack::Pop( reciItem );
       
   745             aIndex++;
       
   746             }
       
   747         CleanupStack::PopAndDestroy( storeContact );
       
   748         }
       
   749     }
       
   750 
       
   751 // -----------------------------------------------------------------------------
       
   752 // CMsgCheckNames::SplitFindStringL
       
   753 //
       
   754 // Divides search string into several pieces if it includes spaces.
       
   755 // -----------------------------------------------------------------------------
       
   756 //
       
   757 CDesCArray* CMsgCheckNames::SplitFindStringL(const TDesC& aFindString)
       
   758     {
       
   759 	CDesCArray* wordArray = new ( ELeave ) CDesCArrayFlat( 10 );
       
   760 	CleanupStack::PushL( wordArray );	
       
   761 	
       
   762     TVPbkWordParserCallbackParam parser( &aFindString, wordArray );
       
   763 	FindWordSplitterL( &parser );
       
   764 	
       
   765 	CleanupStack::Pop(); // wordArray
       
   766 	return parser.iWordArray;
       
   767     }
       
   768     
       
   769 // -----------------------------------------------------------------------------
       
   770 // CMsgCheckNames::IdleFindCallback
       
   771 //
       
   772 // Asynchronous find observer callback.
       
   773 // -----------------------------------------------------------------------------
       
   774 //
       
   775 void CMsgCheckNames::IdleFindCallback()
       
   776     {
       
   777     }
       
   778 
       
   779 // -----------------------------------------------------------------------------
       
   780 // CMsgCheckNames::DefineSearchFieldsL
       
   781 //
       
   782 // Defines that from which fiels the data is searched from 
       
   783 // -----------------------------------------------------------------------------
       
   784 //
       
   785 void CMsgCheckNames::DefineSearchFieldsL( CVPbkFieldTypeRefsList& aOwnList )    
       
   786     {
       
   787     const MVPbkFieldTypeList& refList = iCommonUtils->FieldTypes();
       
   788     TVPbkFieldVersitProperty prop;
       
   789     prop.SetName( EVPbkVersitNameN ); 
       
   790     prop.SetSubField( EVPbkVersitSubFieldFamilyName ); 
       
   791     const MVPbkFieldType* foundType = refList.FindMatch( prop, 0 );
       
   792     
       
   793     if ( foundType )
       
   794         {
       
   795         aOwnList.AppendL( *foundType );   
       
   796         }
       
   797     
       
   798     prop.SetSubField( EVPbkVersitSubFieldGivenName );
       
   799     
       
   800     foundType = refList.FindMatch( prop, 0 );
       
   801     if ( foundType )
       
   802         {
       
   803         aOwnList.AppendL( *foundType );   
       
   804         }
       
   805     
       
   806     prop.SetName( EVPbkVersitNameORG );
       
   807     prop.SetSubField( EVPbkVersitSubFieldOrgName );
       
   808     
       
   809     foundType = refList.FindMatch( prop, 0 );
       
   810     if ( foundType )
       
   811         {
       
   812         aOwnList.AppendL( *foundType );   
       
   813         }
       
   814     }
       
   815 
       
   816 // -----------------------------------------------------------------------------
       
   817 // CMsgCheckNames::ConvertToPbk2Type
       
   818 //
       
   819 // Defines that from which fiels the data is searched from 
       
   820 // -----------------------------------------------------------------------------
       
   821 //
       
   822 TAiwAddressSelectType CMsgCheckNames::ConvertToPbk2Type( 
       
   823                                         TMsgAddressSelectType aAddressSelectType )
       
   824    
       
   825     {
       
   826     TAiwAddressSelectType addressType = EAiwPhoneNumberSelect;
       
   827     
       
   828     switch ( aAddressSelectType )
       
   829         {
       
   830         case EMsgTypeSms:
       
   831             {
       
   832             addressType = EAiwPhoneNumberSelect;
       
   833             break;
       
   834             }
       
   835         case EMsgTypeMms:
       
   836             {
       
   837             addressType = EAiwMMSSelect;
       
   838             break;
       
   839             }
       
   840         case EMsgTypeMail:
       
   841             {
       
   842             addressType = EAiwEMailSelect;
       
   843             break;
       
   844             }
       
   845         case EMsgTypeAll:
       
   846             {
       
   847             addressType = EAiwAllItemsSelect;
       
   848             break;
       
   849             }
       
   850         default:
       
   851             {
       
   852             addressType = EAiwPhoneNumberSelect;
       
   853             break;
       
   854             }
       
   855         }
       
   856         
       
   857     return addressType;
       
   858     }
       
   859               
       
   860 // -----------------------------------------------------------------------------
       
   861 // CMsgCheckNames::SetDefaultPrioritiesL
       
   862 // -----------------------------------------------------------------------------
       
   863 //
       
   864 void CMsgCheckNames::SetDefaultPrioritiesL( TAiwAddressSelectType aAddressType, 
       
   865                                             RVPbkContactFieldDefaultPriorities& aPriorities ) const
       
   866     {
       
   867     switch ( aAddressType )
       
   868         {
       
   869         case EAiwMMSSelect:
       
   870             {
       
   871             User::LeaveIfError( aPriorities.Append( EVPbkDefaultTypeMms ) );
       
   872             User::LeaveIfError( aPriorities.Append( EVPbkDefaultTypeSms ) );
       
   873             User::LeaveIfError( aPriorities.Append( EVPbkDefaultTypeEmailOverSms ) );
       
   874             break;
       
   875             }
       
   876         case EAiwPhoneNumberSelect:
       
   877             {
       
   878             User::LeaveIfError( aPriorities.Append( EVPbkDefaultTypeSms ) );
       
   879             break;
       
   880             }
       
   881         case EAiwEMailSelect:
       
   882             {
       
   883             User::LeaveIfError( aPriorities.Append( EVPbkDefaultTypeEmail ) );
       
   884             break;
       
   885             }
       
   886         case EAiwAllItemsSelect:
       
   887             {
       
   888             User::LeaveIfError( aPriorities.Append( EVPbkDefaultTypeMms ) );
       
   889             User::LeaveIfError( aPriorities.Append( EVPbkDefaultTypeSms ) );
       
   890             User::LeaveIfError( aPriorities.Append( EVPbkDefaultTypeEmail ) );
       
   891             User::LeaveIfError( aPriorities.Append( EVPbkDefaultTypeEmailOverSms ) );
       
   892             break;
       
   893             }
       
   894         default:
       
   895             {
       
   896             break;
       
   897             }
       
   898         }
       
   899     }
       
   900 
       
   901 // -----------------------------------------------------------------------------
       
   902 // CMsgCheckNames::UseAdvancedMatch
       
   903 //
       
   904 // If string does not contain spaces and contains only non-latin characters
       
   905 // perform matching advanced match algorithm.
       
   906 // -----------------------------------------------------------------------------
       
   907 //
       
   908 TBool CMsgCheckNames::UseAdvancedMatch( const TDesC& aFindString )
       
   909     {
       
   910     TBool containsSpace( EFalse );
       
   911     TBool nonLatinString( ETrue );
       
   912     
       
   913     for ( TInt current = 0; current < aFindString.Length(); current++ )
       
   914         {
       
   915         TUint currentChar( aFindString[current] );
       
   916         if ( TChar( currentChar ).IsSpace() )
       
   917             {
       
   918             containsSpace = ETrue;
       
   919             }
       
   920             
       
   921         if ( currentChar < KLatinCharEnd )
       
   922             {
       
   923             nonLatinString = EFalse;
       
   924             break;
       
   925             }
       
   926         }
       
   927     
       
   928     return !containsSpace && nonLatinString;
       
   929     }
       
   930 
       
   931 // -----------------------------------------------------------------------------
       
   932 // CMsgCheckNames::PerformAdvancedMatchL
       
   933 //
       
   934 // Perform matching by trying to set space between every character. 
       
   935 // This is done because in written chinese it is common to write contact names without spaces and
       
   936 // symbian's contact DB requires search text as a word list.
       
   937 // -----------------------------------------------------------------------------
       
   938 //
       
   939 void CMsgCheckNames::PerformAdvancedMatchL( const TDesC& aFindString,
       
   940                                             CVPbkFieldTypeRefsList* aFieldTypes )
       
   941     {
       
   942     HBufC* firstBuffer = HBufC::NewLC( aFindString.Length() );
       
   943     HBufC* secondBuffer = HBufC::NewLC( aFindString.Length() );
       
   944     
       
   945     TPtr firstBufferPtr = firstBuffer->Des();
       
   946     TPtr secondBufferPtr = secondBuffer->Des();
       
   947     
       
   948     CDesCArray* wordArray = new( ELeave ) CDesCArrayFlat( 2 );
       
   949 	CleanupStack::PushL( wordArray );
       
   950     
       
   951     TCallBack findParser( FindWordSplitterL );
       
   952     
       
   953     TInt count( Min( aFindString.Length(), 4) );
       
   954     
       
   955     for ( TInt index = 1; index < count; index++ )
       
   956         {
       
   957         firstBufferPtr.Insert( 0, aFindString.Mid( 0, index ) );
       
   958         secondBufferPtr.Insert( 0, aFindString.Mid( index, 
       
   959                                                     aFindString.Length() - index ) );
       
   960         
       
   961         wordArray->AppendL( firstBufferPtr );
       
   962         wordArray->AppendL( secondBufferPtr );
       
   963         
       
   964         iCommonUtils->MatchDataL( *wordArray, 
       
   965                                   *aFieldTypes, 
       
   966                                   *iFoundContacts, 
       
   967                                   findParser );
       
   968         
       
   969         wordArray->Reset();
       
   970         
       
   971         if ( iFoundContacts->Count() != 0 )
       
   972             {
       
   973             break;
       
   974             }
       
   975             
       
   976         firstBufferPtr.Zero();
       
   977         secondBufferPtr.Zero();                
       
   978         }                
       
   979     
       
   980     CleanupStack::PopAndDestroy( wordArray );
       
   981     
       
   982     CleanupStack::PopAndDestroy( secondBuffer );
       
   983     CleanupStack::PopAndDestroy( firstBuffer );
       
   984     }
       
   985 
       
   986 // -----------------------------------------------------------------------------
       
   987 // CMsgCheckNames::AknInputBlockCancel
       
   988 //
       
   989 // Cancels the AIW service and sets the shutdown flag. This is needed since
       
   990 // we must wait for the AIW service to notify us about the cancel before
       
   991 // application can be shutdown. Otherwise AIW service might try to use
       
   992 // freed resources.
       
   993 // -----------------------------------------------------------------------------
       
   994 //
       
   995 void CMsgCheckNames::AknInputBlockCancel()
       
   996     {
       
   997     if ( iWait.IsStarted() )
       
   998         {
       
   999         TRAPD( error, iServiceHandler->ExecuteServiceCmdL( KAiwCmdSelect, 
       
  1000                                                            iServiceHandler->InParamListL(), 
       
  1001                                                            iServiceHandler->OutParamListL(),
       
  1002                                                            KAiwOptCancel,
       
  1003                                                            this ) );
       
  1004         if ( error == KErrNone )
       
  1005             {
       
  1006             iShutdown = ETrue;
       
  1007             }
       
  1008         }
       
  1009     }
       
  1010 
       
  1011 // ---------------------------------------------------------
       
  1012 // CMsgCheckNames::CleanupResetAndDestroy
       
  1013 // ---------------------------------------------------------
       
  1014 //
       
  1015 void CMsgCheckNames::CleanupResetAndDestroy( TAny* aMatchedRecips )
       
  1016 	{
       
  1017 	CMsgRecipientArray* matches = NULL;
       
  1018     
       
  1019     matches = STATIC_CAST( CMsgRecipientArray*, aMatchedRecips );
       
  1020 	matches->ResetAndDestroy();
       
  1021 	
       
  1022 	delete matches;
       
  1023     matches = NULL;
       
  1024 	}
       
  1025 
       
  1026 // ---------------------------------------------------------
       
  1027 // CMsgCheckNames::ConfigurationChanged
       
  1028 // ---------------------------------------------------------
       
  1029 //
       
  1030 void CMsgCheckNames::ConfigurationChanged()
       
  1031     {
       
  1032     }
       
  1033 
       
  1034 // ---------------------------------------------------------
       
  1035 // CMsgCheckNames::ConfigurationChangedComplete
       
  1036 // ---------------------------------------------------------
       
  1037 //
       
  1038 void CMsgCheckNames::ConfigurationChangedComplete()
       
  1039     {
       
  1040     TRAP_IGNORE( DoConfigurationChangedCompleteL() );
       
  1041     }
       
  1042 
       
  1043 // ---------------------------------------------------------
       
  1044 // CMsgCheckNames::DoConfigurationChangedCompleteL
       
  1045 //
       
  1046 // If store configuration has changed (i.e. used memories (phone, SIM etc.)).
       
  1047 // open the currently selected stores.
       
  1048 // ---------------------------------------------------------
       
  1049 //
       
  1050 void CMsgCheckNames::DoConfigurationChangedCompleteL()
       
  1051     {
       
  1052     CVPbkContactStoreUriArray* storeArray = iStoreConfiguration->SearchStoreConfigurationL();
       
  1053     CleanupStack::PushL( storeArray );
       
  1054     
       
  1055     iCommonUtils->CloseStoresL();     
       
  1056     iCommonUtils->OpenStoreL( *storeArray );
       
  1057     
       
  1058     CleanupStack::PopAndDestroy( storeArray );
       
  1059     }
       
  1060 
       
  1061 //  End of File