Update package version to 0.1.170907
[platform/core/uifw/inputdelegator.git] / src / w-input-smartreply.cpp
old mode 100755 (executable)
new mode 100644 (file)
index af67a96..aa1f5e6
 
 #include "Debug.h"
 
+#include <glib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <locale.h>
 
 #include <dlog.h>
 #include <vconf.h>
-//#include <smartreply_service.h>
+//#include <csc-feature.h>
+#include <smartreply_service.h>
 
 #include "w-input-smartreply.h"
 
+#ifdef SUPPORT_SMART_ACTION
+#include "w-input-smartaction.h"
+#endif
+
 #ifndef INPUT_SMARTREPLY_VCONF
 #define INPUT_SMARTREPLY_VCONF "db/wms/smart_reply"
 #endif
@@ -45,13 +51,14 @@ struct _InputSmartreplyData
        input_smartreply_changed callback;
        void *user_data;
 
-//     smartreply_reply_h *candidate_list;
+       smartreply_reply_h *candidate_list;
        int candidate_list_len;
+       bool enable_location;
 };
 
 
 static void _input_smartreply_get_reply_callback(int handle, int error,
-                                               int length);
+                                               smartreply_reply_h* candidate_list, int length);
 /* Disable smartreply on/off feature
 static void _input_smartreply_vconf_changed(keynode_t *key, void *data);
 */
@@ -60,30 +67,276 @@ static void _input_smartreply_vconf_changed(keynode_t *key, void *data);
 static InputSmartreplyData *g_input_smartreply_data = NULL;
 
 static void _input_smartreply_get_reply_callback(int handle, int error,
-                                               int length)
+                                               smartreply_reply_h* candidate_list, int length)
 {
+       if (g_input_smartreply_data == NULL) {
+               PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
+               return;
+       }
+
+       if (g_input_smartreply_data == NULL) {
+               PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
+               return;
+       }
+
+       if (g_input_smartreply_data->candidate_list) {
+               int ret;
+
+               ret = smartreply_service_destory_list(g_input_smartreply_data->candidate_list);
+               if (ret != SMARTREPLY_ERROR_NONE)
+                       PRINTFUNC(DLOG_ERROR, "can not destroy list : %d", ret);
+       }
+
+       if (error != SMARTREPLY_ERROR_NONE) {
+               PRINTFUNC(DLOG_ERROR, "can not get candidate list : %d", error);
+
+               g_input_smartreply_data->candidate_list = NULL;
+               g_input_smartreply_data->candidate_list_len = 0;
+
+               return;
+       }
 
+       g_input_smartreply_data->candidate_list = candidate_list;
+       g_input_smartreply_data->candidate_list_len = length;
+
+       if (g_input_smartreply_data->callback)
+               g_input_smartreply_data->callback(g_input_smartreply_data->user_data);
 }
 
