Deactivate wakeup engines with no activated assistant
[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                 vector<ISettingsEventObserver*> 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                 vector<ISettingsEventObserver*> 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                 vector<ISettingsEventObserver*> 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 void CWakeupSettings::initialize()
129 {
130         int vconf_ret;
131         char *vconf_str;
132         int vconf_bool;
133         double vconf_double;
134
135         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
136         if (vconf_str) {
137                 mDefaultAssistantAppid = vconf_str;
138                 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
139                 for (const auto& observer : mObservers) {
140                         if (observer) {
141                                 if (!observer->on_default_assistant_appid_changed(vconf_str)) {
142                                         LOGW("[Settings WARNING] One of the observer returned false");
143                                 }
144                         }
145                 }
146                 free(vconf_str);
147                 vconf_str = nullptr;
148         }
149         vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_UI_PANEL_ENABLED, &vconf_bool);
150         if (0 == vconf_ret) {
151                 mUiPanelEnabled = vconf_bool;
152                 MWR_LOGD("ui_panel_enabled : %s", (mUiPanelEnabled ? "true" : "false"));
153         }
154         vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_CONVERSATION_TIMEOUT, &vconf_double);
155         if (0 == vconf_ret) {
156                 mConversationTimeout = vconf_double;
157                 MWR_LOGD("conversation_timeout : %f", mConversationTimeout);
158         }
159         vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE, &vconf_bool);
160         if (0 == vconf_ret) {
161                 mMultipleMode = vconf_bool;
162                 MWR_LOGD("multiple_mode : %s", (mMultipleMode ? "true" : "false"));
163         }
164         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS);
165         if (vconf_str) {
166                 string token;
167                 istringstream iss(vconf_str);
168                 mEnabledAssistants.clear();
169                 while (getline(iss, token, ';')) {
170                         mEnabledAssistants.push_back(token);
171                         MWR_LOGD("enabled_assistants : %s", token.c_str());
172                         for (const auto& observer : mObservers) {
173                                 if (observer) {
174                                         if (!observer->on_assistant_enabled_info_changed(token.c_str(), true)) {
175                                                 LOGW("[Settings WARNING] One of the observer returned false");
176                                         }
177                                 }
178                         }
179                 }
180                 free(vconf_str);
181                 vconf_str = nullptr;
182         }
183         vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_DELAY, &vconf_double);
184         if (0 == vconf_ret) {
185                 mWakeupPolicyDelay = vconf_double;
186                 MWR_LOGD("conversation_timeout : %f", mWakeupPolicyDelay);
187         }
188         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_PRIORITY);
189         if (vconf_str) {
190                 string token;
191                 istringstream iss(vconf_str);
192                 mWakeupPolicyPriority.clear();
193                 while (getline(iss, token, ';')) {
194                         mWakeupPolicyPriority.push_back(token);
195                         MWR_LOGD("wakeup_policy_priority : %s", token.c_str());
196                 }
197                 free(vconf_str);
198                 vconf_str = nullptr;
199         }
200         vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_STREAMING_DURATION_MAX, &vconf_double);
201         if (0 == vconf_ret) {
202                 mStreamingDurationMax = vconf_double;
203                 MWR_LOGD("streaming_duration_max : %f", mStreamingDurationMax);
204         }
205         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
206         if (vconf_str) {
207                 mVoiceInputLanguage = vconf_str;
208                 MWR_LOGD("voice input language : %s", mVoiceInputLanguage.c_str());
209                 for (const auto& observer : mObservers) {
210                         if (observer) {
211                                 if (!observer->on_voice_input_language_changed(vconf_str)) {
212                                         LOGW("[Settings WARNING] One of the observer returned false");
213                                 }
214                         }
215                 }
216                 free(vconf_str);
217                 vconf_str = nullptr;
218         }
219
220         vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE,
221                 wakeup_setting_input_language_changed_cb, this);
222         vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS,
223                 wakeup_setting_enabled_assistants_changed_cb, this);
224         vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID,
225                 wakeup_setting_default_assistant_appid_changed_cb, this);
226 }
227
228 void CWakeupSettings::deinitialize()
229 {
230         vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE, NULL);
231         vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS, NULL);
232         vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID, NULL);
233 }
234
235 void CWakeupSettings::subscribe(ISettingsEventObserver *observer)
236 {
237         mObservers.push_back(observer);
238 }
239
240 void CWakeupSettings::unsubscribe(ISettingsEventObserver *observer)
241 {
242         auto iter = find(mObservers.begin(), mObservers.end(), observer);
243         if (iter != mObservers.end()) {
244                 mObservers.erase(iter);
245         }
246 }
247
248 vector<ISettingsEventObserver*> CWakeupSettings::get_observers()
249 {
250         return mObservers;
251 }
252
253 string CWakeupSettings::get_default_assistant_appid()
254 {
255         char *vconf_str;
256         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
257         MWR_LOGD("default_assistant_appid : %s", vconf_str);
258         if (vconf_str) {
259                 mDefaultAssistantAppid = vconf_str;
260                 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
261                 free(vconf_str);
262                 vconf_str = nullptr;
263         }
264
265         return mDefaultAssistantAppid;
266 }
267
268 bool CWakeupSettings::get_ui_panel_enabled()
269 {
270         return mUiPanelEnabled;
271 }
272
273 float CWakeupSettings::get_conversation_timeout()
274 {
275         return mConversationTimeout;
276 }
277
278 bool CWakeupSettings::get_multiple_mode()
279 {
280         return mMultipleMode;
281 }
282
283 vector<string> CWakeupSettings::get_enabled_assistants()
284 {
285         return mEnabledAssistants;
286 }
287
288 float CWakeupSettings::get_wakeup_policy_delay()
289 {
290         return mWakeupPolicyDelay;
291 }
292
293 vector<string> CWakeupSettings::get_wakeup_policy_priority()
294 {
295         return mWakeupPolicyPriority;
296 }
297
298 float CWakeupSettings::get_streaming_duration_max()
299 {
300         return mStreamingDurationMax;
301 }
302
303 std::string CWakeupSettings::get_current_language(void)
304 {
305         std::string result{"en_US"};
306         char* language = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
307         if (language) {
308                 result = language;
309                 free(language);
310         }
311         return result;
312 }
313
314 } // wakeup
315 } // multiassistant