Apply string localization for web
[platform/framework/native/appfw.git] / src / system-server / setting / providers / FSys_SettingGeneralProvider.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FSys_SettingGeneralProvider.cpp
19  * @brief       This is the implementation for the _SettingGeneralProvider class.
20  */
21 #include <unique_ptr.h>
22
23 #include <Eina.h>
24 #include <Elementary.h>
25
26 #include <runtime_info.h>
27
28 #include <FBase_StringConverter.h>
29
30 #include <FApp.h>
31 #include <FBase.h>
32 #include <FBaseSysLog.h>
33
34 #include "FSys_SettingInfo.h"
35 #include "FSys_SettingGeneralProvider.h"
36
37 using namespace std;
38
39 using namespace Tizen::App;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Utility;
42
43 namespace Tizen { namespace System
44 {
45 static const wchar_t* _DEVICE_NAME = L"http://tizen.org/setting/device_name";
46
47 static const wchar_t* _MOTION_UI = L"http://tizen.org/setting/motion.ui";
48
49 static const wchar_t* _CONTACT_ORDER_FIRSTNAME = L"http://tizen.org/setting/contacts.order.firstname";
50 static const int _CONTACT_ORDER_FIRSTNAME_FIRSTLAST = 0;
51 static const int _CONTACT_ORDER_FIRSTNAME_LASTFIRST = 1;
52
53 static const wchar_t* _DEVELOPER_USBDEBUGGING = L"http://tizen.org/setting/developer.usb_debugging";
54
55 static const wchar_t* _BATTERY_FORMAT_PERCENTAGE = L"http://tizen.org/setting/battery.format.percentage";
56
57 static const wchar_t* _GRAPHICS_GPU_RENDERING = L"http://tizen.org/setting/graphics.gpu.rendering";
58 static const char* _GRAPHICS_GPU_RENDERING_HW = "opengl_x11";
59 static const char* _GRAPHICS_GPU_RENDERING_SW = "software_x11";
60
61 struct charDeleter
62 {
63         void operator()(char* pValue)
64         {
65                 if(pValue != null)
66                 {
67                         free(pValue);
68                         pValue = null;
69                 }
70         }
71 };
72
73 _SettingGeneralProvider::_SettingGeneralProvider()
74         : currentMode(0)
75 {
76         int errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR, SettingEventVConf, null);
77         if(errorCode != 0)
78         {
79                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_DEVICE_NAME_STR event listener.");
80         }
81
82         errorCode = vconf_notify_key_changed(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, SettingEventVConf, null);
83         if(errorCode != 0)
84         {
85                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER event listener.");
86         }
87
88         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, SettingEventVConf, null);
89         if(errorCode != 0)
90         {
91                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL event listener.");
92         }
93
94         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, SettingEventVConf, null);
95         if(errorCode != 0)
96         {
97                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL event listener.");
98         }
99
100         errorCode = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION, SettingEventSettingInfo, null);
101         if(errorCode != 0)
102         {
103                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION event listener.");
104         }
105         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, SettingEventVConf, null);
106         if(errorCode != 0)
107         {
108                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT event listener.");
109         }
110 }
111
112
113 _SettingGeneralProvider::~_SettingGeneralProvider()
114 {
115         int errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR, SettingEventVConf);
116         if(errorCode != 0)
117         {
118                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_DEVICE_NAME_STR event listener.");
119         }
120
121         errorCode = vconf_ignore_key_changed(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, SettingEventVConf);
122         if(errorCode != 0)
123         {
124                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER event listener.");
125         }
126
127         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, SettingEventVConf);
128         if(errorCode != 0)
129         {
130                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL event listener.");
131         }
132
133         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, SettingEventVConf);
134         if(errorCode != 0)
135         {
136                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL event listener.");
137         }
138
139         errorCode = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION);
140         if(errorCode != 0)
141         {
142                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION event listener.");
143         }
144         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, SettingEventVConf);
145         if(errorCode != 0)
146         {
147                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT event listener.");
148         }
149 }
150
151 result
152 _SettingGeneralProvider::GetValue(const String& key, bool& value)
153 {
154         int errorCode = 0;
155         result r = E_OBJ_NOT_FOUND;
156
157         SysLog(NID_SYS, "Key: %ls", key.GetPointer());
158         if (key == _MOTION_UI)
159         {
160                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
161                 r = E_SUCCESS;
162         
163                 int motion = 0;
164                 errorCode = vconf_get_bool(VCONFKEY_SETAPPL_MOTION_ACTIVATION, &motion);
165                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_MOTION_ACTIVATION.");
166                 value = motion;
167                 return r;
168         }
169         else if (key == _CONTACT_ORDER_FIRSTNAME)
170         {
171                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support Contact ordering feature.");
172                 r = E_SUCCESS;
173
174                 int contactOrder = 0;
175                 errorCode = vconf_get_int(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, &contactOrder);
176                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER.");
177
178                 if (contactOrder == _CONTACT_ORDER_FIRSTNAME_FIRSTLAST)
179                 {
180                         value = true;
181                 }
182                 else if (contactOrder == _CONTACT_ORDER_FIRSTNAME_LASTFIRST)
183                 {
184                         value = false;
185                 }
186                 else
187                 {
188                         return E_SYSTEM;
189                 }
190         }
191         else if (key == _DEVELOPER_USBDEBUGGING)
192         {
193                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support USB debugging feature.");
194                 r = E_SUCCESS;
195
196                 int usbMode = 0;
197                 errorCode = vconf_get_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, &usbMode);
198                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL.");
199                 value = usbMode;
200         }
201         else if(key == _BATTERY_FORMAT_PERCENTAGE)
202         {
203                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support battery format feature.");
204                 r = E_SUCCESS;
205
206                 int batteryFormat = 0;
207                 errorCode = vconf_get_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, &batteryFormat);
208                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL.");
209                 value = batteryFormat;
210         }
211         else if(key == _GRAPHICS_GPU_RENDERING)
212         {
213                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support GPU rendering feature.");
214                 r = E_SUCCESS;
215
216                 int gpu_rendering = 0;
217                 errorCode = vconf_get_int(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, &gpu_rendering);
218                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT.");
219                 value = (gpu_rendering == SETTING_RENDERING_ENGINE_HW);
220         }
221         return r;
222 }
223
224 result
225 _SettingGeneralProvider::SetValue(const String& key, const bool value)
226 {
227         int errorCode = 0;
228         result r = E_OBJ_NOT_FOUND;
229         SysLog(NID_SYS, "Key: %ls, %d", key.GetPointer(), value);
230         if(key == _MOTION_UI)
231         {
232                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
233                 r = E_SUCCESS;
234
235                 errorCode = vconf_set_bool(VCONFKEY_SETAPPL_MOTION_ACTIVATION, value);
236                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set the VCONFKEY_SETAPPL_MOTION_ACTIVATION.");
237         }
238         else if (key == _CONTACT_ORDER_FIRSTNAME)
239         {
240                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
241                 r = E_SUCCESS;
242
243                 int contactOrder = 0;
244
245                 if (value)
246                 {
247                         contactOrder = _CONTACT_ORDER_FIRSTNAME_FIRSTLAST;
248                 }
249                 else
250                 {
251                         contactOrder = _CONTACT_ORDER_FIRSTNAME_LASTFIRST;
252                 }
253                 errorCode = vconf_set_int(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, contactOrder);
254                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER vconf");
255         }
256         else if(key == _BATTERY_FORMAT_PERCENTAGE)
257         {
258                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support battery format feature.");
259                 r = E_SUCCESS;
260                 errorCode = vconf_set_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, value);
261                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set the VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL.");
262         }
263         else if(key == _GRAPHICS_GPU_RENDERING)
264         {
265                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support GPU rendering feature.");
266                 r = E_SUCCESS;
267
268                 if(value == true)
269                 {
270                         elm_config_preferred_engine_set(_GRAPHICS_GPU_RENDERING_HW);
271                         errorCode = vconf_set_int(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, SETTING_RENDERING_ENGINE_HW);
272                 }
273                 else
274                 {
275                         elm_config_preferred_engine_set(_GRAPHICS_GPU_RENDERING_SW);
276                         errorCode = vconf_set_int(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, SETTING_RENDERING_ENGINE_SW);
277                 }
278
279                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT.");
280         }
281         return r;
282 }
283
284 result
285 _SettingGeneralProvider::SetValueForPrivilegedKey(const String& key, bool value)
286 {
287         result r = E_OBJ_NOT_FOUND;
288         if(key == _DEVELOPER_USBDEBUGGING)
289         {
290                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
291                 r = E_SUCCESS;
292
293                 int errorCode = vconf_set_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, value);
294                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL vconf");
295         }
296         return r;
297 }
298
299 result
300 _SettingGeneralProvider::GetValue(const String& key, String& value)
301 {
302         result r = E_OBJ_NOT_FOUND;
303         if (key == _DEVICE_NAME)
304         {
305                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
306                 r = E_SUCCESS;
307
308                 unique_ptr<char, charDeleter> pDeviceName(vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR));
309                 SysTryReturnResult(NID_SYS, pDeviceName.get() != null, E_SYSTEM, "It is failed to get device name.");
310
311                 r = StringUtil::Utf8ToString(pDeviceName.get(), value);
312                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "[%s] StringUtil::Utf8ToString is failed", GetErrorMessage(r));
313         }
314         return r;
315 }
316
317 result
318 _SettingGeneralProvider::SetValue(const String& key, const String value)
319 {
320         int errorCode = 0;
321         result r = E_OBJ_NOT_FOUND;
322
323         if(key == _DEVICE_NAME)
324         {
325                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
326                 r = E_SUCCESS;
327
328                 SysTryReturnResult(NID_SYS, value.GetLength() > 0 && value.GetLength() <= 128, E_INVALID_ARG,  "Device name is too long")
329                 unique_ptr<char []> deviceName(_StringConverter::CopyToCharArrayN(value));
330                 SysTryReturnResult(NID_SYS, deviceName.get() != null, E_SYSTEM, "It is failed to convert String to string");
331
332                 errorCode = vconf_set_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR, deviceName.get());
333                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_DEVICE_NAME_STR vconf");
334         }
335         return r;
336 }
337
338 bool
339 _SettingGeneralProvider::HasKey(const Tizen::Base::String& key)
340 {
341         if(key == _DEVICE_NAME)
342         {
343                 return true;
344         }
345         else if(key == _MOTION_UI)
346         {
347                 return true;
348         }
349         else if(key == _CONTACT_ORDER_FIRSTNAME)
350         {
351                 return true;
352         }
353         else if(key == _DEVELOPER_USBDEBUGGING)
354         {
355 #ifndef _OSP_EMUL_
356                 return true;
357 #else
358                 return false;
359 #endif
360         }
361         else if(key == _GRAPHICS_GPU_RENDERING)
362         {
363                 return true;
364         }
365         else if(key == _BATTERY_FORMAT_PERCENTAGE)
366         {
367                 return true;
368         }
369         return false;
370 }
371
372 void
373 _SettingGeneralProvider::SettingEventSettingInfo(system_settings_key_e key, void* userData)
374 {
375         _SettingInfo* pSettingInfo = _SettingInfo::GetInstance();
376         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_SYSTEM, "_SettingInfo is not ready.");
377         String settingKey;
378         SysLog(NID_SYS, "SettingEvent callback is called");
379
380         if(key == SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION)
381         {
382                 settingKey.Append(_MOTION_UI);
383         }
384         else
385         {
386                 return;
387         }
388
389         result r = pSettingInfo->AnnounceSettingEvent(settingKey);
390         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send the event[%ls].", settingKey.GetPointer());
391 }
392
393 void
394 _SettingGeneralProvider::SettingEventVConf(keynode_t* node, void* userData)
395 {
396         _SettingInfo* pSettingInfo = _SettingInfo::GetInstance();
397         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_SYSTEM, "_SettingInfo is not ready.");
398         String settingKey;
399
400         if (strcmp(VCONFKEY_SETAPPL_DEVICE_NAME_STR, vconf_keynode_get_name(node)) == 0)
401         {
402                 settingKey.Append(_DEVICE_NAME);
403         }
404         else if (strcmp(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, vconf_keynode_get_name(node)) == 0)
405         {
406                 settingKey.Append(_CONTACT_ORDER_FIRSTNAME);
407         }
408         else if (strcmp(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, vconf_keynode_get_name(node)) == 0)
409         {
410                 settingKey.Append(_DEVELOPER_USBDEBUGGING);
411         }
412         else if (strcmp(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, vconf_keynode_get_name(node)) == 0)
413         {
414                 settingKey.Append(_BATTERY_FORMAT_PERCENTAGE);
415         }
416         else if (strcmp(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, vconf_keynode_get_name(node)) == 0)
417         {
418                 settingKey.Append(_GRAPHICS_GPU_RENDERING);
419         }
420         else
421         {
422                 return;
423         }
424         result r = pSettingInfo->AnnounceSettingEvent(settingKey);
425         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send the event[%ls].", settingKey.GetPointer());
426 }
427
428 }}