phonebookui/Phonebook2/GroupExtension/src/CPguAddMembersCmd.cpp
changeset 0 e686773b3f54
child 32 2828b4d142c0
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 group command object.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "CPguAddMembersCmd.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include <Pbk2GroupUIRes.rsg>
       
    23 #include <Pbk2Commands.rsg>
       
    24 #include <Pbk2UIControls.rsg>
       
    25 #include <CPbk2FetchDlg.h>
       
    26 #include <MPbk2CommandObserver.h>
       
    27 #include <MPbk2ContactUiControl.h>
       
    28 #include <CPbk2ContactRelocator.h>
       
    29 #include <Pbk2ProcessDecoratorFactory.h>
       
    30 #include <CPbk2ViewState.h>
       
    31 #include <MPbk2AppUi.h>
       
    32 #include <MPbk2ApplicationServices.h>
       
    33 #include <MPbk2ContactViewSupplier.h>
       
    34 
       
    35 // Virtual Phonebook
       
    36 #include <CVPbkFilteredContactView.h>
       
    37 #include <CVPbkContactManager.h>
       
    38 #include <MVPbkContactLink.h>
       
    39 #include <CVPbkContactLinkArray.h>
       
    40 #include <MVPbkContactGroup.h>
       
    41 #include <MVPbkContactOperationBase.h>
       
    42 #include <MVPbkContactStore.h>
       
    43 #include <MVPbkContactStoreProperties.h>
       
    44 #include <VPbkContactStoreUris.h>
       
    45 
       
    46 // System includes
       
    47 #include <coemain.h>
       
    48 #include <eikenv.h>
       
    49 
       
    50 // Debugging headers
       
    51 #include <Pbk2Debug.h>
       
    52 
       
    53 /// Unnamed namespace for local definitions
       
    54 namespace {
       
    55 
       
    56 const TInt KOneContact = 1;
       
    57 const TInt KFirstElement = 0;
       
    58 // This is used in progress bar tweaking
       
    59 const TInt KCertainContactAmount = 3;
       
    60 
       
    61 #ifdef _DEBUG
       
    62 
       
    63 enum TPanicCode
       
    64     {
       
    65     EPanicPreCond_NewL = 1,
       
    66     EPanicPreCond_ExecuteLD,
       
    67     EPanicPreCond_RelocateContactsL,
       
    68     EPanicContactNotGroup,
       
    69     EInvalidView
       
    70     };
       
    71 
       
    72 void Panic(TPanicCode aReason)
       
    73     {
       
    74     _LIT(KPanicText, "CPguAddMembersCmd");
       
    75     User::Panic(KPanicText,aReason);
       
    76     }
       
    77 
       
    78 #endif // _DEBUG
       
    79 
       
    80 } /// namespace
       
    81 
       
    82 // --------------------------------------------------------------------------
       
    83 // CPguAddMembersCmd::CPguAddMembersCmd
       
    84 // --------------------------------------------------------------------------
       
    85 //
       
    86 CPguAddMembersCmd::CPguAddMembersCmd
       
    87         ( MVPbkContactLink& aContactGroup,
       
    88           MPbk2ContactUiControl& aUiControl ) :
       
    89             CActive( CActive::EPriorityStandard ),
       
    90             iUiControl( &aUiControl ),
       
    91             iGroupLink( aContactGroup )
       
    92     {
       
    93     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
    94             ("CPguAddMembersCmd::CPguAddMembersCmd(0x%x)"), this);
       
    95 
       
    96     CActiveScheduler::Add( this );
       
    97 
       
    98     if( iUiControl )
       
    99         {
       
   100         iUiControl->RegisterCommand( this );
       
   101         }
       
   102     }
       
   103 
       
   104 // --------------------------------------------------------------------------
       
   105 // CPguAddMembersCmd::~CPguAddMembersCmd
       
   106 // --------------------------------------------------------------------------
       
   107 //
       
   108 CPguAddMembersCmd::~CPguAddMembersCmd()
       
   109     {
       
   110     Cancel();
       
   111     if ( iFilteredView )
       
   112         {
       
   113         iFilteredView->RemoveObserver( *this );
       
   114         delete iFilteredView;
       
   115         }
       
   116     if( iUiControl )
       
   117         {
       
   118         iUiControl->RegisterCommand( NULL );
       
   119         }
       
   120 
       
   121     delete iRetrieveContactOperation;
       
   122     delete iRetrieveGroupOperation;
       
   123     delete iContactGroup;
       
   124     delete iEntriesToAdd;
       
   125     delete iEntriesToRelocate;
       
   126     delete iContactRelocator;
       
   127     delete iDecorator;
       
   128     delete iContactsInGroup;
       
   129     }
       
   130 
       
   131 // --------------------------------------------------------------------------
       
   132 // CPguAddMembersCmd::ConstructL
       
   133 // --------------------------------------------------------------------------
       
   134 //
       
   135 inline void CPguAddMembersCmd::ConstructL()
       
   136     {
       
   137     iState = ERetrievingContact;
       
   138     }
       
   139 // --------------------------------------------------------------------------
       
   140 // CPguAddMembersCmd::NewLC
       
   141 // --------------------------------------------------------------------------
       
   142 //
       
   143 CPguAddMembersCmd* CPguAddMembersCmd::NewLC
       
   144         ( MVPbkContactLink& aContactGroup,
       
   145           MPbk2ContactUiControl& aUiControl )
       
   146     {
       
   147     CPguAddMembersCmd* self = new ( ELeave ) CPguAddMembersCmd
       
   148         ( aContactGroup, aUiControl );
       
   149     CleanupStack::PushL( self );
       
   150     self->ConstructL();
       
   151     return self;
       
   152     }
       
   153 
       
   154 // --------------------------------------------------------------------------
       
   155 // CPguAddMembersCmd::ExecuteLD
       
   156 // --------------------------------------------------------------------------
       
   157 //
       
   158 void CPguAddMembersCmd::ExecuteLD()
       
   159     {
       
   160     __ASSERT_DEBUG( iCommandObserver, Panic( EPanicPreCond_ExecuteLD ) );
       
   161 
       
   162     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   163         ( "CPguAddMembersCmd::ExecuteLD(0x%x)"), this );
       
   164 
       
   165     IssueRequest();
       
   166     }
       
   167 
       
   168 // --------------------------------------------------------------------------
       
   169 // CPguAddMembersCmd::AddObserver
       
   170 // --------------------------------------------------------------------------
       
   171 //
       
   172 void CPguAddMembersCmd::AddObserver( MPbk2CommandObserver& aObserver )
       
   173     {
       
   174     iCommandObserver = &aObserver;
       
   175     }
       
   176 
       
   177 // --------------------------------------------------------------------------
       
   178 // CPguAddMembersCmd::ResetUiControl
       
   179 // --------------------------------------------------------------------------
       
   180 //
       
   181 void CPguAddMembersCmd::ResetUiControl
       
   182         ( MPbk2ContactUiControl& aUiControl )
       
   183     {
       
   184     if ( iUiControl == &aUiControl )
       
   185         {
       
   186         iUiControl = NULL;
       
   187         }
       
   188     }
       
   189 
       
   190 // --------------------------------------------------------------------------
       
   191 // CPguAddMembersCmd::RunL
       
   192 // --------------------------------------------------------------------------
       
   193 //
       
   194 void CPguAddMembersCmd::RunL()
       
   195     {
       
   196     switch( iState )
       
   197         {
       
   198         case ERetrievingContact:
       
   199             {
       
   200             iRetrieveGroupOperation = Phonebook2::Pbk2AppUi()->
       
   201                 ApplicationServices().ContactManager().
       
   202                     RetrieveContactL( iGroupLink, *this );
       
   203             break;
       
   204             }
       
   205         case EHandleContactLockedEvent:
       
   206             {
       
   207             HandleContactLockedEventL();
       
   208             break;
       
   209             }
       
   210         case EShowingProgressNote:
       
   211             {
       
   212             ShowProgressNoteL();
       
   213             break;
       
   214             }
       
   215         case EAddingContactsToGroup:
       
   216             {
       
   217             AddContactsToGroupL();
       
   218             break;
       
   219             }
       
   220         case ECommitingTransaction:
       
   221             {
       
   222             CommitTransactionL();
       
   223             break;
       
   224             }
       
   225         case EStopping:
       
   226             {
       
   227             ProcessDismissed( KErrNone );
       
   228             break;
       
   229             }
       
   230 
       
   231         default:
       
   232             {
       
   233             // Do nothing
       
   234             break;
       
   235             }
       
   236         }
       
   237     }
       
   238 
       
   239 // --------------------------------------------------------------------------
       
   240 // CPguAddMembersCmd::DoCancel
       
   241 // --------------------------------------------------------------------------
       
   242 //
       
   243 void CPguAddMembersCmd::DoCancel()
       
   244     {
       
   245     }
       
   246 
       
   247 // --------------------------------------------------------------------------
       
   248 // CPguAddMembersCmd::RunError
       
   249 // --------------------------------------------------------------------------
       
   250 //
       
   251 TInt CPguAddMembersCmd::RunError( TInt aError )
       
   252     {
       
   253     if ( aError != KErrNone )
       
   254         {
       
   255         FinishCommand( aError );
       
   256         }
       
   257 
       
   258     return KErrNone;
       
   259     }
       
   260 
       
   261 // --------------------------------------------------------------------------
       
   262 // CPguAddMembersCmd::VPbkSingleContactOperationComplete
       
   263 // --------------------------------------------------------------------------
       
   264 //
       
   265 void CPguAddMembersCmd::VPbkSingleContactOperationComplete
       
   266         ( MVPbkContactOperationBase& aOperation,
       
   267           MVPbkStoreContact* aContact )
       
   268     {
       
   269     TInt error = KErrNone;
       
   270 
       
   271     if ( &aOperation == iRetrieveGroupOperation )
       
   272         {
       
   273         delete iRetrieveGroupOperation;
       
   274         iRetrieveGroupOperation = NULL;
       
   275 
       
   276         iContactGroup = aContact->Group();
       
   277 
       
   278         __ASSERT_DEBUG( iContactGroup, Panic( EPanicContactNotGroup ) );
       
   279 
       
   280         TRAP( error, iContactGroup->LockL( *this ) );
       
   281         }
       
   282     else if ( &aOperation == iRetrieveContactOperation )
       
   283         {
       
   284         TRAP( error, RelocateContactL( aContact ) );
       
   285         }
       
   286 
       
   287     if ( error != KErrNone )
       
   288         {
       
   289         FinishCommand( error );
       
   290         }
       
   291     }
       
   292 
       
   293 // --------------------------------------------------------------------------
       
   294 // CPguAddMembersCmd::VPbkSingleContactOperationFailed
       
   295 // --------------------------------------------------------------------------
       
   296 //
       
   297 void CPguAddMembersCmd::VPbkSingleContactOperationFailed
       
   298         ( MVPbkContactOperationBase& /*aOperation*/, TInt aError )
       
   299     {
       
   300     FinishCommand( aError );
       
   301     }
       
   302 
       
   303 // --------------------------------------------------------------------------
       
   304 // CPguAddMembersCmd::ContactOperationCompleted
       
   305 // --------------------------------------------------------------------------
       
   306 //
       
   307 void CPguAddMembersCmd::ContactOperationCompleted( TContactOpResult aResult )
       
   308     {
       
   309     if ( aResult.iOpCode == MVPbkContactObserver::EContactLock )
       
   310         {
       
   311         iState = EHandleContactLockedEvent;
       
   312         IssueRequest();
       
   313         }
       
   314     else if ( aResult.iOpCode == MVPbkContactObserver::EContactCommit )
       
   315         {
       
   316         FinishCommand( KErrNone );
       
   317         }
       
   318     }
       
   319 
       
   320 // --------------------------------------------------------------------------
       
   321 // CPguAddMembersCmd::ContactOperationFailed
       
   322 // --------------------------------------------------------------------------
       
   323 //
       
   324 void CPguAddMembersCmd::ContactOperationFailed
       
   325         ( TContactOp /*aOpCode*/, TInt aErrorCode, TBool aErrorNotified )
       
   326     {
       
   327     if ( !aErrorNotified )
       
   328         {
       
   329         FinishCommand( aErrorCode );
       
   330         }
       
   331     else
       
   332         {
       
   333         FinishCommand( KErrNone );
       
   334         }
       
   335     }
       
   336 
       
   337 // --------------------------------------------------------------------------
       
   338 // CPguAddMembersCmd::ContactViewReady
       
   339 // --------------------------------------------------------------------------
       
   340 //
       
   341 void CPguAddMembersCmd::ContactViewReady
       
   342         ( MVPbkContactViewBase& /*aView*/ )
       
   343     {
       
   344     // Do nothing
       
   345     }
       
   346 
       
   347 // --------------------------------------------------------------------------
       
   348 // CPguAddMembersCmd::ContactViewUnavailable
       
   349 // --------------------------------------------------------------------------
       
   350 //
       
   351 void CPguAddMembersCmd::ContactViewUnavailable
       
   352         ( MVPbkContactViewBase& /*aView*/ )
       
   353     {
       
   354     // Do nothing
       
   355     }
       
   356 
       
   357 // --------------------------------------------------------------------------
       
   358 // CPguAddMembersCmd::ContactAddedToView
       
   359 // --------------------------------------------------------------------------
       
   360 //
       
   361 void CPguAddMembersCmd::ContactAddedToView
       
   362         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   363           const MVPbkContactLink& /*aContactLink*/ )
       
   364     {
       
   365     // Do nothing
       
   366     }
       
   367 
       
   368 // --------------------------------------------------------------------------
       
   369 // CPguAddMembersCmd::ContactRemovedFromView
       
   370 // --------------------------------------------------------------------------
       
   371 //
       
   372 void CPguAddMembersCmd::ContactRemovedFromView
       
   373         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   374           const MVPbkContactLink& /*aContactLink*/ )
       
   375     {
       
   376     // Do nothing
       
   377     }
       
   378 
       
   379 // --------------------------------------------------------------------------
       
   380 // CPguAddMembersCmd::ContactViewError
       
   381 // --------------------------------------------------------------------------
       
   382 //
       
   383 void CPguAddMembersCmd::ContactViewError
       
   384         ( MVPbkContactViewBase& PBK2_DEBUG_ONLY( aView ),
       
   385         TInt aError, TBool /*aErrorNotified*/ )
       
   386     {
       
   387     __ASSERT_DEBUG( iFilteredView == &aView, Panic( EInvalidView ) );
       
   388 
       
   389     FinishCommand( aError );
       
   390     }
       
   391 
       
   392 // --------------------------------------------------------------------------
       
   393 // CPguAddMembersCmd::IsContactIncluded
       
   394 //
       
   395 // This function should be as efficient as possible because it's called
       
   396 // as many times as there are contacts in all contacts view.
       
   397 // --------------------------------------------------------------------------
       
   398 //
       
   399 TBool CPguAddMembersCmd::IsContactIncluded
       
   400         ( const MVPbkBaseContact& aContact )
       
   401     {
       
   402     TBool result = ETrue;
       
   403     TRAPD ( err,
       
   404         {
       
   405         // Contacts that already belongs to the group are NOT included
       
   406         // to the fetch dialog.
       
   407         if ( !iContactsInGroup )
       
   408             {
       
   409             iContactsInGroup = iContactGroup->ItemsContainedLC();
       
   410             CleanupStack::Pop(); // iContactsInGroup
       
   411             }
       
   412 
       
   413         const TInt count( iContactsInGroup->Count() );
       
   414         for ( TInt i(0); i < count; ++i)
       
   415             {
       
   416             if ( iContactsInGroup->At( i ).RefersTo( aContact ) )
       
   417                 {
       
   418                 result = EFalse;
       
   419                 break;
       
   420                 }
       
   421             }
       
   422         });
       
   423 
       
   424     if ( err != KErrNone )
       
   425         {
       
   426         CCoeEnv::Static()->HandleError( err );
       
   427         }
       
   428 
       
   429     return result;
       
   430     }
       
   431 
       
   432 // --------------------------------------------------------------------------
       
   433 // CPguAddMembersCmd::AcceptFetchSelectionL
       
   434 // --------------------------------------------------------------------------
       
   435 //
       
   436 MPbk2FetchDlgObserver::TPbk2FetchAcceptSelection
       
   437     CPguAddMembersCmd::AcceptFetchSelectionL
       
   438         ( TInt /*aNumMarkedEntries*/,
       
   439           MVPbkContactLink& /*aLastSelection*/ )
       
   440     {
       
   441     return MPbk2FetchDlgObserver::EFetchYes;
       
   442     }
       
   443 
       
   444 // --------------------------------------------------------------------------
       
   445 // CPguAddMembersCmd::FetchCompletedL
       
   446 // --------------------------------------------------------------------------
       
   447 //
       
   448 void CPguAddMembersCmd::FetchCompletedL
       
   449         ( MVPbkContactLinkArray* aMarkedEntries )
       
   450     {
       
   451     if ( aMarkedEntries )
       
   452         {
       
   453         PrepareFetchResultsL( aMarkedEntries );
       
   454         }
       
   455     else
       
   456         {
       
   457         FinishCommand( KErrNone );
       
   458         }
       
   459     }
       
   460 
       
   461 // --------------------------------------------------------------------------
       
   462 // CPguAddMembersCmd::FetchCanceled
       
   463 // --------------------------------------------------------------------------
       
   464 //
       
   465 void CPguAddMembersCmd::FetchCanceled()
       
   466     {
       
   467     FinishCommand( KErrNone );
       
   468     }
       
   469 
       
   470 // --------------------------------------------------------------------------
       
   471 // CPguAddMembersCmd::FetchAborted
       
   472 // --------------------------------------------------------------------------
       
   473 //
       
   474 void CPguAddMembersCmd::FetchAborted()
       
   475     {
       
   476     FinishCommand( KErrNone );
       
   477     }
       
   478 
       
   479 // --------------------------------------------------------------------------
       
   480 // CPguAddMembersCmd::FetchOkToExit
       
   481 // --------------------------------------------------------------------------
       
   482 //
       
   483 TBool CPguAddMembersCmd::FetchOkToExit()
       
   484     {
       
   485     return ETrue;
       
   486     }
       
   487 
       
   488 // --------------------------------------------------------------------------
       
   489 // CPguAddMembersCmd::ContactRelocatedL
       
   490 // --------------------------------------------------------------------------
       
   491 //
       
   492 void CPguAddMembersCmd::ContactRelocatedL
       
   493         ( MVPbkStoreContact* aRelocatedContact )
       
   494     {
       
   495     MVPbkContactLink* contactLink = aRelocatedContact->CreateLinkLC();
       
   496     if ( contactLink )
       
   497         {
       
   498         if ( iEntriesToAdd )
       
   499             {
       
   500             CleanupStack::Pop(); // contactLink
       
   501             iEntriesToAdd->AppendL( contactLink );
       
   502             }
       
   503         else
       
   504             {
       
   505             CleanupStack::PopAndDestroy(); // contactLink
       
   506             }
       
   507         }
       
   508 
       
   509     delete aRelocatedContact;
       
   510 
       
   511     // Advance the relocation progress note
       
   512     if ( iDecorator )
       
   513         {
       
   514         iDecorator->ProcessAdvance( 1 );
       
   515         }
       
   516     }
       
   517 
       
   518 // --------------------------------------------------------------------------
       
   519 // CPguAddMembersCmd::ContactRelocationFailed
       
   520 // --------------------------------------------------------------------------
       
   521 //
       
   522 void CPguAddMembersCmd::ContactRelocationFailed
       
   523         ( TInt aReason, MVPbkStoreContact* aContact )
       
   524     {
       
   525     // Can't do much than just delete the contact we were given ownership,
       
   526     // it can not be added to the group
       
   527     delete aContact;
       
   528 
       
   529     if ( aReason == KErrCancel
       
   530         && iEntriesToAdd
       
   531         && iEntriesToAdd->Count() > 0 )
       
   532         {
       
   533         // Although user discards relocation we have to add other entries
       
   534         // to the group if them exists. First show the progress note.
       
   535         iState = EShowingProgressNote;
       
   536         IssueRequest();
       
   537         }
       
   538     else
       
   539         {
       
   540         FinishCommand( aReason );
       
   541         }
       
   542     }
       
   543 
       
   544 // --------------------------------------------------------------------------
       
   545 // CPguAddMembersCmd::ContactsRelocationFailed
       
   546 // --------------------------------------------------------------------------
       
   547 //
       
   548 void CPguAddMembersCmd::ContactsRelocationFailed
       
   549         ( TInt aReason, CVPbkContactLinkArray* aContacts )
       
   550     {
       
   551     // Can't do much than just delete the contacts we were given ownership,
       
   552     // they can not be added to the group
       
   553     delete aContacts;
       
   554 
       
   555     if ( aReason == KErrCancel
       
   556         && iEntriesToAdd
       
   557         && iEntriesToAdd->Count() > 0 )
       
   558         {
       
   559         // Although user discards relocation we have to add other entries
       
   560         // to the group if them exists. First show the progress note.
       
   561         iState = EShowingProgressNote;
       
   562         IssueRequest();
       
   563         }
       
   564     else
       
   565         {
       
   566         FinishCommand( aReason );
       
   567         }
       
   568     }
       
   569 
       
   570 // --------------------------------------------------------------------------
       
   571 // CPguAddMembersCmd::RelocationProcessComplete
       
   572 // --------------------------------------------------------------------------
       
   573 //
       
   574 void CPguAddMembersCmd::RelocationProcessComplete()
       
   575     {
       
   576     iState = EShowingProgressNote;
       
   577     IssueRequest();
       
   578     }
       
   579 
       
   580 // --------------------------------------------------------------------------
       
   581 // CPguAddMembersCmd::ProcessDismissed
       
   582 // --------------------------------------------------------------------------
       
   583 //
       
   584 void CPguAddMembersCmd::ProcessDismissed( TInt /*aCancelCode*/ )
       
   585     {
       
   586     if ( iUiControl )
       
   587         {
       
   588         iUiControl->UpdateAfterCommandExecution();
       
   589         }
       
   590     if ( iCommandObserver )
       
   591         {
       
   592         iCommandObserver->CommandFinished( *this );
       
   593         }
       
   594     }
       
   595 
       
   596 // --------------------------------------------------------------------------
       
   597 // CPguAddMembersCmd::HandleContactLockedEventL
       
   598 // --------------------------------------------------------------------------
       
   599 //
       
   600 void CPguAddMembersCmd::HandleContactLockedEventL()
       
   601     {
       
   602     delete iFilteredView;
       
   603     iFilteredView = NULL;
       
   604     iFilteredView = CVPbkFilteredContactView::NewL
       
   605         ( *Phonebook2::Pbk2AppUi()->ApplicationServices().ViewSupplier().
       
   606           AllContactsViewL(), *this, *this,
       
   607           Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   608             FieldTypes() );
       
   609 
       
   610     DoLaunchFetchDialogL();
       
   611     }
       
   612 
       
   613 // --------------------------------------------------------------------------
       
   614 // CPguAddMembersCmd::DoLaunchFetchDialogL
       
   615 // --------------------------------------------------------------------------
       
   616 //
       
   617 void CPguAddMembersCmd::DoLaunchFetchDialogL()
       
   618     {
       
   619     CPbk2FetchDlg::TParams params;
       
   620     params.iResId = R_PBK2_MULTIPLE_ENTRY_FETCH_NO_GROUPS_DLG;
       
   621     // Fetch dlg uses this view instead of AllNameslistView
       
   622     params.iNamesListView = iFilteredView;
       
   623     params.iFlags = CPbk2FetchDlg::EFetchMultiple;
       
   624     params.iNaviPaneId = R_PBK2_GROUP_MEMBER_FETCH_NAVILABEL;
       
   625 
       
   626     CPbk2FetchDlg* dlg = CPbk2FetchDlg::NewL( params, *this );
       
   627     // Fetch dialog is not waiting dialog, so we just launch it
       
   628     dlg->ExecuteLD();
       
   629     }
       
   630 
       
   631 // --------------------------------------------------------------------------
       
   632 // CPguAddMembersCmd::PrepareFetchResultsL
       
   633 // --------------------------------------------------------------------------
       
   634 //
       
   635 void CPguAddMembersCmd::PrepareFetchResultsL
       
   636         ( MVPbkContactLinkArray* aMarkedEntries )
       
   637     {
       
   638     if ( aMarkedEntries )
       
   639         {
       
   640         const TInt count = aMarkedEntries->Count();
       
   641 
       
   642         delete iEntriesToAdd;
       
   643         iEntriesToAdd = NULL;
       
   644         iEntriesToAdd = CVPbkContactLinkArray::NewL();
       
   645 
       
   646         delete iEntriesToRelocate;
       
   647         iEntriesToRelocate = NULL;
       
   648         iEntriesToRelocate = CVPbkContactLinkArray::NewL();
       
   649 
       
   650         for ( TInt i = 0; i < count; ++ i )
       
   651             {
       
   652             MVPbkContactLink* contactLink =
       
   653                 aMarkedEntries->At( i ).CloneLC();
       
   654             CleanupStack::Pop(); // contactLink
       
   655 
       
   656             TVPbkContactStoreUriPtr uri =
       
   657                     contactLink->ContactStore().StoreProperties().Uri();
       
   658             TVPbkContactStoreUriPtr phoneMemoryUri(
       
   659                     VPbkContactStoreUris::DefaultCntDbUri() );
       
   660 
       
   661             if ( uri.Compare( phoneMemoryUri,
       
   662                     TVPbkContactStoreUriPtr::EContactStoreUriStoreType )
       
   663                         == 0 )
       
   664                 {
       
   665                 // Phone memory contact can be added to group directly
       
   666                 iEntriesToAdd->AppendL( contactLink );
       
   667                 }
       
   668             else
       
   669                 {
       
   670                 // We must first relocate the contact
       
   671                 iEntriesToRelocate->AppendL( contactLink );
       
   672                 }
       
   673             }
       
   674 
       
   675         if ( iEntriesToRelocate->Count() > KOneContact )
       
   676             {
       
   677             RelocateContactsL();
       
   678             }
       
   679         else if ( iEntriesToRelocate->Count() == KOneContact )
       
   680             {
       
   681             // We'll have to retrieve the one single contact and
       
   682             // use single contact relocator
       
   683             delete iRetrieveContactOperation;
       
   684             iRetrieveContactOperation = NULL;
       
   685             iRetrieveContactOperation =
       
   686                 Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   687                     ContactManager().RetrieveContactL(
       
   688                         iEntriesToRelocate->At( KFirstElement ), *this );
       
   689             }
       
   690         else
       
   691             {
       
   692             // Contacts will be added to group. First show
       
   693             // the progress note.
       
   694             iState = EShowingProgressNote;
       
   695             IssueRequest();
       
   696             }
       
   697         }
       
   698     }
       
   699 
       
   700 // --------------------------------------------------------------------------
       
   701 // CPguAddMembersCmd::RelocateContactL
       
   702 // --------------------------------------------------------------------------
       
   703 //
       
   704 void CPguAddMembersCmd::RelocateContactL( MVPbkStoreContact* aStoreContact )
       
   705     {
       
   706     delete iContactRelocator;
       
   707     iContactRelocator = NULL;
       
   708     iContactRelocator = CPbk2ContactRelocator::NewL();
       
   709 
       
   710     // Asynchronously relocate the contact
       
   711     // One contact doesn't need waiting note
       
   712     iContactRelocator->RelocateContactL( aStoreContact, *this,
       
   713         Pbk2ContactRelocator::EPbk2DisplayBasicQuery,
       
   714         CPbk2ContactRelocator::EPbk2RelocatorExistingContact );
       
   715     }
       
   716 // --------------------------------------------------------------------------
       
   717 // CPguAddMembersCmd::RelocateContactsL
       
   718 // --------------------------------------------------------------------------
       
   719 //
       
   720 void CPguAddMembersCmd::RelocateContactsL()
       
   721     {
       
   722     __ASSERT_DEBUG( iEntriesToRelocate->Count() > KOneContact,
       
   723         Panic( EPanicPreCond_RelocateContactsL ) );
       
   724 
       
   725     TInt count = iEntriesToRelocate->Count();
       
   726     delete iContactRelocator;
       
   727     iContactRelocator = NULL;
       
   728     iContactRelocator = CPbk2ContactRelocator::NewL();
       
   729 
       
   730     // Asynchronously relocate contacts
       
   731     TBool result = iContactRelocator->RelocateContactsL( iEntriesToRelocate,
       
   732         *this, Pbk2ContactRelocator::EPbk2DisplayBasicQuery );
       
   733     iEntriesToRelocate = NULL;    // Ownership was taken
       
   734     if ( result )
       
   735         {
       
   736         // User has accepted relocation, show "Retrieving" progress note
       
   737         ShowRelocationNoteL( count );
       
   738         }
       
   739     }
       
   740 
       
   741 // --------------------------------------------------------------------------
       
   742 // CPguAddMembersCmd::AddContactsToGroupL
       
   743 // --------------------------------------------------------------------------
       
   744 //
       
   745 void CPguAddMembersCmd::AddContactsToGroupL()
       
   746     {
       
   747     TInt count = 0;
       
   748     if ( iEntriesToAdd )
       
   749         {
       
   750         count = iEntriesToAdd->Count();
       
   751         }
       
   752 
       
   753     for ( TInt i = 0; i < count; ++i )
       
   754         {
       
   755         TRAPD( err, iContactGroup->AddContactL( iEntriesToAdd->At( i ) ) );
       
   756         if ( iDecorator )
       
   757             {
       
   758             iDecorator->ProcessAdvance( 1 );
       
   759             }
       
   760         if ( err == KErrInUse )
       
   761             {
       
   762             if ( iEntriesToAdd->Count() == 1 )      		
       
   763                 {
       
   764                 User::Leave( err );
       
   765                 }
       
   766             }
       
   767         else
       
   768             {
       
   769             User::LeaveIfError( err );
       
   770             }
       
   771         }
       
   772     iState = ECommitingTransaction;
       
   773     IssueRequest();
       
   774     }
       
   775 
       
   776 // --------------------------------------------------------------------------
       
   777 // CPguAddMembersCmd::CommitTransactionL
       
   778 // --------------------------------------------------------------------------
       
   779 //
       
   780 void CPguAddMembersCmd::CommitTransactionL()
       
   781     {
       
   782     iContactGroup->CommitL( *this );
       
   783     }
       
   784 
       
   785 // --------------------------------------------------------------------------
       
   786 // CPguAddMembersCmd::FinishCommand
       
   787 // --------------------------------------------------------------------------
       
   788 //
       
   789 void CPguAddMembersCmd::FinishCommand( TInt aResult )
       
   790     {
       
   791     if ( iUiControl )
       
   792         {
       
   793         iUiControl->SetBlank( EFalse );
       
   794         TRAPD(err, SetFocusToLastAddedL() );
       
   795         if ( KErrNone != err )
       
   796             {
       
   797             CCoeEnv::Static()->HandleError( aResult );
       
   798             }
       
   799         }
       
   800 
       
   801     if ( aResult != KErrNone && aResult != KErrCancel )
       
   802         {
       
   803         CCoeEnv::Static()->HandleError( aResult );
       
   804         }
       
   805 
       
   806     if ( iDecorator )
       
   807         {
       
   808         // iDecorator calls ProcessStopped to end this cmd
       
   809         iDecorator->ProcessStopped();
       
   810         }
       
   811     else
       
   812         {
       
   813         if ( iUiControl )
       
   814             {
       
   815             iUiControl->UpdateAfterCommandExecution();
       
   816             }
       
   817         if ( iCommandObserver )
       
   818             {
       
   819             iCommandObserver->CommandFinished( *this );
       
   820             }
       
   821         }
       
   822     }
       
   823 
       
   824 // --------------------------------------------------------------------------
       
   825 // CPguAddMembersCmd::ShowRelocationNoteL
       
   826 // --------------------------------------------------------------------------
       
   827 //
       
   828 void CPguAddMembersCmd::ShowRelocationNoteL( TInt aCount )
       
   829     {
       
   830     if ( iUiControl )
       
   831         {
       
   832         iUiControl->SetBlank( ETrue );
       
   833         }
       
   834 
       
   835     if ( !iDecorator )
       
   836         {
       
   837         // Show relocation note only if there are enough contacts
       
   838         // You can't use here the TBool-delay switch,
       
   839         // it just doesn't progress
       
   840         if ( aCount > KCertainContactAmount )
       
   841             {
       
   842             iDecorator = Pbk2ProcessDecoratorFactory::
       
   843                 CreateProgressDialogDecoratorL
       
   844                 ( R_QTN_GEN_NOTE_RETRIEVING_PROGRESS, EFalse );
       
   845             iDecorator->SetObserver( *this );
       
   846             iDecorator->ProcessStartedL( aCount + 1 );
       
   847             }
       
   848         else
       
   849             {
       
   850             // There are few contacts, so omit the whole note
       
   851             iDecorator = Pbk2ProcessDecoratorFactory::
       
   852                 CreateProgressDialogDecoratorL
       
   853                 ( R_QTN_GEN_NOTE_RETRIEVING_PROGRESS, ETrue );
       
   854             iDecorator->SetObserver( *this );
       
   855             iDecorator->ProcessStartedL( 0 );
       
   856             }
       
   857         }
       
   858     }
       
   859 
       
   860 
       
   861 // --------------------------------------------------------------------------
       
   862 // CPguAddMembersCmd::ShowProgressNoteL
       
   863 // --------------------------------------------------------------------------
       
   864 //
       
   865 void CPguAddMembersCmd::ShowProgressNoteL()
       
   866     {
       
   867     if ( iUiControl )
       
   868         {
       
   869         iUiControl->SetBlank( ETrue );
       
   870         }
       
   871 
       
   872     if ( !iDecorator && iEntriesToAdd )
       
   873         {
       
   874         // Show relocation note only if there are enough contacts
       
   875         // You can't use here the TBool-delay switch, it just doesn't
       
   876         // progress
       
   877         if ( iEntriesToAdd->Count() > KCertainContactAmount )
       
   878             {
       
   879             iDecorator = Pbk2ProcessDecoratorFactory::
       
   880                 CreateProgressDialogDecoratorL
       
   881                 ( R_QTN_GEN_NOTE_RETRIEVING_PROGRESS, EFalse );
       
   882             iDecorator->SetObserver( *this );
       
   883             iDecorator->ProcessStartedL( iEntriesToAdd->Count() + 1 );
       
   884             }
       
   885         else
       
   886             {
       
   887             // There are few contacts, so omit the whole note
       
   888             iDecorator = Pbk2ProcessDecoratorFactory::
       
   889                 CreateProgressDialogDecoratorL
       
   890                 ( R_QTN_GEN_NOTE_RETRIEVING_PROGRESS, ETrue );
       
   891             iDecorator->SetObserver( *this );
       
   892             iDecorator->ProcessStartedL( 0 );
       
   893             }
       
   894         }
       
   895     iState = EAddingContactsToGroup;
       
   896     IssueRequest();
       
   897     }
       
   898 
       
   899 // --------------------------------------------------------------------------
       
   900 // CPguAddMembersCmd::IssueRequest
       
   901 // --------------------------------------------------------------------------
       
   902 //
       
   903 void CPguAddMembersCmd::IssueRequest()
       
   904     {
       
   905     TRequestStatus* status = &iStatus;
       
   906     User::RequestComplete( status, KErrNone );
       
   907     SetActive();
       
   908     }
       
   909 
       
   910 // --------------------------------------------------------------------------
       
   911 // CPguAddMembersCmd::IssueStopRequest
       
   912 // --------------------------------------------------------------------------
       
   913 //
       
   914 void CPguAddMembersCmd::IssueStopRequest()
       
   915     {
       
   916     iState = EStopping;
       
   917     if ( !IsActive() )
       
   918         {
       
   919         IssueRequest();
       
   920         }
       
   921     }
       
   922 
       
   923 // --------------------------------------------------------------------------
       
   924 // CPguAddMembersCmd::SetFocusToLastAddedL
       
   925 // --------------------------------------------------------------------------
       
   926 //
       
   927 void CPguAddMembersCmd::SetFocusToLastAddedL()
       
   928     {
       
   929     if ( iUiControl && iEntriesToAdd && iEntriesToAdd->Count() )
       
   930         {
       
   931         // Sets the focus to last added contactlink
       
   932         TInt focusedIndex( iEntriesToAdd->Count() - 1 );
       
   933         CPbk2ViewState* viewState = iUiControl->ControlStateL();
       
   934         CleanupStack::PushL( viewState );
       
   935 
       
   936         viewState->SetFocusedContact(
       
   937             iEntriesToAdd->At( focusedIndex ).CloneLC() );
       
   938         CleanupStack::Pop(); // iEntriesToAdd->At(focusedIndex).CloneLC
       
   939 
       
   940         iUiControl->RestoreControlStateL( viewState );
       
   941         CleanupStack::PopAndDestroy();  // viewState
       
   942         }
       
   943     }
       
   944 
       
   945 // End of File