phonebookui/Phonebook2/ServerApplication/src/CPbk2PrepareAttributeAssignPhase.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
child 68 9da50d567e3c
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     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 prepare single assign phase.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2PrepareAttributeAssignPhase.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "MPbk2ServicePhaseObserver.h"
       
    23 #include "CPbk2ServerAppAppUi.h"
       
    24 #include "CPbk2KeyEventDealer.h"
       
    25 #include <TPbk2StoreContactAnalyzer.h>
       
    26 #include <MPbk2ApplicationServices.h>
       
    27 #include <Pbk2UIControls.rsg>
       
    28 
       
    29 // Virtual Phonebook
       
    30 #include <MVPbkContactLink.h>
       
    31 #include <CVPbkContactLinkArray.h>
       
    32 #include <MVPbkContactOperationBase.h>
       
    33 #include <MVPbkStoreContact.h>
       
    34 #include <MVPbkContactStore.h>
       
    35 #include <MVPbkContactStoreProperties.h>
       
    36 #include <CVPbkContactManager.h>
       
    37 #include <CVPbkSpeedDialAttribute.h>
       
    38 #include <TVPbkStoreContactAnalyzer.h>
       
    39 #include <VPbkFieldTypeSelectorFactory.h>
       
    40 
       
    41 // System includes
       
    42 #include <featmgr.h>
       
    43 
       
    44 using namespace Pbk2ContactRelocator;
       
    45 
       
    46 /// Unnamed namespace for local definitions
       
    47 namespace {
       
    48 
       
    49 const TInt KFirstElement = 0;
       
    50 
       
    51 /**
       
    52  * Copies a link array to another.
       
    53  *
       
    54  * @param aSourceLinkArray    Link array which is copied
       
    55  * @param aTargetLinkArray    Links are copied to this
       
    56  */
       
    57 void CopyContactLinksL( const MVPbkContactLinkArray& aSourceLinkArray,
       
    58         CVPbkContactLinkArray& aTargetLinkArray )
       
    59     {
       
    60     const TInt count = aSourceLinkArray.Count();
       
    61     for ( TInt i(0); i < count; ++i )
       
    62         {
       
    63         const MVPbkContactLink& contactLink = aSourceLinkArray.At(i);
       
    64         aTargetLinkArray.AppendL( contactLink.CloneLC() );
       
    65         CleanupStack::Pop(); // link
       
    66         }
       
    67     }
       
    68 
       
    69 #ifdef _DEBUG
       
    70 
       
    71 enum TPanicCode
       
    72     {
       
    73     ENullPointer,
       
    74     ELogicRelocation
       
    75     };
       
    76 
       
    77 void Panic(TPanicCode aReason)
       
    78     {
       
    79     _LIT( KPanicText, "CPbk2PrepareAttributeAssignPhase" );
       
    80     User::Panic( KPanicText, aReason );
       
    81     }
       
    82 
       
    83 #endif // _DEBUG
       
    84 
       
    85 } /// namespace
       
    86 
       
    87 // --------------------------------------------------------------------------
       
    88 // CPbk2PrepareAttributeAssignPhase::CPbk2PrepareAttributeAssignPhase
       
    89 // --------------------------------------------------------------------------
       
    90 //
       
    91 CPbk2PrepareAttributeAssignPhase::CPbk2PrepareAttributeAssignPhase
       
    92         ( MPbk2ServicePhaseObserver& aObserver,
       
    93           TPbk2AttributeAssignData aAttributeData,
       
    94             TBool aRemoveAttribute ) :
       
    95             iObserver( aObserver ),
       
    96             iAttributeData( aAttributeData ),
       
    97             iRemoveAttribute( aRemoveAttribute )
       
    98     {
       
    99     }
       
   100 
       
   101 // --------------------------------------------------------------------------
       
   102 // CPbk2PrepareAttributeAssignPhase::~CPbk2PrepareAttributeAssignPhase
       
   103 // --------------------------------------------------------------------------
       
   104 //
       
   105 CPbk2PrepareAttributeAssignPhase::~CPbk2PrepareAttributeAssignPhase()
       
   106     {
       
   107     delete iStoreContact;
       
   108     delete iContactRelocator;
       
   109     delete iRetrieveOperation;
       
   110     delete iDealer;
       
   111     delete iContactLinks;
       
   112     delete iResults;
       
   113     FeatureManager::UnInitializeLib();
       
   114     }
       
   115 
       
   116 // --------------------------------------------------------------------------
       
   117 // CPbk2PrepareAttributeAssignPhase::ConstructL
       
   118 // --------------------------------------------------------------------------
       
   119 //
       
   120 inline void CPbk2PrepareAttributeAssignPhase::ConstructL
       
   121         ( MVPbkContactLinkArray& aContactLinks )
       
   122     {
       
   123     iEikenv = CEikonEnv::Static();
       
   124 
       
   125     iContactLinks = CVPbkContactLinkArray::NewL();
       
   126     CopyContactLinksL( aContactLinks, *iContactLinks );
       
   127 
       
   128     iDealer = CPbk2KeyEventDealer::NewL( *this );
       
   129     FeatureManager::InitializeLibL();
       
   130     }
       
   131 
       
   132 // --------------------------------------------------------------------------
       
   133 // CPbk2PrepareAttributeAssignPhase::NewL
       
   134 // --------------------------------------------------------------------------
       
   135 //
       
   136 CPbk2PrepareAttributeAssignPhase* CPbk2PrepareAttributeAssignPhase::NewL
       
   137         ( MPbk2ServicePhaseObserver& aObserver,
       
   138           MVPbkContactLinkArray* aContactLinks,
       
   139           TPbk2AttributeAssignData aAttributeData,
       
   140           TBool aRemoveAttribute )
       
   141     {
       
   142     CPbk2PrepareAttributeAssignPhase* self =
       
   143         new ( ELeave ) CPbk2PrepareAttributeAssignPhase
       
   144             ( aObserver, aAttributeData, aRemoveAttribute );
       
   145     CleanupStack::PushL( self );
       
   146     self->ConstructL( *aContactLinks );
       
   147     CleanupStack::Pop( self );
       
   148     return self;
       
   149     }
       
   150 
       
   151 // --------------------------------------------------------------------------
       
   152 // CPbk2PrepareAttributeAssignPhase::LaunchServicePhaseL
       
   153 // --------------------------------------------------------------------------
       
   154 //
       
   155 void CPbk2PrepareAttributeAssignPhase::LaunchServicePhaseL()
       
   156     {
       
   157     RetrieveContactL();
       
   158     }
       
   159 
       
   160 // --------------------------------------------------------------------------
       
   161 // CPbk2PrepareAttributeAssignPhase::CancelServicePhase
       
   162 // --------------------------------------------------------------------------
       
   163 //
       
   164 void CPbk2PrepareAttributeAssignPhase::CancelServicePhase()
       
   165     {
       
   166     delete iContactRelocator;
       
   167     iContactRelocator = NULL;
       
   168 
       
   169     iObserver.PhaseCanceled( *this );
       
   170     }
       
   171 
       
   172 // --------------------------------------------------------------------------
       
   173 // CPbk2PrepareAttributeAssignPhase::RequestCancelL
       
   174 // --------------------------------------------------------------------------
       
   175 //
       
   176 void CPbk2PrepareAttributeAssignPhase::RequestCancelL( TInt aExitCommandId )
       
   177     {
       
   178     delete iContactRelocator;
       
   179     iContactRelocator = NULL;
       
   180 
       
   181     // Withdraw our key event agent so that it does not react to
       
   182     // app shutter's escape key event simulation
       
   183     delete iDealer;
       
   184     iDealer = NULL;
       
   185 
       
   186     if ( aExitCommandId == EEikBidCancel )
       
   187         {
       
   188         iObserver.PhaseAborted( *this );
       
   189         }
       
   190     else
       
   191         {
       
   192         iObserver.PhaseCanceled( *this );
       
   193         }
       
   194     }
       
   195 
       
   196 // --------------------------------------------------------------------------
       
   197 // CPbk2PrepareAttributeAssignPhase::AcceptDelayed
       
   198 // --------------------------------------------------------------------------
       
   199 //
       
   200 void CPbk2PrepareAttributeAssignPhase::AcceptDelayedL
       
   201         ( const TDesC8& /*aContactLinkBuffer*/ )
       
   202     {
       
   203     // Nothing to do
       
   204     }
       
   205 
       
   206 // --------------------------------------------------------------------------
       
   207 // CPbk2PrepareAttributeAssignPhase::DenyDelayed
       
   208 // --------------------------------------------------------------------------
       
   209 //
       
   210 void CPbk2PrepareAttributeAssignPhase::DenyDelayedL
       
   211         ( const TDesC8& /*aContactLinkBuffer*/ )
       
   212     {
       
   213     // Nothing to do
       
   214     }
       
   215 
       
   216 // --------------------------------------------------------------------------
       
   217 // CPbk2PrepareAttributeAssignPhase::Results
       
   218 // --------------------------------------------------------------------------
       
   219 //
       
   220 MVPbkContactLinkArray* CPbk2PrepareAttributeAssignPhase::Results() const
       
   221     {
       
   222     return iResults;
       
   223     }
       
   224 
       
   225 // --------------------------------------------------------------------------
       
   226 // CPbk2PrepareAttributeAssignPhase::ExtraResultData
       
   227 // --------------------------------------------------------------------------
       
   228 //
       
   229 TInt CPbk2PrepareAttributeAssignPhase::ExtraResultData() const
       
   230     {
       
   231     return KErrNotSupported;
       
   232     }
       
   233 
       
   234 // --------------------------------------------------------------------------
       
   235 // CPbk2PrepareAttributeAssignPhase::TakeStoreContact
       
   236 // --------------------------------------------------------------------------
       
   237 //
       
   238 MVPbkStoreContact* CPbk2PrepareAttributeAssignPhase::TakeStoreContact()
       
   239     {
       
   240     // Not supported
       
   241     return NULL;
       
   242     }
       
   243 
       
   244 // --------------------------------------------------------------------------
       
   245 // CPbk2PrepareAttributeAssignPhase::FieldContent
       
   246 // --------------------------------------------------------------------------
       
   247 //
       
   248 HBufC* CPbk2PrepareAttributeAssignPhase::FieldContent() const
       
   249     {
       
   250     return NULL;
       
   251     }
       
   252 
       
   253 // --------------------------------------------------------------------------
       
   254 // CPbk2PrepareAttributeAssignPhase::VPbkSingleContactOperationComplete
       
   255 // --------------------------------------------------------------------------
       
   256 //
       
   257 void CPbk2PrepareAttributeAssignPhase::VPbkSingleContactOperationComplete
       
   258         ( MVPbkContactOperationBase& /*aOperation*/,
       
   259           MVPbkStoreContact* aContact )
       
   260     {
       
   261     // Contact retrieval complete
       
   262     delete iStoreContact;
       
   263     iStoreContact = aContact;
       
   264 
       
   265     TRAPD( err, PrepareAssignL() );
       
   266     if ( err != KErrNone )
       
   267         {
       
   268         iObserver.PhaseError( *this, err );
       
   269         }
       
   270     }
       
   271 
       
   272 // --------------------------------------------------------------------------
       
   273 // CPbk2PrepareAttributeAssignPhase::VPbkSingleContactOperationFailed
       
   274 // --------------------------------------------------------------------------
       
   275 //
       
   276 void CPbk2PrepareAttributeAssignPhase::VPbkSingleContactOperationFailed
       
   277         ( MVPbkContactOperationBase& /*aOperation*/, TInt aError )
       
   278     {
       
   279     iObserver.PhaseError( *this, aError );
       
   280     }
       
   281 
       
   282 // --------------------------------------------------------------------------
       
   283 // CPbk2PrepareAttributeAssignPhase::ContactRelocatedL
       
   284 // --------------------------------------------------------------------------
       
   285 //
       
   286 void CPbk2PrepareAttributeAssignPhase::ContactRelocatedL
       
   287         ( MVPbkStoreContact* aRelocatedContact )
       
   288     {
       
   289     delete iStoreContact;
       
   290     iStoreContact = aRelocatedContact;
       
   291     }
       
   292 
       
   293 // --------------------------------------------------------------------------
       
   294 // CPbk2PrepareAttributeAssignPhase::ContactRelocationFailed
       
   295 // --------------------------------------------------------------------------
       
   296 //
       
   297 void CPbk2PrepareAttributeAssignPhase::ContactRelocationFailed
       
   298         ( TInt aReason, MVPbkStoreContact* aContact )
       
   299     {
       
   300     delete aContact;
       
   301 
       
   302     if ( aReason != KErrCancel )
       
   303         {
       
   304         iObserver.PhaseError( *this, aReason );
       
   305         }
       
   306     else
       
   307         {
       
   308         iObserver.PhaseCanceled( *this );
       
   309         }
       
   310     }
       
   311 
       
   312 // --------------------------------------------------------------------------
       
   313 // CPbk2PrepareAttributeAssignPhase::ContactsRelocationFailed
       
   314 // --------------------------------------------------------------------------
       
   315 //
       
   316 void CPbk2PrepareAttributeAssignPhase::ContactsRelocationFailed
       
   317         ( TInt /*aReason*/, CVPbkContactLinkArray* /*aContacts*/ )
       
   318     {
       
   319     // The code should never enter here
       
   320     __ASSERT_DEBUG( EFalse, Panic( ELogicRelocation ) );
       
   321     }
       
   322 
       
   323 // --------------------------------------------------------------------------
       
   324 // CPbk2PrepareAttributeAssignPhase::RelocationProcessComplete
       
   325 // --------------------------------------------------------------------------
       
   326 //
       
   327 void CPbk2PrepareAttributeAssignPhase::RelocationProcessComplete()
       
   328     {
       
   329     TRAPD( err, AppendResultL( iStoreContact ) );
       
   330 
       
   331     if ( err != KErrNone )
       
   332         {
       
   333         iObserver.PhaseError( *this, err );
       
   334         }
       
   335     else
       
   336         {
       
   337         iObserver.NextPhase( *this );
       
   338         }
       
   339     }
       
   340 
       
   341 // --------------------------------------------------------------------------
       
   342 // CPbk2PrepareAttributeAssignPhase::Pbk2ProcessKeyEventL
       
   343 // --------------------------------------------------------------------------
       
   344 //
       
   345 TBool CPbk2PrepareAttributeAssignPhase::Pbk2ProcessKeyEventL
       
   346         ( const TKeyEvent& aKeyEvent, TEventCode aType )
       
   347     {
       
   348     TBool ret = EFalse;
       
   349 
       
   350     if ( aType == EEventKey && aKeyEvent.iCode == EKeyEscape )
       
   351         {
       
   352         iObserver.PhaseOkToExit( *this, EEikBidCancel );
       
   353         ret = ETrue;
       
   354         }
       
   355 
       
   356     return ret;
       
   357     }
       
   358 
       
   359 // --------------------------------------------------------------------------
       
   360 // CPbk2PrepareAttributeAssignPhase::RetrieveContactL
       
   361 // --------------------------------------------------------------------------
       
   362 //
       
   363 void CPbk2PrepareAttributeAssignPhase::RetrieveContactL()
       
   364     {
       
   365     CPbk2ServerAppAppUi& appUi =
       
   366         static_cast<CPbk2ServerAppAppUi&>
       
   367             ( *iEikenv->EikAppUi() );
       
   368 
       
   369     delete iRetrieveOperation;
       
   370     iRetrieveOperation = NULL;
       
   371     iRetrieveOperation = appUi.ApplicationServices().ContactManager().
       
   372         RetrieveContactL( iContactLinks->At( KFirstElement ), *this );
       
   373     }
       
   374 
       
   375 // --------------------------------------------------------------------------
       
   376 // CPbk2PrepareAttributeAssignPhase::PrepareAssignL
       
   377 // --------------------------------------------------------------------------
       
   378 //
       
   379 void CPbk2PrepareAttributeAssignPhase::PrepareAssignL()
       
   380     {
       
   381     TBool contactIsValid = ValidateContactL();
       
   382 
       
   383     if ( contactIsValid )
       
   384         {
       
   385         // If contact does not need relocation, append the original
       
   386         // given link to results array (important not to create a new link
       
   387         // from retrieved contact since it will lose field link information)
       
   388         AppendResultL( iContactLinks->At( KFirstElement ) );
       
   389 
       
   390         iObserver.NextPhase( *this );
       
   391         }
       
   392     }
       
   393 
       
   394 // --------------------------------------------------------------------------
       
   395 // CPbk2PrepareAttributeAssignPhase::ValidateContactL
       
   396 // --------------------------------------------------------------------------
       
   397 //
       
   398 TBool CPbk2PrepareAttributeAssignPhase::ValidateContactL()
       
   399     {
       
   400     TBool ret = ETrue;
       
   401 
       
   402     if ( !ContactMeetsPreconditionsL() )
       
   403         {
       
   404         ret = EFalse;
       
   405         iObserver.PhaseError( *this, KErrArgument );
       
   406         }
       
   407 
       
   408     if ( ret && ContactNeedsRelocation() )
       
   409         {
       
   410         RelocateContactL();
       
   411         ret = EFalse;
       
   412         }
       
   413 
       
   414     return ret;
       
   415     }
       
   416 
       
   417 // --------------------------------------------------------------------------
       
   418 // CPbk2PrepareAttributeAssignPhase::ContactMeetsPreconditionsL
       
   419 // --------------------------------------------------------------------------
       
   420 //
       
   421 TBool CPbk2PrepareAttributeAssignPhase::ContactMeetsPreconditionsL()
       
   422     {
       
   423     TBool ret = ETrue;
       
   424 
       
   425     if ( iAttributeData.iAttributeUid ==
       
   426          TUid::Uid( KVPbkSpeedDialAttributeImplementationUID ) )
       
   427         {
       
   428         CPbk2ServerAppAppUi& appUi =
       
   429             static_cast<CPbk2ServerAppAppUi&>
       
   430                 ( *iEikenv->EikAppUi() );
       
   431 
       
   432         // The contact must have a phone number field
       
   433         TPbk2StoreContactAnalyzer analyzer
       
   434             ( appUi.ApplicationServices().ContactManager(), iStoreContact );
       
   435         
       
   436         TVPbkStoreContactAnalyzer vpbkAnalyzer
       
   437             ( appUi.ApplicationServices().ContactManager(), iStoreContact ); 
       
   438         
       
   439         if( FeatureManager::FeatureSupported( KFeatureIdCommonVoip ) )
       
   440             {
       
   441             if ( ( KErrNotFound == analyzer.HasFieldL( R_PHONEBOOK2_SIP_MSISDN_SELECTOR ) ) 
       
   442               // use vpbk to check whether the installed service can be assigned	
       
   443               && ( KErrNotFound == vpbkAnalyzer.HasFieldL( VPbkFieldTypeSelectorFactory::EVOIPCallSelector ) ) )
       
   444                 {
       
   445                 ret = EFalse;
       
   446                 }
       
   447             }
       
   448         else
       
   449             {
       
   450             if ( KErrNotFound ==
       
   451                     analyzer.HasFieldL( R_PHONEBOOK2_PHONENUMBER_SELECTOR ) )
       
   452                 {
       
   453                 ret = EFalse;
       
   454                 }
       
   455             }
       
   456 
       
   457 
       
   458         if ( ret && iRemoveAttribute )
       
   459             {
       
   460             // In case of attribute removal we must also check that
       
   461             // the given contact link is a field link
       
   462 
       
   463             if ( !iStoreContact->Fields().RetrieveField
       
   464                     ( iContactLinks->At( KFirstElement ) ) )
       
   465                 {
       
   466                 ret = EFalse;
       
   467                 }
       
   468             }
       
   469         }
       
   470 
       
   471     return ret;
       
   472     }
       
   473 
       
   474 // --------------------------------------------------------------------------
       
   475 // CPbk2PrepareAttributeAssignPhase::ContactNeedsRelocation
       
   476 // --------------------------------------------------------------------------
       
   477 //
       
   478 TBool CPbk2PrepareAttributeAssignPhase::ContactNeedsRelocation()
       
   479     {
       
   480     TBool ret = EFalse;
       
   481 
       
   482     if ( iAttributeData.iAttributeUid ==
       
   483          TUid::Uid( KVPbkSpeedDialAttributeImplementationUID ) )
       
   484         {
       
   485         CPbk2ServerAppAppUi& appUi =
       
   486             static_cast<CPbk2ServerAppAppUi&>
       
   487                 ( *iEikenv->EikAppUi() );
       
   488 
       
   489         if ( !iStoreContact->ParentStore().StoreProperties().
       
   490               SupportsSpeedDials() )
       
   491             {
       
   492             ret = ETrue;
       
   493             }
       
   494         }
       
   495 
       
   496     return ret;
       
   497     }
       
   498 
       
   499 // --------------------------------------------------------------------------
       
   500 // CPbk2PrepareAttributeAssignPhase::RelocateContactL
       
   501 // --------------------------------------------------------------------------
       
   502 //
       
   503 void CPbk2PrepareAttributeAssignPhase::RelocateContactL()
       
   504     {
       
   505     delete iContactRelocator;
       
   506     iContactRelocator = NULL;
       
   507     iContactRelocator = CPbk2ContactRelocator::NewL();
       
   508 
       
   509     MVPbkStoreContact* contact = iStoreContact;
       
   510     iStoreContact = NULL;
       
   511 
       
   512     // Asynchronously relocate the contact
       
   513     iContactRelocator->RelocateContactL
       
   514         ( contact, *this, EPbk2DisplayStoreDoesNotSupportQuery,
       
   515           CPbk2ContactRelocator::EPbk2RelocatorExistingContact );
       
   516     }
       
   517 
       
   518 // --------------------------------------------------------------------------
       
   519 // CPbk2PrepareAttributeAssignPhase::AppendResultL
       
   520 // --------------------------------------------------------------------------
       
   521 //
       
   522 void CPbk2PrepareAttributeAssignPhase::AppendResultL
       
   523         ( const MVPbkStoreContact* aStoreContact )
       
   524     {
       
   525     if ( aStoreContact )
       
   526         {
       
   527         // Add the contact link to the result array
       
   528         MVPbkContactLink* link = aStoreContact->CreateLinkLC();
       
   529         if ( link )
       
   530             {
       
   531             if ( !iResults )
       
   532                 {
       
   533                 iResults = CVPbkContactLinkArray::NewL();
       
   534                 }
       
   535 
       
   536             CleanupStack::Pop(); // aStoreContact->CreateLinkLC()
       
   537 
       
   538             iResults->AppendL( link );
       
   539             }
       
   540         }
       
   541     }
       
   542 
       
   543 // --------------------------------------------------------------------------
       
   544 // CPbk2PrepareAttributeAssignPhase::AppendResultL
       
   545 // --------------------------------------------------------------------------
       
   546 //
       
   547 void CPbk2PrepareAttributeAssignPhase::AppendResultL
       
   548         ( const MVPbkContactLink& aContactLink )
       
   549     {
       
   550     MVPbkContactLink* link = aContactLink.CloneLC();
       
   551 
       
   552     if ( !iResults )
       
   553         {
       
   554         iResults = CVPbkContactLinkArray::NewL();
       
   555         }
       
   556 
       
   557     CleanupStack::Pop(); // aContactLink->CloneLC()
       
   558     iResults->AppendL( link );
       
   559     }
       
   560 
       
   561 // End of File