summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntmodel/src/ccntnotificationmonitor.cpp
blob: 9fa2b02cac78d80cdc9983811da7a61392914144 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (c) 2005-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: 
*
*/


/**
 @file
 @internalComponent
 @released
*/


#include "ccontactprivate.h"
#include "ccntnotificationmonitor.h"
#include "rcntmodel.h"
#include "ccntpackager.h"

extern void DebugLogNotification(const TDesC& aMethod, const TContactDbObserverEvent &aEvent);

const TInt KWaitTime = 4000000;

CCntNotificationMonitor::CCntNotificationMonitor(RCntModel& aSession) 
	: CActive(EPriorityStandard), iSession(aSession)
	{}


CCntNotificationMonitor::~CCntNotificationMonitor()
	{
	Cancel();
	}


void CCntNotificationMonitor::DoCancel() 
	{
	// TRequestStatus* status = &iStatus;
	// User::RequestComplete(status, KErrNone);
	}	


/**
This method is called each time the request for an event from the Server is
completed.
*/
void CCntDbNotifyMonitor::RunL()
	{
	if ( iStatus == KErrDied || iStatus == KErrServerTerminated || !iIsConnected )
		{
		// We have lost our connection to the Server.  Attempt to restart it,
		// reconnect the session and open the Contacts database.
		TRAPD( err, iSession.HandlePrematureServerTerminationL() );
		if( err )
		    {
		    iIsConnected = EFalse;
		    User::Leave( err );
		    }
		iIsConnected = ETrue;
		// Request another event.
		Start();
		return;
		}
	
	TInt count = iObserverArray.Count();
	TContactDbObserverEventV2& event = iEventMsg();
	
	CContactIdArray* idArray = NULL;
	TRAP_IGNORE(idArray = iPackager->UnpackCntIdArrayLC();
        CleanupStack::Pop(idArray);
        );
	event.iAdditionalContactIds = idArray;
	
#if defined(__VERBOSE_DEBUG__)	
	DebugLogNotification(_L("[CNTMODEL] CCntDbNotifyMonitor::RunL"), event);	
#endif

	// Notify all registered observers of the event.
	for (TInt i = 0 ; i < count ; ++i)
		{
		iCurrentProcessedObserver = i;
        // Ignore any leave so we can notify all registered observers.
	    TRAP_IGNORE(iObserverArray[i]->HandleDatabaseEventL(event));
		// The call to HandleDatabaseEventL() above may remove or/add observer/observers
		// so get the count again to cover this scenario. Also we have to update current
		// loop counter.
		i = iCurrentProcessedObserver;
		count = iObserverArray.Count();
		}
	
    // Notify all registered observers of the exnteded (V2) event.
	if (event.iTypeV2 != EContactDbObserverEventV2Null)
	    {
        TInt v2Count = iObserverV2Array.Count();
        for (TInt i = 0 ; i < v2Count ; ++i)
            {
            iCurrentProcessedObserver = i;
            if (event.iTypeV2 != EContactDbObserverEventV2Null)
                {
                // Ignore any leave so we can notify all registered observers.
                TRAP_IGNORE(iObserverV2Array[i]->HandleDatabaseEventV2L(event));
                }
            // The call to HandleDatabaseEventL() above may remove or/add observer/observers
            // so get the count again to cover this scenario. Also we have to update current
            // loop counter.
            i = iCurrentProcessedObserver;
            v2Count = iObserverV2Array.Count();
            }
	    }
	
	delete idArray;
	idArray = NULL;
	
	// Request another event.
	Start();
	}

	
/**
First phase constructor.
*/
CCntDbNotifyMonitor::CCntDbNotifyMonitor(RCntModel& aSession) 
	: CCntNotificationMonitor(aSession), iCurrentProcessedObserver(KErrNotFound), iIsConnected(ETrue)
	{}

/**
Second phase constructor.
*/
void CCntDbNotifyMonitor::ConstructL()
    {
    iPackager = CCntPackager::NewL();
    // Allocate big enough buffer for the array of contact ids for
    // notification events. One contact id needs 4 bytes, so let's
    // have a buffer for 10000 contacts.
    iPackagerBuffer = &(iPackager->GetReceivingBufferL(40960));
    }
	
/**
Allocates and constructs new CCntDbNotifyMonitor.

@param aSession Client-side session handle for Contacts Model Server.
*/
CCntDbNotifyMonitor* CCntDbNotifyMonitor::NewL(RCntModel& aSession)
	{
	CCntDbNotifyMonitor* self = new (ELeave) CCntDbNotifyMonitor(aSession);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	CActiveScheduler::Add(self);
	return(self);
	}


CCntDbNotifyMonitor::~CCntDbNotifyMonitor()
	{
	iObserverArray.Close();
	iObserverV2Array.Close();
	delete iPackager;
	}

	
