phonebookui/Phonebook2/ServerApplication/src/CPbk2AttributeAddressSelectPhase.cpp
changeset 0 e686773b3f54
child 18 d4f567ce2e7c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Phonebook 2 server app attribute address select phase.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "CPbk2AttributeAddressSelectPhase.h"
       
    19 
       
    20 // Phonebook 2
       
    21 #include "MPbk2ServicePhaseObserver.h"
       
    22 #include "CPbk2ServerAppAppUi.h"
       
    23 #include "CPbk2KeyEventDealer.h"
       
    24 #include <MPbk2DialogEliminator.h>
       
    25 #include <CPbk2AddressSelect.h>
       
    26 #include <TPbk2AddressSelectParams.h>
       
    27 #include <MPbk2ApplicationServices.h>
       
    28 #include <Pbk2UIControls.rsg>
       
    29 #include <CPbk2StoreManager.h>
       
    30 
       
    31 // Virtual Phonebook
       
    32 #include <MVPbkContactLink.h>
       
    33 #include <CVPbkContactLinkArray.h>
       
    34 #include <MVPbkContactOperationBase.h>
       
    35 #include <MVPbkStoreContact.h>
       
    36 #include <CVPbkContactManager.h>
       
    37 #include <CVPbkSpeedDialAttribute.h>
       
    38 #include <VPbkFieldTypeSelectorFactory.h>
       
    39 #include <CVPbkFieldTypeSelector.h>
       
    40 #include <VPbkFieldTypeSelectors.rsg>
       
    41 #include <VPbkContactViewFilterBuilder.h>
       
    42 
       
    43 // System includes
       
    44 #include <barsread.h>
       
    45 #include <avkon.rsg>
       
    46 #include <featmgr.h>
       
    47 
       
    48 /// Unnamed namespace for local definitions
       
    49 namespace {
       
    50 
       
    51 const TInt KFirstElement = 0;
       
    52 
       
    53 /**
       
    54  * Copies a link array to another.
       
    55  *
       
    56  * @param aSourceLinkArray    Link array which is copied
       
    57  * @param aTargetLinkArray    Links are copied to this
       
    58  */
       
    59 void CopyContactLinksL( const MVPbkContactLinkArray& aSourceLinkArray,
       
    60         CVPbkContactLinkArray& aTargetLinkArray )
       
    61     {
       
    62     const TInt count = aSourceLinkArray.Count();
       
    63     for ( TInt i(0); i < count; ++i )
       
    64         {
       
    65         const MVPbkContactLink& contactLink = aSourceLinkArray.At(i);
       
    66         aTargetLinkArray.AppendL( contactLink.CloneLC() );
       
    67         CleanupStack::Pop(); // link
       
    68         }
       
    69     }
       
    70 
       
    71 } /// namespace
       
    72 
       
    73 // --------------------------------------------------------------------------
       
    74 // CPbk2AttributeAddressSelectPhase::CPbk2AttributeAddressSelectPhase
       
    75 // --------------------------------------------------------------------------
       
    76 //
       
    77 CPbk2AttributeAddressSelectPhase::CPbk2AttributeAddressSelectPhase
       
    78         ( MPbk2ServicePhaseObserver& aObserver,
       
    79           TPbk2AttributeAssignData aAttributeData,
       
    80           TBool aRskBack ) :
       
    81             iObserver( aObserver ),
       
    82             iAttributeData( aAttributeData ),
       
    83             iRskBack( aRskBack )
       
    84     {
       
    85     }
       
    86 
       
    87 // --------------------------------------------------------------------------
       
    88 // CPbk2AttributeAddressSelectPhase::~CPbk2AttributeAddressSelectPhase
       
    89 // --------------------------------------------------------------------------
       
    90 //
       
    91 CPbk2AttributeAddressSelectPhase::~CPbk2AttributeAddressSelectPhase()
       
    92     {
       
    93     if ( iAddressSelectEliminator )
       
    94         {
       
    95         iAddressSelectEliminator->ForceExit();
       
    96         }
       
    97     delete iStoreContact;
       
    98     delete iRetrieveOperation;
       
    99     delete iContactLinks;
       
   100     delete iResults;
       
   101     delete iDealer;
       
   102 
       
   103     FeatureManager::UnInitializeLib();
       
   104     }
       
   105 
       
   106 // --------------------------------------------------------------------------
       
   107 // CPbk2AttributeAddressSelectPhase::ConstructL
       
   108 // --------------------------------------------------------------------------
       
   109 //
       
   110 inline void CPbk2AttributeAddressSelectPhase::ConstructL
       
   111         ( MVPbkContactLinkArray* aContactLinks )
       
   112     {
       
   113     iEikEnv = CEikonEnv::Static();
       
   114 
       
   115     iContactLinks = CVPbkContactLinkArray::NewL();
       
   116     if ( aContactLinks )
       
   117         {
       
   118         CopyContactLinksL( *aContactLinks, *iContactLinks );
       
   119         }
       
   120     FeatureManager::InitializeLibL();
       
   121     }
       
   122 
       
   123 // --------------------------------------------------------------------------
       
   124 // CPbk2AttributeAddressSelectPhase::NewL
       
   125 // --------------------------------------------------------------------------
       
   126 //
       
   127 CPbk2AttributeAddressSelectPhase* CPbk2AttributeAddressSelectPhase::NewL
       
   128         ( MPbk2ServicePhaseObserver& aObserver,
       
   129           MVPbkContactLinkArray* aContactLinks,
       
   130           TPbk2AttributeAssignData aAttributeData,
       
   131           TBool aRskBack )
       
   132     {
       
   133     CPbk2AttributeAddressSelectPhase* self =
       
   134         new ( ELeave ) CPbk2AttributeAddressSelectPhase
       
   135             ( aObserver, aAttributeData, aRskBack );
       
   136     CleanupStack::PushL( self );
       
   137     self->ConstructL( aContactLinks );
       
   138     CleanupStack::Pop( self );
       
   139     return self;
       
   140     }
       
   141 
       
   142 // --------------------------------------------------------------------------
       
   143 // CPbk2AttributeAddressSelectPhase::LaunchServicePhaseL
       
   144 // --------------------------------------------------------------------------
       
   145 //
       
   146 void CPbk2AttributeAddressSelectPhase::LaunchServicePhaseL()
       
   147     {
       
   148     RetrieveContactL();
       
   149     }
       
   150 
       
   151 // --------------------------------------------------------------------------
       
   152 // CPbk2AttributeAddressSelectPhase::CancelServicePhase
       
   153 // --------------------------------------------------------------------------
       
   154 //
       
   155 void CPbk2AttributeAddressSelectPhase::CancelServicePhase()
       
   156     {
       
   157     if ( iAddressSelectEliminator )
       
   158         {
       
   159         iAddressSelectEliminator->ForceExit();
       
   160         }
       
   161 
       
   162     iObserver.PhaseCanceled( *this );
       
   163     }
       
   164 
       
   165 // --------------------------------------------------------------------------
       
   166 // CPbk2AttributeAddressSelectPhase::RequestCancelL
       
   167 // --------------------------------------------------------------------------
       
   168 //
       
   169 void CPbk2AttributeAddressSelectPhase::RequestCancelL( TInt aExitCommandId )
       
   170     {
       
   171     if ( iAddressSelectEliminator )
       
   172         {
       
   173         iAddressSelectEliminator->RequestExitL( aExitCommandId );
       
   174         }
       
   175 
       
   176     // Withdraw our key event agent so that it does not react to
       
   177     // app shutter's escape key event simulation
       
   178     delete iDealer;
       
   179     iDealer = NULL;
       
   180 
       
   181    if ( aExitCommandId == EEikBidCancel )
       
   182         {
       
   183         iObserver.PhaseAborted( *this );
       
   184         }
       
   185     else
       
   186         {
       
   187         iObserver.PhaseCanceled( *this );
       
   188         }
       
   189     }
       
   190 
       
   191 // --------------------------------------------------------------------------
       
   192 // CPbk2AttributeAddressSelectPhase::AcceptDelayed
       
   193 // --------------------------------------------------------------------------
       
   194 //
       
   195 void CPbk2AttributeAddressSelectPhase::AcceptDelayedL
       
   196         ( const TDesC8& /*aContactLinkBuffer*/ )
       
   197     {
       
   198     // Nothing to accept
       
   199     }
       
   200 
       
   201 // --------------------------------------------------------------------------
       
   202 // CPbk2AttributeAddressSelectPhase::Results
       
   203 // --------------------------------------------------------------------------
       
   204 //
       
   205 MVPbkContactLinkArray* CPbk2AttributeAddressSelectPhase::Results() const
       
   206     {
       
   207     return iResults;
       
   208     }
       
   209 
       
   210 // --------------------------------------------------------------------------
       
   211 // CPbk2AttributeAddressSelectPhase::ExtraResultData
       
   212 // --------------------------------------------------------------------------
       
   213 //
       
   214 TInt CPbk2AttributeAddressSelectPhase::ExtraResultData() const
       
   215     {
       
   216     return KErrNotSupported;
       
   217     }
       
   218 
       
   219 // --------------------------------------------------------------------------
       
   220 // CPbk2AttributeAddressSelectPhase::TakeStoreContact
       
   221 // --------------------------------------------------------------------------
       
   222 //
       
   223 MVPbkStoreContact* CPbk2AttributeAddressSelectPhase::TakeStoreContact()
       
   224     {
       
   225     // Not supported
       
   226     return NULL;
       
   227     }
       
   228 
       
   229 // --------------------------------------------------------------------------
       
   230 // CPbk2AttributeAddressSelectPhase::FieldContent
       
   231 // --------------------------------------------------------------------------
       
   232 //
       
   233 HBufC* CPbk2AttributeAddressSelectPhase::FieldContent() const
       
   234     {
       
   235     return NULL;
       
   236     }
       
   237 
       
   238 // --------------------------------------------------------------------------
       
   239 // CPbk2AttributeAddressSelectPhase::VPbkSingleContactOperationComplete
       
   240 // --------------------------------------------------------------------------
       
   241 //
       
   242 void CPbk2AttributeAddressSelectPhase::VPbkSingleContactOperationComplete
       
   243         ( MVPbkContactOperationBase& /*aOperation*/,
       
   244           MVPbkStoreContact* aContact )
       
   245     {
       
   246     CPbk2ServerAppAppUi& appUi = static_cast<CPbk2ServerAppAppUi&>
       
   247         ( *iEikEnv->EikAppUi() );
       
   248     TInt err( KErrNone );
       
   249     
       
   250     // Contact retrieval complete
       
   251     delete iStoreContact;
       
   252     iStoreContact = aContact;
       
   253 
       
   254     TRAP( err, SelectAddressesL() );       
       
   255   
       
   256     if ( err != KErrNone )
       
   257         {
       
   258 		// Deregister store events if something went wrong. 
       
   259         appUi.StoreManager().DeregisterStoreEvents( *this );
       
   260         iObserver.PhaseError( *this, err );
       
   261         }
       
   262     }
       
   263 
       
   264 // --------------------------------------------------------------------------
       
   265 // CPbk2AttributeAddressSelectPhase::VPbkSingleContactOperationFailed
       
   266 // --------------------------------------------------------------------------
       
   267 //
       
   268 void CPbk2AttributeAddressSelectPhase::VPbkSingleContactOperationFailed
       
   269         ( MVPbkContactOperationBase& /*aOperation*/, TInt aError )
       
   270     {
       
   271     iObserver.PhaseError( *this, aError );
       
   272     }
       
   273 
       
   274 // --------------------------------------------------------------------------
       
   275 // CPbk2AttributeAddressSelectPhase::Pbk2ProcessKeyEventL
       
   276 // --------------------------------------------------------------------------
       
   277 //
       
   278 TBool CPbk2AttributeAddressSelectPhase::Pbk2ProcessKeyEventL
       
   279         ( const TKeyEvent& aKeyEvent, TEventCode aType )
       
   280     {
       
   281     TBool ret = EFalse;
       
   282 
       
   283     if ( aType == EEventKey && aKeyEvent.iCode == EKeyEscape )
       
   284         {
       
   285         iObserver.PhaseOkToExit( *this, EEikBidCancel );
       
   286         ret = ETrue;
       
   287         }
       
   288 
       
   289     return ret;
       
   290     }
       
   291 
       
   292 // --------------------------------------------------------------------------
       
   293 // CPbk2AttributeAddressSelectPhase::StoreReady
       
   294 // --------------------------------------------------------------------------
       
   295 //
       
   296 void CPbk2AttributeAddressSelectPhase::StoreReady(
       
   297     MVPbkContactStore& /*aContactStore*/ )
       
   298     {
       
   299     // not interested
       
   300     }
       
   301 
       
   302 // --------------------------------------------------------------------------
       
   303 // CPbk2AttributeAddressSelectPhase::StoreUnavailable
       
   304 // --------------------------------------------------------------------------
       
   305 //
       
   306 void CPbk2AttributeAddressSelectPhase::StoreUnavailable(
       
   307     MVPbkContactStore& /*aContactStore*/,
       
   308     TInt /*aReason*/ )
       
   309     {
       
   310     // not interested
       
   311     }
       
   312     
       
   313 // --------------------------------------------------------------------------
       
   314 // CPbk2AttributeAddressSelectPhase::StoreUnavailable
       
   315 // --------------------------------------------------------------------------
       
   316 //    
       
   317 void CPbk2AttributeAddressSelectPhase::HandleStoreEventL(
       
   318      MVPbkContactStore& /*aContactStore*/,
       
   319      TVPbkContactStoreEvent aEvent )
       
   320     {
       
   321     if ( aEvent.iContactLink != NULL && iStoreContact != NULL )
       
   322         {
       
   323         if ( aEvent.iContactLink->RefersTo( *iStoreContact ) )
       
   324             {
       
   325             RDebug::Print(_L("----CPbk2AttributeAddressSelectPhase::HandleStoreEventL() - dismiss dialog"));
       
   326             // dismiss address selection dialog since contact was changed
       
   327             if ( iAddressSelectEliminator )
       
   328                 {
       
   329                 iAddressSelectEliminator->ForceExit();
       
   330                 }
       
   331             
       
   332             // observer will be notified in SelectAddressesL() after closing 
       
   333             // address selection dialog
       
   334             }
       
   335         }
       
   336     }
       
   337 
       
   338 // --------------------------------------------------------------------------
       
   339 // CPbk2AttributeAddressSelectPhase::RetrieveContactL
       
   340 // --------------------------------------------------------------------------
       
   341 //
       
   342 void CPbk2AttributeAddressSelectPhase::RetrieveContactL()
       
   343     {
       
   344     CPbk2ServerAppAppUi& appUi =
       
   345         static_cast<CPbk2ServerAppAppUi&>
       
   346             ( *iEikEnv->EikAppUi() );
       
   347 
       
   348     delete iRetrieveOperation;
       
   349     iRetrieveOperation = NULL;
       
   350     iRetrieveOperation = appUi.ApplicationServices().ContactManager().
       
   351         RetrieveContactL( iContactLinks->At( KFirstElement ), *this );
       
   352     }
       
   353 
       
   354 // --------------------------------------------------------------------------
       
   355 // CPbk2AttributeAddressSelectPhase::SelectAddressesL
       
   356 // --------------------------------------------------------------------------
       
   357 //
       
   358 void CPbk2AttributeAddressSelectPhase::SelectAddressesL()
       
   359     {
       
   360     MVPbkStoreContactField* field = CheckIfFieldAlreadySelectedL();
       
   361 
       
   362     if ( !field )
       
   363         {
       
   364         TInt resourceId = AddressSelectResourceId( iAttributeData );
       
   365         // Field was not already selected --> querying it
       
   366         TResourceReader reader;
       
   367         CCoeEnv::Static()->CreateResourceReaderLC( reader, resourceId );
       
   368 
       
   369         CPbk2ServerAppAppUi& appUi = static_cast<CPbk2ServerAppAppUi&>
       
   370             ( *iEikEnv->EikAppUi() );
       
   371 
       
   372         TPbk2AddressSelectParams params
       
   373             ( *iStoreContact, appUi.ApplicationServices().ContactManager(),
       
   374               appUi.ApplicationServices().NameFormatter(),
       
   375               appUi.ApplicationServices().FieldProperties(),
       
   376               reader, R_QTN_PHOB_TITLE_SELECT_DEFAULT );
       
   377         params.SetTitleToIncludeContactName( EFalse );
       
   378 
       
   379         CVPbkFieldTypeSelector* fieldTypeSelector = NULL;
       
   380         
       
   381         // In speed dial, for voip fields we build a fieldselector 
       
   382         // from vpbkfieldtypeselectorfactory. This makes sure the
       
   383         // right voip fields are shown in the address select dialog
       
   384         if ( iAttributeData.iAttributeUid ==
       
   385                  TUid::Uid( KVPbkSpeedDialAttributeImplementationUID ) )
       
   386             {
       
   387             // Speedial specific filter
       
   388             if( FeatureManager::FeatureSupported( KFeatureIdCommonVoip ) )
       
   389                 {
       
   390                 fieldTypeSelector = VPbkFieldTypeSelectorFactory::
       
   391                     BuildContactActionTypeSelectorL(
       
   392                         VPbkFieldTypeSelectorFactory::EVOIPCallSelector,
       
   393                         appUi.ApplicationServices().ContactManager().FieldTypes() );
       
   394                 CleanupStack::PushL( fieldTypeSelector );
       
   395                 }
       
   396             else
       
   397                 {
       
   398                 fieldTypeSelector = CVPbkFieldTypeSelector::NewL( 
       
   399                         appUi.ApplicationServices().ContactManager().FieldTypes() );
       
   400                 CleanupStack::PushL( fieldTypeSelector );
       
   401                 }
       
   402             
       
   403             VPbkContactViewFilterBuilder::BuildContactViewFilterL( 
       
   404                                       *fieldTypeSelector, 
       
   405                                       EVPbkContactViewFilterPhoneNumber, 
       
   406                                       appUi.ApplicationServices().ContactManager() );
       
   407             }
       
   408         
       
   409         // Run address select
       
   410         CPbk2AddressSelect* addressSelect = CPbk2AddressSelect::NewL(
       
   411                 params,
       
   412                 *fieldTypeSelector,
       
   413                 NULL,
       
   414                 NULL );
       
   415         
       
   416         // Correct CBA buttons
       
   417         TInt correctedCba = CorrectRSK( resourceId );
       
   418         if ( correctedCba > KErrNone )
       
   419             {
       
   420             addressSelect->SetCba( correctedCba );
       
   421             }
       
   422 
       
   423         // Execute
       
   424         iAddressSelectEliminator = addressSelect;
       
   425         iAddressSelectEliminator->ResetWhenDestroyed
       
   426             ( &iAddressSelectEliminator );
       
   427         appUi.StoreManager().RegisterStoreEventsL( *this );
       
   428         field = addressSelect->ExecuteLD();
       
   429         appUi.StoreManager().DeregisterStoreEvents( *this );
       
   430         if ( fieldTypeSelector )
       
   431 			{
       
   432 			CleanupStack::PopAndDestroy( 2 ); // fieldTypeSelector, reader
       
   433 			}
       
   434 		else
       
   435 			{
       
   436 			CleanupStack::PopAndDestroy(); // reader
       
   437 			}
       
   438 		
       
   439         }
       
   440 
       
   441     if ( field )
       
   442         {
       
   443         CleanupDeletePushL( field );
       
   444         AppendResultL( field );
       
   445         CleanupStack::PopAndDestroy( field );
       
   446         iObserver.NextPhase( *this );
       
   447         }
       
   448     else
       
   449         {
       
   450         iObserver.PhaseCanceled( *this );
       
   451         }
       
   452     }
       
   453 
       
   454 // --------------------------------------------------------------------------
       
   455 // CPbk2AttributeAddressSelectPhase::CheckIfFieldAlreadySelectedL
       
   456 // --------------------------------------------------------------------------
       
   457 //
       
   458 inline MVPbkStoreContactField*
       
   459         CPbk2AttributeAddressSelectPhase::CheckIfFieldAlreadySelectedL()
       
   460     {
       
   461     MVPbkStoreContactField* ret = NULL;
       
   462 
       
   463     const MVPbkContactLink& contactLink = iContactLinks->At( KFirstElement );
       
   464     MVPbkStoreContactFieldCollection& fields = iStoreContact->Fields();
       
   465     MVPbkStoreContactField* field = fields.RetrieveField( contactLink );
       
   466     if ( field )
       
   467         {
       
   468         ret = field->CloneLC();
       
   469         CleanupStack::Pop(); // field->CloneLC()
       
   470         }
       
   471 
       
   472     return ret;
       
   473     }
       
   474 
       
   475 // --------------------------------------------------------------------------
       
   476 // CPbk2AttributeAddressSelectPhase::AddressSelectResourceId
       
   477 // --------------------------------------------------------------------------
       
   478 //
       
   479 inline TInt CPbk2AttributeAddressSelectPhase::AddressSelectResourceId
       
   480         ( TPbk2AttributeAssignData aAttributeData )
       
   481     {
       
   482     TInt result = KErrNotFound;
       
   483 
       
   484     if ( aAttributeData.iAttributeUid ==
       
   485          TUid::Uid( KVPbkSpeedDialAttributeImplementationUID ) )
       
   486         {
       
   487         result = R_PBK2_PHONE_NUMBER_SELECT;
       
   488         }
       
   489 
       
   490     return result;
       
   491     }
       
   492 
       
   493 // --------------------------------------------------------------------------
       
   494 // CPbk2AttributeAddressSelectPhase::CorrectRSK
       
   495 // --------------------------------------------------------------------------
       
   496 //
       
   497 inline TInt CPbk2AttributeAddressSelectPhase::CorrectRSK
       
   498         ( TInt aAddressSelectResourceId )
       
   499     {
       
   500     TInt result = KErrNone;
       
   501 
       
   502     if ( iRskBack )
       
   503         {
       
   504         switch ( aAddressSelectResourceId )
       
   505             {
       
   506             case R_PBK2_PHONE_NUMBER_SELECT:
       
   507                 {
       
   508                 result = R_AVKON_SOFTKEYS_SELECT_BACK__SELECT;
       
   509                 break;
       
   510                 }
       
   511 
       
   512             default:
       
   513                 {
       
   514                 // Do nothing
       
   515                 break;
       
   516                 }
       
   517             }
       
   518         }
       
   519 
       
   520     return result;
       
   521     }
       
   522 
       
   523 // --------------------------------------------------------------------------
       
   524 // CPbk2AttributeAddressSelectPhase::AppendResultL
       
   525 // --------------------------------------------------------------------------
       
   526 //
       
   527 void CPbk2AttributeAddressSelectPhase::AppendResultL
       
   528         ( const MVPbkStoreContactField* aField )
       
   529     {
       
   530     if ( aField )
       
   531         {
       
   532         // Add the contact link to the result array
       
   533         MVPbkContactLink* link = aField->CreateLinkLC();
       
   534         if ( link )
       
   535             {
       
   536             if ( !iResults )
       
   537                 {
       
   538                 iResults = CVPbkContactLinkArray::NewL();
       
   539                 }
       
   540 
       
   541             CleanupStack::Pop(); // aField->CreateLinkLC()
       
   542 
       
   543             iResults->AppendL( link );
       
   544             }
       
   545         }
       
   546     }
       
   547 
       
   548 // End of File