summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntview/cntphbksyncwatcher.cpp
blob: 733008eb4c3751b8416f5e41cda3db6c6ebccbc6 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* 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: 
*
*/



#include <cntdb.h>
#include <phbksync.h>

#include "cntstd.h"
#include "cntphbksyncwatcher.h"


//uncomment for commonly required debug printing
//#define __VERBOSE_DEBUG__



/*
 class CContactPhbkSyncWatcher is a simple class that aggregates
 synchronisation events from the different ICC phonebooks (e.g. Global ADN or USIM App ADN)
 */
CContactPhbkSyncWatcher* CContactPhbkSyncWatcher::NewL(MContactSynchroniser& aSyncPlugin)
	{
	CContactPhbkSyncWatcher* self = new (ELeave) CContactPhbkSyncWatcher(aSyncPlugin);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}
	

/*
 Release all resources.
 */
CContactPhbkSyncWatcher::~CContactPhbkSyncWatcher()
	{
	__ASSERT_DEBUG(iObservers.Count() == 0, Panic(ECntPhonebookSyncObserverList));
	iObservers.Close();
	// delete each phbk watcher
	iWatchers.ResetAndDestroy();
	iWatchers.Close();
	}


CContactPhbkSyncWatcher::CContactPhbkSyncWatcher(MContactSynchroniser& aSyncPlugin) : iSyncPlugin(aSyncPlugin)
	{
	}


/*
 Create an array of active objects, one for each Phonebook on the ICC (SIM card),
 to observer Phonebook Synchroniser state change events.
 */
void CContactPhbkSyncWatcher::ConstructL()
	{
	RArray<TUid> phbkList;
	CleanupClosePushL(phbkList);

	// get list of phonebooks for type of ICC (SIM card) present
	TInt error = iSyncPlugin.PhonebookList(phbkList);
	User::LeaveIfError(error);
	const TInt count = phbkList.Count();

	for (TInt j = 0; j < count; ++j)
		{
		iWatchers.AppendL(CSinglePhbkWatcher::NewLC(*this, phbkList[j]));
		CleanupStack::Pop(); // CSinglePhbkWatcher
		}

	CleanupStack::PopAndDestroy();	// close phbkList
	}


/*
 Add Contact View to observer list.
 */
void CContactPhbkSyncWatcher::AddPhbkObserverL(MContactPhbkSyncObserver& aChangeNotifier)
	{
	iObservers.AppendL(&aChangeNotifier);
	}


/*
 Remove Contact View from observer list.
 */
void CContactPhbkSyncWatcher::RemovePhbkObserver(MContactPhbkSyncObserver& aChangeNotifier)
	{
	const TInt observerIndex = iObservers.Find(&aChangeNotifier);

	if (observerIndex != KErrNotFound)
		{
		iObservers.Remove(observerIndex);
		}
	}


/*
 Count of views Phonebooks that are Synchronised.
 */
TInt CContactPhbkSyncWatcher::PhonebooksReady()
	{
	// number of phonebooks that are ready
	return iPhonebooksReady;
	}

/*
 Count of views Phonebooks that are locked (PIN not yet entered).
 */
TInt CContactPhbkSyncWatcher::PhonebooksWaiting()
	{
	// number of phonebooks that are waiting
	return iPhonebooksWaiting;
	}

/*
 Last error, if any, from Phonebook Synchronisation.
 */
TInt CContactPhbkSyncWatcher::PhonebookSyncError()
	{
	// most recent phbksync error
	return iSyncError;
	}

/*
 Pointer to the Phonebook Synchroniser plug-in.
 */
MContactSynchroniser& CContactPhbkSyncWatcher::SyncPlugin()
	{
	return iSyncPlugin;
	}


/*
 Count of views observing Phonebook Synchroniser events.
 */
TInt CContactPhbkSyncWatcher::ObserverCount()
	{
	return iObservers.Count();
	}


