/* * 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_group_create(contacts_group_h* group) { CONTACTS_NULL_ARG_CHECK(group); CTSvalue* ret = contacts_svc_value_new(CTS_VALUE_GROUP); if(ret == NULL) { LOGE("[%s] CONTACTS_ERROR_OUT_OF_MEMORY(0x%08x)", __FUNCTION__, CONTACTS_ERROR_OUT_OF_MEMORY); return CONTACTS_ERROR_OUT_OF_MEMORY; } *group = (contacts_group_h)ret; return CONTACTS_ERROR_NONE; } int contacts_group_destroy(contacts_group_h group) { CONTACTS_NULL_ARG_CHECK(group); if(contacts_svc_value_free((CTSvalue*)group) == 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_group_insert_to_db(contacts_group_h group, int address_book_db_id, int* db_id) { CONTACTS_NULL_ARG_CHECK(group); CONTACTS_NULL_ARG_CHECK(db_id); CONTACTS_INVALID_ARG_CHECK(address_book_db_id < 0); int group_index = contacts_svc_insert_group(address_book_db_id, (CTSvalue*)group); if(group_index < 0) { LOGE("[%s] CONTACTS_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CONTACTS_ERROR_DB_FAILED); return CONTACTS_ERROR_DB_FAILED; } if(db_id != NULL) *db_id = group_index; return CONTACTS_ERROR_NONE; } int contacts_group_update_to_db(contacts_group_h group) { CONTACTS_NULL_ARG_CHECK(group); int ret = CTS_SUCCESS; if((ret=contacts_svc_update_group((CTSvalue*)group)) == 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_group_delete_from_db(int db_id) { CONTACTS_INVALID_ARG_CHECK(db_id <= 0); int ret = CTS_SUCCESS; if((ret=contacts_svc_delete_group(db_id)) == 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_group_get_db_id(contacts_group_h group, int* db_id) { CONTACTS_NULL_ARG_CHECK(group); CONTACTS_NULL_ARG_CHECK(db_id); *db_id = contacts_svc_value_get_int((CTSvalue*)group, CTS_GROUP_VAL_ID_INT); if(*db_id < 0) { *db_id = 0; } return CONTACTS_ERROR_NONE; } int contacts_group_get_from_db(int db_id, contacts_group_h* group) { CONTACTS_INVALID_ARG_CHECK(db_id <= 0); CONTACTS_NULL_ARG_CHECK(group); int ret = CTS_SUCCESS; if((ret=contacts_svc_get_group(db_id, (CTSvalue**)group)) == 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_group_get_name(contacts_group_h group, char** name) { CONTACTS_NULL_ARG_CHECK(group); CONTACTS_NULL_ARG_CHECK(name); *name = NULL; *name = _contacts_safe_strdup(contacts_svc_value_get_str((CTSvalue*)group, CTS_GROUP_VAL_NAME_STR)); return CONTACTS_ERROR_NONE; } int contacts_group_set_name(contacts_group_h group, const char* name) { CONTACTS_NULL_ARG_CHECK(group); CONTACTS_NULL_ARG_CHECK(name); if(contacts_svc_value_set_str((CTSvalue*)group, CTS_GROUP_VAL_NAME_STR, name) == 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_group_get_ringtonepath(contacts_group_h group, char** ringtonepath) { CONTACTS_NULL_ARG_CHECK(group); CONTACTS_NULL_ARG_CHECK(ringtonepath); *ringtonepath = NULL; *ringtonepath = _contacts_safe_strdup(contacts_svc_value_get_str((CTSvalue*)group, CTS_GROUP_VAL_RINGTONE_STR)); return CONTACTS_ERROR_NONE; } int contacts_group_set_ringtonepath(contacts_group_h group, const char* ringtonepath) { CONTACTS_NULL_ARG_CHECK(group); CONTACTS_NULL_ARG_CHECK(ringtonepath); if(contacts_svc_value_set_str((CTSvalue*)group, CTS_GROUP_VAL_RINGTONE_STR, ringtonepath) == 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_group_get_member_count_from_db(contacts_group_h group, int* count) { CONTACTS_NULL_ARG_CHECK(group); CONTACTS_NULL_ARG_CHECK(count); int group_id = contacts_svc_value_get_int((CTSvalue*)group, CTS_GROUP_VAL_ID_INT); if(group_id < 0) { LOGE("[%s] CONTACTS_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CONTACTS_ERROR_INVALID_PARAMETER); return CONTACTS_ERROR_INVALID_PARAMETER; } *count = contacts_svc_count_with_int(CTS_GET_COUNT_CONTACTS_IN_GROUP, group_id); return CONTACTS_ERROR_NONE; } int contacts_group_foreach_group_from_db(contacts_foreach_query_group_cb cb, void* user_data) { CONTACTS_NULL_ARG_CHECK(cb); CTSiter *iter = NULL; int func_ret = 0; if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_GROUP, &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_group_s query_data; query_data.group_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_GROUP_ID_INT); query_data.address_book_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_GROUP_ADDRESSBOOK_ID_INT); query_data.group_name = _contacts_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_GROUP_NAME_STR)); func_ret = cb(&query_data, user_data); contacts_svc_value_free(foreach_data); _contacts_safe_free(query_data.group_name); if(func_ret == 0) { break; } } contacts_svc_iter_remove(iter); return CONTACTS_ERROR_NONE; } int contacts_group_get_address_book_db_id(contacts_group_h group, int *address_book_db_id) { CONTACTS_NULL_ARG_CHECK(group); CONTACTS_NULL_ARG_CHECK(address_book_db_id); *address_book_db_id = contacts_svc_value_get_int((CTSvalue*)group, CTS_GROUP_VAL_ADDRESSBOOK_ID_INT); if(*address_book_db_id < 0) { *address_book_db_id = 0; } return CONTACTS_ERROR_NONE; } int contacts_group_query_group_by_address_book(contacts_foreach_query_group_cb callback, int address_book_db_id, void *user_data) { CONTACTS_NULL_ARG_CHECK(callback); CONTACTS_INVALID_ARG_CHECK(address_book_db_id < 0); CTSiter *iter = NULL; int func_ret = 0; if(CTS_SUCCESS != contacts_svc_get_list_with_int(CTS_LIST_GROUPS_OF_ADDRESSBOOK_ID, address_book_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_group_s query_data; query_data.group_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_GROUP_ID_INT); query_data.address_book_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_GROUP_ADDRESSBOOK_ID_INT); query_data.group_name = _contacts_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_GROUP_NAME_STR)); func_ret = callback(&query_data, user_data); contacts_svc_value_free(foreach_data); _contacts_safe_free(query_data.group_name); if(func_ret == 0) { break; } } contacts_svc_iter_remove(iter); return CONTACTS_ERROR_NONE;}