Modified to load only required wakeup engines
[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
7 namespace multiassistant
8 {
9 namespace wakeup
10 {
11
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)
15 {
16         return end(v) != find(begin(v), end(v), x);
17 }
18
19 CWakeupSettings::CWakeupSettings()
20 {
21 }
22
23 CWakeupSettings::~CWakeupSettings()
24 {
25 }
26
27 static void wakeup_setting_input_language_changed_cb(keynode_t* node, void* data)
28 {
29         MWR_LOGD("[ENTER]");
30         if (nullptr == node) return;
31
32         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
33         if (nullptr == settings) return;
34
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) {
39                         if (observer) {
40                                 if (!observer->on_voice_input_language_changed(value)) {
41                                         LOGW("[Settings WARNING] One of the observer returned false");
42                                 }
43                         }
44                 }
45         } else {
46                 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
47         }
48 }
49
50 static void wakeup_setting_enabled_assistants_changed_cb(keynode_t* node, void* data)
51 {
52         MWR_LOGD("[ENTER]");
53         if (nullptr == node) return;
54
55         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
56         if (nullptr == settings) return;
57
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);
62                 if (value) {
63                         vector<string> previouslyEnabledAssistants = settings->get_enabled_assistants();
64                         vector<string> currentlyEnabledAssistants;
65                         string token;
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());
71                         }
72
73                         for (const auto& assistant : currentlyEnabledAssistants) {
74                                 if (!contains(previouslyEnabledAssistants, assistant)) {
75                                         newlyAddedAssistants.push_back(assistant);
76                                 }
77                         }
78                         for (const auto& assistant : previouslyEnabledAssistants) {
79                                 if (!contains(currentlyEnabledAssistants, assistant)) {
80                                         newlyRemovedAssistants.push_back(assistant);
81                                 }
82                         }
83                 }
84
85                 const auto& observers = settings->get_observers();
86                 for (const auto& observer : observers) {
87                         if (observer) {
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");
91                                         }
92                                 }
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");
96                                         }
97                                 }
98                         }
99                 }
100         } else {
101                 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
102         }
103 }
104
105 static void wakeup_setting_default_assistant_appid_changed_cb(keynode_t* node, void* data)
106 {
107         MWR_LOGD("[ENTER]");
108         if (nullptr == node) return;
109
110         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
111         if (nullptr == settings) return;
112
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) {
117                         if (observer) {
118                                 if (!observer->on_default_assistant_appid_changed(value)) {
119                                         LOGW("[Settings WARNING] One of the observer returned false");
120                                 }
121                         }
122                 }
123         } else {
124                 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
125         }
126 }
127
128 static void wakeup_setting_multiple_mode_changed_cb(keynode_t* node, void* data)
129 {
130         MWR_LOGD("[ENTER]");
131
132         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
133         if (nullptr == settings) return;
134
135         const auto& observers = settings->get_observers();
136         for (const auto& observer : observers) {
137                 if (observer) {
138                         if (!observer->on_multiple_mode_changed()) {
139                                 LOGW("[Settings WARNING] One of the observer returned false");
140                         }
141                 }
142         }
143 }
144
145 void CWakeupSettings::initialize()
146 {
147         int vconf_ret;
148         char *vconf_str;
149         int vconf_bool;
150         double vconf_double;
151
152         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
153         if (vconf_str) {
154                 mDefaultAssistantAppid = vconf_str;
155                 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
156                 for (const auto& observer : mObservers) {
157                         if (observer) {
158                                 if (!observer->on_default_assistant_appid_changed(vconf_str)) {
159                                         LOGW("[Settings WARNING] One of the observer returned false");
160                                 }
161                         }
162                 }
163                 free(vconf_str);
164                 vconf_str = nullptr;
165         }
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"));
170         }
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);
175         }
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"));
180         }
181         if (true == mMultipleMode) {
182                 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS);
183                 if (vconf_str) {
184                         string token;
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) {
191                                         if (observer) {
192                                                 if (!observer->on_assistant_enabled_info_changed(token.c_str(), true)) {
193                                                         LOGW("[Settings WARNING] One of the observer returned false");
194                                                 }
195                                         }
196                                 }
197                         }
198                         free(vconf_str);
199                         vconf_str = nullptr;
200                 }
201         }
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);
206         }
207         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_PRIORITY);
208         if (vconf_str) {
209                 string token;
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());
215                 }
216                 free(vconf_str);
217                 vconf_str = nullptr;
218         }
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);
223         }
224         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
225         if (vconf_str) {
226                 mVoiceInputLanguage = vconf_str;
227                 MWR_LOGD("voice input language : %s", mVoiceInputLanguage.c_str());
228                 for (const auto& observer : mObservers) {
229                         if (observer) {
230                                 if (!observer->on_voice_input_language_changed(vconf_str)) {
231                                         LOGW("[Settings WARNING] One of the observer returned false");
232                                 }
233                         }
234                 }
235                 free(vconf_str);
236                 vconf_str = nullptr;
237         }
238
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);
246         }
247         vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE,
248                 wakeup_setting_multiple_mode_changed_cb, this);
249 }
250
251 void CWakeupSettings::deinitialize()
252 {
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);
260         }
261         vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE,
262                 wakeup_setting_multiple_mode_changed_cb);
263 }
264
265 void CWakeupSettings::subscribe(ISettingsEventObserver *observer)
266 {
267         mObservers.push_back(observer);
268 }
269
270 void CWakeupSettings::unsubscribe(ISettingsEventObserver *observer)
271 {
272         auto iter = find(mObservers.begin(), mObservers.end(), observer);
273         if (iter != mObservers.end()) {
274                 mObservers.erase(iter);
275         }
276 }
277
278 const vector<ISettingsEventObserver*>& CWakeupSettings::get_observers()
279 {
280         return mObservers;
281 }
282
283 string CWakeupSettings::get_default_assistant_appid()
284 {
285         char *vconf_str;
286         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
287         MWR_LOGD("vconf default_assistant_appid : %s", vconf_str);
288         if (vconf_str) {
289                 mDefaultAssistantAppid = vconf_str;
290                 MWR_LOGD("member default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
291                 free(vconf_str);
292                 vconf_str = nullptr;
293         }
294
295         return mDefaultAssistantAppid;
296 }
297
298 void CWakeupSettings::set_default_assistant_appid(std::string appid)
299 {
300         if (appid.compare(get_default_assistant_appid()) == 0) {
301                 MWR_LOGE("Default assistant appid not changed, ignoring...");
302                 return;
303         }
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;
307 }
308
309 bool CWakeupSettings::get_ui_panel_enabled()
310 {
311         return mUiPanelEnabled;
312 }
313
314 float CWakeupSettings::get_conversation_timeout()
315 {
316         return mConversationTimeout;
317 }
318
319 bool CWakeupSettings::get_multiple_mode()
320 {
321         return mMultipleMode;
322 }
323
324 vector<string> CWakeupSettings::get_enabled_assistants()
325 {
326         return mEnabledAssistants;
327 }
328
329 float CWakeupSettings::get_wakeup_policy_delay()
330 {
331         return mWakeupPolicyDelay;
332 }
333
334 vector<string> CWakeupSettings::get_wakeup_policy_priority()
335 {
336         return mWakeupPolicyPriority;
337 }
338
339 float CWakeupSettings::get_streaming_duration_max()
340 {
341         return mStreamingDurationMax;
342 }
343
344 std::string CWakeupSettings::get_current_language(void)
345 {
346         std::string result{"en_US"};
347         char* language = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
348         if (language) {
349                 result = language;
350                 free(language);
351         }
352         return result;
353 }
354
355 } // wakeup
356 } // multiassistant