71af39da8fe2f5eb056b90cdeaa14745eba20ad9
[platform/core/uifw/stt.git] / server / sttd_config.c
1 /*
2 *  Copyright (c) 2011-2014 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.
12 */
13
14
15 #include "stt_config_mgr.h"
16 #include "sttd_config.h"
17 #include "sttd_main.h"
18
19
20 static sttd_config_engine_changed_cb g_engine_cb;
21
22 static sttd_config_language_changed_cb g_lang_cb;
23
24 static sttd_config_silence_changed_cb g_silence_cb;
25
26 static void* g_user_data;
27
28
29 const char* stt_tag()
30 {
31         return "sttd";
32 }
33
34
35 void __sttd_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, bool support_silence, bool need_credential, void* user_data)
36 {
37         /* Need to check engine is valid */
38         if (false == stt_config_check_default_engine_is_valid(engine_id)) {
39                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "Engine id is NOT valid : %s", engine_id);
40                 return;
41         }
42
43         if (NULL != g_engine_cb)
44                 g_engine_cb(engine_id, language, support_silence, need_credential, g_user_data);
45         else
46                 SLOG(LOG_ERROR, TAG_STTD, "Engine changed callback is NULL");
47 }
48
49 void __sttd_config_lang_changed_cb(const char* before_language, const char* current_language, void* user_data)
50 {
51         if (false == stt_config_check_default_language_is_valid(current_language)) {
52                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "Language is NOT valid : %s", current_language);
53                 return;
54         }
55
56         if (NULL != g_lang_cb)
57                 g_lang_cb(current_language, g_user_data);
58         else
59                 SLOG(LOG_ERROR, TAG_STTD, "Language changed callback is NULL");
60 }
61
62 void __config_bool_changed_cb(stt_config_type_e type, bool bool_value, void* user_data)
63 {
64         SECURE_SLOG(LOG_DEBUG, TAG_STTD, " type(%d) bool(%s)", type, bool_value ? "on" : "off");
65
66         if (STT_CONFIG_TYPE_OPTION_SILENCE_DETECTION == type) {
67                 if (NULL != g_silence_cb) {
68                         g_silence_cb(bool_value, g_user_data);
69                         SLOG(LOG_DEBUG, TAG_STTD, "Call back silence detection changed");
70                 }
71         }
72
73         return;
74 }
75
76 int sttd_config_initialize(sttd_config_engine_changed_cb engine_cb, 
77                            sttd_config_language_changed_cb lang_cb, 
78                            sttd_config_silence_changed_cb silence_cb, 
79                            void* user_data)
80 {
81         if (NULL == engine_cb || NULL == lang_cb || NULL == silence_cb) {
82                 SLOG(LOG_ERROR, TAG_STTD, "[Config] Invalid parameter");
83                 return -1;
84         }
85
86         int ret = -1;
87         ret = stt_config_mgr_initialize(getpid());
88         if (0 != ret) {
89                 SLOG(LOG_ERROR, TAG_STTD, "[Config] Fail to initialize config manager");
90                 return -1;
91         }
92
93         ret = stt_config_mgr_set_callback(getpid(), __sttd_config_engine_changed_cb, __sttd_config_lang_changed_cb, 
94                 __config_bool_changed_cb, NULL);
95         if (0 != ret) {
96                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to set config changed : %d", ret);
97                 return -1;
98         }
99
100         g_engine_cb = engine_cb;
101         g_lang_cb = lang_cb;
102         g_silence_cb = silence_cb;
103         g_user_data = user_data;
104
105         return 0;
106 }
107
108 int sttd_config_finalize()
109 {
110         stt_config_mgr_finalize(getpid());
111
112         return 0;
113 }
114
115 int sttd_config_get_default_engine(char** engine_id)
116 {
117         if (NULL == engine_id)
118                 return -1;
119
120         if (0 != stt_config_mgr_get_engine(engine_id)) {
121                 SLOG(LOG_ERROR, TAG_STTD, "[Config ERROR] Fail to get engine id");
122         }
123
124         return 0;
125 }
126
127 int sttd_config_set_default_engine(const char* engine_id)
128 {
129         if (NULL == engine_id)
130                 return -1;
131
132         if (0 != stt_config_mgr_set_engine(engine_id)) {
133                 SLOG(LOG_ERROR, TAG_STTD, "[Config ERROR] Fail to set engine id");
134         }
135
136         return 0;
137 }
138
139 int sttd_config_get_default_language(char** language)
140 {
141         if (NULL == language)
142                 return -1;
143
144         if (0 != stt_config_mgr_get_default_language(language)) {
145                 SLOG(LOG_ERROR, TAG_STTD, "[Config ERROR] Fail to get language");
146                 return -1;
147         }
148
149         return 0;
150 }
151
152 int sttd_config_get_default_silence_detection(int* silence)
153 {
154         if (NULL == silence)
155                 return -1;
156
157         bool value;
158         if (0 != stt_config_mgr_get_silence_detection(&value)) {
159                 SLOG(LOG_ERROR, TAG_STTD, "[Config ERROR] Fail to set language");
160                 return -1;
161         }
162
163         *silence = (int)value;
164
165         return 0;
166 }
167
168 int sttd_config_time_add(int index, int event, const char* text, long start_time, long end_time)
169 {
170         return stt_config_mgr_add_time_info(index, event, text, start_time, end_time);
171 }
172
173 int sttd_config_time_save()
174 {
175         return stt_config_mgr_save_time_info_file();
176 }
177
178 int sttd_config_time_reset()
179 {
180         return stt_config_mgr_reset_time_info();
181 }