Add smartreply feature
[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                         "template_context", &message);
151
152         if (ret != APP_CONTROL_ERROR_NONE) {
153                 PRINTFUNC(DLOG_WARN, "Can not get template_context %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     int ret;
321
322         if (g_input_smartreply_data == NULL) {
323                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
324                 return false;
325         }
326
327         if (g_input_smartreply_data->message == NULL) {
328                 PRINTFUNC(DLOG_ERROR, "message is empty");
329                 return false;
330         }
331
332         if (g_input_smartreply_data->enabled == 0) {
333                 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
334                 return false;
335         }
336
337         ret = smartreply_service_get_replies_async(g_input_smartreply_data->caller_id,
338                                         g_input_smartreply_data->sender,
339                                         g_input_smartreply_data->message,
340                                         _input_smartreply_get_reply_callback);
341
342         return true;
343 }
344
345 int input_smartreply_get_reply_num(void)
346 {
347         if (g_input_smartreply_data == NULL) {
348                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
349                 return 0;
350         }
351
352         if (g_input_smartreply_data->enabled == 0) {
353                 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
354                 return 0;
355         }
356
357         return g_input_smartreply_data->candidate_list_len;
358 }
359
360 char *input_smartreply_get_nth_item(int index, int *type)
361 {
362         int ret;
363         char *message = NULL;
364
365         if (g_input_smartreply_data == NULL) {
366                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
367                 return NULL;
368         }
369
370         if (g_input_smartreply_data->candidate_list == NULL) {
371                 PRINTFUNC(DLOG_ERROR, "Can not get candidate list");
372                 return NULL;
373         }
374
375         if (g_input_smartreply_data->enabled == 0) {
376                 PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
377                 return NULL;
378         }
379
380
381         ret = smartreply_service_get_nth_reply_message(g_input_smartreply_data->candidate_list,
382                                                         index,
383                                                         &message);
384
385         if (ret != SMARTREPLY_ERROR_NONE) {
386                 PRINTFUNC(DLOG_ERROR, "Can not get message from index %d : %d", index, ret);
387                 return NULL;
388         }
389
390         return message;
391 }
392
393 bool input_smartreply_send_feedback(const char *str)
394 {
395         int ret;
396
397         if (g_input_smartreply_data == NULL) {
398                 PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
399                 return false;
400         }
401
402         if (str == NULL) {
403                 PRINTFUNC(DLOG_ERROR, "str is empty");
404                 return false;
405         }
406
407         ret = smartreply_service_record_user_reply(g_input_smartreply_data->caller_id,
408                                                 g_input_smartreply_data->sender,
409                                                 g_input_smartreply_data->message,
410                                                 str);
411
412         if (ret != SMARTREPLY_ERROR_NONE) {
413                 PRINTFUNC(DLOG_ERROR, "Can not send feedback : %d", ret);
414                 return false;
415         }
416
417         return true;
418 }
419
420
421
422 void input_smartreply_set_notify(input_smartreply_changed callback,
423                         void *user_data)
424 {
425         if (callback == NULL) {
426                 PRINTFUNC(DLOG_ERROR, "empty callback function");
427                 return;
428         }
429
430         if (g_input_smartreply_data == NULL) {
431                 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
432                 return;
433         }
434
435         g_input_smartreply_data->callback = callback;
436         g_input_smartreply_data->user_data = user_data;
437 }
438
439
440 void input_smartreply_unset_notify(void)
441 {
442         if (g_input_smartreply_data == NULL) {
443                 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
444                 return;
445         }
446
447         g_input_smartreply_data->callback = NULL;
448         g_input_smartreply_data->user_data = NULL;
449 }
450
451 bool input_smartreply_is_enabled(void)
452 {
453         if (g_input_smartreply_data == NULL) {
454                 PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
455                 return false;
456         }
457
458         if (g_input_smartreply_data->enabled)
459                 return true;
460
461         return false;
462 }