Update package version to 0.1.171018
[platform/core/uifw/inputdelegator.git] / src / w-input-smartreply.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 <glib.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <locale.h>
23
24 #include <dlog.h>
25 #include <vconf.h>
26 //#include <csc-feature.h>
27 #include <smartreply_service.h>
28
29 #include "w-input-smartreply.h"
30
31 #ifdef SUPPORT_SMART_ACTION
32 #include "w-input-smartaction.h"
33 #endif
34
35 #ifndef INPUT_SMARTREPLY_VCONF
36 #define INPUT_SMARTREPLY_VCONF "db/wms/smart_reply"
37 #endif
38
39
40 typedef struct _InputSmartreplyData InputSmartreplyData;
41
42 struct _InputSmartreplyData
43 {
44         char *caller_id;
45         char *sender;
46         char *message;
47         char *lang;
48
49         int enabled;
50
51         input_smartreply_changed callback;
52         void *user_data;
53
54         smartreply_reply_h *candidate_list;
55         int candidate_list_len;
56         bool enable_location;
57 };
58
59
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);
64 */
65
66
67 static InputSmartreplyData *g_input_smartreply_data = NULL;
68
69 static void _input_smartreply_get_reply_callback(int handle, int error,
70                                                 smartreply_reply_h* candidate_list, int length)
71 {
72         if (g_input_smartreply_data == NULL) {
73                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
74                 return;
75         }
76
77         if (g_input_smartreply_data == NULL) {
78                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
79                 return;
80         }
81
82         if (g_input_smartreply_data->candidate_list) {
83                 int ret;
84
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);
88         }
89
90         if (error != SMARTREPLY_ERROR_NONE) {
91                 PRINTFUNC(DLOG_ERROR, "can not get candidate list : %d", error);
92
93                 g_input_smartreply_data->candidate_list = NULL;
94                 g_input_smartreply_data->candidate_list_len = 0;
95
96                 return;
97         }
98
99         g_input_smartreply_data->candidate_list = candidate_list;
100         g_input_smartreply_data->candidate_list_len = length;
101
102         if (g_input_smartreply_data->callback)
103                 g_input_smartreply_data->callback(g_input_smartreply_data->user_data);
104 }
105
106 static bool _smartreply_check_here_map_support(void)
107 {
108     return true;
109 }
110
111 /* Disable smartreply on/off feature
112 static void _input_smartreply_vconf_changed(keynode_t *key, void *data)
113 {
114         int enabled = 0;
115
116         if (g_input_smartreply_data == NULL) {
117                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
118                 return;
119         }
120
121         enabled = vconf_keynode_get_int(key);
122         if (g_input_smartreply_data->enabled == enabled)
123                 return;
124
125         g_input_smartreply_data->enabled = enabled;
126
127         if (g_input_smartreply_data->enabled && g_input_smartreply_data->candidate_list == NULL) {
128                 input_smartreply_get_reply_async();
129                 return;
130         }
131
132         if (g_input_smartreply_data->callback)
133                 g_input_smartreply_data->callback(g_input_smartreply_data->user_data);
134 }
135 */
136
137 bool input_smartreply_init(app_control_h app_control)
138 {
139         int ret;
140
141         char *caller_id = NULL;
142         char *sender = NULL;
143         char *message = NULL;
144
145         /* Disable smartreply on/off feature
146         int enabled = 0;
147         */
148
149         ret = app_control_get_extra_data(app_control,
150                         APP_CONTROL_DATA_INPUT_PREDICTION_HINT, &message);
151
152         if (ret != APP_CONTROL_ERROR_NONE) {
153                 PRINTFUNC(DLOG_WARN, "Can not get APP_CONTROL_DATA_INPUT_PREDICTION_HINT %d", ret);
154                 return false;
155         }
156
157         ret = app_control_get_caller(app_control, &caller_id);
158
159         if (ret != APP_CONTROL_ERROR_NONE)
160                 PRINTFUNC(DLOG_WARN, "Can not get caller id %d", ret);
161
162         ret = app_control_get_extra_data(app_control,
163                         "template_sender", &sender);
164
165         if (ret != APP_CONTROL_ERROR_NONE)
166                 PRINTFUNC(DLOG_WARN, "Can not get template_sender %d", ret);
167
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");
172                         if (message)
173                                 free(message);
174
175                         if (caller_id)
176                                 free(caller_id);
177
178                         if (sender)
179                                 free(sender);
180
181                         return false;
182                 }
183         }
184
185         if (g_input_smartreply_data->caller_id)
186                 free(g_input_smartreply_data->caller_id);
187
188         if (g_input_smartreply_data->sender)
189                 free(g_input_smartreply_data->sender);
190
191         if (g_input_smartreply_data->message)
192                 free(g_input_smartreply_data->message);
193
194         if (g_input_smartreply_data->lang)
195                 free(g_input_smartreply_data->lang);
196
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);
201         }
202
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;
207
208         g_input_smartreply_data->enabled = 1;
209
210         g_input_smartreply_data->callback = NULL;
211         g_input_smartreply_data->user_data = NULL;
212
213         g_input_smartreply_data->candidate_list = NULL;
214         g_input_smartreply_data->candidate_list_len = 0;
215
216         g_input_smartreply_data->enable_location = _smartreply_check_here_map_support();
217
218
219         /* Disable smartreply on/off feature
220         ret = vconf_get_int(INPUT_SMARTREPLY_VCONF, &enabled);
221         if (ret == -1)
222                 PRINTFUNC(DLOG_ERROR, "can not get vconf : %s", INPUT_SMARTREPLY_VCONF);
223
224         g_input_smartreply_data->enabled = enabled;
225
226         ret = vconf_notify_key_changed(INPUT_SMARTREPLY_VCONF,
227                                 _input_smartreply_vconf_changed, NULL);
228         if (ret == -1)
229                 PRINTFUNC(DLOG_ERROR, "Can not create vconf notify : %s", INPUT_SMARTREPLY_VCONF);
230         */
231
232         return true;
233 }
234
235 void input_smartreply_deinit(void)
236 {
237         if (g_input_smartreply_data == NULL) {
238                 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
239                 return;
240         }
241
242         if (g_input_smartreply_data->caller_id)
243                 free(g_input_smartreply_data->caller_id);
244
245         if (g_input_smartreply_data->sender)
246                 free(g_input_smartreply_data->sender);
247
248         if (g_input_smartreply_data->message)
249                 free(g_input_smartreply_data->message);
250
251
252         if (g_input_smartreply_data->candidate_list) {
253                 int ret;
254
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);
258         }
259
260         input_smartreply_unset_notify();
261
262         /* Disable smartreply on/off feature
263         vconf_ignore_key_changed(INPUT_SMARTREPLY_VCONF,
264                                 _input_smartreply_vconf_changed);
265         */
266
267         free(g_input_smartreply_data);
268         g_input_smartreply_data = NULL;
269
270         return;
271 }
272
273 bool input_smartreply_get_reply(void)
274 {
275         int ret;
276
277         smartreply_reply_h *candidate_list = NULL;
278         int candidate_list_len = 0;
279
280         if (g_input_smartreply_data == NULL) {
281                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
282                 return false;
283         }
284
285         if (g_input_smartreply_data->message == NULL) {
286                 PRINTFUNC(DLOG_ERROR, "message is empty");
287                 return false;
288         }
289
290         if (g_input_smartreply_data->enabled == 0) {
291                 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
292                 return false;
293         }
294
295         ret = smartreply_service_get_replies(g_input_smartreply_data->caller_id,
296                                         g_input_smartreply_data->sender,
297                                         g_input_smartreply_data->message,
298                                         &candidate_list,
299                                         &candidate_list_len);
300
301         if (ret != SMARTREPLY_ERROR_NONE)  {
302                 PRINTFUNC(DLOG_ERROR, "Can not get replies : %d", ret);
303                 return false;
304         }
305
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);
310         }
311
312         g_input_smartreply_data->candidate_list = candidate_list;
313         g_input_smartreply_data->candidate_list_len = candidate_list_len;
314
315         return true;
316 }
317
318 bool input_smartreply_get_reply_async(void)
319 {
320         if (g_input_smartreply_data == NULL) {
321                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
322                 return false;
323         }
324
325         if (g_input_smartreply_data->message == NULL) {
326                 PRINTFUNC(DLOG_ERROR, "message is empty");
327                 return false;
328         }
329
330         if (g_input_smartreply_data->enabled == 0) {
331                 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
332                 return false;
333         }
334
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);
339
340         return true;
341 }
342
343 int input_smartreply_get_reply_num(void)
344 {
345         if (g_input_smartreply_data == NULL) {
346                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
347                 return 0;
348         }
349
350         if (g_input_smartreply_data->enabled == 0) {
351                 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
352                 return 0;
353         }
354
355         return g_input_smartreply_data->candidate_list_len;
356 }
357
358 char *input_smartreply_get_nth_item(int index, int *type)
359 {
360         int ret;
361         char *message = NULL;
362
363         if (g_input_smartreply_data == NULL) {
364                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
365                 return NULL;
366         }
367
368         if (g_input_smartreply_data->candidate_list == NULL) {
369                 PRINTFUNC(DLOG_ERROR, "Can not get candidate list");
370                 return NULL;
371         }
372
373         if (g_input_smartreply_data->enabled == 0) {
374                 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
375                 return NULL;
376         }
377
378
379         ret = smartreply_service_get_nth_reply_message(g_input_smartreply_data->candidate_list,
380                                                         index,
381                                                         &message);
382
383         if (ret != SMARTREPLY_ERROR_NONE) {
384                 PRINTFUNC(DLOG_ERROR, "Can not get message from index %d : %d", index, ret);
385                 return NULL;
386         }
387
388         return message;
389 }
390
391 bool input_smartreply_send_feedback(const char *str)
392 {
393         int ret;
394
395         if (g_input_smartreply_data == NULL) {
396                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
397                 return false;
398         }
399
400         if (str == NULL) {
401                 PRINTFUNC(DLOG_ERROR, "str is empty");
402                 return false;
403         }
404
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,
408                                                 str);
409
410         if (ret != SMARTREPLY_ERROR_NONE) {
411                 PRINTFUNC(DLOG_ERROR, "Can not send feedback : %d", ret);
412                 return false;
413         }
414
415         return true;
416 }
417
418
419
420 void input_smartreply_set_notify(input_smartreply_changed callback,
421                         void *user_data)
422 {
423         if (callback == NULL) {
424                 PRINTFUNC(DLOG_ERROR, "empty callback function");
425                 return;
426         }
427
428         if (g_input_smartreply_data == NULL) {
429                 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
430                 return;
431         }
432
433         g_input_smartreply_data->callback = callback;
434         g_input_smartreply_data->user_data = user_data;
435 }
436
437
438 void input_smartreply_unset_notify(void)
439 {
440         if (g_input_smartreply_data == NULL) {
441                 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
442                 return;
443         }
444
445         g_input_smartreply_data->callback = NULL;
446         g_input_smartreply_data->user_data = NULL;
447 }
448
449 bool input_smartreply_is_enabled(void)
450 {
451         if (g_input_smartreply_data == NULL) {
452                 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
453                 return false;
454         }
455
456         if (g_input_smartreply_data->enabled)
457                 return true;
458
459         return false;
460 }