meetingrequest/mrgui/src/cesmrfieldeventqueue.cpp
changeset 0 8466d47a6819
child 16 4ce476e64c59
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  Field event queue implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "cesmrfieldeventqueue.h"
       
    21 
       
    22 #include <eikenv.h> // for CEikonEnv::HandleError
       
    23 
       
    24 #include "mesmrfieldevent.h"
       
    25 #include "mesmrfieldeventobserver.h"
       
    26 #include "mesmrfieldeventnotifier.h"
       
    27 
       
    28 // ======== MEMBER FUNCTIONS ========
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // CESMRFieldEventQueue::CESMRFieldEventQueue
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CESMRFieldEventQueue::CESMRFieldEventQueue()
       
    35     {
       
    36     FUNC_LOG;
       
    37     // Do nothing
       
    38     }
       
    39 
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CESMRFieldEventQueue::ConstructL
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 void CESMRFieldEventQueue::ConstructL()
       
    46     {
       
    47     FUNC_LOG;
       
    48     iIdle = CIdle::NewL( CActive::EPriorityStandard );
       
    49     }
       
    50 
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // CESMRFieldEventQueue::NewL
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CESMRFieldEventQueue* CESMRFieldEventQueue::NewL()
       
    57     {
       
    58     FUNC_LOG;
       
    59     CESMRFieldEventQueue* self = CESMRFieldEventQueue::NewLC();
       
    60     CleanupStack::Pop( self );
       
    61     return self;
       
    62     }
       
    63 
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CESMRFieldEventQueue::NewLC
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CESMRFieldEventQueue* CESMRFieldEventQueue::NewLC()
       
    70     {
       
    71     FUNC_LOG;
       
    72     CESMRFieldEventQueue* self = new( ELeave ) CESMRFieldEventQueue;
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     return self;
       
    76     }
       
    77 
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // CESMRFieldEventQueue::~CESMRFieldEventQueue
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 CESMRFieldEventQueue::~CESMRFieldEventQueue()
       
    84     {
       
    85     FUNC_LOG;
       
    86     if ( iIdle )
       
    87         {
       
    88         iIdle->Cancel();
       
    89         delete iIdle;
       
    90         }
       
    91     iObservers.Reset();
       
    92     iEventQueue.ResetAndDestroy();
       
    93     }
       
    94 
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // From class MESMRFieldEventQueue
       
    98 // CESMRFieldEventQueue::AddObserverL
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 void CESMRFieldEventQueue::AddObserverL( MESMRFieldEventObserver* aObserver )
       
   102     {
       
   103     FUNC_LOG;
       
   104     TInt error = iObservers.InsertInAddressOrder( aObserver );
       
   105     if ( error && error != KErrAlreadyExists )
       
   106         {
       
   107         User::Leave( error );
       
   108         }
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // From class MESMRFieldEventQueue
       
   113 // CESMRFieldEventQueue::RemoveObserver
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CESMRFieldEventQueue::RemoveObserver( MESMRFieldEventObserver* aObserver )
       
   117     {
       
   118     FUNC_LOG;
       
   119     TInt index = iObservers.FindInAddressOrder( aObserver );
       
   120     if ( index > KErrNotFound )
       
   121         {
       
   122         iObservers.Remove( index );
       
   123         if ( index < iNotifyIndex )
       
   124             {
       
   125             // Notifying is on going.
       
   126             // aObserver has been already notified, so indexes after it will
       
   127             // decrease by one. Decrease iNotifyIndex also by one.
       
   128             iNotifyIndex = Max( 0, --iNotifyIndex );
       
   129             }
       
   130         }
       
   131     }
       
   132     
       
   133 // ---------------------------------------------------------------------------
       
   134 // From class MESMRFieldEventQueue
       
   135 // CESMRFieldEventQueue::NotifyEventL
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138 void CESMRFieldEventQueue::NotifyEventL( const MESMRFieldEvent& aEvent )
       
   139     {
       
   140     FUNC_LOG;
       
   141     // Reset observer index
       
   142     iNotifyIndex = 0;
       
   143     
       
   144     // Get event sender
       
   145     MESMRFieldEventObserver* sender = NULL;
       
   146     if ( aEvent.Source() )
       
   147         {
       
   148         sender = aEvent.Source()->EventObserver();
       
   149         }
       
   150     
       
   151     while ( iNotifyIndex < iObservers.Count() )
       
   152         {
       
   153         MESMRFieldEventObserver* observer = iObservers[ iNotifyIndex++ ];
       
   154         if ( observer && observer != sender )
       
   155             {
       
   156             // Notify event to other observers but the sender
       
   157             observer->HandleFieldEventL( aEvent );
       
   158             }
       
   159         }
       
   160     }
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // From class MESMRFieldEventQueue
       
   164 // CESMRFieldEventQueue::NotifyEventAsyncL
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 void CESMRFieldEventQueue::NotifyEventAsyncL( MESMRFieldEvent* aEvent )
       
   168     {
       
   169     FUNC_LOG;
       
   170     // Append event to queue
       
   171     iEventQueue.AppendL( aEvent );
       
   172     
       
   173     // Start queue
       
   174     Start();
       
   175     }
       
   176     
       
   177 // ---------------------------------------------------------------------------
       
   178 // From class MESMRFieldEventQueue
       
   179 // CESMRFieldEventQueue::CancelEvent
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 void CESMRFieldEventQueue::CancelEvent( MESMRFieldEvent* aEvent )
       
   183     {
       
   184     FUNC_LOG;
       
   185     // Find event from queue
       
   186     TInt index = iEventQueue.Find( aEvent );
       
   187     if ( index > KErrNotFound )
       
   188         {
       
   189         // Remove and delete event
       
   190         iEventQueue.Remove( index );
       
   191         delete aEvent;
       
   192         
       
   193         // Stop queue if it is empty.
       
   194         if ( iEventQueue.Count() == 0 )
       
   195             {
       
   196             iIdle->Cancel();
       
   197             }
       
   198         }
       
   199     }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CESMRFieldEventQueue::NotifyCallback
       
   203 // CIdle callback function
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 TInt CESMRFieldEventQueue::NotifyCallBack( TAny* aPtr )
       
   207     {
       
   208     FUNC_LOG;
       
   209     TBool ret = EFalse;
       
   210     CESMRFieldEventQueue* self = static_cast< CESMRFieldEventQueue* >( aPtr );
       
   211     if ( self )
       
   212         {
       
   213         TRAPD( error, self->DoNotifyEventAsyncL() )
       
   214         if ( error != KErrNone &&
       
   215              error != KErrCancel &&
       
   216              error != KErrArgument )
       
   217             {
       
   218             // Show error note 
       
   219             CEikonEnv::Static()->HandleError( error ); // codescanner::eikonenvstatic
       
   220             }
       
   221         ret = self->HasEvents();
       
   222         }
       
   223     
       
   224     return ret;
       
   225     }
       
   226     
       
   227 // ---------------------------------------------------------------------------
       
   228 // CESMRFieldEventQueue::DoNotifyEventAsyncL
       
   229 // Removes event from the queue and notifies it to the observers.
       
   230 // ---------------------------------------------------------------------------
       
   231 //
       
   232 void CESMRFieldEventQueue::DoNotifyEventAsyncL()
       
   233     {
       
   234     FUNC_LOG;
       
   235     // Remove first event from queue
       
   236     MESMRFieldEvent* event = iEventQueue[ 0 ];
       
   237     iEventQueue.Remove( 0 );
       
   238     
       
   239     // Push the event into CleanupStack  for NotifyEventL
       
   240     CleanupDeletePushL( event );
       
   241 
       
   242     // Notify event to observers synchronously.
       
   243     NotifyEventL( *event );
       
   244     CleanupStack::PopAndDestroy( event );
       
   245     }
       
   246     
       
   247 // ---------------------------------------------------------------------------
       
   248 // CESMRFieldEventQueue::HasEvents
       
   249 // Checks if event queue has events.
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 TBool CESMRFieldEventQueue::HasEvents() const
       
   253     {
       
   254     FUNC_LOG;
       
   255     return ( iEventQueue.Count() > 0 ); 
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 // CESMRFieldEventQueue::Start
       
   260 // Starts event queue
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 void CESMRFieldEventQueue::Start()
       
   264     {
       
   265     FUNC_LOG;
       
   266     if ( !iIdle->IsActive() && iEventQueue.Count() > 0 )
       
   267         {
       
   268         iIdle->Start( TCallBack( NotifyCallBack, this ) );
       
   269         }
       
   270     }
       
   271