dosservices/dosserver/src/doseventrcvservice.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2002 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 *    Implementation for the CDosEventRcvService class
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <e32base.h>
       
    21 #include "doseventrcvservice.h"
       
    22 #include "doseventmanagerbase.h"
       
    23 #include "doswaitingevent.h"
       
    24 #include "dosclientserver.h"
       
    25 #include "dosserver.h"
       
    26 
       
    27 //
       
    28 // ---------------------------------------------------------
       
    29 // CDosEventRcvService Constructor
       
    30 // ---------------------------------------------------------
       
    31 //  
       
    32 
       
    33 
       
    34 CDosEventRcvService::CDosEventRcvService()
       
    35 : iEventQueue(_FOFF(CDosWaitingEvent,iLink)) , iQueueIter(iEventQueue)
       
    36 {
       
    37 	iRegistered = EFalse;
       
    38 	iReady = EFalse;
       
    39 }
       
    40 
       
    41 
       
    42 //
       
    43 // ---------------------------------------------------------
       
    44 // CDosEventRcvService Destructor
       
    45 // ---------------------------------------------------------
       
    46 //  
       
    47 
       
    48 CDosEventRcvService::~CDosEventRcvService()
       
    49 {
       
    50 	if(iRegistered) iEventManager->UnRegisterListener(this,iEvent);
       
    51 
       
    52 	if (!iEventQueue.IsEmpty())
       
    53 	{
       
    54 		CDosWaitingEvent* item;
       
    55     
       
    56 	    iQueueIter.SetToFirst(); 
       
    57 	    while ((item = iQueueIter++) != NULL)
       
    58 		{
       
    59 			delete item;
       
    60 		}
       
    61 		iEventQueue.Reset();
       
    62 	}
       
    63 }
       
    64 
       
    65 
       
    66 //
       
    67 // ---------------------------------------------------------
       
    68 // CDosEventRcvService::NewL
       
    69 // ---------------------------------------------------------
       
    70 //  
       
    71 
       
    72 CDosEventRcvService* CDosEventRcvService::NewL(CEventManagerBase* aEventManager)
       
    73 {
       
    74 	CDosEventRcvService* result=new (ELeave) CDosEventRcvService;
       
    75 	CleanupStack::PushL(result);
       
    76 	result->ConstructL(aEventManager);
       
    77 	CleanupStack::Pop();
       
    78 
       
    79 	return result;
       
    80 }
       
    81 
       
    82 //
       
    83 // ---------------------------------------------------------
       
    84 // CDosEventRcvService::ConstructL
       
    85 // ---------------------------------------------------------
       
    86 //  
       
    87 
       
    88 void CDosEventRcvService::ConstructL(CEventManagerBase* aEventManager)
       
    89 {
       
    90 	iEventManager = aEventManager;
       
    91 }
       
    92 
       
    93 //
       
    94 // ---------------------------------------------------------
       
    95 // CDosEventRcvService::InformListener
       
    96 // ---------------------------------------------------------
       
    97 //  
       
    98 
       
    99 void CDosEventRcvService::InformListener(CDosWaitingEvent* aEvent)
       
   100 {
       
   101 	TInt err = KErrNone;
       
   102 
       
   103 	if(iReady)
       
   104 	{
       
   105 		iReady = EFalse;
       
   106 		if(aEvent->HasParameter())
       
   107 		{
       
   108 			TRAP(err,iMessage.WriteL(1, aEvent->Parameter()));
       
   109 		}
       
   110 
       
   111 		iMessage.Complete(err);
       
   112 	}
       
   113 	else
       
   114 	{
       
   115 		if(iQueue == EQueue) //The event is queued if the event supports queueing
       
   116 			aEvent->AddMeToQueue(iEventQueue);
       
   117 		else if(iQueue == EOnlyLast)
       
   118 		{
       
   119 			//In the EOnlyLast case the queue only has one member.
       
   120 
       
   121 			if(!iEventQueue.IsEmpty())
       
   122 			{
       
   123 				// If there is an event, we remove it 
       
   124 				CDosWaitingEvent* event = iEventQueue.First();
       
   125 				event->RemoveMeFromQueueD(iEventQueue);
       
   126 			}
       
   127 
       
   128 			aEvent->AddMeToQueue(iEventQueue); // We add the latest event.
       
   129 		}
       
   130 	}
       
   131 }
       
   132 
       
   133 //
       
   134 // ---------------------------------------------------------
       
   135 // CDosEventRcvService::ExecuteMessageL
       
   136 // ---------------------------------------------------------
       
   137 //  
       
   138 
       
   139 TInt CDosEventRcvService::ExecuteMessageL(const RMessage2& aMessage)
       
   140 {
       
   141 	TInt err=KErrNone;
       
   142 
       
   143 	switch (aMessage.Function())
       
   144 	{
       
   145 		case ERegisterEvent:
       
   146 			iEvent = (TInt32)aMessage.Ptr0();
       
   147 			iEventManager->RegisterListenerL(this,iEvent);
       
   148 			iRegistered=ETrue;
       
   149 			return err;
       
   150 
       
   151 		case EUnRegisterEvent:
       
   152 			err = iEventManager->UnRegisterListener(this,iEvent);
       
   153 			if(!err)
       
   154 			{
       
   155 				iRegistered=EFalse;
       
   156 				iReady=EFalse;
       
   157 			}
       
   158 			return err;
       
   159 
       
   160 		case EWaitEvent:
       
   161 			iMessage = aMessage;
       
   162 			iReady = ETrue;
       
   163 			iQueue = (TQueueType)aMessage.Int0();
       
   164 			if(iQueue == EQueue || iQueue == EOnlyLast)
       
   165 			{		
       
   166 				// If we have a Queue we take the events from it.
       
   167 				if(!iEventQueue.IsEmpty())
       
   168 				{
       
   169 					//There are events in the queue. We execute the first now.
       
   170 					CDosWaitingEvent* event = iEventQueue.First();
       
   171 					InformListener(event);
       
   172 					event->RemoveMeFromQueueD(iEventQueue);
       
   173 				}
       
   174 			}
       
   175 			return KErrNone;
       
   176 
       
   177 		case ECancelWaitEvent:
       
   178 			if ( iReady )
       
   179 				{
       
   180 				iReady = EFalse;
       
   181 				iMessage.Complete(KErrCancel);
       
   182 				}
       
   183 			return KErrNone;
       
   184          
       
   185 		default:
       
   186 			PanicClient(aMessage,EPanicIllegalFunction);
       
   187 			return KErrNone;
       
   188 	}
       
   189 }
       
   190 
       
   191