phonebookui/Phonebook2/USIMThinExtension/src/CPsu2CopySimContactsCmd.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
child 68 9da50d567e3c
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2002-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 "new SIM" contacts to phone memory.
       
    15 *
       
    16 * NOTE: CPsu2CopySimContactsCmd contains a lot of similar code to this class.
       
    17 * Reason is that there is no clear place to share common SIM related code between 
       
    18 * USIMThinExtension.dll and USIMExtension.dll. This is a potential place for
       
    19 * future refactoring. 
       
    20 *
       
    21 */
       
    22 
       
    23 
       
    24 // INCLUDE FILES
       
    25 #include "CPsu2CopySimContactsCmd.h"
       
    26 
       
    27 // Phonebook 2
       
    28 #include <MPbk2CommandObserver.h>
       
    29 #include <MPbk2StartupMonitor.h>
       
    30 #include <CPbk2StoreConfiguration.h>
       
    31 #include <CPbk2StorePropertyArray.h>
       
    32 #include <CPbk2StoreProperty.h>
       
    33 #include <CPbk2StoreViewDefinition.h>
       
    34 #include <CPbk2SortOrderManager.h>
       
    35 #include <Pbk2ProcessDecoratorFactory.h>
       
    36 #include <TPbk2CopyContactsResults.h>
       
    37 #include <Phonebook2PublicPSKeys.h>
       
    38 #include <Phonebook2PrivatePSKeys.h>
       
    39 #include <Pbk2UID.h>
       
    40 #include <Pbk2USimThinUIRes.rsg>
       
    41 /// Use R_PBK2_GENERAL_CONFIRMATION_QUERY from UI controls
       
    42 /// As a core components UiControl's resource file is loaded in core Pbk2
       
    43 #include <Pbk2UIControls.rsg>
       
    44 /// Use R_GENERAL_COPY_PROGRESS_NOTE from Commands
       
    45 /// As a core components Command's resource file is loaded in core Pbk2
       
    46 #include <Pbk2Commands.rsg>
       
    47 #include <Pbk2DataCaging.hrh>
       
    48 #include <MPbk2AppUi.h>
       
    49 #include <MPbk2ApplicationServices.h>
       
    50 #include <MPbk2StoreValidityInformer.h>
       
    51 
       
    52 // Virtual Phonebook
       
    53 #include <MVPbkContactStoreProperties.h>
       
    54 #include <CVPbkContactManager.h>
       
    55 #include <MVPbkContactStore.h>
       
    56 #include <MVPbkStoreContact.h>
       
    57 #include <MVPbkFieldType.h>
       
    58 #include <VPbkContactStoreUris.h>
       
    59 #include <CVPbkContactStoreUriArray.h>
       
    60 #include <VPbkSimStoreFactory.h>
       
    61 #include <MVPbkSimStateInformation.h>
       
    62 #include <MVPbkContactStoreList.h>
       
    63 #include <MVPbkContactStoreInfo.h>
       
    64 #include <MVPbkContactView.h>
       
    65 #include <CVPbkContactLinkArray.h>
       
    66 #include <MVPbkContactOperationBase.h>
       
    67 #include <CVPbkContactCopier.h>
       
    68 
       
    69 // System includes
       
    70 #include <AknQueryDialog.h>
       
    71 #include <e32property.h>
       
    72 #include <StringLoader.h>
       
    73 #include <e32cmn.h>
       
    74 #include <e32capability.h>
       
    75 
       
    76 // Debugging headers
       
    77 #include <Pbk2Debug.h>
       
    78 
       
    79 /// Unnamed namespace for local definitions
       
    80 namespace {
       
    81 
       
    82 // CONSTANTS
       
    83 const TInt KSimNotChecked = 0;
       
    84 const TInt KSimChecked = 1;
       
    85 _LIT( KPsu2ThinUiExtResourceFile, "Pbk2USimThinUIRes.rsc" );
       
    86 
       
    87 
       
    88 #ifdef _DEBUG
       
    89 enum TPanicCode
       
    90     {
       
    91     EPreCond_ValidStoreConfigurationL,
       
    92     EStorePropertyNotFound_CreateSimViewsL,
       
    93     ENullTarget_CopyContactsL
       
    94     };
       
    95 
       
    96 void Panic(TInt aReason)
       
    97     {
       
    98     _LIT(KPanicText, "CPsu2CopySimContactsCmd");
       
    99     User::Panic(KPanicText, aReason);
       
   100     }
       
   101 #endif // _DEBUG
       
   102 
       
   103 /// Tasks
       
   104 enum TNextTask
       
   105     {
       
   106     ECheckCopyPrerequisites,
       
   107     EOpenSimStores,
       
   108     ECheckSimStorePrerequisites,
       
   109     ELaunchNewSIMInsertedQuery,
       
   110     ECreateSimViews,
       
   111     ECopyContacts,
       
   112     EShowResults,
       
   113     EComplete
       
   114     };
       
   115 
       
   116 } /// namespace
       
   117 
       
   118 
       
   119 // --------------------------------------------------------------------------
       
   120 // CPsu2CopySimContactsCmd::CPsu2CopySimContactsCmd
       
   121 // --------------------------------------------------------------------------
       
   122 //
       
   123 CPsu2CopySimContactsCmd::CPsu2CopySimContactsCmd
       
   124         ( MPbk2StartupMonitor& aStartupMonitor ) :
       
   125             CActive( EPriorityStandard ),
       
   126             iStartupMonitor( aStartupMonitor ),
       
   127             iResourceFile( *CCoeEnv::Static() )
       
   128     {
       
   129     }
       
   130 
       
   131 // --------------------------------------------------------------------------
       
   132 // CPsu2CopySimContactsCmd::~CPsu2CopySimContactsCmd
       
   133 // --------------------------------------------------------------------------
       
   134 //
       
   135 CPsu2CopySimContactsCmd::~CPsu2CopySimContactsCmd()
       
   136     {
       
   137     Cancel();
       
   138     delete iDecorator;
       
   139     delete iCopyOperation;
       
   140     delete iCopier;
       
   141     delete iCopiedContacts;
       
   142     delete iValidSourceStoreUris;
       
   143     iSourceViews.ResetAndDestroy();
       
   144     CloseStores();
       
   145     delete iSourceContactLinks;
       
   146     iSourceStores.Close();
       
   147     iResourceFile.Close();
       
   148     }
       
   149 
       
   150 // --------------------------------------------------------------------------
       
   151 // CPsu2CopySimContactsCmd::NewL
       
   152 // --------------------------------------------------------------------------
       
   153 //
       
   154 CPsu2CopySimContactsCmd* CPsu2CopySimContactsCmd::NewL
       
   155         ( MPbk2StartupMonitor& aStartupMonitor )
       
   156     {
       
   157     CPsu2CopySimContactsCmd* self =
       
   158         new( ELeave ) CPsu2CopySimContactsCmd( aStartupMonitor );
       
   159     CleanupStack::PushL( self );
       
   160     self->ConstructL();
       
   161     CleanupStack::Pop( self );
       
   162     return self;
       
   163     }
       
   164 
       
   165 // --------------------------------------------------------------------------
       
   166 // CPsu2CopySimContactsCmd::ConstructL
       
   167 // Symbian 2nd phase constructor can leave.
       
   168 // --------------------------------------------------------------------------
       
   169 //
       
   170 void CPsu2CopySimContactsCmd::ConstructL()
       
   171     {
       
   172     CActiveScheduler::Add(this);
       
   173 
       
   174     iDecorator = Pbk2ProcessDecoratorFactory::CreateProgressDialogDecoratorL
       
   175         ( R_GENERAL_COPY_PROGRESS_NOTE, EFalse );
       
   176     iDecorator->SetObserver( *this );
       
   177     }
       
   178 
       
   179 // --------------------------------------------------------------------------
       
   180 // CPsu2CopySimContactsCmd::RunL
       
   181 // --------------------------------------------------------------------------
       
   182 //
       
   183 void CPsu2CopySimContactsCmd::RunL()
       
   184     {
       
   185     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   186         ("CPsu2CopySimContactsCmd::RunL res %d task %d"),
       
   187         iStatus.Int(), iNextTask);
       
   188 
       
   189     if ( iStatus.Int() == KErrNone )
       
   190         {
       
   191         switch ( iNextTask )
       
   192             {
       
   193             case ECheckCopyPrerequisites:
       
   194                 {
       
   195                 CheckCopyPrerequisitesL();
       
   196                 break;
       
   197                 }
       
   198             case EOpenSimStores:
       
   199                 {
       
   200                 OpenSimStoresL();
       
   201                 break;
       
   202                 }
       
   203             case ECheckSimStorePrerequisites:
       
   204                 {
       
   205                 CheckSimStorePrerequisitesL();
       
   206                 break;
       
   207                 }
       
   208             case ELaunchNewSIMInsertedQuery:
       
   209                 {
       
   210                 LaunchNewSIMInsertedQueryL();
       
   211                 break;
       
   212                 }
       
   213             case ECreateSimViews:
       
   214                 {
       
   215                 CreateSimViewsL();
       
   216                 break;
       
   217                 }
       
   218             case ECopyContacts:
       
   219                 {
       
   220                 CopyContactsL();
       
   221                 break;
       
   222                 }
       
   223             case EShowResults:
       
   224                 {
       
   225                 ShowResultsL();
       
   226                 break;
       
   227                 }
       
   228             case EComplete: // FALLTHROUGH
       
   229             default:
       
   230                 {
       
   231                 CompleteL();
       
   232                 break;
       
   233                 }
       
   234             }
       
   235         }
       
   236     else
       
   237         {
       
   238         CompleteWithError( iStatus.Int() );
       
   239         }
       
   240     }
       
   241 
       
   242 // --------------------------------------------------------------------------
       
   243 // CPsu2CopySimContactsCmd::DoCancel
       
   244 // --------------------------------------------------------------------------
       
   245 //
       
   246 void CPsu2CopySimContactsCmd::DoCancel()
       
   247     {
       
   248     // Nothing to do here
       
   249     }
       
   250 
       
   251 // --------------------------------------------------------------------------
       
   252 // CPsu2CopySimContactsCmd::RunError
       
   253 // --------------------------------------------------------------------------
       
   254 //
       
   255 TInt CPsu2CopySimContactsCmd::RunError( TInt aError )
       
   256     {
       
   257     iStartupMonitor.HandleStartupFailed( aError );
       
   258     iCommandObserver->CommandFinished(*this);
       
   259     return KErrNone;
       
   260     }
       
   261 
       
   262 // --------------------------------------------------------------------------
       
   263 // CPsu2CopySimContactsCmd::ExecuteLD
       
   264 // --------------------------------------------------------------------------
       
   265 //
       
   266 void CPsu2CopySimContactsCmd::ExecuteLD()
       
   267     {
       
   268     iNextTask = ECheckCopyPrerequisites;
       
   269     IssueRequest();
       
   270     }
       
   271 
       
   272 // --------------------------------------------------------------------------
       
   273 // CPsu2CopySimContactsCmd::AddObserver
       
   274 // --------------------------------------------------------------------------
       
   275 //
       
   276 void CPsu2CopySimContactsCmd::AddObserver( MPbk2CommandObserver& aObserver )
       
   277     {
       
   278     iCommandObserver = &aObserver;
       
   279     }
       
   280 
       
   281 // --------------------------------------------------------------------------
       
   282 // CPsu2CopySimContactsCmd::ResetUiControl
       
   283 // --------------------------------------------------------------------------
       
   284 //
       
   285 void CPsu2CopySimContactsCmd::ResetUiControl
       
   286         ( MPbk2ContactUiControl& /*aUiControl*/ )
       
   287     {
       
   288     // Do nothing
       
   289     }
       
   290 
       
   291 // --------------------------------------------------------------------------
       
   292 // CPsu2CopySimContactsCmd::StoreReady
       
   293 // --------------------------------------------------------------------------
       
   294 //
       
   295 void CPsu2CopySimContactsCmd::StoreReady
       
   296         ( MVPbkContactStore& /*aContactStore*/)
       
   297     {
       
   298     ++iCounter;
       
   299     if ( iSourceStores.Count() == iCounter )
       
   300         {
       
   301         iNextTask = ECheckSimStorePrerequisites;
       
   302         IssueRequest();
       
   303         }
       
   304     }
       
   305 
       
   306 // --------------------------------------------------------------------------
       
   307 // CPsu2CopySimContactsCmd::StoreUnavailable
       
   308 // --------------------------------------------------------------------------
       
   309 //
       
   310 void CPsu2CopySimContactsCmd::StoreUnavailable
       
   311         ( MVPbkContactStore& aContactStore,  TInt /*aReason*/ )
       
   312     {
       
   313     // Remove URI
       
   314     iValidSourceStoreUris->Remove( aContactStore.StoreProperties().Uri() );
       
   315 
       
   316     // Remove store
       
   317     const TInt count = iSourceStores.Count();
       
   318     TBool storeRemoved = EFalse;
       
   319     for ( TInt i = 0; i < count && !storeRemoved; ++i )
       
   320         {
       
   321         if ( iSourceStores[i] == &aContactStore )
       
   322             {
       
   323             iSourceStores[i]->Close( *this ); 
       
   324             iSourceStores.Remove( i );
       
   325             storeRemoved = ETrue;
       
   326             }
       
   327         }
       
   328 
       
   329     // If all source stores failed to open -> complete command
       
   330     if ( iSourceStores.Count() == 0 )
       
   331         {
       
   332         iNextTask = EComplete;
       
   333         IssueRequest();
       
   334         }
       
   335     // All available stores are open
       
   336     else if ( iSourceStores.Count() == iCounter )
       
   337         {
       
   338         iNextTask = ECheckSimStorePrerequisites;
       
   339         IssueRequest();
       
   340         }
       
   341     }
       
   342 
       
   343 // --------------------------------------------------------------------------
       
   344 // CPsu2CopySimContactsCmd::HandleStoreEventL
       
   345 // --------------------------------------------------------------------------
       
   346 //
       
   347 void CPsu2CopySimContactsCmd::HandleStoreEventL
       
   348         ( MVPbkContactStore& /*aContactStore*/,
       
   349           TVPbkContactStoreEvent /*aStoreEvent*/ )
       
   350     {
       
   351     // Nothing to be done here
       
   352     }
       
   353 
       
   354 // --------------------------------------------------------------------------
       
   355 // CPsu2CopySimContactsCmd::ContactViewReady
       
   356 // --------------------------------------------------------------------------
       
   357 //
       
   358 void CPsu2CopySimContactsCmd::ContactViewReady
       
   359         ( MVPbkContactViewBase& /*aView*/)
       
   360     {
       
   361     ++iCounter;
       
   362     if ( iSourceViews.Count() == iCounter )
       
   363         {
       
   364         iNextTask = ECopyContacts;
       
   365         IssueRequest();
       
   366         }
       
   367     }
       
   368 
       
   369 // --------------------------------------------------------------------------
       
   370 // CPsu2CopySimContactsCmd::ContactViewUnavailable
       
   371 // --------------------------------------------------------------------------
       
   372 //
       
   373 void CPsu2CopySimContactsCmd::ContactViewUnavailable
       
   374         ( MVPbkContactViewBase& /*aView*/ )
       
   375     {
       
   376     // Nothing to be done here, wait view ready events
       
   377     }
       
   378 
       
   379 // --------------------------------------------------------------------------
       
   380 // CPsu2CopySimContactsCmd::ContactAddedToView
       
   381 // --------------------------------------------------------------------------
       
   382 //
       
   383 void CPsu2CopySimContactsCmd::ContactAddedToView
       
   384         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   385           const MVPbkContactLink& /*aContactLink*/ )
       
   386     {
       
   387     // Nothing to be done here
       
   388     }
       
   389 
       
   390 // --------------------------------------------------------------------------
       
   391 // CPsu2CopySimContactsCmd::ContactRemovedFromView
       
   392 // --------------------------------------------------------------------------
       
   393 //
       
   394 void CPsu2CopySimContactsCmd::ContactRemovedFromView
       
   395         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   396           const MVPbkContactLink& /*aContactLink*/ )
       
   397     {
       
   398     // Nothing to be done here
       
   399     }
       
   400 
       
   401 // --------------------------------------------------------------------------
       
   402 // CPsu2CopySimContactsCmd::ContactViewError
       
   403 // --------------------------------------------------------------------------
       
   404 //
       
   405 void CPsu2CopySimContactsCmd::ContactViewError
       
   406         ( MVPbkContactViewBase& /*aView*/, TInt aError,
       
   407           TBool /*aErrorNotified*/ )
       
   408     {
       
   409     IssueRequest( aError );
       
   410     }
       
   411 
       
   412 // --------------------------------------------------------------------------
       
   413 // CPsu2CopySimContactsCmd::StepComplete
       
   414 // --------------------------------------------------------------------------
       
   415 //
       
   416 void CPsu2CopySimContactsCmd::StepComplete
       
   417         ( MVPbkContactOperationBase& /*aOperation*/, TInt aStepSize )
       
   418     {
       
   419     iCopiedSuccessfully += aStepSize;
       
   420     iDecorator->ProcessAdvance( aStepSize );
       
   421     }
       
   422 
       
   423 // --------------------------------------------------------------------------
       
   424 // CPsu2CopySimContactsCmd::StepFailed
       
   425 // --------------------------------------------------------------------------
       
   426 //
       
   427 TBool CPsu2CopySimContactsCmd::StepFailed
       
   428         ( MVPbkContactOperationBase& /*aOperation*/, TInt /*aStepSize*/,
       
   429           TInt aError )
       
   430     {
       
   431     CCoeEnv::Static()->HandleError(aError);
       
   432     if( aError == KErrDiskFull )
       
   433     	{
       
   434     	// When disk get full during copy, show the results
       
   435         Cancel();
       
   436         delete iCopyOperation;
       
   437         iCopyOperation = NULL;
       
   438         iNextTask = EShowResults;
       
   439         IssueRequest();
       
   440     	}
       
   441     else
       
   442     	{
       
   443         IssueRequest( aError );
       
   444     	}
       
   445     return EFalse;
       
   446     }
       
   447 
       
   448 // --------------------------------------------------------------------------
       
   449 // CPsu2CopySimContactsCmd::OperationComplete
       
   450 // --------------------------------------------------------------------------
       
   451 //
       
   452 void CPsu2CopySimContactsCmd::OperationComplete
       
   453         ( MVPbkContactOperationBase& /*aOperation*/ )
       
   454     {
       
   455     // Decorator calls ProcessDismissed
       
   456     iDecorator->ProcessStopped();
       
   457     }
       
   458 
       
   459 // --------------------------------------------------------------------------
       
   460 // CPsu2CopySimContactsCmd::ProcessDismissed
       
   461 // --------------------------------------------------------------------------
       
   462 //
       
   463 void CPsu2CopySimContactsCmd::ProcessDismissed( TInt /*aCancelCode*/ )
       
   464     {
       
   465     Cancel();
       
   466     delete iCopyOperation;
       
   467     iCopyOperation = NULL;
       
   468     iNextTask = EShowResults;
       
   469     IssueRequest();
       
   470     }
       
   471 
       
   472 // --------------------------------------------------------------------------
       
   473 // CPsu2CopySimContactsCmd::IssueRequest
       
   474 // --------------------------------------------------------------------------
       
   475 //
       
   476 void CPsu2CopySimContactsCmd::IssueRequest()
       
   477     {
       
   478     IssueRequest( KErrNone );
       
   479     }
       
   480 
       
   481 // --------------------------------------------------------------------------
       
   482 // CPsu2CopySimContactsCmd::IssueRequest
       
   483 // --------------------------------------------------------------------------
       
   484 //
       
   485 void CPsu2CopySimContactsCmd::IssueRequest( TInt aResult )
       
   486     {
       
   487     TRequestStatus* status = &iStatus;
       
   488     User::RequestComplete( status, aResult );
       
   489     SetActive();
       
   490     }
       
   491 
       
   492 // --------------------------------------------------------------------------
       
   493 // CPsu2CopySimContactsCmd::CheckCopyPrerequisitesL
       
   494 // --------------------------------------------------------------------------
       
   495 //
       
   496 void CPsu2CopySimContactsCmd::CheckCopyPrerequisitesL()
       
   497     {
       
   498     if ( ValidStoreConfigurationL() && 
       
   499          NewSimCardForPhonebook2AppL() )
       
   500         {
       
   501         iNextTask = EOpenSimStores;
       
   502         }
       
   503     else
       
   504         {
       
   505         iNextTask = EComplete;
       
   506         }
       
   507     IssueRequest();
       
   508     }
       
   509 
       
   510 // --------------------------------------------------------------------------
       
   511 // CPsu2CopySimContactsCmd::OpenSimStoresL
       
   512 // --------------------------------------------------------------------------
       
   513 //
       
   514 void CPsu2CopySimContactsCmd::OpenSimStoresL()
       
   515     {
       
   516     // Load and open source stores to contact manager
       
   517     const TInt count = iValidSourceStoreUris->Count();
       
   518     for ( TInt i = 0; i < count; ++i )
       
   519         {
       
   520         Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   521             LoadContactStoreL( (*iValidSourceStoreUris)[i] );
       
   522         MVPbkContactStore* source =
       
   523             Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   524                 ContactStoresL().Find( (*iValidSourceStoreUris)[i] );
       
   525         iSourceStores.AppendL( source );
       
   526         source->OpenL( *this );
       
   527         }
       
   528     }
       
   529 
       
   530 // --------------------------------------------------------------------------
       
   531 // CPsu2CopySimContactsCmd::CheckSimStorePrerequisitesL
       
   532 // --------------------------------------------------------------------------
       
   533 //
       
   534 void CPsu2CopySimContactsCmd::CheckSimStorePrerequisitesL()
       
   535     {
       
   536     // If there are no contacts to copy then do nothing
       
   537     TInt sourceContactCount = 0;
       
   538     const TInt count = iSourceStores.Count();
       
   539     for ( TInt i = 0; i < count; ++i )
       
   540         {
       
   541         sourceContactCount +=
       
   542             iSourceStores[i]->StoreInfo().NumberOfContactsL();
       
   543         }
       
   544 
       
   545     iNextTask = EComplete;
       
   546     if ( sourceContactCount > 0 )
       
   547         {
       
   548         iNextTask = ELaunchNewSIMInsertedQuery;
       
   549         }
       
   550     else
       
   551     	{
       
   552     	SetNewSimCardKeyL();    	
       
   553     	}
       
   554     
       
   555     IssueRequest();
       
   556     }
       
   557 
       
   558 // --------------------------------------------------------------------------
       
   559 // CPsu2CopySimContactsCmd::LaunchNewSIMInsertedQueryL
       
   560 // --------------------------------------------------------------------------
       
   561 //
       
   562 void CPsu2CopySimContactsCmd::LaunchNewSIMInsertedQueryL()
       
   563     {
       
   564     // Open resource as late as possible so that it's not opened in start-up
       
   565     // if not needed
       
   566     iResourceFile.OpenL( KPbk2RomFileDrive,
       
   567         KDC_RESOURCE_FILES_DIR, KPsu2ThinUiExtResourceFile );
       
   568 
       
   569     HBufC* text = StringLoader::LoadLC( R_QTN_SIMP_NOTE_NEW_SIM_COPY );
       
   570 
       
   571     CAknQueryDialog* dlg = CAknQueryDialog::NewL();
       
   572     TInt ret = dlg->ExecuteLD( R_PBK2_GENERAL_CONFIRMATION_QUERY, *text );
       
   573     if ( iAvkonAppUi->IsForeground() )
       
   574     	{
       
   575     	// Despite of user answer set P&S key
       
   576         SetNewSimCardKeyL();
       
   577         SetShowSIMContactsL( ret );        
       
   578         iNextTask = ret ? ECreateSimViews : EComplete;        
       
   579     	}
       
   580     else
       
   581     	{
       
   582 	    iNextTask = EComplete;
       
   583     	}
       
   584 
       
   585     CleanupStack::PopAndDestroy( text );
       
   586     IssueRequest();
       
   587     }
       
   588 
       
   589 
       
   590 // --------------------------------------------------------------------------
       
   591 // CPsu2CopySimContactsCmd::SetShowSIMContactsL
       
   592 // --------------------------------------------------------------------------
       
   593 //
       
   594 void CPsu2CopySimContactsCmd::SetShowSIMContactsL( TBool aShow )
       
   595     {
       
   596     const TDesC& adnUri = VPbkContactStoreUris::SimGlobalAdnUri();
       
   597     
       
   598     CVPbkContactStoreUriArray* uriArray = 
       
   599         Phonebook2::Pbk2AppUi()->ApplicationServices().StoreConfiguration().
       
   600             CurrentConfigurationL();
       
   601     CleanupStack::PushL( uriArray );
       
   602 
       
   603     if ( iAvkonAppUi->IsForeground() )
       
   604     	{
       
   605 	    if ( aShow )
       
   606 	        {
       
   607 	        if ( !uriArray->IsIncluded(adnUri) )
       
   608 	            {
       
   609 	            // Show SIM contact also in names list:
       
   610 	            Phonebook2::Pbk2AppUi()->ApplicationServices().StoreConfiguration().
       
   611 	                AddContactStoreURIL( adnUri );
       
   612 	            }
       
   613 	        }
       
   614 	    else
       
   615 	        {
       
   616 	        if ( uriArray->IsIncluded(adnUri) )
       
   617 	            {
       
   618 	            // Do not show SIM contacts:
       
   619 	            Phonebook2::Pbk2AppUi()->ApplicationServices().StoreConfiguration().
       
   620 	                RemoveContactStoreURIL( adnUri );
       
   621 	            }
       
   622 	        }
       
   623 
       
   624 	    iNextTask = EComplete;
       
   625     	}
       
   626 
       
   627     CleanupStack::PopAndDestroy( uriArray );
       
   628     }
       
   629 
       
   630 
       
   631 // --------------------------------------------------------------------------
       
   632 // CPsu2CopySimContactsCmd::CreateSimViewsL
       
   633 // --------------------------------------------------------------------------
       
   634 //
       
   635 void CPsu2CopySimContactsCmd::CreateSimViewsL()
       
   636     {
       
   637     // Set views ready counter to zero
       
   638     iCounter = 0;
       
   639 
       
   640     const TInt count = iSourceStores.Count();
       
   641     for ( TInt i = 0; i < count; ++i )
       
   642         {
       
   643         // Find store property
       
   644         const CPbk2StoreProperty* prop =
       
   645             Phonebook2::Pbk2AppUi()->ApplicationServices().StoreProperties().
       
   646                 FindProperty( iSourceStores[i]->StoreProperties().Uri() );
       
   647         __ASSERT_DEBUG( prop,
       
   648             Panic( EStorePropertyNotFound_CreateSimViewsL ) );
       
   649         // Get Virtual Phonebook view definition
       
   650         // There is only one view defintion for SIM stores
       
   651         // -> take the first one
       
   652         const CVPbkContactViewDefinition& viewDef =
       
   653             (prop->Views())[0]->ViewDefinition();
       
   654         MVPbkContactView* view = iSourceStores[i]->CreateViewLC(
       
   655             viewDef,
       
   656             *this,
       
   657             Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   658                 SortOrderManager().SortOrder() );
       
   659         iSourceViews.AppendL( view );
       
   660         CleanupStack::Pop(); // view
       
   661         }
       
   662     }
       
   663 
       
   664 // --------------------------------------------------------------------------
       
   665 // CPsu2CopySimContactsCmd::CopyContactsL
       
   666 // --------------------------------------------------------------------------
       
   667 //
       
   668 void CPsu2CopySimContactsCmd::CopyContactsL()
       
   669     {
       
   670     if ( !iCopier )
       
   671         {
       
   672         iCopier = CVPbkContactCopier::NewL
       
   673             ( Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager() );
       
   674         }
       
   675     
       
   676     // Create an array for source contact links
       
   677     if ( !iSourceContactLinks )
       
   678         {
       
   679         iSourceContactLinks = CVPbkContactLinkArray::NewL();
       
   680         }
       
   681 
       
   682     if ( !iCopiedContacts )
       
   683         {
       
   684         iCopiedContacts = CVPbkContactLinkArray::NewL();
       
   685         }
       
   686     
       
   687     // Create links to the contacts that are copied
       
   688     const TInt viewCount = iSourceViews.Count();
       
   689     for ( TInt i = 0; i < viewCount; ++i )
       
   690         {
       
   691         const TInt contactCount = iSourceViews[i]->ContactCountL();
       
   692         for( TInt j = 0; j < contactCount; ++j )
       
   693             {
       
   694             MVPbkContactLink* link =
       
   695                 iSourceViews[i]->ContactAtL(j).CreateLinkLC();
       
   696             iSourceContactLinks->AppendL( link );
       
   697             CleanupStack::Pop(); // link
       
   698             }
       
   699         }
       
   700 
       
   701     // Views can be destroyed now because links have been created
       
   702     iSourceViews.ResetAndDestroy();
       
   703 
       
   704     // Get target store
       
   705     MVPbkContactStore* targetStore =
       
   706         Phonebook2::Pbk2AppUi()->ApplicationServices().ContactManager().
       
   707             ContactStoresL().Find( VPbkContactStoreUris::DefaultCntDbUri() );
       
   708     // Target store must exist as cheked in ValidStoreConfigurationL
       
   709     __ASSERT_DEBUG( targetStore, Panic( ENullTarget_CopyContactsL ));
       
   710 
       
   711     // Start copying
       
   712     iCopiedContacts->ResetAndDestroy();
       
   713     iCopyOperation = iCopier->CopyContactsL(
       
   714         CVPbkContactCopier::EVPbkUsePlatformSpecificDuplicatePolicy,
       
   715         *iSourceContactLinks, targetStore, *iCopiedContacts, *this );
       
   716 
       
   717 
       
   718     // Show progress note
       
   719     iDecorator->ProcessStartedL( iSourceContactLinks->Count() );
       
   720     }
       
   721 
       
   722 // --------------------------------------------------------------------------
       
   723 // CPsu2CopySimContactsCmd::ShowResultsL
       
   724 // --------------------------------------------------------------------------
       
   725 //
       
   726 void CPsu2CopySimContactsCmd::ShowResultsL()
       
   727     {
       
   728     TPbk2CopyContactsResults results(
       
   729         iCopiedSuccessfully, iSourceContactLinks->Count() );
       
   730     results.ShowNoteL();
       
   731 
       
   732     iNextTask = EComplete;
       
   733     IssueRequest();
       
   734     }
       
   735 
       
   736 // --------------------------------------------------------------------------
       
   737 // CPsu2CopySimContactsCmd::CompleteL
       
   738 // --------------------------------------------------------------------------
       
   739 //
       
   740 void CPsu2CopySimContactsCmd::CompleteL()
       
   741     {
       
   742     iStartupMonitor.HandleStartupComplete();
       
   743     iCommandObserver->CommandFinished( *this );
       
   744 
       
   745     if (iAvkonAppUi->IsForeground())
       
   746         {
       
   747         PublishOpenCompleteL();
       
   748         }
       
   749     }
       
   750 
       
   751 // --------------------------------------------------------------------------
       
   752 // CPsu2CopySimContactsCmd::PublishOpenCompleteL
       
   753 // --------------------------------------------------------------------------
       
   754 //
       
   755 void CPsu2CopySimContactsCmd::PublishOpenCompleteL()
       
   756     {
       
   757     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   758         ("CPsu2CopySimContactsCmd::PublishOpenCompleteL") );
       
   759 
       
   760     TInt err = RProperty::Define( TUid::Uid( KPbk2PSUidPublic ),
       
   761                                   KPhonebookOpenCompleted, RProperty::EInt );
       
   762     if ( err != KErrAlreadyExists )
       
   763         {
       
   764         User::LeaveIfError( err );
       
   765         }
       
   766     RProperty prop;
       
   767     CleanupClosePushL( prop );
       
   768     User::LeaveIfError(prop.Attach( TUid::Uid( KPbk2PSUidPublic ),
       
   769                                     KPhonebookOpenCompleted )); 
       
   770     TInt value( EPhonebookClosed );
       
   771     err = prop.Get( value );
       
   772     if ( err == KErrNone && value != EPhonebookOpenCompleted)
       
   773         {
       
   774         // Only publish once per phonebook opening
       
   775         PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   776             ("publish EPhonebookOpenCompleted") );
       
   777         err = prop.Set( EPhonebookOpenCompleted );
       
   778         }
       
   779     User::LeaveIfError( err );
       
   780 
       
   781     CleanupStack::PopAndDestroy(&prop);
       
   782     }
       
   783 
       
   784 
       
   785 // --------------------------------------------------------------------------
       
   786 // CPsu2CopySimContactsCmd::CompleteWithError
       
   787 // --------------------------------------------------------------------------
       
   788 //
       
   789 void CPsu2CopySimContactsCmd::CompleteWithError( TInt aError )
       
   790     {
       
   791     iStartupMonitor.HandleStartupFailed( aError );
       
   792     iCommandObserver->CommandFinished( *this );
       
   793     }
       
   794 
       
   795 // --------------------------------------------------------------------------
       
   796 // CPsu2CopySimContactsCmd::ValidStoreConfigurationL
       
   797 // --------------------------------------------------------------------------
       
   798 //
       
   799 TBool CPsu2CopySimContactsCmd::ValidStoreConfigurationL()
       
   800     {
       
   801     __ASSERT_DEBUG( !iValidSourceStoreUris,
       
   802         Panic(EPreCond_ValidStoreConfigurationL));
       
   803 
       
   804     CVPbkContactStoreUriArray* currentConfig =
       
   805         Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   806             StoreConfiguration().CurrentConfigurationL();
       
   807     CleanupStack::PushL( currentConfig );
       
   808     CVPbkContactStoreUriArray* validConfig =
       
   809         Phonebook2::Pbk2AppUi()->ApplicationServices().
       
   810             StoreValidityInformer().CurrentlyValidStoresL();
       
   811     CleanupStack::PushL( validConfig );
       
   812 
       
   813     // ADN/SDN is valid source if it's not in current configuration
       
   814     iValidSourceStoreUris = CVPbkContactStoreUriArray::NewL();
       
   815     const TDesC& adnUri = VPbkContactStoreUris::SimGlobalAdnUri();
       
   816     const TDesC& sdnUri = VPbkContactStoreUris::SimGlobalSdnUri();
       
   817     if ( !currentConfig->IsIncluded( adnUri ))
       
   818         {
       
   819         iValidSourceStoreUris->AppendL( adnUri );
       
   820         }
       
   821     if ( !currentConfig->IsIncluded( sdnUri ))
       
   822         {
       
   823         iValidSourceStoreUris->AppendL( sdnUri );
       
   824         }
       
   825     
       
   826     // No sim store needs to be handled in this command, 
       
   827     // directly set the flag.
       
   828     if ( iValidSourceStoreUris->Count() == 0 )
       
   829     	{
       
   830     	SetNewSimCardKeyL();    	
       
   831     	}
       
   832 
       
   833     TBool result = EFalse;
       
   834     // If there are source stores and contact model is valid
       
   835     // then configuration is valid
       
   836     if ( iValidSourceStoreUris->Count() > 0 &&
       
   837          validConfig->IsIncluded(
       
   838             VPbkContactStoreUris::DefaultCntDbUri() ))
       
   839         {
       
   840         result = ETrue;
       
   841         }
       
   842 
       
   843     CleanupStack::PopAndDestroy( 2 );
       
   844     return result;
       
   845     }
       
   846 
       
   847 // --------------------------------------------------------------------------
       
   848 // CPsu2CopySimContactsCmd::NewSimCardForPhonebook2AppL
       
   849 // --------------------------------------------------------------------------
       
   850 //
       
   851 TBool CPsu2CopySimContactsCmd::NewSimCardForPhonebook2AppL()
       
   852     {
       
   853     MVPbkSimStateInformation* simStateInfo =
       
   854         VPbkSimStoreFactory::GetSimStateInformationL();
       
   855     CleanupDeletePushL( simStateInfo );
       
   856     TBool newSimCard = simStateInfo->NewSimCardL();
       
   857     CleanupStack::PopAndDestroy(); // simStateInfo
       
   858 
       
   859     if ( newSimCard )
       
   860         {
       
   861         RProperty queryProperty;
       
   862         TInt queryDone = KSimNotChecked;
       
   863         TInt result = queryProperty.Get(
       
   864             TUid::Uid( KPbk2UID3 ), KPhonebookSimCopyAsked, queryDone );
       
   865         queryProperty.Close();
       
   866         PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   867             ("CPsu2CopySimContactsCmd::NewSimCardForPhonebook2AppL res %d status %d"),
       
   868             result, queryDone);
       
   869         // If result is KErrNone it means that the key has been set
       
   870         // and query has been asked before, KErrNotFound means that key
       
   871         // is not set and query hasn't been asked
       
   872         if ( result == KErrNotFound )
       
   873             {
       
   874             return ETrue;
       
   875             }
       
   876         }
       
   877     return EFalse;
       
   878     }
       
   879 
       
   880 // --------------------------------------------------------------------------
       
   881 // CPsu2CopySimContactsCmd::CloseStores
       
   882 // --------------------------------------------------------------------------
       
   883 //
       
   884 void CPsu2CopySimContactsCmd::CloseStores()
       
   885     {
       
   886     const TInt count = iSourceStores.Count();
       
   887     for ( TInt i = 0; i < count; ++i )
       
   888         {
       
   889         iSourceStores[i]->Close( *this );
       
   890         }
       
   891     }
       
   892 
       
   893 // --------------------------------------------------------------------------
       
   894 // CPsu2CopySimContactsCmd::SetNewSimCardKeyL
       
   895 // --------------------------------------------------------------------------
       
   896 //
       
   897 void CPsu2CopySimContactsCmd::SetNewSimCardKeyL()
       
   898     {
       
   899     RProperty queryProperty;
       
   900     CleanupClosePushL( queryProperty );
       
   901 
       
   902     // Check if the key has been defined
       
   903     TInt queryDone = KSimNotChecked;
       
   904     TInt result = queryProperty.Get(
       
   905         TUid::Uid( KPbk2UID3 ), KPhonebookSimCopyAsked, queryDone );
       
   906 
       
   907     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   908         ("CPsu2CopySimContactsCmd::SetNewSimCardKeyL res %d status %d"),
       
   909         result, queryDone);
       
   910 
       
   911     if ( result == KErrNotFound )
       
   912         {
       
   913         _LIT_SECURITY_POLICY_C1( newSimCardQueryReadPolicy,
       
   914             ECapabilityReadUserData );
       
   915         _LIT_SECURITY_POLICY_S0( newSimCardQueryWritePolicy, KPbk2UID3 );
       
   916 
       
   917         // Define a property for query done flag
       
   918         User::LeaveIfError( queryProperty.Define(
       
   919             TUid::Uid( KPbk2UID3 ),
       
   920             KPhonebookSimCopyAsked,
       
   921             RProperty::EInt,
       
   922             newSimCardQueryReadPolicy,
       
   923             newSimCardQueryWritePolicy ));
       
   924         }
       
   925 
       
   926     // Set the value to KSimChecked
       
   927     User::LeaveIfError( queryProperty.Set(
       
   928         TUid::Uid( KPbk2UID3 ),
       
   929         KPhonebookSimCopyAsked,
       
   930         KSimChecked ));
       
   931 
       
   932     CleanupStack::PopAndDestroy(); // queryProperty
       
   933     }
       
   934 
       
   935 //  End of File