photosgallery/slideshow/engine/controlsrc/shweventrouter.cpp
changeset 0 4e91876724a2
child 18 bcb43dc84c44
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:    The scheduler for the slideshow
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "shweventrouter.h"
       
    22 #include "shweventobserver.h"
       
    23 #include "shweventpublisher.h"
       
    24 #include "shwevent.h"
       
    25 #include "shwslideshowenginepanic.h"
       
    26 
       
    27 #include <glxlog.h>
       
    28 #include <glxtracer.h>
       
    29 
       
    30 // constants
       
    31 namespace
       
    32 	{
       
    33 	// the typical amount of observers. In current design six, 
       
    34 	// increase if there is more observers in future
       
    35 	const TInt KEventObserverGranularity = 7;
       
    36 	const TInt KMaxNumberOfConcurrentEvents = 10;
       
    37 	}
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // C++ Constructor. Save a few bits of rom with inlining
       
    41 // -----------------------------------------------------------------------------
       
    42 inline CShwEventRouter::CShwEventRouter()
       
    43 	: iObservers( KEventObserverGranularity ), 
       
    44 	iEvents( KMaxNumberOfConcurrentEvents )
       
    45 	{
       
    46 	// No implementation required
       
    47 	}
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // NewL. Static construction
       
    51 // -----------------------------------------------------------------------------
       
    52 CShwEventRouter* CShwEventRouter::NewL()
       
    53 	{
       
    54 	TRACER("CShwEventRouter::NewL()");
       
    55 	CShwEventRouter* self = new( ELeave ) CShwEventRouter;
       
    56 	CleanupStack::PushL( self );
       
    57 	self->ConstructL();
       
    58 	CleanupStack::Pop( self );
       
    59 	return self;
       
    60 	}
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // Destructor.
       
    64 // -----------------------------------------------------------------------------
       
    65 CShwEventRouter::~CShwEventRouter()
       
    66 	{
       
    67 	TRACER("CShwEventRouter::~CShwEventRouter");
       
    68 	GLX_LOG_INFO("CShwEventRouter::~CShwEventRouter");
       
    69 	// need to cleanup cloned events if we did get run down
       
    70 	if( iEvents.Count() > 0 )
       
    71 		{
       
    72 		for( int i = 0; i < iEvents.Count(); i++ )
       
    73 			{
       
    74 			// call the destructor
       
    75 			delete iEvents[ i ];
       
    76 			}
       
    77 		}
       
    78 	// close the event array itself, this frees the memory
       
    79 	iEvents.Close();
       
    80 	// close the observer array itself, this frees the memory
       
    81 	// NOTE! we did not have ownership of the observers
       
    82 	iObservers.Close();
       
    83 	}
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // ConstructL.
       
    87 // -----------------------------------------------------------------------------
       
    88 void CShwEventRouter::ConstructL()
       
    89 	{
       
    90 	TRACER("CShwEventRouter::ConstructL");
       
    91 	GLX_LOG_INFO("CShwEventRouter::ConstructL");
       
    92 	// reserve space for the events so we should never leave in event handling
       
    93 	iEvents.ReserveL( KMaxNumberOfConcurrentEvents );
       
    94 	iInsideEventLoop = EFalse;
       
    95 	}
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // AddObserversL.
       
    99 // -----------------------------------------------------------------------------
       
   100 void CShwEventRouter::AddObserversL( TArray<MShwEventObserver*> aObservers )
       
   101 	{
       
   102 	TRACER("CShwEventRouter::AddObserversL");
       
   103     GLX_LOG_INFO( "CShwEventRouter::AddObserversL" );
       
   104 	// add each observer
       
   105 	for( TInt i = 0; i < aObservers.Count(); i++ )
       
   106 		{
       
   107 		AddObserverL( aObservers[ i ] );
       
   108 		}
       
   109 	}
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // AddObserverL.
       
   113 // -----------------------------------------------------------------------------
       
   114 void CShwEventRouter::AddObserverL( MShwEventObserver* aObserver )
       
   115 	{
       
   116 	TRACER("CShwEventRouter::AddObserverL( MShwEventObserver* aObserver )");
       
   117     GLX_LOG_INFO("CShwEventRouter::AddObserverL(MShwEventObserver* aObserver)");
       
   118 	__ASSERT_DEBUG( aObserver, User::Panic( _L("Null observer not allowed"), 1 ) );
       
   119 	iObservers.AppendL( aObserver );
       
   120 	}
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // AddProducers.
       
   124 // -----------------------------------------------------------------------------
       
   125 void CShwEventRouter::AddProducers( TArray< MShwEventPublisher* > aPublishers )
       
   126 	{
       
   127 	TRACER("CShwEventRouter::AddProducers");
       
   128     GLX_LOG_INFO( "CShwEventRouter::AddProducers" );
       
   129 	// add each publisher
       
   130 	for( TInt i = 0; i < aPublishers.Count(); i++ )
       
   131 		{
       
   132 		AddProducer( aPublishers[ i ] );
       
   133 		}
       
   134 	}
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // AddProducer.
       
   138 // -----------------------------------------------------------------------------
       
   139 void CShwEventRouter::AddProducer( MShwEventPublisher* aPublisher )
       
   140 	{
       
   141 	TRACER("CShwEventRouter::AddProducers(MShwEventPublisher* aPublisher)");
       
   142     GLX_LOG_INFO( "CShwEventRouter::AddProducer" );
       
   143 	// give the publisher the event queue so they can start calling us
       
   144 	aPublisher->SetEventQueue( this );
       
   145 	}
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // SendEventL. The method that handles the event dispatching.
       
   149 // The events are dispatched in same order as they are sent, so if an event 
       
   150 // is sent while previous is beeing dispatched we queue the event and 
       
   151 // send it after the previous is dispatched to all observers
       
   152 // -----------------------------------------------------------------------------
       
   153 void CShwEventRouter::SendEventL( MShwEvent* aEvent )
       
   154 	{
       
   155 	TRACER("CShwEventRouter::SendEventL( MShwEvent* aEvent )");
       
   156     GLX_LOG_INFO( "CShwEventRouter::SendEvent" );
       
   157 	__ASSERT_DEBUG( aEvent, User::Panic( _L("Null event not allowed"), 2 ) );
       
   158 
       
   159 	// add the event to the queue
       
   160 	// need to clone the event as the calling code may get out of scope
       
   161 	// before we have time to deliver the event
       
   162 	// this should never leave as we have reserved space in the array
       
   163 	MShwEvent* clone = aEvent->CloneLC();
       
   164 	iEvents.AppendL( clone );
       
   165 	// we take ownership of the clone so pop it off the stack
       
   166 	CleanupStack::Pop( clone );
       
   167 	
       
   168 	// are we inside event loop: when an event is beeing sent we may be 
       
   169 	// delivering an earlier event
       
   170 	if( iInsideEventLoop )
       
   171 		{
       
   172 		// there is an event beeing served so queue the event and return
       
   173 		// the event will be served once previous one is served
       
   174 		return;
       
   175 		}
       
   176 	// ok, dwell into the event serving loop
       
   177 	// when we fall here we are just about to serve the one event
       
   178 	// we may get new events while processing the old ones however
       
   179 	iInsideEventLoop = ETrue;
       
   180 	do
       
   181 		{
       
   182 		// take the first event
       
   183 		MShwEvent* event = iEvents[ 0 ];
       
   184 		TInt i = 0;
       
   185 		// notify all observers
       
   186 		for( ; i < iObservers.Count(); i++ )
       
   187 			{
       
   188 			iObservers[ i ]->NotifyL( event );
       
   189 			}
       
   190 		// this event is served, remove it and delete it
       
   191 		iEvents.Remove( 0 );
       
   192 		// delete the clone
       
   193 		delete event;
       
   194 		// if there are new events, deliver them as well
       
   195 		}
       
   196 	while( iEvents.Count() > 0 );
       
   197 	// done processing events, safe to enter the loop again
       
   198 	iInsideEventLoop = EFalse;
       
   199 	}
       
   200