phonebookui/Phonebook2/NamesListExtension/src/CPbk2NlxAddToTopContactsCmd.cpp
changeset 0 e686773b3f54
child 5 81f8547efd4f
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 add to top contacts launcher command
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CPbk2NlxAddToTopContactsCmd.h"
       
    21 
       
    22 #include <StringLoader.h>
       
    23 #include <aknnotewrappers.h>
       
    24 #include <Pbk2UIControls.rsg> 
       
    25 #include <Pbk2CommonUi.rsg>
       
    26 #include <Pbk2CmdExtRes.rsg>
       
    27 #include <Pbk2Commands.rsg>
       
    28 
       
    29 #include <Pbk2ProcessDecoratorFactory.h>
       
    30 #include <CPbk2NamesListControl.h>
       
    31 #include <MPbk2CommandObserver.h>
       
    32 #include <MPbk2ContactUiControl.h>
       
    33 #include <CPbk2AppUiBase.h>
       
    34 #include <MPbk2ApplicationServices.h>
       
    35 #include <CVPbkContactManager.h>
       
    36 #include <VPbkContactStoreUris.h>
       
    37 #include <MVPbkContactStore.h>
       
    38 #include <MVPbkContactStoreProperties.h>
       
    39 #include <MVPbkContactOperationBase.h>
       
    40 #include <MVPbkContactLink.h>
       
    41 #include <MVPbkStoreContact.h>
       
    42 #include <Pbk2ExNamesListRes.rsg>
       
    43 #include <Phonebook2PrivateCRKeys.h>
       
    44 #include <centralrepository.h>
       
    45 #include <CVPbkTopContactManager.h>
       
    46 #include <CPbk2FetchDlg.h>
       
    47 #include <MPbk2ContactViewSupplier.h>
       
    48 #include <CPbk2ContactViewBuilder.h>
       
    49 #include <MVPbkContactViewBase.h>
       
    50 #include <CPbk2StoreConfiguration.h>
       
    51 #include <CVPbkContactStoreUriArray.h>
       
    52 #include <CPbk2StorePropertyArray.h>
       
    53 #include <CPbk2StoreProperty.h>
       
    54 #include <Pbk2StoreProperty.hrh>
       
    55 #include <CVPbkFilteredContactView.h>
       
    56 #include <CPbk2ContactRelocator.h>
       
    57 #include "TPbk2ContactIterator.h"
       
    58 
       
    59 // Debugging headers
       
    60 #include <Pbk2Debug.h>
       
    61 #include <Pbk2Profile.h>
       
    62 
       
    63 /// Unnamed namespace for local definitions
       
    64 namespace {
       
    65 
       
    66     enum TNlxPanicCodes
       
    67         {
       
    68         ENlxAtcCommandObserverMissing,
       
    69         ENlxAtcSingleContactRelocation,
       
    70         ENlxAtcSingleContactRelocation2,
       
    71         ENlxAtcSingleTopContactOperation,
       
    72         ENlxAtcWronglyActivated,
       
    73         ENlxAtcDuplicateCallToExecuteLD,
       
    74         ENlxUnexpectedFailurePath,
       
    75         ENlxNoHighlightedContact,
       
    76         ENlxViewsAlreadyCreated,
       
    77         ENlxMergePrecond,
       
    78         ENlxRemoveSimPrecond,
       
    79         ENlxNoteActive
       
    80         };
       
    81     
       
    82     void Panic(TNlxPanicCodes aReason)
       
    83         {
       
    84         _LIT( KPanicText, "CPbk2_nlx_AddToTC" );
       
    85         User::Panic(KPanicText,aReason);
       
    86         }
       
    87 
       
    88     CVPbkContactLinkArray* CloneL(
       
    89         const MVPbkContactLinkArray& aArray )
       
    90         {
       
    91         CVPbkContactLinkArray* newArray = CVPbkContactLinkArray::NewLC();
       
    92         const TInt count = aArray.Count();
       
    93         for ( TInt n = 0; n < count; ++n )
       
    94             {
       
    95             MVPbkContactLink* link = aArray.At(n).CloneLC();
       
    96             newArray->AppendL( link );
       
    97             CleanupStack::Pop(); // link
       
    98             }
       
    99         CleanupStack::Pop( newArray );
       
   100         return newArray;
       
   101         }
       
   102 
       
   103 } /// namespace
       
   104 
       
   105 
       
   106 // --------------------------------------------------------------------------
       
   107 // CPbk2NlxAddToTopContactsCmd::CPbk2NlxAddToTopContactsCmd
       
   108 // --------------------------------------------------------------------------
       
   109 //
       
   110 CPbk2NlxAddToTopContactsCmd::CPbk2NlxAddToTopContactsCmd(
       
   111     MPbk2ContactUiControl& aUiControl,
       
   112     TBool aAddFavourites ) :
       
   113         CActive( EPriorityStandard ),
       
   114         iNextPhase( EPhaseNone ),
       
   115         iUiControl( &aUiControl ),
       
   116         iAddFavourites( aAddFavourites )
       
   117     {
       
   118     CActiveScheduler::Add( this );
       
   119     }
       
   120 
       
   121 // --------------------------------------------------------------------------
       
   122 // CPbk2NlxAddToTopContactsCmd::~CPbk2NlxAddToTopContactsCmd
       
   123 // --------------------------------------------------------------------------
       
   124 //
       
   125 CPbk2NlxAddToTopContactsCmd::~CPbk2NlxAddToTopContactsCmd()
       
   126     {
       
   127     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   128         ("CPbk2NlxAddToTopContactsCmd(%x)::~CPbk2NlxAddToTopContactsCmd()"), 
       
   129         this);
       
   130     
       
   131     Cancel();
       
   132     if (iUiControl)
       
   133         {
       
   134         iUiControl->RegisterCommand( NULL );
       
   135         }
       
   136 
       
   137     CleanAfterFetching();
       
   138     delete iRelocatedContacts;
       
   139     delete iMarkedEntries;
       
   140     delete iContactIterator;
       
   141     delete iContactOperation; //Cancels request if pending
       
   142     delete iDecorator;
       
   143     delete iVPbkTopContactManager;
       
   144     delete iContactRelocator;
       
   145     }
       
   146 
       
   147 // --------------------------------------------------------------------------
       
   148 // CPbk2NlxAddToTopContactsCmd::NewL
       
   149 // --------------------------------------------------------------------------
       
   150 //
       
   151 CPbk2NlxAddToTopContactsCmd* CPbk2NlxAddToTopContactsCmd::NewL(
       
   152     MPbk2ContactUiControl& aUiControl, TBool aAddFavourites )
       
   153     {
       
   154     CPbk2NlxAddToTopContactsCmd* self = 
       
   155         new ( ELeave ) CPbk2NlxAddToTopContactsCmd(
       
   156         aUiControl, aAddFavourites );
       
   157     CleanupStack::PushL( self );
       
   158     self->ConstructL();
       
   159     CleanupStack::Pop( self );
       
   160     return self;
       
   161     }
       
   162 
       
   163 // --------------------------------------------------------------------------
       
   164 // CPbk2NlxAddToTopContactsCmd::ConstructL
       
   165 // --------------------------------------------------------------------------
       
   166 //
       
   167 void CPbk2NlxAddToTopContactsCmd::ConstructL()
       
   168     {    
       
   169     if( iUiControl )
       
   170         {
       
   171         iUiControl->RegisterCommand( this );
       
   172         }    
       
   173     
       
   174     iContactManager =
       
   175         &Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager();
       
   176     iVPbkTopContactManager = CVPbkTopContactManager::NewL( *iContactManager );
       
   177     }
       
   178     
       
   179 // --------------------------------------------------------------------------
       
   180 // CPbk2NlxAddToTopContactsCmd::ExecuteLD
       
   181 // --------------------------------------------------------------------------
       
   182 //
       
   183 void CPbk2NlxAddToTopContactsCmd::ExecuteLD()
       
   184     {
       
   185     __ASSERT_ALWAYS( iCommandObserver, Panic( ENlxAtcCommandObserverMissing ));	    
       
   186     __ASSERT_ALWAYS( !IsActive(), Panic( ENlxAtcWronglyActivated ));	
       
   187     __ASSERT_ALWAYS( iNextPhase == EPhaseNone, Panic( ENlxAtcDuplicateCallToExecuteLD ));	
       
   188 
       
   189     if( iUiControl )
       
   190         {
       
   191         iUiControl->SetBlank( ETrue );
       
   192         } 
       
   193 
       
   194     if ( iAddFavourites )
       
   195         {
       
   196         // The user has started the "Add favorites" namesList command.
       
   197         // Launch Fetch dialog asynchronously.
       
   198         GetSelectionWithFetchUiL();
       
   199         // The Fetch dialog will inform fetch completion with a callback.
       
   200         }
       
   201     else
       
   202         {
       
   203         // The user has selected "Set as favorite" from options menu.
       
   204         GetUiControlSelectionL();
       
   205         }
       
   206     
       
   207     //This can be deleted only when callback of operation is done.
       
   208     //Ownership of this object is in caller (i.e. CPbk2CommandActivator)
       
   209     //No push and pop to cleanupstack in this function must be done.
       
   210     }
       
   211     
       
   212 // --------------------------------------------------------------------------
       
   213 // CPbk2NlxAddToTopContactsCmd::ResetUiControl
       
   214 // --------------------------------------------------------------------------
       
   215 //
       
   216 void CPbk2NlxAddToTopContactsCmd::ResetUiControl( 
       
   217     MPbk2ContactUiControl& aUiControl )
       
   218     {
       
   219     if ( iUiControl == &aUiControl )
       
   220         {
       
   221         iUiControl = NULL;
       
   222         }
       
   223     }
       
   224     
       
   225 // --------------------------------------------------------------------------
       
   226 // CPbk2NlxAddToTopContactsCmd::AddObserver
       
   227 // --------------------------------------------------------------------------
       
   228 //
       
   229 void CPbk2NlxAddToTopContactsCmd::AddObserver( 
       
   230     MPbk2CommandObserver& aObserver )
       
   231     {
       
   232     iCommandObserver = &aObserver;
       
   233     }        
       
   234 
       
   235 
       
   236 // --------------------------------------------------------------------------
       
   237 // CPbk2NlxAddToTopContactsCmd::RunL
       
   238 // --------------------------------------------------------------------------
       
   239 //
       
   240 void CPbk2NlxAddToTopContactsCmd::RunL()
       
   241     {
       
   242     if( !iUiControl )
       
   243         {
       
   244         //UiControl reseted, so cancel all processing
       
   245         iNextPhase = EFinish ;
       
   246         }
       
   247 
       
   248     switch( iNextPhase )
       
   249     	{
       
   250     	case ERelocate:
       
   251     	    {
       
   252     	    CleanAfterFetching();
       
   253             const TInt numberOfSimContacts = CountSimContacts();
       
   254     	    if ( numberOfSimContacts > 0 )
       
   255                 {
       
   256                 StartSimContactsRelocationL();
       
   257                 iNextPhase = ESetAsTopContact;
       
   258                 }
       
   259             else
       
   260                 {
       
   261                 StartNext(ESetAsTopContact);
       
   262                 }
       
   263     	    ShowWaitNoteL();
       
   264     	    }
       
   265     	    break;
       
   266         case ESetAsTopContact:
       
   267             {
       
   268             // prepare iMarkedEntries
       
   269             ReplaceSimContactsL();
       
   270             // set as top
       
   271             iContactOperation = iVPbkTopContactManager->AddToTopL(
       
   272                     *iMarkedEntries,
       
   273                     *this,
       
   274                     *this );
       
   275             }
       
   276             break;
       
   277     	case EFinish:
       
   278         	{
       
   279             if (iUiControl)
       
   280                 {
       
   281                 iUiControl->SetBlank(EFalse);
       
   282                 iUiControl->UpdateAfterCommandExecution();
       
   283                 }
       
   284             if ( iCommandObserver )
       
   285                 {
       
   286                 iCommandObserver->CommandFinished( *this );
       
   287                 }
       
   288         	}
       
   289     		break;
       
   290     	default:
       
   291     		Panic( ENlxAtcWronglyActivated );
       
   292     		break;
       
   293     	}
       
   294     }    
       
   295 
       
   296 // --------------------------------------------------------------------------
       
   297 // CPbk2NlxAddToTopContactsCmd::RunError
       
   298 // --------------------------------------------------------------------------
       
   299 //
       
   300 TInt CPbk2NlxAddToTopContactsCmd::RunError(TInt aError)
       
   301 	{
       
   302 	Finish( aError );
       
   303     return KErrNone;
       
   304 	}
       
   305     
       
   306 // --------------------------------------------------------------------------
       
   307 // CPbk2NlxAddToTopContactsCmd::DoCancel
       
   308 // --------------------------------------------------------------------------
       
   309 //
       
   310 void CPbk2NlxAddToTopContactsCmd::DoCancel()
       
   311     {
       
   312     }
       
   313 
       
   314 MPbk2FetchDlgObserver::TPbk2FetchAcceptSelection CPbk2NlxAddToTopContactsCmd::AcceptFetchSelectionL(
       
   315     TInt /*aNumMarkedEntries*/,
       
   316     MVPbkContactLink& /*aLastSelection*/ )
       
   317     {
       
   318     return MPbk2FetchDlgObserver::EFetchYes;
       
   319     }
       
   320 
       
   321 void CPbk2NlxAddToTopContactsCmd::FetchCompletedL(
       
   322     MVPbkContactLinkArray* aMarkedEntries )
       
   323     {
       
   324     // Don't take ownership of aMarkedEntries!
       
   325     
       
   326     // If nothing was marked, then aMarkedEntries can be NULL
       
   327     if ( aMarkedEntries )
       
   328         {
       
   329         // Yes, something was selected by user.
       
   330         iMarkedEntries = CloneL( *aMarkedEntries );
       
   331         TPbk2ContactIterator* iterator = new (ELeave) TPbk2ContactIterator;
       
   332         iterator->SetLinkArray( *iMarkedEntries );
       
   333         delete iContactIterator;
       
   334         iContactIterator = iterator; // takes ownership
       
   335         
       
   336         StartNext(); // Goes to RunL()
       
   337         }
       
   338     else
       
   339         {
       
   340         // Nothing was selected by user.
       
   341         Finish( KErrNone );
       
   342         }
       
   343     }
       
   344 
       
   345 void CPbk2NlxAddToTopContactsCmd::FetchCanceled()
       
   346     {
       
   347     Finish( KErrCancel );
       
   348     }
       
   349 
       
   350 void CPbk2NlxAddToTopContactsCmd::FetchAborted()
       
   351     {
       
   352     Finish( KErrCancel );
       
   353     }
       
   354 
       
   355 TBool CPbk2NlxAddToTopContactsCmd::FetchOkToExit()
       
   356     {
       
   357     return ETrue;
       
   358     }
       
   359 
       
   360 void CPbk2NlxAddToTopContactsCmd::GetSelectionWithFetchUiL()
       
   361     {
       
   362     __ASSERT_DEBUG( !iNonEmptyGroupsView, Panic( ENlxViewsAlreadyCreated ));
       
   363     __ASSERT_DEBUG( !iAllContactsView, Panic( ENlxViewsAlreadyCreated ));
       
   364     
       
   365     CPbk2FetchDlg::TParams params;
       
   366     params.iResId = R_PBK2_MULTIPLE_ENTRY_FETCH_DLG;
       
   367     
       
   368     CPbk2StorePropertyArray& storeProperties =
       
   369         Phonebook2::Pbk2AppUi()->ApplicationServices().StoreProperties();
       
   370     CPbk2ContactViewBuilder* viewBuilder = CPbk2ContactViewBuilder::NewLC
       
   371             ( *iContactManager,
       
   372               storeProperties );    
       
   373     
       
   374     // Fetch dlg uses this view instead of AllNameslistView
       
   375     iStoreUris = 
       
   376             Phonebook2::Pbk2AppUi()->ApplicationServices().StoreConfiguration().CurrentConfigurationL();
       
   377     TBool createNewView = EFalse;
       
   378     for ( TInt i = iStoreUris->Count()-1; i >= 0; --i )
       
   379         {
       
   380         const CPbk2StoreProperty* storeProperty =
       
   381             storeProperties.FindProperty( (*iStoreUris)[i] );
       
   382         if ( !( storeProperty->Flags() & KPbk2DefaultSavingStorageItem ) )
       
   383             {
       
   384             // Remove read only URI from the array
       
   385             iStoreUris->Remove( (*iStoreUris)[i] );
       
   386             createNewView = ETrue;
       
   387             }
       
   388         }    
       
   389     
       
   390     if ( createNewView )
       
   391         {
       
   392         iAllContactsView = CVPbkFilteredContactView::NewL
       
   393             ( *Phonebook2::Pbk2AppUi()->ApplicationServices().ViewSupplier().
       
   394               AllContactsViewL(), *this, *this,
       
   395               iContactManager->FieldTypes() );
       
   396         
       
   397         params.iNamesListView = iAllContactsView;
       
   398         }
       
   399     else
       
   400         {
       
   401         params.iNamesListView =
       
   402             Phonebook2::Pbk2AppUi()->ApplicationServices().ViewSupplier().AllContactsViewL();
       
   403         }    
       
   404 
       
   405     // create group view without empty groups
       
   406 
       
   407     //use ready sort order from normal AllGroups view
       
   408     const MVPbkFieldTypeList& sortOrder = 
       
   409     Phonebook2::Pbk2AppUi()->ApplicationServices().ViewSupplier().AllGroupsViewL()->SortOrder();
       
   410     iNonEmptyGroupsView = viewBuilder->CreateGroupViewForStoresLC
       
   411                 ( *iStoreUris, *this, sortOrder,
       
   412                   NULL, // no view filtering
       
   413                   EVPbkExcludeEmptyGroups );
       
   414     CleanupStack::Pop(); // iNonEmptyGroupsView
       
   415     
       
   416     CleanupStack::PopAndDestroy( viewBuilder );
       
   417     //CleanupStack::PopAndDestroy( storeUris );
       
   418         
       
   419     params.iGroupsListView = iNonEmptyGroupsView;
       
   420     params.iFlags = CPbk2FetchDlg::EFetchMultiple;
       
   421 
       
   422     CPbk2FetchDlg* dlg = CPbk2FetchDlg::NewL( params, *this );
       
   423     iFetchDlgEliminator = dlg;
       
   424     iFetchDlgEliminator->ResetWhenDestroyed( &iFetchDlgEliminator );
       
   425     
       
   426     // The ExecuteLD is asyncrhonous.
       
   427     dlg->ExecuteLD(); // Completion is signalled with a callback.
       
   428     
       
   429     iNextPhase = ERelocate;
       
   430     }
       
   431 
       
   432 TBool CPbk2NlxAddToTopContactsCmd::IsContactIncluded
       
   433         ( const MVPbkBaseContact& aContact )
       
   434     {
       
   435     for ( TInt i = iStoreUris->Count()-1; i >= 0; --i )
       
   436         {
       
   437         if ( aContact.MatchContactStore((*iStoreUris)[i].UriDes()) )
       
   438             {
       
   439             return ETrue;
       
   440             }
       
   441         }  
       
   442 
       
   443     return EFalse;
       
   444     }
       
   445 
       
   446 // insure the fetch dlg is deleted and destroy resources used by it 
       
   447 void CPbk2NlxAddToTopContactsCmd::CleanAfterFetching()
       
   448     {
       
   449     if ( iFetchDlgEliminator )
       
   450         {
       
   451         iFetchDlgEliminator->ForceExit();
       
   452         }
       
   453     delete iNonEmptyGroupsView;
       
   454     iNonEmptyGroupsView = NULL;
       
   455     delete iAllContactsView;
       
   456     iAllContactsView = NULL;   
       
   457     delete iStoreUris;
       
   458     iStoreUris = NULL;
       
   459     }
       
   460 
       
   461 void CPbk2NlxAddToTopContactsCmd::GetUiControlSelectionL()
       
   462     {
       
   463     // First delete any previous iterator.
       
   464     if ( iUiControl )
       
   465         {
       
   466         delete iContactIterator;
       
   467 		iContactIterator = NULL;
       
   468 
       
   469         // Take the selection from the UI control.
       
   470         const MVPbkBaseContact* contact = NULL;
       
   471 	    iContactIterator = iUiControl->SelectedContactsIteratorL();
       
   472 	    if ( !iContactIterator )
       
   473 	        {
       
   474 	        // Nothing was marked. Use the highlighted contact.
       
   475 	        delete iMarkedEntries;
       
   476 	        iMarkedEntries = NULL;
       
   477 	        contact = iUiControl->FocusedContactL();
       
   478 	        if (contact)
       
   479 	            {
       
   480 	            MVPbkContactLink* link = contact->CreateLinkLC();
       
   481 	            CVPbkContactLinkArray* array = CVPbkContactLinkArray::NewL();
       
   482 	            iMarkedEntries = array; // take ownership here
       
   483 	            array->AppendL( link );
       
   484 	            CleanupStack::Pop(); // link
       
   485 	            // Create an iterator for the link array.
       
   486 	            TPbk2ContactIterator* iterator = new (ELeave) TPbk2ContactIterator;
       
   487 	            iterator->SetLinkArray( *iMarkedEntries );
       
   488 	            iContactIterator = iterator;
       
   489 	            }
       
   490 	        }
       
   491 		if (contact)
       
   492 			{
       
   493 			StartNext(ERelocate);
       
   494 			}
       
   495 		else
       
   496 			{
       
   497 			Finish(KErrNone);
       
   498 			}
       
   499         }
       
   500     }
       
   501 
       
   502 TInt CPbk2NlxAddToTopContactsCmd::CountSimContacts() const
       
   503     {
       
   504     TInt result = 0;
       
   505     
       
   506     const TInt markedCount = iMarkedEntries->Count();
       
   507     for (TInt i = 0; i < markedCount; ++i)
       
   508         {
       
   509         if ( !iMarkedEntries->At(i).ContactStore().StoreProperties().Uri().Compare(
       
   510                 VPbkContactStoreUris::SimGlobalAdnUri(),
       
   511                 TVPbkContactStoreUriPtr::EContactStoreUriAllComponents))
       
   512             {
       
   513             ++result;
       
   514             }
       
   515         }
       
   516 
       
   517     return result;
       
   518     }
       
   519 
       
   520 CVPbkContactLinkArray* CPbk2NlxAddToTopContactsCmd::CreateSimContactsArrayLC() const
       
   521     {
       
   522     CVPbkContactLinkArray* result = CVPbkContactLinkArray::NewLC();
       
   523     iContactIterator->SetToFirst();
       
   524     while( iContactIterator->HasNext() )
       
   525         {
       
   526         MVPbkContactLink* link = iContactIterator->NextL();
       
   527         CleanupDeletePushL( link );
       
   528         
       
   529         if ( !link->ContactStore().StoreProperties().Uri().Compare(
       
   530                 VPbkContactStoreUris::SimGlobalAdnUri(),
       
   531                 TVPbkContactStoreUriPtr::EContactStoreUriAllComponents))
       
   532             {
       
   533             result->AppendL(link);
       
   534             CleanupStack::Pop(); // link
       
   535             }
       
   536         else
       
   537             {
       
   538             CleanupStack::PopAndDestroy(); // link
       
   539             }
       
   540         }
       
   541     
       
   542     return result;
       
   543     }
       
   544 
       
   545 // Replace or remove sim contact links from iMarkedEntries
       
   546 void CPbk2NlxAddToTopContactsCmd::ReplaceSimContactsL()
       
   547     {
       
   548     __ASSERT_DEBUG( iMarkedEntries, Panic(ENlxMergePrecond) );
       
   549     
       
   550     for (TInt i = iMarkedEntries->Count()-1; i >= 0; --i)
       
   551         {
       
   552         if ( !iMarkedEntries->At(i).ContactStore().StoreProperties().Uri().Compare(
       
   553                 VPbkContactStoreUris::SimGlobalAdnUri(),
       
   554                 TVPbkContactStoreUriPtr::EContactStoreUriAllComponents))
       
   555             {
       
   556             // replace link with relocated contact link if there are any,
       
   557             // otherwise just delete it
       
   558             iMarkedEntries->Delete(i);
       
   559             if ( iRelocatedContacts && iRelocatedContacts->Count() > 0 )
       
   560                 {
       
   561                 const TInt lastLinkIndex = iRelocatedContacts->Count()-1;
       
   562                 MVPbkContactLink* link = iRelocatedContacts->At(lastLinkIndex).CloneLC();
       
   563                 iMarkedEntries->InsertL( link, i );
       
   564                 CleanupStack::Pop();//link
       
   565                 iRelocatedContacts->Delete(lastLinkIndex);
       
   566                 }
       
   567             }
       
   568         }
       
   569     }
       
   570 
       
   571 void CPbk2NlxAddToTopContactsCmd::StartSimContactsRelocationL()
       
   572     {
       
   573     if( !iContactRelocator )
       
   574         {
       
   575         iContactRelocator = CPbk2ContactRelocator::NewL();
       
   576         }
       
   577     CVPbkContactLinkArray* simContacts = CreateSimContactsArrayLC();
       
   578     iContactRelocator->RelocateContactsL(
       
   579             simContacts,
       
   580             *this,
       
   581             Pbk2ContactRelocator::EPbk2DisplayNoQueries );
       
   582     //Relocator took ownership
       
   583     CleanupStack::Pop(); //simContacts
       
   584     }
       
   585 
       
   586 // ---------------------------------------------------------------------------
       
   587 // CPbk2NlxAddToTopContactsCmd::Finish
       
   588 // ---------------------------------------------------------------------------
       
   589 //
       
   590 void CPbk2NlxAddToTopContactsCmd::Finish( TInt aReason )
       
   591 	{
       
   592 	if ( aReason != KErrNone && aReason != KErrCancel )
       
   593 	    {
       
   594 	    CCoeEnv::Static()->HandleError( aReason );
       
   595 	    }
       
   596 	
       
   597     if ( iDecorator )
       
   598         {
       
   599         // wait for callback from the wait note and finish then
       
   600         iDecorator->ProcessStopped();
       
   601         }
       
   602     else
       
   603         {
       
   604         StartNext( EFinish );
       
   605         }
       
   606 	}
       
   607 
       
   608 // ---------------------------------------------------------------------------
       
   609 // CPbk2NlxAddToTopContactsCmd::StartNext
       
   610 // ---------------------------------------------------------------------------
       
   611 //
       
   612 void CPbk2NlxAddToTopContactsCmd::StartNext( TPhase aPhase )
       
   613     {
       
   614     __ASSERT_DEBUG( !IsActive(), Panic( ENlxAtcWronglyActivated ));    
       
   615     iNextPhase = aPhase;    
       
   616     TRequestStatus* status = &iStatus;
       
   617     User::RequestComplete(status, KErrNone);
       
   618     SetActive();
       
   619     }
       
   620 
       
   621 void CPbk2NlxAddToTopContactsCmd::StartNext()
       
   622     {
       
   623     __ASSERT_DEBUG( !IsActive(), Panic( ENlxAtcWronglyActivated ));    
       
   624     TRequestStatus* status = &iStatus;
       
   625     User::RequestComplete(status, KErrNone);
       
   626     SetActive();
       
   627     }
       
   628 
       
   629 void CPbk2NlxAddToTopContactsCmd::ShowWaitNoteL()
       
   630     {
       
   631     __ASSERT_DEBUG( !iDecorator, Panic( ENlxNoteActive ));
       
   632     iDecorator = Pbk2ProcessDecoratorFactory::CreateWaitNoteDecoratorL
       
   633         ( R_QTN_GEN_NOTE_SAVING_WAIT, ETrue );
       
   634     iDecorator->SetObserver( *this );
       
   635     iDecorator->ProcessStartedL( 0 ); // wait note doesn't care about amount
       
   636     }
       
   637 
       
   638 void CPbk2NlxAddToTopContactsCmd::ProcessDismissed( TInt /*aCancelCode*/ )
       
   639     {
       
   640     StartNext( EFinish );
       
   641     }
       
   642 
       
   643 ////////////////////////////// CALLBACKS /////////////////////////////////////
       
   644 
       
   645 // ---------------------------------------------------------------------------
       
   646 // CPbk2NlxAddToTopContactsCmd::ContactRelocatedL
       
   647 // ---------------------------------------------------------------------------
       
   648 //
       
   649 void CPbk2NlxAddToTopContactsCmd::ContactRelocatedL( 
       
   650 	MVPbkStoreContact* aRelocatedContact )
       
   651 	{
       
   652     __ASSERT_DEBUG( iNextPhase == ESetAsTopContact, 
       
   653     	Panic( ENlxAtcSingleContactRelocation ));    
       
   654 
       
   655     if ( !iRelocatedContacts )
       
   656         {
       
   657         iRelocatedContacts = CVPbkContactLinkArray::NewL();
       
   658         }
       
   659     
       
   660     MVPbkContactLink* link = aRelocatedContact->CreateLinkLC();
       
   661     iRelocatedContacts->AppendL( link );
       
   662     CleanupStack::Pop(); //link
       
   663     
       
   664     // top contact manager needs links, delete store contacts
       
   665     delete aRelocatedContact; 
       
   666 	}
       
   667 
       
   668 // ---------------------------------------------------------------------------
       
   669 // CPbk2NlxAddToTopContactsCmd::ContactRelocationFailed
       
   670 // ---------------------------------------------------------------------------
       
   671 //
       
   672 void CPbk2NlxAddToTopContactsCmd::ContactRelocationFailed(
       
   673     TInt /* aReason */,
       
   674     MVPbkStoreContact* /* aContact */ )
       
   675 	{
       
   676     __ASSERT_DEBUG( EFalse, Panic(ENlxUnexpectedFailurePath) );
       
   677 	}
       
   678 
       
   679 // ---------------------------------------------------------------------------
       
   680 // CPbk2NlxAddToTopContactsCmd::ContactsRelocationFailed
       
   681 // ---------------------------------------------------------------------------
       
   682 //
       
   683 void CPbk2NlxAddToTopContactsCmd::ContactsRelocationFailed(
       
   684         TInt aReason,
       
   685         CVPbkContactLinkArray* aContacts )
       
   686 	{
       
   687 	delete aContacts;
       
   688 	CCoeEnv::Static()->HandleError( aReason );
       
   689 	}
       
   690 
       
   691 
       
   692 // ---------------------------------------------------------------------------
       
   693 // CPbk2NlxAddToTopContactsCmd::RelocationProcessComplete
       
   694 // ---------------------------------------------------------------------------
       
   695 //
       
   696 void CPbk2NlxAddToTopContactsCmd::RelocationProcessComplete()
       
   697 	{
       
   698     __ASSERT_DEBUG( iNextPhase == ESetAsTopContact,
       
   699         Panic( ENlxAtcSingleContactRelocation2 ));
       
   700     
       
   701     if ( iRelocatedContacts && iRelocatedContacts->Count() > 0 )
       
   702         {
       
   703         // All is OK.
       
   704         StartNext();
       
   705         }
       
   706     else
       
   707         {
       
   708         // Some error has occured. The error has already been handled
       
   709         // in ContactRelocationFailed().
       
   710         Finish( KErrNone );
       
   711         }
       
   712 	}
       
   713 
       
   714 // ---------------------------------------------------------------------------    
       
   715 // CPbk2NlxAddToTopContactsCmd::TopOperationCompleted
       
   716 // ---------------------------------------------------------------------------
       
   717 //
       
   718 void CPbk2NlxAddToTopContactsCmd::VPbkOperationCompleted(
       
   719     MVPbkContactOperationBase*)
       
   720     {
       
   721     // This is a callback for CVPbkTopContactManager::AddToTopL()
       
   722     __ASSERT_DEBUG( iNextPhase == ESetAsTopContact, 
       
   723         	Panic( ENlxAtcSingleTopContactOperation ));
       
   724     Finish( KErrNone );
       
   725     }
       
   726 
       
   727 // ---------------------------------------------------------------------------
       
   728 // CPbk2NlxAddToTopContactsCmd::TopErrorOccured
       
   729 // ---------------------------------------------------------------------------
       
   730 //
       
   731 void CPbk2NlxAddToTopContactsCmd::VPbkOperationFailed(
       
   732     MVPbkContactOperationBase*,TInt aError )
       
   733     {
       
   734     __ASSERT_DEBUG( iNextPhase == ESetAsTopContact, 
       
   735         Panic( ENlxAtcSingleTopContactOperation ));
       
   736     Finish( aError );
       
   737     }
       
   738 
       
   739 // --------------------------------------------------------------------------
       
   740 // CPbk2NlxAddToTopContactsCmd::ContactViewReady
       
   741 // --------------------------------------------------------------------------
       
   742 //
       
   743 void CPbk2NlxAddToTopContactsCmd::ContactViewReady
       
   744         ( MVPbkContactViewBase& /*aView*/ )
       
   745     {
       
   746     // Do nothing
       
   747     }
       
   748 
       
   749 // --------------------------------------------------------------------------
       
   750 // CPbk2NlxAddToTopContactsCmd::ContactViewUnavailable
       
   751 // --------------------------------------------------------------------------
       
   752 //
       
   753 void CPbk2NlxAddToTopContactsCmd::ContactViewUnavailable
       
   754         ( MVPbkContactViewBase& /*aView*/ )
       
   755     {
       
   756     // Do nothing
       
   757     }
       
   758 
       
   759 // --------------------------------------------------------------------------
       
   760 // CPbk2NlxAddToTopContactsCmd::ContactAddedToView
       
   761 // --------------------------------------------------------------------------
       
   762 //
       
   763 void CPbk2NlxAddToTopContactsCmd::ContactAddedToView
       
   764         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   765           const MVPbkContactLink& /*aContactLink*/ )
       
   766     {
       
   767     // Do nothing
       
   768     }
       
   769 
       
   770 // --------------------------------------------------------------------------
       
   771 // CPbk2NlxAddToTopContactsCmd::ContactRemovedFromView
       
   772 // --------------------------------------------------------------------------
       
   773 //
       
   774 void CPbk2NlxAddToTopContactsCmd::ContactRemovedFromView
       
   775         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   776           const MVPbkContactLink& /*aContactLink*/ )
       
   777     {
       
   778     // Do nothing
       
   779     }
       
   780 
       
   781 // --------------------------------------------------------------------------
       
   782 // CPbk2NlxAddToTopContactsCmd::ContactViewError
       
   783 // --------------------------------------------------------------------------
       
   784 //
       
   785 void CPbk2NlxAddToTopContactsCmd::ContactViewError
       
   786         ( MVPbkContactViewBase& /*aView*/, TInt /*aError*/,
       
   787           TBool /*aErrorNotified*/ )
       
   788     {
       
   789     // Do nothing
       
   790     }
       
   791 
       
   792 //  End of File