summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntplsql/src/rpplicccontactstore.cpp
blob: 780229bf683a258445940cac5751887ddf152a05 (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
386
387
388
389
390
391
392
393
394
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* 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 "rpplicccontactstore.h"
#include "clplcontactproperties.h"
#include <phbksync.h>

/**
RPplIccContactStore constructor.
*/
RPplIccContactStore::RPplIccContactStore(CLplContactProperties& aContactProperties)
	:
	iProperties(aContactProperties)	
	{
	}


/**
Close the ICC contacts store.
*/
void RPplIccContactStore::Close()
	{
	iTransactionOps.Close();
	}


/**
Marks the start of a transaction.  If a transaction is already in progress
leaves with KErrAlreadyExists.

This method is part of the MLplTransactionManager interface.
*/
void RPplIccContactStore::StartTransactionL()
	{
	// If transaction already active then leave.
	if (iTransactionActive)
		{
		User::Leave(KErrAlreadyExists);
		}

	iTransactionActive = ETrue;
	}


/**
Commits the current transaction.  If no transaction is in progress leaves with
KErrNotFound.

This method is part of the MLplTransactionManager interface.

@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
*/
void RPplIccContactStore::CommitCurrentTransactionL(TUint aSessionId)
	{
	// If no transaction active then leave.
	if (!iTransactionActive)
		{
		User::Leave(KErrNotFound);
		}

	ExecuteTransactionL(aSessionId);
	// Clear operation cache.
	iTransactionOps.Reset();

	iTransactionActive = EFalse;
	}


/**
Rolls back the current transaction.  If no transaction is in progress then
returns.

This method is part of the MLplTransactionManager interface.

@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
*/
void RPplIccContactStore::RollbackCurrentTransaction(TUint aSessionId)
	{
	if (!iTransactionActive)
		{
		return;
		}

	RollbackTransactionL(aSessionId);
	// Clear operation cache.
	iTransactionOps.Reset();

	iTransactionActive = EFalse;
	}


TBool RPplIccContactStore::IsTransactionActive() const 
	{
	return iTransactionActive;
	}


/**
Creates an ICC entry for the given contact item.

Transactions are handled differently when creating ICC contacts.  We need to
actually write the ICC contact before the transaction is commited.  Update and
Delete operations can be done at commit time.

@param aItem Contact item for which to create ICC entry.
@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
*/
TContactItemId RPplIccContactStore::CreateInDbL(CContactItem& aItem, TUint aSessionId)
	{
	#if defined(__PROFILE_DEBUG__)
		RDebug::Print(_L("[CNTMODEL] MTD: RPplIccContactStore::CreateInDbL"));
	#endif 
		
	// Make sure the contact is a CContactICCEntry.  If it isn't then don't do
	// anything.
	if (aItem.Type() != KUidContactICCEntry)
		{
		return KNullContactId;
		}

	// If in transaction then add to cache.
	if (iTransactionActive)
		{
		TIccContactOperation op(aItem, EIccContactCreate);
		iTransactionOps.AppendL(op);
		}
	
	// Perform operation.
	CreateIccContactL(aItem, aSessionId);
	
	// Obtain the ID of the ICC group.
	TContactItemId groupId = KNullContactId;
	const CContactItemFieldSet& fieldset = aItem.CardFields();
	const TInt n = fieldset.Find(KUidContactFieldICCPhonebook);
	if(n!=KErrNotFound) // Is KUidContactFieldICCPhonebook field supplied?
		{
		TPtrC phonebookField = fieldset[n].TextStorage()->Text();
		TLex lex(phonebookField);
		TUid phonebookUid;
		lex.Val(phonebookUid.iUid);
		groupId = iProperties.ContactSynchroniserL(aSessionId).GroupIdL(phonebookUid);
		}
	else
		{
		// Client hasn't supplied the KUidContactFieldICCPhonebook field.
		// The Phonebook Synchroniser will provide the group ID for the
		// Global/GSM ADN phonebook group as a default.
		groupId = iProperties.ContactSynchroniserL(aSessionId).GroupIdL(KUidIccGlobalAdnPhonebook);
		}

    return groupId;	
	}


/**
Validate the entry to make sure it can be read from the ICC.

@param aItem Contact item to be validated for reading from the ICC.
@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
@param aIccOpenCheck ETrue if validation for open for edit should also be done.
*/
void RPplIccContactStore::ReadL(CContactItem& aItem, TInt /*aInfoToRead*/, TUint aSessionId, TBool aIccOpenCheck) const
	{
	// Make sure the contact is a CContactICCEntry.  If it isn't then don't do
	// anything.
	if (aItem.Type() != KUidContactICCEntry)
		{
		return;
		}
	
	ValidateIccContactL(MContactSynchroniser::ERead, aItem, aSessionId);

	if(aIccOpenCheck)
		{
		ValidateIccContactL(MContactSynchroniser::EEdit, aItem, aSessionId);	
		}
	}


/**
Update an ICC entry using the given contact item.  If in a transaction then just
add the operation to the cache, otherwise do the update directly.

@param aNewItem The contact to be updated.
@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
*/
void RPplIccContactStore::UpdateL(CContactItem& aNewItem, TUint aSessionId)
	{
	// Make sure the contact is a CContactICCEntry.  If it isn't then don't do
	// anything.
	if (aNewItem.Type() != KUidContactICCEntry)
		{
		return;
		}

	// If in transaction then add to cache.
	// Otherwise perform operation.
	if (iTransactionActive)
		{
		TIccContactOperation op(aNewItem, EIccContactUpdate);
		iTransactionOps.AppendL(op);
		}
	else
	    {
        if (aNewItem.IsDeleted() && (aNewItem.AccessCount() == 0))
            {
            DeleteL(aNewItem, aSessionId);
            }
    	else
    		{
    		UpdateIccContactL(aNewItem, aSessionId);
    		}
	    }	
	}


/**
Delete the ICC entry associated with the given contact item.

@param aItem Contact item whose associated ICC entry is to be deleted.
@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
*/
void RPplIccContactStore::DeleteL(const CContactItem& aItem, TUint aSessionId)
	{
	// Make sure the contact is a CContactICCEntry and that it can be deleted.
	// If not then don't do anything.
	if (aItem.Type() != KUidContactICCEntry ||
		!const_cast<CContactItem&>(aItem).IsDeletable())
		{
		return;
		}
	DeleteIccContactL(aItem, aSessionId);
	}


void RPplIccContactStore::PersistorChangeTypeL(TContactItemId /*aItemId*/, TUid /*aNewType*/)
	{
	// Do nothing.
	}


/**
Perform the operations stored in the cache.

@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
*/
void RPplIccContactStore::ExecuteTransactionL(TUint aSessionId)
	{
	for (TInt i = 0; i < iTransactionOps.Count(); i++)
		{
		TIccContactOperation op = iTransactionOps[i];
		switch (op.iType)
			{
			case EIccContactCreate:
				// Nothing to do since create operations are executed
				// immediately, even in transactions.
				break;
			case EIccContactUpdate:
				UpdateIccContactL(op.iContact, aSessionId);
				break;
			case EIccContactDelete:
				DeleteIccContactL(op.iContact, aSessionId);
				break;
			default:
				User::Leave(KErrNotSupported);
				break;
			}
		}
	}

	
/**
Rollback the cached operations.  Only Create operations need special attention
since we need to remove those ICC entries that were created during the
transaction.

@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.

@leave KErrNoMemory Out of memory.
@leave KErrGeneral
@leave [other system-wide leave codes]
*/
void RPplIccContactStore::RollbackTransactionL(TUint aSessionId)
	{
	for (TInt i = 0; i < iTransactionOps.Count(); i++)
		{
		TIccContactOperation op = iTransactionOps[i];
		switch (op.iType)
			{
			case EIccContactCreate:
				// Delete the ICC entry.
				DeleteIccContactL(op.iContact, aSessionId);
				break;
			case EIccContactUpdate:
			case EIccContactDelete:
				// Nothing to do.
				break;
			default:
				// Give up.
				return;
			}
		}
	}


/**
Write a contact using the Phonebook Synchroniser.  We can call the
UpdatePostWriteL() method right after ValidateWriteContact() because we make the
assumption that by the time this function is called the contact item has already
had a UID assigned to it.

@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
*/
void RPplIccContactStore::CreateIccContactL(CContactItem& aItem, TUint aSessionId)
	{
	iProperties.ContactSynchroniserL(aSessionId).UpdatePostWriteL(static_cast<CContactICCEntry&>(aItem));
	}


/**
Update a contact using the Phonebook Synchroniser.

@param aItem Contact item whose associated ICC entry is to be updated.
@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
*/
void RPplIccContactStore::UpdateIccContactL(CContactItem& aCurrItem, TUint aSessionId)
	{
	TInt ret = iProperties.ContactSynchroniserL(aSessionId).ValidateWriteContact(static_cast<CContactICCEntry&>(aCurrItem));
	User::LeaveIfError(ret);			
	}


/**
Delete a contact using the Phonebook Synchroniser.

@param aItem Contact item whose associated ICC entry is to be deleted.
@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.

@leave KErrNoMemory Out of memory.
@leave KErrGeneral
*/
void RPplIccContactStore::DeleteIccContactL(const CContactItem& aItem, TUint aSessionId)
	{
	TInt ret = iProperties.ContactSynchroniserL(aSessionId).DeleteContact(aItem.Id());
	User::LeaveIfError(ret);	
	}


/**
Validate a contact using the Phonebook Synchroniser.

@param aOp Validate operation.
@param aItem Contact item for which the operation is to be validated.
@param aSessionId The ID of the session that issued the request.  Used to
prevent Phonebook Synchroniser deadlock.
*/
void RPplIccContactStore::ValidateIccContactL(MContactSynchroniser::TValidateOperation aOp,
	CContactItem& aItem, TUint aSessionId) const
	{
	TInt ret = iProperties.ContactSynchroniserL(aSessionId).ValidateContact(aOp, aItem.Id());
	User::LeaveIfError(ret);
	}