Fix buxton key layer
[platform/core/uifw/stt.git] / client / stt.c
index fa4dd9c..faf821f 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/wait.h>
 #include <system_info.h>
 #include <unistd.h>
+#include <buxton2.h>
 
 #include "stt.h"
 #include "stt_client.h"
@@ -85,20 +86,21 @@ static int __check_privilege_initialize()
        int ret = cynara_initialize(&p_cynara, NULL);
        if (CYNARA_API_SUCCESS != ret)
                SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to initialize");
-       
+
        return ret == CYNARA_API_SUCCESS;
 }
 
 static int __check_privilege(const char* uid, const char * privilege)
 {
        FILE *fp = NULL;
-       char smack_label[1024] = "/proc/self/attr/current";
+       char label_path[1024] = "/proc/self/attr/current";
+       char smack_label[1024] = {'\0',};
 
        if (!p_cynara) {
            return false;
        }
 
-       fp = fopen(smack_label, "r");
+       fp = fopen(label_path, "r");
        if (fp != NULL) {
            if (fread(smack_label, 1, sizeof(smack_label), fp) <= 0)
                SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to fread");
@@ -110,8 +112,10 @@ static int __check_privilege(const char* uid, const char * privilege)
        char *session = cynara_session_from_pid(pid);
        int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
        SLOG(LOG_DEBUG, TAG_STTC, "[Client]cynara_check returned %d(%s)", ret, (CYNARA_API_ACCESS_ALLOWED == ret) ? "Allowed" : "Denied");
-       if (session)
-           free(session);
+       if (session) {
+               free(session);
+               session = NULL;
+       }
 
        if (ret != CYNARA_API_ACCESS_ALLOWED)
            return false;
@@ -133,7 +137,7 @@ static int __stt_check_privilege()
                SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied");
                return STT_ERROR_PERMISSION_DENIED;
        } else if (-1 == g_privilege_allowed) {
-               if (false == __check_privilege_initialize()){
+               if (false == __check_privilege_initialize()) {
                        SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed");
                        return STT_ERROR_PERMISSION_DENIED;
                }
@@ -148,7 +152,7 @@ static int __stt_check_privilege()
        }
 
        g_privilege_allowed = 1;
-       return STT_ERROR_NONE;  
+       return STT_ERROR_NONE;
 }
 
 static const char* __stt_get_error_code(stt_error_e err)
@@ -168,6 +172,7 @@ static const char* __stt_get_error_code(stt_error_e err)
        case STT_ERROR_ENGINE_NOT_FOUND:        return "STT_ERROR_ENGINE_NOT_FOUND";
        case STT_ERROR_OPERATION_FAILED:        return "STT_ERROR_OPERATION_FAILED";
        case STT_ERROR_NOT_SUPPORTED_FEATURE:   return "STT_ERROR_NOT_SUPPORTED_FEATURE";
+       case STT_ERROR_SERVICE_RESET:           return "STT_ERROR_SERVICE_RESET";
        default:
                return "Invalid error code";
        }
@@ -243,7 +248,7 @@ void __stt_config_engine_changed_cb(const char* engine_id, const char* setting,
        if (NULL != client->engine_changed_cb) {
                client->engine_changed_cb(stt, engine_id, language, support_silence, need_credential, client->engine_changed_user_data);
        } else {
-               SLOG(LOG_WARN, TAG_STTC, "No registered callback function of supported languages");
+               SLOG(LOG_WARN, TAG_STTC, "No registered callback function for engine change");
        }
        return;
 }
@@ -501,6 +506,61 @@ int stt_get_engine(stt_h stt, char** engine_id)
        return ret;
 }
 
+int __stt_set_buxtonkey(const char* engine_id)
+{
+       /* Set vconfkey */
+       struct buxton_client * bux_cli;
+       struct buxton_layer * bux_layer;
+       struct buxton_value * bux_val;
+
+       int ret = buxton_open(&bux_cli, NULL, NULL);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] Fail to open buxton client");
+               return STT_ERROR_OPERATION_FAILED;
+       }
+       SLOG(LOG_DEBUG, stt_tag(), "[DBUS-BUXTON2] buxton_open: %d", ret);
+       bux_layer = buxton_create_layer("system");
+       if (NULL == bux_layer) {
+               SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] buxton_create_layer FAIL");
+               buxton_close(bux_cli);
+               bux_cli = NULL;
+               return STT_ERROR_OPERATION_FAILED;
+       }
+       bux_val = buxton_value_create_string(engine_id);
+       if (NULL == bux_val) {
+               SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] buxton_value_create_string FAIL");
+               buxton_free_layer(bux_layer);
+               buxton_close(bux_cli);
+               bux_layer = NULL;
+               bux_cli = NULL;
+               return STT_ERROR_OPERATION_FAILED;
+       }
+
+       ret = buxton_set_value_sync(bux_cli, bux_layer, STT_ENGINE_DB_CUSTOM, bux_val);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, stt_tag(), "[DBUS-BUXTON2] Fail to set value sync");
+               buxton_value_free(bux_val);
+               buxton_free_layer(bux_layer);
+               buxton_close(bux_cli);
+
+               bux_cli = NULL;
+               bux_layer = NULL;
+               bux_val = NULL;
+               return STT_ERROR_OPERATION_FAILED;
+       }
+       SLOG(LOG_DEBUG, stt_tag(), "[DBUS-BUXTON2] buxton_set_value_sync: %d, %s", ret, STT_ENGINE_DB_CUSTOM);
+
+       buxton_value_free(bux_val);
+       buxton_free_layer(bux_layer);
+       buxton_close(bux_cli);
+
+       bux_cli = NULL;
+       bux_layer = NULL;
+       bux_val = NULL;
+
+       return STT_ERROR_NONE;
+}
+
 int stt_set_engine(stt_h stt, const char* engine_id)
 {
        stt_client_s* client = NULL;
@@ -529,10 +589,20 @@ int stt_set_engine(stt_h stt, const char* engine_id)
 
        if (NULL != client->current_engine_id) {
                free(client->current_engine_id);
+               client->current_engine_id = NULL;
        }
 
+       SLOG(LOG_DEBUG, TAG_STTC, "===== engined_id(%s)", engine_id);
+
        client->current_engine_id = strdup(engine_id);
 
+       /* Set vconfkey */
+       int ret = __stt_set_buxtonkey(engine_id);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] set buxtonkey Failed!!!");
+               return ret;
+       }
+
        SLOG(LOG_DEBUG, TAG_STTC, "=====");
        SLOG(LOG_DEBUG, TAG_STTC, " ");
 
