phonebookengines/VirtualPhonebook/inc/VPbkSendEventUtility.h
changeset 0 e686773b3f54
child 23 5586b4d2ec3e
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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef VPBKENGUTILS_VPBKSENDEVENTUTILITY_H
       
    20 #define VPBKENGUTILS_VPBKSENDEVENTUTILITY_H
       
    21 
       
    22 
       
    23 // INCLUDES
       
    24 #include <e32cmn.h>
       
    25 
       
    26 // FORWARD DECLARATIONS
       
    27 
       
    28 namespace VPbkEng {
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // SendEventToObservers
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 template<typename RefParam1, typename Observer, typename NotifyFunc>
       
    35 void SendEventToObservers( RefParam1& aRefParam1,
       
    36         RPointerArray<Observer>& aObservers, NotifyFunc aNotifyFunc )
       
    37     {
       
    38     Observer* obs = NULL;
       
    39     for ( TInt i = aObservers.Count() - 1; i >= 0; --i )
       
    40         {
       
    41         if ( i < aObservers.Count() && obs != aObservers[i] )
       
    42             {
       
    43             obs = aObservers[i];
       
    44             (obs->*aNotifyFunc)( aRefParam1 );
       
    45             }
       
    46         }
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // SendEventToObservers
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 template<typename RefParam1, typename ValParam2, typename RefParam3,
       
    54     typename Observer, typename NotifyFunc>
       
    55 void SendEventToObservers( RefParam1& aRefParam1, ValParam2 aValParam2,
       
    56         RefParam3& aRefParam3, RPointerArray<Observer>& aObservers,
       
    57         NotifyFunc aNotifyFunc )
       
    58     {
       
    59     Observer* obs = NULL;
       
    60     for ( TInt i = aObservers.Count() - 1; i >= 0; --i )
       
    61         {
       
    62         if ( i < aObservers.Count() && obs != aObservers[i] )
       
    63             {
       
    64             obs = aObservers[i];
       
    65             (obs->*aNotifyFunc)( aRefParam1, aValParam2,
       
    66                     aRefParam3 );
       
    67             }
       
    68         }
       
    69     }
       
    70 
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CopyPointerArrayL
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 template<typename ObjectTypeName>
       
    77 void CopyPointerArrayL( RPointerArray<ObjectTypeName>& aSource,
       
    78         RPointerArray<ObjectTypeName>& aTarget )
       
    79     {
       
    80     const TInt count = aSource.Count();
       
    81     for ( TInt i = 0; i < count; ++i )
       
    82         {
       
    83         aTarget.AppendL( aSource[i] );
       
    84         }
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // SendViewEventToObserversL
       
    89 // See and use SendViewEventToObservers if possible
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 template<typename NotifyFunc, typename Observer, typename RefParam1>
       
    93 void SendViewEventToObserversL( RefParam1& aRefParam1,
       
    94         RPointerArray<Observer>& aObservers, NotifyFunc aNotifyFunc )
       
    95     {
       
    96     RPointerArray<Observer> tmp;
       
    97     CleanupClosePushL( tmp );
       
    98 
       
    99     CopyPointerArrayL( aObservers, tmp );
       
   100 
       
   101     const TInt count = tmp.Count();
       
   102     for ( TInt i = count - 1; i >= 0; --i )
       
   103         {
       
   104         if ( aObservers.Find( tmp[i] ) != KErrNotFound )
       
   105             {
       
   106             (tmp[i]->*aNotifyFunc)( aRefParam1 );
       
   107             }
       
   108         }
       
   109 
       
   110     CleanupStack::PopAndDestroy(); // tmp
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // SendViewEventToObserversL
       
   115 // See and use SendViewEventToObservers if possible
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 
       
   119 template<typename NotifyFunc, typename Observer, typename RefParam1,
       
   120     typename ValParam2, typename RefParam3>
       
   121 void SendViewEventToObserversL( RefParam1& aRefParam1, ValParam2 aValParam2,
       
   122         RefParam3& aRefParam3, RPointerArray<Observer>& aObservers,
       
   123         NotifyFunc aNotifyFunc )
       
   124     {
       
   125     RPointerArray<Observer> tmp;
       
   126     CleanupClosePushL( tmp );
       
   127 
       
   128     CopyPointerArrayL( aObservers, tmp );
       
   129 
       
   130     const TInt count = tmp.Count();
       
   131     for ( TInt i = count - 1; i >= 0; --i )
       
   132         {
       
   133         if ( aObservers.Find( tmp[i] ) != KErrNotFound )
       
   134             {
       
   135             (tmp[i]->*aNotifyFunc)( aRefParam1, aValParam2, aRefParam3 );
       
   136             }
       
   137         }
       
   138 
       
   139     CleanupStack::PopAndDestroy(); // tmp
       
   140     }
       
   141 
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // SendViewEventToObservers
       
   145 //
       
   146 // Usage:
       
   147 // CMyView::HandleNativeViewReady()
       
   148 //     {
       
   149 //     ...
       
   150 //     SendViewEventToObservers( *this, iObservers,
       
   151 //          &MVPbkContactViewObserver::ContactViewReady,
       
   152 //          &MVPbkContactViewObserver::ContactViewError )
       
   153 //     }
       
   154 //
       
   155 // The function slightly bound to MVPbkContactViewBase::ContactViewError for
       
   156 // error handling. If that doesn't suite your needs then use
       
   157 // SendViewEventToObserversL and handle error yourself.
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 template<typename NotifyFunc, typename ErrorFunc, typename Observer,
       
   161     typename RefParam1>
       
   162 void SendViewEventToObservers( RefParam1& aRefParam1,
       
   163         RPointerArray<Observer>& aObservers, NotifyFunc aNotifyFunc,
       
   164         ErrorFunc aErrorFunc )
       
   165     {
       
   166     TRAPD( result,
       
   167         SendViewEventToObserversL( aRefParam1, aObservers, aNotifyFunc ) );
       
   168     if ( result != KErrNone )
       
   169         {
       
   170         TBool errorNotified = EFalse;
       
   171         SendEventToObservers( aRefParam1, result, errorNotified,
       
   172             aObservers, aErrorFunc );
       
   173         }
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // SendViewEventToObservers
       
   178 //
       
   179 // Usage:
       
   180 // CMyView::HandleNativeViewContactAdded(TInt aIndex, TContactId aId)
       
   181 //     {
       
   182 //     ...
       
   183 //     SendViewEventToObservers( *this, aIndex, *iLink, iObservers,
       
   184 //          &MVPbkContactViewObserver::ContactAddedToView,
       
   185 //          &MVPbkContactViewObserver::ContactViewError )
       
   186 //     }
       
   187 //
       
   188 // The function slightly bound to MVPbkContactViewBase::ContactViewError for
       
   189 // error handling. If that doesn't suite your needs then use
       
   190 // SendViewEventToObserversL and handle error yourself.
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 template<typename NotifyFunc, typename ErrorFunc, typename Observer,
       
   194     typename RefParam1, typename ValParam2, typename RefParam3>
       
   195 void SendViewEventToObservers( RefParam1& aRefParam1, ValParam2 aValParam2,
       
   196         RefParam3& aRefParam3, RPointerArray<Observer>& aObservers,
       
   197         NotifyFunc aNotifyFunc, ErrorFunc aErrorFunc )
       
   198     {
       
   199     TRAPD( result, SendViewEventToObserversL( aRefParam1, aValParam2,
       
   200             aRefParam3, aObservers, aNotifyFunc ) );
       
   201     if ( result != KErrNone )
       
   202         {
       
   203         TBool errorNotified = EFalse;
       
   204         SendEventToObservers( aRefParam1, result, errorNotified,
       
   205             aObservers, aErrorFunc );
       
   206         }
       
   207     }
       
   208 } // namespace VPbkEng
       
   209 
       
   210 
       
   211 #endif  // VPBKENGUTILS_VPBKSENDEVENTUTILITY_H
       
   212 // End of file
       
   213 
       
   214