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
48 #define VOLUME_BASE_VALUE 100.0
50 static xmlDocPtr g_config_doc = NULL;
51 char g_engine_id[128] = {0,};
52 char g_setting[128] = {0,};
53 char g_language[128] = {0,};
55 static tts_config_s* g_config_info = NULL;
57 static pthread_mutex_t g_config_info_mutex = PTHREAD_MUTEX_INITIALIZER;
59 int tts_parser_get_engine_info(const char* path, tts_engine_info_s** engine_info)
61 if (NULL == path || NULL == engine_info) {
62 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
66 // Avoid to parse non-xml file
67 char* file_ext = strrchr(path, '.');
68 if (NULL == ++file_ext || (file_ext && 0 != strncmp("xml", file_ext, strlen(file_ext)))) {
69 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] File extention is not XML type, file_ext(%s)", (file_ext) ? file_ext : "NULL");
73 bool isTextsize = false;
75 xmlNodePtr cur = NULL;
77 if (0 == access(path, F_OK)) {
78 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Success to access to %s", path);
79 doc = xmlParseFile(path);
81 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse xml file");
85 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to access to %s", path);
89 cur = xmlDocGetRootElement(doc);
91 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document. doc path(%s, %p)", path, doc);
97 if (xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_BASE_TAG)) {
98 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] The wrong type, root node is NOT 'tts-engine'. doc path(%s, %p)", path, doc);
104 cur = cur->xmlChildrenNode;
106 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document. doc path(%s, %p)", path, doc);
112 /* alloc engine info */
113 tts_engine_info_s* temp;
114 temp = (tts_engine_info_s*)calloc(1, sizeof(tts_engine_info_s));
116 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Out of memory. doc path(%s, %p)", path, doc);
125 temp->setting = NULL;
126 temp->pitch_support = false;
129 while (cur != NULL) {
130 if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_NAME)) {
131 xmlChar *key = xmlNodeGetContent(cur);
133 if (NULL != temp->name) {
137 temp->name = strdup((char*)key);
141 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_NAME);
143 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_ID)) {
144 xmlChar *key = xmlNodeGetContent(cur);
146 if (NULL != temp->uuid) {
150 temp->uuid = strdup((char*)key);
154 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_ID);
156 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_SETTING)) {
157 xmlChar *key = xmlNodeGetContent(cur);
159 if (NULL != temp->setting) {
161 temp->setting = NULL;
163 temp->setting = strdup((char*)key);
167 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_SETTING);
169 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_VOICE_SET)) {
170 xmlNodePtr voice_node = NULL;
171 voice_node = cur->xmlChildrenNode;
173 while (NULL != voice_node) {
174 if (0 == xmlStrcmp(voice_node->name, (const xmlChar *)TTS_TAG_ENGINE_VOICE)) {
175 tts_config_voice_s* temp_voice = (tts_config_voice_s*)calloc(1, sizeof(tts_config_voice_s));
176 if (NULL == temp_voice) {
177 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Out of memory");
181 xmlChar *attr = xmlGetProp(voice_node, (const xmlChar*)TTS_TAG_ENGINE_VOICE_TYPE);
183 if (0 == xmlStrcmp(attr, (const xmlChar *)TTS_TAG_VOICE_TYPE_FEMALE)) {
184 temp_voice->type = (int)TTS_CONFIG_VOICE_TYPE_FEMALE;
185 } else if (0 == xmlStrcmp(attr, (const xmlChar *)TTS_TAG_VOICE_TYPE_MALE)) {
186 temp_voice->type = (int)TTS_CONFIG_VOICE_TYPE_MALE;
187 } else if (0 == xmlStrcmp(attr, (const xmlChar *)TTS_TAG_VOICE_TYPE_CHILD)) {
188 temp_voice->type = (int)TTS_CONFIG_VOICE_TYPE_CHILD;
190 temp_voice->type = (int)TTS_CONFIG_VOICE_TYPE_USER_DEFINED;
195 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_VOICE_TYPE);
201 xmlChar *key = xmlNodeGetContent(voice_node);
203 if (NULL != temp_voice->language) {
204 free(temp_voice->language);
205 temp_voice->language = NULL;
207 temp_voice->language = strdup((char*)key);
210 temp->voices = g_slist_append(temp->voices, temp_voice);
212 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_VOICE);
213 if (NULL != temp_voice) {
219 voice_node = voice_node->next;
221 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_PITCH_SUPPORT)) {
222 xmlChar *key = xmlNodeGetContent(cur);
224 if (0 == xmlStrcmp(key, (const xmlChar *)"true")) {
225 temp->pitch_support = true;
230 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] <%s> has no content", TTS_TAG_ENGINE_PITCH_SUPPORT);
232 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)TTS_TAG_ENGINE_TEXT_SIZE)) {
234 xmlChar *key = xmlNodeGetContent(cur);
236 temp->text_size = atoi((char*)key);
239 SLOG(LOG_INFO, TAG_TTSCONFIG, "[INFO] text size is unlimited.");
246 if (false == isTextsize) {
247 temp->text_size = TTS_MAX_TEXT_SIZE;
251 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] doc path(%s, %p)", path, doc);
256 if (NULL == temp->uuid) {
258 SECURE_SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Invalid engine : %s", path);
259 tts_parser_free_engine_info(temp);
268 int tts_parser_free_engine_info(tts_engine_info_s* engine_info)
270 if (NULL == engine_info) {
271 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
275 if (NULL != engine_info->name) {
276 free(engine_info->name);
277 engine_info->name = NULL;
279 if (NULL != engine_info->uuid) {
280 free(engine_info->uuid);
281 engine_info->uuid = NULL;
283 if (NULL != engine_info->setting) {
284 free(engine_info->setting);
285 engine_info->setting = NULL;
289 if (g_slist_length(engine_info->voices) > 0) {
290 iter = g_slist_nth(engine_info->voices, 0);
291 while (NULL != iter) {
292 tts_config_voice_s *temp_voice = (tts_config_voice_s*)iter->data;
293 if (NULL != temp_voice) {
294 if (NULL != temp_voice->language) {
295 free(temp_voice->language);
296 temp_voice->language = NULL;
302 engine_info->voices = g_slist_delete_link(engine_info->voices, iter);
303 iter = g_slist_nth(engine_info->voices, 0);
307 if (NULL != engine_info) {
315 int tts_parser_print_engine_info(tts_engine_info_s* engine_info)
317 if (NULL == engine_info) {
318 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
322 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "== get engine info ==");
323 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " name : %s", engine_info->name);
324 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " id : %s", engine_info->uuid);
325 if (NULL != engine_info->setting)
326 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " setting : %s", engine_info->setting);
328 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " voices");
330 tts_config_voice_s *temp_voice;
332 if (g_slist_length(engine_info->voices) > 0) {
333 /* Get a first item */
334 iter = g_slist_nth(engine_info->voices, 0);
337 while (NULL != iter) {
338 /*Get handle data from list*/
339 temp_voice = iter->data;
341 SLOG(LOG_DEBUG, TAG_TTSCONFIG, " [%dth] type(%d) lang(%s)",
342 i, temp_voice->type, temp_voice->language);
345 iter = g_slist_next(iter);
349 SLOG(LOG_ERROR, TAG_TTSCONFIG, " Voice is NONE");
352 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "@@@");
357 int tts_parser_load_config(void)
359 xmlDocPtr doc = NULL;
360 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 xmlChar *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 xmlChar *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 xmlChar *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 xmlChar *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 xmlChar *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 xmlChar *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 xmlChar *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 xmlChar *key = xmlNodeGetContent(cur);
544 temp->bg_volume_ratio = atoi((char*)key) / VOLUME_BASE_VALUE;
548 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Background volume ratio is NULL");
557 pthread_mutex_lock(&g_config_info_mutex);
558 g_config_info = temp;
559 pthread_mutex_unlock(&g_config_info_mutex);
562 if (true == is_default_open) {
566 ret = xmlSaveFile(TTS_CONFIG, g_config_doc);
572 if (TTS_RETRY_COUNT == retry_count) {
573 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Save result : %d, err(%d)", ret, errno);
579 if (0 > chmod(TTS_CONFIG, 0664)) {
580 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to change file mode : %d", ret);
583 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Default config is changed : pid(%d)", getpid());
589 int tts_parser_unload_config(void)
591 if (NULL != g_config_doc) {
592 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Free g_config_doc(%p)", g_config_doc);
593 xmlFreeDoc(g_config_doc);
596 pthread_mutex_lock(&g_config_info_mutex);
597 if (NULL != g_config_info) {
598 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Free config_info(%p)", g_config_info);
600 g_config_info = NULL;
602 pthread_mutex_unlock(&g_config_info_mutex);
608 int tts_parser_set_config_info(tts_config_s* config_info)
610 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Set config info");
611 if (NULL == config_info) {
612 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] config_info is NULL");
615 pthread_mutex_lock(&g_config_info_mutex);
616 if (NULL == g_config_info) {
617 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] g_config_info is NULL");
618 pthread_mutex_unlock(&g_config_info_mutex);
621 memcpy(g_config_info, config_info, sizeof(tts_config_s));
622 pthread_mutex_unlock(&g_config_info_mutex);
626 int tts_parser_get_config_info(tts_config_s* config_info)
628 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Get config info");
629 if (NULL == config_info) {
630 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] config_info is NULL");
633 pthread_mutex_lock(&g_config_info_mutex);
634 if (NULL == g_config_info) {
635 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] g_config_info is NULL");
636 pthread_mutex_unlock(&g_config_info_mutex);
639 memcpy(config_info, g_config_info, sizeof(tts_config_s));
640 pthread_mutex_unlock(&g_config_info_mutex);
644 int tts_parser_copy_xml(const char* original, const char* destination)
646 if (NULL == original || NULL == destination) {
647 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
651 xmlDocPtr doc = NULL;
652 if (0 == access(original, F_OK)) {
653 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Success to access to %s", original);
654 doc = xmlParseFile(original);
656 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse file error : %s", original);
660 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to access to %s", original);
664 int ret = xmlSaveFile(destination, doc);
666 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Save result : %d", ret);
669 pFile = fopen(destination, "r");
672 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to open file %s", destination);
677 SLOG(LOG_INFO, TAG_TTSCONFIG, "[DEBUG] Success to fsync %s", destination);
679 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Success to save %s", destination);
683 if (0 > chmod(destination, 0664)) {
684 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to change file mode : %d", ret);
688 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] doc(%p)", doc);
693 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[SUCCESS] Copying xml");
698 static int __set_value_into_configuration(const char* key, const char* value)
700 if(NULL == key || NULL == value) {
701 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Parameter is NULL");
704 xmlNodePtr cur = NULL;
705 cur = xmlDocGetRootElement(g_config_doc);
707 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document");
711 if (xmlStrcmp(cur->name, (const xmlChar *) TTS_TAG_CONFIG_BASE_TAG)) {
712 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] The wrong type, root node is NOT %s", TTS_TAG_CONFIG_BASE_TAG);
716 cur = cur->xmlChildrenNode;
718 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document");
722 while (cur != NULL) {
723 if (0 == xmlStrcmp(cur->name, (const xmlChar *)key)) {
724 xmlNodeSetContent(cur, (const xmlChar *)value);
726 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Set key(%s) : value(%s)", key, value);
736 static void __save_configuration(xmlDocPtr config_doc)
738 int ret = xmlSaveFile(TTS_CONFIG, config_doc);
740 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Save result : file(%s), ret(%d)", TTS_CONFIG, ret);
743 pFile = fopen(TTS_CONFIG, "r");
746 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to open file %s", TTS_CONFIG);
751 SLOG(LOG_INFO, TAG_TTSCONFIG, "[DEBUG] Success to fsync %s", TTS_CONFIG);
753 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Success to save %s", TTS_CONFIG);
757 static char* __convert_voice_type_into_char(tts_config_voice_type_e voice_type)
759 switch (voice_type) {
760 case TTS_CONFIG_VOICE_TYPE_MALE:
761 return TTS_TAG_VOICE_TYPE_MALE;
762 case TTS_CONFIG_VOICE_TYPE_FEMALE:
763 return TTS_TAG_VOICE_TYPE_FEMALE;
764 case TTS_CONFIG_VOICE_TYPE_CHILD:
765 return TTS_TAG_VOICE_TYPE_CHILD;
767 return TTS_TAG_VOICE_TYPE_FEMALE;
771 int tts_parser_set_engine(const char* engine_id, const char* setting, const char* language, int type)
773 if (NULL == engine_id) {
774 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
778 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_ENGINE_ID, engine_id)) {
782 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_LANGUAGE, language)) {
786 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_ENGINE_SETTING, setting)) {
790 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_VOICE_TYPE, __convert_voice_type_into_char(type))) {
794 __save_configuration(g_config_doc);
799 int tts_parser_set_voice(const char* language, int type)
801 if (NULL == language) {
802 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
806 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_LANGUAGE, language)) {
810 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_VOICE_TYPE, __convert_voice_type_into_char(type))) {
814 __save_configuration(g_config_doc);
819 int tts_parser_set_auto_voice(bool value)
821 char* temp = value ? "on" : "off";
822 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_AUTO_VOICE, temp)) {
826 __save_configuration(g_config_doc);
831 int tts_parser_set_speech_rate(int value)
834 memset(temp, '\0', 10);
835 snprintf(temp, 10, "%d", value);
837 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_SPEECH_RATE, temp)) {
841 __save_configuration(g_config_doc);
846 int tts_parser_set_pitch(int value)
849 memset(temp, '\0', 10);
850 snprintf(temp, 10, "%d", value);
852 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_PITCH, temp)) {
856 __save_configuration(g_config_doc);
861 int tts_parser_set_bg_volume_ratio(double value)
864 memset(temp, '\0', 10);
865 snprintf(temp, 10, "%d", (int)(value * VOLUME_BASE_VALUE));
867 if (0 != __set_value_into_configuration(TTS_TAG_CONFIG_BACKGROUND_VOLUME_RATIO, temp)) {
871 __save_configuration(g_config_doc);
876 int tts_parser_find_config_changed(char** engine, char**setting, bool* auto_voice, char** language, int* voice_type,
877 int* speech_rate, int* pitch, double* bg_volume_ratio)
879 if (NULL == engine || NULL == setting || NULL == language || NULL == voice_type || NULL == speech_rate) {
880 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Input parameter is NULL");
884 xmlDocPtr doc = NULL;
885 xmlNodePtr cur_new = NULL;
886 xmlNodePtr cur_old = NULL;
889 while (NULL == doc) {
890 if (0 == access(TTS_CONFIG, F_OK)) {
891 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Success to access to %s", TTS_CONFIG);
892 doc = xmlParseFile(TTS_CONFIG);
900 if (TTS_RETRY_COUNT == retry_count) {
901 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse file error : %s", TTS_CONFIG);
906 cur_new = xmlDocGetRootElement(doc);
907 cur_old = xmlDocGetRootElement(g_config_doc);
908 if (cur_new == NULL || cur_old == NULL) {
909 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document(%p)", doc);
915 if (xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_BASE_TAG) ||
916 xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_BASE_TAG)) {
917 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] The wrong type, root node is NOT %s. doc(%p)", TTS_TAG_CONFIG_BASE_TAG, doc);
923 cur_new = cur_new->xmlChildrenNode;
924 cur_old = cur_old->xmlChildrenNode;
925 if (cur_new == NULL || cur_old == NULL) {
926 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Empty document(%p)", doc);
932 while (cur_new != NULL && cur_old != NULL) {
933 if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_ENGINE_ID)) {
934 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_ENGINE_ID)) {
935 xmlChar *key_old = xmlNodeGetContent(cur_old);
936 if (NULL != key_old) {
937 xmlChar *key_new = xmlNodeGetContent(cur_new);
938 if (NULL != key_new) {
939 if (0 != xmlStrcmp(key_old, key_new)) {
940 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old engine id(%s), New engine(%s)",
941 (char*)key_old, (char*)key_new);
942 if (NULL != *engine) {
946 *engine = strdup((char*)key_new);
955 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
957 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_ENGINE_SETTING)) {
958 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_ENGINE_SETTING)) {
959 xmlChar *key_old = xmlNodeGetContent(cur_old);
960 if (NULL != key_old) {
961 xmlChar *key_new = xmlNodeGetContent(cur_new);
962 if (NULL != key_new) {
963 if (0 != xmlStrcmp(key_old, key_new)) {
964 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old engine setting(%s), New engine setting(%s)",
965 (char*)key_old, (char*)key_new);
966 if (NULL != *setting) {
970 *setting = strdup((char*)key_new);
979 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
981 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_AUTO_VOICE)) {
982 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_AUTO_VOICE)) {
983 xmlChar *key_old = xmlNodeGetContent(cur_old);
984 if (NULL != key_old) {
985 xmlChar *key_new = xmlNodeGetContent(cur_new);
986 if (NULL != key_new) {
987 if (0 != xmlStrcmp(key_old, key_new)) {
988 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old auto voice (%s), New auto voice(%s)",
989 (char*)key_old, (char*)key_new);
990 if (0 == xmlStrcmp((const xmlChar*)"on", key_new)) {
1003 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1005 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_LANGUAGE)) {
1006 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_LANGUAGE)) {
1007 xmlChar *key_old = xmlNodeGetContent(cur_old);
1008 if (NULL != key_old) {
1009 xmlChar *key_new = xmlNodeGetContent(cur_new);
1010 if (NULL != key_new) {
1011 if (0 != xmlStrcmp(key_old, key_new)) {
1012 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old language(%s), New language(%s)",
1013 (char*)key_old, (char*)key_new);
1014 if (NULL != *language) {
1018 *language = strdup((char*)key_new);
1027 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1029 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_VOICE_TYPE)) {
1030 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_VOICE_TYPE)) {
1031 xmlChar *key_old = xmlNodeGetContent(cur_old);
1032 if (NULL != key_old) {
1033 xmlChar *key_new = xmlNodeGetContent(cur_new);
1034 if (NULL != key_new) {
1035 if (0 != xmlStrcmp(key_old, key_new)) {
1036 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old voice type(%s), New voice type(%s)",
1037 (char*)key_old, (char*)key_new);
1038 if (0 == xmlStrcmp(key_new, (const xmlChar *)TTS_TAG_VOICE_TYPE_FEMALE)) {
1039 *voice_type = (int)TTS_CONFIG_VOICE_TYPE_FEMALE;
1040 } else if (0 == xmlStrcmp(key_new, (const xmlChar *)TTS_TAG_VOICE_TYPE_MALE)) {
1041 *voice_type = (int)TTS_CONFIG_VOICE_TYPE_MALE;
1042 } else if (0 == xmlStrcmp(key_new, (const xmlChar *)TTS_TAG_VOICE_TYPE_CHILD)) {
1043 *voice_type = (int)TTS_CONFIG_VOICE_TYPE_CHILD;
1045 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] New voice type is not valid");
1055 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1057 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_SPEECH_RATE)) {
1058 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_SPEECH_RATE)) {
1059 xmlChar *key_old = xmlNodeGetContent(cur_old);
1060 if (NULL != key_old) {
1061 xmlChar *key_new = xmlNodeGetContent(cur_new);
1062 if (NULL != key_new) {
1063 if (0 != xmlStrcmp(key_old, key_new)) {
1064 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old speech rate(%s), New speech rate(%s)",
1065 (char*)key_old, (char*)key_new);
1066 *speech_rate = atoi((char*)key_new);
1075 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1077 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_PITCH)) {
1078 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_PITCH)) {
1079 xmlChar *key_old = xmlNodeGetContent(cur_old);
1080 if (NULL != key_old) {
1081 xmlChar *key_new = xmlNodeGetContent(cur_new);
1082 if (NULL != key_new) {
1083 if (0 != xmlStrcmp(key_old, key_new)) {
1084 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old pitch(%s), New pitch(%s)",
1085 (char*)key_old, (char*)key_new);
1086 *pitch = atoi((char*)key_new);
1095 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1097 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)TTS_TAG_CONFIG_BACKGROUND_VOLUME_RATIO)) {
1098 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)TTS_TAG_CONFIG_BACKGROUND_VOLUME_RATIO)) {
1099 xmlChar *key_old = xmlNodeGetContent(cur_old);
1100 if (NULL != key_old) {
1101 xmlChar *key_new = xmlNodeGetContent(cur_new);
1102 if (NULL != key_new) {
1103 if (0 != xmlStrcmp(key_old, key_new)) {
1104 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Old bg volume ratio(%s), New bg volume ratio(%s)",
1105 (char*)key_old, (char*)key_new);
1106 *bg_volume_ratio = atoi((char*)key_new) / VOLUME_BASE_VALUE;
1115 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] old config and new config are different");
1121 cur_new = cur_new->next;
1122 cur_old = cur_old->next;
1125 if (NULL != g_config_doc) {
1126 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Free g_config_doc(%p)", g_config_doc);
1127 xmlFreeDoc(g_config_doc);
1128 g_config_doc = NULL;
1135 int tts_parser_reset()
1137 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Reset g_config_doc as %s", TTS_DEFAULT_CONFIG);
1139 if (NULL != g_config_doc) {
1140 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[DEBUG] Free g_config_doc(%p)", g_config_doc);
1141 xmlFreeDoc(g_config_doc);
1142 g_config_doc = NULL;
1145 g_config_doc = xmlParseFile(TTS_DEFAULT_CONFIG);
1146 if (NULL == g_config_doc) {
1147 SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse %s", TTS_DEFAULT_CONFIG);
1151 char* engine_id = vconf_get_str(TTS_ENGINE_DB_DEFAULT);
1153 int ret = __set_value_into_configuration(TTS_TAG_CONFIG_ENGINE_ID, engine_id);
1154 SLOG(LOG_DEBUG, TAG_TTSCONFIG, "[DEBUG] Set engine ID into xml. engine_id(%s), ret(%d/%s)", engine_id, ret, get_error_message(ret));
1157 __save_configuration(g_config_doc);