Symbian
Symbian OS Library

FAQ-0526 How do I find contact fields using a VCard mapping?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: Application Engines
Created: 10/18/2000 Modified: 09/01/2001
Number: FAQ-0526
Platform: ER5, Symbian OS v6.0, Symbian OS v6.1

Question:
How do I find contact fields using a VCard mapping?

Answer:
The CContactItemFieldSet class provides methods to find fields by a field type or by the field type and a mapping.

The first Find() and FindNext() methods provide a means of searching for a field by a field type. The possible field types can be found in the SDK documentation under the heading "Contacts Model Constants."

As an example calling FindNext(KUidContactFieldPhoneNumber, 0) would search for the first field (from 0) containing a phone number, most likely the contacts Mobile number. Repeated FindNext() calls with an updated search position as a second parameter will then find additional phone numbers including the contacts home number, and work number.

This is illustrated by the following code extract which opens the default contacts database, retrieves a contact, and displays all phone numbers within the contact, regardless of location i.e. home/work.

// Find a contact.
CContactItemFieldDef* fields = new (ELeave) CContactItemFieldDef();
CleanupStack::PushL(fields);
fields->AppendL(KUidContactFieldGivenName);
CContactIdArray* currentSel = iDatabase->FindLC(_L("Dan"), fields);
CContactItem* iItem = iDatabase->ReadContactL((*currentSel)[0]);
CleanupStack::PushL(iItem);
CContactItemFieldSet& fieldSet=iItem->CardFields();

// Display general phone numbers
TInt pos = fieldSet.FindNext(KUidContactFieldPhoneNumber,0);
while (pos != KErrNotFound)
{
CContactItemField& itemField=fieldSet[pos];

if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
{
CContactTextField* textField = itemField.TextStorage();
iEikonEnv->InfoWinL(itemField.Label(), textField->Text());
}

pos = fieldSet.FindNext(KUidContactFieldPhoneNumber,pos+1);
}

To more explicitly locate a field the second type of Find() and FindNext() methods can be used. These methods take a mapping as a second parameter.

A list of the VCard mappings which can be used in the Find() and FindNext() methods can also be found in the SDK documentation under "Contacts Model Constants".

The VCard mappings KUidContactFieldVCardMapWORK and KUidContactFieldVCardMapHOME can not be used within the Find() and FindNext() methods. However it is still possible to determine whether a field such as a phone number is a home number or work number by using the CContentType::ContainsFieldType() method.

The following code example demonstrates this by finding all phone numbers, testing each for its location, and displaying information on only the work phone number.

// Display only Work number

// Find the first telephone number

pos=fieldSet.FindNext(KUidContactFieldPhoneNumber,KUidContactFieldVCardMapTEL,0);

while (pos != KErrNotFound)
{
// Only display info on work number
if (fieldSet[pos].ContentType().ContainsFieldType(KUidContactFieldVCardMapWORK))
{
CContactItemField& itemField=fieldSet[pos];
if (!(itemField.IsHidden()) && !(itemField.IsDisabled()))
{
CContactTextField* textField = itemField.TextStorage();
iEikonEnv->InfoWinL(itemField.Label(), textField->Text());
}
}

// Find the next telephone number

pos = fieldSet.FindNext( KUidContactFieldPhoneNumber, KUidContactFieldVCardMapTEL, pos+1);
}