phonebookui/Phonebook2/ServerApplication/src/CPbk2PrepareAttributeAssignPhase.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 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::Results
       
   208 // --------------------------------------------------------------------------
       
   209 //
       
   210 MVPbkContactLinkArray* CPbk2PrepareAttributeAssignPhase::Results() const
       
   211     {
       
   212     return iResults;
       
   213     }
       
   214 
       
   215 // --------------------------------------------------------------------------
       
   216 // CPbk2PrepareAttributeAssignPhase::ExtraResultData
       
   217 // --------------------------------------------------------------------------
       
   218 //
       
   219 TInt CPbk2PrepareAttributeAssignPhase::ExtraResultData() const
       
   220     {
       
   221     return KErrNotSupported;
       
   222     }
       
   223 
       
   224 // --------------------------------------------------------------------------
       
   225 // CPbk2PrepareAttributeAssignPhase::TakeStoreContact
       
   226 // --------------------------------------------------------------------------
       
   227 //
       
   228 MVPbkStoreContact* CPbk2PrepareAttributeAssignPhase::TakeStoreContact()
       
   229     {
       
   230     // Not supported
       
   231     return NULL;
       
   232     }
       
   233 
       
   234 // --------------------------------------------------------------------------
       
   235 // CPbk2PrepareAttributeAssignPhase::FieldContent
       
   236 // --------------------------------------------------------------------------
       
   237 //
       
   238 HBufC* CPbk2PrepareAttributeAssignPhase::FieldContent() const
       
   239     {
       
   240     return NULL;
       
   241     }
       
   242 
       
   243 // --------------------------------------------------------------------------
       
   244 // CPbk2PrepareAttributeAssignPhase::VPbkSingleContactOperationComplete
       
   245 // --------------------------------------------------------------------------
       
   246 //
       
   247 void CPbk2PrepareAttributeAssignPhase::VPbkSingleContactOperationComplete
       
   248         ( MVPbkContactOperationBase& /*aOperation*/,
       
   249           MVPbkStoreContact* aContact )
       
   250     {
       
   251     // Contact retrieval complete
       
   252     delete iStoreContact;
       
   253     iStoreContact = aContact;
       
   254 
       
   255     TRAPD( err, PrepareAssignL() );
       
   256     if ( err != KErrNone )
       
   257         {
       
   258         iObserver.PhaseError( *this, err );
       
   259         }
       
   260     }
       
   261 
       
   262 // --------------------------------------------------------------------------
       
   263 // CPbk2PrepareAttributeAssignPhase::VPbkSingleContactOperationFailed
       
   264 // --------------------------------------------------------------------------
       
   265 //
       
   266 void CPbk2PrepareAttributeAssignPhase::VPbkSingleContactOperationFailed
       
   267         ( MVPbkContactOperationBase& /*aOperation*/, TInt aError )
       
   268     {
       
   269     iObserver.PhaseError( *this, aError );
       
   270     }
       
   271 
       
   272 // --------------------------------------------------------------------------
       
   273 // CPbk2PrepareAttributeAssignPhase::ContactRelocatedL
       
   274 // --------------------------------------------------------------------------
       
   275 //
       
   276 void CPbk2PrepareAttributeAssignPhase::ContactRelocatedL
       
   277         ( MVPbkStoreContact* aRelocatedContact )
       
   278     {
       
   279     delete iStoreContact;
       
   280     iStoreContact = aRelocatedContact;
       
   281     }
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // CPbk2PrepareAttributeAssignPhase::ContactRelocationFailed
       
   285 // --------------------------------------------------------------------------
       
   286 //
       
   287 void CPbk2PrepareAttributeAssignPhase::ContactRelocationFailed
       
   288         ( TInt aReason, MVPbkStoreContact* aContact )
       
   289     {
       
   290     delete aContact;
       
   291 
       
   292     if ( aReason != KErrCancel )
       
   293         {
       
   294         iObserver.PhaseError( *this, aReason );
       
   295         }
       
   296     else
       
   297         {
       
   298         iObserver.PhaseCanceled( *this );
       
   299         }
       
   300     }
       
   301 
       
   302 // --------------------------------------------------------------------------
       
   303 // CPbk2PrepareAttributeAssignPhase::ContactsRelocationFailed
       
   304 // --------------------------------------------------------------------------
       
   305 //
       
   306 void CPbk2PrepareAttributeAssignPhase::ContactsRelocationFailed
       
   307         ( TInt /*aReason*/, CVPbkContactLinkArray* /*aContacts*/ )
       
   308     {
       
   309     // The code should never enter here
       
   310     __ASSERT_DEBUG( EFalse, Panic( ELogicRelocation ) );
       
   311     }
       
   312 
       
   313 // --------------------------------------------------------------------------
       
   314 // CPbk2PrepareAttributeAssignPhase::RelocationProcessComplete
       
   315 // --------------------------------------------------------------------------
       
   316 //
       
   317 void CPbk2PrepareAttributeAssignPhase::RelocationProcessComplete()
       
   318     {
       
   319     TRAPD( err, AppendResultL( iStoreContact ) );
       
   320 
       
   321     if ( err != KErrNone )
       
   322         {
       
   323         iObserver.PhaseError( *this, err );
       
   324         }
       
   325     else
       
   326         {
       
   327         iObserver.NextPhase( *this );
       
   328         }
       
   329     }
       
   330 
       
   331 // --------------------------------------------------------------------------
       
   332 // CPbk2PrepareAttributeAssignPhase::Pbk2ProcessKeyEventL
       
   333 // --------------------------------------------------------------------------
       
   334 //
       
   335 TBool CPbk2PrepareAttributeAssignPhase::Pbk2ProcessKeyEventL
       
   336         ( const TKeyEvent& aKeyEvent, TEventCode aType )
       
   337     {
       
   338     TBool ret = EFalse;
       
   339 
       
   340     if ( aType == EEventKey && aKeyEvent.iCode == EKeyEscape )
       
   341         {
       
   342         iObserver.PhaseOkToExit( *this, EEikBidCancel );
       
   343         ret = ETrue;
       
   344         }
       
   345 
       
   346     return ret;
       
   347     }
       
   348 
       
   349 // --------------------------------------------------------------------------
       
   350 // CPbk2PrepareAttributeAssignPhase::RetrieveContactL
       
   351 // --------------------------------------------------------------------------
       
   352 //
       
   353 void CPbk2PrepareAttributeAssignPhase::RetrieveContactL()
       
   354     {
       
   355     CPbk2ServerAppAppUi& appUi =
       
   356         static_cast<CPbk2ServerAppAppUi&>
       
   357             ( *iEikenv->EikAppUi() );
       
   358 
       
   359     delete iRetrieveOperation;
       
   360     iRetrieveOperation = NULL;
       
   361     iRetrieveOperation = appUi.ApplicationServices().ContactManager().
       
   362         RetrieveContactL( iContactLinks->At( KFirstElement ), *this );
       
   363     }
       
   364 
       
   365 // --------------------------------------------------------------------------
       
   366 // CPbk2PrepareAttributeAssignPhase::PrepareAssignL
       
   367 // --------------------------------------------------------------------------
       
   368 //
       
   369 void CPbk2PrepareAttributeAssignPhase::PrepareAssignL()
       
   370     {
       
   371     TBool contactIsValid = ValidateContactL();
       
   372 
       
   373     if ( contactIsValid )
       
   374         {
       
   375         // If contact does not need relocation, append the original
       
   376         // given link to results array (important not to create a new link
       
   377         // from retrieved contact since it will lose field link information)
       
   378         AppendResultL( iContactLinks->At( KFirstElement ) );
       
   379 
       
   380         iObserver.NextPhase( *this );
       
   381         }
       
   382     }
       
   383 
       
   384 // --------------------------------------------------------------------------
       
   385 // CPbk2PrepareAttributeAssignPhase::ValidateContactL
       
   386 // --------------------------------------------------------------------------
       
   387 //
       
   388 TBool CPbk2PrepareAttributeAssignPhase::ValidateContactL()
       
   389     {
       
   390     TBool ret = ETrue;
       
   391 
       
   392     if ( !ContactMeetsPreconditionsL() )
       
   393         {
       
   394         ret = EFalse;
       
   395         iObserver.PhaseError( *this, KErrArgument );
       
   396         }
       
   397 
       
   398     if ( ret && ContactNeedsRelocation() )
       
   399         {
       
   400         RelocateContactL();
       
   401         ret = EFalse;
       
   402         }
       
   403 
       
   404     return ret;
       
   405     }
       
   406 
       
   407 // --------------------------------------------------------------------------
       
   408 // CPbk2PrepareAttributeAssignPhase::ContactMeetsPreconditionsL
       
   409 // --------------------------------------------------------------------------
       
   410 //
       
   411 TBool CPbk2PrepareAttributeAssignPhase::ContactMeetsPreconditionsL()
       
   412     {
       
   413     TBool ret = ETrue;
       
   414 
       
   415     if ( iAttributeData.iAttributeUid ==
       
   416          TUid::Uid( KVPbkSpeedDialAttributeImplementationUID ) )
       
   417         {
       
   418         CPbk2ServerAppAppUi& appUi =
       
   419             static_cast<CPbk2ServerAppAppUi&>
       
   420                 ( *iEikenv->EikAppUi() );
       
   421 
       
   422         // The contact must have a phone number field
       
   423         TPbk2StoreContactAnalyzer analyzer
       
   424             ( appUi.ApplicationServices().ContactManager(), iStoreContact );
       
   425         
       
   426         TVPbkStoreContactAnalyzer vpbkAnalyzer
       
   427             ( appUi.ApplicationServices().ContactManager(), iStoreContact ); 
       
   428         
       
   429         if( FeatureManager::FeatureSupported( KFeatureIdCommonVoip ) )
       
   430             {
       
   431             if ( ( KErrNotFound == analyzer.HasFieldL( R_PHONEBOOK2_SIP_MSISDN_SELECTOR ) ) 
       
   432               // use vpbk to check whether the installed service can be assigned	
       
   433               && ( KErrNotFound == vpbkAnalyzer.HasFieldL( VPbkFieldTypeSelectorFactory::EVOIPCallSelector ) ) )
       
   434                 {
       
   435                 ret = EFalse;
       
   436                 }
       
   437             }
       
   438         else
       
   439             {
       
   440             if ( KErrNotFound ==
       
   441                     analyzer.HasFieldL( R_PHONEBOOK2_PHONENUMBER_SELECTOR ) )
       
   442                 {
       
   443                 ret = EFalse;
       
   444                 }
       
   445             }
       
   446 
       
   447 
       
   448         if ( ret && iRemoveAttribute )
       
   449             {
       
   450             // In case of attribute removal we must also check that
       
   451             // the given contact link is a field link
       
   452 
       
   453             if ( !iStoreContact->Fields().RetrieveField
       
   454                     ( iContactLinks->At( KFirstElement ) ) )
       
   455                 {
       
   456                 ret = EFalse;
       
   457                 }
       
   458             }
       
   459         }
       
   460 
       
   461     return ret;
       
   462     }
       
   463 
       
   464 // --------------------------------------------------------------------------
       
   465 // CPbk2PrepareAttributeAssignPhase::ContactNeedsRelocation
       
   466 // --------------------------------------------------------------------------
       
   467 //
       
   468 TBool CPbk2PrepareAttributeAssignPhase::ContactNeedsRelocation()
       
   469     {
       
   470     TBool ret = EFalse;
       
   471 
       
   472     if ( iAttributeData.iAttributeUid ==
       
   473          TUid::Uid( KVPbkSpeedDialAttributeImplementationUID ) )
       
   474         {
       
   475         CPbk2ServerAppAppUi& appUi =
       
   476             static_cast<CPbk2ServerAppAppUi&>
       
   477                 ( *iEikenv->EikAppUi() );
       
   478 
       
   479         if ( !iStoreContact->ParentStore().StoreProperties().
       
   480               SupportsSpeedDials() )
       
   481             {
       
   482             ret = ETrue;
       
   483             }
       
   484         }
       
   485 
       
   486     return ret;
       
   487     }
       
   488 
       
   489 // --------------------------------------------------------------------------
       
   490 // CPbk2PrepareAttributeAssignPhase::RelocateContactL
       
   491 // --------------------------------------------------------------------------
       
   492 //
       
   493 void CPbk2PrepareAttributeAssignPhase::RelocateContactL()
       
   494     {
       
   495     delete iContactRelocator;
       
   496     iContactRelocator = NULL;
       
   497     iContactRelocator = CPbk2ContactRelocator::NewL();
       
   498 
       
   499     MVPbkStoreContact* contact = iStoreContact;
       
   500     iStoreContact = NULL;
       
   501 
       
   502     // Asynchronously relocate the contact
       
   503     iContactRelocator->RelocateContactL
       
   504         ( contact, *this, EPbk2DisplayStoreDoesNotSupportQuery,
       
   505           CPbk2ContactRelocator::EPbk2RelocatorExistingContact );
       
   506     }
       
   507 
       
   508 // --------------------------------------------------------------------------
       
   509 // CPbk2PrepareAttributeAssignPhase::AppendResultL
       
   510 // --------------------------------------------------------------------------
       
   511 //
       
   512 void CPbk2PrepareAttributeAssignPhase::AppendResultL
       
   513         ( const MVPbkStoreContact* aStoreContact )
       
   514     {
       
   515     if ( aStoreContact )
       
   516         {
       
   517         // Add the contact link to the result array
       
   518         MVPbkContactLink* link = aStoreContact->CreateLinkLC();
       
   519         if ( link )
       
   520             {
       
   521             if ( !iResults )
       
   522                 {
       
   523                 iResults = CVPbkContactLinkArray::NewL();
       
   524                 }
       
   525 
       
   526             CleanupStack::Pop(); // aStoreContact->CreateLinkLC()
       
   527 
       
   528             iResults->AppendL( link );
       
   529             }
       
   530         }
       
   531     }
       
   532 
       
   533 // --------------------------------------------------------------------------
       
   534 // CPbk2PrepareAttributeAssignPhase::AppendResultL
       
   535 // --------------------------------------------------------------------------
       
   536 //
       
   537 void CPbk2PrepareAttributeAssignPhase::AppendResultL
       
   538         ( const MVPbkContactLink& aContactLink )
       
   539     {
       
   540     MVPbkContactLink* link = aContactLink.CloneLC();
       
   541 
       
   542     if ( !iResults )
       
   543         {
       
   544         iResults = CVPbkContactLinkArray::NewL();
       
   545         }
       
   546 
       
   547     CleanupStack::Pop(); // aContactLink->CloneLC()
       
   548     iResults->AppendL( link );
       
   549     }
       
   550 
       
   551 // End of File