@@ -565,6 +635,10 @@ int stt_set_credential(stt_h stt, const char* credential)
                return STT_ERROR_INVALID_STATE;
        }
 
+       if (NULL != client->credential) {
+               free(client->credential);
+               client->credential = NULL;
+       }
        client->credential = strdup(credential);
 
        SLOG(LOG_DEBUG, TAG_STTC, "=====");
@@ -671,6 +745,11 @@ int stt_get_private_data(stt_h stt, const char* key, char** data)
                }
        }
 
+       if (0 == strncmp(*data, "NULL", strlen(*data))) {
+               free(*data);
+               *data = NULL;
+       }
+
        SLOG(LOG_DEBUG, TAG_STTC, "=====");
        SLOG(LOG_DEBUG, TAG_STTC, "");
 
@@ -688,7 +767,7 @@ static Eina_Bool __stt_connect_daemon(void *data)
 
        /* Send hello */
        int ret = -1;
-       ret = stt_dbus_request_hello();
+       ret = stt_dbus_request_hello(client->uid);
 
        if (0 != ret) {
                if (STT_ERROR_INVALID_STATE == ret) {
@@ -699,7 +778,7 @@ static Eina_Bool __stt_connect_daemon(void *data)
        }
 
        g_connect_timer = NULL;
-       SLOG(LOG_DEBUG, TAG_STTC, "===== Connect daemon");
+       SLOG(LOG_DEBUG, TAG_STTC, "===== Connect stt-service");
 
        /* request initialization */
        bool silence_supported = false;
@@ -719,17 +798,20 @@ static Eina_Bool __stt_connect_daemon(void *data)
                SLOG(LOG_ERROR, TAG_STTC, "[WARNING] Fail to connection. Retry to connect");
                return EINA_TRUE;
        } else {
-               /* success to connect stt-daemon */
+               /* success to connect stt-service */
                client->silence_supported = silence_supported;
                client->credential_needed = credential_needed;
                SLOG(LOG_DEBUG, TAG_STTC, "Supported options : silence(%s), credential(%s)", silence_supported ? "support" : "no support", credential_needed ? "need" : "no need");
        }
 
+#ifdef __UNUSED_CODES__
        if (NULL != client->current_engine_id) {
                ret = -1;
                int count = 0;
                silence_supported = false;
                credential_needed = false;
+               SLOG(LOG_DEBUG, TAG_STTC, "[WARNING] current_engine_id(%s)", client->current_engine_id);
+
                while (0 != ret) {
                        ret = stt_dbus_request_set_current_engine(client->uid, client->current_engine_id, &silence_supported, &credential_needed);
                        if (0 != ret) {
@@ -754,7 +836,7 @@ static Eina_Bool __stt_connect_daemon(void *data)
                        }
                }
        }
-
+#endif
        SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] uid(%d)", client->uid);
 
        client->before_state = client->current_state;
@@ -938,6 +1020,7 @@ int stt_foreach_supported_languages(stt_h stt, stt_supported_language_cb callbac
 
        if (NULL != current_engine_id) {
                free(current_engine_id);
+               current_engine_id = NULL;
        }
 
        client->supported_lang_cb = NULL;
@@ -1664,6 +1747,15 @@ int __stt_cb_error(int uid, int reason, char* err_msg)
                SLOG(LOG_WARN, TAG_STTC, "[WARNING] Error callback is null");
        }
 
+       if (STT_ERROR_SERVICE_RESET == reason) {
+               SLOG(LOG_WARN, TAG_STTC, "[WARNING] Service reset");
+
+               client->current_state = STT_STATE_CREATED;
+               if (0 != stt_prepare(client->stt)) {
+                       SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to prepare");
+               }
+       }
+
        return 0;
 }