summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntplsql/src/clplcontactproperties.cpp
blob: 41eaa5bd92ea7f4be8633fe94ac523637a9b25ae (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
* 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 <barsc.h> // For resource reading.
#include <bautils.h>
#include <cntmodel.rsg> // For System template resources.
#include <phbksync.h> // For KUidIccGlobalAdnPhonebook.

#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
#include "cntsyncecom.h"
#endif

#include "persistencelayerimpl.h"
#include "clplcontactproperties.h" 
#include "plplugins.h"


CLplContactProperties::CLplContactProperties(RFs& aFs)
	:
	iFs(aFs),
	iDbViewContactType(KUidContactItem)
	{
	}


CLplContactProperties::~CLplContactProperties()
	{
	delete iPhoneParser;
	if (iSyncPlugin)
		{
		iSyncPlugin->Release();
		iSyncPlugin = NULL;
		}
	delete iSyncDummy;
	delete iSystemTemplate;
	delete iGroupIdList;
	}


void CLplContactProperties::SetContactItemManagerL(CPplContactItemManager& aContactItemManager)
	{
	__ASSERT_ALWAYS(iSystemTemplate == NULL, User::Leave(KErrArgument));
	iContactItemManager = &aContactItemManager;
	iSystemTemplate = CSystemTemplateManager::NewL(*iContactItemManager, iFs);
	}


void CLplContactProperties::SetDbViewContactType(TUid aDbViewContactType)
	{
	iDbViewContactType = aDbViewContactType;
	}


TUid CLplContactProperties::GetDbViewContactType() const
	{
	return iDbViewContactType;
	}

CContactPhoneParser& CLplContactProperties::ContactPhoneParserL()
	{
	if (!iPhoneParser)
		{
		iPhoneParser = CContactPhoneParser::NewL();
		}
	__ASSERT_DEBUG(iPhoneParser != NULL, User::Leave(KErrGeneral));
	return *iPhoneParser;
	}


void CLplContactProperties::DisableSynchroniser(TUint aSessionId)
	{
	iDisabledPhonebookSessionId = aSessionId;
	}


void CLplContactProperties::SessionDeleted(TUint aSessionId)
	{
	if (aSessionId == KIgnorePhbkSync || aSessionId == iDisabledPhonebookSessionId)
		{
		iDisabledPhonebookSessionId = 0;			
		}
	}


MContactSynchroniser& CLplContactProperties::ContactSynchroniserL(TUint aSessionId)
	{
	// If the caller asked to ignore the plugin the return the dummy plugin.
	if (aSessionId == KIgnorePhbkSync || aSessionId == iDisabledPhonebookSessionId)
		{
		if (!iSyncDummy)
			{
			iSyncDummy = new (ELeave) CDummySynchroniser();
			}
		// Return the dummy plugin.
		return *iSyncDummy;
		}
	else
		{
		if (!iSyncPlugin)
			{
			iSyncPlugin = CContactSynchroniser::NewL();
			}
		__ASSERT_DEBUG(iSyncPlugin != NULL, User::Leave(KErrGeneral));
		// Return the real plugin.
		return *iSyncPlugin;
		}
	}


CSystemTemplateManager& CLplContactProperties::SystemTemplateManager() const
   	{
  	return *iSystemTemplate;
  	}


const CContactTemplate& CLplContactProperties::SystemTemplateL() const
   	{
  	return iSystemTemplate->TemplateL();
  	}


const RArray<TUid>& CLplContactProperties::CustomFilterableFieldsL()
	{
	return iSystemTemplate->CustomFilterableFieldsL();
	}


void CLplContactProperties::RecreateSystemTemplateL()
	{
	iSystemTemplate->RecreateSystemTemplateL();
	}


TInt CLplContactProperties::CardTemplatePrefIdL() const
	{
	return iContactItemManager->CardTemplatePrefIdL();
	}

	
void CLplContactProperties::SetCardTemplatePrefIdL(TInt aCardTemplatePrefId)
	{
	iContactItemManager->SetCardTemplatePrefIdL(aCardTemplatePrefId);
	}


CContactIdArray& CLplContactProperties::CardTemplateIdsL()
	{
	return iContactItemManager->CardTemplateIdsL();
	}	


CContactIdArray& CLplContactProperties::GroupIdListL()
	{
	delete iGroupIdList;
	iGroupIdList = NULL;
	iGroupIdList = iContactItemManager->GroupIdListL();
	return *iGroupIdList;
	}	


CBufSeg* CLplContactProperties::DetailsListL(const TDesC& aSearchQuery) const 
    {
    return iContactItemManager->DetailsListL(aSearchQuery);
    }   
 

TInt64 CLplContactProperties::MachineIdL() const
    {
    return iContactItemManager->PreferencesPersitor().MachineIdL();
    }

	
void CLplContactProperties::SetMachineIdL(TInt64 aMachineId)
	{
	iContactItemManager->PreferencesPersitor().SetMachineIdL(aMachineId);
	}

	
TPtrC CLplContactProperties::UniqueIdL()
	{
	return iContactItemManager->PreferencesPersitor().DatabaseUidL();  
	}
	
	
CContactTemplate* CLplContactProperties::SystemTemplateFromResourceL(const TDesC& aResouceFileName) const
	{
	return iSystemTemplate->SystemTemplateFromResourceL(aResouceFileName);
	}	


TContactItemId CLplContactProperties::OwnCardIdL()
	{
	return iContactItemManager->OwnCardIdL();
	}


void CLplContactProperties::SetOwnCardIdL(TContactItemId aId, TBool aPersist)
	{
	iContactItemManager->SetOwnCardIdL(aId, aPersist);
	}


const CArrayFix<CContactDatabase::TSortPref>& CLplContactProperties::SortPrefsL()
	{
	return iContactItemManager->PreferencesPersitor().PreferredSortOrderL();
	}
 

void CLplContactProperties::SetSortPrefsL(CArrayFix<CContactDatabase::TSortPref>* aPrefs)
	{
	iContactItemManager->PreferencesPersitor().SetPreferredSortOrderL(aPrefs);
	}


/**
Determines whether or note the given contact type UID complies with one of a
set of rules.
*/
TBool CLplContactProperties::CheckType(TUid aUid) const
	{
	// Rule 1: Contact type UID is not the System template UID.
	if (aUid == KUidContactTemplate)
		{
		return EFalse;
		}

	// Rule 2: Contact type UID matches current view contact type UID.
	if (iDbViewContactType == KUidContactItem)
		{
		return ETrue;
		}

	// Rule 3: Contact type UID is a card or group and current view contact type
	// UID is cards or groups.
	if (iDbViewContactType == KUidContactCardOrGroup &&
		(aUid == KUidContactCard || aUid == KUidContactGroup))
		{
		return ETrue;
		}

	// Rule 4: Contact type UID is Own Card and current view contact type UID is
	// cards.
	if (iDbViewContactType == KUidContactCard && aUid == KUidContactOwnCard)
		{
		return ETrue;
		}
	// Rule 5: Contact type UID matches current view contact type UID.
	else if (aUid == iDbViewContactType)
		{
		return ETrue;
		}
	
	// Did not comply with any of the rules.	
	return EFalse;
	}


TContactItemId CLplContactProperties::ICCTemplateIdL(TUid aPhonebookUid, TUint aSessionId)
	{
	TContactItemId id = ContactSynchroniserL(aSessionId).ICCTemplateIdL(aPhonebookUid);
	return (aPhonebookUid == KNullUid) ? KNullContactId : id;
	}


TContactItemId CLplContactProperties::PhonebookGroupIdL(TUint aSessionId)
	{
	return ContactSynchroniserL(aSessionId).GroupIdL(KUidIccGlobalAdnPhonebook);
	}


CSystemTemplateManager* CSystemTemplateManager::NewL(CPplContactItemManager& aContactItemManager, RFs& aFileSession)
	{
	return new(ELeave) CSystemTemplateManager(aContactItemManager, aFileSession);
	}

	
CSystemTemplateManager::CSystemTemplateManager(CPplContactItemManager& aContactItemManager, RFs& aFileSession)
	:
	iContactItemManager(aContactItemManager),
	iFileSession(aFileSession)
	{
	}


CSystemTemplateManager::~CSystemTemplateManager()	
	{
	delete iSystemTemplate;
	iCustFiltFields.Reset();
	iCustFiltFields.Close();
	}


const CContactTemplate& CSystemTemplateManager::TemplateL()
	{
	if (iSystemTemplate)
		{
		return *iSystemTemplate;
		}

 	CContactItemViewDef* vDef = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields, CContactItemViewDef::EIncludeHiddenFields);
	vDef->AddL(KUidContactFieldMatchAll);
	iSystemTemplate = static_cast<CContactTemplate*>(iContactItemManager.ReadLC(KGoldenTemplateId, *vDef, EPlAllInfo, KIgnorePhbkSync)); 
	
	CleanupStack::Pop(); 
	CleanupStack::PopAndDestroy(vDef);

	return *iSystemTemplate;
	}

