List contacts’ details (name, phone numbers, e-mail addresses, URLs, notes)
This code snippet retrieves all the contacts and lists their details such as first, middle and last name, all phone numbers, all e-mail addresses, all URLs and all notes connected with contact. PRIVILEGE NEEDED to be set in tizen-manifest.xml file: http://tizen.org/privilege/contact.read
// PRIVILEGE NEEDED to be set in tizen-manifest.xml file:
// http://tizen.org/privilege/contact.read
#include <contacts.h>
#include <stdlib.h>
#include <dlog.h> // for logging purposes
void list_contacts()
{
contacts_list_h contacts_list;
if (contacts_connect() == CONTACTS_ERROR_NONE)
{
if (contacts_db_get_all_records(_contacts_contact._uri, 0, 0, &contacts_list) == CONTACTS_ERROR_NONE)
{
LOGI ("Listing contacts...");
contacts_record_h contact;
while (contacts_list_get_current_record_p(contacts_list, &contact) == CONTACTS_ERROR_NONE)
{
LOGI("-----------Contact found!------------");
char *contact_name = NULL;
if (contacts_record_get_str(contact, _contacts_contact.display_name, &contact_name) != CONTACTS_ERROR_NONE
&& contact_name != NULL)
{
LOGE ("Error occurred when trying to get contact's display name!");
contact_name = strdup("Not defined");
}
LOGI("Display name: %s", contact_name);
// Notice that if you use the default Contacts app
// to add a contact without a name and only with phone number,
// the display name retrieved will be the phone number itself.
//GETTING FULL CONTACT NAME
int err;
//First, middle and last name will be printed, if they exist:
contacts_record_h child_record_name = NULL;
char *contact_name_first = NULL, *contact_name_middle = NULL, *contact_name_last = NULL;
//There can be only one child record with contact name details, so we get the record with index "0".
err = contacts_record_get_child_record_at_p(contact, _contacts_contact.name, 0, &child_record_name);
if (err == CONTACTS_ERROR_NONE)
{
// Some fields of the name record may be NULL (eg. middle name) so if they don't exist, we don't display them.
if(contacts_record_get_str(child_record_name, _contacts_name.first, &contact_name_first) == CONTACTS_ERROR_NONE
&& contact_name_first != NULL)
{
LOGI("First name: %s", contact_name_first);
}
if(contacts_record_get_str(child_record_name, _contacts_name.addition, &contact_name_middle) == CONTACTS_ERROR_NONE
&& contact_name_middle != NULL)
{
LOGI("Middle name: %s", contact_name_middle);
}
if(contacts_record_get_str(child_record_name, _contacts_name.last, &contact_name_last) == CONTACTS_ERROR_NONE
&& contact_name_last != NULL)
{
LOGI("Last name: %s", contact_name_last);
}
}
else if (err == CONTACTS_ERROR_NO_DATA)
{
LOGI("Contact name details not defined."); //This can happen when you add a contact with phone number only.
}
else
{
LOGE ("Error occurred when getting the name record!");
}
free(contact_name_first);
free(contact_name_middle);
free(contact_name_last);
//GETTING ALL PHONE NUMBERS CONNECTED WITH CURRENT CONTACT:
int child_phones_count = 0;
if (contacts_record_get_child_record_count(contact, _contacts_contact.number, &child_phones_count) == CONTACTS_ERROR_NONE)
{
int i;
// Iterate through all phone numbers and print each one.
for (i = 0; i < child_phones_count; i++)
{
contacts_record_h child_record_phone = NULL;
char *contact_number = NULL;
if (contacts_record_get_child_record_at_p(contact, _contacts_contact.number, i, &child_record_phone) == CONTACTS_ERROR_NONE
&& contacts_record_get_str(child_record_phone, _contacts_number.number, &contact_number) == CONTACTS_ERROR_NONE
&& contact_number != NULL)
{
// Successfully retrieved the phone number
LOGI("Phone number: %s", contact_number);
}
else
{
LOGE ("Error occurred when getting the phone number record!");
}
free(contact_number);
}
}
else
{
LOGE ("Error occurred when getting the count of phone numbers!");
}
//GETTING ALL E-MAILS CONNECTED WITH CURRENT CONTACT:
int child_emails_count = 0;
if (contacts_record_get_child_record_count(contact, _contacts_contact.email, &child_emails_count) == CONTACTS_ERROR_NONE)
{
int i;
// Iterate through all e-mails and print each one.
for (i = 0; i < child_emails_count; i++)
{
contacts_record_h child_record_email = NULL;
char *email_address = NULL;
if (contacts_record_get_child_record_at_p(contact, _contacts_contact.email, i, &child_record_email) == CONTACTS_ERROR_NONE
&& contacts_record_get_str(child_record_email, _contacts_email.email, &email_address) == CONTACTS_ERROR_NONE
&& email_address != NULL)
{
// Successfully retrieved the e-mail address
LOGI("E-mail: %s", email_address);
}
else
{
LOGE ("Error occurred when getting the e-mail record!");
}
free(email_address);
}
}
else
{
LOGE ("Error occurred when getting the count of e-mails!");
}
//GETTING ALL URLs CONNECTED WITH CURRENT CONTACT:
int child_urls_count = 0;
if (contacts_record_get_child_record_count(contact, _contacts_contact.url, &child_urls_count) == CONTACTS_ERROR_NONE)
{
int i;
// Iterate through all URLs and print each one.
for (i = 0; i < child_urls_count; i++)
{
contacts_record_h child_record_url = NULL;
char *url = NULL;
if (contacts_record_get_child_record_at_p(contact, _contacts_contact.url, i, &child_record_url) == CONTACTS_ERROR_NONE
&& contacts_record_get_str(child_record_url, _contacts_url.url, &url) == CONTACTS_ERROR_NONE
&& url != NULL)
{
// Successfully retrieved the URL
LOGI("URL: %s", url);
}
else
{
LOGE ("Error occurred when getting the URL record!");
}
free(url);
}
}
else
{
LOGE ("Error occurred when getting the count of URLs!");
}
//GETTING ALL NOTES CONNECTED WITH CURRENT CONTACT:
int child_notes_count = 0;
if (contacts_record_get_child_record_count(contact, _contacts_contact.note, &child_notes_count) == CONTACTS_ERROR_NONE)
{
int i;
// Iterate through all notes and print each one.
for (i = 0; i < child_notes_count; i++)
{
contacts_record_h child_record_note = NULL;
char *note = NULL;
if (contacts_record_get_child_record_at_p(contact, _contacts_contact.note, i, &child_record_note) == CONTACTS_ERROR_NONE
&& contacts_record_get_str(child_record_note, _contacts_note.note, ¬e) == CONTACTS_ERROR_NONE
&& note != NULL)
{
// Successfully retrieved the note
LOGI("Note: %s", note);
}
else
{
LOGE ("Error occurred when getting the note record!");
}
free(note);
}
}
else
{
LOGE ("Error occurred when getting the count of notes!");
}
free(contact_name);
// proceed to next contact on a list
err = contacts_list_next(contacts_list);
if (err == CONTACTS_ERROR_NO_DATA)
{
LOGE ("List finished!");
break;
}
else if (err != CONTACTS_ERROR_NONE)
{
LOGE ("Error occurred when going to next contact!");
}
}
LOGI ("Listing contacts completed!");
if (contacts_list_destroy(contacts_list, true) != CONTACTS_ERROR_NONE)
{
LOGE ("Error occurred when freeing tcontact list!");
}
}
else
{
LOGE ("Error occurred when getting the contact list!");
}
if (contacts_disconnect() != CONTACTS_ERROR_NONE)
{
LOGE ("Error occurred when disconnecting from contacts service!");
}
}
else
{
LOGE ("Error occurred when connecting to contacts service!");
}
}