summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/src/cntfield.cpp
blob: 596af5fe5075626c19b451035f5ca1f12ee332ae (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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
/*
* 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: 
*
*/


#include <s32std.h>

#include "cntstd.h"
#include <cntdef.h>
#include <cntfield.h>
#include <cntfldst.h>

#include <cntitem.h>
#include <versit.h>
#include "cntprof.h"
#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
#include "cntfieldheader.h"
#include "cnthint.h"
#include "cntfield_internal.h"
#endif

//
// class TContactFieldAtts
//
const TUint32 KAttribsMask		    = 0xf0000fff;
const TUint32 KExtendedAttribsMask	= 0x000ff000;
const TUint32 KStorageTypeMask	    = 0x00f00000;

const TUint32 KTypeBitShift		    = 20;
const TUint32 KExtendedAttsBitShift = 12;

/** Set contact field attributes.

@param aAttribs contact field arributes to be set.
@internalTechnology
*/
void TContactFieldAtts::SetAttribs(TUint32 aAttribs)
	{
	iStorage |= aAttribs;
	}
	
/** Set contact field extended arributes.

@param aExtendedAttribs contact field extended arributes to be set.
@internalTechnology
*/
void TContactFieldAtts::SetExtendedAttribs(TUint32 aExtendedAttribs)
	{
	iStorage |= aExtendedAttribs << KExtendedAttsBitShift;
	}

/** Set storage type.

@param aType storage type to set.
@internalTechnology
*/
void TContactFieldAtts::SetType(TStorageType aType)
	{
	iStorage |= aType << KTypeBitShift;
	}

/** Get contact field arributes.

@return contact field arributes.
@internalTechnology
*/
TUint32 TContactFieldAtts::Attribs() const
	{
	return (iStorage & KAttribsMask);
	}
	
/** Get contact field extended arributes.

@return contact field extended arributes.
@internalTechnology
*/
TUint32 TContactFieldAtts::ExtendedAttribs() const
	{
	return (iStorage & KExtendedAttribsMask) >> KExtendedAttsBitShift;
	}
	
/** Get contact field storage type.

@return contact field storage type.
@internalTechnology
*/
TStorageType TContactFieldAtts::Type() const
	{
	return (iStorage & KStorageTypeMask) >> KTypeBitShift;
	}
	
/** Internalize implementation.

@internalTechnology
*/
void TContactFieldAtts::InternalizeL(RReadStream& aStream)
	{
	iStorage=aStream.ReadUint32L();
	}
	
/** Externalize implementation.

@internalTechnology
*/
void TContactFieldAtts::ExternalizeL(RWriteStream& aStream) const
	{
	aStream.WriteUint32L(iStorage);
	}


//
// class CContentType
//
   
EXPORT_C CContentType::~CContentType()
/** Frees all resources owned by the content type object, prior 
to its destruction. */
    {
	delete iFieldTypes;
	}
      
CContentType::CContentType()
    {
	}
      
CContentType::CContentType(TUid aMapping) : iMapping(aMapping)
	{
	}
	
/** Constructs a new content type based on a RReadStream.

@param aStream RReadStream containing object to internalize.
@return Pointer to the newly created CContentType. This is left on the cleanup stack. 
@internalTechnology
*/
CContentType* CContentType::NewLC(RReadStream& aStream)
	{ // static
	CContentType* type = NewL();
	CleanupStack::PushL(type);
	type->InternalizeL(aStream);
	
	return type;
	}		

EXPORT_C CContentType* CContentType::NewL()
/** Allocates and constructs a new default CContentType.

The object has no field types and the mapping is set to KNullUid

@return Pointer to the newly created content type object. */
	{
	CContentType* self=new(ELeave) CContentType(KNullUid);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();	// self
	return(self);
	}

EXPORT_C CContentType* CContentType::NewL(TFieldType aFieldType,TUid aMapping)
/** Allocates and constructs a new CContentType with a single field 
type and a mapping.

@param aFieldType The field type to add to the content type.
@param aMapping The mapping.
@return Pointer to the newly created content type object. */
	{
	CContentType* self=new(ELeave) CContentType(aMapping);
	CleanupStack::PushL(self);
	self->ConstructL();
	self->AddFieldTypeL(aFieldType);
	CleanupStack::Pop();	// self
	return(self);
	}

EXPORT_C CContentType* CContentType::NewL(const CContentType &aContentType)
/** Allocates and constructs a new CContentType based on another one.

@param aContentType The CContentType on which to base the new one.
@return Pointer to the newly created content type object. */
	{ // static
	CContentType* self=new(ELeave) CContentType;
	CleanupStack::PushL(self);
	self->ConstructL();
	self->CloneL(aContentType);
	CleanupStack::Pop();	// self
	return self;
	}
 
void CContentType::ConstructL()
	{
	iFieldTypes=new(ELeave) CArrayFixFlat<TUid>(2);
	}

EXPORT_C void CContentType::AddFieldTypeL(TFieldType aFieldType)
/** Appends a field type to the content type's list of field types.

Note that certain combinations of field types are not valid and should not be
used.

@param aFieldType The field type to append to the list of field types. */
	{
	const TInt KMaxFieldTypes=15;
	if (iFieldTypes->Count()==KMaxFieldTypes)
		{
		User::Leave(KErrOverflow);
		}
    		
   	iFieldTypes->AppendL(aFieldType);
	}

EXPORT_C void CContentType::RemoveFieldType(TFieldType aFieldType)
/** Removes a field type from the list of field types.

@param aFieldType The field type to remove from the list of field types. */
	{
	for(TInt loop=0;loop<iFieldTypes->Count();loop++)
		if ((*iFieldTypes)[loop]==aFieldType)
			{
			iFieldTypes->Delete(loop);
			break;
			}
	}

EXPORT_C void CContentType::SetMapping(TUid aMapping)
/** Sets the vCard mapping.

@param aMapping The new vCard mapping for the content type. */
	{
	iMapping=aMapping;
	}

void CContentType::CloneL(const CContentType &aContentType)
	{
    iMapping=aContentType.iMapping;
	if (aContentType.iFieldTypes)
		for(TInt loop=0;loop<aContentType.iFieldTypes->Count();loop++)
			AddFieldTypeL((*aContentType.iFieldTypes)[loop]);
	}

EXPORT_C TInt CContentType::FieldTypeCount() const
/** Gets the number of field types in the content type's list of field types.

@return The number of field types in the content type. */
	{
	return(iFieldTypes->Count());
	}

EXPORT_C TFieldType CContentType::FieldType(TInt aIndex) const
/** Gets the indexed field type.

@param aIndex Index into the list of field types. The position is relative 
to zero; i.e. zero implies the first element in the array. This value must 
be non-negative and less than the number of objects currently within the array 
otherwise the operator raises a panic.
@return The indexed field type. */
	{
	__ASSERT_DEBUG(aIndex<iFieldTypes->Count(),Panic(ECntPanicFieldTypeIndex));
	return((*iFieldTypes)[aIndex]);
	}

EXPORT_C TUid CContentType::Mapping() const
/** Gets the vCard mapping.

@return The vCard mapping. */
	{
	return(iMapping);
	}

EXPORT_C TBool CContentType::ContainsFieldType(TFieldType aFieldType) const
/** Tests whether the content type object contains the specified field type UID 
either as the mapping value or in its list of field types.

@param aFieldType The field type of interest.
@return ETrue if the CContentType contains the specified field type. EFalse 
if not. */
	{
	if (iMapping==aFieldType)
		return(ETrue);
	for(TInt loop=0;loop<iFieldTypes->Count();loop++)
		if ((*iFieldTypes)[loop]==aFieldType)
			return(ETrue);
	return(EFalse);
	}

void CContentType::InternalizeAdditionalUidsL(TInt aCount,RReadStream& aStream)
	{
	for (TInt ii=0;ii<aCount;ii++) 
		iFieldTypes->AppendL(TUid::Uid(aStream.ReadInt32L()));
	}

EXPORT_C TBool CContentType::SupportsMultipleLines() const
/** Tests whether the content type supports multiple lines of text. If the content 
type object contains a field type which supports this, either in its list 
of field types, or as its mapping, the function returns ETrue. Examples of 
such field types are address fields (type KUidContactFieldAddress) and note 
fields (type KUidContactFieldNote).

@return ETrue if the CContentType supports multiple lines of text. EFalse 
if not. */
	{
	return(ContainsFieldType(KUidContactFieldAddress) || ContainsFieldType(KUidContactFieldNote));
	}

EXPORT_C TBool CContentType::operator==(const CContentType &aType) const
/** Compares two content type objects for equality. 
Two content type objects are equal according to this method if all following 
conditions are met:
- vCard property mapping matches
- same number of field types
- main field type matches
- additional field types and vCard property parameter mappings match
 
@param aType The content type to compare with this CContentType.
+@return ETrue if aType is equal with current content type. */
	{
	// Compare vCard property mapping.
	if (Mapping() != aType.Mapping())
		{
		return EFalse;
		}
	
	// Compare field type counts.
	const TInt typeCount = FieldTypeCount();
	if (typeCount != aType.FieldTypeCount())
		{
		return EFalse;
		}
	if (typeCount <= 0)
		{
		// Nothing more to compare.
		return ETrue;
		}
	
	// Compare main field type.
	if (FieldType(0) != aType.FieldType(0))
		{
		return EFalse;
		}
	
	// Compare additional field types and vCard property parameter mappings.
	for (TInt lhsIndex = 1; lhsIndex < typeCount; ++lhsIndex)
		{
		TInt rhsIndex = 1;
		for (rhsIndex = 1; rhsIndex < typeCount; ++rhsIndex)
			{
			if (FieldType(lhsIndex) == aType.FieldType(rhsIndex))
			    break;
			}
		if (rhsIndex == typeCount)
			{
			// No match found.
			return EFalse;
			}
		}
	
	return ETrue;
	}

EXPORT_C TBool CContentType::IsEqual(const CContentType &aType) const
/** Compares two content type objects for equality.
Two content type objects are equal according to this method if all following 
conditions are met:
- vCard property mapping matches
- same number of field types
- main field type matches
- additional field types and vCard property parameter mappings match
 
@param aType The content type to compare with this CContentType.
@return ETrue if aType is equal with current content type. */
	{
	// Compare vCard property mapping.
	if (iMapping!=aType.iMapping)
		{
		return(EFalse);	
		}
	
	// Compare field type counts.
	const TInt typeCount = aType.FieldTypeCount();
	if (FieldTypeCount()!= typeCount)
		{
		return(EFalse);	
		}
		
    if (typeCount <= 0)
        {
        // Nothing more to compare.
        return ETrue;
        }
			
    // Compare main field type.
    if (FieldType(0) != aType.FieldType(0))
        {
        return EFalse;
        }

    // Compare additional field types and vCard property parameter mappings.
    for (TInt leftIndex = 1; leftIndex < typeCount; ++leftIndex)
        {
        TUint rightIndex = 1;
        for (rightIndex = 1; rightIndex < typeCount; ++rightIndex)
            {
            if (FieldType(leftIndex) == aType.FieldType(rightIndex))
                break;
            }
        if (rightIndex == typeCount)
            {
            // No match found.
            return EFalse;
            }
        }

    return ETrue;
   	}
  	
void CContentType::Reset()
	{
	iMapping=TUid::Uid(0);
	iFieldTypes->Reset();
	}


/*
 * Determine if content types are suitable to match for synchronization purpose. 
 * Some properties are subject to specific processing depending of the <code>CContentType</code> Mapping. 
 * Notably: VOICE, PREF, VoiceDial and SpeedDial. 
 * VOICE is a a default property parameter for all TEL properties.
 *
 * @param		"const CContentType& aType"
 *				The content type to compare with this
 *				<code>CContentType</code>. Beware, to do a proper comparison,
 *				the <code>aType</code> parameter must not contains VoiceDial or SpeedDial properties.
 *				Otherwise the fields will not match.				  
 *
 * @return 		"TBool"
 *				<code>ETrue</code> if <code>aType</code> match this content type.
 *				Field types do not need to be in the same order in the 
 *				list of field types for a match to be made.
 */
EXPORT_C TBool CContentType::IsEqualForSyncUpdate(const CContentType& aType) const
/** Tests whether the content types are suitable to match for synchronisation purpose. 

@param aType The content type to compare with this CContentType.
@return ETrue if aType is an identical content type
 */
	{
	if (iMapping!=aType.iMapping)
		return(EFalse);	

	switch (iMapping.iUid)
		{
	case KIntContactFieldVCardMapTEL:
		{
		TInt countModifier=0;
		TInt loop=0;

		if (aType.ContainsFieldType(KUidContactFieldVCardMapVOICE))
			{
			++countModifier;
			}

		if (aType.ContainsFieldType(KUidContactFieldVCardMapPREF))
			{
			++countModifier;
			}
		
		if (ContainsFieldType(KUidContactFieldVCardMapVOICE))
			{
			--countModifier;
			}

		if (ContainsFieldType(KUidContactFieldVCardMapPREF))
			{
			--countModifier;
			}

		if (ContainsFieldType(KUidContactsVoiceDialField))
			{
			--countModifier;
			}

		//look for speed dial property only in db field
		for(loop=0;loop<iFieldTypes->Count();loop++)
			{
			TInt uid = (*iFieldTypes)[loop].iUid;
			if (uid >= KUidSpeedDialOneValue && uid <= KUidSpeedDialNineValue)
				{
				--countModifier;
				break; //only one speed dial property allowed for a field
				}
			}
	
		//now check for field count	
		if ((FieldTypeCount()+countModifier)!=aType.FieldTypeCount())
			{
			return(EFalse);
			}
				
		//Check that all type from the imported field match a type in this field but VOICE and PREF
		for(loop=0;loop<aType.FieldTypeCount();loop++)
			{
			if (!ContainsFieldType(aType.FieldType(loop)))
				{
				if (aType.FieldType(loop).iUid==KIntContactFieldVCardMapVOICE || aType.FieldType(loop).iUid==KIntContactFieldVCardMapPREF)
					{
					continue;
					}
				return(EFalse);
				}
			}
		return(ETrue);
		}

	case KIntContactFieldVCardMapEMAILINTERNET:
		{
		TInt countModifier=0;		

		if (aType.ContainsFieldType(KUidContactFieldVCardMapPREF))
			{
			++countModifier;
			}
		
		if (ContainsFieldType(KUidContactFieldVCardMapPREF))
			{
			--countModifier;
			}
	
		//now check for field count	
		if ((FieldTypeCount()+countModifier)!=aType.FieldTypeCount())
			{
			return(EFalse);
			}
				
		//Check that all type from the imported field match a type in this field but PREF
		for(TInt loop=0;loop<aType.FieldTypeCount();loop++)
			{
			if (!ContainsFieldType(aType.FieldType(loop)))
				{
				if (aType.FieldType(loop).iUid==KIntContactFieldVCardMapPREF)
					{
					continue;
					}
				return(EFalse);
				}
			}
		return(ETrue);
		}
	
	case KIntContactFieldVCardMapAGENT:
		{
		return(ETrue);
		}	

	default:
		return *this==aType;
		}
	}
	
void CContentType::InternalizeL(RReadStream& aStream)
/** Internalises a CContentType object from a read stream. 
@param aStream Stream from which the object should be internalised. */
	{
    // TUid iMapping;
	iMapping.iUid = aStream.ReadInt32L();
	
	// CArrayFix<TUid>* iFieldTypes;
	const TInt count=aStream.ReadInt32L();

	// Allocated in constructor
	TUid tempID;
	for(TInt index=0; index<count; ++index)
		{
		
		aStream>>tempID;		
		iFieldTypes->AppendL(tempID);
		}
	}

void CContentType::ExternalizeL(RWriteStream& aStream) const 
/** Externalises a CContentType object to a write stream.
@param aStream Stream to which the object should be externalised. */
	{
	aStream.WriteInt32L(iMapping.iUid);
	
	const TInt count = iFieldTypes->Count();
	aStream.WriteInt32L(count);	
	for(TInt index=0; index<count; ++index)
		{
		aStream<<iFieldTypes->At(index);
		}
	}		


//
// class CContactItemField
//

EXPORT_C CContactItemField* CContactItemField::NewLC()
/** Allocates and constructs a new default contact item field. 

The field's storage type, content type and label are unspecified. 
The ESynchronize attribute is set.

@return Pointer to the newly created contact item field. */
	{
    CContactItemField* self=new(ELeave) CContactItemField;
    CleanupStack::PushL(self);
	self->iContentType=CContentType::NewL();
    return self;
	}

EXPORT_C CContactItemField* CContactItemField::NewL(TStorageType aType)
/** Allocates and constructs a contact item field with a storage type. 

The field's label and content type are unspecified.

@param aType The field's storage type.
@return Pointer to the newly created contact item field. */
	{
    CContactItemField* self=CContactItemField::NewLC(aType);
    CleanupStack::Pop(); // self
    return self;
	}

EXPORT_C CContactItemField* CContactItemField::NewLC(TStorageType aType)
/** Allocates and constructs a contact item field with a storage type. 

The field's label and content type are unspecified.

@param aType The field's storage type.
@return Pointer to the newly created contact item field. This is left on 
the cleanup stack. */
	{
    CContactItemField* self=new(ELeave) CContactItemField(aType);
    CleanupStack::PushL(self);
    self->ConstructStorageL();
	self->iContentType=CContentType::NewL();
    return self;
	}

EXPORT_C CContactItemField* CContactItemField::NewL(TStorageType aType, TFieldType aFieldType)
/** Allocates and constructs a contact item field with a storage type 
and a field type.

The field's content type is initialised with the field type, 
and its vCard mapping is set by default to KNullUid. The field's label is 
unspecified.

@param aType The field's storage type.
@param aFieldType The field type as defined in cntdef.h.
@return Pointer to the newly created contact item field. */
    { // static
    CContactItemField* self=CContactItemField::NewLC(aType,aFieldType);
    CleanupStack::Pop(); // self
    return self;
    }

EXPORT_C CContactItemField* CContactItemField::NewLC(TStorageType aType, TFieldType aFieldType)
/** Allocates and constructs a contact item field with a storage type 
and a field type.

The field's content type is initialised with the field type, 
and its vCard mapping is set by default to KNullUid. The field's label is 
unspecified.

@param aType The field's storage type.
@param aFieldType The field type as defined in cntdef.h.
@return Pointer to the newly created contact item field. This is left on 
the cleanup stack. */
    { // static
    CContactItemField* self=new(ELeave) CContactItemField(aType);
    CleanupStack::PushL(self);
    self->ConstructStorageL();
	self->iContentType=CContentType::NewL(aFieldType);
    return self;
    }

EXPORT_C CContactItemField* CContactItemField::NewL(const CContactItemField &aField)
/** Allocates and constructs a contact item field based on another one.

All details (content type, storage type, attributes and label) are copied 
from the specified field.

@param aField The contact field to copy.
@return Pointer to the newly created contact item field. */
	{ // static
	CContactItemField* self=CContactItemField::NewLC(aField);
	CleanupStack::Pop(); // self
	return self;
	}

EXPORT_C CContactItemField* CContactItemField::NewLC(const CContactItemField &aField)
/** Allocates and constructs a contact item field based on another one.

All details (content type, storage type, attributes and label) are copied 
from the specified field.

@param aField The contact field to copy.
@return Pointer to the newly created contact item field. This is left on 
the cleanup stack. */
	{ // static
	CContactItemField* self=new(ELeave) CContactItemField(aField.StorageType());
	CleanupStack::PushL(self);
	self->ConstructStorageL();
	self->CloneL(aField);
	return self;
	}
 
EXPORT_C CContactItemField* CContactItemField::NewL(TStorageType aType, const CContentType &aContentType)
/** Allocates and constructs a contact item field with a content type 
and a storage type. 

The field's label is unspecified.

@param aType The field's storage type.
@param aContentType The field's content type. 
@return Pointer to the newly created contact item field. */
	{ // static
	CContactItemField* self=CContactItemField::NewLC(aType,aContentType);
	CleanupStack::Pop(); // self
	return self;
	}

EXPORT_C CContactItemField* CContactItemField::NewLC(TStorageType aType, const CContentType &aContentType)
/** Allocates and constructs a contact item field with a content type 
and a storage type. 

The field's label is unspecified.

@param aType The field's storage type.
@param aContentType The field's content type. 
@return Pointer to the newly created contact item field. This is left on 
the cleanup stack. */
	{ // static
	CContactItemField* self=new(ELeave) CContactItemField(aType);
	CleanupStack::PushL(self);
	self->ConstructStorageL();
	self->iContentType=CContentType::NewL(aContentType);
	return self;
	}
 
CContactItemField::CContactItemField(TStorageType aType)
	: iStorageType(aType), iAttributes(ESynchronize|EOverRidesLabel|ELabelUnspecified),iTemplateFieldId(KNullFieldId)
    {}

CContactItemField::CContactItemField()
	: iAttributes(ESynchronize),iTemplateFieldId(KNullFieldId)
    {}

void CContactItemField::ConstructStorageL()
    {
	__ASSERT_DEBUG(iStorage==NULL,Panic(ECntPanicStorageAlreadyAllocated));
    switch (iStorageType)
        {
    case KStorageTypeText:
        iStorage=new(ELeave) CContactTextField;
        break;
    case KStorageTypeStore:
        iStorage=new(ELeave) CContactStoreField;
        break;
    case KStorageTypeDateTime:
        iStorage=new(ELeave) CContactDateField;
        break;
    case KStorageTypeContactItemId:
        iStorage=new(ELeave) CContactAgentField;
        break;
    default:
    	User::Leave(KErrNotSupported);
    	break;
        }
    }

void CContactItemField::CloneL(const CContactItemField &aField)
	{
	iContentType=CContentType::NewL(aField.ContentType());
	if (aField.iLabel)
		iLabel=aField.iLabel->AllocL();
	iAttributes=aField.iAttributes;
	// copy extended attributes as well
	iExtendedAttributes=aField.iExtendedAttributes; 
	iTemplateFieldId=aField.iTemplateFieldId;
	CopyStorageL(aField);
	}

void CContactItemField::CopyStorageL(const CContactItemField &aField)
	{
	switch(aField.StorageType())
		{
		case KStorageTypeText:
			TextStorage()->SetTextL(aField.TextStorage()->Text());
			break;
		case KStorageTypeDateTime:
			DateTimeStorage()->SetTime(aField.DateTimeStorage()->Time());
			break;
		case KStorageTypeContactItemId:
			AgentStorage()->SetAgentId(aField.AgentStorage()->Value());
			break;
		case KStorageTypeStore:
			if (aField.StoreStorage()->Thing())
				{
				StoreStorage()->SetThingL(*aField.StoreStorage()->Thing());
				}
			break;
		}
	}

EXPORT_C CContactItemField::~CContactItemField()
/** Frees all resources owned by the field (the label, the stored 
data and the content type), prior to its destruction. */
    {
    delete iLabel;
    delete iStorage;
	delete iContentType;
    }

/**
@internalTechnology
  */
EXPORT_C void CContactItemField::Reset()
	{
    delete iLabel;
	iLabel=NULL;
    delete iStorage;
	iStorage=NULL;
	iContentType->Reset();
	iStorageType=0;
	iId=KNullFieldId	;
	iAttributes=ESynchronize;
	iExtendedAttributes=0;
	iTemplateFieldId=KNullFieldId;
	}

void CContactItemField::MapHintsToFieldTypesL(THint aHint)
	{
	if (aHint.IsPhone())
		AddFieldTypeL(KUidContactFieldPhoneNumber);
	if (aHint.IsMsg())
		AddFieldTypeL(KUidContactFieldMsg);
	if (aHint.IsCompanyName())
		AddFieldTypeL(KUidContactFieldCompanyName);
	if (aHint.IsFamilyName())
		AddFieldTypeL(KUidContactFieldFamilyName);
	if (aHint.IsGivenName())
		AddFieldTypeL(KUidContactFieldGivenName);
	if (aHint.IsCompanyNamePronunciation())
		AddFieldTypeL(KUidContactFieldCompanyNamePronunciation);
	if (aHint.IsFamilyNamePronunciation())
		AddFieldTypeL(KUidContactFieldFamilyNamePronunciation);
	if (aHint.IsGivenNamePronunciation())
		AddFieldTypeL(KUidContactFieldGivenNamePronunciation);
	if (aHint.IsAddress())
		AddFieldTypeL(KUidContactFieldAddress);
	if (aHint.IsAdditionalName())
		AddFieldTypeL(KUidContactFieldAdditionalName);
	if (aHint.IsSuffixName())
		AddFieldTypeL(KUidContactFieldSuffixName);
	if (aHint.IsPrefixName())
		AddFieldTypeL(KUidContactFieldPrefixName);
	if (aHint.IsEmail())
		AddFieldTypeL(KUidContactFieldEMail);
	if (aHint.IsStorageInline())
		AddFieldTypeL(KUidContactStorageInline);
	}

TBool CContactItemField::AddFieldToHint(TFieldType aFieldType, CContactItemField::THint &aHint) const 
  	{
	TBool matchedHint = ETrue;
	switch(aFieldType.iUid)
		{
		case KUidContactFieldAddressValue:
			aHint.SetIsAddress();
			break;
		case KUidContactFieldCompanyNameValue:
			aHint.SetIsCompanyName();
			break;
		case KUidContactFieldPhoneNumberValue:
			aHint.SetIsPhone();
			break;
		case KUidContactFieldGivenNameValue:
			aHint.SetIsGivenName();
			break;
		case KUidContactFieldFamilyNameValue:
			aHint.SetIsFamilyName();
			break;
		case KUidContactFieldCompanyNamePronunciationValue:
			aHint.SetIsCompanyNamePronunciation();
			break;
		case KUidContactFieldGivenNamePronunciationValue:
			aHint.SetIsGivenNamePronunciation();
			break;
		case KUidContactFieldFamilyNamePronunciationValue:
			aHint.SetIsFamilyNamePronunciation();
			break;
		case KUidContactFieldAdditionalNameValue:
			aHint.SetIsAdditionalName();
			break;
		case KUidContactFieldSuffixNameValue:
			aHint.SetIsSuffixName();
			break;
		case KUidContactFieldPrefixNameValue:
			aHint.SetIsPrefixName();
			break;
		case KUidContactFieldEMailValue:
			aHint.SetIsEmail();
			break;
		case KUidContactFieldMsgValue:
			aHint.SetIsMsg();
			break;
		case KUidContactFieldStorageInlineValue:
			aHint.SetStorageIsInline();
			break;
		default:
			matchedHint=EFalse;
			break;
		}
	return(matchedHint);
	}


/**
Encode contact field data into stream store.

@param aTextStream the text blob stream to export text data.
@param aBlobStore the binary blob stream to export binary data.
@param aTextFieldIndex the index of text field stored in the storage

@return field header which is to be stored into header blob. 
@internalTechnology 
*/
TFieldHeader CContactItemField::StoreL(RWriteStream& aTextStream, CStreamStore& aBlobStore, TInt aTextFieldIndex)
	{
	TStreamId streamId = KNullStreamIdValue;
	if (iStorageType == KStorageTypeText)
		{
		STATIC_CAST(CContactTextField*,iStorage)->ExternalizeL(aTextStream, ETrue, aTextFieldIndex);
		}
	else
	    {
		streamId = iStorage->StoreL(aBlobStore);
	    }
	
	TContactFieldAtts atts;
	/* sets all attributes of the field:- hidden private etc.. + type (text etc..)
	   into TContactFieldAtts which is then stored ahead of the field data */
	atts.SetAttribs(iAttributes);
	atts.SetExtendedAttribs(iExtendedAttributes);
	atts.SetType(iStorageType);
    return TFieldHeader(atts, iId, streamId);
	}


/**
Decode given blob header stream into contact field relevent data.

@param aStream reference to the blob header stream to be decoded.
@param aSystemTemplateFields cached template fields.

@return the stream id of content data stored in data store 
@internalTechnology 
*/
EXPORT_C TStreamId CContactItemField::RestoreFieldTypesL(RReadStream &aStream, const CContactItemFieldSet *aSystemTemplateFields)
  	{
	THint hint;
	TStreamId nestedId(KNullStreamIdValue);
	TContactFieldAtts fieldAtts;

	aStream >> fieldAtts;
	
	iAttributes = fieldAtts.Attribs();
	iExtendedAttributes = fieldAtts.ExtendedAttribs();
	iStorageType = fieldAtts.Type();
	
    if(fieldAtts.Type() != KStorageTypeText)
        {
   	    //Only import stream id when the storage type is not text
        aStream >> nestedId;
        }
        
    iId = aStream.ReadUint32L();
    iTemplateFieldId = aStream.ReadUint32L();
    
	const CContactItemField* templateField = NULL;
	
	if (UsesTemplateTypes() || !OverRidesLabel())
		{
		if (aSystemTemplateFields == NULL)
		    {
			User::Leave(KErrCorrupt);
		    }
		templateField = aSystemTemplateFields->FindById(iTemplateFieldId);
		}
		
	if (templateField)
		{
		iAttributes |= (templateField->iAttributes&ETemplateMask);
		}
		
	if (UsesTemplateTypes() && templateField)
		{
		CContentType* newContent=CContentType::NewL(templateField->ContentType());
		delete iContentType;
		iContentType = NULL;
		iContentType=newContent;
		}
	else
		{
  		hint = aStream.ReadInt32L();
  		
 		if(hint.HasVCardMappingUid())
 		    {
        	iContentType->SetMapping(TUid::Uid(aStream.ReadInt32L()));
 		    }
 		    
		MapHintsToFieldTypesL(hint);
 		iContentType->InternalizeAdditionalUidsL(hint.AdditionalUidsNum(),aStream);
		}
		
	ConstructStorageL();
	
	if (OverRidesLabel())
		{
		const TInt length = aStream.ReadInt32L();
		if (length)
		    {
			iLabel = HBufC::NewL(aStream,length);
		    }
		}
	else if(templateField)
	    {
		iLabel = templateField->Label().AllocL();
	    }
		    
	return nestedId;
    }
	

void CContactItemField::RestoreDataL(CStreamStore& aStore,TStreamId aId)
    {
    RStoreReadStream stream;
    stream.OpenLC(aStore,aId);
	iStorage->RestoreL(aStore,stream);
	CleanupStack::PopAndDestroy(); // stream
	}

/**
@internalTechnology 
*/
EXPORT_C void CContactItemField::RestoreTextL(HBufC *aTextStream,TInt aTextFieldIndex)
    {
	__ASSERT_ALWAYS(iStorageType==KStorageTypeText,Panic(ECntPanicInvalidStorageType));
    STATIC_CAST(CContactTextField*,iStorage)->InternalizeL(aTextStream,aTextFieldIndex);
	}

/**
@internalTechnology 
*/
EXPORT_C TBool CContactItemField::RestoreIfMatchL(RReadStream& aStream,const CContactItemFieldDef *aFieldDef, const CContactItemFieldSet *aSystemTemplateFields,HBufC *aTextStream,TInt aTextIndex)
    {
    TStreamId nestedId;
    nestedId=RestoreFieldTypesL(aStream,aSystemTemplateFields);
    TBool match=EFalse;
    if (aFieldDef==NULL)
        match=ETrue;
    else
        {
        for(TInt loop=0;loop<aFieldDef->Count();loop++)
            {
            TUid fieldType=(*aFieldDef)[loop];
            if (fieldType==KUidContactFieldMatchAll || iContentType->ContainsFieldType(fieldType))
                {
                match=ETrue;
                break;
                }
            }
        }
    if (match && iStorageType==KStorageTypeText)
        RestoreTextL(aTextStream,aTextIndex);
    return match;
    }

TBool CContactItemField::RestoreIfMatchL(RReadStream& aRootStream,TFieldType aFieldType, const CContactItemFieldSet *aSystemTemplateFields,HBufC *aTextStream,TInt aTextIndex)
	{
	CContactItemFieldDef* fieldDef=new(ELeave) CContactItemFieldDef();
	CleanupStack::PushL(fieldDef);
	fieldDef->AppendL(aFieldType);
	TBool ret=RestoreIfMatchL(aRootStream,fieldDef, aSystemTemplateFields,aTextStream,aTextIndex);
	CleanupStack::PopAndDestroy();	// fieldDef
	return(ret);
	}

EXPORT_C TStorageType CContactItemField::StorageType() const
/** Gets the field's storage type.

@return The field's storage type. */
    {
    return iStorageType;
    }

EXPORT_C const CContentType &CContactItemField::ContentType() const
/** Gets the field's content type.

@return Reference to the field's content type. */
    {
    return *iContentType;
    }

const CContentType &CContactItemField::TemplateContentType(const CContactItemFieldSet &aSystemTemplateFields) const
	{
	if (UsesTemplateTypes() || OverRidesLabel())
		return(*iContentType);
	const CContactItemField *templateField=aSystemTemplateFields.FindById(iTemplateFieldId);
	if (!templateField)
		return(*iContentType);
	return(templateField->ContentType());
	}

EXPORT_C TPtrC CContactItemField::Label() const
/** Gets the field's label.

@return The field label. If no label has been set, its length is zero. */
    {
    TPtrC label;
    if (iLabel)
        label.Set(*iLabel);
    return label;
    }

EXPORT_C void CContactItemField::ResetStore()
/** Resets the field storage. The field's store is deleted, then re-allocated. */
    {
    delete iStorage;
	iStorage=NULL;
	ConstructStorageL();
	}

EXPORT_C CContactFieldStorage *CContactItemField::Storage() const
/** Gets a pointer to the field's base storage.

Rather than using this function and then casting to a specific storage class, 
one of the following functions should normally be used: TextStorage(), 
StoreStorage(), AgentStorage(), or DateTimeStorage().

@return The field's base storage type. */
    {
    return iStorage;
    }

EXPORT_C CContactTextField *CContactItemField::TextStorage() const
/** Gets a pointer to the field's storage as a CContactTextField. 

If the field's storage type is not KStorageTypeText, this function raises a panic.

@return The field's storage as a CContactTextField*. */
    {
	__ASSERT_ALWAYS(iStorageType==KStorageTypeText,Panic(ECntPanicInvalidStorageType));
    return (CContactTextField *)iStorage;
	}

EXPORT_C CContactStoreField *CContactItemField::StoreStorage() const
/** Gets a pointer to the field's storage as a CContactStoreField. 

This indicates the field data is stored in a descriptor or descriptor array. 
If the field storage type is not KStorageTypeStore, this function raises a panic.

@return Field's storage as a CContactStoreField*. */
    {
	__ASSERT_ALWAYS(iStorageType==KStorageTypeStore,Panic(ECntPanicInvalidStorageType));
    return (CContactStoreField *)iStorage;
	}

EXPORT_C CContactAgentField *CContactItemField::AgentStorage() const
/** Gets a pointer to the field's storage as a CContactAgentField. 

An agent is a property in a vCard which contains another person's contact details. 
If the field storage type is not KStorageTypeContactItemId, this function raises 
a panic.

@return Field's storage as a CContactAgentField*. */
    {
	__ASSERT_ALWAYS(iStorageType==KStorageTypeContactItemId,Panic(ECntPanicInvalidStorageType));
    return (CContactAgentField *)iStorage;
	}

EXPORT_C CContactDateField *CContactItemField::DateTimeStorage() const
/** Returns a pointer to the field's storage as a CContactDateField. 

If the field storage type is not KStorageTypeDateTime, this function raises a panic.

@return Field's storage as a CContactDateField*. */
    {
	__ASSERT_ALWAYS(iStorageType==KStorageTypeDateTime,Panic(ECntPanicInvalidStorageType));
    return (CContactDateField *)iStorage;
	}

EXPORT_C void CContactItemField::AddFieldTypeL(TFieldType aFieldType)
/** Appends a field type to the field's content type.

Note that certain combinations of field types are not valid and should not be
used.

@param aFieldType The field type to append to the field's content type. */
    {
    iContentType->AddFieldTypeL(aFieldType);
	}

EXPORT_C void CContactItemField::RemoveFieldType(TFieldType aFieldType)
/** Removes a field type from the field's content type.

@param aFieldType The field type to remove from the field's content type. */
	{
    iContentType->RemoveFieldType(aFieldType);
	}

EXPORT_C void CContactItemField::SetMapping(TUid aMapping)
/** Sets the vCard mapping for the field's content type.

@param aMapping The new mapping for the field's content type. */
    {
    iContentType->SetMapping(aMapping);
    }

EXPORT_C void CContactItemField::SetHidden(TBool aHidden)
/** Sets the value of the hidden attribute. 

If hidden fields are included in the view definition, the field is displayed 
like other fields. If the view definition masks hidden fields, it is not displayed. 
See the TMode enumeration defined in class CContactItemViewDef.

@param aHidden ETrue for hidden, EFalse for displayed. */
    {
    if (aHidden)
        iAttributes|=EHidden;
    else
        iAttributes&=~EHidden;
    }

EXPORT_C void CContactItemField::SetReadOnly(TBool aReadOnly)
/** Sets the value of the field's read only attribute.

@param aReadOnly ETrue to set the field's read only attribute, EFalse to unset 
the attribute. */
    {
    if (aReadOnly)
        iAttributes|=EReadOnly;
    else
        iAttributes&=~EReadOnly;
    }

EXPORT_C void CContactItemField::SetSynchronize(TBool aSynchronize)
/** Sets the value of the field's synchronise attribute.

@param aSynchronize ETrue to set synchronise attribute, EFalse to unset it. */
    {
    if (aSynchronize)
        iAttributes|=ESynchronize;
    else
        iAttributes&=~ESynchronize;
    }

EXPORT_C void CContactItemField::SetDisabled(TBool aDisabled)
/** Sets the value of the disabled attribute.

@param aDisabled ETrue to set the disabled attribute, EFalse to unset the 
attribute. */
    {
    if (aDisabled)
        iAttributes|=EDisabled;
    else
        iAttributes&=~EDisabled;
    }

/** 
@internalComponent
*/
void CContactItemField::UsesTemplateData(TInt aTemplateFieldId)
	{
	iTemplateFieldId=aTemplateFieldId;
	}

void CContactItemField::SetLabelUnspecified(TBool aUnspecified)
	{
	if (aUnspecified)
		iAttributes|=ELabelUnspecified;
	else
		iAttributes&=~ELabelUnspecified;
	}

EXPORT_C void CContactItemField::SetUserAddedField(TBool aUserAddedField)
/** Sets the user added field attribute.

@param aUserAddedField ETrue to set the field's user added attribute, EFalse 
to unset it. */
	{
	if (aUserAddedField)
		iAttributes|=EUserAddedField;
	else
		iAttributes&=~EUserAddedField;
	}

EXPORT_C void CContactItemField::SetTemplateField(TBool aTemplateField)
/** Sets whether the field is a template field.

@param aTemplateField ETrue to set the field's Is template attribute. EFalse 
to unset it. */
	{
	if (aTemplateField)
		iAttributes|=ETemplate;
	else
		iAttributes&=~ETemplate;
	}

EXPORT_C void CContactItemField::SetPrivate(TBool aPrivateField)
/** Sets the value of the field's private attribute. 

This is used by the contact database when exporting a contact item as a vCard, 
to identify fields which should not be exported.

@param aTemplateField ETrue to set the field's private attribute, EFalse to 
unset it. */
	{
	if (aPrivateField)
		iExtendedAttributes|=EPrivate;
	else
		iExtendedAttributes&=~EPrivate;
	}

EXPORT_C void CContactItemField::SetSpeedDial(TBool aSpeedDialField)
/** Sets the value of the field's speed dial attribute.

@param aSpeedDialField ETrue if the field should be a speed dial field, EFalse 
if not. */
	{
	if (aSpeedDialField)
		iExtendedAttributes|=ESpeedDial;
	else
		iExtendedAttributes&=~ESpeedDial;
	}

/**
@internalTechnology 
*/
EXPORT_C void CContactItemField::SetCustomFilterable(EContactFieldFlags aContactFilterType)
	{
	if (aContactFilterType == EContactFieldFlagFilterable) 
		{
 		iExtendedAttributes |= EUserDefinedFilter;
 		}
  	else if (aContactFilterType == EContactFieldFlagFilterable1) 
 		{
 		iExtendedAttributes |= EUserDefinedFilter1;
 		}
 	else if (aContactFilterType == EContactFieldFlagFilterable2) 
 		{
 		iExtendedAttributes |= EUserDefinedFilter2;
 		}
 	else if (aContactFilterType == EContactFieldFlagFilterable3) 
 		{
 		iExtendedAttributes |= EUserDefinedFilter3;
 		}
 	else if (aContactFilterType == EContactFieldFlagFilterable4) 
 		{
 		iExtendedAttributes |= EUserDefinedFilter4;
 		}
	}

/** 
Determine if a custom filter exists. If it does, return the filter type.

@param aContactFieldFlag The custom filter type if one exists.
@return ETrue if custom filter exists.
@internalTechnology 
 */	
EXPORT_C TBool CContactItemField::HasCustomFilter(EContactFieldFlags& aContactFieldFlag) const
	{
	if (iExtendedAttributes & EUserDefinedFilter) 
 		{
 		aContactFieldFlag = EContactFieldFlagFilterable;
 		return ETrue;
 		}
	else if (iExtendedAttributes & EUserDefinedFilter1) 
 		{
 		aContactFieldFlag = EContactFieldFlagFilterable1;
 		return ETrue;
 		}
 	else if (iExtendedAttributes & EUserDefinedFilter2) 
 		{
 		aContactFieldFlag = EContactFieldFlagFilterable2;
 		return ETrue;
 		}
 	else if (iExtendedAttributes & EUserDefinedFilter3) 
 		{
 		aContactFieldFlag = EContactFieldFlagFilterable3;
 		return ETrue;
 		}
 	else if (iExtendedAttributes & EUserDefinedFilter4) 
 		{
 		aContactFieldFlag = EContactFieldFlagFilterable4;
 		return ETrue;
 		}
 		
 	return EFalse;
  	}
  	

void CContactItemField::UsesTemplateLabel()
	{
	iAttributes&=~EOverRidesLabel;
	SetLabelUnspecified(EFalse);
	}

void CContactItemField::SetOverRidesLabel(TBool aValue)
	{
	if (aValue)
		{
		iAttributes|=EOverRidesLabel;
		}
	else
		{
		iAttributes&=~EOverRidesLabel;
		}
	}

void CContactItemField::SetUsesTemplateTypes(TBool aUsesTemplateTypes)
	{
	if (aUsesTemplateTypes)
		iAttributes|=EUsesTemplateData;
	else
		iAttributes&=~EUsesTemplateData;
	}

EXPORT_C void CContactItemField::SetLabelL(const TDesC& aLabel)
/** Sets the field label. 

The label is allocated using TDesC::AllocL(), so the function can leave. 
Any existing label is replaced.

@param aLabel The new field label. */
    {
	SetLabel(aLabel.AllocL());
    }

EXPORT_C void CContactItemField::SetLabel(HBufC* aLabel)
/** Sets the field label. 

The field takes ownership of aLabel so the function cannot leave.

@param aLabel The new field label. */
	{
	delete iLabel;
	iLabel=aLabel;
	SetLabelUnspecified(EFalse);
	iAttributes|=EOverRidesLabel;
	}

EXPORT_C void CContactItemField::SetId(TInt aId)
/** Sets the ID which uniquely identifies a field within a field set..

Note that the field ID value is initialised when the field is added to the 
field set (using CContactItemFieldSet::AddL()). It is equivalent to the field's 
index within the field set array, and should not normally be changed.

@param aId The new field ID. */
	{
	iId=aId;
	}

EXPORT_C TInt CContactItemField::Id() const
/** Gets the field ID.

@return The field ID. */
	{
	return(iId);
	}

EXPORT_C TUint CContactItemField::UserFlags() const
/** Gets the value of the user flags, as set by SetUserFlags().

@return The user flags value. */
	{
	return((iAttributes&EUserMask)>>EUserMaskShift);
	}

EXPORT_C void CContactItemField::SetUserFlags(TUint aFlags)
/** Sets the value of the user flags.

@param aFlags The user flags value. */
	{
	iAttributes&=~EUserMask;
	iAttributes|=((aFlags<<EUserMaskShift)&EUserMask);
	}

TInt CContactItemField::TemplateFieldId() const
	{
	return iTemplateFieldId;
	}

void CContactItemField::SetDeleted(TBool aDeleted)
	{
	if (aDeleted)
		iAttributes|=STATIC_CAST(TUint, EDeleted); 
	else
		iAttributes&=~EDeleted;
	}

void CContactItemField::UpdateFieldFlags(const CContactItemFieldSet& aTemplateFieldSet)
	{
	TInt templatePosition;
	TBool exactMatch;

	if (!Storage()->IsFull() && !IsTemplate())
		{
		SetDeleted(ETrue);
		}
	else
		{
		SetDeleted(EFalse);
		templatePosition = aTemplateFieldSet.MatchTemplateField(ContentType(),UserFlags(),exactMatch);
		if (templatePosition != KErrNotFound)
			{
			const CContactItemField& templateField = aTemplateFieldSet[templatePosition];
			UsesTemplateData(templateField.Id());
			SetUsesTemplateTypes(exactMatch);
			if (LabelUnspecified() || templateField.Label().CompareF(Label())==0)
				{
				UsesTemplateLabel();
				}
			}
		}
	SetLabelUnspecified(EFalse);
	}

/** 
@internalTechnology 
 */
EXPORT_C void CContactItemField::GetFieldText(TDes &aText) const 
	{
	if (StorageType()==KStorageTypeText)
		{
		TPtrC text=TextStorage()->Text();
		TInt length=text.Length();
		const TInt carriageReturn=13;
		const TInt lineFeed=10;
		for(TInt loop=0;loop<length;loop++)
  			if ((TChar(text[loop])==carriageReturn) || (TChar(text[loop])==lineFeed))
				{
				length=loop;
				break;
				}
		if (length>KTextFieldMinimalLength)
			length=KTextFieldMinimalLength;
		aText.Copy(text.Left(length));
		}
	}

EXPORT_C TBool CContactItemField::IsValidLabel(const TDesC& aLabel,TInt& aInvalidPos)
/** Tests whether a field label is valid.

Note: the label is invalid if it contains any of the following characters: 

[] (left or right square bracket) 

= (equals sign)

. (dot) 

: (colon) 

, (comma)

@param aLabel The field label to test.
@param aInvalidPos On return, contains the character position within the label 
of the first invalid character. The first character position is zero.
@return ETrue if valid, EFalse if invalid. */
	{
		return !(((aInvalidPos = aLabel.Locate(KVersitTokenLSquareBracketVal)) != KErrNotFound) ||
		((aInvalidPos = aLabel.Locate(KVersitTokenRSquareBracketVal)) != KErrNotFound) ||
		((aInvalidPos = aLabel.Locate(KVersitTokenEqualsVal)) != KErrNotFound) ||
		((aInvalidPos = aLabel.Locate(KVersitTokenColonVal)) != KErrNotFound) ||
		((aInvalidPos = aLabel.Locate(KVersitTokenPeriodVal)) != KErrNotFound) ||
		((aInvalidPos = aLabel.Locate(KVersitTokenCommaVal)) != KErrNotFound));
	}

EXPORT_C TBool CContactItemField::IsTemplateLabelField() const
/** Tests whether the field is a template label field (a field which holds 
the label for a contact card template: see class CContactCardTemplate).

@return ETrue if the field is a template label field, EFalse if not. */
	{
	return (iContentType->ContainsFieldType(KUidContactFieldTemplateLabel));
	}


/** 
Part of the system template update implementation. 
This could be used for a generic update method at a later stage.
@internalTechnology
*/
void CContactItemField::PopulateStoreL(RStoreWriteStream& aRootStream, TInt aCount, CArrayFix<TFieldHeader>& aFieldHeaderArray) const
	{
	if (!IsDeleted())
		{
		//Store attributes
        aRootStream << aFieldHeaderArray[aCount].FieldAtts();
        
		// since field is in fullFields it cannot be empty, so there is no need to check
        if (StorageType() != KStorageTypeText)
            {
            aRootStream << aFieldHeaderArray[aCount].StreamId();
            }
		
		//Store field id.
		aRootStream.WriteUint32L(iId);
		aRootStream.WriteUint32L(iTemplateFieldId);
        
		CContactItemField::THint hint(0);
        if(!UsesTemplateTypes())
            {
			CArrayFixFlat<TUid>* additionalFields = new(ELeave) CArrayFixFlat<TUid>(5);
			CleanupStack::PushL(additionalFields);
			
			for(TInt loop=0; loop < iContentType->FieldTypeCount(); loop++)
				{
				TFieldType fieldType = iContentType->FieldType(loop);
				if (!AddFieldToHint(fieldType, hint))
				    {
					additionalFields->AppendL(fieldType);
				    }
				}
				
			const TInt KAdditionalUidsCount = additionalFields->Count();
			hint.SetAdditionalUidsNum(KAdditionalUidsCount);
			
            if(iContentType->Mapping() != KNullUid)
                {
                //Store vCard mapping uid if there is a one.
                hint.SetHasVCardMappingUid();
                aRootStream.WriteInt32L(hint.iHintValue);
    			aRootStream.WriteInt32L(iContentType->Mapping().iUid); // mapping
                }
            else
                {
                aRootStream.WriteInt32L(hint.iHintValue);            
                }			
			
			for (TInt ii = 0; ii < KAdditionalUidsCount; ++ii) 
				{
				aRootStream.WriteInt32L((*additionalFields)[ii].iUid); // additional typing uids
				}
			CleanupStack::PopAndDestroy(); // additionalFields
            }    
		
		if (OverRidesLabel())
			{ // store label if not inherited from template
			
			TInt length;
			
			if(iLabel)
				{
				length = iLabel->Length();
				}
			else
				{
				length = 0;
				}			
			
			aRootStream.WriteInt32L(length);	
				
			if (length > 0)
			    {
				aRootStream << *(iLabel);
			    }
			}
		} //if (!IsDeleted())
	}


/** 
Part of the system template update implementation. 
This could be used for a generic update method at a later stage.
@since 7.0
@internalTechnology
*/
void CContactItemField::PrepareFieldAsTemplateL(CContactItemFieldSet& aSystemTemplateFieldSet)
	{
	TInt templatePosition = KErrNotFound;
	TBool exactMatch = EFalse;
	
	if ( !iStorage->IsFull() && !IsTemplate() )
		{
		SetDeleted(ETrue);
		} // if
	else
		{
		SetDeleted(EFalse);
		templatePosition = aSystemTemplateFieldSet.MatchTemplateField(ContentType(), UserFlags(), exactMatch);
		if (templatePosition != KErrNotFound)
			{
			const CContactItemField& systemTemplateField = aSystemTemplateFieldSet[templatePosition];
			UsesTemplateData( systemTemplateField.Id() );
			SetUsesTemplateTypes( exactMatch );
			if( LabelUnspecified() || systemTemplateField.Label().CompareF(Label()) == 0 )
				{
				UsesTemplateLabel();
				} 
			} //if
		} //else

	SetLabelUnspecified(EFalse);
	}

void CContactItemField::InternalizeL(RReadStream& aStream)
/** Internalises a CContactItemField object from a read stream. 
@param aStream Stream from which the object should be internalised. */
	{

	delete iContentType;
	iContentType = NULL;
	iContentType = CContentType::NewL();
	iContentType->InternalizeL(aStream);
	
	iStorageType = aStream.ReadUint32L();
	
	if(iLabel)
		{
		delete iLabel;
		iLabel = NULL;
		}
	const TInt length=aStream.ReadInt32L();
	if (length)
		{
		iLabel=HBufC::NewL(aStream,length);	
		}

	iId = aStream.ReadInt32L();
	
	iAttributes = aStream.ReadUint32L();
		
	iExtendedAttributes = aStream.ReadUint32L();
	
	delete iStorage;
	iStorage = NULL;
	ConstructStorageL();
	iStorage->InternalizeL(aStream);
	
	iTemplateFieldId = aStream.ReadInt32L();		

	}

void CContactItemField::ExternalizeL(RWriteStream& aStream) const 
/** Externalises a CContactItemField object to a write stream.
@param aStream Stream to which the object should be externalised. */
	{
	iContentType->ExternalizeL(aStream);
	
	aStream.WriteUint32L(iStorageType);
	
	if(iLabel)
		{
		
		const TInt length=iLabel->Length();
		aStream.WriteInt32L(length);	
		if (iLabel && length)
			{
			aStream<<*(iLabel);
			}
		}
		
	else
		{
		aStream.WriteInt32L(0);
		}
	
	aStream.WriteInt32L(iId);
	
	aStream.WriteUint32L(iAttributes);
		
	aStream.WriteUint32L(iExtendedAttributes);
	
	iStorage->ExternalizeL(aStream);
	
	aStream.WriteInt32L(iTemplateFieldId);
	}



//
//	class THint
//
CContactItemField::THint::THint() : iHintValue(0)
	{}

CContactItemField::THint::THint(TInt aValue)
	{
	iHintValue = aValue;
	}

TBool CContactItemField::THint::operator==(const THint& aHint) const
	{
	return (iHintValue == aHint.iHintValue);
	}

TBool CContactItemField::THint::operator!=(const THint& aHint) const	
	{
	return (iHintValue != aHint.iHintValue);
	}

TInt  CContactItemField::THint::HintType() const
	{
	return (iHintValue & KHintTypeMask);
	}

TInt CContactItemField::THint::TemplateFieldId() const
    {
    return (iHintValue & KHintTemplateFieldMask);    
    }
    
void  CContactItemField::THint::SetTemplateFieldId(TInt aTemplateFieldId)
    {
    iHintValue |= (aTemplateFieldId & KHintTemplateFieldMask);
    }

inline void CContactItemField::THint::SetHasVCardMappingUid()
    {
    iHintValue |= KHintVCardMappingMask;    
    }
    
inline TBool CContactItemField::THint::HasVCardMappingUid() const
    {
    return (iHintValue & KHintVCardMappingMask);    
    }

inline TInt CContactItemField::THint::AdditionalUidsNum() const
    {
    return ((iHintValue & KHintAdditionalMask) >> KHintAdditionalMaskShift);
    }
    
inline void CContactItemField::THint::SetAdditionalUidsNum(TInt aNumber)
    {
    iHintValue |= ((aNumber << KHintAdditionalMaskShift) & KHintAdditionalMask);       
    }
#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS	
inline void CContactItemField::THint::SetIsPhone()
	{iHintValue|=KIntContactHintIsPhone;}
inline void CContactItemField::THint::SetIsMsg()
	{iHintValue|=KIntContactHintIsMsg;}
// turn off Pronunciation bit too?
inline void CContactItemField::THint::SetIsCompanyName()
	{iHintValue|=KIntContactHintIsCompanyName;}
inline void CContactItemField::THint::SetIsFamilyName()
	{iHintValue|=KIntContactHintIsFamilyName;}
inline void CContactItemField::THint::SetIsGivenName()
	{iHintValue|=KIntContactHintIsGivenName;}
inline void CContactItemField::THint::SetIsCompanyNamePronunciation()
	{iHintValue|=KIntContactHintIsCompanyName|KIntContactHintIsPronunciation;}
inline void CContactItemField::THint::SetIsFamilyNamePronunciation()
	{iHintValue|=KIntContactHintIsFamilyName|KIntContactHintIsPronunciation;}
inline void CContactItemField::THint::SetIsGivenNamePronunciation()
	{iHintValue|=KIntContactHintIsGivenName|KIntContactHintIsPronunciation;}
inline void CContactItemField::THint::SetIsAddress()
	{iHintValue|=KIntContactHintIsAddress;}
inline void CContactItemField::THint::SetIsAdditionalName()
	{iHintValue|=KIntContactHintIsAdditionalName;}
inline void CContactItemField::THint::SetIsSuffixName()
	{iHintValue|=KIntContactHintIsSuffixName;}
inline void CContactItemField::THint::SetIsPrefixName()
	{iHintValue|=KIntContactHintIsPrefixName;}
inline void CContactItemField::THint::SetStorageIsInline()
	{iHintValue|=KIntContactHintStorageInline;}
inline void CContactItemField::THint::SetIsEmail()
	{iHintValue|=KIntContactHintIsEmail;}
inline TBool CContactItemField::THint::IsPhone() const
	{return (iHintValue&KIntContactHintIsPhone);}
inline TBool CContactItemField::THint::IsMsg() const
	{return (iHintValue&KIntContactHintIsMsg);}
inline TBool CContactItemField::THint::IsCompanyName() const
	{return ((iHintValue&KIntContactHintIsCompanyNamePronunciation) == KIntContactHintIsCompanyName);}
inline TBool CContactItemField::THint::IsFamilyName() const
	{return ((iHintValue&KIntContactHintIsFamilyNamePronunciation)==KIntContactHintIsFamilyName);}
inline TBool CContactItemField::THint::IsGivenName() const
	{return ((iHintValue&KIntContactHintIsGivenNamePronunciation)==KIntContactHintIsGivenName);}
inline TBool CContactItemField::THint::IsCompanyNamePronunciation() const
	{return ((iHintValue&KIntContactHintIsCompanyNamePronunciation) == KIntContactHintIsCompanyNamePronunciation);}
inline TBool CContactItemField::THint::IsFamilyNamePronunciation() const
	{return ((iHintValue&KIntContactHintIsFamilyNamePronunciation)==KIntContactHintIsFamilyNamePronunciation);}
inline TBool CContactItemField::THint::IsGivenNamePronunciation() const
	{return ((iHintValue&KIntContactHintIsGivenNamePronunciation)==KIntContactHintIsGivenNamePronunciation);}
inline TBool CContactItemField::THint::IsAddress() const
	{return (iHintValue&KIntContactHintIsAddress);}
inline TBool CContactItemField::THint::IsAdditionalName() const
	{return (iHintValue&KIntContactHintIsAdditionalName);}
inline TBool CContactItemField::THint::IsSuffixName() const
	{return (iHintValue&KIntContactHintIsSuffixName);}
inline TBool CContactItemField::THint::IsPrefixName() const
	{return (iHintValue&KIntContactHintIsPrefixName);}
inline TBool CContactItemField::THint::IsStorageInline() const
	{return (iHintValue&KIntContactHintStorageInline);}
inline TBool CContactItemField::THint::IsEmail() const
	{return (iHintValue&KIntContactHintIsEmail);}
	
#ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__	
inline void CContactItemField::THint::SetHasAdditionalUids()
	{iHintValue|=KHintAdditionalMask;}
#endif //__SYMBIAN_CNTMODEL_USE_SQLITE__ 
inline TBool CContactItemField::THint::Contains(const THint& aHint) const
	{return (iHintValue&aHint.iHintValue);}
#endif	
//
//	class TFieldHeader
//

/*
 * Default constructor
 */
TFieldHeader::TFieldHeader()
: iFieldUid(0),
  iStreamId(KNullStreamIdValue)
    {
    }

/*
 * Overloaded constructor
 */
TFieldHeader::TFieldHeader(TContactFieldAtts aAtts, TUint32 aFieldUid, TStreamId aId)
: iAtts(aAtts),
  iFieldUid(aFieldUid),
  iStreamId(aId)
    {
    }
    
inline TInt TFieldHeader::FieldId() const
	{
	return iFieldUid;
	}

inline void TFieldHeader::SetFieldId(TInt aId) 
	{
    iFieldUid = aId;
	}

inline TContactFieldAtts TFieldHeader::FieldAtts() const
    {
    return iAtts;
    }
    
inline void TFieldHeader::SetFieldAtts(TContactFieldAtts aAtts)
    {
    iAtts = aAtts; 
    }

inline TStreamId TFieldHeader::StreamId() const
    {
    return iStreamId;    
    }
    
inline void TFieldHeader::SetStreamId(TStreamId aId)
    {
    iStreamId = aId;
    }