Coding rule check
[platform/core/uifw/inputdelegator.git] / src / w-input-keyboard.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 <Elementary.h>
21
22 #include <stdlib.h>
23
24 #include "w-input-keyboard.h"
25 #include "w-input-selector.h"
26 #include "w-input-smartreply.h"
27
28 extern InputKeyboardData g_input_keyboard_data;
29 extern App_Data* app_data;
30
31 Evas_Object *entry;
32
33 static void input_keyboard_app_control_send_reply_cb(app_control_h request, app_control_h reply,
34                                                 app_control_result_e result, void *user_data);
35
36 static void input_keyboard_app_control_send_reply_cb(app_control_h request, app_control_h reply,
37                                                 app_control_result_e result, void *user_data)
38 {
39     char *feedback = NULL;
40         if (result == APP_CONTROL_RESULT_SUCCEEDED) {
41         if (reply) {
42                         int ret;
43                         ret = app_control_get_extra_data(reply,
44                                         "template_feedback", &feedback);
45
46                         if (ret == APP_CONTROL_ERROR_NONE) {
47                                 if (feedback) {
48                                         input_smartreply_send_feedback(feedback);
49                                 }
50                         }
51                 }
52                 reply_to_sender_by_callback(feedback, "keyboard");
53         free(feedback);
54                 elm_exit();
55         }
56 }
57
58
59 bool input_keyboard_init(app_control_h app_control)
60 {
61         int ret = -1;
62
63         char *app_id = NULL;
64         char **data_array = NULL;
65         int data_array_len = -1;
66     char *default_text = NULL;
67     char *guide_text = NULL;
68
69         input_keyboard_deinit();
70
71     ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_INPUT_DEFAULT_TEXT, &default_text);
72     if (ret == APP_CONTROL_ERROR_NONE) {
73         g_input_keyboard_data.default_text = default_text;
74     }
75     ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_INPUT_GUIDE_TEXT, &guide_text);
76     if (ret == APP_CONTROL_ERROR_NONE) {
77         g_input_keyboard_data.guide_text = guide_text;
78     }
79
80         ret = app_control_get_extra_data(app_control,
81                                         "selector_keyboard_app_id",
82                                         &app_id);
83
84         if (ret != APP_CONTROL_ERROR_NONE) {
85                 PRINTFUNC(DLOG_WARN, "can not get selector_keyboard_app_id: %d", ret);
86                 return false;
87         }
88
89         ret = app_control_get_extra_data_array(app_control,
90                                         "selector_keyboard_data_array",
91                                         &data_array, &data_array_len);
92
93
94         if (ret != APP_CONTROL_ERROR_NONE) {
95                 if (app_id)
96                         free(app_id);
97
98                 PRINTFUNC(DLOG_WARN, "can not get selector_keyboard_data_array : %d", ret);
99                 return false;
100         }
101
102         if (app_id)
103                 g_input_keyboard_data.app_id = app_id;
104
105         if (data_array)
106                 g_input_keyboard_data.data_array = data_array;
107
108         g_input_keyboard_data.data_array_len = data_array_len;
109
110         PRINTFUNC(DLOG_WARN, "app_id = %s", app_id);
111         PRINTFUNC(DLOG_WARN, "data_array = %s", data_array);
112
113         return true;
114 }
115
116 void input_keyboard_deinit(void)
117 {
118         int i = 0;
119         char **data_array = NULL;
120
121         if (g_input_keyboard_data.guide_text)
122                 free(g_input_keyboard_data.guide_text);
123
124         if (g_input_keyboard_data.default_text)
125                 free(g_input_keyboard_data.default_text);
126
127         if (g_input_keyboard_data.app_id)
128                 free(g_input_keyboard_data.app_id);
129
130         data_array = g_input_keyboard_data.data_array;
131         if (data_array) {
132                 for (i = 0; i < g_input_keyboard_data.data_array_len; i++) {
133                         if (*(data_array + i))
134                                 free(*(data_array + i));
135                 }
136
137                 free(data_array);
138         }
139
140         g_input_keyboard_data.app_id = NULL;
141         g_input_keyboard_data.data_array = NULL;
142         g_input_keyboard_data.data_array_len = 0;
143
144         return;
145 }
146
147 bool input_keyboard_launch(Evas_Object *window)
148 {
149         int ret;
150
151         char *app_id;
152
153         app_control_h app_control;
154
155         if (window == NULL) {
156                 PRINTFUNC(DLOG_ERROR, "Can not get window");
157                 return false;
158         }
159
160         ret = app_control_create(&app_control);
161         if (ret != APP_CONTROL_ERROR_NONE) {
162                 PRINTFUNC(DLOG_ERROR, "Can not create app_control : %d", ret);
163                 return false;
164         }
165
166         app_id = g_input_keyboard_data.app_id;
167         if (app_id == NULL) {
168                 PRINTFUNC(DLOG_INFO, "app id is undefined, use defualt");
169                 app_id = "com.samsung.message.appcontrol.compose";
170         }
171
172         app_control_set_app_id(app_control, app_id);
173         app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
174 /*
175         ret =  app_control_set_window(app_control,
176                                 elm_win_xwindow_get(window));
177         if (ret != APP_CONTROL_ERROR_NONE) {
178                 PRINTFUNC(DLOG_ERROR, "Can not app control set window : %d", ret);
179                 app_control_destroy(app_control);
180                 return false;
181         }
182 */
183         if (g_input_keyboard_data.data_array && g_input_keyboard_data.data_array_len > 0) {
184                  app_control_add_extra_data_array(app_control,
185                                                  "selector_keyboard_data_array",
186                                                  (const char**)(g_input_keyboard_data.data_array),
187                                                  g_input_keyboard_data.data_array_len);
188         }
189
190         set_source_caller_app_id(app_control);
191
192         ret = app_control_send_launch_request(app_control,
193                                         input_keyboard_app_control_send_reply_cb,
194                                         NULL);
195         if (ret != APP_CONTROL_ERROR_NONE)
196                 PRINTFUNC(DLOG_ERROR, "Can not launch app_control: %d", ret);
197
198         app_control_destroy(app_control);
199
200         return true;
201 }
202
203 void btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
204 {
205     app_control_h app_control;
206     int ret = app_control_create(&app_control);
207         if (ret != APP_CONTROL_ERROR_NONE) {
208                 PRINTFUNC(DLOG_ERROR, "Can not create app_control : %d", ret);
209                 return;
210         }
211
212     const char *getText = elm_entry_entry_get(entry);
213     LOGD("button key clicked!! : getText = %s", getText);
214
215     if (getText)
216         input_smartreply_send_feedback(getText);
217
218     char *app_id = NULL;
219     app_control_get_caller(app_data->source_app_control, &app_id);
220     if (app_id != NULL)
221         app_control_set_app_id(app_control, app_id);
222         app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
223     set_source_caller_app_id(app_control);
224     free(app_id);
225         reply_to_sender_by_callback(getText, "keyboard");
226         elm_exit();
227 }
228
229 static Eina_Bool custom_back_cb(void *data, Elm_Object_Item *it)
230 {
231     _back_to_genlist_for_selector();
232     return EINA_TRUE;
233 }
234
235 void create_fullscreen_editor(void *data)
236 {
237         App_Data *ad = (App_Data *)data;
238
239     Evas_Object *box = elm_box_add(ad->naviframe);
240     evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
241     evas_object_show(box);
242     elm_win_resize_object_add(ad->naviframe, box);
243
244     entry = elm_entry_add(box);
245         elm_entry_single_line_set(entry, EINA_TRUE);
246         elm_entry_scrollable_set(entry, EINA_TRUE);
247         elm_scroller_policy_set(entry, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
248         elm_object_part_text_set(entry, "elm.guide", g_input_keyboard_data.guide_text);
249     elm_entry_entry_set(entry, g_input_keyboard_data.default_text);
250         elm_entry_cursor_end_set(entry);
251         evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
252         evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
253         evas_object_show(entry);
254         elm_box_pack_end(box, entry);
255
256         Evas_Object *btn = elm_button_add(box);
257         elm_object_text_set(btn, "SEND");
258         evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, 0.5);
259         evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
260         evas_object_smart_callback_add(btn, "clicked", btn_clicked_cb, NULL);
261         evas_object_show(btn);
262         elm_box_pack_end(box, btn);
263         evas_object_resize(ad->naviframe, 360, 360);
264         evas_object_show(ad->naviframe);
265
266     Elm_Object_Item *nf_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, box, "empty");
267     elm_naviframe_item_pop_cb_set(nf_item, custom_back_cb, NULL);
268 }
269
270 bool input_keyboard_launch_with_ui(Evas_Object *window, void *data) {
271         if (window == NULL) {
272                 PRINTFUNC(DLOG_ERROR, "Can not get window");
273                 return false;
274         }
275     create_fullscreen_editor(data);
276     return true;
277 }
278