phonebookui/Phonebook2/ServerApplication/src/CPbk2ContactFetchPhase.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
child 85 38bb213f60ba
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     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 server app contact fetch phase.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2ContactFetchPhase.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "MPbk2ServicePhaseObserver.h"
       
    23 #include "CPbk2ServerAppAppUi.h"
       
    24 #include "CPbk2ServerAppStoreManager.h"
       
    25 #include <CPbk2FetchDlg.h>
       
    26 #include <MPbk2ApplicationServices.h>
       
    27 #include <CPbk2ContactViewBuilder.h>
       
    28 
       
    29 // Virtual Phonebook
       
    30 #include <MVPbkContactLink.h>
       
    31 #include <MVPbkContactOperationBase.h>
       
    32 #include <MVPbkContactViewBase.h>
       
    33 #include <CVPbkContactManager.h>
       
    34 #include <MVPbkStoreContactField.h>
       
    35 #include <MVPbkFieldType.h>
       
    36 #include <CVPbkFieldTypeSelector.h>
       
    37 #include <MVPbkStoreContact.h>
       
    38 #include <MVPbkStoreContactFieldCollection.h>
       
    39 
       
    40 /// Unnamed namespace for local definitions
       
    41 namespace {
       
    42 
       
    43 const TInt KFirstElement = 0;
       
    44 
       
    45 } /// namespace
       
    46 
       
    47 // --------------------------------------------------------------------------
       
    48 // CPbk2ContactFetchPhase::CPbk2ContactFetchPhase
       
    49 // --------------------------------------------------------------------------
       
    50 //
       
    51 CPbk2ContactFetchPhase::CPbk2ContactFetchPhase
       
    52         ( MPbk2ServicePhaseObserver& aObserver,
       
    53           CPbk2ServerAppStoreManager& aStoreManager,
       
    54           CPbk2FetchDlg::TParams aFetchParams,
       
    55           TBool aFetchOkToExit,
       
    56           MPbk2FetchDlgObserver::TPbk2FetchAcceptSelection
       
    57             aFetchAcceptPolicy,
       
    58           CVPbkFieldTypeSelector* aViewFilter,
       
    59           TBool aUsingDefaultConfig,
       
    60           const CVPbkContactStoreUriArray* aStoreUris ) :
       
    61                 iObserver( aObserver ),
       
    62                 iServerAppStoreManager( aStoreManager ),
       
    63                 iFetchParams( aFetchParams ),
       
    64                 iFetchOkToExit( aFetchOkToExit ),
       
    65                 iFetchAcceptPolicy( aFetchAcceptPolicy ),
       
    66                 iViewFilter( aViewFilter ),
       
    67                 iUsingDefaultConfig( aUsingDefaultConfig ),
       
    68                 iStoreUris( aStoreUris )
       
    69     {
       
    70     }
       
    71 
       
    72 // --------------------------------------------------------------------------
       
    73 // CPbk2ContactFetchPhase::~CPbk2ContactFetchPhase
       
    74 // --------------------------------------------------------------------------
       
    75 //
       
    76 CPbk2ContactFetchPhase::~CPbk2ContactFetchPhase()
       
    77     {
       
    78     if ( iFetchDialogEliminator )
       
    79         {
       
    80         iFetchDialogEliminator->ForceExit();
       
    81         }
       
    82     delete iFetchNamesListView;
       
    83     delete iFetchGroupsListView;
       
    84     delete iFetchResults;
       
    85     delete iContactLinks;
       
    86     delete iRetrieveOperation;
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // CPbk2ContactFetchPhase::NewL
       
    91 // --------------------------------------------------------------------------
       
    92 //
       
    93 CPbk2ContactFetchPhase* CPbk2ContactFetchPhase::NewL
       
    94         ( MPbk2ServicePhaseObserver& aObserver,
       
    95           CPbk2ServerAppStoreManager& aStoreManager,
       
    96           CPbk2FetchDlg::TParams aFetchParams,
       
    97           TBool aFetchOkToExit,
       
    98           MPbk2FetchDlgObserver::TPbk2FetchAcceptSelection
       
    99             aFetchAcceptPolicy,
       
   100           CVPbkFieldTypeSelector* aViewFilter,
       
   101           TBool aUsingDefaultConfig,
       
   102           const CVPbkContactStoreUriArray* aStoreUris )
       
   103     {
       
   104     CPbk2ContactFetchPhase* self =
       
   105         new ( ELeave ) CPbk2ContactFetchPhase
       
   106             ( aObserver, aStoreManager, aFetchParams, aFetchOkToExit,
       
   107               aFetchAcceptPolicy, aViewFilter, aUsingDefaultConfig,
       
   108               aStoreUris );
       
   109     return self;
       
   110     }
       
   111 
       
   112 // --------------------------------------------------------------------------
       
   113 // CPbk2ContactFetchPhase::LaunchServicePhaseL
       
   114 // --------------------------------------------------------------------------
       
   115 //
       
   116 void CPbk2ContactFetchPhase::LaunchServicePhaseL()
       
   117     {
       
   118     CPbk2ServerAppAppUi* appUi = static_cast<CPbk2ServerAppAppUi*>
       
   119             ( CEikonEnv::Static()->EikAppUi() );
       
   120 
       
   121     TUint32 flags = EVPbkContactViewFlagsNone;
       
   122     // Because the filter needs to have the contact store open,
       
   123     // we can't build the view earlier
       
   124     delete iFetchNamesListView;
       
   125     iFetchNamesListView = NULL;
       
   126     iFetchNamesListView = iServerAppStoreManager.BuildFetchViewL
       
   127         ( EVPbkContactsView, *iStoreUris, iViewFilter,
       
   128           appUi->ApplicationServices().StoreProperties(),
       
   129           appUi->ApplicationServices().SortOrderManager(),
       
   130           flags );
       
   131 
       
   132     flags = EVPbkExcludeEmptyGroups;
       
   133     delete iFetchGroupsListView;
       
   134     iFetchGroupsListView = NULL;
       
   135     iFetchGroupsListView = iServerAppStoreManager.BuildFetchViewL
       
   136         ( EVPbkGroupsView, *iStoreUris, iViewFilter,
       
   137           appUi->ApplicationServices().StoreProperties(),
       
   138           appUi->ApplicationServices().SortOrderManager(),
       
   139           flags );
       
   140 
       
   141     // Launch the fetch dialog
       
   142     iFetchParams.iExitCallback = this;
       
   143     iFetchParams.iNamesListView = iFetchNamesListView;
       
   144     iFetchParams.iGroupsListView = iFetchGroupsListView;
       
   145     iFetchDlg = CPbk2FetchDlg::NewL( iFetchParams, *this );
       
   146     iFetchDialogEliminator = iFetchDlg;
       
   147     iFetchDialogEliminator->ResetWhenDestroyed( &iFetchDialogEliminator );
       
   148     iFetchDlg->ExecuteLD();
       
   149     }
       
   150 
       
   151 // --------------------------------------------------------------------------
       
   152 // CPbk2ContactFetchPhase::CancelServicePhase
       
   153 // --------------------------------------------------------------------------
       
   154 //
       
   155 void CPbk2ContactFetchPhase::CancelServicePhase()
       
   156     {
       
   157     if ( iFetchDialogEliminator )
       
   158         {
       
   159         iFetchDialogEliminator->ForceExit();
       
   160         }
       
   161     }
       
   162 
       
   163 // --------------------------------------------------------------------------
       
   164 // CPbk2ContactFetchPhase::RequestCancelL
       
   165 // --------------------------------------------------------------------------
       
   166 //
       
   167 void CPbk2ContactFetchPhase::RequestCancelL( TInt aExitCommandId )
       
   168     {
       
   169     if ( iFetchDialogEliminator )
       
   170         {
       
   171         iFetchDialogEliminator->RequestExitL( aExitCommandId );
       
   172         }
       
   173     }
       
   174 
       
   175 // --------------------------------------------------------------------------
       
   176 // CPbk2ContactFetchPhase::AcceptDelayed
       
   177 // --------------------------------------------------------------------------
       
   178 //
       
   179 void CPbk2ContactFetchPhase::AcceptDelayedL
       
   180         ( const TDesC8& aContactLinkBuffer )
       
   181     {
       
   182     if ( iFetchDlg )
       
   183         {
       
   184         iFetchDlg->AcceptDelayedFetchL( aContactLinkBuffer );
       
   185         }
       
   186     }
       
   187 
       
   188 // --------------------------------------------------------------------------
       
   189 // CPbk2ContactFetchPhase::DenyDelayed
       
   190 // --------------------------------------------------------------------------
       
   191 //
       
   192 void CPbk2ContactFetchPhase::DenyDelayedL
       
   193         ( const TDesC8& aContactLinkBuffer )
       
   194     {
       
   195     if ( iFetchDlg )
       
   196         {
       
   197         iFetchDlg->DenyDelayedFetchL( aContactLinkBuffer );
       
   198         }
       
   199     }
       
   200 
       
   201 // --------------------------------------------------------------------------
       
   202 // CPbk2ContactFetchPhase::Results
       
   203 // --------------------------------------------------------------------------
       
   204 //
       
   205 MVPbkContactLinkArray* CPbk2ContactFetchPhase::Results() const
       
   206     {
       
   207     return iFetchResults;
       
   208     }
       
   209 
       
   210 // --------------------------------------------------------------------------
       
   211 // CPbk2ContactFetchPhase::ExtraResultData
       
   212 // --------------------------------------------------------------------------
       
   213 //
       
   214 TInt CPbk2ContactFetchPhase::ExtraResultData() const
       
   215     {
       
   216     return KErrNotSupported;
       
   217     }
       
   218 
       
   219 // --------------------------------------------------------------------------
       
   220 // CPbk2ContactFetchPhase::TakeStoreContact
       
   221 // --------------------------------------------------------------------------
       
   222 //
       
   223 MVPbkStoreContact* CPbk2ContactFetchPhase::TakeStoreContact()
       
   224     {
       
   225     // No store contact concept in fetch
       
   226     return NULL;
       
   227     }
       
   228 
       
   229 // --------------------------------------------------------------------------
       
   230 // CPbk2ContactFetchPhase::FieldContent
       
   231 // --------------------------------------------------------------------------
       
   232 //
       
   233 HBufC* CPbk2ContactFetchPhase::FieldContent() const
       
   234     {
       
   235     return NULL;
       
   236     }
       
   237 
       
   238 // --------------------------------------------------------------------------
       
   239 // CPbk2ContactFetchPhase::Pbk2AcceptFetchSelectionL
       
   240 // --------------------------------------------------------------------------
       
   241 //
       
   242 MPbk2FetchDlgObserver::TPbk2FetchAcceptSelection
       
   243     CPbk2ContactFetchPhase::AcceptFetchSelectionL
       
   244         ( TInt aNumMarkedEntries, MVPbkContactLink& aLastSelection )
       
   245     {
       
   246     MPbk2FetchDlgObserver::TPbk2FetchAcceptSelection result =
       
   247         iFetchAcceptPolicy;
       
   248 
       
   249     if ( result == MPbk2FetchDlgObserver::EFetchDelayed )
       
   250         {
       
   251         HBufC8* linkData = aLastSelection.PackLC();
       
   252         TPckg<TInt> buffer(aNumMarkedEntries);
       
   253         iObserver.PhaseAccept( *this, buffer, *linkData );
       
   254         CleanupStack::PopAndDestroy(); // linkData
       
   255         }
       
   256 
       
   257     return result;
       
   258     }
       
   259 
       
   260 // --------------------------------------------------------------------------
       
   261 // CPbk2ContactFetchPhase::FetchCompletedL
       
   262 // --------------------------------------------------------------------------
       
   263 //
       
   264 void CPbk2ContactFetchPhase::FetchCompletedL
       
   265         ( MVPbkContactLinkArray* aMarkedEntries )
       
   266     {
       
   267     delete iFetchResults;
       
   268     iFetchResults = NULL;
       
   269     iFetchResults = CVPbkContactLinkArray::NewL();
       
   270 
       
   271     CVPbkContactLinkArray* linkArray = iFetchResults;
       
   272 
       
   273     // View filter can not guarentee that returned contacts match the filter.
       
   274     // If user selects a group all the contacts of the group are returned
       
   275     // by fetch and we must manually filter them here.
       
   276     if ( iViewFilter )
       
   277         {
       
   278         delete iContactLinks;
       
   279         iContactLinks = NULL;
       
   280         iContactLinks = CVPbkContactLinkArray::NewL();
       
   281         linkArray = iContactLinks;
       
   282         }
       
   283 
       
   284     const TInt count = aMarkedEntries->Count();
       
   285     for (TInt i = 0; i < count; ++i)
       
   286         {
       
   287         linkArray->AppendL( aMarkedEntries->At(i).CloneLC() );
       
   288         CleanupStack::Pop(); // aMarkedEntries->At(i).CloneLC()
       
   289         }
       
   290 
       
   291     if ( iViewFilter )
       
   292         {
       
   293         // Go through contacts one by one
       
   294         RetrieveContactL();
       
   295         }
       
   296     else
       
   297         {
       
   298         // Finished
       
   299         iObserver.NextPhase( *this );
       
   300         }
       
   301     }
       
   302 
       
   303 // --------------------------------------------------------------------------
       
   304 // CPbk2ContactFetchPhase::FetchCanceled
       
   305 // --------------------------------------------------------------------------
       
   306 //
       
   307 void CPbk2ContactFetchPhase::FetchCanceled()
       
   308     {
       
   309     delete iFetchResults;
       
   310     iFetchResults = NULL;
       
   311     delete iRetrieveOperation;
       
   312     iRetrieveOperation = NULL;
       
   313 
       
   314     iObserver.PhaseCanceled( *this );
       
   315     }
       
   316 
       
   317 // --------------------------------------------------------------------------
       
   318 // CPbk2ContactFetchPhase::FetchAborted
       
   319 // --------------------------------------------------------------------------
       
   320 //
       
   321 void CPbk2ContactFetchPhase::FetchAborted()
       
   322     {
       
   323     delete iFetchResults;
       
   324     iFetchResults = NULL;
       
   325 
       
   326     iObserver.PhaseAborted( *this );
       
   327     }
       
   328 
       
   329 // --------------------------------------------------------------------------
       
   330 // CPbk2ContactFetchPhase::FetchOkToExit
       
   331 // --------------------------------------------------------------------------
       
   332 //
       
   333 TInt CPbk2ContactFetchPhase::FetchOkToExit()
       
   334     {
       
   335     return iFetchOkToExit;
       
   336     }
       
   337 
       
   338 // --------------------------------------------------------------------------
       
   339 // CPbk2ContactFetchPhase::OkToExitL
       
   340 // --------------------------------------------------------------------------
       
   341 //
       
   342 TBool CPbk2ContactFetchPhase::OkToExitL( TInt aCommandId )
       
   343     {
       
   344     return iObserver.PhaseOkToExit( *this, aCommandId );
       
   345     }
       
   346 
       
   347 // --------------------------------------------------------------------------
       
   348 // CPbk2ContactFetchPhase::VPbkSingleContactOperationComplete
       
   349 // --------------------------------------------------------------------------
       
   350 //
       
   351 void CPbk2ContactFetchPhase::VPbkSingleContactOperationComplete
       
   352         ( MVPbkContactOperationBase& /*aOperation*/,
       
   353           MVPbkStoreContact* aContact )
       
   354     {
       
   355     TRAPD( err, HandleContactOperationCompleteL( aContact ) );
       
   356     if ( err != KErrNone )
       
   357         {
       
   358         iObserver.PhaseError( *this, err );
       
   359         }
       
   360     }
       
   361 
       
   362 // --------------------------------------------------------------------------
       
   363 // CPbk2ContactFetchPhase::VPbkSingleContactOperationFailed
       
   364 // --------------------------------------------------------------------------
       
   365 //
       
   366 void CPbk2ContactFetchPhase::VPbkSingleContactOperationFailed
       
   367         ( MVPbkContactOperationBase& /*aOperation*/, TInt aError )
       
   368     {
       
   369     iObserver.PhaseError( *this, aError );
       
   370     }
       
   371 
       
   372 // --------------------------------------------------------------------------
       
   373 // CPbk2ContactFetchPhase::HandleContactOperationCompleteL
       
   374 // --------------------------------------------------------------------------
       
   375 //
       
   376 void CPbk2ContactFetchPhase::HandleContactOperationCompleteL
       
   377         ( MVPbkStoreContact* aContact )
       
   378     {
       
   379     // Verify current contact
       
   380     VerifyContactL( aContact );
       
   381     
       
   382     // Retrieve next contact
       
   383     RetrieveContactL();
       
   384     }
       
   385 
       
   386 // --------------------------------------------------------------------------
       
   387 // CPbk2ContactFetchPhase::RetrieveContactL
       
   388 // --------------------------------------------------------------------------
       
   389 //
       
   390 void CPbk2ContactFetchPhase::RetrieveContactL()
       
   391     {
       
   392     CPbk2ServerAppAppUi* appUi = static_cast<CPbk2ServerAppAppUi*>
       
   393             ( CEikonEnv::Static()->EikAppUi() );
       
   394 
       
   395     // Fetch one contact at a time if service cancellation is not
       
   396     // commanded.
       
   397     if ( iContactLinks && iContactLinks->Count() > 0 )
       
   398         {
       
   399         delete iRetrieveOperation;
       
   400         iRetrieveOperation = NULL;
       
   401         iRetrieveOperation = appUi->ApplicationServices().ContactManager().
       
   402             RetrieveContactL( iContactLinks->At( KFirstElement ), *this );
       
   403         iContactLinks->Delete( KFirstElement );
       
   404         }
       
   405     else
       
   406         {
       
   407         iObserver.NextPhase( *this );
       
   408         }
       
   409     }
       
   410 
       
   411 // --------------------------------------------------------------------------
       
   412 // CPbk2ContactFetchPhase::VerifyContactL
       
   413 // --------------------------------------------------------------------------
       
   414 //
       
   415 void CPbk2ContactFetchPhase::VerifyContactL( MVPbkStoreContact* aContact )
       
   416     {
       
   417     CleanupDeletePushL( aContact );
       
   418 
       
   419     MVPbkStoreContactFieldCollection& fields = aContact->Fields();
       
   420     TInt fieldCount = fields.FieldCount();
       
   421 
       
   422     for ( TInt i = 0; i < fieldCount; ++i )
       
   423         {
       
   424         const MVPbkStoreContactField& field = fields.FieldAt( i );
       
   425         const MVPbkFieldType* fieldType = field.BestMatchingFieldType();
       
   426         if ( fieldType && iViewFilter->IsFieldTypeIncluded( *fieldType ) )
       
   427             {
       
   428             MVPbkContactLink* link = aContact->CreateLinkLC();
       
   429             CleanupStack::Pop(); // link
       
   430             iFetchResults->AppendL( link );
       
   431             break;
       
   432             }
       
   433         }
       
   434 
       
   435     CleanupStack::PopAndDestroy(); // aContact
       
   436     }
       
   437 
       
   438 // End of File