phonebookui/Phonebook2/CommandsExtension/src/CPbk2CopyContactsStarterCmd.cpp
changeset 0 e686773b3f54
child 68 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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 contacts starter command.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2CopyContactsStarterCmd.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "CPbk2CopyContactsCmd.h"
       
    23 #include "TPbk2CopyContactsUtil.h"
       
    24 #include <CPbk2AppUiBase.h>
       
    25 #include <CPbk2StoreListQuery.h>
       
    26 #include <CPbk2UIExtensionManager.h>
       
    27 #include <MPbk2UIExtensionFactory.h>
       
    28 #include <MPbk2CommandObserver.h>
       
    29 #include <CPbk2StoreProperty.h>
       
    30 #include <CPbk2StorePropertyArray.h>
       
    31 #include <CPbk2StoreConfiguration.h>
       
    32 #include <MPbk2AppUi.h>
       
    33 #include <CPbk2ApplicationServices.h>
       
    34 #include <Pbk2Commands.hrh>
       
    35 #include <Pbk2Commands.rsg>
       
    36 
       
    37 // Virtual Phonebook
       
    38 #include <CVPbkContactManager.h>
       
    39 #include <MVPbkContactStore.h>
       
    40 #include <MVPbkContactStoreList.h>
       
    41 #include <CVPbkContactStoreUriArray.h>
       
    42 #include <TVPbkContactStoreUriPtr.h>
       
    43 #include <MVPbkContactStoreProperties.h>
       
    44 #include <CVPbkContactLinkArray.h>
       
    45 #include <MVPbkContactLink.h>
       
    46 #include <MVPbkBaseContact.h>
       
    47 
       
    48 // System includes
       
    49 #include <StringLoader.h>
       
    50 #include <aknlistquerydialog.h>
       
    51 #include <aknnotewrappers.h>
       
    52 #include <badesca.h>
       
    53 
       
    54 /// Unnamed namespace for local definitions
       
    55 namespace {
       
    56 
       
    57 #ifdef _DEBUG
       
    58 enum TPanicCode
       
    59     {
       
    60     EPanicPreCond_Null_Pointer = 1
       
    61     };
       
    62 
       
    63 void Panic(TPanicCode aReason)
       
    64     {
       
    65     _LIT(KPanicText, "CPbk2CopyContactsStarterCmd");
       
    66     User::Panic(KPanicText,aReason);
       
    67     }
       
    68 #endif // _DEBUG
       
    69 
       
    70 } /// namespace
       
    71 
       
    72 // --------------------------------------------------------------------------
       
    73 // CPbk2CopyContactsStarterCmd::CPbk2CopyContactsStarterCmd
       
    74 // --------------------------------------------------------------------------
       
    75 //
       
    76 CPbk2CopyContactsStarterCmd::CPbk2CopyContactsStarterCmd
       
    77         ( MPbk2ContactUiControl& aUiControl ) :
       
    78             CActive( EPriorityStandard ),
       
    79             iUiControl( &aUiControl),
       
    80             iState( EPbk2CopyOpenStores )
       
    81     {
       
    82     }
       
    83 
       
    84 // --------------------------------------------------------------------------
       
    85 // CPbk2CopyContactsStarterCmd::~CPbk2CopyContactsStarterCmd
       
    86 // --------------------------------------------------------------------------
       
    87 //
       
    88 CPbk2CopyContactsStarterCmd::~CPbk2CopyContactsStarterCmd()
       
    89     {
       
    90     Cancel();
       
    91         
       
    92     delete iExecutiveCommand;
       
    93     delete iSelectedContacts;
       
    94     delete iTargetStoreUris;
       
    95     delete iFailedStoreUris;
       
    96     delete iStoreControl;
       
    97     delete iIdleQuery;
       
    98     if ( iUiControl )
       
    99         {
       
   100         iUiControl->UpdateAfterCommandExecution();  
       
   101         }
       
   102     
       
   103     // Close all store handles
       
   104     // This should be done after other members are deleted because they
       
   105     // might use stores that we close here.
       
   106     if ( iStoreList )
       
   107         {
       
   108         const TInt count = iStoreList->Count();
       
   109         for (TInt j = 0; j < count; ++j)
       
   110             {
       
   111             MVPbkContactStore& store = iStoreList->At(j);
       
   112             store.Close(*this);
       
   113             }
       
   114         }
       
   115     Release( iAppServices );
       
   116     }
       
   117 
       
   118 // --------------------------------------------------------------------------
       
   119 // CPbk2CopyContactsStarterCmd::ConstructL
       
   120 // --------------------------------------------------------------------------
       
   121 //
       
   122 inline void CPbk2CopyContactsStarterCmd::ConstructL()
       
   123     {
       
   124     iAppServices = CPbk2ApplicationServices::InstanceL();
       
   125 
       
   126     CActiveScheduler::Add( this );
       
   127     iUiControl->RegisterCommand( this );
       
   128     
       
   129     iFailedStoreUris = CVPbkContactStoreUriArray::NewL();
       
   130 
       
   131     iStoreControl = CPbk2ContactUiControlSubstitute::NewL();
       
   132     
       
   133     iIdleQuery = CIdle::NewL( CActive::EPriorityIdle );
       
   134     }
       
   135 
       
   136 // --------------------------------------------------------------------------
       
   137 // CPbk2CopyContactsStarterCmd::NewL
       
   138 // --------------------------------------------------------------------------
       
   139 //
       
   140 CPbk2CopyContactsStarterCmd* CPbk2CopyContactsStarterCmd::NewL
       
   141         ( MPbk2ContactUiControl& aUiControl )
       
   142     {
       
   143     CPbk2CopyContactsStarterCmd* self = 
       
   144         new ( ELeave ) CPbk2CopyContactsStarterCmd( aUiControl );    
       
   145     CleanupStack::PushL( self );
       
   146     self->ConstructL();
       
   147     CleanupStack::Pop( self );
       
   148     return self;
       
   149     }
       
   150 
       
   151 // --------------------------------------------------------------------------
       
   152 // CPbk2CopyContactsStarterCmd::ExecuteLD
       
   153 // --------------------------------------------------------------------------
       
   154 //
       
   155 void CPbk2CopyContactsStarterCmd::ExecuteLD()
       
   156     {
       
   157     IssueRequest();
       
   158     }
       
   159 
       
   160 // --------------------------------------------------------------------------
       
   161 // CPbk2CopyContactsStarterCmd::AddObserver
       
   162 // --------------------------------------------------------------------------
       
   163 //
       
   164 void CPbk2CopyContactsStarterCmd::AddObserver
       
   165         ( MPbk2CommandObserver& aObserver )
       
   166     {
       
   167     iCommandObserver = &aObserver;
       
   168     }
       
   169 
       
   170 // --------------------------------------------------------------------------
       
   171 // CPbk2CopyContactsStarterCmd::ResetUiControl
       
   172 // --------------------------------------------------------------------------
       
   173 //
       
   174 void CPbk2CopyContactsStarterCmd::ResetUiControl
       
   175         ( MPbk2ContactUiControl& aUiControl )
       
   176     {
       
   177     if ( iUiControl == &aUiControl )
       
   178         {
       
   179         iUiControl = NULL;
       
   180         }
       
   181     }
       
   182 
       
   183 // --------------------------------------------------------------------------
       
   184 // CPbk2CopyContactsStarterCmd::RunL
       
   185 // --------------------------------------------------------------------------
       
   186 //
       
   187 void CPbk2CopyContactsStarterCmd::RunL()
       
   188     {
       
   189     switch ( iState )
       
   190         {
       
   191         case EPbk2CopyStarterQuery:
       
   192             {
       
   193             // Cancel any outstanding before starting new one.
       
   194             iIdleQuery->Cancel();
       
   195             // Request idle query. If the append above failed,
       
   196             // it doesn't matter.
       
   197             iIdleQuery->Start( TCallBack(
       
   198                 ( &CPbk2CopyContactsStarterCmd::IdleQueryCallbackL ),this ) );
       
   199             break;
       
   200             }
       
   201         case EPbk2CopyStarterExecuteCopy:
       
   202             {
       
   203             ExecuteCopyCommandL();
       
   204             break;
       
   205             }
       
   206         case EPbk2CopyOpenStores:
       
   207             {
       
   208             OpenStoresL();
       
   209             break;
       
   210             }
       
   211         case EPbk2CopyStarterInit:
       
   212             {
       
   213             InitCommandL();
       
   214             break;
       
   215             }
       
   216         case EPbk2CopyStarterComplete:  // FALLTHROUGH
       
   217         default:
       
   218             {
       
   219             if ( iUiControl )
       
   220                 {
       
   221                 iUiControl->UpdateAfterCommandExecution();  
       
   222                 }            
       
   223             iCommandObserver->CommandFinished(*this);
       
   224             break;
       
   225             }
       
   226         }
       
   227     }
       
   228 
       
   229 // --------------------------------------------------------------------------
       
   230 // CPbk2CopyContactsStarterCmd::DoCancel
       
   231 // --------------------------------------------------------------------------
       
   232 //
       
   233 void CPbk2CopyContactsStarterCmd::DoCancel()
       
   234     {
       
   235     // Nothing to cancel
       
   236     }
       
   237 
       
   238 // --------------------------------------------------------------------------
       
   239 // CPbk2CopyContactsStarterCmd::RunError
       
   240 // --------------------------------------------------------------------------
       
   241 //
       
   242 TInt CPbk2CopyContactsStarterCmd::RunError( TInt aError )
       
   243     {
       
   244     CCoeEnv::Static()->HandleError( aError );
       
   245     iCommandObserver->CommandFinished( *this );
       
   246     return KErrNone;
       
   247     }
       
   248 
       
   249 
       
   250 // --------------------------------------------------------------------------
       
   251 // CPbk2CopyContactsStarterCmd::CommandFinished
       
   252 // --------------------------------------------------------------------------
       
   253 //
       
   254 void CPbk2CopyContactsStarterCmd::CommandFinished
       
   255         ( const MPbk2Command& /*aCommand*/ )
       
   256     {
       
   257     __ASSERT_DEBUG( iCommandObserver,
       
   258         Panic(EPanicPreCond_Null_Pointer));
       
   259     
       
   260     iCommandObserver->CommandFinished( *this );
       
   261     }
       
   262 
       
   263 // --------------------------------------------------------------------------
       
   264 // CPbk2CopyContactsStarterCmd::StoreReady
       
   265 // --------------------------------------------------------------------------
       
   266 //
       
   267 void CPbk2CopyContactsStarterCmd::StoreReady
       
   268         ( MVPbkContactStore& /*aContactStore*/ )
       
   269     {
       
   270     ++iRespondedStores;
       
   271 
       
   272     // All stores have responded
       
   273     if ( iRespondedStores == iStoreList->Count() )
       
   274         {
       
   275         iState = EPbk2CopyStarterInit;
       
   276         IssueRequest();
       
   277         }
       
   278     }
       
   279 
       
   280 // --------------------------------------------------------------------------
       
   281 // CPbk2CopyContactsStarterCmd::StoreUnavailable
       
   282 // --------------------------------------------------------------------------
       
   283 //
       
   284 void CPbk2CopyContactsStarterCmd::StoreUnavailable
       
   285         ( MVPbkContactStore& aContactStore, TInt /*aReason*/ )
       
   286     {
       
   287     ++iRespondedStores;
       
   288 
       
   289     TRAPD( err, iFailedStoreUris->AppendL
       
   290         ( aContactStore.StoreProperties().Uri() ) );
       
   291 
       
   292     if (err != KErrNone)
       
   293         {
       
   294         CCoeEnv::Static()->HandleError(err);
       
   295         }
       
   296 
       
   297     // All stores have responded
       
   298     if ( iRespondedStores == iStoreList->Count() )
       
   299         {
       
   300         iState = EPbk2CopyStarterInit;
       
   301         IssueRequest();
       
   302         }
       
   303     }
       
   304 
       
   305 // --------------------------------------------------------------------------
       
   306 // CPbk2CopyContactsStarterCmd::HandleStoreEventL
       
   307 // --------------------------------------------------------------------------
       
   308 //
       
   309 void CPbk2CopyContactsStarterCmd::HandleStoreEventL
       
   310         ( MVPbkContactStore& /*aContactStore*/,
       
   311           TVPbkContactStoreEvent /*aStoreEvent*/ )
       
   312     {
       
   313     // Nothing to do
       
   314     }
       
   315 
       
   316 // --------------------------------------------------------------------------
       
   317 // CPbk2CopyContactsStarterCmd::IdleQueryCallbackL
       
   318 // --------------------------------------------------------------------------
       
   319 //
       
   320 TInt CPbk2CopyContactsStarterCmd::IdleQueryCallbackL( TAny* aSelf )
       
   321 	{
       
   322 	CPbk2CopyContactsStarterCmd* self = static_cast<CPbk2CopyContactsStarterCmd*>( aSelf );
       
   323     self->QueryL();
       
   324     return 0;
       
   325 	}
       
   326 
       
   327 // --------------------------------------------------------------------------
       
   328 // CPbk2CopyContactsStarterCmd::QueryL
       
   329 // --------------------------------------------------------------------------
       
   330 //
       
   331 void CPbk2CopyContactsStarterCmd::QueryL()
       
   332     {
       
   333     const TInt targetCount = iTargetStoreUris->Count();
       
   334     switch( targetCount )
       
   335         {
       
   336         case EPbk2NoContacts: // No targets
       
   337             {
       
   338             // There were no writable stores, show note
       
   339             HBufC* noStores = StringLoader::LoadLC( R_QTN_PBCOP_INFO_NO_STORES );
       
   340             CAknInformationNote* dlg = new(ELeave) CAknInformationNote( EFalse );
       
   341             dlg->ExecuteLD( *noStores );
       
   342             CleanupStack::PopAndDestroy( noStores );            
       
   343             
       
   344             // Complete command
       
   345             iState = EPbk2CopyStarterComplete;
       
   346             IssueRequest();
       
   347             break;
       
   348             }
       
   349         default:
       
   350             {
       
   351             const TInt KOneStore = 1;
       
   352             HBufC* title = GetQueryTitleLC();
       
   353             CPbk2StoreListQuery* query = CPbk2StoreListQuery::NewL();
       
   354             
       
   355             // Query modifies iTargetStoreUris array.
       
   356             // After the query there must be only one, the selected, store.
       
   357             if ( query->ExecuteLD(*title, *iTargetStoreUris) == EAknSoftkeyOk &&
       
   358                 iTargetStoreUris->Count() == KOneStore )
       
   359                 {
       
   360                 MVPbkContactStore* store = 
       
   361                     iAppServices->ContactManager().
       
   362                         ContactStoresL().Find( (*iTargetStoreUris )[0] );
       
   363                 iStoreControl->SetSelectedStore(*store);
       
   364                 iState = EPbk2CopyStarterExecuteCopy;
       
   365                 }
       
   366             else
       
   367                 {
       
   368                 iState = EPbk2CopyStarterComplete;
       
   369                 }
       
   370             CleanupStack::PopAndDestroy(title);
       
   371             IssueRequest();
       
   372             break;
       
   373             }
       
   374         }
       
   375     }
       
   376 
       
   377 // --------------------------------------------------------------------------
       
   378 // CPbk2CopyContactsStarterCmd::ExecuteCopyCommandL
       
   379 // --------------------------------------------------------------------------
       
   380 //
       
   381 void CPbk2CopyContactsStarterCmd::ExecuteCopyCommandL()
       
   382     {
       
   383     TInt executiveCommandId = EPbk2CmdCopyContacts;
       
   384     
       
   385     CPbk2UIExtensionManager* extManager = CPbk2UIExtensionManager::InstanceL();
       
   386     extManager->PushL();
       
   387     MPbk2Command* cmd = NULL;
       
   388     if (iUiControl)
       
   389         {
       
   390         iStoreControl->SetParentUiControl(*iUiControl);
       
   391         cmd = extManager->FactoryL()->CreatePbk2CommandForIdL(
       
   392                 executiveCommandId, *iStoreControl);
       
   393         }
       
   394     if (!cmd)
       
   395         {
       
   396         // Create Phonebook2 copy command if extension
       
   397         // didn't offer a command
       
   398         cmd = CPbk2CopyContactsCmd::NewL(*iStoreControl); 
       
   399         }
       
   400     // NULL UI control, because created cmd is topmost command
       
   401     iUiControl = NULL;
       
   402     cmd->AddObserver(*this);
       
   403     cmd->ExecuteLD(); //This not really delete command
       
   404     iExecutiveCommand = cmd;
       
   405     CleanupStack::PopAndDestroy(); // extManager
       
   406     }
       
   407 
       
   408 // --------------------------------------------------------------------------
       
   409 // CPbk2CopyContactsStarterCmd::OpenStoresL
       
   410 // --------------------------------------------------------------------------
       
   411 //
       
   412 void CPbk2CopyContactsStarterCmd::OpenStoresL()
       
   413     {
       
   414     // We need to open all the possible stores
       
   415     CPbk2StoreConfiguration& config = iAppServices->StoreConfiguration();
       
   416     CVPbkContactStoreUriArray* stores =
       
   417         config.SupportedStoreConfigurationL();
       
   418     CleanupStack::PushL(stores);
       
   419 
       
   420     CVPbkContactManager& manager = iAppServices->ContactManager();
       
   421     const TInt storeCount( stores->Count() ); 
       
   422     for (TInt i=0; i< storeCount; ++i)
       
   423         {
       
   424         // Ensure stores loaded
       
   425         manager.LoadContactStoreL((*stores)[i]);
       
   426         }
       
   427 
       
   428     CleanupStack::PopAndDestroy(stores);
       
   429 
       
   430     iStoreList = &manager.ContactStoresL();
       
   431 
       
   432     // Open all stores one by one (in order to be able to close them too)
       
   433     const TInt storeListCount( iStoreList->Count() );
       
   434     for (TInt j=0; j< storeListCount; ++j)
       
   435         {
       
   436         MVPbkContactStore& store = iStoreList->At(j);
       
   437         store.OpenL(*this);
       
   438         }
       
   439     }
       
   440 
       
   441 // --------------------------------------------------------------------------
       
   442 // CPbk2CopyContactsStarterCmd::InitCommandL
       
   443 // --------------------------------------------------------------------------
       
   444 //
       
   445 void CPbk2CopyContactsStarterCmd::InitCommandL()
       
   446     {
       
   447     // Stores failed
       
   448     if ( iFailedStoreUris->Count() == iStoreList->Count() )
       
   449         {
       
   450         HBufC* noStores = StringLoader::LoadLC(R_QTN_PBCOP_INFO_NO_STORES);
       
   451         CAknInformationNote* dlg =
       
   452             new ( ELeave ) CAknInformationNote( EFalse );
       
   453         dlg->ExecuteLD( *noStores );
       
   454         CleanupStack::PopAndDestroy( noStores );
       
   455         }
       
   456 
       
   457     if ( iUiControl )
       
   458         {
       
   459         iTargetStoreUris = 
       
   460             TPbk2CopyContactsUtil::CopyContactTargetStoresLC
       
   461                 ( *iUiControl, iFailedStoreUris );
       
   462 
       
   463         CleanupStack::Pop( iTargetStoreUris );
       
   464         
       
   465         iSelectedContacts = iUiControl->SelectedContactsOrFocusedContactL();
       
   466         
       
   467         if ( iSelectedContacts )
       
   468             {
       
   469             iState = EPbk2CopyStarterQuery;
       
   470             }
       
   471         else
       
   472             {
       
   473             iState = EPbk2CopyStarterComplete;
       
   474             }
       
   475             
       
   476         }
       
   477     else
       
   478         {
       
   479         // control doesnt exists, end copy operation
       
   480         iState = EPbk2CopyStarterComplete;
       
   481         }
       
   482     IssueRequest();
       
   483     }
       
   484 
       
   485 // --------------------------------------------------------------------------
       
   486 // CPbk2CopyContactsStarterCmd::IssueRequest
       
   487 // --------------------------------------------------------------------------
       
   488 //
       
   489 void CPbk2CopyContactsStarterCmd::IssueRequest()
       
   490     {
       
   491     TRequestStatus* status = &iStatus;
       
   492     User::RequestComplete( status, KErrNone );
       
   493     SetActive();
       
   494     }
       
   495 
       
   496 // --------------------------------------------------------------------------
       
   497 // CPbk2CopyContactsStarterCmd::GetQueryTitleLC
       
   498 // --------------------------------------------------------------------------
       
   499 //
       
   500 HBufC* CPbk2CopyContactsStarterCmd::GetQueryTitleLC()
       
   501     {
       
   502     TInt count = EPbk2NoContacts;
       
   503     if (iSelectedContacts)
       
   504         {
       
   505         count = iSelectedContacts->Count();
       
   506         }
       
   507     
       
   508     TInt resid(0);
       
   509     if (count > EPbk2OneContact)
       
   510         {
       
   511         resid = R_QTN_PHOB_QUERY_TITLE_COPY_MANY;
       
   512         }
       
   513     else
       
   514         {
       
   515         resid = R_QTN_PHOB_QUERY_TITLE_COPY;
       
   516         }
       
   517         
       
   518     return StringLoader::LoadLC( resid );        
       
   519     }
       
   520 
       
   521 // --------------------------------------------------------------------------
       
   522 // CPbk2CopyContactsStarterCmd::HandleError
       
   523 // --------------------------------------------------------------------------
       
   524 //
       
   525 void CPbk2CopyContactsStarterCmd::HandleError(TInt aError)
       
   526     {
       
   527     if (aError != KErrNone)
       
   528         {
       
   529         iState = EPbk2CopyStarterComplete;
       
   530         IssueRequest();
       
   531         CCoeEnv::Static()->HandleError(aError);
       
   532         }
       
   533     }
       
   534 
       
   535 //  End of File