phonebookengines/contactsmodel/tsrc/CContactViewEventQueue.cpp
author Pat Downey <patd@symbian.org>
Wed, 01 Sep 2010 12:29:52 +0100
branchRCL_3
changeset 20 f4a778e096c2
parent 0 e686773b3f54
permissions -rw-r--r--
Revert incorrect RCL_3 drop: Revision: 201033 Kit: 201035

// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
// All rights reserved.
// This component and the accompanying materials are made available
// under the terms of "Eclipse Public License v1.0"
// which accompanies this distribution, and is available
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
//
// Initial Contributors:
// Nokia Corporation - initial contribution.
//
// Contributors:
//
// Description:
//


// INCLUDES
#include "CContactViewEventQueue.h"

namespace {

// LOCAL CONSTANTS
enum TPanicCode
    {
    EPanicPreCond_HandleContactViewEvent = 1,
    EPanicEventQueueFull
    };

// LOCAL FUNCTIONS
void Panic(TPanicCode aReason)
    {
    _LIT(KPanicText, "CContactViewEventQueue");
    User::Panic(KPanicText,aReason);
    }

void ResetEvent(TContactViewEvent& aEvent)
    {
    aEvent.iEventType = TContactViewEvent::TEventType(-1);
    aEvent.iInt = -1;
    aEvent.iContactId = KNullContactId;
    }

}


// ================= MEMBER FUNCTIONS =======================

CContactViewEventQueue* CContactViewEventQueue::NewL
        (CContactViewBase* aView, TInt aMaxQueueSize)
    {
    CContactViewEventQueue* self = new(ELeave) CContactViewEventQueue;
    CleanupStack::PushL(self);
    self->ConstructL(aView,aMaxQueueSize);
    CleanupStack::Pop(self);
    return self;
    }

CContactViewEventQueue::~CContactViewEventQueue()
	{
	CTimer::Cancel();
	if (iView)
		{
		iView->Close(*this);
		}
	}

TBool CContactViewEventQueue::ListenForEvent(TTimeIntervalSeconds aTimeOut, TContactViewEvent& aEvent)
	{
	ResetEvent(aEvent);
	CTimer::Cancel();

	if (iQueue.IsEmpty())
		{
		CTimer::After(aTimeOut.Int() * 1000000);
		// wait for event or for the timer to expire
		CActiveScheduler::Start();
		}

	if (!iQueue.IsEmpty())
		{
		aEvent = iQueue.Head();
		iQueue.PopHead();
		return ETrue;
		}
	else
		{
		return EFalse;
		}
	}

void CContactViewEventQueue::RunL()
    {
    // Timer expired
    CActiveScheduler::Stop();
    }

void CContactViewEventQueue::HandleContactViewEvent
        (const CContactViewBase& aView, const TContactViewEvent& aEvent)
    {
    __ASSERT_ALWAYS(!iView || iView == &aView, Panic(EPanicPreCond_HandleContactViewEvent));

	TBool eventPushed = iQueue.Push(aEvent);
    __ASSERT_ALWAYS(eventPushed,Panic(EPanicEventQueueFull));

    // If we receive item removed event, it is a signal for the this
    // view observer to stop observing.
    // The observer could have been for example a memory entry representation 
    // and the memory entry has now been deleted -> the observer must close and exit.
	if(iCloseOnItemRemoved && (aEvent.iEventType == TContactViewEvent::EItemRemoved))
        {
        iView->Close(*this);
        }

    if (IsActive())
    	{
    	CTimer::Cancel();
	    CActiveScheduler::Stop();
    	}
    }

CContactViewEventQueue::CContactViewEventQueue()
    : CTimer(CActive::EPriorityStandard)
    {
    }

void CContactViewEventQueue::ConstructL
        (CContactViewBase* aView, TInt aMaxQueueSize)
    {
	iCloseOnItemRemoved = EFalse;
    CTimer::ConstructL();
    iQueue.ConstructL(aMaxQueueSize);
    CActiveScheduler::Add(this);
    if (aView)
        {
        aView->OpenL(*this);
        iView = aView;
        }
    }


// End of File