remove the legacy code for sms appcontrol
[platform/core/uifw/inputdelegator.git] / src / w-input-selector.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "Debug.h"
18
19 #include <app.h>
20 #include <dlog.h>
21 #include <Eina.h>
22 #include <string>
23 #include <efl_assist.h>
24 #include <vconf.h>
25 #include <vconf-keys.h>
26
27 //#include "ui_extension.h"
28 #include "w-input-selector.h"
29 #include "w-input-template.h"
30 #include "w-input-smartreply.h"
31 #include "w-input-keyboard.h"
32 #include "w-input-stt-ise.h"
33 #include "w-input-emoticon.h"
34 #include "w-input-stt-tos.h"
35
36 #include <stt.h>
37
38
39 using namespace std;
40
41 App_Data* app_data = NULL;
42
43 InputKeyboardData g_input_keyboard_data;
44
45 static Elm_Object_Item *it_empty;
46 static Elm_Object_Item *it_title;
47 static Elm_Object_Item *it_plus;
48 static int g_smartreply_current_number = 0;     /* currently loaded smartreply number */
49 static unsigned int g_template_current_number = 0;      /* currnetly loaded template number */
50
51 Evas_Coord last_step; // 0 ~ 9 for gesture, 10~11 for rotary
52
53 static Eina_Bool force_highlight_to_top = EINA_FALSE;
54 static Eina_Bool force_highlight_to_bottom = EINA_FALSE;
55 static Eina_Bool is_genlist_highlighed = EINA_FALSE;
56
57 void _init_app_data(App_Data* app_data);
58 static void _app_language_changed(app_event_info_h event_info, void *user_data);
59 static int _app_control_request_transient_app_cb(void *data);
60
61 static char *_genlist_text_set_theme_color(const char *str, const char *code);
62
63 Evas_Object* _create_genlist(Evas_Object* parent);
64 void _create_genlist_items(void* user_data, bool is_type_reply);
65 void _update_genlist_items(void *user_data);
66 void _update_smartreply_items(void *user_data);
67 void _update_template_items(void *user_data);
68 static void _popup_close_cb(void *data, Evas_Object *obj, void *event_info);
69 static void _popup_back_cb(void *data, Evas_Object *obj, void *event_info);
70
71
72
73 void _init_app_data(App_Data* app_data)
74 {
75     app_data->win_main = NULL;
76     app_data->layout_main = NULL;
77     app_data->conform = NULL;
78     app_data->naviframe = NULL;
79     app_data->genlist = NULL;
80
81     app_data->res_path = NULL;
82 }
83
84
85
86 Evas_Object* load_edj(Evas_Object* parent, const char* file, const char* group)
87 {
88         Evas_Object* window;
89         Evas_Object* content;
90         int ret;
91
92         window = elm_layout_add(parent);
93         if (window) {
94                 ret = elm_layout_file_set(window, file, group);
95                 if (!ret) {
96                         evas_object_del(window);
97                         return NULL;
98                 }
99
100                 content = elm_layout_add(parent);
101                 elm_layout_theme_set(content, "layout", "application", "default");
102                 elm_object_part_content_set(window, "elm.swallow.content", content);
103
104                 evas_object_size_hint_weight_set(window, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
105         }
106
107         return window;
108 }
109
110 void init_customizing_theme(void)
111 {
112         string stt_edj_path = get_resource_path();
113         string app_edj_path = get_resource_path();
114
115         stt_edj_path = stt_edj_path + STT_EDJ_FILE;
116         app_edj_path = app_edj_path + APP_EDJ_FILE;
117
118         elm_theme_extension_add(NULL, stt_edj_path.c_str());
119         elm_theme_extension_add(NULL, app_edj_path.c_str());
120 }
121
122
123 static Eina_Bool back_cb(void *data, Elm_Object_Item *it)
124 {
125     reply_to_sender_by_callback_for_back();
126     elm_exit();
127     return EINA_FALSE;
128 }
129
130 static void _stt_clicked_cb(void *data, Evas_Object * obj, void *event_info)
131 {
132         App_Data* ad = (App_Data*)data;
133
134         PRINTFUNC(DLOG_DEBUG, "");
135
136         if(!ad)
137                 return;
138
139 #if 0
140         bool bAgreed = false;
141         stt_get_user_agreement(&bAgreed);
142         if(!bAgreed){
143 #endif
144         if(is_tos_N66_agreed() == EINA_FALSE){
145                 if(is_sap_connection() == EINA_TRUE){
146                         ise_show_tos_n66_popup(ad);
147                 } else {
148                         launch_bt_connection_popup(ad);
149                 }
150         } else {
151                 ise_show_stt_popup(ad);
152         }
153 }
154
155 static void _input_smartreply_notify_cb(void *user_data)
156 {
157         ecore_main_loop_iterate();
158         _update_genlist_items((void *)app_data);
159 }
160
161 static void _input_template_notify_cb(void *user_data)
162 {
163         _update_genlist_items((void *)app_data);
164 }
165
166
167 static void _emoticon_clicked_cb(void *data, Evas_Object * obj, void *event_info)
168 {
169         App_Data* ad = (App_Data*)data;
170
171         PRINTFUNC(DLOG_DEBUG, "");
172
173         if(!ad)
174                 return;
175
176 //      ise_show_emoticon_popup(ad);
177         ise_show_emoticon_popup_rotary(ad);
178 }
179
180 static void _keyboard_clicked_cb(void *data, Evas_Object * obj, void *event_info)
181 {
182         App_Data* ad = (App_Data *)data;
183
184         PRINTFUNC(DLOG_DEBUG, "");
185
186         if (!ad)
187                 return;
188
189         input_keyboard_launch(ad->win_main, data);
190 }
191
192 static void __bt_connection_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
193 {
194         char *val = NULL;
195
196         if (reply == NULL) {
197                 PRINTFUNC(DLOG_ERROR, "service_h is NULL");
198                 return;
199         }
200
201         app_control_get_extra_data(reply, "__BT_CONNECTION__", &val);
202         if (val) {
203                 if ( strcmp(val, "Connected") == 0 ) {
204                         PRINTFUNC(DLOG_ERROR, "BT Connected");
205                 } else {
206                         PRINTFUNC(DLOG_ERROR, "BT Not Connected");
207                 }
208                 free(val);
209         }
210 }
211
212
213 static void __ise_smartreply_gl_sel(void *data, Evas_Object *obj, void *event_info)
214 {
215         App_Data* app_data = (App_Data*) data;
216         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
217
218         if (item) {
219                 elm_genlist_item_selected_set(item, EINA_FALSE);
220
221                 int index = (unsigned int) elm_object_item_data_get(item);
222
223                 char *reply = input_smartreply_get_nth_item(index);
224                 if (reply) {
225                         input_smartreply_send_feedback(reply);
226                         reply_to_sender_by_callback(reply, "smartreply");
227                         free(reply);
228                         elm_exit();
229                 }
230         }
231 }
232
233 static void __ise_template_add_gl_sel(void *data, Evas_Object *obj, void *event_info)
234 {
235         App_Data* app_data = (App_Data*) data;
236         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
237
238         if (item) {
239                 elm_genlist_item_selected_set(item, EINA_FALSE);
240
241                 if(is_sap_connection() == EINA_TRUE){
242                         // Show toast popup
243                         show_popup_toast(gettext("WDS_IME_TPOP_ADD_TEMPLATE_ON_YOUR_PHONE_ABB"), true);
244
245                         // send message to Host
246                         app_control_h appctrl;
247                         app_control_create(&appctrl);
248                         app_control_set_app_id(appctrl, "com.samsung.w-manager-service");
249                         app_control_add_extra_data(appctrl, "type", "gm_setting");
250                         app_control_add_extra_data(appctrl, "data/setting_menu", "texttemplate_setting");
251                         app_control_send_launch_request(appctrl, NULL, NULL);
252                         app_control_destroy(appctrl);
253                 } else {
254                         app_control_h app_control;
255                         app_control_create(&app_control);
256                         app_control_set_app_id(app_control, "com.samsung.bt-connection-popup");
257                         app_control_add_extra_data(app_control, "msg", "perform");
258                         app_control_send_launch_request(app_control, __bt_connection_result_cb, NULL);
259                         app_control_destroy(app_control);
260                 }
261         }
262 }
263
264 static void __ise_template_gl_sel(void *data, Evas_Object *obj, void *event_info)
265 {
266         App_Data* app_data = (App_Data*) data;
267         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
268         int index = 0;
269
270         if (item) {
271                 elm_genlist_item_selected_set(item, EINA_FALSE);
272
273                 index = (unsigned int) elm_object_item_data_get(item);
274                 const std::vector<TemplateData>  template_list = input_template_get_list();
275
276                 //@ToDo : should call reply function when any template string is selected and confirmed.
277                 // Here reply string will be template_list[index].text.c_str();
278                 if( index < template_list.size()) {
279                         reply_to_sender_by_callback(gettext(template_list[index].text.c_str()), "template");
280                         elm_exit();
281                 }
282         }
283 }
284
285 static Evas_Object * __ise_gl_3button_content_get(void *data, Evas_Object *obj, const char *part)
286 {
287         if (!strcmp(part, "elm.icon.1") ||  (!strcmp(part, "elm.icon.2")) ||  (!strcmp(part, "elm.icon.3"))) {
288                 Evas_Object* btn = elm_button_add(obj);
289                 evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
290                 evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
291                 Evas_Object* ic = elm_image_add(btn);
292                 elm_image_resizable_set(ic, EINA_TRUE, EINA_TRUE);
293                 string path = get_resource_path();
294                 if (!strcmp(part, "elm.icon.1")) {
295                         elm_object_style_set(btn, "anchor");
296                         string path_ic = path + "images/w_mode_stt_ic.png";
297                         elm_image_file_set(ic, path_ic.c_str(), NULL);
298                         elm_object_content_set(btn, ic);
299                         evas_object_layer_set(btn, 32000);
300
301                 } else if (!strcmp(part, "elm.icon.2")){
302                         elm_object_style_set(btn, "anchor");
303                         string path_ic = path + "images/Delta_w_mode_emoticon_ic.png";
304                         elm_image_file_set(ic, path_ic.c_str(), NULL);
305                         elm_object_content_set(btn, ic);
306                         evas_object_layer_set(btn, 32000);
307
308                 } else if (!strcmp(part, "elm.icon.3")){
309                         elm_object_style_set(btn, "anchor");
310                         string path_ic = path + "images/w_mode_keyboard_ic.png";
311                         elm_image_file_set(ic, path_ic.c_str(), NULL);
312                         elm_object_content_set(btn, ic);
313                         evas_object_layer_set(btn, 32000);
314                         evas_object_propagate_events_set(btn, EINA_FALSE);
315                 }
316
317                 return btn;
318         } else if (!strcmp(part, "elm.icon.1.touch_area") ||  (!strcmp(part, "elm.icon.2.touch_area")) ||  (!strcmp(part, "elm.icon.3.touch_area"))) {
319                 Evas_Object* btn = elm_button_add(obj);
320                 elm_object_style_set(btn, "touch_area");
321                 evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
322                 evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
323                 string path = get_resource_path();
324                 if (!strcmp(part, "elm.icon.1.touch_area")) {
325                         evas_object_layer_set(btn, 32000);
326                         evas_object_smart_callback_add(btn, "clicked", _stt_clicked_cb, app_data);
327
328                 } else if (!strcmp(part, "elm.icon.2.touch_area")){
329                         evas_object_layer_set(btn, 32000);
330                         evas_object_smart_callback_add(btn, "clicked", _emoticon_clicked_cb, app_data);
331                 } else if (!strcmp(part, "elm.icon.3.touch_area")) {
332                         evas_object_layer_set(btn, 32000);
333                         evas_object_propagate_events_set(btn, EINA_FALSE);
334                         evas_object_smart_callback_add(btn, "clicked", _keyboard_clicked_cb, app_data);
335                 }
336
337                 return btn;
338         } else if (!strcmp(part, "base")) {
339                 Evas_Object* btn = elm_button_add(obj);
340                 elm_object_style_set(btn, "ime_transparent");
341                 return btn;
342         }
343         return NULL;
344 }
345
346 static char * __ise_smartreply_gl_text_get(void *data, Evas_Object *obj, const char *part)
347 {
348         if(!strcmp(part, "elm.text")) {
349                 int index;
350                 char *reply = NULL;
351
352                 index = (int)data;
353                 if (index < 0)
354                         return NULL;
355
356                 reply = input_smartreply_get_nth_item(index);
357                 if (reply == NULL)
358                         return NULL;
359
360                 /* Set hightlight color on first 3 smartreply items */
361                 if (index < 3) {
362                         char *colored = _genlist_text_set_theme_color(reply, "AT0113");
363
364                         if (colored == NULL) {
365                                 return reply;
366                         } else {
367                                 free(reply);
368                                 return colored;
369                         }
370                 }
371
372                 return reply;
373         }
374         return NULL;
375 }
376
377 static char * __ise_template_gl_text_get(void *data, Evas_Object *obj, const char *part)
378 {
379         if(!strcmp(part, "elm.text")) {
380                 unsigned int index = (unsigned int)data;
381                 const std::vector<TemplateData>  template_list = input_template_get_list();
382
383                 if(index < template_list.size()) {
384                         if(template_list[index].use_gettext) {
385                                 return strdup(gettext(template_list[index].text.c_str()));
386                         } else {
387                                 Eina_Strbuf *buf = NULL;
388                                 const char *str = NULL;
389                                 char *markup = NULL;
390
391                                 buf = eina_strbuf_new();
392                                 if(buf) {
393                                         eina_strbuf_append(buf, template_list[index].text.c_str());
394                                         eina_strbuf_replace_all(buf, "\n", "");
395                                         eina_strbuf_replace_all(buf, "\r", "");
396
397                                         str = eina_strbuf_string_get(buf);
398
399                                         if (str) {
400                                                 markup = elm_entry_utf8_to_markup(str);
401                                         }
402                                         eina_strbuf_free(buf);
403                                 }
404
405                                 return markup;
406                         }
407                 }
408         }
409         return NULL;
410 }
411
412 static char * __ise_addtemplate_gl_text_get(void *data, Evas_Object *obj, const char *part)
413 {
414         if(!strcmp(part, "elm.text")) {
415                 return(strdup(gettext("WDS_IME_MBODY_ADD_TEMPLATE_ABB")));
416         }
417         return NULL;
418 }
419
420 static Evas_Object * __ise_gl_1button_content_get(void *data, Evas_Object *obj, const char *part)
421 {
422         if (!strcmp(part, "elm.icon")) {
423                 Evas_Object* btn = elm_button_add(obj);
424                 evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
425                 evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
426                 Evas_Object* ic = elm_image_add(btn);
427                 elm_image_resizable_set(ic, EINA_TRUE, EINA_TRUE);
428                 string path = get_resource_path();
429                 elm_object_style_set(btn, "ime_button_template");
430                 string path_ic = path + "/images/w_list_add_ic.png";
431                 elm_image_file_set(ic, path_ic.c_str(), NULL);
432                 elm_object_content_set(btn, ic);
433                 evas_object_layer_set(btn, 32000);
434
435                 return btn;
436         }
437         return NULL;
438 }
439
440 static void __ise_gl_lang_changed(void *data, Evas_Object *obj, void *event_info)
441 {
442         //Update genlist items. The Item texts will be translated in the _gl_text_get().
443         elm_genlist_realized_items_update(obj);
444 }
445
446 static int _app_control_request_transient_app_cb(void *data)
447 {
448         return 0;
449 }
450
451
452 static char *_genlist_text_set_theme_color(const char *str, const char *code)
453 {
454         int r, g, b, a;
455         Eina_Strbuf *buf = NULL;
456         char *colored = NULL;
457
458         if (code == NULL)
459                 return NULL;
460
461         buf = eina_strbuf_new();
462         if (buf == NULL) {
463                 PRINTFUNC(DLOG_ERROR, "Can not get create strbuf");
464                 return NULL;
465         }
466
467 //      ea_theme_color_get(code,
468 //                      &r, &g, &b, &a,
469 //                      NULL, NULL, NULL, NULL,
470 //                      NULL, NULL, NULL, NULL);
471
472         eina_strbuf_append_printf(buf, "<color=#%02x%02x%02x%02x>%s</color>",
473                         r, g, b, a, str);
474
475         colored = eina_strbuf_string_steal(buf);
476
477         eina_strbuf_free(buf);
478
479         return colored;
480 }
481
482
483 static void _app_control_send_reply_cb(app_control_h request, app_control_h reply,
484                                                 app_control_result_e result, void *user_data)
485 {
486                 PRINTFUNC(DLOG_DEBUG, "");
487                 reply_to_sender_by_callback(NULL, NULL);
488                 elm_exit();
489 }
490
491 void set_source_caller_app_id(app_control_h app_control)
492 {
493         if(!app_control){
494                 PRINTFUNC(DLOG_ERROR, "can't get app_control");
495                 return;
496         }
497
498         char *caller = NULL;
499         app_control_get_caller(app_data->source_app_control, &caller);
500
501         if(caller){
502                 PRINTFUNC(DLOG_DEBUG, "caller = %s", caller);
503                 app_control_add_extra_data(app_control, "caller_appid", caller);
504                 free(caller);
505         }
506 }
507
508 void reply_to_sender_by_callback(const char *value, const char *type)
509 {
510         PRINTFUNC(DLOG_DEBUG, "");
511
512         app_control_h app_control;
513
514         if(app_control_create(&app_control) == 0) {
515                 int ret;
516
517                 if (value)
518                         app_control_add_extra_data(app_control, "selected_context", value);
519
520                 if (type)
521                         app_control_add_extra_data(app_control, "reply_type", type);
522
523                 set_source_caller_app_id(app_control);
524
525                 ret = app_control_reply_to_launch_request(app_control, app_data->source_app_control, APP_CONTROL_RESULT_SUCCEEDED);
526                 if (ret != APP_CONTROL_ERROR_NONE)
527                         PRINTFUNC(DLOG_ERROR, "reply failed : %d", ret);
528
529                 app_control_destroy(app_control);
530                 app_control_destroy(app_data->source_app_control);
531                 app_data->source_app_control = NULL;
532         }
533 }
534
535 void reply_to_sender_by_callback_for_back()
536 {
537         PRINTFUNC(DLOG_DEBUG, "");
538
539         app_control_h app_control;
540
541         if(app_control_create(&app_control) == 0) {
542                 int ret;
543
544                 app_control_add_extra_data(app_control, "back_to_composer", "yes");
545
546                 ret = app_control_reply_to_launch_request(app_control, app_data->source_app_control, APP_CONTROL_RESULT_SUCCEEDED);
547                 if (ret != APP_CONTROL_ERROR_NONE)
548                         PRINTFUNC(DLOG_ERROR, "reply failed : %d", ret);
549
550                 app_control_destroy(app_control);
551                 app_control_destroy(app_data->source_app_control);
552                 app_data->source_app_control = NULL;
553         }
554 }
555
556 char* get_resource_path()
557 {
558         if(NULL == app_data->res_path) {
559                 app_data->res_path = app_get_resource_path();
560                 PRINTFUNC(DLOG_INFO, "set resource path = %s", app_data->res_path);
561         }
562         return (app_data->res_path);
563 }
564
565 char* get_shared_resource_path()
566 {
567         if(NULL == app_data->shared_res_path) {
568                 app_data->shared_res_path = app_get_shared_resource_path();
569                 PRINTFUNC(DLOG_INFO, "set shared resource path = %s", app_data->shared_res_path);
570         }
571         return (app_data->shared_res_path);
572 }
573
574 void show_gl_focus(Eina_Bool bVisible){
575         if(app_data->genlist == NULL)
576                 return;
577
578         if(bVisible == EINA_TRUE){
579                 elm_object_signal_emit(app_data->genlist, "elm,state,focus_bg,enable", "elm");
580                 //elm_layout_theme_set(app_data->genlist, "genlist", "base", "focus_bg");
581                 //elm_object_signal_emit(app_data->genlist, "elm,state,focus_bg,show", "elm");
582         } else {
583                 elm_object_signal_emit(app_data->genlist, "elm,state,focus_bg,disable", "elm");
584                 //elm_layout_theme_set(app_data->genlist, "genlist", "base", "default");
585                 //elm_object_signal_emit(app_data->genlist, "elm,state,focus_bg,hide", "elm");
586         }
587 }
588
589 void show_popup_toast(const char *text, bool check_img)
590 {
591         PRINTFUNC(DLOG_ERROR, "show_popup_toast");
592
593         Evas_Object *popup;
594
595         popup = elm_popup_add(app_data->win_main);
596         elm_object_style_set(popup, "toast/circle");
597         elm_popup_orient_set(popup, ELM_POPUP_ORIENT_BOTTOM);
598         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
599
600         if (check_img) {
601                 string path = get_resource_path();
602                 string path_ic = path + "/images/toast_check_icon.png";
603                 Evas_Object * img = elm_image_add(popup);
604                 elm_image_file_set(img, path_ic.c_str(), NULL);
605                 elm_object_part_content_set(popup, "toast,icon", img);
606         }
607         if(text) {
608                 elm_object_part_text_set(popup, "elm.text", text);
609         }
610
611         eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, _popup_back_cb, NULL);
612         evas_object_smart_callback_add(popup, "dismissed", _popup_close_cb, NULL);
613         evas_object_smart_callback_add(popup, "block,clicked", _popup_back_cb, NULL);
614
615         elm_popup_timeout_set(popup, 2.0);
616         evas_object_show(popup);
617 }
618
619 static void _popup_close_cb(void *data, Evas_Object *obj, void *event_info)
620 {
621         if(obj){
622                 evas_object_hide(obj);
623                 evas_object_del(obj);
624         }
625 }
626
627 static void _popup_back_cb(void *data, Evas_Object *obj, void *event_info)
628 {
629         if(obj)
630                 elm_popup_dismiss(obj);
631 }
632
633
634 void _back_to_genlist_for_selector()
635 {
636         PRINTFUNC(DLOG_DEBUG, "");
637
638         if(!app_data) return;
639
640         if(app_data->app_type == APP_TYPE_STT || app_data->app_type == APP_TYPE_EMOTICON){
641                 PRINTFUNC(DLOG_DEBUG, "launched as STT/EMOTICON mode, So exit here.");
642                 reply_to_sender_by_callback(NULL, NULL);
643                 elm_exit();
644         }
645
646         Evas_Object *circle_genlist = (Evas_Object *) evas_object_data_get(app_data->genlist, "circle");
647
648         eext_rotary_object_event_activated_set(circle_genlist, EINA_TRUE);
649 }
650
651 static void _item_realized(void *data, Evas_Object *obj, void *event_info) //called when list scrolled
652 {
653         PRINTFUNC(DLOG_DEBUG, "%s", __func__);
654 }
655
656 Evas_Object* _create_genlist(Evas_Object* navi)
657 {
658         Evas_Object* genlist = elm_genlist_add(navi);
659         if(NULL == genlist)
660                 return NULL;
661
662         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
663         elm_genlist_scroller_policy_set(genlist, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_OFF);
664
665         Evas_Object* circle_object_genlist = eext_circle_object_genlist_add(genlist, app_data->circle_surface);
666         eext_circle_object_genlist_scroller_policy_set(circle_object_genlist, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_OFF);
667         evas_object_data_set(genlist, "circle", (void *) circle_object_genlist);
668         eext_rotary_object_event_activated_set(circle_object_genlist, EINA_TRUE);
669
670         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
671         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
672
673         evas_object_smart_callback_add(genlist, "language,changed", __ise_gl_lang_changed, genlist);
674         evas_object_show(genlist);
675
676 //      uxt_genlist_set_bottom_margin_enabled(genlist, EINA_TRUE);
677
678         show_gl_focus(EINA_FALSE);
679
680         Elm_Object_Item *nf_main_item = elm_naviframe_item_push(navi,
681             NULL,
682             NULL,
683             NULL,
684             genlist,
685             "empty");
686
687         elm_naviframe_item_pop_cb_set(nf_main_item, back_cb, app_data);
688         evas_object_smart_callback_add(genlist, "realized", _item_realized, NULL);
689
690         return genlist;
691 }
692
693 static void _item_position_changed_cb(void *data, Evas_Object *obj, void *event_info)
694 {
695         if(!obj) return;
696         if(!data) return;
697
698         Evas_Object *genlist = (Evas_Object *)obj;
699         Elm_Object_Item *gen_item = (Elm_Object_Item *)data;
700
701         Evas_Coord y;
702         elm_scroller_region_get(genlist, NULL, &y, NULL, NULL);
703
704 //      PRINTFUNC(DLOG_DEBUG,"y=%d",y);
705
706         if (250 > y && y >= 100){
707                 if (last_step == 0) return;
708                 last_step = 0;
709                 elm_object_item_signal_emit(gen_item, "elm,action,ime,0.0", "elm");
710                 show_gl_focus(EINA_TRUE);
711         } else if (100 > y && y >= 50){
712                 if (last_step == 1) return;
713                 last_step = 1;
714                 elm_object_item_signal_emit(gen_item, "elm,action,ime,0.1", "elm");
715                 show_gl_focus(EINA_TRUE);
716         } else if (y < 50 && y >=0){
717                 if (last_step == 2) return;
718                 last_step = 2;
719                 elm_object_item_signal_emit(gen_item, "elm,action,ime,0.9", "elm");
720                 show_gl_focus(EINA_FALSE);
721         }
722 }
723
724 static char *
725 _main_menu_title_text_get(void *data, Evas_Object *obj, const char *part)
726 {
727         char buf[1024];
728
729         snprintf(buf, 1023, "%s", "Select method");
730         return strdup(buf);
731 }
732
733 void _create_genlist_items(void* user_data, bool is_type_reply)
734 {
735         LOGD("[psw] _create_genlist_items");
736
737         App_Data* app_data = (App_Data*) user_data;
738
739         if(NULL == app_data->genlist) {
740                 app_data->genlist = _create_genlist(app_data->naviframe);
741                 if(NULL == app_data->genlist)
742                 return;
743         }
744
745         elm_genlist_clear(app_data->genlist);
746         g_smartreply_current_number = 0;        /* reset previous smartreply size to 0 */
747
748         Elm_Genlist_Item_Class * itc0 = elm_genlist_item_class_new();
749         itc0->item_style = "title";
750         itc0->func.text_get = _main_menu_title_text_get;
751         itc0->func.content_get = NULL;
752         itc0->func.state_get = NULL;
753         itc0->func.del = NULL;
754
755         Elm_Genlist_Item_Class * itc1 = elm_genlist_item_class_new();
756         itc1->item_style = "3button";
757         itc1->func.text_get = NULL;
758         itc1->func.content_get = __ise_gl_3button_content_get;
759         itc1->func.state_get = NULL;
760         itc1->func.del = NULL;
761
762
763         // dummy title for empty space
764         it_empty = elm_genlist_item_append(app_data->genlist, itc0,
765                         NULL, NULL,
766                         ELM_GENLIST_ITEM_NONE,
767                         NULL, NULL);
768
769         if (!is_type_reply) {
770                 // 3 Buttons
771                 it_title = elm_genlist_item_append(app_data->genlist, itc1,
772                                 NULL, NULL,
773                                 ELM_GENLIST_ITEM_NONE,
774                                 NULL, NULL);
775
776                 elm_genlist_item_select_mode_set(it_title, ELM_OBJECT_SELECT_MODE_NONE);
777         }
778         if (input_smartreply_is_enabled() == false) {
779                 _update_template_items(app_data);
780                 if(g_template_current_number > 0) {
781                         Evas_Object *circle_genlist = (Evas_Object *) evas_object_data_get(app_data->genlist, "circle");
782                         eext_circle_object_genlist_scroller_policy_set(circle_genlist,
783                                         ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
784                 }
785         } else {
786                 // dummy title for bottom
787                 Elm_Object_Item *dummy;
788
789                 dummy = elm_genlist_item_append(app_data->genlist, itc0,
790                                 NULL, NULL,
791                                 ELM_GENLIST_ITEM_NONE,
792                                 NULL, NULL);
793
794                 elm_genlist_item_select_mode_set(dummy, ELM_OBJECT_SELECT_MODE_NONE);
795         }
796
797
798         Elm_Object_Item *item = elm_genlist_item_next_get(it_title);
799         elm_genlist_item_show(item, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
800
801         evas_object_smart_callback_add(app_data->genlist, "elm,item,position,changed", _item_position_changed_cb, it_title);
802
803         elm_genlist_item_class_free(itc0);
804         elm_genlist_item_class_free(itc1);
805 }
806
807 void _update_genlist_items(void *user_data)
808 {
809         int len = 0;
810
811         if (input_smartreply_is_enabled())
812                 len = input_smartreply_get_reply_num();
813
814         if (g_smartreply_current_number != len)
815                 _update_smartreply_items(user_data);
816
817         _update_template_items(user_data);
818
819         g_smartreply_current_number = len;
820
821         if (g_smartreply_current_number > 0 || g_template_current_number > 0) {
822                 Evas_Object *circle_genlist = (Evas_Object *) evas_object_data_get(app_data->genlist, "circle");
823                 eext_circle_object_genlist_scroller_policy_set(circle_genlist,
824                                                         ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
825         }
826 }
827
828 void _update_smartreply_items(void *user_data)
829 {
830         App_Data* app_data;
831
832         Elm_Object_Item *first;
833         Elm_Object_Item *menu;
834         Elm_Object_Item *pos;
835
836         int len = 0;
837         int i = 0;
838
839         app_data = (App_Data *)user_data;
840
841         if (app_data == NULL) {
842                 PRINTFUNC(DLOG_ERROR, "Can not get app_data");
843                 return;
844         }
845
846         if (app_data->genlist == NULL) {
847                 /* smartreply will update when genlist is exist only */
848                 PRINTFUNC(DLOG_ERROR, "Can not get getlist");
849                 return;
850         }
851
852         if (input_smartreply_is_enabled())
853                 len = input_smartreply_get_reply_num();
854
855         if (g_smartreply_current_number == len)
856                 return;
857
858         first = elm_genlist_first_item_get(app_data->genlist);
859         menu = elm_genlist_item_next_get(first);
860
861         /* Remove current smartreply list */
862         pos = elm_genlist_item_next_get(menu);
863         for (i = 0; i < g_smartreply_current_number; i++) {
864                 Elm_Object_Item *next;
865
866                 if (pos == NULL)
867                         break;
868
869                 next = elm_genlist_item_next_get(pos);
870                 elm_object_item_del(pos);
871                 pos = next;
872         }
873
874         /* Append newly added smartreply list */
875         if (len > 0) {
876                 Elm_Genlist_Item_Class *itc;
877
878                 itc = elm_genlist_item_class_new();
879
880                 itc->item_style = "1text";
881                 itc->func.text_get = __ise_smartreply_gl_text_get;
882                 itc->func.content_get = NULL;
883                 itc->func.state_get = NULL;
884                 itc->func.del = NULL;
885
886                 pos = menu;
887                 for (i = 0; i < len; i++) {
888                         pos = elm_genlist_item_insert_after(app_data->genlist,
889                                                         itc,
890                                                         (void *)i,
891                                                         NULL,
892                                                         pos,
893                                                         ELM_GENLIST_ITEM_NONE,
894                                                         __ise_smartreply_gl_sel,
895                                                         app_data);
896                 }
897
898                 if (menu)
899                         elm_genlist_item_show(menu, ELM_GENLIST_ITEM_SCROLLTO_TOP);
900
901                 elm_genlist_item_class_free(itc);
902         }
903
904         g_smartreply_current_number = len;
905 }
906
907
908 void _update_template_items(void *user_data)
909 {
910         App_Data* app_data;
911
912         Elm_Object_Item *first;
913         Elm_Object_Item *menu;
914         Elm_Object_Item *pos;
915         Elm_Object_Item *template_pos;
916
917         unsigned int template_len = 0;
918         unsigned int i = 0;
919
920         app_data = (App_Data *)user_data;
921
922         if (app_data == NULL) {
923                 PRINTFUNC(DLOG_ERROR, "Can not get app_data");
924                 return;
925         }
926
927         if (app_data->genlist == NULL) {
928                 /* smartreply will update when genlist is exist only */
929                 PRINTFUNC(DLOG_ERROR, "Can not get getlist");
930                 return;
931         }
932
933         if (app_data->app_type == APP_TYPE_REPLY) {
934                 menu = elm_genlist_first_item_get(app_data->genlist);
935         } else {
936                 first = elm_genlist_first_item_get(app_data->genlist);
937                 menu = elm_genlist_item_next_get(first);
938         }
939
940         pos = menu;
941         /* move to smartreply next if it need */
942         if (input_smartreply_is_enabled()) {
943                 int smartreply_len = 0;
944
945                 smartreply_len = input_smartreply_get_reply_num();
946                 if (smartreply_len > 0) {
947                         int j = 0;
948
949                         for (j = 0; j < smartreply_len; j++) {
950                                 if (pos == NULL)
951                                         break;
952
953                                 pos = elm_genlist_item_next_get(pos);
954                         }
955                 }
956         }
957
958         /* Remove Current template list */
959         template_pos = elm_genlist_item_next_get(pos);
960         for (i = 0 ; i < g_template_current_number; i++) {
961                 Elm_Object_Item *next;
962
963                 if (template_pos == NULL) {
964                         PRINTFUNC(DLOG_ERROR, "Template mismatched, iter : %d, current num: %d",
965                                         i, g_template_current_number);
966                         break;
967                 }
968
969                 next = elm_genlist_item_next_get(template_pos);
970                 elm_object_item_del(template_pos);
971                 template_pos = next;
972         }
973
974         /* Remove Add or dummpy button */
975         if(template_pos) {
976                 Elm_Object_Item *next;
977
978                 next = elm_genlist_item_next_get(template_pos);
979                 elm_object_item_del(template_pos);
980                 template_pos = next;
981         }
982
983         /* Append New Template list */
984         const std::vector<TemplateData> template_list = input_template_get_list();
985         template_len = 0;
986
987         if (template_list.size() > 0) {
988                 Elm_Genlist_Item_Class *itc;
989
990                 itc = elm_genlist_item_class_new();
991
992                 itc->item_style = "1text";
993                 itc->func.text_get = __ise_template_gl_text_get;
994                 itc->func.content_get = NULL;
995                 itc->func.state_get = NULL;
996                 itc->func.del = NULL;
997
998                 for (i = 0; i < template_list.size(); i++) {
999                         bool matched = false;
1000
1001                         if(!template_list[i].use_gettext) {
1002                                 int smartreply_len = 0;
1003                                 int j = 0;
1004
1005                                 smartreply_len = input_smartreply_get_reply_num();
1006                                 for (j = 0; j < smartreply_len; j++) {
1007                                         char *reply = NULL;
1008
1009                                         reply = input_smartreply_get_nth_item(j);
1010                                         if (reply) {
1011                                                 if (!strcmp(reply, template_list[i].text.c_str())) {
1012                                                         matched = true;
1013                                                         free(reply);
1014                                                         break;
1015                                                 }
1016                                                 free(reply);
1017                                         }
1018                                 }
1019                         }
1020
1021                         if (matched == false) {
1022                                 pos = elm_genlist_item_insert_after(app_data->genlist,
1023                                                 itc,
1024                                                 (void *)i,
1025                                                 NULL,
1026                                                 pos,
1027                                                 ELM_GENLIST_ITEM_NONE,
1028                                                 __ise_template_gl_sel,
1029                                                 app_data);
1030                                 template_len++;
1031                         }
1032                 }
1033
1034                 elm_genlist_item_class_free(itc);
1035         }
1036
1037
1038         // template add button (+)
1039 /*      if (!input_template_is_user_template()) {
1040                 Elm_Genlist_Item_Class *itc;
1041
1042                 itc = elm_genlist_item_class_new();
1043
1044                 itc->item_style = "1text.1icon";
1045                 itc->func.text_get = __ise_addtemplate_gl_text_get;;
1046                 itc->func.content_get = __ise_gl_1button_content_get;
1047                 itc->func.state_get = NULL;
1048                 itc->func.del = NULL;
1049
1050
1051                 pos = elm_genlist_item_insert_after(app_data->genlist,
1052                                 itc,
1053                                 NULL,
1054                                 NULL,
1055                                 pos,
1056                                 ELM_GENLIST_ITEM_NONE,
1057                                 __ise_template_add_gl_sel,
1058                                 app_data);
1059
1060                 elm_genlist_item_class_free(itc);
1061         }
1062 */
1063         if (menu)
1064                 elm_genlist_item_show(menu, ELM_GENLIST_ITEM_SCROLLTO_TOP);
1065
1066         g_template_current_number = template_len;
1067 }
1068
1069
1070 bool _app_create(void* user_data)
1071 {
1072         int width = 1000, height = 1000;
1073 //      App_Data* app_data = NULL;
1074         Evas_Object* layout = NULL;
1075         Evas_Object* conform = NULL;
1076         Evas_Object* bg = NULL;
1077         Evas_Object* window = NULL;
1078         Eext_Circle_Surface *surface;
1079
1080         if (!user_data) {
1081                 return false;
1082         }
1083
1084         _app_language_changed(NULL, NULL);
1085
1086         app_data = (App_Data*)user_data;
1087
1088         elm_app_base_scale_set(1.3);
1089
1090         window = elm_win_add(NULL, PACKAGE, ELM_WIN_BASIC);
1091         if (window) {
1092                 init_customizing_theme();
1093
1094                 elm_win_title_set(window, PACKAGE);
1095                 elm_win_borderless_set(window, EINA_TRUE);
1096 //              ecore_x_window_size_get(ecore_x_window_root_first_get(), &width, &height);
1097                 evas_object_resize(window, width, height);
1098                 elm_win_indicator_mode_set(window, ELM_WIN_INDICATOR_SHOW);
1099         } else {
1100                 LOGE("elm_win_add() is failed.");
1101                 return false;
1102         }
1103
1104         bg = elm_bg_add(window);
1105         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1106         elm_win_resize_object_add(window, bg);
1107         evas_object_show(bg);
1108
1109         layout = elm_layout_add(window);
1110         elm_layout_theme_set(layout, "layout", "application", "default");
1111         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1112
1113         conform = elm_conformant_add(window);
1114         surface = eext_circle_surface_conformant_add(conform);
1115         evas_object_size_hint_weight_set(conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1116
1117
1118         elm_win_resize_object_add(window, conform);
1119         elm_object_content_set(conform, layout);
1120
1121         evas_object_show(layout);
1122         evas_object_show(conform);
1123         evas_object_show(window);
1124         app_data->win_main = window;
1125         app_data->conform = conform;
1126         app_data->layout_main = layout;
1127         app_data->circle_surface = surface;
1128         app_data->app_type = APP_TYPE_SELECT_MODE;
1129
1130         Evas_Object *naviframe = elm_naviframe_add(layout);
1131         elm_naviframe_prev_btn_auto_pushed_set(naviframe, EINA_FALSE);
1132         eext_object_event_callback_add(naviframe, EEXT_CALLBACK_BACK, eext_naviframe_back_cb, NULL);
1133         elm_object_part_content_set(layout, "elm.swallow.content", naviframe);
1134         evas_object_show(naviframe);
1135
1136         app_data->naviframe = naviframe;
1137
1138         return true;
1139 }
1140
1141 void _app_service(app_control_h service, void* user_data)
1142 {
1143         int ret;
1144         bool b_ret;
1145         char *context = NULL;
1146
1147         app_control_clone(&(app_data->source_app_control), service);
1148         app_data->reply_type = REPLY_APP_NORMAL;
1149
1150     bool is_type_reply = false;
1151         ret = app_control_get_extra_data(service, APP_CONTROL_DATA_INPUT_TYPE, &context);
1152         if (ret == APP_CONTROL_ERROR_NONE) {
1153                 if (!strcmp(context, "input_voice")) {
1154                         app_data->app_type = APP_TYPE_STT;
1155                         _stt_clicked_cb((void *)app_data, NULL, NULL);
1156                         goto ACTIVATE;
1157                 } else if (!strcmp(context, "input_emoticon")) {
1158                         app_data->app_type = APP_TYPE_EMOTICON;
1159                         _emoticon_clicked_cb((void *)app_data, NULL, NULL);
1160                         goto ACTIVATE;
1161         } else if (!strcmp(context, "input_keyboard")) {
1162                         app_data->app_type = APP_TYPE_KEYBOARD;
1163                         input_keyboard_init(service);
1164                         _keyboard_clicked_cb((void *)app_data, NULL, NULL);
1165                         goto ACTIVATE;
1166                 } else if (!strcmp(context, "input_reply")) {
1167                         app_data->app_type = APP_TYPE_REPLY;
1168             is_type_reply = true;
1169                 }
1170         }
1171
1172         if (app_data->genlist == NULL)
1173                 app_data->genlist = _create_genlist(app_data->naviframe);
1174
1175         input_keyboard_init(service);
1176
1177 //      input_smartreply_init(service);
1178 //      input_smartreply_set_notify(_input_smartreply_notify_cb, NULL);
1179
1180 //      if (input_smartreply_is_enabled())
1181 //              input_smartreply_get_reply_async();
1182
1183
1184         input_template_init(service);
1185         input_template_set_notify(_input_template_notify_cb,  NULL);
1186
1187         _create_genlist_items(app_data, is_type_reply);
1188
1189 ACTIVATE :
1190         elm_win_activate(app_data->win_main);
1191
1192 //      app_control_request_transient_app(service, elm_win_xwindow_get(app_data->win_main), _app_control_request_transient_app_cb,  NULL);
1193         if(context)
1194                 free(context);
1195 }
1196
1197
1198 void _app_pause(void* user_data)
1199 {
1200         PRINTFUNC(DLOG_DEBUG, "");
1201         pause_voice();
1202 }
1203
1204 void _app_resume(void* user_data)
1205 {
1206         PRINTFUNC(DLOG_DEBUG, "");
1207 }
1208
1209 void _app_terminate(void* user_data)
1210 {
1211         App_Data* app_data = NULL;
1212         app_data = (App_Data*)user_data;
1213
1214         if(app_data->genlist){
1215                 evas_object_smart_callback_del(app_data->genlist, "elm,item,position,changed", _item_position_changed_cb);
1216         }
1217
1218         if(app_data->res_path)
1219                 free(app_data->res_path);
1220
1221         if(app_data->shared_res_path)
1222                 free(app_data->shared_res_path);
1223
1224         input_keyboard_deinit();
1225         input_smartreply_deinit();
1226
1227         input_template_unset_notify();
1228         input_template_deinit();
1229 }
1230
1231 static int init_i18n(const char *domain, const char *dir, char *lang_str)
1232 {
1233         if (setlocale(LC_ALL, "") == NULL) {
1234                 PRINTFUNC(DLOG_INFO, "Some environment variable is invalid, setlocale(LC_ALL, \"\") has returns\ed a null pointer");
1235                 if (setlocale(LC_ALL, lang_str) == NULL)
1236                         return -1;
1237         }
1238         if (bindtextdomain(domain, dir) == NULL)
1239                 return -1;
1240         if (textdomain(domain) == NULL)
1241                 return -1;
1242
1243         return 0;
1244 }
1245
1246 /**
1247  * @brief Set language and locale.
1248  *
1249  * @return void
1250  */
1251 static void _app_language_changed(app_event_info_h event_info, void *user_data)
1252 {
1253         char* lang_str = vconf_get_str(VCONFKEY_LANGSET);
1254         if (lang_str) {
1255                 setenv("LANG", lang_str, 1);
1256                 setenv("LC_MESSAGES", lang_str, 1);
1257         } else {
1258                 setenv("LANG", "en_GB.utf8", 1);
1259                 setenv("LC_MESSAGES", "en_GB.utf8", 1);
1260                 lang_str = strdup("en_GB.UTF-8");
1261         }
1262
1263         init_i18n(PACKAGE, LOCALEDIR, lang_str);
1264
1265         if (lang_str) {
1266                 elm_language_set(lang_str);
1267                 free(lang_str);
1268         }
1269 }
1270
1271
1272
1273 int main(int argc, char* argv[])
1274 {
1275         App_Data app_data = {0, };
1276         ui_app_lifecycle_callback_s event_callback = {0, };
1277         app_event_handler_h handlers[5] = {NULL, };
1278
1279         event_callback.create = _app_create;
1280         event_callback.terminate = _app_terminate;
1281         event_callback.pause = _app_pause;
1282         event_callback.resume = _app_resume;
1283         event_callback.app_control = _app_service;
1284
1285         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, NULL, &app_data);
1286         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, NULL, &app_data);
1287         ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, NULL, &app_data);
1288         ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _app_language_changed, &app_data);
1289         ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, NULL, &app_data);
1290
1291         _init_app_data(&app_data);
1292
1293         int ret = ui_app_main(argc, argv, &event_callback, &app_data);
1294         if (ret != APP_ERROR_NONE) {
1295                 LOGD("ui_app_main() is failed. err = %d", ret);
1296         }
1297
1298         return ret;
1299 }