phonebookui/Phonebook2/USIMExtension/src/CPsu2CopyAllToPbkCmd.cpp
branchRCL_3
changeset 17 2666d9724c76
child 21 9da50d567e3c
equal deleted inserted replaced
15:34879f5cfc63 17:2666d9724c76
       
     1 /*
       
     2 * Copyright (c) 2010 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 copy all SIM contacts to phone memory.
       
    15 *
       
    16 * NOTE: Contains a lot of similar code that in CPsu2CopySimContactsCmd.
       
    17 * Reason is that there is no clear place to share common SIM related code between 
       
    18 * USIMThinExtension.dll and USIMExtension.dll. This is a potential place for 
       
    19 * future refactoring. 
       
    20 */
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 #include "CPsu2CopyAllToPbkCmd.h"
       
    25 
       
    26 // Phonebook 2
       
    27 #include <MPbk2CommandObserver.h>
       
    28 #include <CPbk2StoreConfiguration.h>
       
    29 #include <CPbk2StorePropertyArray.h>
       
    30 #include <CPbk2StoreProperty.h>
       
    31 #include <CPbk2StoreViewDefinition.h>
       
    32 #include <CPbk2SortOrderManager.h>
       
    33 #include <Pbk2ProcessDecoratorFactory.h>
       
    34 #include <Pbk2UID.h>
       
    35 /// Use R_PBK2_GENERAL_CONFIRMATION_QUERY from UI controls
       
    36 /// As a core components UiControl's resource file is loaded in core Pbk2
       
    37 #include <Pbk2UIControls.rsg>
       
    38 #include <Pbk2USimUIRes.rsg>
       
    39 /// Use R_GENERAL_COPY_PROGRESS_NOTE from Commands
       
    40 /// As a core components Command's resource file is loaded in core Pbk2
       
    41 #include <Pbk2Commands.rsg>
       
    42 #include <Pbk2DataCaging.hrh>
       
    43 #include <MPbk2AppUi.h>
       
    44 #include <MPbk2ApplicationServices.h>
       
    45 #include <MPbk2StoreValidityInformer.h>
       
    46 
       
    47 // Virtual Phonebook
       
    48 #include <MVPbkContactStoreProperties.h>
       
    49 #include <CVPbkContactManager.h>
       
    50 #include <MVPbkContactStore.h>
       
    51 #include <MVPbkStoreContact.h>
       
    52 #include <MVPbkFieldType.h>
       
    53 #include <VPbkContactStoreUris.h>
       
    54 #include <CVPbkContactStoreUriArray.h>
       
    55 
       
    56 #include <MVPbkContactStoreList.h>
       
    57 #include <MVPbkContactStoreInfo.h>
       
    58 #include <MVPbkContactView.h>
       
    59 #include <CVPbkContactLinkArray.h>
       
    60 #include <MVPbkContactOperationBase.h>
       
    61 #include <CVPbkContactCopier.h>
       
    62 
       
    63 #include <TPsu2SimCopyResults.h>
       
    64 #include <MPbk2ContactNameFormatter.h>
       
    65 
       
    66 // System includes
       
    67 #include <AknQueryDialog.h>
       
    68 #include <e32property.h>
       
    69 #include <StringLoader.h>
       
    70 #include <e32cmn.h>
       
    71 #include <e32capability.h>
       
    72 
       
    73 // Debugging headers
       
    74 #include <Pbk2Debug.h>
       
    75 
       
    76 /// Unnamed namespace for local definitions
       
    77 namespace {
       
    78 
       
    79 
       
    80 const TInt KTargetStoresCount = 1;
       
    81 
       
    82 #ifdef _DEBUG
       
    83 enum TPanicCode
       
    84     {
       
    85     EStorePropertyNotFound_CreateSimViewsL
       
    86     };
       
    87 
       
    88 void Panic(TInt aReason)
       
    89     {
       
    90     _LIT(KPanicText, "CPsu2CopyAllToPbkCmd");
       
    91     User::Panic(KPanicText, aReason);
       
    92     }
       
    93 #endif // _DEBUG
       
    94 
       
    95 
       
    96 } /// namespace
       
    97 
       
    98 
       
    99 // --------------------------------------------------------------------------
       
   100 // CPsu2CopyAllToPbkCmd::CPsu2CopyAllToPbkCmd
       
   101 // --------------------------------------------------------------------------
       
   102 //
       
   103 CPsu2CopyAllToPbkCmd::CPsu2CopyAllToPbkCmd() 
       
   104     : CActive( EPriorityStandard ) 
       
   105     {
       
   106     }
       
   107 
       
   108 // --------------------------------------------------------------------------
       
   109 // CPsu2CopyAllToPbkCmd::~CPsu2CopyAllToPbkCmd
       
   110 // --------------------------------------------------------------------------
       
   111 //
       
   112 CPsu2CopyAllToPbkCmd::~CPsu2CopyAllToPbkCmd()
       
   113     {
       
   114     Cancel();
       
   115     delete iDecorator;
       
   116     delete iCopyOperation;
       
   117     delete iCopier;
       
   118     delete iCopiedContacts;
       
   119     delete iValidSourceStoreUris;
       
   120     iSourceViews.ResetAndDestroy();
       
   121     CloseStores();
       
   122     delete iSourceContactLinks;
       
   123     iSourceStores.Close();
       
   124     delete iTheOnlyContactTitle;
       
   125     }
       
   126 
       
   127 // --------------------------------------------------------------------------
       
   128 // CPsu2CopyAllToPbkCmd::NewL
       
   129 // --------------------------------------------------------------------------
       
   130 //
       
   131 CPsu2CopyAllToPbkCmd* CPsu2CopyAllToPbkCmd::NewL()
       
   132     {
       
   133     CPsu2CopyAllToPbkCmd* self =
       
   134         new( ELeave ) CPsu2CopyAllToPbkCmd( );
       
   135     CleanupStack::PushL( self );
       
   136     self->ConstructL();
       
   137     CleanupStack::Pop( self );
       
   138     return self;
       
   139     }
       
   140 
       
   141 // --------------------------------------------------------------------------
       
   142 // CPsu2CopyAllToPbkCmd::ConstructL
       
   143 // Symbian 2nd phase constructor can leave.
       
   144 // --------------------------------------------------------------------------
       
   145 //
       
   146 void CPsu2CopyAllToPbkCmd::ConstructL()
       
   147     {
       
   148     CActiveScheduler::Add(this);
       
   149 
       
   150     iValidSourceStoreUris = CVPbkContactStoreUriArray::NewL();
       
   151     iValidSourceStoreUris->AppendL( 
       
   152             VPbkContactStoreUris::SimGlobalSdnUri() );  
       
   153     iValidSourceStoreUris->AppendL( 
       
   154             VPbkContactStoreUris::SimGlobalAdnUri() );  
       
   155     }
       
   156 
       
   157 // --------------------------------------------------------------------------
       
   158 // CPsu2CopyAllToPbkCmd::RunL
       
   159 // --------------------------------------------------------------------------
       
   160 //
       
   161 void CPsu2CopyAllToPbkCmd::RunL()
       
   162     {
       
   163     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   164         ("CPsu2CopyAllToPbkCmd::RunL res %d task %d"),
       
   165         iStatus.Int(), iNextTask);
       
   166     
       
   167     switch ( iNextTask )
       
   168         {
       
   169         case EOpenStores:
       
   170             {
       
   171             OpenStoresL();
       
   172             break;
       
   173             }
       
   174         case ECheckSimStorePrerequisites:
       
   175             {
       
   176             CheckSimStorePrerequisitesL();
       
   177             break;
       
   178             }
       
   179         case ELaunchCopyAllSIMContactsQueryL:
       
   180             {
       
   181             LaunchCopyAllSIMContactsQueryL();
       
   182             break;
       
   183             }
       
   184         case ECreateSimViews:
       
   185             {
       
   186             CreateSimViewsL();
       
   187             break;
       
   188             }
       
   189         case ECopyContacts:
       
   190             {
       
   191             CopyContactsL();
       
   192             break;
       
   193             }
       
   194         case EComplete: // FALLTHROUGH
       
   195         default:
       
   196             {
       
   197             Complete();
       
   198             break;
       
   199             }
       
   200         }
       
   201     }
       
   202    
       
   203 // --------------------------------------------------------------------------
       
   204 // CPsu2CopyAllToPbkCmd::DoCancel
       
   205 // --------------------------------------------------------------------------
       
   206 //
       
   207 void CPsu2CopyAllToPbkCmd::DoCancel()
       
   208     {
       
   209     // Nothing to do here
       
   210     }
       
   211 
       
   212 // --------------------------------------------------------------------------
       
   213 // CPsu2CopyAllToPbkCmd::RunError
       
   214 // --------------------------------------------------------------------------
       
   215 //
       
   216 TInt CPsu2CopyAllToPbkCmd::RunError( TInt aError )
       
   217     {
       
   218     Finish( aError );
       
   219     return KErrNone;
       
   220     }
       
   221 
       
   222 // --------------------------------------------------------------------------
       
   223 // CPsu2CopyAllToPbkCmd::ExecuteLD
       
   224 // --------------------------------------------------------------------------
       
   225 //
       
   226 void CPsu2CopyAllToPbkCmd::ExecuteLD()
       
   227     {
       
   228     StartNext( EOpenStores );
       
   229     }
       
   230 
       
   231 // --------------------------------------------------------------------------
       
   232 // CPsu2CopyAllToPbkCmd::AddObserver
       
   233 // --------------------------------------------------------------------------
       
   234 //
       
   235 void CPsu2CopyAllToPbkCmd::AddObserver( MPbk2CommandObserver& aObserver )
       
   236     {
       
   237     iCommandObserver = &aObserver;
       
   238     }
       
   239 
       
   240 // --------------------------------------------------------------------------
       
   241 // CPsu2CopyAllToPbkCmd::ResetUiControl
       
   242 // --------------------------------------------------------------------------
       
   243 //
       
   244 void CPsu2CopyAllToPbkCmd::ResetUiControl
       
   245         ( MPbk2ContactUiControl& /*aUiControl*/ )
       
   246     {
       
   247     // Do nothing
       
   248     }
       
   249 
       
   250 // --------------------------------------------------------------------------
       
   251 // CPsu2CopyAllToPbkCmd::StoreReady
       
   252 // --------------------------------------------------------------------------
       
   253 //
       
   254 void CPsu2CopyAllToPbkCmd::StoreReady
       
   255         ( MVPbkContactStore& /*aContactStore*/)
       
   256     {
       
   257     ++iCounter;
       
   258     // KTargetStoresCount = 1, as we are always copying to phone memory
       
   259     if ( iNextTask == EOpenStores &&
       
   260          iSourceStores.Count() + KTargetStoresCount == iCounter ) 
       
   261         {
       
   262         StartNext(ECheckSimStorePrerequisites); 
       
   263         }
       
   264     
       
   265     // iNextTask == EOpenStores above
       
   266     //    --> ignore the case where a store later becomes ready and command is 
       
   267     //        already in later phases
       
   268     }
       
   269 
       
   270 // --------------------------------------------------------------------------
       
   271 // CPsu2CopyAllToPbkCmd::StoreUnavailable
       
   272 // --------------------------------------------------------------------------
       
   273 //
       
   274 void CPsu2CopyAllToPbkCmd::StoreUnavailable
       
   275         ( MVPbkContactStore& aContactStore,  TInt /*aReason*/ )
       
   276     {
       
   277     // Remove URI
       
   278     iValidSourceStoreUris->Remove( aContactStore.StoreProperties().Uri() );
       
   279 
       
   280     // Remove store
       
   281     const TInt count = iSourceStores.Count();
       
   282     TBool storeRemoved = EFalse;
       
   283     for ( TInt i = 0; i < count && !storeRemoved; ++i )
       
   284         {
       
   285         if ( iSourceStores[i] == &aContactStore )
       
   286             {
       
   287             iSourceStores[i]->Close( *this ); 
       
   288             iSourceStores.Remove( i );
       
   289             storeRemoved = ETrue;
       
   290             }
       
   291         }
       
   292 
       
   293     // If already moved on to next phase, ignore store becoming unavailable
       
   294     // let the ContactViewError or StepFailed handle this 
       
   295     if ( iNextTask == EOpenStores)
       
   296         {
       
   297         // If all source stores failed to open -> complete command
       
   298         if ( iSourceStores.Count() == 0 )
       
   299             {  
       
   300             StartNext( EComplete );
       
   301             }
       
   302         // All available stores are open
       
   303         else if ( iSourceStores.Count() + KTargetStoresCount == iCounter )
       
   304             {
       
   305             StartNext( ECheckSimStorePrerequisites );
       
   306             }
       
   307         }
       
   308     }
       
   309 
       
   310 // --------------------------------------------------------------------------
       
   311 // CPsu2CopyAllToPbkCmd::HandleStoreEventL
       
   312 // --------------------------------------------------------------------------
       
   313 //
       
   314 void CPsu2CopyAllToPbkCmd::HandleStoreEventL
       
   315         ( MVPbkContactStore& /*aContactStore*/,
       
   316           TVPbkContactStoreEvent /*aStoreEvent*/ )
       
   317     {
       
   318     // Nothing to be done here
       
   319     }
       
   320 
       
   321 // --------------------------------------------------------------------------
       
   322 // CPsu2CopyAllToPbkCmd::ContactViewReady
       
   323 // --------------------------------------------------------------------------
       
   324 //
       
   325 void CPsu2CopyAllToPbkCmd::ContactViewReady
       
   326         ( MVPbkContactViewBase& /*aView*/)
       
   327     {
       
   328     ++iCounter;
       
   329     if ( iSourceViews.Count() == iCounter )
       
   330         {
       
   331         StartNext( ECopyContacts );
       
   332         }
       
   333     }
       
   334 
       
   335 // --------------------------------------------------------------------------
       
   336 // CPsu2CopyAllToPbkCmd::ContactViewUnavailable
       
   337 // --------------------------------------------------------------------------
       
   338 //
       
   339 void CPsu2CopyAllToPbkCmd::ContactViewUnavailable
       
   340         ( MVPbkContactViewBase& /*aView*/ )
       
   341     {
       
   342     // Nothing to be done here, wait view ready events
       
   343     }
       
   344 
       
   345 // --------------------------------------------------------------------------
       
   346 // CPsu2CopyAllToPbkCmd::ContactAddedToView
       
   347 // --------------------------------------------------------------------------
       
   348 //
       
   349 void CPsu2CopyAllToPbkCmd::ContactAddedToView
       
   350         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   351           const MVPbkContactLink& /*aContactLink*/ )
       
   352     {
       
   353     // Nothing to be done here
       
   354     }
       
   355 
       
   356 // --------------------------------------------------------------------------
       
   357 // CPsu2CopyAllToPbkCmd::ContactRemovedFromView
       
   358 // --------------------------------------------------------------------------
       
   359 //
       
   360 void CPsu2CopyAllToPbkCmd::ContactRemovedFromView
       
   361         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   362           const MVPbkContactLink& /*aContactLink*/ )
       
   363     {
       
   364     // Nothing to be done here
       
   365     }
       
   366 
       
   367 // --------------------------------------------------------------------------
       
   368 // CPsu2CopyAllToPbkCmd::ContactViewError
       
   369 // --------------------------------------------------------------------------
       
   370 //
       
   371 void CPsu2CopyAllToPbkCmd::ContactViewError
       
   372         ( MVPbkContactViewBase& /*aView*/, TInt aError,
       
   373           TBool /*aErrorNotified*/ )
       
   374     { 
       
   375     Finish( aError );
       
   376     }
       
   377 
       
   378 // --------------------------------------------------------------------------
       
   379 // CPsu2CopyAllToPbkCmd::StepComplete
       
   380 // --------------------------------------------------------------------------
       
   381 //
       
   382 void CPsu2CopyAllToPbkCmd::StepComplete
       
   383         ( MVPbkContactOperationBase& /*aOperation*/, TInt aStepSize )
       
   384     {
       
   385     iCopiedSuccessfully += aStepSize;
       
   386     iDecorator->ProcessAdvance( aStepSize );
       
   387     }
       
   388 
       
   389 // --------------------------------------------------------------------------
       
   390 // CPsu2CopyAllToPbkCmd::StepFailed
       
   391 // --------------------------------------------------------------------------
       
   392 //
       
   393 TBool CPsu2CopyAllToPbkCmd::StepFailed
       
   394         ( MVPbkContactOperationBase& /*aOperation*/, TInt /*aStepSize*/,
       
   395           TInt aError )
       
   396     {
       
   397     Finish( aError );
       
   398     
       
   399     return EFalse;
       
   400     }
       
   401 
       
   402 // --------------------------------------------------------------------------
       
   403 // CPsu2CopyAllToPbkCmd::OperationComplete
       
   404 // --------------------------------------------------------------------------
       
   405 //
       
   406 void CPsu2CopyAllToPbkCmd::OperationComplete
       
   407         ( MVPbkContactOperationBase& /*aOperation*/ )
       
   408     {
       
   409     // Decorator calls ProcessDismissed
       
   410     iDecorator->ProcessStopped();
       
   411     }
       
   412 
       
   413 // --------------------------------------------------------------------------
       
   414 // CPsu2CopyAllToPbkCmd::ProcessDismissed
       
   415 // --------------------------------------------------------------------------
       
   416 //
       
   417 void CPsu2CopyAllToPbkCmd::ProcessDismissed( TInt /*aCancelCode*/ )
       
   418     {
       
   419     iDecoratorShown = EFalse;
       
   420     Cancel();
       
   421     delete iCopyOperation;
       
   422     iCopyOperation = NULL;
       
   423     StartNext( EComplete );
       
   424     }
       
   425 
       
   426 // --------------------------------------------------------------------------
       
   427 // CPsu2CopyAllToPbkCmd::OpenStoresL
       
   428 // --------------------------------------------------------------------------
       
   429 //
       
   430 void CPsu2CopyAllToPbkCmd::OpenStoresL()
       
   431     {
       
   432     // Load and open source stores to contact manager
       
   433     const TInt count = iValidSourceStoreUris->Count();
       
   434     for ( TInt i = 0; i < count; ++i )
       
   435         {
       
   436         Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   437             LoadContactStoreL( (*iValidSourceStoreUris)[i] );
       
   438         MVPbkContactStore* source =
       
   439             Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   440                 ContactStoresL().Find( (*iValidSourceStoreUris)[i] );
       
   441        if ( source )          
       
   442             {    
       
   443             iSourceStores.AppendL( source );
       
   444             source->OpenL( *this );
       
   445             }
       
   446         }
       
   447     
       
   448     // Also make sure the target store is always loaded and opened
       
   449     Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   450                 LoadContactStoreL( VPbkContactStoreUris::DefaultCntDbUri() );
       
   451     iTargetStore = Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   452                        ContactStoresL().Find( VPbkContactStoreUris::DefaultCntDbUri() );         
       
   453     iTargetStore->OpenL( *this );
       
   454     
       
   455     // Next: wait for StoreReady and/or StoreUnavailable notifications
       
   456     }
       
   457 
       
   458 // --------------------------------------------------------------------------
       
   459 // CPsu2CopyAllToPbkCmd::CheckSimStorePrerequisitesL
       
   460 // --------------------------------------------------------------------------
       
   461 //
       
   462 void CPsu2CopyAllToPbkCmd::CheckSimStorePrerequisitesL()
       
   463     {
       
   464     // If there are no contacts to copy then do nothing
       
   465     TInt sourceContactCount = 0;
       
   466     const TInt count = iSourceStores.Count();
       
   467     for ( TInt i = 0; i < count; ++i )
       
   468         {
       
   469         sourceContactCount +=
       
   470             iSourceStores[i]->StoreInfo().NumberOfContactsL();
       
   471         }
       
   472 
       
   473     if ( sourceContactCount > 0 )
       
   474         {    
       
   475         StartNext ( ELaunchCopyAllSIMContactsQueryL );
       
   476         }
       
   477     else
       
   478     	{
       
   479         StartNext ( EComplete );
       
   480     	}
       
   481     }
       
   482 
       
   483 // --------------------------------------------------------------------------
       
   484 // CPsu2CopyAllToPbkCmd::LaunchCopyAllSIMContactsQueryL
       
   485 // --------------------------------------------------------------------------
       
   486 //
       
   487 void CPsu2CopyAllToPbkCmd::LaunchCopyAllSIMContactsQueryL()
       
   488     {
       
   489     HBufC* text = StringLoader::LoadLC( R_QTN_SIMP_NOTE_COPY_ALL_SIM );
       
   490 
       
   491     CAknQueryDialog* dlg = CAknQueryDialog::NewL();
       
   492     TInt ret = dlg->ExecuteLD( R_PBK2_GENERAL_CONFIRMATION_QUERY, *text );
       
   493  	     	
       
   494     CleanupStack::PopAndDestroy( text );
       
   495     
       
   496     StartNext ( ret ? ECreateSimViews : EComplete );    
       
   497     }
       
   498 
       
   499 // --------------------------------------------------------------------------
       
   500 // CPsu2CopyAllToPbkCmd::CreateSimViewsL
       
   501 // --------------------------------------------------------------------------
       
   502 //
       
   503 void CPsu2CopyAllToPbkCmd::CreateSimViewsL()
       
   504     {
       
   505     // Set views ready counter to zero
       
   506     iCounter = 0;
       
   507 
       
   508     const TInt count = iSourceStores.Count();
       
   509     for ( TInt i = 0; i < count; ++i )
       
   510         {
       
   511         // Find store property
       
   512         const CPbk2StoreProperty* prop =
       
   513             Phonebook2::Pbk2AppUi()->ApplicationServices().StoreProperties().
       
   514                 FindProperty( iSourceStores[i]->StoreProperties().Uri() );
       
   515         __ASSERT_DEBUG( prop,
       
   516             Panic( EStorePropertyNotFound_CreateSimViewsL ) );
       
   517         // Get Virtual Phonebook view definition
       
   518         // There is only one view defintion for SIM stores
       
   519         // -> take the first one
       
   520         const CVPbkContactViewDefinition& viewDef =
       
   521             (prop->Views())[0]->ViewDefinition();
       
   522         MVPbkContactView* view = iSourceStores[i]->CreateViewLC(
       
   523             viewDef,
       
   524             *this,
       
   525             Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   526                 SortOrderManager().SortOrder() );
       
   527         iSourceViews.AppendL( view );
       
   528         CleanupStack::Pop(); // view
       
   529         }
       
   530     }
       
   531 
       
   532 // --------------------------------------------------------------------------
       
   533 // CPsu2CopyAllToPbkCmd::CopyContactsL
       
   534 // --------------------------------------------------------------------------
       
   535 //
       
   536 void CPsu2CopyAllToPbkCmd::CopyContactsL()
       
   537     {
       
   538     if ( !iCopier )
       
   539         {
       
   540         iCopier = CVPbkContactCopier::NewL
       
   541             ( Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager() );
       
   542         }
       
   543     
       
   544     // Create an array for source contact links
       
   545     if ( !iSourceContactLinks )
       
   546         {
       
   547         iSourceContactLinks = CVPbkContactLinkArray::NewL();
       
   548         }
       
   549 
       
   550     if ( !iCopiedContacts )
       
   551         {
       
   552         iCopiedContacts = CVPbkContactLinkArray::NewL();
       
   553         }
       
   554     
       
   555     // Create links to the contacts that are copied
       
   556     const TInt viewCount = iSourceViews.Count();
       
   557     
       
   558     for ( TInt i = 0; i < viewCount; ++i )
       
   559         {
       
   560         const TInt contactCount = iSourceViews[i]->ContactCountL();
       
   561         for( TInt j = 0; j < contactCount; ++j )
       
   562             {
       
   563             // store the first contact's title, if only one contact copied,
       
   564             // show the name of that single contact in the results note.
       
   565             if ( j == 0 )
       
   566                 {
       
   567                 iTheOnlyContactTitle = Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   568                         NameFormatter().GetContactTitleL(
       
   569                                 iSourceViews[i]->ContactAtL(j).Fields(),
       
   570                                 MPbk2ContactNameFormatter::EPreserveLeadingSpaces);
       
   571                 }
       
   572             // create link
       
   573             MVPbkContactLink* link =
       
   574                 iSourceViews[i]->ContactAtL(j).CreateLinkLC();
       
   575             iSourceContactLinks->AppendL( link );
       
   576             CleanupStack::Pop(); // link
       
   577             }
       
   578         }
       
   579 
       
   580     // Views can be destroyed now because links have been created
       
   581     iSourceViews.ResetAndDestroy();
       
   582 
       
   583     // Get target store
       
   584     MVPbkContactStore* targetStore =
       
   585         Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   586             ContactStoresL().Find( VPbkContactStoreUris::DefaultCntDbUri() );
       
   587    
       
   588     
       
   589     if (targetStore)
       
   590         {
       
   591         // Start copying
       
   592         iDecorator = Pbk2ProcessDecoratorFactory::CreateProgressDialogDecoratorL
       
   593                 ( R_GENERAL_COPY_PROGRESS_NOTE, EFalse );
       
   594         iDecorator->SetObserver( *this );
       
   595      
       
   596         iCopiedContacts->ResetAndDestroy();      
       
   597         iCopyOperation = iCopier->CopyContactsL(
       
   598                 CVPbkContactCopier::EVPbkUsePlatformSpecificDuplicatePolicy,
       
   599                 *iSourceContactLinks, targetStore, *iCopiedContacts, *this );
       
   600 
       
   601         // Show progress note
       
   602         iDecorator->ProcessStartedL( iSourceContactLinks->Count() );
       
   603         iDecoratorShown = ETrue;
       
   604         }
       
   605     else
       
   606         {
       
   607         StartNext( EComplete );
       
   608         }
       
   609     }
       
   610 
       
   611 // --------------------------------------------------------------------------
       
   612 // CPsu2CopyAllToPbkCmd::ShowResultsL
       
   613 // --------------------------------------------------------------------------
       
   614 //
       
   615 void CPsu2CopyAllToPbkCmd::ShowResultsL()
       
   616     {
       
   617 
       
   618     if (iSourceContactLinks->Count() == 1 && iTheOnlyContactTitle)
       
   619         {
       
   620         TPsu2SimCopyResults  results(
       
   621                 iCopiedSuccessfully, iTheOnlyContactTitle->Des() );
       
   622         results.ShowNoteL();
       
   623         }
       
   624     else 
       
   625         {
       
   626         TPsu2SimCopyResults  results(
       
   627                 iCopiedSuccessfully, iSourceContactLinks->Count() );
       
   628         results.ShowNoteL();
       
   629         }
       
   630    
       
   631     }
       
   632 
       
   633 // --------------------------------------------------------------------------
       
   634 // CPsu2CopyAllToPbkCmd::Complete   
       
   635 // --------------------------------------------------------------------------
       
   636 //
       
   637 void CPsu2CopyAllToPbkCmd::Complete()
       
   638     {    
       
   639     if ( iSourceContactLinks )
       
   640         {
       
   641         TRAP_IGNORE( ShowResultsL() );
       
   642         }
       
   643     
       
   644     iCommandObserver->CommandFinished( *this );
       
   645     }
       
   646 
       
   647 // ---------------------------------------------------------------------------
       
   648 // CPsu2CopyAllToPbkCmd::Finish
       
   649 // ---------------------------------------------------------------------------
       
   650 //
       
   651 void CPsu2CopyAllToPbkCmd::Finish( TInt aReason )
       
   652     {
       
   653     if ( aReason != KErrNone && aReason != KErrCancel )
       
   654         {
       
   655         CCoeEnv::Static()->HandleError( aReason );
       
   656         }
       
   657     
       
   658     if (iDecorator && iDecoratorShown)
       
   659         {
       
   660         // Decorator calls ProcessDismissed --> StartNext( EComplete );
       
   661         iDecorator->ProcessStopped();
       
   662         }
       
   663     else
       
   664         {
       
   665         StartNext( EComplete );
       
   666         }
       
   667     }
       
   668 
       
   669 // ---------------------------------------------------------------------------
       
   670 // CPsu2CopyAllToPbkCmd::StartNext
       
   671 // ---------------------------------------------------------------------------
       
   672 //
       
   673 void CPsu2CopyAllToPbkCmd::StartNext( TNextTask aNextTask )
       
   674     {
       
   675     iNextTask = aNextTask;
       
   676     
       
   677     TRequestStatus* status = &iStatus;
       
   678     User::RequestComplete( status, KErrNone );
       
   679     SetActive();
       
   680     }
       
   681 
       
   682 // --------------------------------------------------------------------------
       
   683 // CPsu2CopyAllToPbkCmd::CloseStores
       
   684 // --------------------------------------------------------------------------
       
   685 //
       
   686 void CPsu2CopyAllToPbkCmd::CloseStores()
       
   687     {
       
   688     const TInt count = iSourceStores.Count();
       
   689     for ( TInt i = 0; i < count; ++i )
       
   690         {
       
   691         iSourceStores[i]->Close( *this );
       
   692         }
       
   693     
       
   694     iTargetStore->Close( *this );
       
   695     }
       
   696 
       
   697 //  End of File