summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/ccontactvieweventqueue.cpp
blob: 793af453411c04e6be3b224ee58e9091f4cf93d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* 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: 
*
*/

#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