RArray<TUid>& CSystemTemplateManager::CustomFilterableFieldsL()
	{
	if (iCustFiltFields.Count() == 0) // Array is not yet initialised.
		{
		// Field order used to store user defined filter.
		TInt orderOfPriority = 0;

		// Initialise custom filterable fields.
		const TUid KUidCustFilterNullValue = {KNullContactId}; 
		TInt ii = 0;
		for (; ii < KMaxCustomFilterableFields; ++ii)
			{
			iCustFiltFields.InsertL(KUidCustFilterNullValue,ii);
			}

		ii = iSystemTemplate->CardFields().Count() - 1;
		EContactFieldFlags contactFieldFlag = EContactFieldFlagFilterable; 
		
		// For all the fields of the template find any fields which have one
		// of the filterable field flags and add them to the custom filterable
		// fields array.
		for (; ii>=0; --ii) 
			{
			// Current template field.
			CContactItemField& field = iSystemTemplate->CardFields()[ii]; 
				
			if (field.HasCustomFilter(contactFieldFlag))
				{
				const TFieldType fieldType = (field.ContentType()).FieldType(0);

				if (contactFieldFlag == EContactFieldFlagFilterable)
					{
					if (orderOfPriority > KMaxCustomFilterableFields)
						{
						User::Leave(KErrNotSupported);
						}
					iCustFiltFields[orderOfPriority++] = fieldType;
					}
				else if (contactFieldFlag == EContactFieldFlagFilterable1)
					{
					// Store filter value at highest priority position zero.
					iCustFiltFields[0] = fieldType;
					}
				else if (contactFieldFlag == EContactFieldFlagFilterable2)
					{
					// Store filter value at next priority position.
					iCustFiltFields[1] = fieldType;
					}
				else if (contactFieldFlag == EContactFieldFlagFilterable3)
					{
					iCustFiltFields[2] = fieldType;
					}
				else if (contactFieldFlag == EContactFieldFlagFilterable4)
					{
					iCustFiltFields[3] = fieldType;
					}
				}
			}

		// Validate array with customer filterable fields.  Leave if highest
		// priority UID is KUidCustFilterNullValue or if KUidCustFilterNullValue
		// appears between valid field type UIDs.
		ii = 0;
		for(; ii < KMaxCustomFilterableFields - 1; ++ii)
			{
			if (iCustFiltFields[ii] == KUidCustFilterNullValue && (iCustFiltFields[ii+1] != KUidCustFilterNullValue))
          		{
				User::Leave(KErrNotSupported);	
           		}
			}
			
		// Remove KUidCustFilterNullValue values from the end of the array.
		ii = KMaxCustomFilterableFields - 1;
		for(; ii >= 0; --ii)
			{
			if (iCustFiltFields[ii] == KUidCustFilterNullValue)	
			   {
			   iCustFiltFields.Remove(ii);
			   }	
			}
		}

		return iCustFiltFields;
	}
	

