diff options
Diffstat (limited to 'lib/common/ph-common.c')
-rwxr-xr-x | lib/common/ph-common.c | 1930 |
1 files changed, 1930 insertions, 0 deletions
diff --git a/lib/common/ph-common.c b/lib/common/ph-common.c new file mode 100755 index 0000000..92d88f0 --- /dev/null +++ b/lib/common/ph-common.c @@ -0,0 +1,1930 @@ +/* +* Copyright 2012 Samsung Electronics Co., Ltd +* +* Licensed under the Flora License, Version 1.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://floralicense.org/license/ +* +* 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 <app.h> +#include <fcntl.h> +#include <ui-gadget-module.h> +#include <account-types.h> +#include <syspopup_caller.h> +#include <account.h> +#include <notification.h> + +#include "phone.h" +#include "phone-common.h" + + +#define CT_CBAR_ITEM_MAX 4 + +#ifdef PH_TIMECHECK +double ph_set_start_time(void) +{ + struct timeval tv; + double curtime; + + gettimeofday(&tv, NULL); + curtime = tv.tv_sec * 1000 + (double)tv.tv_usec/1000; + return curtime; +} + +double ph_exec_time(double start) +{ + double end = ph_set_start_time(); + return (end - start - ph_correction); +} + +int ph_init_time(void) +{ + double temp_t; + temp_t = ph_set_start_time(); + ph_correction = ph_exec_time(temp_t); + + return 0; +} +#endif + +static void progress_popup_cancel_cb(void *data, Evas_Object *obj, void *event_info) +{ + bool *is_alive = data; + *is_alive = false; +} + +void phone_free_contacts(Eina_List *list) +{ + Eina_List *l; + ph_contact_d *cd; + + p_retm_if(NULL == list, "list is NULL"); + + EINA_LIST_FOREACH(list, l, cd) { + if (NULL == cd) + continue; + + free(cd->display); + free(cd->img_path); + free(cd->number); + free(cd); + } + eina_list_free(list); +} + +void ctui_list_edit_navi_pop(void *data, Evas_Object *obj, void *event_info) +{ + int cnt_checked; + int person_count = 0; + int number_count = 0; + int email_count = 0; + int *result_list_number = NULL; + int *result_list_email = NULL; + int *result_list_person = NULL; + Eina_List *l; + ct_person_list *person_info; + ct_list_data *clist_d = data; + + cnt_checked = ctui_list_checked_cnt_get(clist_d->list); + + PH_DBG("clist_d->ug_request %d count %d", clist_d->ug_request, cnt_checked); + if (CT_UG_REQUEST_CHECK_FOR_NUMBER == clist_d->ug_request) + result_list_number = calloc(cnt_checked, sizeof(int)); + else if (CT_UG_REQUEST_CHECK_FOR_EMAIL == clist_d->ug_request) + result_list_email = calloc(cnt_checked, sizeof(int)); + else if (CT_UG_REQUEST_CHECK_FOR_NUMBER_OR_EMAIL == clist_d->ug_request) { + result_list_number = calloc(cnt_checked, sizeof(int)); + result_list_email = calloc(cnt_checked, sizeof(int)); + } + else + result_list_person = calloc(cnt_checked, sizeof(int)); + + + EINA_LIST_FOREACH(clist_d->list, l, person_info) { + if (NULL == person_info || !person_info->checked) continue; + if (1 == person_info->id_ret_type) + result_list_number[number_count++] = person_info->id_ret; + else if (2 == person_info->id_ret_type) + result_list_email[email_count++] = person_info->id_ret; + else + result_list_person[person_count++] = person_info->id; + } + + // setting - import,export / group-view - message,email,chaton,calendar + if (clist_d->select_result_cb) { // called by setting view - check person_info + if (0 < cnt_checked) { + if (result_list_number) + clist_d->select_result_cb(clist_d->select_result_cb_data, result_list_number, number_count); + else if (result_list_email) + clist_d->select_result_cb(clist_d->select_result_cb_data, result_list_email, email_count); + else + clist_d->select_result_cb(clist_d->select_result_cb_data, result_list_person, person_count); + } + } + if(result_list_person) + free(result_list_person); + if(result_list_number) + free(result_list_number); + if(result_list_email) + free(result_list_email); +} + +void phone_view_back_btn_cb(void *data, Evas_Object *obj, void *event_info) +{ + /* TODO : remove evas_object_smart_callback_del, it will be controlled by WINSET */ + evas_object_smart_callback_del(obj, "clicked", phone_view_back_btn_cb); + phone_hide_notify(data); + evas_object_smart_callback_del(data, "transition,finished", ctui_list_edit_navi_pop); + elm_naviframe_item_pop(data); +} + +void ug_common_layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv) +{ + Evas_Object *base; + + if (!ug) + return; + + base = ug_get_layout(ug); + if (!base) { + ERR("ug_get_layout() return NULL"); + ug_destroy(ug); + return; + } + evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + evas_object_show(base); +} + +void ug_common_destroy_cb(ui_gadget_h ug, void *priv) +{ + struct ug_priv_data *priv_data; + + p_retm_if(NULL == ug, "ug is NULL"); + ug_destroy(ug); + + p_retm_if(NULL == priv, "priv is NULL"); + + priv_data = (struct ug_priv_data *)priv; + + if (priv_data->destroy_cb) + priv_data->destroy_cb(priv_data->cb_param); + + free(priv); + + PH_FN_END; +} + +void * phone_image_crop_ug(ui_gadget_h parent, const char *photo, + struct ug_cbs *cbs) +{ + ui_gadget_h ug; + + service_h service; + + service_create(&service); + service_add_extra_data(service, "Path", photo); + service_add_extra_data(service, "View Mode", "SETAS"); + service_add_extra_data(service, "Setas type", "CallerID"); + service_add_extra_data(service, "Area Size", "100"); + + cbs->layout_cb = ug_common_layout_cb; + + ug = ug_create(parent, IMG_VIEWER_UG, UG_MODE_FULLVIEW, service, cbs); + p_warn_if(NULL == ug, "ug_create() Failed"); + + service_destroy(service); + + return ug; +} + +void* phone_launch_msg_composer_ug(void *parent_ug, char *number, int msg_id, + void(*destroy_cb)(void*), void *cb_param) +{ + PH_FN_CALL; + ui_gadget_h ug; + struct ug_cbs cbs = {0}; + struct ug_priv_data *priv_data; + service_h service; + char ret[PH_TEXT_MAX_LEN] = {0}; + + priv_data = calloc(1, sizeof(struct ug_priv_data)); + p_retvm_if(NULL == priv_data, NULL, "calloc is failed"); + priv_data->destroy_cb = destroy_cb; + priv_data->cb_param = cb_param; + + service_create(&service); + if (number && *number) + service_add_extra_data(service, "TO", (char*)number); + else if (msg_id) { + service_add_extra_data(service, "type", "msg_id"); + snprintf(ret, sizeof(ret), "%d", msg_id); + service_add_extra_data(service, "msgId", ret); + } + + cbs.priv = priv_data; + cbs.layout_cb = ug_common_layout_cb; + cbs.result_cb = NULL; + cbs.destroy_cb = ug_common_destroy_cb; + + ug = ug_create(parent_ug, MSG_COMPOSER_UG, UG_MODE_FULLVIEW, service, &cbs); + service_destroy(service); + + if (ug == NULL) { + free(priv_data); + ERR("%s ug_create is failed", MSG_COMPOSER_UG); + } + return ug; +} + +void* phone_launch_contact_detail_ug(void *parent_ug, int contact_index, + void(*destroy_cb)(void*), void *cb_param) +{ + PH_FN_CALL; + char buf[PH_TEXT_MAX_LEN] = {0}; + struct ug_cbs cbs = {0}; + service_h service; + ui_gadget_h ug; + struct ug_priv_data *priv_data; + + priv_data = calloc(1, sizeof(struct ug_priv_data)); + p_retvm_if(NULL == priv_data, NULL, "calloc is failed"); + priv_data->destroy_cb = destroy_cb; + priv_data->cb_param = cb_param; + + service_create(&service); + + snprintf(buf, sizeof(buf), "%d", contact_index); + service_add_extra_data(service, CT_UG_BUNDLE_ID, buf); + snprintf(buf, sizeof(buf), "%d", CT_UG_REQUEST_DETAIL); + service_add_extra_data(service, CT_UG_BUNDLE_TYPE, buf); + + cbs.priv = priv_data; + cbs.layout_cb = ug_common_layout_cb; + cbs.result_cb = NULL; + cbs.destroy_cb = ug_common_destroy_cb; + + ug = ug_create(parent_ug, UG_CONTACTS_DETAILS, UG_MODE_FULLVIEW, service, &cbs); + service_destroy(service); + + if (NULL == ug) { + free(priv_data); + ERR("%s ug_create fail", UG_CONTACTS_DETAILS); + } + return ug; +} + +void* phone_launch_contact_add_list_edit_ug(void *parent_ug, char *data, + void (*destroy_cb)(void *), void *cb_param) +{ + PH_FN_END; + char buf[PH_TEXT_MAX_LEN] = {0}; + struct ug_cbs cbs = {0}; + struct ug_priv_data *priv_data; + ui_gadget_h ug; + service_h service; + + priv_data = calloc(1, sizeof(struct ug_priv_data)); + p_retvm_if(NULL == priv_data, NULL, "calloc is failed"); + priv_data->destroy_cb = destroy_cb; + priv_data->cb_param = cb_param; + + service_create(&service); + + snprintf(buf, sizeof(buf), "%d", CT_UG_REQUEST_UPDATE_WITH_NUM); + service_add_extra_data(service, CT_UG_BUNDLE_TYPE, buf); + snprintf(buf, sizeof(buf), "%s", data); + service_add_extra_data(service, CT_UG_BUNDLE_NUM, buf); + PH_DBG("number %s", buf); + + cbs.priv = priv_data; + cbs.layout_cb = ug_common_layout_cb; + cbs.result_cb = NULL; + cbs.destroy_cb = ug_common_destroy_cb; + + ug = ug_create(parent_ug, UG_CONTACTS_LIST, UG_MODE_FULLVIEW, service, &cbs); + service_destroy(service); + if (NULL == ug) { + free(priv_data); + ERR("%s ug_create fail", UG_CONTACTS_LIST); + } + return ug; +} + +static void ug_contact_list_result_cb(ui_gadget_h ug, service_h service, void *priv) +{ + PH_FN_CALL; + char *temp_p = NULL; + struct ug_priv_data *priv_data = priv; + int id; + p_retm_if(NULL == priv, "private data is NULL"); + + service_get_extra_data(service, "id", &temp_p); + id = atoi(temp_p); + free(temp_p); + if (priv_data->result_cb) + priv_data->result_cb(priv_data->cb_param, (void*)&id); + PH_FN_END; +} + +void* phone_launch_contact_list_ug(void *parent_ug, int request, + void(*result_cb)(void*, void*), void(*destroy_cb)(void*), void*cb_param) +{ + PH_FN_CALL; + char ug_request[PH_TEXT_MAX_LEN] = {0}; + struct ug_priv_data *priv_data; + ui_gadget_h ug; + struct ug_cbs cbs = {0}; + service_h service; + + priv_data = calloc(1, sizeof(struct ug_priv_data)); + p_retvm_if(NULL == priv_data, NULL, "calloc is failed"); + priv_data->destroy_cb = destroy_cb; + priv_data->result_cb = result_cb; + priv_data->cb_param = cb_param; + + service_create(&service); + + snprintf(ug_request, sizeof(ug_request), "%d", request); + service_add_extra_data(service, CT_UG_BUNDLE_TYPE, ug_request); + + cbs.priv = priv_data; + cbs.layout_cb = ug_common_layout_cb; + cbs.destroy_cb = ug_common_destroy_cb; + cbs.result_cb = ug_contact_list_result_cb; + + ug = ug_create(parent_ug, UG_CONTACTS_LIST, UG_MODE_FULLVIEW, service, &cbs); + service_destroy(service); + if (NULL == ug) { + free(priv_data); + ERR("%s ug_create fail", UG_CONTACTS_LIST); + } + return ug; +} + +Evas_Object* phone_create_list_sweep_btn(Evas_Object *parent, char *fullpath) +{ + Evas_Object *btn; + Evas_Object *icon; + + btn = elm_button_add(parent); + p_retvm_if(NULL == btn, NULL, "elm_button_add() return NULL"); + + icon = elm_icon_add(btn); + p_retvm_if(NULL == icon, NULL, "phone_create_icon_with_img() return NULL"); + + elm_icon_file_set(icon, fullpath, NULL); + elm_icon_resizable_set(icon, EINA_FALSE, EINA_FALSE); + elm_object_part_content_set(btn, "icon", icon); + + return btn; +} + +void phone_resize_notify(Evas_Object *parent, int rotate) +{ + Evas_Object *notify; + Evas_Object *layout; + const char *msg; + p_retm_if(NULL == parent, "parent is NULL"); + notify = phone_get_notify(parent); + layout = elm_object_content_get(notify); + msg = edje_object_part_text_get(_EDJ(layout), "elm.text"); + + elm_layout_theme_set(layout, "standard", "selectioninfo", "center_text"); + + if (msg && *msg) + edje_object_part_text_set(_EDJ(layout), "elm.text", msg); +} + +Evas_Object * phone_get_notify(Evas_Object *parent) +{ + Evas_Object *notify; + + notify = evas_object_data_get(parent, "notify"); + if (NULL == notify) { + Evas_Object *info_layout; + notify = phone_create_base_layout(parent, false); + elm_object_part_content_set(parent, "selectioninfo", notify); + info_layout = elm_layout_add(parent); + elm_object_part_content_set(notify, "elm.swallow.content", info_layout); + elm_layout_theme_set(info_layout, "standard", "selectioninfo", "center_text"); + evas_object_data_set(parent, "notify", notify); + } + evas_object_show(notify); + return notify; +} + +void phone_show_tickernoti(const char *msg) +{ + int err = notification_status_message_post(msg); + p_retm_if(NOTIFICATION_ERROR_NONE != err, "notification_status_message_post is failed"); +} + +void phone_show_notify(Evas_Object *parent, const char *msg) +{ + Evas_Object *selectioninfo; + Evas_Object *notify; + + notify = phone_get_notify(parent); + selectioninfo = elm_object_part_content_get(notify, "elm.swallow.content"); + elm_object_part_text_set(selectioninfo, "elm.text", msg); + elm_object_signal_emit(parent, "show,selection,info", "elm"); + evas_object_show(selectioninfo); +} + +void phone_hide_notify(Evas_Object *parent) +{ + Evas_Object *selectioninfo; + Evas_Object *notify; + + notify = evas_object_data_get(parent, "notify"); + selectioninfo = elm_object_part_content_get(notify, "elm.swallow.content"); + elm_object_signal_emit(parent, "hide,selection,info", "elm"); + evas_object_hide(notify); + evas_object_hide(selectioninfo); +} + +static void popup_cancel_btn_cb(void *data, Evas_Object *obj, void *event_info) +{ + Evas_Object **popup = data; + evas_object_del(*popup); + *popup = NULL; +} + +void ctui_popup_hide_cb(void *data, Evas_Object *obj, void *event_info) +{ + evas_object_del(data); +} + +Evas_Object *phone_show_popup(Evas_Object *parent, const char *desc, double timeout) +{ + Evas_Object *popup; + + popup = evas_object_data_get(parent, "popup"); + if (NULL == popup) { + popup = elm_popup_add(parent); + evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + evas_object_smart_callback_add(popup, "block,clicked", ctui_popup_hide_cb, NULL); + evas_object_show(popup); + } + elm_object_text_set(popup, desc); + elm_popup_timeout_set(popup, timeout); + return popup; +} + +Evas_Object* phone_create_confirm_popup(Evas_Object **popup, Evas_Object *win, const char* description, + void (*btn_clicked_cb)(void *, Evas_Object *, void *), void *cb_data) +{ + Evas_Object *btn1, *btn2; + *popup = elm_popup_add(win); + evas_object_size_hint_weight_set(*popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + elm_object_text_set(*popup, description); + + btn1 = elm_button_add(*popup); + elm_object_text_set(btn1, S_(PH_SYS_SK_YES)); + elm_object_part_content_set(*popup, "button1", btn1); + elm_object_style_set(btn1, "popup_button/default"); + evas_object_smart_callback_add(btn1, "clicked", btn_clicked_cb, cb_data); + + btn2 = elm_button_add(*popup); + elm_object_text_set(btn2, S_(PH_SYS_SK_NO)); + elm_object_part_content_set(*popup, "button2", btn2); + elm_object_style_set(btn2, "popup_button/default"); + evas_object_smart_callback_add(btn2, "clicked", popup_cancel_btn_cb, &(*popup)); + + evas_object_show(*popup); + + return btn1; +} + +void phone_launch_voice_call(const int person_id, const char* number) +{ + char buf[PH_TEXT_MAX_LEN] = {0}; + service_h service; + int ret; + + ret = service_create(&service); + p_retm_if(SERVICE_ERROR_NONE != ret, "service_create() Failed(%d)", ret); + + service_set_operation(service, SERVICE_OPERATION_CALL); + snprintf(buf, sizeof(buf), "tel:%s", number); + service_set_uri(service, buf); + + if (0 < person_id) { + char buf[PH_TEXT_MAX_LEN] = {0}; + snprintf(buf, sizeof(buf), "%d", person_id); + service_add_extra_data(service, "ctindex", buf); + + } + service_send_launch_request(service, NULL, NULL); + service_destroy(service); +} + +Evas_Object* phone_create_base_layout(Evas_Object *parent, bool is_fullview) +{ + Evas_Object *base; + + base = elm_layout_add(parent); + p_retvm_if(NULL == base, NULL, "elm_layout_add() Failed"); + if (is_fullview) + elm_layout_theme_set(base, "layout", "application", "default"); + else + elm_layout_theme_set(base, "layout", "application", "noindicator"); + evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + + return base; +} + +void phone_ug_destroy_me_cb(void *data, Evas_Object *obj, void *event_info) +{ + PH_FN_CALL; + if(data) + ug_destroy_me(data); + PH_FN_END; +} + +void phone_back_btn_clicked_cb(void *data, Evas_Object *obj, void *event_info) +{ + Evas_Object *win = data; + elm_win_lower(win); +} + +Evas_Object* phone_create_bg(Evas_Object *parent) +{ + Evas_Object *bg; + + bg = elm_bg_add(parent); + p_retvm_if(NULL == bg, NULL, "elm_bg_add() return NULL"); + + evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + elm_object_part_content_set(parent, "elm.swallow.bg", bg); + + return bg; +} + +Evas_Object* phone_progressbar_popup(Evas_Object *parent, ph_progress_info *p_info, const char *title) +{ + char buf[PH_TEXT_MAX_LEN]; + Evas_Object *popup; + Evas_Object *layout; + Evas_Object *label; + Evas_Object *progressbar; + Evas_Object *btn; + + popup = elm_popup_add(parent); + p_retvm_if(NULL == popup, NULL, "elm_popup_add() return NULL"); + + layout = elm_layout_add(popup); + p_retvm_if(NULL == layout, NULL, "elm_layout_add() return NULL"); + elm_layout_file_set(layout, PROGRESS_EDJ, "popup_center_text_progressview"); + evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + p_info->layout = layout; + + label = elm_label_add(popup); + p_retvm_if(NULL == label, NULL, "elm_label_add() return NULL"); + elm_object_style_set(label, "popup/progressview"); + elm_label_line_wrap_set(label, ELM_WRAP_WORD); + elm_object_text_set(label, title); + evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0); + evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL); + evas_object_show(label); + + progressbar = elm_progressbar_add(popup); + p_retvm_if(NULL == progressbar, NULL, "elm_progressbar_add() return NULL"); + p_info->progressbar= progressbar; + + if (0 < p_info->cnt_checked_total) { + elm_object_style_set(progressbar, "list_progress"); + elm_progressbar_value_set(progressbar, 0.0); + } + else { + elm_object_style_set(progressbar, "pending_list"); + elm_progressbar_pulse(progressbar, EINA_TRUE); + } + + evas_object_size_hint_weight_set(progressbar, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); + evas_object_show(progressbar); + + elm_progressbar_horizontal_set(progressbar, EINA_TRUE); + elm_object_part_content_set(layout, "elm.swallow.content", label); + elm_object_part_content_set(layout, "elm.swallow.end", progressbar); + evas_object_show(layout); + elm_object_content_set(popup, layout); + + if(false == p_info->without_cancel_btn) + { + btn = elm_button_add(popup); + elm_object_text_set(btn, S_(PH_SYS_SK_CANCEL)); + elm_object_part_content_set(popup, "button1", btn); + elm_object_style_set(btn, "popup_button/default"); + evas_object_smart_callback_add(btn, "clicked", progress_popup_cancel_cb, &p_info->alive); + } + evas_object_show(popup); + return popup; +} + +void phone_navi_item_clear(Elm_Object_Item *navi_item) +{ + int i = 0; + Evas_Object *toolbar_button = NULL; + const char* toolbar_button_name[] = { + "toolbar_button1", + "toolbar_button2", + "toolbar_more_btn", + "title_left_btn", + "title_right_btn", + "prev_btn", + }; + + for (i=0;i<sizeof(toolbar_button_name)/sizeof(char*);i++) { + toolbar_button = elm_object_item_part_content_unset(navi_item, toolbar_button_name[i]); + if (toolbar_button) + evas_object_del(toolbar_button); + } +} + +Evas_Object *phone_create_icon_with_img(Evas_Object *parent, char *full_path) +{ + Evas_Object *ic = elm_icon_add(parent); + p_retvm_if(NULL == ic, NULL, "elm_icon_add() return NULL"); + elm_icon_file_set(ic, full_path, NULL); + elm_icon_resizable_set(ic, EINA_TRUE, EINA_TRUE); + evas_object_image_smooth_scale_set(ic, EINA_FALSE); + return ic; +} + + +contacts_error_e phone_cts_get_list(const char *view_uri, int property_id, int match_value, contacts_list_h *out_list) +{ + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_filter_h filter = NULL; + contacts_query_h query = NULL; + + p_retvm_if(NULL == out_list, CONTACTS_ERROR_INVALID_PARAMETER, "parameter(out_list) is NULL"); + + unsigned int person_grouprel_projection[] = { + _contacts_person_grouprel.person_id, + _contacts_person_grouprel.status, + _contacts_person_grouprel.display_name, + _contacts_person_grouprel.display_name_index, + _contacts_person_grouprel.addressbook_ids, + _contacts_person_grouprel.image_thumbnail_path, + _contacts_person_grouprel.link_count, + }; + + err = contacts_filter_create(view_uri, &filter); + p_retvm_if(CONTACTS_ERROR_NONE != err, err, "contactcs_filter_create() Failed(%d)", err); + + do { + if (CONTACTS_ERROR_NONE != (err = contacts_filter_add_int(filter, property_id, CONTACTS_MATCH_EQUAL, match_value))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_query_create(view_uri, &query))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_query_set_filter(query, filter))) break; + + if (0 == strcmp(view_uri, _contacts_person_grouprel._uri)) { + if (CONTACTS_ERROR_NONE != (err = contacts_query_set_distinct(query, true))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_query_set_projection(query, person_grouprel_projection, sizeof(person_grouprel_projection)/sizeof(int)))) break; + } + + if (CONTACTS_ERROR_NONE != (err = contacts_db_get_records_with_query(query, 0, 0, out_list))) break; + + contacts_filter_destroy(filter); + contacts_query_destroy(query); + + return CONTACTS_ERROR_NONE; + } while (0); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_query_create() Failed(%d)", err); + + contacts_filter_destroy(filter); + contacts_query_destroy(query); + + return err; +} + +void phone_cts_free_list(contacts_list_h list) +{ + contacts_error_e err = CONTACTS_ERROR_NONE; + err = contacts_list_destroy(list, true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_list_destroy() Failed(%d)", err); +} + +contacts_error_e phone_cts_get_int_from_list(contacts_list_h list, int property_id, int *out_int) +{ + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + + p_retvm_if(NULL == out_int, CONTACTS_ERROR_INVALID_PARAMETER, "parameter(out_int) is NULL"); + p_retvm_if(NULL == list, CONTACTS_ERROR_INVALID_PARAMETER, "parameter(list) is NULL"); + + err = contacts_list_get_current_record_p(list, &record); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_list_get_current_record_p() Failed(%d)", err); + return err; + } + + err = contacts_record_get_int(record, property_id, out_int); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_record_get_int() Failed(%d)", err); + return err; + } + + return CONTACTS_ERROR_NONE; +} + +contacts_error_e phone_cts_get_str_from_list(contacts_list_h list, int property_id, char **out_str) +{ + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + + p_retvm_if(NULL == out_str, CONTACTS_ERROR_INVALID_PARAMETER, "parameter(out_int) is NULL"); + p_retvm_if(NULL == list, CONTACTS_ERROR_INVALID_PARAMETER, "parameter(list) is NULL"); + + err = contacts_list_get_current_record_p(list, &record); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_list_get_current_record_p() Failed(%d)", err); + return err; + } + + err = contacts_record_get_str(record, property_id, out_str); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_record_get_str() Failed(%d)", err); + return err; + } + + return CONTACTS_ERROR_NONE; +} + +contacts_error_e phone_cts_get_bool_from_list(contacts_list_h list, int property_id, bool *out_bool) +{ + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + + p_retvm_if(NULL == out_bool, CONTACTS_ERROR_INVALID_PARAMETER, "parameter(out_bool) is NULL"); + p_retvm_if(NULL == list, CONTACTS_ERROR_INVALID_PARAMETER, "parameter(list) is NULL"); + + err = contacts_list_get_current_record_p(list, &record); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_list_get_current_record_p() Failed(%d)", err); + return err; + } + + err = contacts_record_get_bool(record, property_id, out_bool); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_record_get_bool() Failed(%d)", err); + return err; + } + + return CONTACTS_ERROR_NONE; +} + + +char* ctui_get_addressbook_icon_path(int addressbook_id) +{ + int ret; + int account_id; + char *icon_path; + char *addressbook_name = NULL; + account_h account; + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + + err = contacts_db_get_record(_contacts_address_book._uri, addressbook_id, &record); + p_retvm_if(CONTACTS_ERROR_NONE != err, NULL, "phone_cts_get_list() Failed(%d)", err); + + err = contacts_record_get_int(record, _contacts_address_book.account_id, &account_id); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_record_get_int() Failed(%d)", err); + err = contacts_record_destroy(record, true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_destroy() Failed(%d)", err); + return NULL; + } + err = contacts_record_get_str_p(record, _contacts_address_book.name, &addressbook_name); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_record_get_int() Failed(%d)", err); + err = contacts_record_destroy(record, true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_destroy() Failed(%d)", err); + return NULL; + } + + if (0 == account_id){ + return SAFE_STRDUP(CTUI_IMG_ACC_PHONE); + } + + + ret = account_connect(); + p_retvm_if(ACCOUNT_ERROR_NONE != ret, NULL, "account_connect() Failed(%d)", ret); + + account = NULL; + ret = account_create(&account); + p_retvm_if(ACCOUNT_ERROR_NONE != ret, false, "account_create() Failed(%d)", ret); + + ret = account_query_account_by_account_id(account_id, &account); + if (ACCOUNT_ERROR_NONE != ret) { + ERR("account_query_account_by_account_id() Failed(%d)", ret); + account_destroy(account); + account_disconnect(); + return NULL; + } + p_retvm_if(ACCOUNT_ERROR_NONE != ret, NULL, "account_query_account_by_account_id() Failed(%d)", ret); + + ret = account_get_icon_path(account, &icon_path); + if (ACCOUNT_ERROR_NONE != ret) { + ERR("account_get_icon_path() Failed(%d)", ret); + account_destroy(account); + account_disconnect(); + return NULL; + } + account_destroy(account); + account_disconnect(); + + return icon_path; +} + +Evas_Object* phone_create_navi_btn(Evas_Object *parent, const char *style) +{ + Evas_Object *btn; + + btn = elm_button_add(parent); + elm_object_style_set(btn, style); + evas_object_show(btn); + + return btn; +} + +void phone_set_view_info(ph_view_info *view_info, Evas_Object *win, Evas_Object *navi, + void *ug, int ug_request, int cnt_max, char *ug_data) +{ + view_info->win = win; + view_info->navi = navi; + view_info->ug = ug; + view_info->ug_request = ug_request; + view_info->ug_cnt_max = cnt_max; + view_info->ug_data = ug_data; +} + +void phone_list_ug_return_ids(ui_gadget_h ug, int *result_list, int cnt, const char *bd_key) +{ + gchar *encoded_str; + service_h service; + + service_create(&service); + + if (result_list) { + encoded_str = g_base64_encode((guchar *)result_list, cnt*sizeof(int)); + service_add_extra_data(service, bd_key, encoded_str); + g_free(encoded_str); + } + + ug_send_result(ug, service); + + service_destroy(service); +} + +void phone_list_ug_return_number_email_ids(ui_gadget_h ug, int *result_number_list, int number_list_count, int *result_email_list, int email_list_count) +{ + gchar *encoded_str; + service_h service; + service_create(&service); + + if (result_number_list && number_list_count) { + encoded_str = g_base64_encode((guchar *)result_number_list, number_list_count*sizeof(int)); + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_NUMBER_ID_LIST, encoded_str); + g_free(encoded_str); + } + + if (result_email_list && email_list_count) { + encoded_str = g_base64_encode((guchar *)result_email_list, email_list_count*sizeof(int)); + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_EMAIL_ID_LIST, encoded_str); + g_free(encoded_str); + } + + ug_send_result(ug, service); + service_destroy(service); +} + + +#define CT_UG_BUNDLE_RESULT_ID "id" +#define CT_UG_BUNDLE_RESULT_VCARD "vcard" + +void phone_ug_return_id(ui_gadget_h ug, int ct_id, int num_id, int email_id) +{ + service_h service; + char buf[16]; + + service_create(&service); + + if (ct_id) { + snprintf(buf, sizeof(buf), "%d", ct_id); + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_ID, buf); + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_PERSON_ID, buf); + } + + if (num_id) { + snprintf(buf, sizeof(buf), "%d", num_id); + service_remove_extra_data(service, CT_UG_BUNDLE_RESULT_ID); + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_ID, buf); + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_NUMBER_ID, buf); + } + + if (email_id) { + snprintf(buf, sizeof(buf), "%d", email_id); + service_remove_extra_data(service, CT_UG_BUNDLE_RESULT_ID); + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_ID, buf); + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_EMAIL_ID, buf); + } + + PH_DBG("ct_id =%d, num_id=%d,email_id=%d",ct_id,num_id,email_id); + ug_send_result(ug, service); + + service_destroy(service); +} + +void phone_ug_return_vcard(ui_gadget_h ug, int ct_id, char *vcard) +{ + service_h service; + char buf[16]; + + service_create(&service); + + if (ct_id) { + snprintf(buf, sizeof(buf), "%d", ct_id); + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_ID, buf); + } + + if (vcard) { + service_add_extra_data(service, CT_UG_BUNDLE_RESULT_VCARD, vcard); + } + + ug_send_result(ug, service); + + service_destroy(service); +} + +static void editfield_exceed_string_cb(void *data, Evas_Object *obj, void *event_info) +{ + phone_show_tickernoti(T_(CT_GET_TEXT_ERR, CTTEXT_EXCEED_LIMIT)); +} + +static void editfield_changed_cb(void *data, Evas_Object *obj, void *event_info) +{ + if (!elm_entry_is_empty(obj)) + elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm"); + + if (elm_object_focus_get(data)) { + if (elm_entry_is_empty(obj)) + elm_object_signal_emit(data, "elm,state,eraser,hide", "elm"); + else + elm_object_signal_emit(data, "elm,state,eraser,show", "elm"); + } +} +static void editfield_focused_cb(void *data, Evas_Object *obj, void *event_info) +{ + if (!elm_entry_is_empty(obj)) + elm_object_signal_emit(data, "elm,state,eraser,show", "elm"); + elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm"); +} + +static void editfield_unfocused_cb(void *data, Evas_Object *obj, void *event_info) +{ + if (elm_entry_is_empty(obj)) + elm_object_signal_emit(data, "elm,state,guidetext,show", "elm"); + elm_object_signal_emit(data, "elm,state,eraser,hide", "elm"); +} + +static void editfield_eraser_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source) +{ + elm_entry_entry_set(data, ""); +} + +Evas_Object* ctui_create_editfield(Evas_Object *parent, const char *title, const char *guide, bool multiline) +{ + Evas_Object *layout, *entry; + + layout = elm_layout_add(parent); + if (title && *title) { + elm_layout_theme_set(layout, "layout", "editfield", "title"); + elm_object_part_text_set(layout, "elm.text", title); + } + else + elm_layout_theme_set(layout, "layout", "editfield", "default"); + + if (guide && *guide) + elm_object_part_text_set(layout, "elm.guidetext", guide); + + entry = elm_entry_add(parent); + if (!multiline) { + elm_entry_scrollable_set(entry, EINA_TRUE); + elm_entry_single_line_set(entry, EINA_TRUE); + } + ctui_entry_limit_size_set(entry); + elm_entry_cnp_mode_set(entry, ELM_CNP_MODE_PLAINTEXT); + elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NORMAL); + evas_object_smart_callback_add(entry, "changed", editfield_changed_cb, layout); + evas_object_smart_callback_add(entry, "preedit,changed", editfield_changed_cb, layout); + evas_object_smart_callback_add(entry, "focused", editfield_focused_cb, layout); + evas_object_smart_callback_add(entry, "unfocused", editfield_unfocused_cb, layout); + evas_object_smart_callback_add(entry, "maxlength,reached", editfield_exceed_string_cb, layout); + elm_object_part_content_set(layout, "elm.swallow.content", entry); + elm_object_signal_callback_add(layout, "elm,eraser,clicked", "elm", editfield_eraser_clicked_cb, entry); + return layout; +} + +// This function is used in Contact view, Group view +void* ctui_account_ug(void *data, Evas_Object *obj, void *event_info, + void(*destroy_cb)(void*), void *cb_param) +{ + struct ug_cbs cbs = {0}; + ui_gadget_h ug; + service_h service; + struct ug_priv_data *priv_data; + + priv_data = calloc(1, sizeof(struct ug_priv_data)); + p_retvm_if(NULL == priv_data, NULL, "calloc is failed"); + priv_data->destroy_cb = destroy_cb; + priv_data->cb_param = cb_param; + + service_create(&service); + service_add_extra_data(service, "mode", "account_list"); + service_add_extra_data(service, "capability_filter", ACCOUNT_SUPPORTS_CAPABILITY_CONTACT); + + cbs.layout_cb = ug_common_layout_cb; + cbs.result_cb = NULL; + cbs.destroy_cb = ug_common_destroy_cb; + cbs.priv = priv_data; + + ug = ug_create(data, ACCOUNT_UG, UG_MODE_FULLVIEW, service, &cbs); + p_warn_if(NULL == ug, "ug_create() Failed"); + + service_destroy(service); + return ug; +} + +Evas_Object* phone_create_nocontents(Evas_Object *parent, const char* label) +{ + Evas_Object *layout; + + layout = elm_layout_add(parent); + p_retvm_if(NULL == layout, NULL, "elm_layout_add() return NULL"); + + elm_layout_theme_set(layout, "layout", "nocontents", "full"); + elm_object_part_text_set(layout, "elm.text", label); + + return layout; +} + +void ctui_genlist_append_separator_20px_with_line(Evas_Object *genlist) +{ + static Elm_Genlist_Item_Class itc = { .item_style="dialogue/separator/21/with_line"}; + + Elm_Object_Item *item; + item = elm_genlist_item_append(genlist, &itc, NULL, NULL, + ELM_GENLIST_ITEM_NONE, NULL, NULL); + elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY); +} + +void phone_toolbar_disabled_item_append(Evas_Object *toolbar, int cnt) +{ + int i; + Elm_Object_Item *item; + for (i=0;i<cnt;i++) { + item = elm_toolbar_item_append(toolbar, NULL, NULL, NULL, NULL); + elm_object_item_disabled_set(item, EINA_TRUE); + } +} + +void ctui_navi_left_btn_disabled_set(Elm_Object_Item *item, Eina_Bool disabled) +{ + Evas_Object *btn; + btn = elm_object_item_part_content_get(item, "title_left_btn"); + p_retm_if(NULL == btn, "elm_object_item_part_content_get() return NULL"); + elm_object_focus_set(btn, EINA_FALSE); + elm_object_disabled_set(btn, disabled); +} + +int ctui_get_mfc_option(void) +{ + int fd; + int size; + char buf[PH_TEXT_SHORT_LEN]; + + fd = open(SETTING_MFC_PATH, O_RDONLY); + if(fd < 0){ + ERR("Open(%s) O_RDONLY Failed", SETTING_MFC_PATH); + fd = open(SETTING_MFC_PATH, O_WRONLY|O_CREAT|O_TRUNC, 0644); + p_retvm_if(fd < 0, 0, "Open(%s) Failed", SETTING_MFC_PATH); + if (-1 == write(fd, "0", 1)) + ERR("write()... Failed(%s)", strerror(errno)); + } + size = read(fd, buf, sizeof(buf)); + close(fd); + if (0 < size) + return atoi(buf); + else + return 0; +} + +static void genlist_selectall_mouseup_cb(void *data, Evas *e, Evas_Object *obj, void *event_info) +{ + int x, y, w, h; + Eina_Bool is_checked; + Evas_Object *check; + Evas_Event_Mouse_Up *ev = event_info; + void *cb_data; + selectall_changed_cb changed_cb = data; + + if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) + return; + + evas_object_geometry_get(obj, &x, &y, &w, &h); + if (ev->output.y < y || y + h < ev->output.y) + return; + + p_retm_if(NULL == changed_cb, "changed_cb is NULL"); + + check = elm_object_part_content_get(obj, "elm.icon"); + p_retm_if(NULL == check, "elm_object_part_content_get() return NULL"); + + cb_data = evas_object_data_get(check, "cb_data"); + p_retm_if(NULL == cb_data, "cb_data is NULL"); + + is_checked = !elm_check_state_get(check); + elm_check_state_set(check, is_checked); + + changed_cb(cb_data, is_checked); +} + +static void genlist_selectall_changed_cb(void *data, Evas_Object *obj, void *event_info) +{ + Eina_Bool is_checked; + void *cb_data; + selectall_changed_cb changed_cb = data; + p_retm_if(NULL == changed_cb, "changed_cb is NULL"); + + cb_data = evas_object_data_get(obj, "cb_data"); + p_retm_if(NULL == cb_data, "cb_data is NULL"); + + is_checked = elm_check_state_get(obj); + changed_cb(cb_data, is_checked); +} + +Evas_Object* phone_create_select_all(Evas_Object *parent, const char *selectall_text, + selectall_changed_cb changed_cb, void *cb_data) +{ + Evas_Object *layout; + Evas_Object *check; + + p_retvm_if(NULL == parent, NULL, "parameter(parent) is NULL"); + + layout = elm_layout_add(parent); + p_retvm_if(NULL == layout, NULL, "elm_layout_add() return NULL"); + elm_layout_theme_set(layout, "genlist", "item", "select_all/default"); + evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, 0); + evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL); + evas_object_event_callback_add(layout, EVAS_CALLBACK_MOUSE_UP, genlist_selectall_mouseup_cb, changed_cb); + + check = elm_check_add(layout); + p_retvm_if(NULL == check, NULL, "elm_check_add() return NULL"); + evas_object_propagate_events_set(check, EINA_FALSE); + elm_object_part_content_set(layout, "elm.icon", check); + evas_object_smart_callback_add(check, "changed", genlist_selectall_changed_cb, changed_cb); + evas_object_data_set(check, "cb_data", cb_data); + + elm_object_part_text_set(layout, "elm.text", selectall_text); + evas_object_show(layout); + + return layout; +} + +void phone_move_ctxpopup(Evas_Object *popup, Evas_Object *item) +{ + int x,y,w,h; + int pos_x; + int pos_y; + p_retm_if(NULL == popup, "parameter(popup) is NULL"); + p_retm_if(NULL == item, "parameter(item) is NULL"); + + evas_object_geometry_get(item, &x, &y, &w, &h); + + pos_x = x + (Evas_Coord)w/2; + pos_y = y + (Evas_Coord)h/2; + + evas_object_move(popup, pos_x, pos_y); +} + +void ctui_get_addressbook_name(int addressbook_id, char *dest, int dest_len) +{ + char *display_name = NULL; + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + + p_retm_if(NULL == dest, "parameter(dest) is NULL"); + dest[0] = '\0'; + + if (addressbook_id <= 0) { + snprintf(dest, dest_len, "%s", T_(CT_GET_TEXT_BASIC, CTTEXT_PHONE)); + return; + } + + err = contacts_db_get_record(_contacts_address_book._uri, addressbook_id, &record); + p_retm_if(CONTACTS_ERROR_NONE != err, "contacts_db_get_record() Failed(%d)", err); + + err = contacts_record_get_str_p(record, _contacts_address_book.name, &display_name); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_get_str_p() Failed(%d)", err); + + if (display_name && *display_name) + snprintf(dest, dest_len, "%s", display_name); + + err = contacts_record_destroy(record, true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_destroy() Failed(%d)", err); +} + +void ctui_get_account_name_by_address_book_id(int address_book_id, char *dest, int dest_len) +{ + PH_FN_CALL; + int ret; + int account_id; + char *domain_name; + account_h account; + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + + err = contacts_db_get_record(_contacts_address_book._uri, address_book_id, &record); + p_retm_if(CONTACTS_ERROR_NONE != err, "phone_cts_get_list() Failed(%d)", err); + + err = contacts_record_get_int(record, _contacts_address_book.account_id, &account_id); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_record_get_int() Failed(%d)", err); + err = contacts_record_destroy(record, true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_destroy() Failed(%d)", err); + return; + } + + err = contacts_record_destroy(record, true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_destroy() Failed(%d)", err); + + if (0 == account_id) + return; + + ret = account_connect(); + p_retm_if(ACCOUNT_ERROR_NONE != ret, "account_connect() Failed(%d)", ret); + + account = NULL; + ret = account_create(&account); + if (ACCOUNT_ERROR_NONE != ret) { + ERR("account_create() Failed(%d)", ret); + account_disconnect(); + return; + } + + ret = account_query_account_by_account_id(account_id, &account); + if (ACCOUNT_ERROR_NONE != ret) { + ERR("account_query_account_by_account_id() Failed(%d)", ret); + account_destroy(account); + account_disconnect(); + return; + } + + ret = account_get_domain_name(account, &domain_name); + if (ACCOUNT_ERROR_NONE != ret) { + ERR("account_get_display_name() Failed(%d)", ret); + account_destroy(account); + account_disconnect(); + return; + } + PH_DBG("domain_name = %s", domain_name); + + account_destroy(account); + account_disconnect(); + + if (domain_name && *domain_name) + snprintf(dest, dest_len, "%s", domain_name); + + free(domain_name); +} + +// TODO: Fixme +Eina_List* ctui_person_get_writable_contact_list(int person_id) +{ + int addressbook_id; + int addressbook_mode; + Eina_List *list_contact_record = NULL; + + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_list_h list_contact = NULL; + contacts_record_h record_addressbook = NULL; + contacts_record_h record_contact = NULL; + contacts_record_h record_contact_clone = NULL; + + p_retvm_if(person_id <= 0, NULL, "Invalid parameter(%d)", person_id); + + err = phone_cts_get_list(_contacts_contact._uri, _contacts_contact.person_id, person_id, &list_contact); + p_retvm_if(CONTACTS_ERROR_NONE != err, NULL, "phone_cts_get_list() Failed(%d)", err); + + while (CONTACTS_ERROR_NONE == err) { + err = phone_cts_get_int_from_list(list_contact, _contacts_contact.address_book_id, &addressbook_id); + if (CONTACTS_ERROR_NONE != err) { + ERR("phone_cts_get_int_from_list() Failed(%d)", err); + break; + } + + err = contacts_db_get_record(_contacts_address_book._uri, addressbook_id, &record_addressbook); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_db_get_record() Failed(%d)", err); + break; + } + + err = contacts_record_get_int(record_addressbook, _contacts_address_book.mode, &addressbook_mode); + if (CONTACTS_ERROR_NONE != err) { + ERR("phone_cts_get_int_from_list() Failed(%d)", err); + err = contacts_record_destroy(record_addressbook, true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_destroy() Failed(%d)", err); + break; + } + + err = contacts_record_destroy(record_addressbook, true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_destroy() Failed(%d)", err); + + // TODO: Fixme + if (1 == addressbook_mode) + continue; + + err = contacts_list_get_current_record_p(list_contact, &record_contact); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_list_get_current_record_p() Failed(%d)", err); + break; + } + + err = contacts_record_clone(record_contact, &record_contact_clone); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_record_clone() Failed(%d)", err); + break; + } + + list_contact_record = eina_list_append(list_contact_record, record_contact_clone); + + err = contacts_list_next(list_contact); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_list_next() Failed(%d)", err); + break; + } + } + phone_cts_free_list(list_contact); + + return list_contact_record; +} + +static bool __phone_list_is_email_exist(Eina_List *list, char *email_address) +{ + Eina_List *l; + char *email_address_temp; + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record_temp = NULL; + + EINA_LIST_FOREACH(list, l, record_temp) { + if (NULL == record_temp) + continue; + + err = contacts_record_get_str_p(record_temp, _contacts_email.email, &email_address_temp); + p_retvm_if(CONTACTS_ERROR_NONE != err, false, "contacts_record_get_str_p() Failed(%d)", err); + + if (NULL == email_address_temp) + continue; + + if (0 == strcmp(email_address_temp, email_address)) + return true; + } + return false; +} + +Eina_List* ctui_person_get_email_list(Eina_List *contact_list) +{ + Eina_Bool checkduplicate = false; + int index = 0; + char *email_address; + Eina_List *l; + Eina_List *list_email_record = NULL; + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + contacts_record_h record_email = NULL; + contacts_record_h record_clone = NULL; + + EINA_LIST_FOREACH(contact_list, l, record) { + index = 0; + while (CONTACTS_ERROR_NONE == contacts_record_get_child_record_at_p(record, _contacts_contact.email, index++, &record_email)) { + err = contacts_record_get_str_p(record_email, _contacts_email.email, &email_address); + if (CONTACTS_ERROR_NONE != err) { + ERR("phone_cts_get_str_from_list() Failed(%d)", err); + break; + } + if( checkduplicate ){ + if (__phone_list_is_email_exist(list_email_record, email_address)) { + ERR("__phone_list_is_email_exist() return true (email address = %s", email_address); + continue; + } + } + err = contacts_record_clone(record_email, &record_clone); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_list_get_current_record_p() Failed(%d)", err); + break; + } + list_email_record = eina_list_append(list_email_record, record_clone); + } + checkduplicate = true; + } + return list_email_record; +} + +static bool __phone_list_is_number_exist(Eina_List *list, char *number_str) +{ + Eina_List *l; + char *number_str_temp; + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record_temp = NULL; + + EINA_LIST_FOREACH(list, l, record_temp) { + if (NULL == record_temp) + continue; + + err = contacts_record_get_str_p(record_temp, _contacts_number.number, &number_str_temp); + p_retvm_if(CONTACTS_ERROR_NONE != err, false, "contacts_record_get_str_p() Failed(%d)", err); + + if (NULL == number_str_temp) + continue; + + if (0 == strcmp(number_str_temp, number_str)) + return true; + } + return false; +} + +Eina_List* ctui_person_get_number_list(Eina_List *contact_list) +{ + Eina_Bool checkduplicate = false; + int index = 0; + char *number_str; + Eina_List *l; + Eina_List *list_number_record = NULL; + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + contacts_record_h record_number = NULL; + contacts_record_h record_clone = NULL; + + EINA_LIST_FOREACH(contact_list, l, record) { + index = 0; + while (CONTACTS_ERROR_NONE == contacts_record_get_child_record_at_p(record, _contacts_contact.number, index++, &record_number)) { + err = contacts_record_get_str_p(record_number, _contacts_number.number, &number_str); + if (CONTACTS_ERROR_NONE != err) { + ERR("phone_cts_get_str_from_list() Failed(%d)", err); + break; + } + if( checkduplicate ){ + if (__phone_list_is_number_exist(list_number_record, number_str)) { + ERR("__phone_list_is_number_exist() return true (number = %s", number_str); + continue; + } + } + err = contacts_record_clone(record_number, &record_clone); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_list_get_current_record_p() Failed(%d)", err); + break; + } + list_number_record = eina_list_append(list_number_record, record_clone); + } + checkduplicate = true; + } + return list_number_record; +} + +Eina_List* ctui_person_get_contact_list(int person_id) +{ + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record_contact = NULL; + contacts_record_h record_contact_clone = NULL; + contacts_list_h list_contacts = NULL; + Eina_List *list_contact_records = NULL; + + err = phone_cts_get_list(_contacts_contact._uri, _contacts_contact.person_id, person_id, &list_contacts); + p_retvm_if(CONTACTS_ERROR_NONE != err, NULL, "phone_cts_get_list() Failed(%d)", err); + + while (err == CONTACTS_ERROR_NONE) { + err = contacts_list_get_current_record_p(list_contacts, &record_contact); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_list_get_current_record_p() Failed(%d)", err); + break; + } + + err = contacts_record_clone(record_contact, &record_contact_clone); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_record_clone() Failed(%d)", err); + break; + } + + list_contact_records = eina_list_append(list_contact_records, record_contact_clone); + + err = contacts_list_next(list_contacts); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_list_next() Failed(%d)", err); + } + phone_cts_free_list(list_contacts); + + return list_contact_records; +} + +void ctui_free_record_list(Eina_List *contact_list) +{ + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + p_retm_if(NULL == contact_list, "contact_list is NULL"); + + EINA_LIST_FREE(contact_list, record) { + if (NULL == record) continue; + err = contacts_record_destroy(record, true); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_record_destroy() Failed(%d)", err); + continue; + } + } +} + +// TODO: Fixme +int ctui_get_email_type_index(int type) +{ + switch (type) { + case CONTACTS_EMAIL_TYPE_CUSTOM: + return 1; + case CONTACTS_EMAIL_TYPE_HOME: + return 2; + case CONTACTS_EMAIL_TYPE_WORK: + return 3; + default: + return 0; + } +} + +int ctui_get_view_mode(void) +{ + PH_FN_CALL; + int fd; + int size; + char buf[PH_TEXT_SHORT_LEN]; + + fd = open(VIEW_MODE_PATH, O_RDONLY); + if(fd < 0) { + ctui_set_view_mode(-1); + PH_DBG("Open(%s) Failed so create new", VIEW_MODE_PATH); + return -1; + } + size = read(fd, buf, sizeof(buf)); + close(fd); + if (0 < size) + return atoi(buf); + else + return -1; +} + +void ctui_set_view_mode(int addressbook_id) +{ + PH_FN_CALL; + int fd; + char buf[PH_TEXT_SHORT_LEN] = {0}; + + fd = open(VIEW_MODE_PATH, O_WRONLY|O_CREAT|O_TRUNC, 0644); + p_retm_if(fd < 0, "Open(%s) Failed", VIEW_MODE_PATH); + snprintf(buf, sizeof(buf), "%d",addressbook_id); + if (-1 == write(fd, buf, strlen(buf))) + ERR("write()... Failed(%s)", strerror(errno)); + close(fd); +} + +contacts_error_e phone_cts_get_default_number(int person_id, contacts_record_h *record_clone) +{ + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + contacts_list_h list = NULL; + contacts_filter_h filter = NULL; + contacts_query_h query = NULL; + + p_retvm_if(NULL == record_clone, CONTACTS_ERROR_INVALID_PARAMETER, "parameter(record_clone) is NULL"); + + err = contacts_query_create(_contacts_person_number._uri, &query); + p_retvm_if(CONTACTS_ERROR_NONE != err, err, "contacts_query_create() Failed(%d)", err); + + err = contacts_filter_create(_contacts_person_number._uri, &filter); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_filter_create() Failed(%d)", err); + contacts_query_destroy(query); + return err; + } + + do { + if (CONTACTS_ERROR_NONE != (err = contacts_filter_add_int(filter, _contacts_person_number.person_id, CONTACTS_MATCH_EQUAL, person_id))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_filter_add_bool(filter, _contacts_person_number.is_primary_default, true))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_query_set_filter(query, filter))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_db_get_records_with_query(query, 0, 0, &list))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_list_get_current_record_p(list, &record))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_record_clone(record, record_clone))) break; + + contacts_query_destroy(query); + contacts_filter_destroy(filter); + contacts_list_destroy(list, true); + + return CONTACTS_ERROR_NONE; + } while (0); + + contacts_query_destroy(query); + contacts_filter_destroy(filter); + if (list) + contacts_list_destroy(list, true); + + return err; +} + +contacts_error_e phone_cts_get_default_email(int person_id, contacts_record_h *record_clone) +{ + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + contacts_list_h list = NULL; + contacts_filter_h filter = NULL; + contacts_query_h query = NULL; + + p_retvm_if(NULL == record_clone, CONTACTS_ERROR_INVALID_PARAMETER, "parameter(record_clone) is NULL"); + + err = contacts_query_create(_contacts_person_email._uri, &query); + p_retvm_if(CONTACTS_ERROR_NONE != err, err, "contacts_query_create() Failed(%d)", err); + + err = contacts_filter_create(_contacts_person_email._uri, &filter); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_filter_create() Failed(%d)", err); + contacts_query_destroy(query); + return err; + } + + do { + if (CONTACTS_ERROR_NONE != (err = contacts_filter_add_int(filter, _contacts_person_email.person_id, CONTACTS_MATCH_EQUAL, person_id))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_filter_add_bool(filter, _contacts_person_email.is_primary_default, true))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_query_set_filter(query, filter))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_db_get_records_with_query(query, 0, 0, &list))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_list_get_current_record_p(list, &record))) break; + if (CONTACTS_ERROR_NONE != (err = contacts_record_clone(record, record_clone))) break; + + contacts_query_destroy(query); + contacts_filter_destroy(filter); + contacts_list_destroy(list, true); + + return CONTACTS_ERROR_NONE; + } while (0); + + contacts_query_destroy(query); + contacts_filter_destroy(filter); + + if (list) + contacts_list_destroy(list, true); + return err; +} + +bool ctui_list_make_vcard(int person_id, char *path, int path_length) +{ + int fd; + char *display = NULL; + char *vcard = NULL; + contacts_record_h record = NULL; + contacts_error_e err = CONTACTS_ERROR_NONE; + + err = contacts_db_get_record(_contacts_person._uri, person_id, &record); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_db_get_record() Failed(%d)", err); + return false; + } + + err = contacts_record_get_str_p(record, _contacts_person.display_name, &display); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_get_str_p() Failed(%d)", err); + + if (display && *display) + snprintf(path, path_length, "%s/%s.vcf", UGDATAIDR, display); + else + snprintf(path, path_length, "%s/%s.vcf", UGDATAIDR, T_(CT_GET_TEXT_ERR, CTTEXT_NONAME)); + + err = contacts_vcard_make_from_person(record, &vcard); + if (CONTACTS_ERROR_NONE != err) { + ERR("contacts_vcard_make_from_person() Failed(%d)", err); + err = contacts_record_destroy(record, true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_destroy() Failed(%d)", err); + return false; + } + + fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644); + + if (-1 == write(fd, vcard, strlen(vcard))) { + ERR("write()... Failed(%s)", strerror(errno)); + free(vcard); + return false; + } + free(vcard); + return true; +} + + +int ctui_get_addressbook_id_by_index(Eina_List *list, int index) +{ + p_retvm_if(NULL == list, -1, "list is NULL"); + void *addressbook_id; + + if(eina_list_count(list) <= index) + { + ERR("index is bigger than count"); + return -1; + } + + addressbook_id = eina_list_nth(list, index); + + return (int)addressbook_id; +} + +Eina_List* ctui_get_addressbook_id_list(const char *ori_string, const char *sep) +{ + Eina_List *e_list = NULL; + char *p = NULL; + char *copy_string; + char *save_p; + + if (!ori_string) { + PH_DBG("Input string is NULL"); + return NULL; + } + + copy_string = SAFE_STRDUP(ori_string); + + if (copy_string) { + p = strtok_r(copy_string, sep, &save_p); + + while (p) { + int id = atoi(p); + if(eina_list_data_find(e_list, (void*)id) == NULL) + e_list = eina_list_append(e_list, (void*)id); + + p = strtok_r(NULL, sep, &save_p); + } + + free(copy_string); + } + + return e_list; +} + + +Eina_List* __make_tokenize_list(const char *ori_string, const char *sep) +{ + Eina_List *e_list = NULL; + char *p = NULL; + char *copy_string; + char *save_p; + + if (!ori_string) { + PH_DBG("Input string is NULL"); + return NULL; + } + + copy_string = SAFE_STRDUP(ori_string); + + if (copy_string) { + p = strtok_r(copy_string, sep, &save_p); + + if (p) + PH_DBG("list item = %s", p); + + while (p) { + char *c = SAFE_STRDUP(p); + + e_list = eina_list_append(e_list, c); + + p = strtok_r(NULL, sep, &save_p); + + if (p) + PH_DBG("list item = %s", p); + } + + free(copy_string); + } + + return e_list; +} + + + +contacts_record_h ctui_get_record_image(contacts_record_h record_contact) +{ + PH_FN_CALL; + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_record_h record = NULL; + p_retvm_if(NULL == record_contact, NULL, "parameter(record_contact) is NULL"); + + err = contacts_record_get_child_record_at_p(record_contact, _contacts_contact.image, 0, &record); + if (NULL != record) return record; + + err = contacts_record_create(_contacts_image._uri, &record); + p_retvm_if(CONTACTS_ERROR_NONE != err, NULL, "contacts_record_create() Failed(%d)", err); + + err = contacts_record_add_child_record(record_contact, _contacts_contact.image, record); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_record_add_child_record() Failed(%d)", err); + + return record; +} + + +Evas_Object *phone_create_toolbar_btn(Evas_Object *parent, const char *text, Evas_Smart_Cb func, void *data) +{ + Evas_Object *btn = elm_button_add(parent); + if (!btn) return NULL; + elm_object_style_set(btn, "naviframe/toolbar/default"); + elm_object_text_set(btn, text); + evas_object_smart_callback_add(btn, "clicked", func, data); + return btn; +} + +Evas_Object* ctui_create_more_btn(Evas_Object *parent, Evas_Smart_Cb func, void *data) +{ + Evas_Object *btn = elm_button_add(parent); + if (!btn) return NULL; + elm_object_style_set(btn, "naviframe/more/default"); + evas_object_smart_callback_add(btn, "clicked", func, data); + return btn; +} + +int ctui_get_addressbook_id(char* addressbook_name) +{ + PH_FN_CALL; + PH_DBG("addressbook_name = %s", addressbook_name); + contacts_error_e err = CONTACTS_ERROR_NONE; + contacts_filter_h filter = NULL; + contacts_query_h query = NULL; + contacts_list_h list = NULL; + contacts_record_h record = NULL; + unsigned int count =0; + int addressbook_id =-1;//0 is phone addressbook id + + err = contacts_filter_create(_contacts_address_book._uri, &filter); + p_warn_if(CONTACTS_ERROR_NONE != err, "contactcs_filter_create() Failed(%d)", err); + + + err = contacts_filter_add_str(filter,_contacts_address_book.name , CONTACTS_MATCH_FULLSTRING, addressbook_name); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_filter_add_str() Failed(%d)", err); + err = contacts_query_create(_contacts_address_book._uri, &query); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_query_create() Failed(%d)", err); + err = contacts_query_set_filter(query, filter); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_query_set_filter() Failed(%d)", err); + err = contacts_db_get_records_with_query(query, 0, 0, &list); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_db_get_records_with_query() Failed(%d)", err); + + if(list != NULL) + { + err = contacts_list_get_count(list,&count ); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_list_get_count() Failed(%d)", err); + PH_DBG("count = %d",count); + if(count > 0){ + err = contacts_list_get_current_record_p(list, &record); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_list_get_current_record_p() Failed(%d)", err); + contacts_record_get_int(record,_contacts_address_book.id,&addressbook_id); + err = contacts_list_destroy(list,true); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_list_destroy() Failed(%d)", err); + } + } + err = contacts_filter_destroy(filter); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_filter_destroy() Failed(%d)", err); + err = contacts_query_destroy(query); + p_warn_if(CONTACTS_ERROR_NONE != err, "contacts_query_destroy() Failed(%d)", err); + + PH_DBG("addressbook id = %d",addressbook_id); + return addressbook_id; + +} + +void ctui_create_contacts_error_popup(Evas_Object *parent, int err) +{ + p_retm_if(CONTACTS_ERROR_NONE == err, "ctui_create_contacts_error_popup no errer"); + p_retm_if(NULL == parent, "ctui_create_contacts_error_popup parent is null"); + + ERR("ctui_create_contacts_error_popup() err(%d)", err); + switch (err) { + case CONTACTS_ERROR_DB: + phone_show_popup(parent, T_(CT_GET_TEXT_ERR, CTTEXT_ERR_DB_LOCKED), 1.5); + break; + case CONTACTS_ERROR_FILE_NO_SPACE: + phone_show_popup(parent, S_(CT_SYS_POP_NOT_ENOUGH_MEMORY), 1.5); + break; + default: + phone_show_popup(parent, S_(PH_SYS_POP_FAILED), 1.5); + break; + } + + return; + +} + |