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