/**
This method is called to request a Contacts database event from the Server.
The RunL() method will be called when an event is available (i.e. when the
Server completes the request.
*/
void CCntDbNotifyMonitor::Start()
	{
    // Check if a request for a Contacts database event from the Server has
	// already been issued.
	if (IsActive())
		{
		return;
		}
	iStatus=KRequestPending;
	iSession.StartNotificationTransfer(iStatus, iEventMsg, *iPackagerBuffer);	
	SetActive();
	}


/**
Add a Contacts database event observer.

@param aObserver The Contacts database event observer to add.
*/
void CCntDbNotifyMonitor::AddObserverL(MContactDbObserver& aObserver)
	{
	// Observers can be added from HandleDatabaseEventL() called from CCntDbNotifyMonitor::RunL()
	// To avoid overcomplicated algorithms for concurent access to handlers array, new observer will
	// be added always at the end of array. Whilst duplications are not allowed, a search is 
	// needed to check if observer is already added to observers array
	
	const TInt index = iObserverArray.Find(&aObserver);
	if( index != KErrNotFound )
		{
		User::Leave( KErrAlreadyExists );
		}
	
	User::LeaveIfError( iObserverArray.Append(&aObserver) );
	
	// At least one observer now added so start request for Contacts database
	// event from the Server (if it hasn't already been started i.e. if the AO
	// is not already active).
	Start();
	}


/**
Remove a Contacts database event observer.

@param aObserver The Contacts database event observer to remove.
*/
void CCntDbNotifyMonitor::RemoveObserver(const MContactDbObserver& aObserver)
	{
	TInt index = iObserverArray.Find(&aObserver);
	if (index != KErrNotFound)
		{
		iObserverArray.Remove(index);
		
		// Observers can be removed from HandleDatabaseEventL() called from CCntDbNotifyMonitor::RunL()
		// In order to notify correctly remaining observers, we need to update current processed observer.
		if( index <= iCurrentProcessedObserver )
			{
			--iCurrentProcessedObserver;
			}
			
		}
	// If there are no more observers then cancel the event request in the
	// Server.
	if (iObserverArray.Count() + iObserverV2Array.Count() == 0)
		{
		iSession.EndNotificationTransfer();
		}
	}
	
/**
Add a Contacts database event observer.

@param aObserver The Contacts database group event observer to add.
*/
void CCntDbNotifyMonitor::AddObserverV2L(MContactDbObserverV2& aObserver)
    {
    // Observers can be added from HandleDatabaseEventV2L() called from CCntDbNotifyMonitor::RunL()
    // To avoid overcomplicated algorithms for concurent access to handlers array, new observer will
    // be added always at the end of array. Whilst duplications are not allowed, a search is 
    // needed to check if observer is already added to observers array
    
    const TInt index = iObserverV2Array.Find(&aObserver);
    if( index != KErrNotFound )
        {
        User::Leave( KErrAlreadyExists );
        }
    
    User::LeaveIfError( iObserverV2Array.Append(&aObserver) );
    
    // At least one observer now added so start request for Contacts database
    // event from the Server (if it hasn't already been started i.e. if the AO
    // is not already active).
    Start();
    }


/**
Remove a Contacts database event observer.

@param aObserver The Contacts database group event observer to remove.
*/
void CCntDbNotifyMonitor::RemoveObserverV2(const MContactDbObserverV2& aObserver)
    {
    TInt index = iObserverV2Array.Find(&aObserver);
    if (index != KErrNotFound)
        {
        iObserverV2Array.Remove(index);
        
        // Observers can be removed from HandleDatabaseGroupEventL() called from CCntDbNotifyMonitor::RunL()
        // In order to notify correctly remaining observers, we need to update current processed observer.
        if( index <= iCurrentProcessedObserver )
            {
            --iCurrentProcessedObserver;
            }
            
        }
    // If there are no more observers then cancel the event request in the
    // Server.
    if (iObserverArray.Count() + iObserverV2Array.Count() == 0)
        {
        iSession.EndNotificationTransfer();
        }
    }

/**
Get the number of registered observers.

@return The number of registered observers.
*/
TInt CCntDbNotifyMonitor::NumberOfListeners() const
	{
	return iObserverArray.Count() + iObserverV2Array.Count();
	}


TInt CCntDbNotifyMonitor::RunError(TInt aError)
	{
#if defined(__VERBOSE_DEBUG__)	
	TContactDbObserverEvent& event = iEventMsg();
	DebugLogNotification(_L("[CNTMODEL] CCntDbNotifyMonitor::RunError"), event);		
#endif

	if (aError != KErrDied && aError != KErrServerTerminated)
	    {
	    if ( iIsConnected )
	        {
	        Start();
	        }
	    else
	        {
	        // This may happen during server update
	        // when server binary is being replaced.
	        // Wait and try to restart connection.
	        iStatus = KRequestPending;
	        if(!IsActive())
	            {
	            SetActive();
	            }
	        User::After( KWaitTime );
	        TRequestStatus* status = &iStatus;
	        User::RequestComplete(status, KErrServerTerminated);
	        }
	    }
		
	return KErrNone;
	}