Fix memory leak
[platform/core/uifw/inputdelegator.git] / src / w-input-keyboard.cpp
index 410e0f0..5e9ae09 100755 (executable)
 
 #include "w-input-keyboard.h"
 #include "w-input-selector.h"
-#include "w-input-smartreply.h"
 
 extern InputKeyboardData g_input_keyboard_data;
 extern App_Data* app_data;
 
 Evas_Object *entry;
 
-static void input_keyboard_app_control_send_reply_cb(app_control_h request, app_control_h reply,
-                                               app_control_result_e result, void *user_data);
-
-static void input_keyboard_app_control_send_reply_cb(app_control_h request, app_control_h reply,
-                                               app_control_result_e result, void *user_data)
-{
-    char *feedback = NULL;
-       if (result == APP_CONTROL_RESULT_SUCCEEDED) {
-        if (reply) {
-                       int ret;
-                       ret = app_control_get_extra_data(reply,
-                                       "template_feedback", &feedback);
-
-                       if (ret == APP_CONTROL_ERROR_NONE) {
-                               if (feedback) {
-                                       input_smartreply_send_feedback(feedback);
-                               }
-                       }
-               }
-               reply_to_sender_by_callback(feedback, "keyboard");
-        free(feedback);
-               elm_exit();
-       }
-}
-
-
 bool input_keyboard_init(app_control_h app_control)
 {
        int ret = -1;
-
-       char *app_id = NULL;
-       char **data_array = NULL;
-       int data_array_len = -1;
-    char *default_text = NULL;
-    char *guide_text = NULL;
+       char *default_text = NULL;
+       char *guide_text = NULL;
+       char *return_key_type = _("IDS_AMEMO_BUTTON_SEND");
+       char *max_text_length = NULL;
+       char *cursor_position_set = NULL;
 
        input_keyboard_deinit();
 
-    ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_INPUT_DEFAULT_TEXT, &default_text);
-    if (ret == APP_CONTROL_ERROR_NONE) {
-        g_input_keyboard_data.default_text = default_text;
-    }
-    ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_INPUT_GUIDE_TEXT, &guide_text);
-    if (ret == APP_CONTROL_ERROR_NONE) {
-        g_input_keyboard_data.guide_text = guide_text;
-    }
-
-       ret = app_control_get_extra_data(app_control,
-                                       "selector_keyboard_app_id",
-                                       &app_id);
-
-       if (ret != APP_CONTROL_ERROR_NONE) {
-               PRINTFUNC(DLOG_WARN, "can not get selector_keyboard_app_id: %d", ret);
-               return false;
+       ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_INPUT_DEFAULT_TEXT, &default_text);
+       if (ret == APP_CONTROL_ERROR_NONE) {
+               g_input_keyboard_data.default_text = default_text;
        }
-
-       ret = app_control_get_extra_data_array(app_control,
-                                       "selector_keyboard_data_array",
-                                       &data_array, &data_array_len);
-
-
-       if (ret != APP_CONTROL_ERROR_NONE) {
-               if (app_id)
-                       free(app_id);
-
-               PRINTFUNC(DLOG_WARN, "can not get selector_keyboard_data_array : %d", ret);
-               return false;
+       ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_INPUT_GUIDE_TEXT, &guide_text);
+       if (ret == APP_CONTROL_ERROR_NONE) {
+               g_input_keyboard_data.guide_text = guide_text;
+       }
+       ret = app_control_get_extra_data(app_control, "return_key_type", &return_key_type);
+       if (ret == APP_CONTROL_ERROR_NONE) {
+               g_input_keyboard_data.return_key_type = return_key_type;
+       }
+       ret = app_control_get_extra_data(app_control, "max_text_length", &max_text_length);
+       if (ret == APP_CONTROL_ERROR_NONE) {
+               g_input_keyboard_data.max_text_length = atoi(max_text_length);
+               free(max_text_length);
+       }
+       ret = app_control_get_extra_data(app_control, "cursor_position_set", &cursor_position_set);
+       if (ret == APP_CONTROL_ERROR_NONE) {
+               g_input_keyboard_data.cursor_position_set = atoi(cursor_position_set);
+               free(cursor_position_set);
        }
