Merge "Fix to check whether it is possible to access to files" into tizen
[platform/core/uifw/tts.git] / common / tts_config_parser.c
index 50bf5f2..443e2dd 100644 (file)
@@ -29,6 +29,7 @@
 #define TTS_TAG_ENGINE_VOICE           "voice"
 #define TTS_TAG_ENGINE_VOICE_TYPE      "type"
 #define TTS_TAG_ENGINE_PITCH_SUPPORT   "pitch-support"
+#define TTS_TAG_ENGINE_TEXT_SIZE       "text-size"
 
 #define TTS_TAG_CONFIG_BASE_TAG                "tts-config"
 #define TTS_TAG_CONFIG_ENGINE_ID       "engine"
@@ -42,6 +43,7 @@
 #define TTS_TAG_VOICE_TYPE_MALE                "male"
 #define TTS_TAG_VOICE_TYPE_CHILD       "child"
 
+#define TTS_MAX_TEXT_SIZE      2000
 
 extern char* tts_tag();
 
@@ -57,14 +59,21 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                return -1;
        }
 
+       bool isTextsize = false;
        xmlDocPtr doc = NULL;
        xmlNodePtr cur = NULL;
        xmlChar *key = NULL;
        xmlChar *attr = NULL;
 
-       doc = xmlParseFile(path);
-       if (doc == NULL) {
-               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to parse xml file");
+       if (0 == access(path, F_OK)) {
+               SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Success to access to %s", path);
+               doc = xmlParseFile(path);
+               if (doc == NULL) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to parse xml file");
+                       return -1;
+               }
+       } else {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to access to %s", path);
                return -1;
        }
 
@@ -106,6 +115,7 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
        temp->voices = NULL;
        temp->setting = NULL;
        temp->pitch_support = false;
+       temp->text_size = 0;
 
        while (cur != NULL) {
                if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_NAME)) {
@@ -210,14 +220,28 @@ int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info
                        } else {
                                SLOG(LOG_ERROR, tts_tag(), "[ERROR] <%s> has no content", TTS_TAG_ENGINE_PITCH_SUPPORT);
                        }
+               } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_TEXT_SIZE)) {
+                       isTextsize = true;
+                       key = xmlNodeGetContent(cur);
+                       if (NULL != key) {
+                               temp->text_size = atoi((char*)key);
+                               xmlFree(key);
+                       } else {
+                               SLOG(LOG_INFO, tts_tag(), "[INFO] text size is unlimited.");
+                               temp->text_size = 0;
+                       }
                }
                cur = cur->next;
        }
 
+       if (false == isTextsize) {
+               temp->text_size = TTS_MAX_TEXT_SIZE;
+       }
+
        xmlFreeDoc(doc);
        doc = NULL;
 
-       if (NULL == temp->name || NULL == temp->uuid) {
+       if (NULL == temp->uuid) {
                /* Invalid engine */
                SECURE_SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid engine : %s", path);
                tts_parser_free_engine_info(temp);
@@ -311,7 +335,7 @@ int tts_parser_print_engine_info(tts_engine_info_s* engine_info)
                SLOG(LOG_ERROR, tts_tag(), "  Voice is NONE");
        }
 
-       SLOG(LOG_DEBUG, tts_tag(), "=====================");
+       SLOG(LOG_DEBUG, tts_tag(), "@@@");
 
        return 0;
 }
@@ -506,7 +530,7 @@ int tts_parser_load_config(tts_config_s** config_info)
                int ret = -1;
                do {
                        ret = xmlSaveFile(TTS_CONFIG, g_config_doc);
-                       if (0 == ret)
+                       if (0 < ret)
                                break;
                        retry_count++;
                        usleep(10000);
@@ -518,7 +542,7 @@ int tts_parser_load_config(tts_config_s** config_info)
                } while (0 != ret);
 
                /* Set mode */
-               if (0 > chmod(TTS_CONFIG, 0666)) {
+               if (0 > chmod(TTS_CONFIG, 0600)) {
                        SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to change file mode : %d", ret);
                }
 
@@ -557,19 +581,27 @@ int tts_parser_copy_xml(const char* original, const char* destination)
        }
 
        xmlDocPtr doc = NULL;