void CSystemTemplateManager::RecreateSystemTemplateL()
	{
 	#if defined(__PROFILE_DEBUG__)
		RDebug::Print(_L("[CNTMODEL] MTD: CSystemTemplateManager::RecreateSystemTemplateL"));
	#endif 
	
	_LIT(KCntS60SystemTemplate,"z:\\resource\\cntmodel\\pbkcntmodel.rsc");
	CContactTemplate* systemTemplate = NULL;
	//try first S60 contacts template
	TRAPD(err, systemTemplate = SystemTemplateFromResourceL(KCntS60SystemTemplate););
	if (err != KErrNone)
	    {
	    //try default template
	    _LIT(KCntDefaultSystemTemplate,"z:\\resource\\cntmodel\\cntmodel.rsc");
	    systemTemplate = SystemTemplateFromResourceL(KCntDefaultSystemTemplate);
	    }
	CleanupStack::PushL(systemTemplate);

	if (iContactItemManager.IsDatabaseEmptyL())
		{
	    iContactItemManager.CreateL(*systemTemplate, KIgnorePhbkSync);
		}
	else
		{
		iContactItemManager.UpdateL(*systemTemplate, KIgnorePhbkSync);
		}
	
	CleanupStack::PopAndDestroy(systemTemplate);
	}


void CSystemTemplateManager::DeleteTemplate()
	{
	delete iSystemTemplate;
	iSystemTemplate = NULL;
	iCustFiltFields.Reset();
	iCustFiltFields.Close();
	}


