2 * Copyright (c) 2012, 2013 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.
14 #include <Ecore_File.h>
16 /* For multi-user support */
17 #include <tzplatform_config.h>
19 #include "sttd_main.h"
20 #include "sttd_config.h"
23 #define CONFIG_FILE_PATH tzplatform_mkpath(TZ_USER_HOME, ".voice/sttd.conf")
24 #define CONFIG_DEFAULT BASE_DIRECTORY_DEFAULT"/sttd.conf"
26 #define ENGINE_ID "ENGINE_ID"
27 #define LANGUAGE "LANGUAGE"
28 #define SILENCE "SILENCE"
29 #define PROFANITY "PROFANITY"
30 #define PUNCTUATION "PUNCTUATION"
33 static char* g_engine_id;
34 static char* g_language;
36 static int g_profanity;
37 static int g_punctuation;
39 int __sttd_config_save()
41 if (0 != access(CONFIG_FILE_PATH, R_OK|W_OK)) {
42 if (0 == ecore_file_mkpath(CONFIG_DIRECTORY)) {
43 SLOG(LOG_ERROR, TAG_STTD, "[Config ERROR ] Fail to create directory (%s)", CONFIG_DIRECTORY);
47 SLOG(LOG_WARN, TAG_STTD, "[Config] Create directory (%s)", CONFIG_DIRECTORY);
51 config_fp = fopen(CONFIG_FILE_PATH, "w+");
53 if (NULL == config_fp) {
54 /* make file and file default */
55 SLOG(LOG_ERROR, TAG_STTD, "[Config ERROR] Fail to load config (engine id)");
59 SLOG(LOG_DEBUG, TAG_STTD, "[Config] Rewrite config file");
62 fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
65 fprintf(config_fp, "%s %s\n", LANGUAGE, g_language);
67 /* Write silence detection */
68 fprintf(config_fp, "%s %d\n", SILENCE, g_silence);
71 fprintf(config_fp, "%s %d\n", PROFANITY, g_profanity);
73 /* Write punctuation */
74 fprintf(config_fp, "%s %d\n", PUNCTUATION, g_punctuation);
81 int __sttd_config_load()
84 char buf_id[256] = {0};
85 char buf_param[256] = {0};
87 bool is_default_open = false;
89 config_fp = fopen(CONFIG_FILE_PATH, "r");
91 if (NULL == config_fp) {
92 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Not open file(%s)", CONFIG_FILE_PATH);
94 config_fp = fopen(CONFIG_DEFAULT, "r");
95 if (NULL == config_fp) {
96 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Not open original config file(%s)", CONFIG_FILE_PATH);
100 is_default_open = true;
104 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
106 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (engine id)");
107 __sttd_config_save();
110 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
111 g_engine_id = strdup(buf_param);
114 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (engine id)");
115 __sttd_config_save();
121 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
123 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (language)");
124 __sttd_config_save();
127 if (0 == strncmp(LANGUAGE, buf_id, strlen(LANGUAGE))) {
128 g_language = strdup(buf_param);
131 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (language)");
132 __sttd_config_save();
137 /* Read silence detection */
138 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
140 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (silence)");
141 __sttd_config_save();
144 if (0 == strncmp(SILENCE, buf_id, strlen(SILENCE))) {
145 g_silence = int_param;
148 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (silence)");
149 __sttd_config_save();
154 /* Read profanity filter */
155 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
157 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (profanity filter)");
158 __sttd_config_save();
161 if (0 == strncmp(PROFANITY, buf_id, strlen(PROFANITY))) {
162 g_profanity = int_param;
165 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (profanity filter)");
166 __sttd_config_save();
172 /* Read punctuation override */
173 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
175 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (punctuation override)");
176 __sttd_config_save();
179 if (0 == strncmp(PUNCTUATION, buf_id, strlen(PUNCTUATION))) {
180 g_punctuation = int_param;
183 SLOG(LOG_WARN, TAG_STTD, "[Config WARNING] Fail to load config (punctuation override)");
184 __sttd_config_save();
191 SLOG(LOG_DEBUG, TAG_STTD, "[Config] Load config : engine(%s), language(%s), silence(%d), profanity(%d), punctuation(%d)",
192 g_engine_id, g_language, g_silence, g_profanity, g_punctuation);
194 if (true == is_default_open) {
195 if(0 == __sttd_config_save()) {
196 SLOG(LOG_DEBUG, TAG_STTD, "[Config] Create config(%s)", CONFIG_FILE_PATH);
203 int sttd_config_initialize()
211 __sttd_config_load();
216 int sttd_config_finalize()
218 __sttd_config_save();
222 int sttd_config_get_default_engine(char** engine_id)
224 if (NULL == engine_id)
227 *engine_id = strdup(g_engine_id);
231 int sttd_config_set_default_engine(const char* engine_id)
233 if (NULL == engine_id)
236 if (NULL != g_engine_id)
239 g_engine_id = strdup(engine_id);
240 __sttd_config_save();
244 int sttd_config_get_default_language(char** language)
246 if (NULL == language)
249 *language = strdup(g_language);
254 int sttd_config_set_default_language(const char* language)
256 if (NULL == language)
259 if (NULL != g_language)
262 g_language = strdup(language);
264 __sttd_config_save();
269 int sttd_config_get_default_silence_detection(int* silence)
274 *silence = g_silence;
279 int sttd_config_set_default_silence_detection(int silence)
282 __sttd_config_save();
286 int sttd_config_get_default_profanity_filter(int* profanity)
288 if (NULL == profanity)
291 *profanity = g_profanity;
296 int sttd_config_set_default_profanity_filter(int profanity)
298 g_profanity = profanity;
299 __sttd_config_save();
303 int sttd_config_get_default_punctuation_override(int* punctuation)
305 if (NULL == punctuation)
308 *punctuation = g_punctuation;
313 int sttd_config_set_default_punctuation_override(int punctuation)
315 g_punctuation = punctuation;
316 __sttd_config_save();