1 #include "wakeup_settings.h"
2 #include "wakeup_manager_main.h"
7 namespace multiassistant
12 /* Utility function for checking if an element exists in a container */
13 template<class C, class T>
14 static auto contains(const C& v, const T& x) -> decltype(end(v), true)
16 return end(v) != find(begin(v), end(v), x);
19 CWakeupSettings::CWakeupSettings()
23 CWakeupSettings::~CWakeupSettings()
27 static void wakeup_setting_input_language_changed_cb(keynode_t* node, void* data)
30 if (nullptr == node) return;
32 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
33 if (nullptr == settings) return;
35 if (VCONF_TYPE_STRING == node->type) {
36 const char* value = static_cast<const char*>(node->value.s);
37 const auto& observers = settings->get_observers();
38 for (const auto& observer : observers) {
40 if (!observer->on_voice_input_language_changed(value)) {
41 LOGW("[Settings WARNING] One of the observer returned false");
46 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
50 static void wakeup_setting_enabled_assistants_changed_cb(keynode_t* node, void* data)
53 if (nullptr == node) return;
55 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
56 if (nullptr == settings) return;
58 if (VCONF_TYPE_STRING == node->type) {
59 vector<string> newlyAddedAssistants;
60 vector<string> newlyRemovedAssistants;
61 const char* value = static_cast<const char*>(node->value.s);
63 vector<string> previouslyEnabledAssistants = settings->get_enabled_assistants();
64 vector<string> currentlyEnabledAssistants;
66 istringstream iss(value);
67 currentlyEnabledAssistants.clear();
68 while (getline(iss, token, ';')) {
69 currentlyEnabledAssistants.push_back(token);
70 MWR_LOGD("enabled_assistants : %s", token.c_str());
73 for (const auto& assistant : currentlyEnabledAssistants) {
74 if (!contains(previouslyEnabledAssistants, assistant)) {
75 newlyAddedAssistants.push_back(assistant);
78 for (const auto& assistant : previouslyEnabledAssistants) {
79 if (!contains(currentlyEnabledAssistants, assistant)) {
80 newlyRemovedAssistants.push_back(assistant);
85 const auto& observers = settings->get_observers();
86 for (const auto& observer : observers) {
88 for (const auto& assistant : newlyAddedAssistants) {
89 if (!observer->on_assistant_enabled_info_changed(assistant.c_str(), true)) {
90 LOGW("[Settings WARNING] One of the observer returned false");
93 for (const auto& assistant : newlyRemovedAssistants) {
94 if (!observer->on_assistant_enabled_info_changed(assistant.c_str(), false)) {
95 LOGW("[Settings WARNING] One of the observer returned false");
101 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
105 static void wakeup_setting_default_assistant_appid_changed_cb(keynode_t* node, void* data)
108 if (nullptr == node) return;
110 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
111 if (nullptr == settings) return;
113 if (VCONF_TYPE_STRING == node->type) {
114 const char* value = static_cast<const char*>(node->value.s);
115 const auto& observers = settings->get_observers();
116 for (const auto& observer : observers) {
118 if (!observer->on_default_assistant_appid_changed(value)) {
119 LOGW("[Settings WARNING] One of the observer returned false");
124 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
128 static void wakeup_setting_multiple_mode_changed_cb(keynode_t* node, void* data)
132 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
133 if (nullptr == settings) return;
135 const auto& observers = settings->get_observers();
136 for (const auto& observer : observers) {
138 if (!observer->on_multiple_mode_changed()) {
139 LOGW("[Settings WARNING] One of the observer returned false");
145 void CWakeupSettings::initialize()
152 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
154 mDefaultAssistantAppid = vconf_str;
155 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
156 for (const auto& observer : mObservers) {
158 if (!observer->on_default_assistant_appid_changed(vconf_str)) {
159 LOGW("[Settings WARNING] One of the observer returned false");
166 vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_UI_PANEL_ENABLED, &vconf_bool);
167 if (0 == vconf_ret) {
168 mUiPanelEnabled = vconf_bool;
169 MWR_LOGD("ui_panel_enabled : %s", (mUiPanelEnabled ? "true" : "false"));
171 vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_CONVERSATION_TIMEOUT, &vconf_double);
172 if (0 == vconf_ret) {
173 mConversationTimeout = vconf_double;
174 MWR_LOGD("conversation_timeout : %f", mConversationTimeout);
176 vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE, &vconf_bool);
177 if (0 == vconf_ret) {
178 mMultipleMode = vconf_bool;
179 MWR_LOGD("multiple_mode : %s", (mMultipleMode ? "true" : "false"));
181 if (true == mMultipleMode) {
182 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS);
185 istringstream iss(vconf_str);
186 mEnabledAssistants.clear();
187 while (getline(iss, token, ';')) {
188 mEnabledAssistants.push_back(token);
189 MWR_LOGD("enabled_assistants : %s", token.c_str());
190 for (const auto& observer : mObservers) {
192 if (!observer->on_assistant_enabled_info_changed(token.c_str(), true)) {
193 LOGW("[Settings WARNING] One of the observer returned false");
202 vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_DELAY, &vconf_double);
203 if (0 == vconf_ret) {
204 mWakeupPolicyDelay = vconf_double;
205 MWR_LOGD("conversation_timeout : %f", mWakeupPolicyDelay);
207 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_PRIORITY);
210 istringstream iss(vconf_str);
211 mWakeupPolicyPriority.clear();
212 while (getline(iss, token, ';')) {
213 mWakeupPolicyPriority.push_back(token);
214 MWR_LOGD("wakeup_policy_priority : %s", token.c_str());
219 vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_STREAMING_DURATION_MAX, &vconf_double);
220 if (0 == vconf_ret) {
221 mStreamingDurationMax = vconf_double;
222 MWR_LOGD("streaming_duration_max : %f", mStreamingDurationMax);
224 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
226 mVoiceInputLanguage = vconf_str;
227 MWR_LOGD("voice input language : %s", mVoiceInputLanguage.c_str());
228 for (const auto& observer : mObservers) {
230 if (!observer->on_voice_input_language_changed(vconf_str)) {
231 LOGW("[Settings WARNING] One of the observer returned false");
239 vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE,
240 wakeup_setting_input_language_changed_cb, this);
241 vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID,
242 wakeup_setting_default_assistant_appid_changed_cb, this);
243 if (true == mMultipleMode) {
244 vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS,
245 wakeup_setting_enabled_assistants_changed_cb, this);
247 vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE,
248 wakeup_setting_multiple_mode_changed_cb, this);
251 void CWakeupSettings::deinitialize()
253 vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE,
254 wakeup_setting_input_language_changed_cb);
255 vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID,
256 wakeup_setting_default_assistant_appid_changed_cb);
257 if (true == mMultipleMode) {
258 vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS,
259 wakeup_setting_enabled_assistants_changed_cb);
261 vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE,
262 wakeup_setting_multiple_mode_changed_cb);
265 void CWakeupSettings::subscribe(ISettingsEventObserver *observer)
267 mObservers.push_back(observer);
270 void CWakeupSettings::unsubscribe(ISettingsEventObserver *observer)
272 auto iter = find(mObservers.begin(), mObservers.end(), observer);
273 if (iter != mObservers.end()) {
274 mObservers.erase(iter);
278 const vector<ISettingsEventObserver*>& CWakeupSettings::get_observers()
283 string CWakeupSettings::get_default_assistant_appid()
286 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
287 MWR_LOGD("vconf default_assistant_appid : %s", vconf_str);
289 mDefaultAssistantAppid = vconf_str;
290 MWR_LOGD("member default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
295 return mDefaultAssistantAppid;
298 void CWakeupSettings::set_default_assistant_appid(std::string appid)
300 if (appid.compare(get_default_assistant_appid()) == 0) {
301 MWR_LOGE("Default assistant appid not changed, ignoring...");
304 int ret = vconf_set_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID, appid.c_str());
305 MWR_LOGD("default_assistant_appid : %s, %d", appid.c_str(), ret);
306 mDefaultAssistantAppid = appid;
309 bool CWakeupSettings::get_ui_panel_enabled()
311 return mUiPanelEnabled;
314 float CWakeupSettings::get_conversation_timeout()
316 return mConversationTimeout;
319 bool CWakeupSettings::get_multiple_mode()
321 return mMultipleMode;
324 vector<string> CWakeupSettings::get_enabled_assistants()
326 return mEnabledAssistants;
329 float CWakeupSettings::get_wakeup_policy_delay()
331 return mWakeupPolicyDelay;
334 vector<string> CWakeupSettings::get_wakeup_policy_priority()
336 return mWakeupPolicyPriority;
339 float CWakeupSettings::get_streaming_duration_max()
341 return mStreamingDurationMax;
344 std::string CWakeupSettings::get_current_language(void)
346 std::string result{"en_US"};
347 char* language = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);