-
-       if (app_id)
-               g_input_keyboard_data.app_id = app_id;
-
-       if (data_array)
-               g_input_keyboard_data.data_array = data_array;
-
-       g_input_keyboard_data.data_array_len = data_array_len;
-
-       PRINTFUNC(DLOG_WARN, "app_id = %s", app_id);
-       PRINTFUNC(DLOG_WARN, "data_array = %s", data_array);
 
        return true;
 }
 
 void input_keyboard_deinit(void)
 {
-       int i = 0;
-       char **data_array = NULL;
-
        if (g_input_keyboard_data.guide_text)
                free(g_input_keyboard_data.guide_text);
 
        if (g_input_keyboard_data.default_text)
                free(g_input_keyboard_data.default_text);
 
-       if (g_input_keyboard_data.app_id)
-               free(g_input_keyboard_data.app_id);
-
-       data_array = g_input_keyboard_data.data_array;
-       if (data_array) {
-               for (i = 0; i < g_input_keyboard_data.data_array_len; i++) {
-                       if (*(data_array + i))
-                               free(*(data_array + i));
-               }
-
-               free(data_array);
-       }
+       if (g_input_keyboard_data.return_key_type)
+               free(g_input_keyboard_data.return_key_type);
 
-       g_input_keyboard_data.app_id = NULL;
-       g_input_keyboard_data.data_array = NULL;
-       g_input_keyboard_data.data_array_len = 0;
+       g_input_keyboard_data.default_text = NULL;
+       g_input_keyboard_data.guide_text = NULL;
+       g_input_keyboard_data.return_key_type = _("IDS_AMEMO_BUTTON_SEND");
+       g_input_keyboard_data.max_text_length = KEYBOARD_EDITOR_CHAR_COUNT_MAX;
+       g_input_keyboard_data.cursor_position_set = 0;
 
        return;
 }
 
-bool input_keyboard_launch(Evas_Object *window)
+void exit_keyboard()
 {
-       int ret;
-
-       char *app_id;
-
        app_control_h app_control;
-
-       if (window == NULL) {
-               PRINTFUNC(DLOG_ERROR, "Can not get window");
-               return false;
-       }
-
-       ret = app_control_create(&app_control);
+       int ret = app_control_create(&app_control);
        if (ret != APP_CONTROL_ERROR_NONE) {
                PRINTFUNC(DLOG_ERROR, "Can not create app_control : %d", ret);
-               return false;
+               return;
        }
 
-       app_id = g_input_keyboard_data.app_id;
-       if (app_id == NULL) {
-               PRINTFUNC(DLOG_INFO, "app id is undefined, use defualt");
-               app_id = "com.samsung.message.appcontrol.compose";
-       }
+       const char *getText = elm_entry_entry_get(entry);
+       SECURE_LOGD("button key clicked!! : getText = %s", getText);
 
-       app_control_set_app_id(app_control, app_id);
-       app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
-/*
-       ret =  app_control_set_window(app_control,
-                               elm_win_xwindow_get(window));
-       if (ret != APP_CONTROL_ERROR_NONE) {
-               PRINTFUNC(DLOG_ERROR, "Can not app control set window : %d", ret);
-               app_control_destroy(app_control);
-               return false;
-       }
-*/
-       if (g_input_keyboard_data.data_array && g_input_keyboard_data.data_array_len > 0) {
-                app_control_add_extra_data_array(app_control,
-                                                "selector_keyboard_data_array",
-                                                (const char**)(g_input_keyboard_data.data_array),
-                                                g_input_keyboard_data.data_array_len);
-       }
+       char cursorPosition[512];
+       snprintf(cursorPosition, sizeof(cursorPosition), "%d", elm_entry_cursor_pos_get(entry));
 
+       char *app_id = NULL;
+       app_control_get_caller(app_data->source_app_control, &app_id);
+       if (app_id != NULL)
+               app_control_set_app_id(app_control, app_id);
+       app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
        set_source_caller_app_id(app_control);
+       free(app_id);
+       reply_to_sender_by_callback(getText, "keyboard", NULL, cursorPosition);
+       ui_app_exit();
+}
 
-       ret = app_control_send_launch_request(app_control,
-                                       input_keyboard_app_control_send_reply_cb,
-                                       NULL);
-       if (ret != APP_CONTROL_ERROR_NONE)
-               PRINTFUNC(DLOG_ERROR, "Can not launch app_control: %d", ret);
+void btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
+{
+       exit_keyboard();
+}
 
-       app_control_destroy(app_control);
+static Eina_Bool custom_back_cb(void *data, Elm_Object_Item *it)
+{
+       _back_to_genlist_for_selector();
+       return EINA_TRUE;
+}
 
-       return true;
+static void maxlength_cb(void *data, Evas_Object *obj, void *event_info)
+{
+       LOGD("maxlength_cb : size = %d", KEYBOARD_EDITOR_CHAR_COUNT_MAX);
+       char text[512];
+       const char *guide = _(MAX_TEXT_LENGTH_REACH);
+       snprintf(text, sizeof(text), guide, g_input_keyboard_data.max_text_length);
+       show_popup_toast((const char *)text, false);
 }
 
