This page was last modified 09:27, 11 December 2007.
Getting contact info from default database
From Forum Nokia Wiki
The code example below gets contact info from default contact database according to application needs. In the example we get general mobile phones, home mobile phones and work mobile phones from default contact database. The code logic explained below. First of all we should open default database.
CContactDatabase* contact_db; TBuf<60> dbFile; //gets default contact database file CContactDatabase::GetDefaultNameL( dbFile ); //opens default contact database file TRAPD(err, contact_db= CContactDatabase::OpenL(dbFile));
Because it is possible to have few the same type contact info we iterate through contacts first.
//create iterarot for contact_db TContactIter contact_iterator(*contact_db); TContactItemId contact_id; //go through contacts while there any info while( ( contact_id = contact_iterator.NextL() ) != KNullContactId)
Then we iterate through fields itself.
CContactItem* contact_item = contact_db->ReadContactL(contact_id); CContactItemFieldSet &field_set = contact_item->CardFields(); //go through all the fields of contact and extract info according to //application needs for (TInt i = 0 ; i < field_set.Count() ; i++)
After that we extract necessary information and process it according to application logic.
if(field.ContentType().ContainsFieldType(KUidContactFieldVCardMapCELL)&& field.ContentType().ContainsFieldType(KUidContactFieldVCardMapWORK)) { // This is work mobile processing. It will get all work mobiles in the //contact list wmobile.Fill('\0',1); wmobile.Copy(field.TextStorage()->Text()); wmobile.ZeroTerminate(); //process work mobile according to application logic continue; }
Note: It is very important to get work and home mobile first before general one! Otherwise it will mix it. This rule applies to all contact info.
The complete function code below:
//open current contacts from default database void CContacts::OpenContacts() { CContactDatabase* contact_db; TBuf<60> dbFile; //gets default contact database file CContactDatabase::GetDefaultNameL( dbFile ); //opens default contact database file TRAPD(err, contact_db= CContactDatabase::OpenL(dbFile)); //process errors if any if ( err == KErrNotFound ) { // "does not exist" } else if( err == KErrLocked ) { // "locked" } else if( err == KErrBadName ) { // "bad name" } else if( err == KErrDiskFull ) { // "disk full" } else if (err != KErrNone) { // "error opening default contact db" } //create iterarot for contact_db TContactIter contact_iterator(*contact_db); TContactItemId contact_id; //general mobile, home mobile and work mobile TBuf<100> mobile(0), hphone(0), wmobile(0); //go through contacts while there any info while( ( contact_id = contact_iterator.NextL() ) != KNullContactId) { CContactItem* contact_item = contact_db->ReadContactL(contact_id); CleanupStack::PushL(contact_item); CContactItemFieldSet &field_set = contact_item->CardFields(); //go through all the fields of contact and extract info according to application needs for (TInt i = 0 ; i < field_set.Count() ; i++) { CContactItemField& field = field_set[i]; //work mobile if (field.ContentType().ContainsFieldType(KUidContactFieldVCardMapCELL)&& field.ContentType().ContainsFieldType(KUidContactFieldVCardMapWORK)) { // This is work mobile processing. It will get all work mobiles in the contact list wmobile.Fill('\0',1); wmobile.Copy(field.TextStorage()->Text()); wmobile.ZeroTerminate(); //process work mobile according to application logic continue; }else if (field.ContentType().ContainsFieldType(KUidContactFieldVCardMapCELL)&& field.ContentType().ContainsFieldType(KUidContactFieldVCardMapHOME)) { // This is home mobile processing. It will get all home mobiles in the contact list hmobile.Fill('\0',1); hmobile.Copy(field.TextStorage()->Text()); hmobile.ZeroTerminate(); //process home mobile according to application logic continue; }else if (field.ContentType().ContainsFieldType(KUidContactFieldVCardMapCELL)) { // This is general mobile processing. It will get all general mobiles in the contact list mobile.Fill('\0',1); mobile.Copy(field.TextStorage()->Text()); mobile.ZeroTerminate(); //process general mobile according to application logic continue; } //TO DO add other KUidContact fields according to application logic } contact_db->CloseContactL(contact_item->Id()); CleanupStack::PopAndDestroy(); } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| N72上通讯录成功保存,但无法拨打电话。 | diyer_xu | Symbian | 10 | 2008-07-03 02:54 |
| Creating custom fields in contact items | vaibhavjain | General Symbian C++ | 7 | 2007-11-16 13:51 |
| view activation notifications | noam segal | Symbian User Interface | 0 | 2008-03-05 17:12 |
| how to distinguish between the phone and mobile phone field in the contact database. | acelias | General Symbian C++ | 2 | 2005-03-19 07:42 |
| how to get default contacts database name and path(3rd SDK) | bearzhang | General Symbian C++ | 1 | 2007-07-31 04:41 |
