phonebookui/Phonebook2/CommonUI/src/CPbk2NavigationBase.cpp
changeset 0 e686773b3f54
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 contact navigator base.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2NavigationBase.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include <MPbk2NavigationObserver.h>
       
    23 #include <MPbk2ContactNavigationLoader.h>
       
    24 
       
    25 // Virtual Phonebook
       
    26 #include <MVPbkContactStore.h>
       
    27 #include <MVPbkContactViewBase.h>
       
    28 #include <MVPbkContactLink.h>
       
    29 
       
    30 // System Includes
       
    31 #include <coemain.h>
       
    32 
       
    33 // Debugging headers
       
    34 #include <Pbk2Debug.h>
       
    35 
       
    36 
       
    37 // --------------------------------------------------------------------------
       
    38 // CPbk2NavigationBase::CPbk2NavigationBase
       
    39 // --------------------------------------------------------------------------
       
    40 //
       
    41 CPbk2NavigationBase::CPbk2NavigationBase
       
    42         ( MPbk2NavigationObserver& aObserver,
       
    43           MPbk2ContactNavigationLoader& aNavigationLoader,
       
    44           TArray<MVPbkContactStore*> aStoreList ):
       
    45             iNavigationObserver( aObserver ),
       
    46             iNavigationLoader( aNavigationLoader ),
       
    47             iStoreList( aStoreList )
       
    48     {
       
    49     }
       
    50 
       
    51 // --------------------------------------------------------------------------
       
    52 // CPbk2NavigationBase::~CPbk2NavigationBase
       
    53 // --------------------------------------------------------------------------
       
    54 //
       
    55 CPbk2NavigationBase::~CPbk2NavigationBase()
       
    56     {
       
    57     RemoveViewObserver();
       
    58     delete iOwnedView;
       
    59     
       
    60     CloseStores();
       
    61     
       
    62     delete iIdleNotifier;
       
    63     delete iDeletedContactInfo;
       
    64     }
       
    65 
       
    66 // --------------------------------------------------------------------------
       
    67 // CPbk2NavigationBase::BaseConstructL
       
    68 // --------------------------------------------------------------------------
       
    69 //
       
    70 void CPbk2NavigationBase::BaseConstructL()
       
    71     {
       
    72     iDeletedContactInfo = new( ELeave ) CDeletedContactInfo( *this );
       
    73     
       
    74     const TInt storeCount = iStoreList.Count();
       
    75     for (TInt i = 0; i < storeCount; ++i)
       
    76         {
       
    77         MVPbkContactStore& store = *iStoreList[i];
       
    78         // Asynchronous call to open a store
       
    79         store.OpenL( *this );
       
    80         }
       
    81     }
       
    82     
       
    83 // --------------------------------------------------------------------------
       
    84 // CPbk2NavigationBase::SetScrollViewL
       
    85 // --------------------------------------------------------------------------
       
    86 //
       
    87 void CPbk2NavigationBase::SetScrollViewL
       
    88         ( MVPbkContactViewBase* aScrollView, TBool aOwnershipTransfered )
       
    89     {
       
    90     // If there was a scroll view set, remove observer and delete it
       
    91     if ( iScrollView )
       
    92         {
       
    93         iScrollView->RemoveObserver( *this );
       
    94         delete iOwnedView;
       
    95         iOwnedView = NULL;
       
    96         }
       
    97 
       
    98     iScrollView = NULL;
       
    99 
       
   100     // Set new scroll view and add observer
       
   101     if ( aScrollView )
       
   102         {
       
   103         iScrollView = aScrollView;
       
   104         if ( aOwnershipTransfered )
       
   105             {
       
   106             iOwnedView = aScrollView;
       
   107             }
       
   108         iScrollView->AddObserverL( *this );
       
   109         }
       
   110     }
       
   111 
       
   112 // --------------------------------------------------------------------------
       
   113 // CPbk2NavigationBase::CurrentContactLC
       
   114 // --------------------------------------------------------------------------
       
   115 //
       
   116 MVPbkContactLink* CPbk2NavigationBase::CurrentContactLC() const
       
   117     {
       
   118     MVPbkContactLink* link = iDeletedContactInfo->FocusedContactOrNullLC();
       
   119     if ( !link )
       
   120         {
       
   121         CleanupStack::Pop( link );
       
   122         link = iNavigationLoader.ContactLinkLC();
       
   123         }
       
   124     return link;
       
   125     }
       
   126 
       
   127 // --------------------------------------------------------------------------
       
   128 // CPbk2NavigationBase::ContactViewReady
       
   129 // --------------------------------------------------------------------------
       
   130 //
       
   131 void CPbk2NavigationBase::ContactViewReady
       
   132         ( MVPbkContactViewBase& /*aView*/ )
       
   133     {
       
   134     // Do nothing
       
   135     }
       
   136 
       
   137 // --------------------------------------------------------------------------
       
   138 // CPbk2NavigationBase::ContactViewUnavailable
       
   139 // --------------------------------------------------------------------------
       
   140 //
       
   141 void CPbk2NavigationBase::ContactViewUnavailable
       
   142         ( MVPbkContactViewBase& /*aView*/ )
       
   143     {
       
   144     // Do nothing
       
   145     }
       
   146 
       
   147 // --------------------------------------------------------------------------
       
   148 // CPbk2NavigationBase::ContactAddedToView
       
   149 // --------------------------------------------------------------------------
       
   150 //
       
   151 void CPbk2NavigationBase::ContactAddedToView
       
   152         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   153           const MVPbkContactLink& /*aContactLink*/ )
       
   154     {
       
   155     // Do nothing
       
   156     }
       
   157 
       
   158 // --------------------------------------------------------------------------
       
   159 // CPbk2VoidNavigation::ContactRemovedFromView
       
   160 // --------------------------------------------------------------------------
       
   161 //
       
   162 void CPbk2NavigationBase::ContactRemovedFromView
       
   163         ( MVPbkContactViewBase& aView, TInt aIndex,
       
   164           const MVPbkContactLink& aContactLink )
       
   165     {
       
   166     TRAPD( res, HandleContactRemovedFromViewL( aView,
       
   167             aIndex, aContactLink ) );
       
   168     if ( res != KErrNone )
       
   169         {
       
   170         CCoeEnv::Static()->HandleError( res );
       
   171         }
       
   172     }
       
   173 
       
   174 // --------------------------------------------------------------------------
       
   175 // CPbk2NavigationBase::ContactViewError
       
   176 // --------------------------------------------------------------------------
       
   177 //
       
   178 void CPbk2NavigationBase::ContactViewError
       
   179         ( MVPbkContactViewBase& /*aView*/, TInt /*aError*/,
       
   180           TBool /*aErrorNotified*/ )
       
   181     {
       
   182     // Do nothing
       
   183     }
       
   184 
       
   185 // --------------------------------------------------------------------------
       
   186 // CPbk2NavigationBase::StoreReady
       
   187 // --------------------------------------------------------------------------
       
   188 //
       
   189 void CPbk2NavigationBase::StoreReady
       
   190         ( MVPbkContactStore& /*aContactStore*/ )
       
   191     {
       
   192     // Do nothing
       
   193     }
       
   194 
       
   195 // --------------------------------------------------------------------------
       
   196 // CPbk2NavigationBase::StoreUnavailable
       
   197 // --------------------------------------------------------------------------
       
   198 //
       
   199 void CPbk2NavigationBase::StoreUnavailable
       
   200         ( MVPbkContactStore& /*aContactStore*/, TInt /*aReason*/ )
       
   201     {
       
   202     // Do nothing
       
   203     }
       
   204 
       
   205 // --------------------------------------------------------------------------
       
   206 // CPbk2NavigationBase::HandleStoreEventL
       
   207 // --------------------------------------------------------------------------
       
   208 //
       
   209 void CPbk2NavigationBase::HandleStoreEventL
       
   210         ( MVPbkContactStore& aContactStore,
       
   211           TVPbkContactStoreEvent aStoreEvent )
       
   212     {
       
   213     iDeletedContactInfo->HandleStoreEventL( aContactStore, aStoreEvent );
       
   214     
       
   215     switch ( aStoreEvent.iEventType )
       
   216         {
       
   217         case TVPbkContactStoreEvent::EContactChanged:
       
   218             {
       
   219             MVPbkContactLink* currentContact =
       
   220                 iNavigationLoader.ContactLinkLC();
       
   221 
       
   222             if ( currentContact && aStoreEvent.iContactLink &&
       
   223                 currentContact->IsSame( *aStoreEvent.iContactLink ) )
       
   224                 {
       
   225                 // Reload current contact
       
   226                 iNavigationLoader.ChangeContactL( *currentContact );
       
   227                 }
       
   228             CleanupStack::PopAndDestroy(); // currentContact
       
   229             break;
       
   230             }
       
   231         case TVPbkContactStoreEvent::EGroupDeleted: // FALLTHROUGH
       
   232         case TVPbkContactStoreEvent::EContactDeleted:
       
   233             {
       
   234             // Start the call back after we have got event from both
       
   235             // store and view. But if we do not have iScrollView then only
       
   236             // store event is enough.
       
   237             if ( iDeletedContactInfo->DeletedAndRemoved() || !iScrollView )
       
   238                 {
       
   239                 StartIdleCallBackL();
       
   240                 }
       
   241             break;
       
   242             }
       
   243         default:
       
   244             {
       
   245             // Do nothing
       
   246             break;
       
   247             }
       
   248         }
       
   249     }
       
   250 
       
   251 // --------------------------------------------------------------------------
       
   252 // CPbk2NavigationBase::HandleContactRemovedFromViewL
       
   253 // --------------------------------------------------------------------------
       
   254 //
       
   255 void CPbk2NavigationBase::HandleContactRemovedFromViewL( 
       
   256         MVPbkContactViewBase& aView,
       
   257         TInt aIndex,
       
   258         const MVPbkContactLink& aContactLink )
       
   259     {
       
   260     iDeletedContactInfo->HandleContactRemovedFromViewL( aView,
       
   261             aIndex, aContactLink );
       
   262     
       
   263     if ( iDeletedContactInfo->DeletedAndRemoved() )
       
   264         {
       
   265         StartIdleCallBackL();
       
   266         }
       
   267     }
       
   268 
       
   269 // --------------------------------------------------------------------------
       
   270 // CPbk2NavigationBase::CloseStores
       
   271 // --------------------------------------------------------------------------
       
   272 //        
       
   273 void CPbk2NavigationBase::CloseStores()
       
   274     {
       
   275     for ( TInt i = 0; i < iStoreList.Count(); ++i )
       
   276         {
       
   277         MVPbkContactStore& store = *iStoreList[i];
       
   278         store.Close( *this );
       
   279         }
       
   280     }
       
   281 
       
   282 // --------------------------------------------------------------------------
       
   283 // CPbk2NavigationBase::RemoveViewObserver
       
   284 // --------------------------------------------------------------------------
       
   285 //        
       
   286 void CPbk2NavigationBase::RemoveViewObserver()
       
   287     {
       
   288     if ( iScrollView )
       
   289         {
       
   290         iScrollView->RemoveObserver( *this );
       
   291         }
       
   292     }
       
   293 
       
   294 // --------------------------------------------------------------------------
       
   295 // CPbk2NavigationBase::StartIdleCallBackL
       
   296 // --------------------------------------------------------------------------
       
   297 //    
       
   298 void CPbk2NavigationBase::StartIdleCallBackL()
       
   299     {
       
   300     // The viewed contact was deleted, signal the
       
   301     // observer asynchronously that the contact was deleted
       
   302     if ( !iIdleNotifier )
       
   303         {
       
   304         iIdleNotifier = CIdle::NewL( CActive::EPriorityStandard );
       
   305         }
       
   306     else
       
   307         {
       
   308         iIdleNotifier->Cancel();
       
   309         }
       
   310     iIdleNotifier->Start
       
   311         ( TCallBack( &IdleNotifierCallBack, this ) );
       
   312     }
       
   313     
       
   314 // --------------------------------------------------------------------------
       
   315 // CPbk2NavigationBase::IdleNotifierCallBack
       
   316 // --------------------------------------------------------------------------
       
   317 //
       
   318 TInt CPbk2NavigationBase::IdleNotifierCallBack( TAny* aSelf )
       
   319     {
       
   320     static_cast<CPbk2NavigationBase*>( aSelf )->HandleIdleNotify();
       
   321     return EFalse;
       
   322     }
       
   323 
       
   324 // --------------------------------------------------------------------------
       
   325 // CPbk2NavigationBase::HandleIdleNotify
       
   326 // --------------------------------------------------------------------------
       
   327 //
       
   328 void CPbk2NavigationBase::HandleIdleNotify()
       
   329     {
       
   330     iNavigationObserver.HandleNavigationEvent
       
   331         ( MPbk2NavigationObserver::EContactDeleted );
       
   332     }
       
   333 
       
   334 // --------------------------------------------------------------------------
       
   335 // CPbk2NavigationBase::CDeletedContactInfo::CDeletedContactInfo
       
   336 // --------------------------------------------------------------------------
       
   337 //
       
   338 CPbk2NavigationBase::CDeletedContactInfo::CDeletedContactInfo( 
       
   339         CPbk2NavigationBase& aNavigationBase )
       
   340         :   iNavigationBase ( aNavigationBase ),
       
   341             iRemovedContactIndex( KErrNotFound )
       
   342     {
       
   343     }
       
   344 
       
   345 // --------------------------------------------------------------------------
       
   346 // CPbk2NavigationBase::CDeletedContactInfo::~CDeletedContactInfo
       
   347 // --------------------------------------------------------------------------
       
   348 //     
       
   349 CPbk2NavigationBase::CDeletedContactInfo::~CDeletedContactInfo()
       
   350     {
       
   351     delete iDeletedContact;
       
   352     delete iRemovedContact;
       
   353     }
       
   354 
       
   355 // --------------------------------------------------------------------------
       
   356 // CPbk2NavigationBase::CDeletedContactInfo::HandleContactRemovedFromViewL
       
   357 // --------------------------------------------------------------------------
       
   358 //             
       
   359 void CPbk2NavigationBase::CDeletedContactInfo::HandleContactRemovedFromViewL(
       
   360         MVPbkContactViewBase& /*aView*/,
       
   361         TInt aIndex,
       
   362         const MVPbkContactLink& aContactLink )
       
   363     {
       
   364     /// Handle delete event but not if we already have the information
       
   365     /// that the current contact was deleted.
       
   366     if ( !DeletedAndRemoved() )
       
   367         {
       
   368         MVPbkContactLink* curContact = 
       
   369                 iNavigationBase.NavigationLoader().ContactLinkLC();
       
   370         
       
   371         // Save the removed contact if it's the current contact
       
   372         if ( curContact && curContact->IsSame( aContactLink ) )
       
   373             {
       
   374             delete iRemovedContact;
       
   375             iRemovedContact = curContact;
       
   376             CleanupStack::Pop(); // curContact
       
   377             iRemovedContactIndex = aIndex;
       
   378             }
       
   379         else
       
   380             {
       
   381             CleanupStack::PopAndDestroy(); // curContact
       
   382             }
       
   383         }
       
   384     }
       
   385 
       
   386 // --------------------------------------------------------------------------
       
   387 // CPbk2NavigationBase::CDeletedContactInfo::HandleStoreEventL
       
   388 // --------------------------------------------------------------------------
       
   389 //                     
       
   390 void CPbk2NavigationBase::CDeletedContactInfo::HandleStoreEventL(
       
   391         MVPbkContactStore& /*aContactStore*/,
       
   392         TVPbkContactStoreEvent aStoreEvent )
       
   393     {
       
   394     /// Handle delete event but not if we already have the information
       
   395     /// that the current contact was deleted.
       
   396     if ( !DeletedAndRemoved() &&
       
   397          (aStoreEvent.iEventType == TVPbkContactStoreEvent::EContactDeleted
       
   398          || aStoreEvent.iEventType == TVPbkContactStoreEvent::EGroupDeleted))
       
   399         {
       
   400         // Save the deleted contact if it's the current contact.
       
   401         MVPbkContactLink* curContact = 
       
   402             iNavigationBase.NavigationLoader().ContactLinkLC();
       
   403         if ( curContact && aStoreEvent.iContactLink && 
       
   404              curContact->IsSame( *aStoreEvent.iContactLink ) )
       
   405             {
       
   406             delete iDeletedContact;
       
   407             iDeletedContact = curContact;
       
   408             CleanupStack::Pop(); // curContact
       
   409             }
       
   410         else
       
   411             {
       
   412             CleanupStack::PopAndDestroy(); // curContact
       
   413             }
       
   414         }
       
   415     }
       
   416 
       
   417 // --------------------------------------------------------------------------
       
   418 // CPbk2NavigationBase::CDeletedContactInfo::DeletedAndRemoved
       
   419 // --------------------------------------------------------------------------
       
   420 //                             
       
   421 TBool CPbk2NavigationBase::CDeletedContactInfo::DeletedAndRemoved() const
       
   422     {
       
   423     TBool res = EFalse;
       
   424     if ( iDeletedContact && iRemovedContact && 
       
   425          iRemovedContact->IsSame( *iDeletedContact ) )
       
   426         {
       
   427         res = ETrue;
       
   428         }
       
   429     return res;
       
   430     }
       
   431 
       
   432 // --------------------------------------------------------------------------
       
   433 // CPbk2NavigationBase::CDeletedContactInfo::FocusedContactOrNullLC
       
   434 // --------------------------------------------------------------------------
       
   435 //                             
       
   436 MVPbkContactLink* 
       
   437         CPbk2NavigationBase::CDeletedContactInfo::FocusedContactOrNullLC()
       
   438     {
       
   439     MVPbkContactLink* res = NULL;
       
   440     MVPbkContactViewBase* view = iNavigationBase.ScrollView();
       
   441     
       
   442     if ( DeletedAndRemoved() && view )
       
   443         {
       
   444         TInt index = iRemovedContactIndex;
       
   445         if ( index >= 0 )
       
   446             {
       
   447             const TInt lastIndex = view->ContactCountL() - 1;
       
   448             if ( index > lastIndex )
       
   449                 {
       
   450                 index = lastIndex;
       
   451                 }
       
   452             
       
   453             // Returns a link to the contact that is in the same position
       
   454             // as the removed one or in the last position.   
       
   455             if ( index >= 0 )
       
   456                 {
       
   457                 res = view->CreateLinkLC( index );
       
   458                 }
       
   459             }
       
   460         }
       
   461     
       
   462     if ( !res )
       
   463         {
       
   464         // LC -semantics, push also NULL to cleanup stack
       
   465         CleanupStack::PushL( res );
       
   466         }
       
   467         
       
   468     return res;
       
   469     }
       
   470 // End of File