-void btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
+static void enter_keydown_cb(void *data, Evas_Object *obj, void *event_info)
 {
-    app_control_h app_control;
-    int ret = app_control_create(&app_control);
-       if (ret != APP_CONTROL_ERROR_NONE) {
-               PRINTFUNC(DLOG_ERROR, "Can not create app_control : %d", ret);
+       LOGD("enter_keydown_cb");
+       Evas_Object *entry = obj;
+       if (entry == NULL)
                return;
-       }
-
-    const char *getText = elm_entry_entry_get(entry);
-    LOGD("button key clicked!! : getText = %s", getText);
 
-    if(getText)
-        input_smartreply_send_feedback(getText);
+       Ecore_IMF_Context *imf_context = NULL;
+       imf_context = (Ecore_IMF_Context*)elm_entry_imf_context_get(entry);
+       if (imf_context)
+               ecore_imf_context_input_panel_hide(imf_context);
 
-    char *app_id = NULL;
-    app_control_get_caller(app_data->source_app_control, &app_id);
-    if(app_id != NULL)
-       app_control_set_app_id(app_control, app_id);
-       app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
-    set_source_caller_app_id(app_control);
-    free(app_id);
-       reply_to_sender_by_callback(getText, "keyboard");
-       elm_exit();
-}
+       elm_object_focus_set(entry, EINA_FALSE);
 
-static Eina_Bool custom_back_cb(void *data, Elm_Object_Item *it)
-{
-    _back_to_genlist_for_selector();
-    return EINA_TRUE;
+       exit_keyboard();
 }
 
 void create_fullscreen_editor(void *data)
 {
        App_Data *ad = (App_Data *)data;
 
-    Evas_Object *box = elm_box_add(ad->naviframe);
-    evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
-    evas_object_show(box);
-    elm_win_resize_object_add(ad->naviframe, box);
+       Evas_Object *box = elm_box_add(ad->naviframe);
+       evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+       evas_object_show(box);
+       elm_win_resize_object_add(ad->naviframe, box);
+
+       entry = elm_entry_add(box);
+
+       static Elm_Entry_Filter_Limit_Size limit_filter_data;
+       limit_filter_data.max_char_count = g_input_keyboard_data.max_text_length;
+       elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, &limit_filter_data);
+       evas_object_smart_callback_add(entry, "maxlength,reached", maxlength_cb, data);
 
-    entry = elm_entry_add(box);
        elm_entry_single_line_set(entry, EINA_TRUE);
        elm_entry_scrollable_set(entry, EINA_TRUE);
        elm_scroller_policy_set(entry, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
        elm_object_part_text_set(entry, "elm.guide", g_input_keyboard_data.guide_text);
-    elm_entry_entry_set(entry, g_input_keyboard_data.default_text);
+       elm_entry_entry_set(entry, g_input_keyboard_data.default_text);
        elm_entry_cursor_end_set(entry);
        evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
        evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
+       if (g_input_keyboard_data.return_key_type && strcmp(g_input_keyboard_data.return_key_type, "DONE") == 0) {
+               elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
+               evas_object_smart_callback_add(entry, "activated", enter_keydown_cb, ad);
+       }
+       if (g_input_keyboard_data.cursor_position_set != 0) {
+               elm_entry_cursor_pos_set(entry, g_input_keyboard_data.cursor_position_set);
+       }
        evas_object_show(entry);
        elm_box_pack_end(box, entry);
 
        Evas_Object *btn = elm_button_add(box);
-       elm_object_text_set(btn, "SEND");
+       elm_object_text_set(btn, g_input_keyboard_data.return_key_type);
        evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, 0.5);
        evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
        evas_object_smart_callback_add(btn, "clicked", btn_clicked_cb, NULL);
@@ -263,16 +193,118 @@ void create_fullscreen_editor(void *data)
        evas_object_resize(ad->naviframe, 360, 360);
        evas_object_show(ad->naviframe);
 
