+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Debug.h"
+
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+#include <locale.h>
+
+#include <dlog.h>
+#include <vconf.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
+
+
+typedef struct _InputSmartreplyData InputSmartreplyData;
+
+struct _InputSmartreplyData
+{
+ char *caller_id;
+ char *sender;
+ char *message;
+ char *lang;
+
+ int enabled;
+
+ input_smartreply_changed callback;
+ void *user_data;
+
+ smartreply_reply_h *candidate_list;
+ int candidate_list_len;
+ bool enable_location;
+};
+
+
+static void _input_smartreply_get_reply_callback(int handle, int error,
+ smartreply_reply_h* candidate_list, int length);
+/* Disable smartreply on/off feature
+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,
+ 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,
+ "template_context", &message);
+
+ if (ret != APP_CONTROL_ERROR_NONE) {
+ PRINTFUNC(DLOG_WARN, "Can not get template_context %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)
+{
+ int ret;
+
+ 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_async(g_input_smartreply_data->caller_id,
+ g_input_smartreply_data->sender,
+ g_input_smartreply_data->message,
+ _input_smartreply_get_reply_callback);
+
+ return true;
+}
+
+int input_smartreply_get_reply_num(void)
+{
+ if (g_input_smartreply_data == NULL) {
+ PRINTFUNC(DLOG_ERROR, "InputSmartreplyData uninitialized");
+ return 0;
+ }
+
+ if (g_input_smartreply_data->enabled == 0) {
+ PRINTFUNC(DLOG_WARN, "Smartreply is disabled");
+ return 0;
+ }
+
+ return g_input_smartreply_data->candidate_list_len;
+}
+
+char *input_smartreply_get_nth_item(int index, int *type)
+{
+ 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;
+}
+
+
+
+void input_smartreply_set_notify(input_smartreply_changed callback,
+ void *user_data)
+{
+ if (callback == NULL) {
+ PRINTFUNC(DLOG_ERROR, "empty callback function");
+ return;
+ }
+
+ if (g_input_smartreply_data == NULL) {
+ PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
+ return;
+ }
+
+ g_input_smartreply_data->callback = callback;
+ g_input_smartreply_data->user_data = user_data;
+}
+
+
+void input_smartreply_unset_notify(void)
+{
+ if (g_input_smartreply_data == NULL) {
+ PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
+ return;
+ }
+
+ g_input_smartreply_data->callback = NULL;
+ g_input_smartreply_data->user_data = NULL;
+}
+
+bool input_smartreply_is_enabled(void)
+{
+ if (g_input_smartreply_data == NULL) {
+ PRINTFUNC(DLOG_WARN, "InputSmartreplyData uninitialized");
+ return false;
+ }
+
+ if (g_input_smartreply_data->enabled)
+ return true;
+
+ return false;
+}