+static bool _smartreply_check_here_map_support(void)
+{
+    return true;
+}
+
+/* Disable smartreply on/off feature
+static void _input_smartreply_vconf_changed(keynode_t *key, void *data)
+{
+       int enabled = 0;
 
+       if (g_input_smartreply_data == NULL) {
+               PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
+               return;
+       }
+
+       enabled = vconf_keynode_get_int(key);
+       if (g_input_smartreply_data->enabled == enabled)
+               return;
+
+       g_input_smartreply_data->enabled = enabled;
+
+       if (g_input_smartreply_data->enabled && g_input_smartreply_data->candidate_list == NULL) {
+               input_smartreply_get_reply_async();
+               return;
+       }
+
+       if (g_input_smartreply_data->callback)
+               g_input_smartreply_data->callback(g_input_smartreply_data->user_data);
+}
+*/
 
 bool input_smartreply_init(app_control_h app_control)
 {
+       int ret;
+
+       char *caller_id = NULL;
+       char *sender = NULL;
+       char *message = NULL;
+
+       /* Disable smartreply on/off feature
+       int enabled = 0;
+       */
+
+       ret = app_control_get_extra_data(app_control,
+                       APP_CONTROL_DATA_INPUT_PREDICTION_HINT, &message);
+
+       if (ret != APP_CONTROL_ERROR_NONE) {
+               PRINTFUNC(DLOG_WARN, "Can not get APP_CONTROL_DATA_INPUT_PREDICTION_HINT %d", ret);
+               return false;
+       }
+
+       ret = app_control_get_caller(app_control, &caller_id);
+
+       if (ret != APP_CONTROL_ERROR_NONE)
+               PRINTFUNC(DLOG_WARN, "Can not get caller id %d", ret);
+
+       ret = app_control_get_extra_data(app_control,
+                       "template_sender", &sender);
+
+       if (ret != APP_CONTROL_ERROR_NONE)
+               PRINTFUNC(DLOG_WARN, "Can not get template_sender %d", ret);
+
+       if (g_input_smartreply_data == NULL) {
+               g_input_smartreply_data = (InputSmartreplyData *)calloc(1, sizeof(InputSmartreplyData));
+               if (g_input_smartreply_data == NULL) {
+                       PRINTFUNC(DLOG_ERROR, "Can not alloc InputSmartreplyData");
+                       if (message)
+                               free(message);
+
+                       if (caller_id)
+                               free(caller_id);
+
+                       if (sender)
+                               free(sender);
+
+                       return false;
+               }
+       }
+
+       if (g_input_smartreply_data->caller_id)
+               free(g_input_smartreply_data->caller_id);
+
+       if (g_input_smartreply_data->sender)
+               free(g_input_smartreply_data->sender);
+
+       if (g_input_smartreply_data->message)
+               free(g_input_smartreply_data->message);
+
+       if (g_input_smartreply_data->lang)
+               free(g_input_smartreply_data->lang);
+
+       if (g_input_smartreply_data->candidate_list) {
+               ret = smartreply_service_destory_list(g_input_smartreply_data->candidate_list);
+               if (ret != SMARTREPLY_ERROR_NONE)
+                       PRINTFUNC(DLOG_ERROR, "can not destroy list : %d", ret);
+       }
+
+       g_input_smartreply_data->caller_id = caller_id;
+       g_input_smartreply_data->sender = sender;
+       g_input_smartreply_data->message = message;
+       g_input_smartreply_data->lang = NULL;
+
+       g_input_smartreply_data->enabled = 1;
+
+       g_input_smartreply_data->callback = NULL;
+       g_input_smartreply_data->user_data = NULL;
+
+       g_input_smartreply_data->candidate_list = NULL;
+       g_input_smartreply_data->candidate_list_len = 0;
+
+       g_input_smartreply_data->enable_location = _smartreply_check_here_map_support();
+
+
+       /* Disable smartreply on/off feature
+       ret = vconf_get_int(INPUT_SMARTREPLY_VCONF, &enabled);
+       if (ret == -1)
+               PRINTFUNC(DLOG_ERROR, "can not get vconf : %s", INPUT_SMARTREPLY_VCONF);
+
+       g_input_smartreply_data->enabled = enabled;
+
+       ret = vconf_notify_key_changed(INPUT_SMARTREPLY_VCONF,
+                               _input_smartreply_vconf_changed, NULL);
+       if (ret == -1)
+               PRINTFUNC(DLOG_ERROR, "Can not create vconf notify : %s", INPUT_SMARTREPLY_VCONF);
+       */
+
        return true;
 }
 
 void input_smartreply_deinit(void)
 {
+       if (g_input_smartreply_data == NULL) {
+               PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
+               return;
+       }
+
+       if (g_input_smartreply_data->caller_id)
+               free(g_input_smartreply_data->caller_id);
+
+       if (g_input_smartreply_data->sender)
+               free(g_input_smartreply_data->sender);
+
+       if (g_input_smartreply_data->message)
+               free(g_input_smartreply_data->message);
+
+
+       if (g_input_smartreply_data->candidate_list) {
+               int ret;
+
+               ret = smartreply_service_destory_list(g_input_smartreply_data->candidate_list);
+               if (ret != SMARTREPLY_ERROR_NONE)
+                       PRINTFUNC(DLOG_ERROR, "can not destroy list : %d", ret);
+       }
+
+       input_smartreply_unset_notify();
+
+       /* Disable smartreply on/off feature
+       vconf_ignore_key_changed(INPUT_SMARTREPLY_VCONF,
+                               _input_smartreply_vconf_changed);
+       */
+
+       free(g_input_smartreply_data);
+       g_input_smartreply_data = NULL;
+
        return;
 }
 
 bool input_smartreply_get_reply(void)
 {
+       int ret;
+
+       smartreply_reply_h *candidate_list = NULL;
+       int candidate_list_len = 0;
+
+       if (g_input_smartreply_data == NULL) {
+               PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
+               return false;
+       }
+
+       if (g_input_smartreply_data->message == NULL) {
+               PRINTFUNC(DLOG_ERROR, "message is empty");
+               return false;
+       }
+
+       if (g_input_smartreply_data->enabled == 0) {
+               PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
+               return false;
+       }
+
+       ret = smartreply_service_get_replies(g_input_smartreply_data->caller_id,
+                                       g_input_smartreply_data->sender,
+                                       g_input_smartreply_data->message,
+                                       &candidate_list,
+                                       &candidate_list_len);
+
+       if (ret != SMARTREPLY_ERROR_NONE)  {
+               PRINTFUNC(DLOG_ERROR, "Can not get replies : %d", ret);
+               return false;
+       }
+
+       if (g_input_smartreply_data->candidate_list) {
+               ret = smartreply_service_destory_list(g_input_smartreply_data->candidate_list);
+               if (ret != SMARTREPLY_ERROR_NONE)
+                       PRINTFUNC(DLOG_ERROR, "can not destroy list : %d", ret);
+       }
+
+       g_input_smartreply_data->candidate_list = candidate_list;
+       g_input_smartreply_data->candidate_list_len = candidate_list_len;
+
        return true;
 }
 
 bool input_smartreply_get_reply_async(void)
 {
+       if (g_input_smartreply_data == NULL) {
+               PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
+               return false;
+       }
+
+       if (g_input_smartreply_data->message == NULL) {
+               PRINTFUNC(DLOG_ERROR, "message is empty");
+               return false;
+       }
+
+       if (g_input_smartreply_data->enabled == 0) {
+               PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
+               return false;
+       }
+
+       smartreply_service_get_replies_async(g_input_smartreply_data->caller_id,
+                                       g_input_smartreply_data->sender,
+                                       g_input_smartreply_data->message,
+                                       _input_smartreply_get_reply_callback);
+
        return true;
 }
 