-    Elm_Object_Item *nf_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, box, "empty");
-    elm_naviframe_item_pop_cb_set(nf_item, custom_back_cb, NULL);
+       const char *item_style = NULL;
+       if (_WEARABLE)
+               item_style = "empty";
+       Elm_Object_Item *nf_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, box, item_style);
+       elm_naviframe_item_pop_cb_set(nf_item, custom_back_cb, NULL);
+}
+
+static void editfield_focused_cb(void *data, Evas_Object *obj, void *event_info)
+{
+       Evas_Object *editfield = (Evas_Object *)data;
+       elm_object_signal_emit(editfield, "elm,state,focused", "");
+
+       if (!elm_entry_is_empty(obj))
+               elm_object_signal_emit(editfield, "elm,action,show,button", "");
+}
+
+static void editfield_unfocused_cb(void *data, Evas_Object *obj, void *event_info)
+{
+       Evas_Object *editfield = (Evas_Object *)data;
+       elm_object_signal_emit(editfield, "elm,state,unfocused", "");
+       elm_object_signal_emit(editfield, "elm,action,hide,button", "");
+}
+
+static Evas_Object *create_multiline_editfield_layout(Evas_Object *parent, void *data)
+{
+       App_Data *ad = (App_Data *)data;
+       Evas_Object *editfield;
+
+       editfield = elm_layout_add(parent);
+       elm_layout_theme_set(editfield, "layout", "editfield", "multiline");
+       evas_object_size_hint_align_set(editfield, EVAS_HINT_FILL, 0.0);
+       evas_object_size_hint_weight_set(editfield, EVAS_HINT_EXPAND, 0.0);
+
+       entry = elm_entry_add(editfield);
+       static Elm_Entry_Filter_Limit_Size limit_filter_data;
+       limit_filter_data.max_char_count = g_input_keyboard_data.max_text_length;
+       elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, &limit_filter_data);
+       evas_object_smart_callback_add(entry, "maxlength,reached", maxlength_cb, data);
+
+       elm_entry_scrollable_set(entry, EINA_TRUE);
+       elm_scroller_policy_set(entry, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
+       elm_object_part_text_set(entry, "elm.guide", g_input_keyboard_data.guide_text);
+       elm_entry_entry_set(entry, g_input_keyboard_data.default_text);
+       elm_entry_cursor_end_set(entry);
+       evas_object_smart_callback_add(entry, "focused", editfield_focused_cb, editfield);
+       evas_object_smart_callback_add(entry, "unfocused", editfield_unfocused_cb, editfield);
+       if (g_input_keyboard_data.return_key_type && strcmp(g_input_keyboard_data.return_key_type, "DONE") == 0) {
+               elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
+               evas_object_smart_callback_add(entry, "activated", enter_keydown_cb, ad);
+       }
+       if (g_input_keyboard_data.cursor_position_set != 0) {
+               elm_entry_cursor_pos_set(entry, g_input_keyboard_data.cursor_position_set);
+       }
+
+       evas_object_show(entry);
+       elm_object_part_content_set(editfield, "elm.swallow.content", entry);
+
+       return editfield;
+}
+
+static Evas_Object *create_editfield_view(void *data)
+{
+       App_Data *ad = (App_Data *)data;
+
+       Evas_Object *main_scroller, *main_box, *editfield;
+
+       main_scroller = elm_scroller_add(ad->naviframe);
+       elm_scroller_bounce_set(main_scroller, EINA_FALSE, EINA_TRUE);
+       evas_object_size_hint_weight_set(main_scroller, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+       evas_object_size_hint_align_set(main_scroller, EVAS_HINT_FILL, EVAS_HINT_FILL);
+       evas_object_show(main_scroller);
+
+       main_box = elm_box_add(main_scroller);
+       evas_object_size_hint_align_set(main_box, EVAS_HINT_FILL, 0.0);
+       evas_object_size_hint_weight_set(main_box, EVAS_HINT_EXPAND, 0.0);
+       evas_object_show(main_box);
+
+       editfield = create_multiline_editfield_layout(main_box, data);
+       elm_box_pack_end(main_box, editfield);
+       evas_object_show(editfield);
+
+       Evas_Object *btn = elm_button_add(main_box);
+       elm_object_text_set(btn, "OK");
+       evas_object_size_hint_weight_set(btn, 0.5, 0.5);
+       evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
+       evas_object_smart_callback_add(btn, "clicked", btn_clicked_cb, NULL);
+       evas_object_show(btn);
+       elm_box_pack_end(main_box, btn);
+
+       elm_object_content_set(main_scroller, main_box);
+
+       evas_object_show(ad->naviframe);
+       const char *item_style = NULL;
+       if (_WEARABLE)
+               item_style = "empty";
+       Elm_Object_Item *nf_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, main_scroller, item_style);
+       elm_naviframe_item_pop_cb_set(nf_item, custom_back_cb, NULL);
+
+       return main_scroller;
 }
 
-bool input_keyboard_launch_with_ui(Evas_Object *window, void *data) {
+bool input_keyboard_launch(Evas_Object *window, void *data) {
        if (window == NULL) {
                PRINTFUNC(DLOG_ERROR, "Can not get window");
                return false;
        }
-    create_fullscreen_editor(data);
-    return true;
+
+       if (_WEARABLE)
+               create_fullscreen_editor(data);
+       else
+               create_editfield_view(data);
+
+       return true;
 }