vtengines/videoteleng/Src/Base/CVtEngEventManager.cpp
changeset 0 ed9695c8bcbe
child 20 b95d12697049
equal deleted inserted replaced
-1:000000000000 0:ed9695c8bcbe
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Event manager implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "CVtEngEventManager.h"
       
    22 #include    "MVtEngEventObserver.h"
       
    23 #include    "VtEngUtils.h"
       
    24 #include    "VtEngEvents.h"
       
    25 #include    "VtEngPanic.h"
       
    26 #include    <cvtlogger.h>
       
    27 
       
    28 // CONSTANTS
       
    29 const TInt KMaxEventsInQueue = 8;
       
    30 
       
    31 // max amount of simultanous event observers
       
    32 const TInt KMaxVtEventObservers = 2;
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CVtEngEventManager::CVtEngEventManager
       
    38 // C++ constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CVtEngEventManager::CVtEngEventManager() : CActive( CActive::EPriorityHigh ),
       
    43     iObservers( KMaxVtEventObservers ) 
       
    44     {
       
    45     CActiveScheduler::Add( this );
       
    46     }
       
    47 
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CVtEngEventManager::NewL
       
    51 // Two-phased constructor.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CVtEngEventManager* CVtEngEventManager::NewL(
       
    55     MVtEngEventObserver& aEventObserver )
       
    56     {
       
    57     CVtEngEventManager* self = 
       
    58         new ( ELeave ) CVtEngEventManager();        
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL( aEventObserver );
       
    61     CleanupStack::Pop(); // self 
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CVtEngEventManager::ConstructL
       
    67 // 2nd phase constructor.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 void CVtEngEventManager::ConstructL( MVtEngEventObserver& aEventObserver )
       
    71 	{
       
    72 	iEventQueue = new ( ELeave ) CArrayFixFlat< TInt >( KMaxEventsInQueue );
       
    73     iEventQueue->SetReserveL( KMaxEventsInQueue );
       
    74     iObservers.Append( &aEventObserver );
       
    75 	}
       
    76 	
       
    77 // Destructor
       
    78 CVtEngEventManager::~CVtEngEventManager()
       
    79     {
       
    80     Cancel();
       
    81     delete iEventQueue;
       
    82     iObservers.Close();
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CVtEngEventManager::SessionStateChangedL
       
    87 // Notifies session state change to UI.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void CVtEngEventManager::SessionStateChanged()
       
    91     {
       
    92     __VTPRINTENTER( "EventManager.SessionStateChanged" )
       
    93     NotifyEvent( KVtEngSessionStateChanged );
       
    94     __VTPRINTEXIT( "EventManager.SessionStateChanged" )
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CVtEngEventManager::NotifyEvent
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 void CVtEngEventManager::NotifyEvent( const TInt aEvent )
       
   102     {
       
   103     __VTPRINTENTER( "EventManager.NotifyEvent" )
       
   104     __VTPRINT2( DEBUG_GEN, "EventManager.NotifyEvent event=%d", aEvent );
       
   105     CVtEngEventManager& eventManager = 
       
   106             CVtEngUtility::EventManager();
       
   107     if ( !eventManager.QueueAsyncEvent( aEvent ) )
       
   108         {        
       
   109         eventManager.DoNotifyEvent( aEvent );
       
   110         }
       
   111     __VTPRINTEXIT( "EventManager.NotifyEvent" )
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CVtEngEventManager::AddObserverL
       
   116 //
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 void CVtEngEventManager::AddObserverL( MVtEngEventObserver* aObserver )
       
   120     {
       
   121     TInt count( iObservers.Count() );
       
   122     TBool duplicate = EFalse;
       
   123     while ( count-- )
       
   124         {
       
   125         if ( aObserver == iObservers[ count ] )
       
   126             {
       
   127             count = 0; // break loop
       
   128             duplicate = ETrue;
       
   129             }
       
   130         }
       
   131     if ( !duplicate )
       
   132         {
       
   133         User::LeaveIfError( iObservers.Append( aObserver ) );
       
   134         }
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CVtEngEventManager::RemoveObserver
       
   139 //
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 void CVtEngEventManager::RemoveObserver( const MVtEngEventObserver* aObserver )
       
   143     {
       
   144     TInt count( iObservers.Count() );
       
   145     while ( count-- )
       
   146         {
       
   147         if ( aObserver == iObservers[ count ] )
       
   148             {
       
   149             iObservers.Remove( count );
       
   150             count = 0; // break loop
       
   151             }
       
   152         }
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CVtEngEventManager::RunL
       
   157 // Notify event
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 void CVtEngEventManager::RunL()
       
   161     {
       
   162     __VTPRINTENTER( "EventManager.RunL" )
       
   163     __VTPRINT2( DEBUG_GEN, "EventManager.RunL count=%d", iEventQueue->Count() );
       
   164     // get oldest event in queue (event at index 0)
       
   165     const TInt event = iEventQueue->At( 0 ) ;
       
   166     
       
   167     // delete event before calling DoNotifyEvent(), because DoNotifyEvent() may
       
   168     // hang if it causes ASynchronous call in handler
       
   169     iEventQueue->Delete( 0 );
       
   170 
       
   171     // if more events pending -> signal again
       
   172     if( iEventQueue->Count() > 0  )
       
   173         {
       
   174         Signal();    
       
   175         }
       
   176 
       
   177     // and finally notify event
       
   178     DoNotifyEvent( event );      
       
   179     __VTPRINTEXIT( "EventManager.RunL" )
       
   180     }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CVtEngEventManager::DoCancel
       
   184 // No op
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CVtEngEventManager::DoCancel()
       
   188     {
       
   189     }
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CVtEngEventManager::DoNotifyEvent
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 void CVtEngEventManager::DoNotifyEvent( const TInt aEvent )
       
   196     {
       
   197     __VTPRINTENTER( "EventManager.DoNotifyEvent" )
       
   198     __VTPRINT2( DEBUG_GEN, "EventManager.DoNotifyEvent event=%d", aEvent );
       
   199     TRAP_IGNORE( {
       
   200         TInt count( iObservers.Count() );
       
   201         while ( count-- )
       
   202             {
       
   203             iObservers[count]->HandleVtEventL( aEvent ) ;
       
   204             }
       
   205         } )
       
   206     __VTPRINTEXIT( "EventManager.DoNotifyEvent" )
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CVtEngEventManager::QueueAsyncEvent
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 TBool CVtEngEventManager::QueueAsyncEvent( const TInt aEvent )
       
   214     {
       
   215     __VTPRINTENTER( "EventManager.QueueAsyncEvent" )
       
   216     __VTPRINT2( DEBUG_GEN, "EventManager.QueueAsyncEvent event=%d", aEvent );
       
   217     TBool async( EFalse );
       
   218     switch ( aEvent )
       
   219         {
       
   220         case KVtEngSessionStateChanged:
       
   221         case KVtEngNegotiationProblem:
       
   222         case KVtEngShareImageInitializeBegin:
       
   223         case KVtEngShareImageInitializeEnd:
       
   224             async = ETrue;
       
   225             QueueAndSignal( aEvent );
       
   226             break;
       
   227         default:
       
   228             break;
       
   229         }
       
   230     __VTPRINTEXIT( "EventManager.QueueAsyncEvent" )
       
   231     return async;
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CVtEngEventManager::QueueAndSignal
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 void CVtEngEventManager::QueueAndSignal( const TInt aEvent )
       
   239     {
       
   240     __VTPRINTENTER( "EventManager.QueueAndSignal" )
       
   241     __VTPRINT2( DEBUG_GEN, "EventManager.QueueAndSignal event=%d", aEvent );
       
   242     if( iEventQueue->Count() < KMaxEventsInQueue )
       
   243         {
       
   244         TRAP_IGNORE( iEventQueue->AppendL( aEvent ) ); // can't leave
       
   245         Signal();
       
   246         }
       
   247     else
       
   248         {
       
   249         Panic( EVtEngPanicInvalidTooManyPendingEvents );        
       
   250         }
       
   251     __VTPRINTEXIT( "EventManager.QueueAndSignal" )
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // CVtEngEventManager::Signal
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 void CVtEngEventManager::Signal()
       
   259     {
       
   260     __VTPRINTENTER( "EventManager.Signal" )
       
   261     if( !IsActive() )
       
   262         {
       
   263         SetActive();
       
   264         TRequestStatus* status = &iStatus;
       
   265         User::RequestComplete( status, KErrNone );
       
   266         __VTPRINT2( DEBUG_GEN, "EventManager.QueueAndSignal count=%d", iEventQueue->Count() );
       
   267         }                
       
   268     __VTPRINTEXIT( "EventManager.Signal" )
       
   269     }
       
   270 
       
   271 //  End of File