0787f09f9da49d98070c89af9120bdfae2f4ac8a
[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 CWakeupSettings::CWakeupSettings()
13 {
14 }
15
16 CWakeupSettings::~CWakeupSettings()
17 {
18 }
19
20 static void wakeup_setting_input_language_changed_cb(keynode_t *node, void* data)
21 {
22         if (nullptr == node) return;
23
24         CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
25         if (nullptr == settings) return;
26
27         if (VCONF_TYPE_STRING == node->type) {
28                 const char* value = static_cast<const char*>(node->value.s);
29                 vector<ISettingsEventObserver*> observers = settings->get_observers();
30                 for (const auto& observer : observers) {
31                         if (observer) {
32                                 if (!observer->on_voice_input_language_changed(value)) {
33                                         LOGW("[Settings WARNING] One of the observer returned false");
34                                 }
35                         }
36                 }
37         } else {
38                 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
39         }
40 }
41
42 void CWakeupSettings::initialize()
43 {
44         int vconf_ret;
45         char *vconf_str;
46         int vconf_bool;
47         double vconf_double;
48
49         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
50         if (vconf_str) {
51                 mDefaultAssistantAppid = vconf_str;
52                 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
53                 free(vconf_str);
54                 vconf_str = nullptr;
55         }
56         vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_UI_PANEL_ENABLED, &vconf_bool);
57         if (0 == vconf_ret) {
58                 mUiPanelEnabled = vconf_bool;
59                 MWR_LOGD("ui_panel_enabled : %s", (mUiPanelEnabled ? "true" : "false"));
60         }
61         vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_CONVERSATION_TIMEOUT, &vconf_double);
62         if (0 == vconf_ret) {
63                 mConversationTimeout = vconf_double;
64                 MWR_LOGD("conversation_timeout : %f", mConversationTimeout);
65         }
66         vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE, &vconf_bool);
67         if (0 == vconf_ret) {
68                 mMultipleMode = vconf_bool;
69                 MWR_LOGD("multiple_mode : %s", (mMultipleMode ? "true" : "false"));
70         }
71         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS);
72         if (vconf_str) {
73                 string token;
74                 istringstream iss(vconf_str);
75                 mEnabledAssistants.clear();
76                 while (getline(iss, token, ';')) {
77                         mEnabledAssistants.push_back(token);
78                         MWR_LOGD("enabled_assistants : %s", token.c_str());
79                 }
80                 free(vconf_str);
81                 vconf_str = nullptr;
82         }
83         vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_DELAY, &vconf_double);
84         if (0 == vconf_ret) {
85                 mWakeupPolicyDelay = vconf_double;
86                 MWR_LOGD("conversation_timeout : %f", mWakeupPolicyDelay);
87         }
88         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_PRIORITY);
89         if (vconf_str) {
90                 string token;
91                 istringstream iss(vconf_str);
92                 mWakeupPolicyPriority.clear();
93                 while (getline(iss, token, ';')) {
94                         mWakeupPolicyPriority.push_back(token);
95                         MWR_LOGD("wakeup_policy_priority : %s", token.c_str());
96                 }
97                 free(vconf_str);
98                 vconf_str = nullptr;
99         }
100         vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_STREAMING_DURATION_MAX, &vconf_double);
101         if (0 == vconf_ret) {
102                 mStreamingDurationMax = vconf_double;
103                 MWR_LOGD("streaming_duration_max : %f", mStreamingDurationMax);
104         }
105
106         vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE,
107                 wakeup_setting_input_language_changed_cb, this);
108 }
109
110 void CWakeupSettings::deinitialize()
111 {
112         vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE, NULL);
113 }
114
115 void CWakeupSettings::subscribe(ISettingsEventObserver *observer)
116 {
117         mObservers.push_back(observer);
118 }
119
120 void CWakeupSettings::unsubscribe(ISettingsEventObserver *observer)
121 {
122         auto iter = find(mObservers.begin(), mObservers.end(), observer);
123         if (iter != mObservers.end()) {
124                 mObservers.erase(iter);
125         }
126 }
127
128 vector<ISettingsEventObserver*> CWakeupSettings::get_observers()
129 {
130         return mObservers;
131 }
132
133 string CWakeupSettings::get_default_assistant_appid()
134 {
135         char *vconf_str;
136         vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
137         MWR_LOGD("default_assistant_appid : %s", vconf_str);
138         if (vconf_str) {
139                 mDefaultAssistantAppid = vconf_str;
140                 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
141                 free(vconf_str);
142                 vconf_str = nullptr;
143         }
144
145         return mDefaultAssistantAppid;
146 }
147
148 bool CWakeupSettings::get_ui_panel_enabled()
149 {
150         return mUiPanelEnabled;
151 }
152
153 float CWakeupSettings::get_conversation_timeout()
154 {
155         return mConversationTimeout;
156 }
157
158 bool CWakeupSettings::get_multiple_mode()
159 {
160         return mMultipleMode;
161 }
162
163 vector<string> CWakeupSettings::get_enabled_assistants()
164 {
165         return mEnabledAssistants;
166 }
167
168 float CWakeupSettings::get_wakeup_policy_delay()
169 {
170         return mWakeupPolicyDelay;
171 }
172
173 vector<string> CWakeupSettings::get_wakeup_policy_priority()
174 {
175         return mWakeupPolicyPriority;
176 }
177
178 float CWakeupSettings::get_streaming_duration_max()
179 {
180         return mStreamingDurationMax;
181 }
182
183 std::string CWakeupSettings::get_current_language(void)
184 {
185         std::string result{"en_US"};
186         char* language = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
187         if (language) {
188                 result = language;
189                 free(language);
190         }
191         return result;
192 }
193
194 } // wakeup
195 } // multiassistant