CContactTemplate* CSystemTemplateManager::SystemTemplateFromResourceL(const TDesC& aResouceFileName)
	{
	TFileName fileName(aResouceFileName);
	BaflUtils::NearestLanguageFile(iFileSession, fileName);

	RResourceFile resourceFile;
	resourceFile.OpenL(iFileSession, fileName);
	CleanupClosePushL(resourceFile);
	resourceFile.ConfirmSignatureL(4);
	
	TResourceReader resReader;
	resReader.SetBuffer(resourceFile.AllocReadLC(R_CNTUI_NEW_FIELD_DEFNS));

	CContactTemplate* templ = CContactTemplate::NewLC();
	TInt count 				= resReader.ReadInt16(); // num fields
	
	while(count--)
		{
		TStorageType storageType = resReader.ReadInt32();
		TUid contactFieldUid	 = TUid::Uid(resReader.ReadInt32());
		TUid vCardMappingUid 	 = TUid::Uid(resReader.ReadInt32());
		CContactItemField* field = CContactItemField::NewLC(storageType);

		field->SetMapping (vCardMappingUid);
		if (contactFieldUid!=KUidContactFieldNone)
			{
			field->AddFieldTypeL (contactFieldUid);
			}

		TInt mappingCount = 0;
		if((mappingCount=resReader.ReadInt16())!=0)
			{
			while(mappingCount--)
				{
				field->AddFieldTypeL(TUid::Uid(resReader.ReadInt32()));
				}
			}

		field->SetUserFlags(resReader.ReadInt32());
		field->SetLabel(resReader.ReadHBufCL());

		TUint flags=resReader.ReadInt32();
		if (flags&EContactFieldFlagHidden)
			{
			field->SetHidden(ETrue);
			}
		if (flags&EContactFieldFlagReadOnly)
			{
			field->SetReadOnly(ETrue);
			}
		if (flags&EContactFieldFlagSynchronize)
			{
			field->SetSynchronize(ETrue);
			}
		if (flags&EContactFieldFlagDisabled)
			{
			field->SetDisabled(ETrue);
			}

		// Custom filterable fields.  Here we only set field flags, construction
		// and validation of the custom filterable fields array happens on
		// demand in CustomFilterableFieldsL() method.
		if (flags&EContactFieldFlagFilterable)
			{
			field->SetCustomFilterable(EContactFieldFlagFilterable);
			}
		else if (flags&EContactFieldFlagFilterable1)
			{
			field->SetCustomFilterable(EContactFieldFlagFilterable1);
			}
		else if (flags&EContactFieldFlagFilterable2)
			{
			field->SetCustomFilterable(EContactFieldFlagFilterable2);
			}
		else if (flags&EContactFieldFlagFilterable3)
			{
			field->SetCustomFilterable(EContactFieldFlagFilterable3);
			}
		else if (flags&EContactFieldFlagFilterable4)
			{
			field->SetCustomFilterable(EContactFieldFlagFilterable4);
			}

		templ->AddFieldL(*field);
		CleanupStack::Pop(field);
		}
	
	CleanupStack::Pop(templ);
	CleanupStack::PopAndDestroy(2, &resourceFile); // resRead buf, close res files

	return templ;
	}


TInt CDummySynchroniser::ValidateContact(TValidateOperation, TContactItemId)
	{
	return KErrNone;
	}


TInt CDummySynchroniser::ValidateWriteContact(CContactICCEntry&)
	{
	return KErrNone;
	}


TInt CDummySynchroniser::DeleteContact(TContactItemId)
	{
	return KErrNone;
	}


void CDummySynchroniser::Release()
	{
	}


void CDummySynchroniser::UpdatePostWriteL(const CContactICCEntry&)
	{
	}


TContactItemId CDummySynchroniser::ICCTemplateIdL(TUid)
	{
	return KNullContactId;
	}


TContactItemId CDummySynchroniser::GroupIdL(TUid)
	{
	return KNullContactId;
	}


void CDummySynchroniser::NotifySyncStateChange(TRequestStatus& aStatus, TUid)
	{
	aStatus = KRequestPending;
	TRequestStatus* stat = &aStatus;
	User::RequestComplete(stat, KErrNone);
	}


void CDummySynchroniser::CancelNotifyRequest(TUid)
	{
	}


TBool CDummySynchroniser::IsSynchronisedL(TUid)
	{
	return ETrue;
	}


TInt CDummySynchroniser::PhonebookList(RArray<TUid>&)
	{
	return KErrNone;
	}