diff options
Diffstat (limited to 'src/calllog.c')
-rwxr-xr-x | src/calllog.c | 394 |
1 files changed, 394 insertions, 0 deletions
diff --git a/src/calllog.c b/src/calllog.c new file mode 100755 index 0000000..7ca5855 --- /dev/null +++ b/src/calllog.c @@ -0,0 +1,394 @@ +/* + * 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 <tizen.h> +#include <calllog.h> +#include <contacts-svc.h> +#include <calllog_private.h> +#include <dlog.h> +#include <stdlib.h> +#include <string.h> + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "TIZEN_N_CALLLOG" +#define LOG_MODE (1) + +int calllog_connect(void) +{ + if(contacts_svc_connect() == CTS_SUCCESS) { + return CALLLOG_ERROR_NONE; + } + LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED); + return CALLLOG_ERROR_DB_FAILED; +} + +int calllog_disconnect(void) +{ + if(contacts_svc_disconnect() == CTS_SUCCESS) { + return CALLLOG_ERROR_NONE; + } + LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED); + return CALLLOG_ERROR_DB_FAILED; +} + +int calllog_create(calllog_h* log) +{ + if(log == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSvalue* value = contacts_svc_value_new(CTS_VALUE_PHONELOG); + if(value == NULL) { + LOGE("[%s] CALLLOG_ERROR_OUT_OF_MEMORY(0x%08x)", __FUNCTION__, CALLLOG_ERROR_OUT_OF_MEMORY); + return CALLLOG_ERROR_OUT_OF_MEMORY; + } + *log = (calllog_h)value; + return CALLLOG_ERROR_NONE; +} + +int calllog_destroy(calllog_h log) +{ + if(log == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + if(contacts_svc_value_free((CTSvalue*)log) == CTS_SUCCESS) { + return CALLLOG_ERROR_NONE; + } + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; +} + +int calllog_insert_to_db(calllog_h log) +{ + if(log == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + if(contacts_svc_insert_phonelog((CTSvalue*)log) == CTS_SUCCESS) { + return CALLLOG_ERROR_NONE; + } + LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED); + return CALLLOG_ERROR_DB_FAILED; +} + +int calllog_delete_from_db(int calllog_db_id) +{ + if(calllog_db_id < 0) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + if(contacts_svc_delete_phonelog(CTS_PLOG_DEL_BY_ID, calllog_db_id) == CTS_SUCCESS) { + return CALLLOG_ERROR_NONE; + } + LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED); + return CALLLOG_ERROR_DB_FAILED; +} + +int calllog_delete_all_from_db() +{ + CTSiter *iter = NULL; + if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter)) { + LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED); + return CALLLOG_ERROR_DB_FAILED; + } + + // get count + int count = 0; + while(CTS_SUCCESS == contacts_svc_iter_next(iter)) { + CTSvalue* foreach_data = contacts_svc_iter_get_info(iter); + if(foreach_data == NULL) + break; + count++; + contacts_svc_value_free(foreach_data); + } + contacts_svc_iter_remove(iter); + + if(count == 0) { + return CALLLOG_ERROR_NONE; + } + + // create id list + iter = NULL; + if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter)) { + LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED); + return CALLLOG_ERROR_DB_FAILED; + } + int *id_list = NULL; + int i = 0; + id_list = calloc(count, sizeof(int)); + if(id_list == NULL) { + LOGE("[%s] CALLLOG_ERROR_OUT_OF_MEMORY(0x%08x)", __FUNCTION__, CALLLOG_ERROR_OUT_OF_MEMORY); + return CALLLOG_ERROR_OUT_OF_MEMORY; + } + while(CTS_SUCCESS == contacts_svc_iter_next(iter)) { + CTSvalue* foreach_data = contacts_svc_iter_get_info(iter); + if(foreach_data == NULL) + break; + id_list[i] = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_ID_INT); + contacts_svc_value_free(foreach_data); + i++; + } + contacts_svc_iter_remove(iter); + + // delete all call log + for(i=0; i<count; i++) + { + contacts_svc_delete_phonelog(CTS_PLOG_DEL_BY_ID, id_list[i]); + } + + _calllog_safe_free(id_list); + + return CALLLOG_ERROR_NONE; +} + +int calllog_update_missed_unchecked_to_checked_to_db(int calllog_db_id) +{ + if(calllog_db_id < 0) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + if(contacts_svc_connect() != CTS_SUCCESS) { + LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED); + return CALLLOG_ERROR_DB_FAILED; + } + + CTSvalue *phonelog = NULL; + if(contacts_svc_get_phonelog(calllog_db_id, &phonelog) != CTS_SUCCESS) { + contacts_svc_disconnect(); + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + + if(contacts_svc_phonelog_set_seen(calllog_db_id, ((_calllog_s*)phonelog)->calllog_type) < 0) { + contacts_svc_value_free(phonelog); + contacts_svc_disconnect(); + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + + contacts_svc_value_free(phonelog); + contacts_svc_disconnect(); + return CALLLOG_ERROR_NONE; +} + +int calllog_get_time(calllog_h log, time_t* log_time) +{ + if(log == NULL || time == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSvalue * CTSlog = (CTSvalue *)log; + *log_time=(time_t)contacts_svc_value_get_int(CTSlog, CTS_PLOG_VAL_LOG_TIME_INT); + return CALLLOG_ERROR_NONE; +} + +int calllog_set_time(calllog_h log, time_t log_time) +{ + if(log == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSvalue * CTSlog = (CTSvalue *)log; + if(contacts_svc_value_set_int(CTSlog,CTS_PLOG_VAL_LOG_TIME_INT, (int)log_time) == CTS_SUCCESS) { + return CALLLOG_ERROR_NONE; + } + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; +} + + +int calllog_get_type(calllog_h log, calllog_type_e* type) +{ + if(log == NULL || type == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSvalue * CTSlog = (CTSvalue *)log; + * type=contacts_svc_value_get_int(CTSlog,CTS_PLOG_VAL_LOG_TYPE_INT); + return CALLLOG_ERROR_NONE; +} + +int calllog_set_type(calllog_h log, calllog_type_e type) +{ + if(log == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSvalue * CTSlog = (CTSvalue *)log; + + if(contacts_svc_value_set_int(CTSlog,CTS_PLOG_VAL_LOG_TYPE_INT,type) == CTS_SUCCESS) { + return CALLLOG_ERROR_NONE; + } + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; +} + + +int calllog_get_duration(calllog_h log, int* duration_sec) +{ + if(log == NULL || duration_sec == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSvalue * CTSlog = (CTSvalue *)log; + *duration_sec=contacts_svc_value_get_int(CTSlog,CTS_PLOG_VAL_DURATION_INT); + return CALLLOG_ERROR_NONE; +} + +int calllog_set_duration(calllog_h log, int duration_sec) +{ + if(log == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSvalue * CTSlog = (CTSvalue *)log; + if(contacts_svc_value_set_int(CTSlog,CTS_PLOG_VAL_DURATION_INT,duration_sec) == CTS_SUCCESS) { + return CALLLOG_ERROR_NONE; + } + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; +} + +int calllog_get_number(calllog_h log, char** number) +{ + if(log == NULL || number == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + *number = NULL; + *number = _calllog_safe_strdup(((_calllog_s*)log)->number); + + return CALLLOG_ERROR_NONE; +} + +int calllog_set_number(calllog_h log, const char* number) +{ + if(log == NULL || number == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSvalue * CTSlog = (CTSvalue *)log; + + if(contacts_svc_value_set_str(CTSlog,CTS_PLOG_VAL_NUMBER_STR,number) == CTS_SUCCESS) { + return CALLLOG_ERROR_NONE; + } + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; +} + +int calllog_foreach_calllog_from_db(calllog_foreach_cb cb, void *user_data) +{ + if(cb == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSiter *iter = NULL; + int func_ret = 0; + if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_PLOG, &iter)) { + LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED); + return CALLLOG_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; + + calllog_query_detail_s query_data; + query_data.calllog_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_ID_INT); + query_data.calllog_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TYPE_INT); + query_data.contact_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_RELATED_ID_INT); + query_data.first_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_FIRST_NAME_STR)); + query_data.last_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_LAST_NAME_STR)); + query_data.display_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_DISPLAY_NAME_STR)); + query_data.contact_image_path = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_IMG_PATH_STR)); + query_data.phone_number_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_NUM_TYPE_INT); + query_data.phone_number = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_NUMBER_STR)); + query_data.short_message = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_SHORTMSG_STR)); + query_data.timestamp = (time_t)contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TIME_INT); + query_data.duration_sec = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_DURATION_INT); + + func_ret = cb(&query_data, user_data); + + contacts_svc_value_free(foreach_data); + + _calllog_safe_free(query_data.first_name); + _calllog_safe_free(query_data.last_name); + _calllog_safe_free(query_data.display_name); + _calllog_safe_free(query_data.contact_image_path); + _calllog_safe_free(query_data.phone_number); + _calllog_safe_free(query_data.short_message); + } + + contacts_svc_iter_remove(iter); + return CALLLOG_ERROR_NONE; +} + +int calllog_query_calllog_by_number(calllog_foreach_cb cb, const char* number_to_find, void* user_data) +{ + if(cb == NULL || number_to_find == NULL) { + LOGE("[%s] CALLLOG_ERROR_INVALID_PARAMETER(0x%08x)", __FUNCTION__, CALLLOG_ERROR_INVALID_PARAMETER); + return CALLLOG_ERROR_INVALID_PARAMETER; + } + CTSiter *iter = NULL; + int func_ret = 0; + if(CTS_SUCCESS != contacts_svc_get_list_with_str(CTS_LIST_PLOGS_OF_NUMBER, number_to_find, &iter)) { + LOGE("[%s] CALLLOG_ERROR_DB_FAILED(0x%08x)", __FUNCTION__, CALLLOG_ERROR_DB_FAILED); + return CALLLOG_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; + + calllog_query_detail_s query_data; + query_data.calllog_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_ID_INT); + query_data.calllog_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TYPE_INT); + query_data.contact_db_id = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_RELATED_ID_INT); + query_data.first_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_FIRST_NAME_STR)); + query_data.last_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_LAST_NAME_STR)); + query_data.display_name = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_DISPLAY_NAME_STR)); + query_data.contact_image_path = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_IMG_PATH_STR)); + query_data.phone_number_type = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_NUM_TYPE_INT); + query_data.phone_number = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_NUMBER_STR)); + query_data.short_message = _calllog_safe_strdup(contacts_svc_value_get_str(foreach_data, CTS_LIST_PLOG_SHORTMSG_STR)); + query_data.timestamp = (time_t)contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_LOG_TIME_INT); + query_data.duration_sec = contacts_svc_value_get_int(foreach_data, CTS_LIST_PLOG_DURATION_INT); + + func_ret = cb(&query_data, user_data); + + contacts_svc_value_free(foreach_data); + + _calllog_safe_free(query_data.first_name); + _calllog_safe_free(query_data.last_name); + _calllog_safe_free(query_data.display_name); + _calllog_safe_free(query_data.contact_image_path); + _calllog_safe_free(query_data.phone_number); + _calllog_safe_free(query_data.short_message); + } + + contacts_svc_iter_remove(iter); + return CALLLOG_ERROR_NONE; +} + + + |