/* * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #ifdef LOG_TAG #undef LOG_TAG #endif #define LOG_TAG "TIZEN_N_CONTACTS" #define LOG_MODE (1) int contacts_connect(void) { if(contacts_svc_connect() == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } int contacts_disconnect(void) { if(contacts_svc_disconnect() == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } int contacts_get_db_version(int* contacts_db_version) { CONTACTS_NULL_ARG_CHECK(contacts_db_version); if(contacts_svc_begin_trans() != CTS_SUCCESS) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } *contacts_db_version = contacts_svc_end_trans(true); return CONTACTS_ERROR_NONE; } int contacts_get_name_display_order(contacts_name_display_order_e *name_display_order) { CONTACTS_NULL_ARG_CHECK(name_display_order); if ((*name_display_order = contacts_svc_get_order(CTS_ORDER_OF_DISPLAY)) < 0) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } return CONTACTS_ERROR_NONE; } int contacts_set_name_display_order(contacts_name_display_order_e name_display_order) { CONTACTS_INVALID_ARG_CHECK(name_display_order < 0 || 1 < name_display_order); if (CTS_SUCCESS == contacts_svc_set_order(CTS_ORDER_OF_DISPLAY, name_display_order)) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } int contacts_add_address_book_db_changed_cb(contacts_db_changed_cb callback, void *user_data) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_subscribe_change(CTS_SUBSCRIBE_ADDRESSBOOK_CHANGE, callback, user_data) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_remove_address_book_db_changed_cb(contacts_db_changed_cb callback) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_ADDRESSBOOK_CHANGE, callback) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_add_contact_db_changed_cb(contacts_db_changed_cb callback, void *user_data) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_subscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, callback, user_data) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_remove_contact_db_changed_cb(contacts_db_changed_cb callback) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE, callback) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_add_favorite_db_changed_cb(contacts_db_changed_cb callback, void *user_data) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_subscribe_change(CTS_SUBSCRIBE_FAVORITE_CHANGE, callback, user_data) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_remove_favorite_db_changed_cb(contacts_db_changed_cb callback) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_FAVORITE_CHANGE, callback) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_add_group_db_changed_cb(contacts_db_changed_cb callback, void *user_data) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_subscribe_change(CTS_SUBSCRIBE_GROUP_CHANGE, callback, user_data) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_remove_group_db_changed_cb(contacts_db_changed_cb callback) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_GROUP_CHANGE, callback) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_add_group_relation_changed_cb(contacts_db_changed_cb callback, void *user_data) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_subscribe_change(CTS_SUBSCRIBE_GROUP_RELATION_CHANGE, callback, user_data) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_remove_group_relation_changed_cb(contacts_db_changed_cb callback) { CONTACTS_NULL_ARG_CHECK(callback); if(contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_GROUP_RELATION_CHANGE, callback) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_import_all_contacts_from_sim_card() { int ret = contacts_svc_import_sim(); if(CTS_ERR_DB_FAILED == ret || CTS_ERR_DB_NOT_OPENED == ret) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } return CONTACTS_ERROR_NONE; } int contacts_address_book_foreach_address_book_from_db(contacts_foreach_query_address_book_cb callback, void *user_data) { CONTACTS_NULL_ARG_CHECK(callback); CTSiter *iter = NULL; int func_ret = 0; if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_ADDRESSBOOK, &iter)) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } while(CTS_SUCCESS == contacts_svc_iter_next(iter)) { CTSvalue* foreach_data = contacts_svc_iter_get_info(iter); if(foreach_data == NULL) { break; } contacts_query_address_book_s query_data; _contacts_set_query_address_book_struct(&query_data, foreach_data); func_ret = callback(&query_data, user_data); contacts_svc_value_free(foreach_data); _contacts_free_query_address_book_struct_member_only(&query_data); if(func_ret == 0) { break; } } contacts_svc_iter_remove(iter); return CONTACTS_ERROR_NONE; } int contacts_address_book_query_address_book_by_account_db_id(contacts_foreach_query_address_book_cb callback, int account_db_id, void *user_data) { CONTACTS_NULL_ARG_CHECK(callback); CTSiter *iter = NULL; int func_ret = 0; if(CTS_SUCCESS != contacts_svc_get_list_with_int(CTS_LIST_ADDRESSBOOKS_OF_ACCOUNT_ID, account_db_id, &iter)) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } while(CTS_SUCCESS == contacts_svc_iter_next(iter)) { CTSvalue* foreach_data = contacts_svc_iter_get_info(iter); if(foreach_data == NULL) { break; } contacts_query_address_book_s query_data; _contacts_set_query_address_book_struct(&query_data, foreach_data); func_ret = callback(&query_data, user_data); contacts_svc_value_free(foreach_data); _contacts_free_query_address_book_struct_member_only(&query_data); if(func_ret == 0) { break; } } contacts_svc_iter_remove(iter); return CONTACTS_ERROR_NONE; } int contacts_address_book_get_from_db(int address_book_db_id, contacts_address_book_h *address_book) { CONTACTS_NULL_ARG_CHECK(address_book); CONTACTS_INVALID_ARG_CHECK(address_book_db_id < 0); int ret = CTS_SUCCESS; if((ret=contacts_svc_get_addressbook(address_book_db_id, (CTSvalue**)address_book)) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } else if(ret == CTS_ERR_DB_FAILED || ret == CTS_ERR_DB_NOT_OPENED || ret == CTS_ERR_DB_RECORD_NOT_FOUND) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_address_book_create(contacts_address_book_h *address_book) { CONTACTS_NULL_ARG_CHECK(address_book); CTSvalue* ret = contacts_svc_value_new(CTS_VALUE_ADDRESSBOOK); if(ret == NULL) { LOGE("[%s] CONTACTS_ERROR_OUT_OF_MEMORY(0x%08x)", __FUNCTION__, CONTACTS_ERROR_OUT_OF_MEMORY); return CONTACTS_ERROR_OUT_OF_MEMORY; } *address_book = (contacts_address_book_h)ret; return CONTACTS_ERROR_NONE; } int contacts_address_book_destroy(contacts_address_book_h address_book) { CONTACTS_NULL_ARG_CHECK(address_book); if(contacts_svc_value_free((CTSvalue*)address_book) == CTS_SUCCESS) { return CONTACTS_ERROR_NONE; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_address_book_insert_to_db(contacts_address_book_h address_book, int *address_book_db_id) { CONTACTS_NULL_ARG_CHECK(address_book); int ret; CTSvalue* value = (CTSvalue *)address_book; ret = contacts_svc_insert_addressbook(value); if(ret < CTS_SUCCESS) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } *address_book_db_id = ret; return CONTACTS_ERROR_NONE; } int contacts_address_book_delete_from_db(int address_book_db_id) { CONTACTS_INVALID_ARG_CHECK(address_book_db_id < 0); int ret; ret = contacts_svc_delete_addressbook(address_book_db_id); if (CTS_SUCCESS == ret) return CONTACTS_ERROR_NONE; else if (CTS_ERR_DB_FAILED == ret) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_address_book_update_to_db(contacts_address_book_h address_book) { CONTACTS_NULL_ARG_CHECK(address_book); int ret; ret = contacts_svc_update_addressbook((CTSvalue*)address_book); if (CTS_SUCCESS == ret) return CONTACTS_ERROR_NONE; else if (CTS_ERR_DB_FAILED == ret) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } int contacts_address_book_set_type(contacts_address_book_h address_book, contacts_address_book_type_e address_book_type) { CONTACTS_NULL_ARG_CHECK(address_book); int ret; ret = contacts_svc_value_set_int((CTSvalue *)address_book, CTS_ADDRESSBOOK_VAL_ACC_TYPE_INT, address_book_type); if (CTS_SUCCESS != ret) { LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } return CONTACTS_ERROR_NONE; } int contacts_address_book_get_db_id(contacts_address_book_h address_book, int *address_book_db_id) { CONTACTS_NULL_ARG_CHECK(address_book); CONTACTS_NULL_ARG_CHECK(address_book_db_id); *address_book_db_id = 0; *address_book_db_id = contacts_svc_value_get_int((CTSvalue*)address_book, CTS_ADDRESSBOOK_VAL_ID_INT); if(*address_book_db_id < 0) { *address_book_db_id = 0; } return CONTACTS_ERROR_NONE; } int contacts_address_book_get_name(contacts_address_book_h address_book, char **address_book_name) { CONTACTS_NULL_ARG_CHECK(address_book); CONTACTS_NULL_ARG_CHECK(address_book_name); *address_book_name = NULL; *address_book_name = _contacts_safe_strdup(contacts_svc_value_get_str((CTSvalue*)address_book, CTS_ADDRESSBOOK_VAL_NAME_STR)); return CONTACTS_ERROR_NONE; } int contacts_address_book_set_name(contacts_address_book_h address_book, const char *address_book_name) { CONTACTS_NULL_ARG_CHECK(address_book); CONTACTS_NULL_ARG_CHECK(address_book_name); int ret = contacts_svc_value_set_str((CTSvalue*)address_book, CTS_ADDRESSBOOK_VAL_NAME_STR, address_book_name); if (CTS_SUCCESS != ret) { LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } return CONTACTS_ERROR_NONE; } int contacts_address_book_get_read_only(contacts_address_book_h address_book, bool *is_read_only) { CONTACTS_NULL_ARG_CHECK(address_book); CONTACTS_NULL_ARG_CHECK(is_read_only); *is_read_only = false; *is_read_only = contacts_svc_value_get_int((CTSvalue*)address_book, CTS_ADDRESSBOOK_VAL_MODE_INT); return CONTACTS_ERROR_NONE; } int contacts_address_book_set_read_only(contacts_address_book_h address_book, bool is_read_only) { CONTACTS_NULL_ARG_CHECK(address_book); int ret = contacts_svc_value_set_int((CTSvalue*)address_book, CTS_ADDRESSBOOK_VAL_MODE_INT, is_read_only); if (CTS_SUCCESS != ret) { LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } return CONTACTS_ERROR_NONE; } int contacts_address_book_get_type(contacts_address_book_h address_book, contacts_address_book_type_e *address_book_type) { CONTACTS_NULL_ARG_CHECK(address_book); CONTACTS_NULL_ARG_CHECK(address_book_type); *address_book_type = CONTACTS_ADDRESS_BOOK_TYPE_OTHER; *address_book_type = contacts_svc_value_get_int((CTSvalue*)address_book, CTS_ADDRESSBOOK_VAL_ACC_TYPE_INT); return CONTACTS_ERROR_NONE; } int contacts_address_book_get_account_db_id(contacts_address_book_h address_book, int *account_db_id) { CONTACTS_NULL_ARG_CHECK(address_book); CONTACTS_NULL_ARG_CHECK(account_db_id); *account_db_id = 0; *account_db_id = contacts_svc_value_get_int((CTSvalue*)address_book, CTS_ADDRESSBOOK_VAL_ACC_ID_INT); if(*account_db_id < 0) { *account_db_id = 0; } return CONTACTS_ERROR_NONE; } int contacts_address_book_set_account_db_id(contacts_address_book_h address_book, int account_db_id) { CONTACTS_NULL_ARG_CHECK(address_book); int ret = contacts_svc_value_set_int((CTSvalue*)address_book, CTS_ADDRESSBOOK_VAL_ACC_ID_INT, account_db_id); if (CTS_SUCCESS != ret) { LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } return CONTACTS_ERROR_NONE; }