2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 //#include <csc-feature.h>
27 #include <smartreply_service.h>
29 #include "w-input-smartreply.h"
31 #ifdef SUPPORT_SMART_ACTION
32 #include "w-input-smartaction.h"
35 #ifndef INPUT_SMARTREPLY_VCONF
36 #define INPUT_SMARTREPLY_VCONF "db/wms/smart_reply"
40 typedef struct _InputSmartreplyData InputSmartreplyData;
42 struct _InputSmartreplyData
51 input_smartreply_changed callback;
54 smartreply_reply_h *candidate_list;
55 int candidate_list_len;
60 static void _input_smartreply_get_reply_callback(int handle, int error,
61 smartreply_reply_h* candidate_list, int length);
62 /* Disable smartreply on/off feature
63 static void _input_smartreply_vconf_changed(keynode_t *key, void *data);
67 static InputSmartreplyData *g_input_smartreply_data = NULL;
69 static void _input_smartreply_get_reply_callback(int handle, int error,
70 smartreply_reply_h* candidate_list, int length)
72 if (g_input_smartreply_data == NULL) {
73 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
77 if (g_input_smartreply_data == NULL) {
78 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
82 if (g_input_smartreply_data->candidate_list) {
85 ret = smartreply_service_destory_list(g_input_smartreply_data->candidate_list);
86 if (ret != SMARTREPLY_ERROR_NONE)
87 PRINTFUNC(DLOG_ERROR, "can not destroy list : %d", ret);
90 if (error != SMARTREPLY_ERROR_NONE) {
91 PRINTFUNC(DLOG_ERROR, "can not get candidate list : %d", error);
93 g_input_smartreply_data->candidate_list = NULL;
94 g_input_smartreply_data->candidate_list_len = 0;
99 g_input_smartreply_data->candidate_list = candidate_list;
100 g_input_smartreply_data->candidate_list_len = length;
102 if (g_input_smartreply_data->callback)
103 g_input_smartreply_data->callback(g_input_smartreply_data->user_data);
106 static bool _smartreply_check_here_map_support(void)
111 /* Disable smartreply on/off feature
112 static void _input_smartreply_vconf_changed(keynode_t *key, void *data)
116 if (g_input_smartreply_data == NULL) {
117 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
121 enabled = vconf_keynode_get_int(key);
122 if (g_input_smartreply_data->enabled == enabled)
125 g_input_smartreply_data->enabled = enabled;
127 if (g_input_smartreply_data->enabled && g_input_smartreply_data->candidate_list == NULL) {
128 input_smartreply_get_reply_async();
132 if (g_input_smartreply_data->callback)
133 g_input_smartreply_data->callback(g_input_smartreply_data->user_data);
137 bool input_smartreply_init(app_control_h app_control)
141 char *caller_id = NULL;
143 char *message = NULL;
145 /* Disable smartreply on/off feature
149 ret = app_control_get_extra_data(app_control,
150 APP_CONTROL_DATA_INPUT_PREDICTION_HINT, &message);
152 if (ret != APP_CONTROL_ERROR_NONE) {
153 PRINTFUNC(DLOG_WARN, "Can not get APP_CONTROL_DATA_INPUT_PREDICTION_HINT %d", ret);
157 ret = app_control_get_caller(app_control, &caller_id);
159 if (ret != APP_CONTROL_ERROR_NONE)
160 PRINTFUNC(DLOG_WARN, "Can not get caller id %d", ret);
162 ret = app_control_get_extra_data(app_control,
163 "template_sender", &sender);
165 if (ret != APP_CONTROL_ERROR_NONE)
166 PRINTFUNC(DLOG_WARN, "Can not get template_sender %d", ret);
168 if (g_input_smartreply_data == NULL) {
169 g_input_smartreply_data = (InputSmartreplyData *)calloc(1, sizeof(InputSmartreplyData));
170 if (g_input_smartreply_data == NULL) {
171 PRINTFUNC(DLOG_ERROR, "Can not alloc InputSmartreplyData");
185 if (g_input_smartreply_data->caller_id)
186 free(g_input_smartreply_data->caller_id);
188 if (g_input_smartreply_data->sender)
189 free(g_input_smartreply_data->sender);
191 if (g_input_smartreply_data->message)
192 free(g_input_smartreply_data->message);
194 if (g_input_smartreply_data->lang)
195 free(g_input_smartreply_data->lang);
197 if (g_input_smartreply_data->candidate_list) {
198 ret = smartreply_service_destory_list(g_input_smartreply_data->candidate_list);
199 if (ret != SMARTREPLY_ERROR_NONE)
200 PRINTFUNC(DLOG_ERROR, "can not destroy list : %d", ret);
203 g_input_smartreply_data->caller_id = caller_id;
204 g_input_smartreply_data->sender = sender;
205 g_input_smartreply_data->message = message;
206 g_input_smartreply_data->lang = NULL;
208 g_input_smartreply_data->enabled = 1;
210 g_input_smartreply_data->callback = NULL;
211 g_input_smartreply_data->user_data = NULL;
213 g_input_smartreply_data->candidate_list = NULL;
214 g_input_smartreply_data->candidate_list_len = 0;
216 g_input_smartreply_data->enable_location = _smartreply_check_here_map_support();
219 /* Disable smartreply on/off feature
220 ret = vconf_get_int(INPUT_SMARTREPLY_VCONF, &enabled);
222 PRINTFUNC(DLOG_ERROR, "can not get vconf : %s", INPUT_SMARTREPLY_VCONF);
224 g_input_smartreply_data->enabled = enabled;
226 ret = vconf_notify_key_changed(INPUT_SMARTREPLY_VCONF,
227 _input_smartreply_vconf_changed, NULL);
229 PRINTFUNC(DLOG_ERROR, "Can not create vconf notify : %s", INPUT_SMARTREPLY_VCONF);
235 void input_smartreply_deinit(void)
237 if (g_input_smartreply_data == NULL) {
238 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
242 if (g_input_smartreply_data->caller_id)
243 free(g_input_smartreply_data->caller_id);
245 if (g_input_smartreply_data->sender)
246 free(g_input_smartreply_data->sender);
248 if (g_input_smartreply_data->message)
249 free(g_input_smartreply_data->message);
252 if (g_input_smartreply_data->candidate_list) {
255 ret = smartreply_service_destory_list(g_input_smartreply_data->candidate_list);
256 if (ret != SMARTREPLY_ERROR_NONE)
257 PRINTFUNC(DLOG_ERROR, "can not destroy list : %d", ret);
260 input_smartreply_unset_notify();
262 /* Disable smartreply on/off feature
263 vconf_ignore_key_changed(INPUT_SMARTREPLY_VCONF,
264 _input_smartreply_vconf_changed);
267 free(g_input_smartreply_data);
268 g_input_smartreply_data = NULL;
273 bool input_smartreply_get_reply(void)
277 smartreply_reply_h *candidate_list = NULL;
278 int candidate_list_len = 0;
280 if (g_input_smartreply_data == NULL) {
281 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
285 if (g_input_smartreply_data->message == NULL) {
286 PRINTFUNC(DLOG_ERROR, "message is empty");
290 if (g_input_smartreply_data->enabled == 0) {
291 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
295 ret = smartreply_service_get_replies(g_input_smartreply_data->caller_id,
296 g_input_smartreply_data->sender,
297 g_input_smartreply_data->message,
299 &candidate_list_len);
301 if (ret != SMARTREPLY_ERROR_NONE) {
302 PRINTFUNC(DLOG_ERROR, "Can not get replies : %d", ret);
306 if (g_input_smartreply_data->candidate_list) {
307 ret = smartreply_service_destory_list(g_input_smartreply_data->candidate_list);
308 if (ret != SMARTREPLY_ERROR_NONE)
309 PRINTFUNC(DLOG_ERROR, "can not destroy list : %d", ret);
312 g_input_smartreply_data->candidate_list = candidate_list;
313 g_input_smartreply_data->candidate_list_len = candidate_list_len;
318 bool input_smartreply_get_reply_async(void)
320 if (g_input_smartreply_data == NULL) {
321 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
325 if (g_input_smartreply_data->message == NULL) {
326 PRINTFUNC(DLOG_ERROR, "message is empty");
330 if (g_input_smartreply_data->enabled == 0) {
331 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
335 smartreply_service_get_replies_async(g_input_smartreply_data->caller_id,
336 g_input_smartreply_data->sender,
337 g_input_smartreply_data->message,
338 _input_smartreply_get_reply_callback);
343 int input_smartreply_get_reply_num(void)
345 if (g_input_smartreply_data == NULL) {
346 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
350 if (g_input_smartreply_data->enabled == 0) {
351 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
355 return g_input_smartreply_data->candidate_list_len;
358 char *input_smartreply_get_nth_item(int index, int *type)
361 char *message = NULL;
363 if (g_input_smartreply_data == NULL) {
364 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
368 if (g_input_smartreply_data->candidate_list == NULL) {
369 PRINTFUNC(DLOG_ERROR, "Can not get candidate list");
373 if (g_input_smartreply_data->enabled == 0) {
374 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
379 ret = smartreply_service_get_nth_reply_message(g_input_smartreply_data->candidate_list,
383 if (ret != SMARTREPLY_ERROR_NONE) {
384 PRINTFUNC(DLOG_ERROR, "Can not get message from index %d : %d", index, ret);
391 bool input_smartreply_send_feedback(const char *str)
395 if (g_input_smartreply_data == NULL) {
396 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
401 PRINTFUNC(DLOG_ERROR, "str is empty");
405 ret = smartreply_service_record_user_reply(g_input_smartreply_data->caller_id,
406 g_input_smartreply_data->sender,
407 g_input_smartreply_data->message,
410 if (ret != SMARTREPLY_ERROR_NONE) {
411 PRINTFUNC(DLOG_ERROR, "Can not send feedback : %d", ret);
420 void input_smartreply_set_notify(input_smartreply_changed callback,
423 if (callback == NULL) {
424 PRINTFUNC(DLOG_ERROR, "empty callback function");
428 if (g_input_smartreply_data == NULL) {
429 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
433 g_input_smartreply_data->callback = callback;
434 g_input_smartreply_data->user_data = user_data;
438 void input_smartreply_unset_notify(void)
440 if (g_input_smartreply_data == NULL) {
441 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
445 g_input_smartreply_data->callback = NULL;
446 g_input_smartreply_data->user_data = NULL;
449 bool input_smartreply_is_enabled(void)
451 if (g_input_smartreply_data == NULL) {
452 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
456 if (g_input_smartreply_data->enabled)