Assign return values to individual variables
[platform/core/uifw/multi-assistant-service.git] / plugins / wakeup-manager / src / wakeup_settings.cpp
1 #include "wakeup_settings.h"
2 #include "wakeup_manager_main.h"
3
4 #include <sstream>
5 #include <algorithm>
6 #include <json/json.h>
7
8 namespace multiassistant
9 {
10 namespace wakeup
11 {
12
13 /* Utility function for checking if an element exists in a container */
14 template<class C, class T>
15 static auto contains(const C& v, const T& x) -> decltype(end(v), true)
16 {
17         return end(v) != find(begin(v), end(v), x);
18 }
19
20 static string prev_wake_word_enabled;
21
22 CWakeupSettings::CWakeupSettings()
23 {
24 }
25
26 CWakeupSettings::~CWakeupSettings()
27 {
28 }
29
30 static void wakeup_setting_input_language_changed_cb(keynode_t* node, void* data)
31 {
32         MWR_LOGD("[ENTER]");
33         if (nullptr == node) return;
34
35         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
36         if (nullptr == settings) return;
37
38         if (VCONF_TYPE_STRING == node->type) {
39                 const char* value = static_cast<const char*>(node->value.s);
40                 const auto& observers = settings->get_observers();
41                 for (const auto& observer : observers) {
42                         if (observer) {
43                                 if (!observer->on_voice_input_language_changed(value)) {
44                                         LOGW("[Settings WARNING] One of the observer returned false");
45                                 }
46                         }
47                 }
48         } else {
49                 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
50         }
51 }
52
53 static void wakeup_setting_enabled_assistants_changed_cb(keynode_t* node, void* data)
54 {
55         MWR_LOGD("[ENTER]");
56         if (nullptr == node) return;
57
58         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
59         if (nullptr == settings) return;
60
61         if (VCONF_TYPE_STRING == node->type) {
62                 vector<string> newlyAddedAssistants;
63                 vector<string> newlyRemovedAssistants;
64                 const char* value = static_cast<const char*>(node->value.s);
65                 if (value) {
66                         vector<string> previouslyEnabledAssistants = settings->get_enabled_assistants();
67                         vector<string> currentlyEnabledAssistants;
68                         string token;
69                         istringstream iss(value);
70                         currentlyEnabledAssistants.clear();
71                         while (getline(iss, token, ';')) {
72                                 currentlyEnabledAssistants.push_back(token);
73                                 MWR_LOGD("enabled_assistants : %s", token.c_str());
74                         }
75
76                         for (const auto& assistant : currentlyEnabledAssistants) {
77                                 if (!contains(previouslyEnabledAssistants, assistant)) {
78                                         newlyAddedAssistants.push_back(assistant);
79                                 }
80                         }
81                         for (const auto& assistant : previouslyEnabledAssistants) {
82                                 if (!contains(currentlyEnabledAssistants, assistant)) {
83                                         newlyRemovedAssistants.push_back(assistant);
84                                 }
85                         }
86                 }
87
88                 const auto& observers = settings->get_observers();
89                 for (const auto& observer : observers) {
90                         if (observer) {
91                                 for (const auto& assistant : newlyAddedAssistants) {
92                                         if (!observer->on_assistant_enabled_info_changed(assistant.c_str(), true)) {
93                                                 LOGW("[Settings WARNING] One of the observer returned false");
94                                         }
95                                 }
96                                 for (const auto& assistant : newlyRemovedAssistants) {
97                                         if (!observer->on_assistant_enabled_info_changed(assistant.c_str(), false)) {
98                                                 LOGW("[Settings WARNING] One of the observer returned false");
99                                         }
100                                 }
101                         }
102                 }
103         } else {
104                 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
105         }
106 }
107
108 static void wakeup_setting_default_assistant_appid_changed_cb(keynode_t* node, void* data)
109 {
110         MWR_LOGD("[ENTER]");
111         if (nullptr == node) return;
112
113         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
114         if (nullptr == settings) return;
115
116         if (VCONF_TYPE_STRING == node->type) {
117                 const char* value = static_cast<const char*>(node->value.s);
118                 const auto& observers = settings->get_observers();
119                 for (const auto& observer : observers) {
120                         if (observer) {
121                                 if (!observer->on_default_assistant_appid_changed(value)) {
122                                         LOGW("[Settings WARNING] One of the observer returned false");
123                                 }
124                         }
125                 }
126         } else {
127                 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
128         }
129 }
130
131 static void wakeup_setting_multiple_mode_changed_cb(keynode_t* node, void* data)
132 {
133         MWR_LOGD("[ENTER]");
134
135         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
136         if (nullptr == settings) return;
137
138         const auto& observers = settings->get_observers();
139         for (const auto& observer : observers) {
140                 if (observer) {
141                         if (!observer->on_multiple_mode_changed()) {
142                                 LOGW("[Settings WARNING] One of the observer returned false");
143                         }
144                 }
145         }
146 }
147
148 static void wakeup_setting_enabled_wake_word_detection_changed_cb(keynode_t* node, void* data)
149 {
150         MWR_LOGD("[ENTER]");
151
152         char *vconf_str = vconf_keynode_get_str(node);
153         if (!vconf_str) return;
154
155         MWR_LOGI("vconf value : %s", vconf_str);
156         if (0 == prev_wake_word_enabled.compare(string(vconf_str))) {
157                 return;
158         }
159
160         prev_wake_word_enabled = string(vconf_str);
161
162         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
163         if (nullptr == settings) return;
164
165         const auto& observers = settings->get_observers();
166         for (const auto& observer : observers) {
167                 if (observer) {
168                         if (!observer->on_wake_word_detection_enabled_info_changed()) {
169                                 LOGW("[Settings WARNING] One of the observer returned false");
170                         }
171                 }
172         }
173 }
174
175 void CWakeupSettings::initialize(map<string, string> custom_keys)
176 {
177         int vconf_ret;
178         int vconf_bool;
179         double vconf_double;
180
181         mCustomVconfKeys.clear();
182         mCustomVconfKeys = custom_keys;
183
184         char *default_assistant_appid = vconf_get_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str());
185         if (default_assistant_appid) {
186                 mDefaultAssistantAppid = default_assistant_appid;
187                 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
188                 for (const auto& observer : mObservers) {
189                         if (observer) {
190                                 if (!observer->on_default_assistant_appid_changed(default_assistant_appid)) {
191                                         LOGW("[Settings WARNING] One of the observer returned false");
192                                 }
193                         }
194                 }
195                 free(default_assistant_appid);
196                 default_assistant_appid = nullptr;
197         }
198         vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_UI_PANEL_ENABLED, &vconf_bool);
199         if (0 == vconf_ret) {
200                 mUiPanelEnabled = vconf_bool;
201                 MWR_LOGD("ui_panel_enabled : %s", (mUiPanelEnabled ? "true" : "false"));
202         }
203         vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_CONVERSATION_TIMEOUT, &vconf_double);
204         if (0 == vconf_ret) {
205                 mConversationTimeout = vconf_double;
206                 MWR_LOGD("conversation_timeout : %f", mConversationTimeout);
207         }
208         vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE, &vconf_bool);
209         if (0 == vconf_ret) {
210                 mMultipleMode = vconf_bool;
211                 MWR_LOGD("multiple_mode : %s", (mMultipleMode ? "true" : "false"));
212         }
213         if (true == mMultipleMode) {
214                 char *enabled_assistants = vconf_get_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS].c_str());
215                 if (enabled_assistants) {
216                         string token;
217                         istringstream iss(enabled_assistants);
218                         mEnabledAssistants.clear();
219                         while (getline(iss, token, ';')) {
220                                 mEnabledAssistants.push_back(token);
221                                 MWR_LOGD("enabled_assistants : %s", token.c_str());
222                                 for (const auto& observer : mObservers) {
223                                         if (observer) {
224                                                 if (!observer->on_assistant_enabled_info_changed(token.c_str(), true)) {
225                                                         LOGW("[Settings WARNING] One of the observer returned false");
226                                                 }
227                                         }
228                                 }
229                         }
230                         free(enabled_assistants);
231                         enabled_assistants = nullptr;
232                 }
233         }
234         char *wakeword_detection_enabled = vconf_get_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_WAKE_WORD_DETECTION_ENABLED].c_str());
235         if (wakeword_detection_enabled) {
236                 Json::Reader reader;
237                 Json::Value root;
238                 mWakeWordDisabledAssistants.clear();
239                 if (!reader.parse(wakeword_detection_enabled, root)) {
240                         LOGW("[Settings WARNING] Failed to parse Json : %s", reader.getFormattedErrorMessages().c_str());
241                 } else {
242                         auto member = root.getMemberNames();
243                         for (string m : member) {
244                                 if (0 == root[m].asString().compare("off")) {
245                                         mWakeWordDisabledAssistants.push_back(m);
246                                         MWR_LOGD("wake_word_detection_disabled assistant : %s", m.c_str());
247                                 }
248                         }
249                 }
250                 free(wakeword_detection_enabled);
251                 wakeword_detection_enabled = nullptr;
252         }
253         vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_DELAY, &vconf_double);
254         if (0 == vconf_ret) {
255                 mWakeupPolicyDelay = vconf_double;
256                 MWR_LOGD("conversation_timeout : %f", mWakeupPolicyDelay);
257         }
258         char *wakeup_policy_priority = vconf_get_str(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_PRIORITY);
259         if (wakeup_policy_priority) {
260                 string token;
261                 istringstream iss(wakeup_policy_priority);
262                 mWakeupPolicyPriority.clear();
263                 while (getline(iss, token, ';')) {
264                         mWakeupPolicyPriority.push_back(token);
265                         MWR_LOGD("wakeup_policy_priority : %s", token.c_str());
266                 }
267                 free(wakeup_policy_priority);
268                 wakeup_policy_priority = nullptr;
269         }
270         vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_STREAMING_DURATION_MAX, &vconf_double);
271         if (0 == vconf_ret) {
272                 mStreamingDurationMax = vconf_double;
273                 MWR_LOGD("streaming_duration_max : %f", mStreamingDurationMax);
274         }
275 #ifdef ENABLE_VOICE_INPUT_LANGUAGE_SETTINGS
276         char *voice_input_language = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
277         if (voice_input_language) {
278                 mVoiceInputLanguage = voice_input_language;
279                 MWR_LOGD("voice input language : %s", mVoiceInputLanguage.c_str());
280                 for (const auto& observer : mObservers) {
281                         if (observer) {
282                                 if (!observer->on_voice_input_language_changed(voice_input_language)) {
283                                         LOGW("[Settings WARNING] One of the observer returned false");
284                                 }
285                         }
286                 }
287                 free(voice_input_language);
288                 voice_input_language = nullptr;
289         }
290 #endif
291
292 #ifdef ENABLE_VOICE_INPUT_LANGUAGE_SETTINGS
293         vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE,
294                 wakeup_setting_input_language_changed_cb, this);
295 #endif
296         vconf_notify_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str(),
297                 wakeup_setting_default_assistant_appid_changed_cb, this);
298         if (true == mMultipleMode) {
299                 vconf_notify_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS].c_str(),
300                         wakeup_setting_enabled_assistants_changed_cb, this);
301         }
302         vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE,
303                 wakeup_setting_multiple_mode_changed_cb, this);
304         vconf_notify_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_WAKE_WORD_DETECTION_ENABLED].c_str(),
305                 wakeup_setting_enabled_wake_word_detection_changed_cb, this);
306 }
307
308 void CWakeupSettings::deinitialize()
309 {
310 #ifdef ENABLE_VOICE_INPUT_LANGUAGE_SETTINGS
311         vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE,
312                 wakeup_setting_input_language_changed_cb);
313 #endif
314         vconf_ignore_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str(),
315                 wakeup_setting_default_assistant_appid_changed_cb);
316         if (true == mMultipleMode) {
317                 vconf_ignore_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS].c_str(),
318                         wakeup_setting_enabled_assistants_changed_cb);
319         }
320         vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE,
321                 wakeup_setting_multiple_mode_changed_cb);
322         vconf_ignore_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_WAKE_WORD_DETECTION_ENABLED].c_str(),
323                 wakeup_setting_enabled_wake_word_detection_changed_cb);
324 }
325
326 void CWakeupSettings::subscribe(ISettingsEventObserver *observer)
327 {
328         mObservers.push_back(observer);
329 }
330
331 void CWakeupSettings::unsubscribe(ISettingsEventObserver *observer)
332 {
333         auto iter = find(mObservers.begin(), mObservers.end(), observer);
334         if (iter != mObservers.end()) {
335                 mObservers.erase(iter);
336         }
337 }
338
339 const vector<ISettingsEventObserver*>& CWakeupSettings::get_observers()
340 {
341         return mObservers;
342 }
343
344 string CWakeupSettings::get_default_assistant_appid()
345 {
346         char *vconf_str;
347         vconf_str = vconf_get_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str());
348         MWR_LOGD("vconf default_assistant_appid : %s", vconf_str);
349         if (vconf_str) {
350                 mDefaultAssistantAppid = vconf_str;
351                 MWR_LOGD("member default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
352                 free(vconf_str);
353                 vconf_str = nullptr;
354         }
355
356         return mDefaultAssistantAppid;
357 }
358
359 void CWakeupSettings::set_default_assistant_appid(std::string appid)
360 {
361         if (appid.empty()) {
362                 MWR_LOGE("appid is NULL");
363                 return;
364         }
365         if (appid.compare(get_default_assistant_appid()) == 0) {
366                 MWR_LOGE("Default assistant appid not changed, ignoring...");
367                 return;
368         }
369         int ret = vconf_set_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str(), appid.c_str());
370         MWR_LOGD("default_assistant_appid : %s, %d", appid.c_str(), ret);
371         mDefaultAssistantAppid = appid;
372 }
373
374 bool CWakeupSettings::get_ui_panel_enabled()
375 {
376         return mUiPanelEnabled;
377 }
378
379 float CWakeupSettings::get_conversation_timeout()
380 {
381         return mConversationTimeout;
382 }
383
384 bool CWakeupSettings::get_multiple_mode()
385 {
386         return mMultipleMode;
387 }
388
389 vector<string> CWakeupSettings::get_enabled_assistants()
390 {
391         return mEnabledAssistants;
392 }
393
394 float CWakeupSettings::get_wakeup_policy_delay()
395 {
396         return mWakeupPolicyDelay;
397 }
398
399 vector<string> CWakeupSettings::get_wakeup_policy_priority()
400 {
401         return mWakeupPolicyPriority;
402 }
403
404 float CWakeupSettings::get_streaming_duration_max()
405 {
406         return mStreamingDurationMax;
407 }
408
409 std::string CWakeupSettings::get_current_language(void)
410 {
411         std::string result{"en_US"};
412
413 #ifdef ENABLE_VOICE_INPUT_LANGUAGE_SETTINGS
414         char* language = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
415         if (language) {
416                 result = language;
417                 free(language);
418         }
419 #endif
420
421         return result;
422 }
423
424 vector<string> CWakeupSettings::get_wake_word_disabled_assistants()
425 {
426         return mWakeWordDisabledAssistants;
427 }
428
429 } // wakeup
430 } // multiassistant