/*
 Distributor of Phonebook Sync state changes.
 
 
 */
void CContactPhbkSyncWatcher::SendWatcherEvent(TPhonebookState aNewState, TPhonebookState aOldState, TInt aError)
	{
#if defined(__VERBOSE_DEBUG__)
	RDebug::Print(_L("[CNTMODEL] CContactPhbkSyncWatcher::SendWatcherEvent(NewState = %i, OldState = %i, error = %i)\n\r"), 
		aNewState, aOldState, aError);
#endif

	// Guard maybe the state has not really changed
	if (aNewState != aOldState)
		{
		// track numbers of synchronised & locked phonebooks:
		
		// decrement count associated with the old state
		switch (aOldState)
			{
		case EIccPhbkNotSynchronised:
			break;
		case EIccPhbkSynchronised:
			--iPhonebooksReady;
			break;
		case EIccWaitForPhbkToBeReady:
			--iPhonebooksWaiting;
			break;
		case EIccPhbkSyncError:
			--iPhonebookErrors;
			if (0 == iPhonebookErrors)
				{
				iSyncError = KErrNone;
				}
			break;
			}

		// increment count associated with the new state
		switch (aNewState)
			{
		case EIccPhbkNotSynchronised:
			break;
		case EIccPhbkSynchronised:
			++iPhonebooksReady;
			break;
		case EIccWaitForPhbkToBeReady:
			++iPhonebooksWaiting;
			break;
		case EIccPhbkSyncError:
			++iPhonebookErrors;
			iSyncError = aError;
			break;
			}

#if defined(__VERBOSE_DEBUG__)
		RDebug::Print(_L("[ PhonebooksReady = %i, PhonebooksWaiting = %i, PhonebookErrors = %i, SyncError = %i)\n\r"), 
			iPhonebooksReady, iPhonebooksWaiting, iPhonebookErrors, iSyncError);
#endif

		// distribute the state change to all observing contact views
		const TInt count = iObservers.Count();
		for(TInt loop = count; loop > 0; --loop)
			{
			iObservers[loop - 1]->ContactPhbkSyncEventHandler(aNewState);
			}
		}
	}


/*
 class CSinglePhbkWatcher tracks the status of an ICC phonebook

 This is a very simple CActive derived class.
 */
CSinglePhbkWatcher* CSinglePhbkWatcher::NewLC(CContactPhbkSyncWatcher& aSyncWatcher, TUid& aPhonebookUid)
	{
#if defined(__VERBOSE_DEBUG__)
	RDebug::Print(_L("[CNTMODEL] CSinglePhbkWatcher::CSinglePhbkWatcher(PhonebookUid = 0x%X)\n\r"), aPhonebookUid.iUid);
#endif

	CSinglePhbkWatcher* self = new(ELeave) CSinglePhbkWatcher(aSyncWatcher, aPhonebookUid);
	CleanupStack::PushL(self);
	return self;
	}
	
	
/*
 Standard CActive destructor.
 */
CSinglePhbkWatcher::~CSinglePhbkWatcher()
	{
	Cancel();
	}


/*
 Standard CActive constructor, adds itself to the active scheduler.
 */
CSinglePhbkWatcher::CSinglePhbkWatcher(CContactPhbkSyncWatcher& aSyncWatcher, TUid& aPhonebookUid) :
	CActive(CActive::EPriorityStandard), iSyncWatcher(aSyncWatcher), iPhonebookUid(aPhonebookUid), iPhbkState(EIccPhbkNotSynchronised)
	{
	CActiveScheduler::Add(this);
	// ensure active scheduler calls RunL for the first time
	TRequestStatus* ptrStatus = &iStatus;
	*ptrStatus = KRequestPending;
	User::RequestComplete(ptrStatus, KErrNone);
	SetActive();
	}


/*
 Register for Phonebook Synchroniser state change notification for this phonebook.
 */