-       doc = xmlParseFile(original);
-       if (doc == NULL) {
-               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to parse file error : %s", original);
+       if (0 == access(original, F_OK)) {
+               SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Success to access to %s", original);
+               doc = xmlParseFile(original);
+               if (doc == NULL) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to parse file error : %s", original);
+                       return -1;
+               }
+       } else {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to access to %s", original);
                return -1;
        }
 
        int ret = xmlSaveFile(destination, doc);
        if (0 > ret) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Save result : %d", ret);
+       } else {
+               SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Success to save %s", destination);
        }
 
        /* Set mode */
-       if (0 > chmod(destination, 0666)) {
+       if (0 > chmod(destination, 0600)) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to change file mode : %d", ret);
        }
 
@@ -633,6 +665,8 @@ int tts_parser_set_engine(const char* engine_id, const char* setting, const char
        int ret = xmlSaveFile(TTS_CONFIG, g_config_doc);
        if (0 > ret) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Save result : %d", ret);
+       } else {
+               SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Success to save %s", TTS_CONFIG);
        }
 
        return 0;
@@ -666,7 +700,7 @@ int tts_parser_set_voice(const char* language, int type)
        while (cur != NULL) {
                if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_LANGUAGE)) {
                        xmlNodeSetContent(cur, (const xmlChar *)language);
-               } 
+               }
 
                if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_VOICE_TYPE)) {
                        switch (type) {
@@ -686,6 +720,8 @@ int tts_parser_set_voice(const char* language, int type)
        int ret = xmlSaveFile(TTS_CONFIG, g_config_doc);
        if (0 > ret) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Save result : %d", ret);
+       } else {
+               SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Success to save %s", TTS_CONFIG);
        }
 
        return 0;
@@ -729,6 +765,8 @@ int tts_parser_set_auto_voice(bool value)
        int ret = xmlSaveFile(TTS_CONFIG, g_config_doc);
        if (0 > ret) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Save result : %d", ret);
+       } else {
+               SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Success to save %s", TTS_CONFIG);
        }
 
        return 0;
@@ -772,6 +810,8 @@ int tts_parser_set_speech_rate(int value)
        int ret = xmlSaveFile(TTS_CONFIG, g_config_doc);
        if (0 > ret) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Save result : %d", ret);
+       } else {
+               SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Success to save %s", TTS_CONFIG);
        }
 
        return 0;
@@ -804,7 +844,7 @@ int tts_parser_set_pitch(int value)
                        snprintf(temp, 10, "%d", value);
                        xmlNodeSetContent(cur, (const xmlChar *)temp);
                        break;
-               } 
+               }
 
                cur = cur->next;
        }
@@ -812,6 +852,8 @@ int tts_parser_set_pitch(int value)
        int ret = xmlSaveFile(TTS_CONFIG, g_config_doc);
        if (0 > ret) {
                SLOG(LOG_ERROR, tts_tag(), "[ERROR] Save result : %d", ret);
+       } else {
+               SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Success to save %s", TTS_CONFIG);
        }
 
        return 0;
@@ -834,9 +876,12 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
 
        int retry_count = 0;
        while (NULL == doc) {
-               doc = xmlParseFile(TTS_CONFIG);
-               if (NULL != doc) {
-                       break;
+               if (0 == access(TTS_CONFIG, F_OK)) {
+                       SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Success to access to %s", TTS_CONFIG);
+                       doc = xmlParseFile(TTS_CONFIG);
+                       if (NULL != doc) {
+                               break;
+                       }
                }
                retry_count++;
                usleep(10000);
@@ -1054,3 +1099,26 @@ int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voic
 
        return 0;
 }
+
+int tts_parser_reset()
+{
+       SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] Reset g_config_doc as %s", TTS_DEFAULT_CONFIG);
+
+       if (NULL != g_config_doc) {
+               xmlFreeDoc(g_config_doc);
+               g_config_doc = NULL;
+       }
+
+       g_config_doc = xmlParseFile(TTS_DEFAULT_CONFIG);
+       if (NULL == g_config_doc) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to parse %s", TTS_DEFAULT_CONFIG);
+               return -1;
+       }
+
+       int ret = xmlSaveFile(TTS_CONFIG, g_config_doc);
+       if (0 > ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to save %s", TTS_CONFIG);
+       }
+
+       return 0;
+}