phonebookui/Phonebook2/CommandsExtension/src/CPbk2CopyContactsCmd.cpp
changeset 0 e686773b3f54
child 3 04ab22b956c2
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 copy contacts command.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CPbk2CopyContactsCmd.h"
       
    21 
       
    22 // Phonebook 2
       
    23 #include "TPbk2CopyContactsResults.h"
       
    24 #include <MPbk2CommandObserver.h>
       
    25 #include <MPbk2ContactUiControl.h>
       
    26 #include <Pbk2ProcessDecoratorFactory.h>
       
    27 #include <Pbk2Commands.rsg>
       
    28 #include <MPbk2ContactNameFormatter.h>
       
    29 #include <CPbk2DriveSpaceCheck.h>
       
    30 #include <CPbk2AppUiBase.h>
       
    31 #include <CPbk2ApplicationServices.h>
       
    32 
       
    33 // Virtual Phonebook
       
    34 #include <CVPbkContactManager.h>
       
    35 #include <CVPbkContactLinkArray.h>
       
    36 #include <CVPbkContactCopier.h>
       
    37 #include <MVPbkContactOperationBase.h>
       
    38 #include <MVPbkContactStore.h>
       
    39 #include <MVPbkContactStoreProperties.h>
       
    40 #include <MVPbkContactLink.h>
       
    41 #include <MVPbkStoreContact.h>
       
    42 #include <MVPbkContactBookmark.h>
       
    43 
       
    44 // System includes
       
    45 #include <StringLoader.h>
       
    46 #include <aknnotewrappers.h>
       
    47 #include <coemain.h>
       
    48 
       
    49 /// Unnamed namespace for local definitions
       
    50 namespace {
       
    51 
       
    52 const TInt KOneSelected = 1;
       
    53 
       
    54 enum TPbk2CopyContactsCmdState
       
    55     {
       
    56     EPbk2CopyContactsCmdInit,
       
    57     EPbk2CopyContactsCmdRetrieve,
       
    58     EPbk2CopyContactsCmdCopy,
       
    59     EPbk2CopyContactsCmdShowResults,
       
    60     EPbk2CopyContactsCmdComplete
       
    61     };
       
    62 
       
    63 #ifdef _DEBUG
       
    64 enum TPanicCode
       
    65     {
       
    66     EPreCond_ConstructL,
       
    67     EInvalidStoreSelection
       
    68     };
       
    69 
       
    70 void Panic(TInt aReason)
       
    71     {
       
    72     _LIT(KPanicText, "CPbk2CopyContactsCmd");
       
    73     User::Panic(KPanicText, aReason);
       
    74     }
       
    75 #endif // _DEBUG
       
    76 
       
    77 } /// namespace
       
    78 
       
    79 // --------------------------------------------------------------------------
       
    80 // CPbk2CopyContactsCmd::CPbk2CopyContactsCmd
       
    81 // --------------------------------------------------------------------------
       
    82 //
       
    83 CPbk2CopyContactsCmd::CPbk2CopyContactsCmd(
       
    84         MPbk2ContactUiControl& aUiControl ) :
       
    85     CActive( EPriorityStandard ),
       
    86     iStoreUiControl( &aUiControl ),
       
    87     iState( EPbk2CopyContactsCmdInit )
       
    88     {
       
    89     }
       
    90 
       
    91 // --------------------------------------------------------------------------
       
    92 // CPbk2CopyContactsCmd::~CPbk2CopyContactsCmd
       
    93 // --------------------------------------------------------------------------
       
    94 //
       
    95 CPbk2CopyContactsCmd::~CPbk2CopyContactsCmd()
       
    96     {
       
    97     Cancel();
       
    98     
       
    99     if ( iNameListUiControl  )
       
   100         {
       
   101         iNameListUiControl->RegisterCommand( NULL );
       
   102         }    
       
   103 
       
   104     delete iFocusedContactBookmark;
       
   105     delete iCopiedContacts;
       
   106     delete iDecorator;
       
   107     delete iContactLinks;
       
   108     delete iCopyOperation;
       
   109     delete iCopier;
       
   110     delete iContact;
       
   111     delete iRetrieveOperation;
       
   112     delete iDriveSpaceCheck;
       
   113     Release( iAppServices );
       
   114     }
       
   115 
       
   116 // --------------------------------------------------------------------------
       
   117 // CPbk2CopyContactsCmd::NewL
       
   118 // --------------------------------------------------------------------------
       
   119 //
       
   120 CPbk2CopyContactsCmd* CPbk2CopyContactsCmd::NewL(
       
   121         MPbk2ContactUiControl& aUiControl)
       
   122     {
       
   123     CPbk2CopyContactsCmd* self =
       
   124         new( ELeave ) CPbk2CopyContactsCmd(aUiControl);
       
   125     CleanupStack::PushL( self );
       
   126     self->ConstructL();
       
   127     CleanupStack::Pop( self );
       
   128     return self;
       
   129     }
       
   130 
       
   131 // --------------------------------------------------------------------------
       
   132 // CPbk2CopyContactsCmd::ConstructL
       
   133 // --------------------------------------------------------------------------
       
   134 //
       
   135 void CPbk2CopyContactsCmd::ConstructL()
       
   136     {
       
   137     iAppServices = CPbk2ApplicationServices::InstanceL();
       
   138 
       
   139     // safe since this is a contructor reference parameter
       
   140     iNameListUiControl = iStoreUiControl->ParentControl();
       
   141     if ( !iNameListUiControl ) 
       
   142         {
       
   143         iNameListUiControl = iStoreUiControl;
       
   144         }
       
   145     __ASSERT_DEBUG(iNameListUiControl, Panic(EPreCond_ConstructL));
       
   146    
       
   147     iNameListUiControl->RegisterCommand( this );
       
   148     
       
   149     CActiveScheduler::Add(this);
       
   150 
       
   151     iDecorator = Pbk2ProcessDecoratorFactory::CreateProgressDialogDecoratorL(
       
   152             R_GENERAL_COPY_PROGRESS_NOTE, ETrue);
       
   153     iDecorator->SetObserver(*this);
       
   154     iContactLinks = CVPbkContactLinkArray::NewL();
       
   155     iDriveSpaceCheck = CPbk2DriveSpaceCheck::NewL
       
   156         ( CCoeEnv::Static()->FsSession() );
       
   157     }
       
   158 
       
   159 // --------------------------------------------------------------------------
       
   160 // CPbk2CopyContactsCmd::RunL
       
   161 // --------------------------------------------------------------------------
       
   162 //
       
   163 void CPbk2CopyContactsCmd::RunL()
       
   164     {
       
   165     switch (iState)
       
   166         {
       
   167         case EPbk2CopyContactsCmdInit:
       
   168             {
       
   169             InitL();
       
   170             break;
       
   171             }
       
   172         case EPbk2CopyContactsCmdRetrieve:
       
   173             {
       
   174             RetrieveL();
       
   175             break;
       
   176             }
       
   177         case EPbk2CopyContactsCmdCopy:
       
   178             {
       
   179             StartCopyL();
       
   180             break;
       
   181             }
       
   182         case EPbk2CopyContactsCmdShowResults:
       
   183             {
       
   184             ShowResultsL();
       
   185             break;
       
   186             }
       
   187         default:
       
   188             {
       
   189             FinishCommandL();
       
   190             break;
       
   191             }
       
   192         }
       
   193     }
       
   194 
       
   195 // --------------------------------------------------------------------------
       
   196 // CPbk2CopyContactsCmd::DoCancel
       
   197 // --------------------------------------------------------------------------
       
   198 //
       
   199 void CPbk2CopyContactsCmd::DoCancel()
       
   200     {
       
   201     delete iCopyOperation;
       
   202     iCopyOperation = NULL;
       
   203     }
       
   204 
       
   205 // --------------------------------------------------------------------------
       
   206 // CPbk2CopyContactsCmd::RunError
       
   207 // --------------------------------------------------------------------------
       
   208 //
       
   209 TInt CPbk2CopyContactsCmd::RunError(TInt aError)
       
   210     {
       
   211     HandleError(aError);
       
   212     return KErrNone;
       
   213     }
       
   214 
       
   215 // --------------------------------------------------------------------------
       
   216 // CPbk2CopyContactsCmd::ExecuteLD
       
   217 // --------------------------------------------------------------------------
       
   218 //
       
   219 void CPbk2CopyContactsCmd::ExecuteLD()
       
   220     {
       
   221     IssueRequest();
       
   222     }
       
   223 
       
   224 // --------------------------------------------------------------------------
       
   225 // CPbk2CopyContactsCmd::AddObserver
       
   226 // --------------------------------------------------------------------------
       
   227 //
       
   228 void CPbk2CopyContactsCmd::AddObserver(MPbk2CommandObserver& aObserver)
       
   229     {
       
   230     iCommandObserver = &aObserver;
       
   231     }
       
   232 
       
   233 // --------------------------------------------------------------------------
       
   234 // CPbk2CopyContactsCmd::ResetUiControl
       
   235 // --------------------------------------------------------------------------
       
   236 //
       
   237 void CPbk2CopyContactsCmd::ResetUiControl(
       
   238         MPbk2ContactUiControl& aUiControl)
       
   239     {
       
   240     if (iNameListUiControl == &aUiControl)
       
   241         {
       
   242         iStoreUiControl = NULL;
       
   243         iNameListUiControl = NULL;
       
   244         }
       
   245     }
       
   246 
       
   247 // --------------------------------------------------------------------------
       
   248 // CPbk2CopyContactsCmd::StepComplete
       
   249 // --------------------------------------------------------------------------
       
   250 //
       
   251 void CPbk2CopyContactsCmd::StepComplete(
       
   252         MVPbkContactOperationBase& /*aOperation*/,
       
   253         TInt aStepSize)
       
   254     {
       
   255     iCopiedSuccessfully += aStepSize;
       
   256     iDecorator->ProcessAdvance(aStepSize);
       
   257     }
       
   258 
       
   259 // --------------------------------------------------------------------------
       
   260 // CPbk2CopyContactsCmd::StepFailed
       
   261 // --------------------------------------------------------------------------
       
   262 //
       
   263 TBool CPbk2CopyContactsCmd::StepFailed(
       
   264         MVPbkContactOperationBase& /*aOperation*/,
       
   265         TInt /*aStepSize*/, TInt aError)
       
   266     {
       
   267     HandleError(aError);
       
   268     // Stop copy operation
       
   269     return EFalse;
       
   270     }
       
   271 
       
   272 // --------------------------------------------------------------------------
       
   273 // CPbk2CopyContactsCmd::OperationComplete
       
   274 // --------------------------------------------------------------------------
       
   275 //
       
   276 void CPbk2CopyContactsCmd::OperationComplete(
       
   277         MVPbkContactOperationBase& /*aOperation*/)
       
   278     {
       
   279     iDecorator->ProcessStopped();
       
   280     }
       
   281 
       
   282 // --------------------------------------------------------------------------
       
   283 // CPbk2CopyContactsCmd::ProcessDismissed
       
   284 // --------------------------------------------------------------------------
       
   285 //
       
   286 void CPbk2CopyContactsCmd::ProcessDismissed( TInt /*aCancelCode*/ )
       
   287     {
       
   288     Cancel();
       
   289     delete iCopyOperation;
       
   290     iCopyOperation = NULL;
       
   291     iState = EPbk2CopyContactsCmdShowResults;
       
   292     IssueRequest();
       
   293     }
       
   294 
       
   295 // --------------------------------------------------------------------------
       
   296 // CPbk2CopyContactsCmd::VPbkSingleContactOperationComplete
       
   297 // --------------------------------------------------------------------------
       
   298 //
       
   299 void CPbk2CopyContactsCmd::VPbkSingleContactOperationComplete(
       
   300         MVPbkContactOperationBase& /*aOperation*/,
       
   301         MVPbkStoreContact* aContact)
       
   302     {
       
   303     iContact = aContact;
       
   304     delete iRetrieveOperation;
       
   305     iRetrieveOperation = NULL;
       
   306     iState = EPbk2CopyContactsCmdCopy;
       
   307     IssueRequest();
       
   308     }
       
   309 
       
   310 // --------------------------------------------------------------------------
       
   311 // CPbk2CopyContactsCmd::VPbkSingleContactOperationFailed
       
   312 // --------------------------------------------------------------------------
       
   313 //
       
   314 void CPbk2CopyContactsCmd::VPbkSingleContactOperationFailed(
       
   315         MVPbkContactOperationBase& /*aOperation*/,
       
   316         TInt aError)
       
   317     {
       
   318     HandleError(aError);
       
   319     }
       
   320 
       
   321 // --------------------------------------------------------------------------
       
   322 // CPbk2CopyContactsCmd::IssueRequest
       
   323 // --------------------------------------------------------------------------
       
   324 //
       
   325 void CPbk2CopyContactsCmd::IssueRequest()
       
   326     {
       
   327     TRequestStatus* status = &iStatus;
       
   328     User::RequestComplete(status, KErrNone);
       
   329     SetActive();
       
   330     }
       
   331 
       
   332 // --------------------------------------------------------------------------
       
   333 // CPbk2CopyContactsCmd::InitL
       
   334 // --------------------------------------------------------------------------
       
   335 //
       
   336 void CPbk2CopyContactsCmd::InitL()
       
   337     {
       
   338     if (iStoreUiControl)
       
   339         {        
       
   340         // Get target store
       
   341         CArrayPtr<MVPbkContactStore>* selected =
       
   342                 iStoreUiControl->SelectedContactStoresL();
       
   343 
       
   344         __ASSERT_DEBUG(selected && selected->Count() == KOneSelected,
       
   345             Panic(EInvalidStoreSelection));
       
   346 
       
   347         iTargetStore = (*selected)[0];
       
   348         delete selected;
       
   349     
       
   350         iDriveSpaceCheck->SetStore( iTargetStore->StoreProperties().Uri() );
       
   351         iDriveSpaceCheck->DriveSpaceCheckL();
       
   352         // Get selected contacts
       
   353         MVPbkContactLinkArray* links = 
       
   354             iNameListUiControl->SelectedContactsOrFocusedContactL();
       
   355         if ( links )
       
   356             {
       
   357             CleanupDeletePushL(links);
       
   358             // Create links that are not from target store
       
   359             CreateLinkArrayL(*links);
       
   360             CleanupStack::PopAndDestroy(); // links
       
   361 
       
   362             const TInt contactCount = iContactLinks->Count();
       
   363             if (contactCount == 0)
       
   364                 {
       
   365                 // Nothing to copy
       
   366                 ShowSelectedContactsInfoNoteL();
       
   367                 iState = EPbk2CopyContactsCmdComplete;
       
   368                 }
       
   369             if (contactCount == KOneSelected)
       
   370                 {
       
   371                 iState = EPbk2CopyContactsCmdRetrieve;
       
   372                 }
       
   373             else if (contactCount > KOneSelected)
       
   374                 {
       
   375                 iState = EPbk2CopyContactsCmdCopy;
       
   376                 }
       
   377             }
       
   378         else
       
   379             {
       
   380             iState = EPbk2CopyContactsCmdComplete;
       
   381             }        
       
   382         }
       
   383     else
       
   384         {
       
   385         // UI control doenst exist anymore, end
       
   386         iState = EPbk2CopyContactsCmdComplete;
       
   387         }
       
   388     IssueRequest();
       
   389     }
       
   390 
       
   391 // --------------------------------------------------------------------------
       
   392 // CPbk2CopyContactsCmd::RetrieveL
       
   393 // --------------------------------------------------------------------------
       
   394 //
       
   395 void CPbk2CopyContactsCmd::RetrieveL()
       
   396     {
       
   397     iRetrieveOperation =
       
   398         iAppServices->
       
   399             ContactManager().RetrieveContactL((*iContactLinks)[0],
       
   400             *this );
       
   401     }
       
   402 
       
   403 // --------------------------------------------------------------------------
       
   404 // CPbk2CopyContactsCmd::StartCopyL
       
   405 // --------------------------------------------------------------------------
       
   406 //
       
   407 void CPbk2CopyContactsCmd::StartCopyL()
       
   408     {
       
   409     if ( !iCopier )
       
   410         {
       
   411         iCopier = CVPbkContactCopier::NewL( iAppServices->ContactManager() );
       
   412         }
       
   413 
       
   414     if ( !iCopiedContacts )
       
   415         {
       
   416         iCopiedContacts = CVPbkContactLinkArray::NewL();
       
   417         }
       
   418 
       
   419     // Start copying
       
   420 
       
   421     if (iNameListUiControl)
       
   422         {
       
   423         // Blank UI control to avoid flicker
       
   424         iNameListUiControl->SetBlank( ETrue );
       
   425         
       
   426         // If find is NOT active then save focus. Copying will cause
       
   427         // the UI control to reset find text and focus. Otherwise the focus
       
   428         // must be kept over the copying process.
       
   429         if ( iNameListUiControl->FindTextL().Length() == 0 &&
       
   430              !iFocusedContactBookmark )
       
   431             {
       
   432             const MVPbkBaseContact* cnt = 
       
   433                     iNameListUiControl->FocusedContactL();
       
   434             if ( cnt )
       
   435                 {
       
   436                 iFocusedContactBookmark = cnt->CreateBookmarkLC();
       
   437                 CleanupStack::Pop();
       
   438                 }
       
   439             }
       
   440         }
       
   441         
       
   442     // Copy command doesn't need resulted contact links
       
   443     iCopiedContacts->ResetAndDestroy();
       
   444     iCopyOperation = iCopier->CopyContactsL(
       
   445         CVPbkContactCopier::EVPbkUsePlatformSpecificDuplicatePolicy,
       
   446         *iContactLinks, iTargetStore, *iCopiedContacts, *this );
       
   447 
       
   448     iDecorator->ProcessStartedL(iContactLinks->Count());
       
   449     }
       
   450 
       
   451 // --------------------------------------------------------------------------
       
   452 // CPbk2CopyContactsCmd::ShowResultsL
       
   453 // --------------------------------------------------------------------------
       
   454 //
       
   455 void CPbk2CopyContactsCmd::ShowResultsL()
       
   456     {
       
   457     const TInt contactCount = iContactLinks->Count();
       
   458     if (iContact)
       
   459         {
       
   460         HBufC* title =
       
   461             iAppServices->NameFormatter().
       
   462                 GetContactTitleL(
       
   463                     iContact->Fields(),
       
   464                     MPbk2ContactNameFormatter::EPreserveLeadingSpaces);
       
   465         CleanupStack::PushL(title);
       
   466         TPbk2CopyContactsResults results(iCopiedSuccessfully, *title);
       
   467         results.ShowNoteL();
       
   468         CleanupStack::PopAndDestroy(title);
       
   469         }
       
   470     else
       
   471         {
       
   472         TPbk2CopyContactsResults results(
       
   473             iCopiedSuccessfully, iContactLinks->Count());
       
   474         results.ShowNoteL();
       
   475         }
       
   476     iState = EPbk2CopyContactsCmdComplete;
       
   477     IssueRequest();
       
   478     }
       
   479 
       
   480 // --------------------------------------------------------------------------
       
   481 // CPbk2CopyContactsCmd::FinishCommandL
       
   482 // --------------------------------------------------------------------------
       
   483 //
       
   484 void CPbk2CopyContactsCmd::FinishCommandL()
       
   485     {
       
   486     // After the command is done, wake up the UI control.
       
   487     if ( iNameListUiControl )
       
   488         {
       
   489         iNameListUiControl->SetBlank( EFalse );
       
   490         // Reset find due to case that if contact was not really copied
       
   491         // (=the target contact had already all the same fields) then
       
   492         // UI control doesn't receive any events and find text is not
       
   493         // reset by the UI control.
       
   494         iNameListUiControl->ResetFindL();
       
   495         if ( iFocusedContactBookmark )
       
   496             {
       
   497             // The focus must be set due to reason that when copying
       
   498             // causes a merge of contact data (=editing contact) the names
       
   499             // list UI control receives first 'removed' event that will set
       
   500             // focus to incorrect place.
       
   501             iNameListUiControl->SetFocusedContactL(
       
   502                     *iFocusedContactBookmark );    
       
   503             }
       
   504 
       
   505         iNameListUiControl->UpdateAfterCommandExecution();        
       
   506         }
       
   507     
       
   508     iCommandObserver->CommandFinished(*this);
       
   509     }
       
   510     
       
   511 // --------------------------------------------------------------------------
       
   512 // CPbk2CopyContactsCmd::CreateLinkArrayL
       
   513 // --------------------------------------------------------------------------
       
   514 //
       
   515 void CPbk2CopyContactsCmd::CreateLinkArrayL
       
   516         ( MVPbkContactLinkArray& aSelection )
       
   517     {
       
   518     const TVPbkContactStoreUriPtr& targetUri =
       
   519         iTargetStore->StoreProperties().Uri();
       
   520     const TInt count = aSelection.Count();
       
   521     for (TInt i = 0; i < count; ++i)
       
   522         {
       
   523         TVPbkContactStoreUriPtr uri =
       
   524             aSelection[i].ContactStore().StoreProperties().Uri();
       
   525         if (targetUri.Compare(uri,
       
   526             TVPbkContactStoreUriPtr::EContactStoreUriAllComponents) != 0)
       
   527             {
       
   528             iContactLinks->AppendL(aSelection[i].CloneLC());
       
   529             CleanupStack::Pop(); // created link
       
   530             }
       
   531         }
       
   532     }
       
   533 
       
   534 // --------------------------------------------------------------------------
       
   535 // CPbk2CopyContactsCmd::ShowSelectedContactsInfoNoteL
       
   536 // --------------------------------------------------------------------------
       
   537 //
       
   538 void CPbk2CopyContactsCmd::ShowSelectedContactsInfoNoteL()
       
   539     {
       
   540     HBufC* prompt = StringLoader::LoadLC(R_QTN_PHOB_NOTE_NO_COPY_TO_PHONE);
       
   541     CAknInformationNote* dlg = new(ELeave) CAknInformationNote(ETrue);
       
   542     dlg->ExecuteLD(*prompt);
       
   543     CleanupStack::PopAndDestroy(prompt);
       
   544     }
       
   545 
       
   546 // --------------------------------------------------------------------------
       
   547 // CPbk2CopyContactsCmd::HandleError
       
   548 // --------------------------------------------------------------------------
       
   549 //
       
   550 void CPbk2CopyContactsCmd::HandleError(TInt aError)
       
   551     {
       
   552     if (iNameListUiControl)
       
   553         {
       
   554         iNameListUiControl->SetBlank( EFalse );
       
   555         iNameListUiControl->UpdateAfterCommandExecution(); 
       
   556         }
       
   557         
       
   558     if (aError != KErrNone)
       
   559         {
       
   560         // Copy operation is responsible to stop operation
       
   561         CCoeEnv::Static()->HandleError(aError);
       
   562 
       
   563         iDecorator->ProcessStopped();
       
   564         }
       
   565     }
       
   566 
       
   567 //  End of File