void CSinglePhbkWatcher::Start()
	{
#if defined(__VERBOSE_DEBUG__)
	RDebug::Print(_L("[CNTMODEL] CSinglePhbkWatcher{PhonebookUid = 0x%X}::Start(), &iStatus = 0x%X\n\r"),
		iPhonebookUid.iUid, &iStatus);
#endif

	// Waiting for ICC to be ready and for this Phonebook to synchronise
	iSyncWatcher.SyncPlugin().NotifySyncStateChange(iStatus, iPhonebookUid);
	SetActive();
	}


/*
 Cancel any active request to the phonebook synchroniser
 */
void CSinglePhbkWatcher::DoCancel()
	{
#if defined(__VERBOSE_DEBUG__)
	RDebug::Print(_L("[CNTMODEL] CSinglePhbkWatcher{PhonebookUid = 0x%X}::DoCancel()\n\r"), iPhonebookUid.iUid);
#endif

	// cleanup for Delete, RunL may have left an outstanding NotifySyncStateChangeL request
	iSyncWatcher.SyncPlugin().CancelNotifyRequest(iPhonebookUid);
	}


/*
 Process Phonebook Synchroniser notifications for this Phonebook.
 
 Error conditions drop out to the RunError() handler.
 This can only end up at the Synchronised or Not Synchronised states,
 which are passed back to the parent CContactPhbkSyncWatcher.
 */
void CSinglePhbkWatcher::RunL()
	{
#if defined(__VERBOSE_DEBUG__)
	RDebug::Print(_L("[CNTMODEL] CSinglePhbkWatcher{PhonebookUid = 0x%X}::RunL(), iStatus = %i\n\r"), iPhonebookUid.iUid, iStatus.Int());
#endif
	
	const TInt status = iStatus.Int();

	// re-start, for next change
	Start();

	// ignore KErrCancel
	if (status != KErrCancel)
		{

		User::LeaveIfError(status);

		TBool isPhbkSync = iSyncWatcher.SyncPlugin().IsSynchronisedL(iPhonebookUid);

		TPhonebookState oldState = iPhbkState;
		if (isPhbkSync)
			{
			iPhbkState = EIccPhbkSynchronised;
			}
		else
			{
			iPhbkState = EIccPhbkNotSynchronised;
			}

		iSyncWatcher.SendWatcherEvent(iPhbkState, oldState);
		}
	}


/*
 Process Phonebook Synchroniser errors for this Phonebook.
 
 Error conditions either come from completion with an error code or 
 the Phonebook Synchroniser IsSynchronisedL() method leaving.
 This can only end up at the PhbkLocked or PhbkSyncError states,
 which are passed back to the parent CContactPhbkSyncWatcher.
 */
TInt CSinglePhbkWatcher::RunError(TInt aError)
	{
#if defined(__VERBOSE_DEBUG__)
	RDebug::Print(_L("[CNTMODEL] CSinglePhbkWatcher{PhonebookUid = 0x%X}::RunError(%i)\n\r"), iPhonebookUid.iUid, aError);
#endif

	// IsSynchronisedL() left
	if (IsAcceptableError(aError))
		{
		// notify that ICC phonebook is locked
		iSyncWatcher.SendWatcherEvent(EIccWaitForPhbkToBeReady, iPhbkState);
		iPhbkState = EIccWaitForPhbkToBeReady;
		}
	else
		{
		// notify that ICC phonebook sync has an error
		iSyncWatcher.SendWatcherEvent(EIccPhbkSyncError, iPhbkState, aError);
		iPhbkState = EIccPhbkSyncError;
		}
	return KErrNone;
	}
	
/*
Process Phonebook errors recieved in RunError and check if they should be treated
as acceptable errors or as sync errors.
*/
TBool CSinglePhbkWatcher::IsAcceptableError(TInt aSyncError)
	{
	switch(aSyncError)
		{
		case KErrAccessDenied:
		case KErrNotReady:
			{
			return ETrue;	
			}
		default:
			return EFalse;
		}	
	}