@@ -102,15 +355,63 @@ int input_smartreply_get_reply_num(void)
        return g_input_smartreply_data->candidate_list_len;
 }
 
-char *input_smartreply_get_nth_item(int index)
+char *input_smartreply_get_nth_item(int index, int *type)
 {
-    char *message = NULL;
+       int ret;
+       char *message = NULL;
+
+       if (g_input_smartreply_data == NULL) {
+               PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
+               return NULL;
+       }
+
+       if (g_input_smartreply_data->candidate_list == NULL) {
+               PRINTFUNC(DLOG_ERROR, "Can not get candidate list");
+               return NULL;
+       }
+
+       if (g_input_smartreply_data->enabled == 0) {
+               PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
+               return NULL;
+       }
+
+
+       ret = smartreply_service_get_nth_reply_message(g_input_smartreply_data->candidate_list,
+                                                       index,
+                                                       &message);
+
+       if (ret != SMARTREPLY_ERROR_NONE) {
+               PRINTFUNC(DLOG_ERROR, "Can not get message from index %d : %d", index, ret);
+               return NULL;
+       }
 
        return message;
 }
 
 bool input_smartreply_send_feedback(const char *str)
 {
+       int ret;
+
+       if (g_input_smartreply_data == NULL) {
+               PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
+               return false;
+       }
+
+       if (str == NULL) {
+               PRINTFUNC(DLOG_ERROR, "str is empty");
+               return false;
+       }
+
+       ret = smartreply_service_record_user_reply(g_input_smartreply_data->caller_id,
+                                               g_input_smartreply_data->sender,
+                                               g_input_smartreply_data->message,
+                                               str);
+
+       if (ret != SMARTREPLY_ERROR_NONE) {
+               PRINTFUNC(DLOG_ERROR, "Can not send feedback : %d", ret);
+               return false;
+       }
+
        return true;
 }