2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
20 #include "tts_config_parser.h"
24 #define TTS_TAG_ENGINE_BASE_TAG "tts-engine"
25 #define TTS_TAG_ENGINE_NAME "name"
26 #define TTS_TAG_ENGINE_ID "id"
27 #define TTS_TAG_ENGINE_SETTING "setting"
28 #define TTS_TAG_ENGINE_VOICE_SET "voices"
29 #define TTS_TAG_ENGINE_VOICE "voice"
30 #define TTS_TAG_ENGINE_VOICE_TYPE "type"
31 #define TTS_TAG_ENGINE_PITCH_SUPPORT "pitch-support"
32 #define TTS_TAG_ENGINE_TEXT_SIZE "text-size"
34 #define TTS_TAG_CONFIG_BASE_TAG "tts-config"
35 #define TTS_TAG_CONFIG_ENGINE_ID "engine"
36 #define TTS_TAG_CONFIG_ENGINE_SETTING "engine-setting"
37 #define TTS_TAG_CONFIG_AUTO_VOICE "auto"
38 #define TTS_TAG_CONFIG_VOICE_TYPE "voice-type"
39 #define TTS_TAG_CONFIG_LANGUAGE "language"
40 #define TTS_TAG_CONFIG_SPEECH_RATE "speech-rate"
41 #define TTS_TAG_CONFIG_PITCH "pitch"
42 #define TTS_TAG_CONFIG_BACKGROUND_VOLUME_RATIO "background-volume-ratio"
43 #define TTS_TAG_VOICE_TYPE_FEMALE "female"
44 #define TTS_TAG_VOICE_TYPE_MALE "male"
45 #define TTS_TAG_VOICE_TYPE_CHILD "child"
47 #define TTS_MAX_TEXT_SIZE 2000
49 static xmlDocPtr g_config_doc = NULL;
50 char g_engine_id[128] = {0,};
51 char g_setting[128] = {0,};
52 char g_language[128] = {0,};
54 static tts_config_s* g_config_info = NULL;
56 static pthread_mutex_t g_config_info_mutex = PTHREAD_MUTEX_INITIALIZER;
58 int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info)
60 if (NULL == path || NULL == engine_info) {
61 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
65 // Avoid to parse non-xml file
66 char* file_ext = strrchr(path, '.');
67 if (NULL == ++file_ext || (file_ext && 0 != strncmp("xml", file_ext, strlen(file_ext)))) {
68 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] File extention is not XML type, file_ext(%s)", (file_ext) ? file_ext : "NULL");
72 bool isTextsize = false;
74 xmlNodePtr cur = NULL;
78 if (0 == access(path, F_OK)) {
79 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Success to access to %s", path);
80 doc = xmlParseFile(path);
82 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse xml file");
86 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to access to %s", path);
90 cur = xmlDocGetRootElement(doc);
92 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document. doc path(%s, %p)", path, doc);
98 if (xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_BASE_TAG)) {
99 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] The wrong type, root node is NOT 'tts-engine'. doc path(%s, %p)", path, doc);
105 cur = cur->xmlChildrenNode;
107 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document. doc path(%s, %p)", path, doc);
113 /* alloc engine info */
114 tts_engine_info_s* temp;
115 temp = (tts_engine_info_s*)calloc(1, sizeof(tts_engine_info_s));
117 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Out of memory. doc path(%s, %p)", path, doc);
126 temp->setting = NULL;
127 temp->pitch_support = false;
130 while (cur != NULL) {
131 if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_NAME)) {
132 key = xmlNodeGetContent(cur);
134 if (NULL != temp->name) {
138 temp->name = strdup((char*)key);
142 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_NAME);
144 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_ID)) {
145 key = xmlNodeGetContent(cur);
147 if (NULL != temp->uuid) {
151 temp->uuid = strdup((char*)key);
155 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_ID);
157 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_SETTING)) {
158 key = xmlNodeGetContent(cur);
160 if (NULL != temp->setting) {
162 temp->setting = NULL;
164 temp->setting = strdup((char*)key);
168 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_SETTING);
170 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_VOICE_SET)) {
171 xmlNodePtr voice_node = NULL;
172 voice_node = cur->xmlChildrenNode;
174 while (NULL != voice_node) {
175 if (0 == xmlStrcmp(voice_node->name, (const xmlChar *)TTS_TAG_ENGINE_VOICE)) {
176 tts_config_voice_s* temp_voice = (tts_config_voice_s*)calloc(1, sizeof(tts_config_voice_s));
177 if (NULL == temp_voice) {
178 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Out of memory");
182 attr = xmlGetProp(voice_node, (const xmlChar*)TTS_TAG_ENGINE_VOICE_TYPE);
184 if (0 == xmlStrcmp(attr, (const xmlChar *)TTS_TAG_VOICE_TYPE_FEMALE)) {
185 temp_voice->type = (int)TTS_CONFIG_VOICE_TYPE_FEMALE;
186 } else if (0 == xmlStrcmp(attr, (const xmlChar *)TTS_TAG_VOICE_TYPE_MALE)) {
187 temp_voice->type = (int)TTS_CONFIG_VOICE_TYPE_MALE;
188 } else if (0 == xmlStrcmp(attr, (const xmlChar *)TTS_TAG_VOICE_TYPE_CHILD)) {
189 temp_voice->type = (int)TTS_CONFIG_VOICE_TYPE_CHILD;
191 temp_voice->type = (int)TTS_CONFIG_VOICE_TYPE_USER_DEFINED;
196 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_VOICE_TYPE);
202 key = xmlNodeGetContent(voice_node);
204 if (NULL != temp_voice->language) {
205 free(temp_voice->language);
206 temp_voice->language = NULL;
208 temp_voice->language = strdup((char*)key);
211 temp->voices = g_slist_append(temp->voices, temp_voice);
213 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_VOICE);
214 if (NULL != temp_voice) {
220 voice_node = voice_node->next;
222 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_PITCH_SUPPORT)) {
223 key = xmlNodeGetContent(cur);
225 if (0 == xmlStrcmp(key, (const xmlChar *)"true")) {
226 temp->pitch_support = true;
231 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_PITCH_SUPPORT);
233 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_TEXT_SIZE)) {
235 key = xmlNodeGetContent(cur);
237 temp->text_size = atoi((char*)key);
240 SLOG(LOG_INFO, TAG_TTSCONFIG, "[INFO] text size is unlimited.");
247 if (false == isTextsize) {
248 temp->text_size = TTS_MAX_TEXT_SIZE;
252 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] doc path(%s, %p)", path, doc);
257 if (NULL == temp->uuid) {
259 SECURE_SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Invalid engine : %s", path);
260 tts_parser_free_engine_info(temp);
269 int tts_parser_free_engine_info(tts_engine_info_s* engine_info)
271 if (NULL == engine_info) {
272 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
276 if (NULL != engine_info->name) {
277 free(engine_info->name);
278 engine_info->name = NULL;
280 if (NULL != engine_info->uuid) {
281 free(engine_info->uuid);
282 engine_info->uuid = NULL;
284 if (NULL != engine_info->setting) {
285 free(engine_info->setting);
286 engine_info->setting = NULL;
289 tts_config_voice_s *temp_voice;
290 temp_voice = g_slist_nth_data(engine_info->voices, 0);
292 while (NULL != temp_voice) {
293 if (NULL != temp_voice) {
294 if (NULL != temp_voice->language) {
295 free(temp_voice->language);
296 temp_voice->language = NULL;
298 engine_info->voices = g_slist_remove(engine_info->voices, temp_voice);
303 temp_voice = g_slist_nth_data(engine_info->voices, 0);
306 if (NULL != engine_info) {
314 int tts_parser_print_engine_info(tts_engine_info_s* engine_info)
316 if (NULL == engine_info) {
317 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
321 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "== get engine info ==");
322 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " name : %s", engine_info->name);
323 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " id : %s", engine_info->uuid);
324 if (NULL != engine_info->setting)
325 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " setting : %s", engine_info->setting);
327 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " voices");
329 tts_config_voice_s *temp_voice;
331 if (g_slist_length(engine_info->voices) > 0) {
332 /* Get a first item */
333 iter = g_slist_nth(engine_info->voices, 0);
336 while (NULL != iter) {
337 /*Get handle data from list*/
338 temp_voice = iter->data;
340 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " [%dth] type(%d) lang(%s)",
341 i, temp_voice->type, temp_voice->language);
344 iter = g_slist_next(iter);
348 SLOG(LOG_ERROR, TAG_TTSCONFIG, " Voice is NONE");
351 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "@@@");
356 int tts_parser_load_config(void)
358 xmlDocPtr doc = NULL;
359 xmlNodePtr cur = NULL;
361 bool is_default_open = false;
363 /* For Thread safety */
366 if (0 != access(TTS_CONFIG, F_OK)) {
367 doc = xmlParseFile(TTS_DEFAULT_CONFIG);
369 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse file error : %s", TTS_DEFAULT_CONFIG);
373 is_default_open = true;
377 while (NULL == doc) {
378 doc = xmlParseFile(TTS_CONFIG);
385 if (TTS_RETRY_COUNT == retry_count) {
386 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse file error : %s", TTS_CONFIG);
387 doc = xmlParseFile(TTS_DEFAULT_CONFIG);
389 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse file error : %s", TTS_DEFAULT_CONFIG);
393 is_default_open = true;
399 cur = xmlDocGetRootElement(doc);
401 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document(%p)", doc);
408 if (xmlStrcmp(cur->name, (const xmlChar *) TTS_TAG_CONFIG_BASE_TAG)) {
409 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] The wrong type, root node is NOT %s. doc(%p)", TTS_TAG_CONFIG_BASE_TAG, doc);
416 cur = cur->xmlChildrenNode;
418 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document(%p)", doc);
425 /* alloc engine info */
427 temp = (tts_config_s*)calloc(1, sizeof(tts_config_s));
429 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Out of memory(%p)", doc);
436 memset(g_engine_id, '\0', sizeof(g_engine_id));
437 memset(g_setting, '\0', sizeof(g_setting));
438 memset(g_language, '\0', sizeof(g_language));
440 temp->engine_id = g_engine_id;
441 temp->setting = g_setting;
442 temp->language = g_language;
444 while (cur != NULL) {
445 if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_ENGINE_ID)) {
446 if (is_default_open) {
447 char* engine_id = vconf_get_str(TTS_ENGINE_DB_DEFAULT);
449 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Set default engine id(%s)", engine_id);
450 strncpy(temp->engine_id, engine_id, sizeof(engine_id) - 1);
451 xmlNodeSetContent(cur, (const xmlChar *)engine_id);
461 key = xmlNodeGetContent(cur);
463 strncpy(temp->engine_id, (char*)key, sizeof(g_engine_id) - 1);
467 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] engine id is NULL");
469 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_ENGINE_SETTING)) {
470 key = xmlNodeGetContent(cur);
472 strncpy(temp->setting, (char*)key, sizeof(g_setting) - 1);
476 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] setting path is NULL");
478 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_AUTO_VOICE)) {
479 key = xmlNodeGetContent(cur);
481 if (0 == xmlStrcmp(key, (const xmlChar *)"on")) {
482 temp->auto_voice = true;
483 } else if (0 == xmlStrcmp(key, (const xmlChar *)"off")) {
484 temp->auto_voice = false;
486 SLOG(LOG_ERROR, TAG_TTSCONFIG, "Auto voice is wrong");
487 temp->auto_voice = true;
493 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] voice type is NULL");
495 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_VOICE_TYPE)) {
496 key = xmlNodeGetContent(cur);
498 if (0 == xmlStrcmp(key, (const xmlChar *)TTS_TAG_VOICE_TYPE_MALE)) {
499 temp->type = (int)TTS_CONFIG_VOICE_TYPE_MALE;
500 } else if (0 == xmlStrcmp(key, (const xmlChar *)TTS_TAG_VOICE_TYPE_FEMALE)) {
501 temp->type = (int)TTS_CONFIG_VOICE_TYPE_FEMALE;
502 } else if (0 == xmlStrcmp(key, (const xmlChar *)TTS_TAG_VOICE_TYPE_CHILD)) {
503 temp->type = (int)TTS_CONFIG_VOICE_TYPE_CHILD;
505 SLOG(LOG_WARN, TAG_TTSCONFIG, "Voice type is user defined");
506 temp->type = (int)TTS_CONFIG_VOICE_TYPE_USER_DEFINED;
512 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] voice type is NULL");
514 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_LANGUAGE)) {
515 key = xmlNodeGetContent(cur);
517 strncpy(temp->language, (char*)key, sizeof(g_language) - 1);
521 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] engine uuid is NULL");
524 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_SPEECH_RATE)) {
525 key = xmlNodeGetContent(cur);
527 temp->speech_rate = atoi((char*)key);
530 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] speech rate is NULL");
532 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_PITCH)) {
533 key = xmlNodeGetContent(cur);
535 temp->pitch = atoi((char*)key);
539 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Pitch is NULL");
541 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_CONFIG_BACKGROUND_VOLUME_RATIO)) {
542 key = xmlNodeGetContent(cur);
544 temp->bg_volume_ratio = atof((char*)key);
548 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Background volume ratio is NULL");
557 g_config_info = temp;
560 if (true == is_default_open) {
564 ret = xmlSaveFile(TTS_CONFIG, g_config_doc);
570 if (TTS_RETRY_COUNT == retry_count) {
571 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Save result : %d", ret);
577 if (0 > chmod(TTS_CONFIG, 0600)) {
578 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to change file mode : %d", ret);
581 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Default config is changed : pid(%d)", getpid());
587 int tts_parser_unload_config(void)
589 if (NULL != g_config_doc) {
590 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Free g_config_doc(%p)", g_config_doc);
591 xmlFreeDoc(g_config_doc);
594 pthread_mutex_lock(&g_config_info_mutex);
595 if (NULL != g_config_info) {
596 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Free config_info(%p)", g_config_info);
598 g_config_info = NULL;
600 pthread_mutex_unlock(&g_config_info_mutex);
606 int tts_parser_set_config_info(tts_config_s* config_info)
608 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Set config info");
609 if (NULL == config_info) {
610 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] config_info is NULL");
613 pthread_mutex_lock(&g_config_info_mutex);
614 if (NULL == g_config_info) {
615 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] g_config_info is NULL");
616 pthread_mutex_unlock(&g_config_info_mutex);
619 memcpy(g_config_info, config_info, sizeof(tts_config_s));
620 pthread_mutex_unlock(&g_config_info_mutex);
624 int tts_parser_get_config_info(tts_config_s* config_info)
626 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Get config info");
627 if (NULL == config_info) {
628 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] config_info is NULL");
631 pthread_mutex_lock(&g_config_info_mutex);
632 if (NULL == g_config_info) {
633 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] g_config_info is NULL");
634 pthread_mutex_unlock(&g_config_info_mutex);
637 memcpy(config_info, g_config_info, sizeof(tts_config_s));
638 pthread_mutex_unlock(&g_config_info_mutex);
642 int tts_parser_copy_xml(const char* original, const char* destination)
644 if (NULL == original || NULL == destination) {
645 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
649 xmlDocPtr doc = NULL;
650 if (0 == access(original, F_OK)) {
651 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Success to access to %s", original);
652 doc = xmlParseFile(original);
654 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse file error : %s", original);
658 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to access to %s", original);
662 int ret = xmlSaveFile(destination, doc);
664 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Save result : %d", ret);
667 pFile = fopen(destination, "r");
670 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to open file %s", destination);
675 SLOG(LOG_INFO, TAG_TTSCONFIG, "[DEBUG] Success to fsync %s", destination);
677 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Success to save %s", destination);
681 if (0 > chmod(destination, 0600)) {
682 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to change file mode : %d", ret);
686 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] doc(%p)", doc);
691 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[SUCCESS] Copying xml");
696 static int __set_value_into_configuration(const char* key, const char* value)
698 if(NULL == key || NULL == value) {
699 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Parameter is NULL");
702 xmlNodePtr cur = NULL;
703 cur = xmlDocGetRootElement(g_config_doc);
705 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document");
709 if (xmlStrcmp(cur->name, (const xmlChar *) TTS_TAG_CONFIG_BASE_TAG)) {
710 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] The wrong type, root node is NOT %s", TTS_TAG_CONFIG_BASE_TAG);
714 cur = cur->xmlChildrenNode;
716 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document");
720 while (cur != NULL) {
721 if (0 == xmlStrcmp(cur->name, (const xmlChar *)key)) {
722 xmlNodeSetContent(cur, (const xmlChar *)value);
724 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Set key(%s) : value(%s)", key, value);
734 static void __save_configuration(xmlDocPtr config_doc)
736 int ret = xmlSaveFile(TTS_CONFIG, config_doc);
738 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Save result : file(%s), ret(%d)", TTS_CONFIG, ret);
741 pFile = fopen(TTS_CONFIG, "r");
744 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to open file %s", TTS_CONFIG);
749 SLOG(LOG_INFO, TAG_TTSCONFIG, "[DEBUG] Success to fsync %s", TTS_CONFIG);
751 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Success to save %s", TTS_CONFIG);
755 static char* __convert_voice_type_into_char(tts_config_voice_type_e voice_type)
757 switch (voice_type) {
758 case TTS_CONFIG_VOICE_TYPE_MALE:
759 return TTS_TAG_VOICE_TYPE_MALE;
760 case TTS_CONFIG_VOICE_TYPE_FEMALE:
761 return TTS_TAG_VOICE_TYPE_FEMALE;
762 case TTS_CONFIG_VOICE_TYPE_CHILD:
763 return TTS_TAG_VOICE_TYPE_CHILD;
765 return TTS_TAG_VOICE_TYPE_FEMALE;
769 int tts_parser_set_engine(const char* engine_id, const char* setting, const char* language, int type)
771 if (NULL == engine_id) {
772 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
776 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_ENGINE_ID, engine_id)) {
780 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_LANGUAGE, language)) {
784 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_ENGINE_SETTING, setting)) {
788 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_VOICE_TYPE, __convert_voice_type_into_char(type))) {
792 __save_configuration(g_config_doc);
797 int tts_parser_set_voice(const char* language, int type)
799 if (NULL == language) {
800 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
804 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_LANGUAGE, language)) {
808 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_VOICE_TYPE, __convert_voice_type_into_char(type))) {
812 __save_configuration(g_config_doc);
817 int tts_parser_set_auto_voice(bool value)
819 char* temp = value ? "on" : "off";
820 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_AUTO_VOICE, temp)) {
824 __save_configuration(g_config_doc);
829 int tts_parser_set_speech_rate(int value)
832 memset(temp, '\0', 10);
833 snprintf(temp, 10, "%d", value);
835 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_SPEECH_RATE, temp)) {
839 __save_configuration(g_config_doc);
844 int tts_parser_set_pitch(int value)
847 memset(temp, '\0', 10);
848 snprintf(temp, 10, "%d", value);
850 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_PITCH, temp)) {
854 __save_configuration(g_config_doc);
859 int tts_parser_set_bg_volume_ratio(double value)
862 memset(temp, '\0', 10);
863 snprintf(temp, 10, "%lf", value);
865 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_BACKGROUND_VOLUME_RATIO, temp)) {
869 __save_configuration(g_config_doc);
874 int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voice, char** language, int* voice_type,
875 int* speech_rate, int* pitch, double* bg_volume_ratio)
877 if (NULL == engine || NULL == setting || NULL == language || NULL == voice_type || NULL == speech_rate) {
878 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
882 xmlDocPtr doc = NULL;
883 xmlNodePtr cur_new = NULL;
884 xmlNodePtr cur_old = NULL;
890 while (NULL == doc) {
891 if (0 == access(TTS_CONFIG, F_OK)) {
892 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Success to access to %s", TTS_CONFIG);
893 doc = xmlParseFile(TTS_CONFIG);
901 if (TTS_RETRY_COUNT == retry_count) {
902 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse file error : %s", TTS_CONFIG);
907 cur_new = xmlDocGetRootElement(doc);
908 cur_old = xmlDocGetRootElement(g_config_doc);
909 if (cur_new == NULL || cur_old == NULL) {
910 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document(%p)", doc);
916 if (xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_BASE_TAG) ||
917 xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_BASE_TAG)) {
918 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] The wrong type, root node is NOT %s. doc(%p)", TTS_TAG_CONFIG_BASE_TAG, doc);
924 cur_new = cur_new->xmlChildrenNode;
925 cur_old = cur_old->xmlChildrenNode;
926 if (cur_new == NULL || cur_old == NULL) {
927 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document(%p)", doc);
933 while (cur_new != NULL && cur_old != NULL) {
934 if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_ENGINE_ID)) {
935 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_ENGINE_ID)) {
936 key_old = xmlNodeGetContent(cur_old);
937 if (NULL != key_old) {
938 key_new = xmlNodeGetContent(cur_new);
939 if (NULL != key_new) {
940 if (0 != xmlStrcmp(key_old, key_new)) {
941 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old engine id(%s), New engine(%s)",
942 (char*)key_old, (char*)key_new);
943 if (NULL != *engine) {
947 *engine = strdup((char*)key_new);
956 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
958 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_ENGINE_SETTING)) {
959 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_ENGINE_SETTING)) {
960 key_old = xmlNodeGetContent(cur_old);
961 if (NULL != key_old) {
962 key_new = xmlNodeGetContent(cur_new);
963 if (NULL != key_new) {
964 if (0 != xmlStrcmp(key_old, key_new)) {
965 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old engine setting(%s), New engine setting(%s)",
966 (char*)key_old, (char*)key_new);
967 if (NULL != *setting) {
971 *setting = strdup((char*)key_new);
980 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
982 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_AUTO_VOICE)) {
983 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_AUTO_VOICE)) {
984 key_old = xmlNodeGetContent(cur_old);
985 if (NULL != key_old) {
986 key_new = xmlNodeGetContent(cur_new);
987 if (NULL != key_new) {
988 if (0 != xmlStrcmp(key_old, key_new)) {
989 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old auto voice (%s), New auto voice(%s)",
990 (char*)key_old, (char*)key_new);
991 if (0 == xmlStrcmp((const xmlChar*)"on", key_new)) {
1004 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1006 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_LANGUAGE)) {
1007 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_LANGUAGE)) {
1008 key_old = xmlNodeGetContent(cur_old);
1009 if (NULL != key_old) {
1010 key_new = xmlNodeGetContent(cur_new);
1011 if (NULL != key_new) {
1012 if (0 != xmlStrcmp(key_old, key_new)) {
1013 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old language(%s), New language(%s)",
1014 (char*)key_old, (char*)key_new);
1015 if (NULL != *language) {
1019 *language = strdup((char*)key_new);
1028 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1030 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_VOICE_TYPE)) {
1031 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_VOICE_TYPE)) {
1032 key_old = xmlNodeGetContent(cur_old);
1033 if (NULL != key_old) {
1034 key_new = xmlNodeGetContent(cur_new);
1035 if (NULL != key_new) {
1036 if (0 != xmlStrcmp(key_old, key_new)) {
1037 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old voice type(%s), New voice type(%s)",
1038 (char*)key_old, (char*)key_new);
1039 if (0 == xmlStrcmp(key_new, (const xmlChar *)TTS_TAG_VOICE_TYPE_FEMALE)) {
1040 *voice_type = (int)TTS_CONFIG_VOICE_TYPE_FEMALE;
1041 } else if (0 == xmlStrcmp(key_new, (const xmlChar *)TTS_TAG_VOICE_TYPE_MALE)) {
1042 *voice_type = (int)TTS_CONFIG_VOICE_TYPE_MALE;
1043 } else if (0 == xmlStrcmp(key_new, (const xmlChar *)TTS_TAG_VOICE_TYPE_CHILD)) {
1044 *voice_type = (int)TTS_CONFIG_VOICE_TYPE_CHILD;
1046 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] New voice type is not valid");
1056 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1058 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_SPEECH_RATE)) {
1059 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_SPEECH_RATE)) {
1060 key_old = xmlNodeGetContent(cur_old);
1061 if (NULL != key_old) {
1062 key_new = xmlNodeGetContent(cur_new);
1063 if (NULL != key_new) {
1064 if (0 != xmlStrcmp(key_old, key_new)) {
1065 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old speech rate(%s), New speech rate(%s)",
1066 (char*)key_old, (char*)key_new);
1067 *speech_rate = atoi((char*)key_new);
1076 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1078 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_PITCH)) {
1079 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_PITCH)) {
1080 key_old = xmlNodeGetContent(cur_old);
1081 if (NULL != key_old) {
1082 key_new = xmlNodeGetContent(cur_new);
1083 if (NULL != key_new) {
1084 if (0 != xmlStrcmp(key_old, key_new)) {
1085 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old pitch(%s), New pitch(%s)",
1086 (char*)key_old, (char*)key_new);
1087 *pitch = atoi((char*)key_new);
1096 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1098 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_BACKGROUND_VOLUME_RATIO)) {
1099 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_BACKGROUND_VOLUME_RATIO)) {
1100 key_old = xmlNodeGetContent(cur_old);
1101 if (NULL != key_old) {
1102 key_new = xmlNodeGetContent(cur_new);
1103 if (NULL != key_new) {
1104 if (0 != xmlStrcmp(key_old, key_new)) {
1105 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old bg volume ratio(%s), New bg volume ratio(%s)",
1106 (char*)key_old, (char*)key_new);
1107 *bg_volume_ratio = atof((char*)key_new);
1116 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1122 cur_new = cur_new->next;
1123 cur_old = cur_old->next;
1126 if (NULL != g_config_doc) {
1127 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Free g_config_doc(%p)", g_config_doc);
1128 xmlFreeDoc(g_config_doc);
1129 g_config_doc = NULL;
1136 int tts_parser_reset()
1138 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Reset g_config_doc as %s", TTS_DEFAULT_CONFIG);
1140 if (NULL != g_config_doc) {
1141 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Free g_config_doc(%p)", g_config_doc);
1142 xmlFreeDoc(g_config_doc);
1143 g_config_doc = NULL;
1146 g_config_doc = xmlParseFile(TTS_DEFAULT_CONFIG);
1147 if (NULL == g_config_doc) {
1148 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse %s", TTS_DEFAULT_CONFIG);
1152 char* engine_id = vconf_get_str(TTS_ENGINE_DB_DEFAULT);
1154 int ret = __set_value_into_configuration(TTS_TAG_CONFIG_ENGINE_ID, engine_id);
1155 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Set engine ID into xml. engine_id(%s), ret(%d/%s)", engine_id, ret, get_error_message(ret));
1158 __save_configuration(g_config_doc);