Add Setting feature on system-server lib
[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
46 static const int _NUM_OF_GENERAL_PROVIDER = 6;
47
48 static const wchar_t* _DEVICE_NAME = L"device_name";
49
50 static const wchar_t* _MOTION_UI = L"motion.ui";
51
52 static const wchar_t* _CONTACT_ORDER_FIRSTNAME = L"contacts.order.firstname";
53 static const int _CONTACT_ORDER_FIRSTNAME_FIRSTLAST = 0;
54 static const int _CONTACT_ORDER_FIRSTNAME_LASTFIRST = 1;
55
56 static const wchar_t* _DEVELOPER_USBDEBUGGING = L"developer.usb_debugging";
57
58 static const wchar_t* _BATTERY_FORMAT_PERCENTAGE = L"battery.format.percentage";
59
60 static const wchar_t* _GRAPHICS_GPU_RENDERING = L"graphics.gpu.rendering";
61 static const char* _GRAPHICS_GPU_RENDERING_HW = "opengl_x11";
62 static const char* _GRAPHICS_GPU_RENDERING_SW = "software_x11";
63
64 struct charDeleter
65 {
66         void operator()(char* pValue)
67         {
68                 if(pValue != null)
69                 {
70                         free(pValue);
71                         pValue = null;
72                 }
73         }
74 };
75
76 _SettingGeneralProvider::_SettingGeneralProvider()
77         : currentMode(0)
78 {
79         int errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR, SettingEventVConf, null);
80         if(errorCode != 0)
81         {
82                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_DEVICE_NAME_STR event listener.");
83         }
84
85         errorCode = vconf_notify_key_changed(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, SettingEventVConf, null);
86         if(errorCode != 0)
87         {
88                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER event listener.");
89         }
90
91         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, SettingEventVConf, null);
92         if(errorCode != 0)
93         {
94                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL event listener.");
95         }
96
97         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, SettingEventVConf, null);
98         if(errorCode != 0)
99         {
100                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL event listener.");
101         }
102
103         errorCode = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION, SettingEventSettingInfo, null);
104         if(errorCode != 0)
105         {
106                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION event listener.");
107         }
108         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, SettingEventVConf, null);
109         if(errorCode != 0)
110         {
111                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT event listener.");
112         }
113 }
114
115
116 _SettingGeneralProvider::~_SettingGeneralProvider()
117 {
118         int errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR, SettingEventVConf);
119         if(errorCode != 0)
120         {
121                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_DEVICE_NAME_STR event listener.");
122         }
123
124         errorCode = vconf_ignore_key_changed(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, SettingEventVConf);
125         if(errorCode != 0)
126         {
127                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER event listener.");
128         }
129
130         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, SettingEventVConf);
131         if(errorCode != 0)
132         {
133                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL event listener.");
134         }
135
136         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, SettingEventVConf);
137         if(errorCode != 0)
138         {
139                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL event listener.");
140         }
141
142         errorCode = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION);
143         if(errorCode != 0)
144         {
145                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION event listener.");
146         }
147         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, SettingEventVConf);
148         if(errorCode != 0)
149         {
150                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT event listener.");
151         }
152 }
153
154 result
155 _SettingGeneralProvider::GetValue(const String& key, bool& value)
156 {
157         int errorCode = 0;
158         result r = E_OBJ_NOT_FOUND;
159
160         SysLog(NID_SYS, "Key: %ls", key.GetPointer());
161         if (key == _MOTION_UI)
162         {
163                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
164                 r = E_SUCCESS;
165         
166                 int motion = 0;
167                 errorCode = vconf_get_bool(VCONFKEY_SETAPPL_MOTION_ACTIVATION, &motion);
168                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_MOTION_ACTIVATION.");
169                 value = motion;
170                 return r;
171         }
172         else if (key == _CONTACT_ORDER_FIRSTNAME)
173         {
174                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support Contact ordering feature.");
175                 r = E_SUCCESS;
176
177                 int contactOrder = 0;
178                 errorCode = vconf_get_int(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, &contactOrder);
179                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER.");
180
181                 if (contactOrder == _CONTACT_ORDER_FIRSTNAME_FIRSTLAST)
182                 {
183                         value = true;
184                 }
185                 else if (contactOrder == _CONTACT_ORDER_FIRSTNAME_LASTFIRST)
186                 {
187                         value = false;
188                 }
189                 else
190                 {
191                         return E_SYSTEM;
192                 }
193         }
194         else if (key == _DEVELOPER_USBDEBUGGING)
195         {
196                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support USB debugging feature.");
197                 r = E_SUCCESS;
198
199                 int usbMode = 0;
200                 errorCode = vconf_get_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, &usbMode);
201                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL.");
202                 value = usbMode;
203         }
204         else if(key == _BATTERY_FORMAT_PERCENTAGE)
205         {
206                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support battery format feature.");
207                 r = E_SUCCESS;
208
209                 int batteryFormat = 0;
210                 errorCode = vconf_get_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, &batteryFormat);
211                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL.");
212                 value = batteryFormat;
213         }
214         else if(key == _GRAPHICS_GPU_RENDERING)
215         {
216                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support GPU rendering feature.");
217                 r = E_SUCCESS;
218
219                 int gpu_rendering = 0;
220                 errorCode = vconf_get_int(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, &gpu_rendering);
221                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT.");
222                 value = (gpu_rendering == SETTING_RENDERING_ENGINE_HW);
223         }
224         return r;
225 }
226
227 result
228 _SettingGeneralProvider::SetValue(const String& key, const bool value)
229 {
230         int errorCode = 0;
231         result r = E_OBJ_NOT_FOUND;
232         SysLog(NID_SYS, "Key: %ls, %d", key.GetPointer(), value);
233         if(key == _MOTION_UI)
234         {
235                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
236                 r = E_SUCCESS;
237
238                 errorCode = vconf_set_bool(VCONFKEY_SETAPPL_MOTION_ACTIVATION, value);
239                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set the VCONFKEY_SETAPPL_MOTION_ACTIVATION.");
240         }
241         else if (key == _CONTACT_ORDER_FIRSTNAME)
242         {
243                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
244                 r = E_SUCCESS;
245
246                 int contactOrder = 0;
247
248                 if (value)
249                 {
250                         contactOrder = _CONTACT_ORDER_FIRSTNAME_FIRSTLAST;
251                 }
252                 else
253                 {
254                         contactOrder = _CONTACT_ORDER_FIRSTNAME_LASTFIRST;
255                 }
256                 errorCode = vconf_set_int(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, contactOrder);
257                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER vconf");
258         }
259         else if(key == _BATTERY_FORMAT_PERCENTAGE)
260         {
261                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support battery format feature.");
262                 r = E_SUCCESS;
263                 errorCode = vconf_set_bool(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, value);
264                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set the VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL.");
265         }
266         else if(key == _GRAPHICS_GPU_RENDERING)
267         {
268                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support GPU rendering feature.");
269                 r = E_SUCCESS;
270
271                 if(value == true)
272                 {
273                         elm_config_preferred_engine_set(_GRAPHICS_GPU_RENDERING_HW);
274                         errorCode = vconf_set_int(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, SETTING_RENDERING_ENGINE_HW);
275                 }
276                 else
277                 {
278                         elm_config_preferred_engine_set(_GRAPHICS_GPU_RENDERING_SW);
279                         errorCode = vconf_set_int(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, SETTING_RENDERING_ENGINE_SW);
280                 }
281
282                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get the VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT.");
283         }
284         return r;
285 }
286
287 result
288 _SettingGeneralProvider::SetValueForPrivilegedKey(const String& key, bool value)
289 {
290         result r = E_OBJ_NOT_FOUND;
291         if(key == _DEVELOPER_USBDEBUGGING)
292         {
293                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
294                 r = E_SUCCESS;
295
296                 int errorCode = vconf_set_bool(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, value);
297                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL vconf");
298         }
299         return r;
300 }
301
302 result
303 _SettingGeneralProvider::GetValue(const String& key, String& value)
304 {
305         result r = E_OBJ_NOT_FOUND;
306         if (key == _DEVICE_NAME)
307         {
308                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
309                 r = E_SUCCESS;
310
311                 unique_ptr<char, charDeleter> pDeviceName(vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR));
312                 SysTryReturnResult(NID_SYS, pDeviceName.get() != null, E_SYSTEM, "It is failed to get device name.");
313
314                 r = StringUtil::Utf8ToString(pDeviceName.get(), value);
315                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "[%s] StringUtil::Utf8ToString is failed", GetErrorMessage(r));
316         }
317         return r;
318 }
319
320 result
321 _SettingGeneralProvider::SetValue(const String& key, const String value)
322 {
323         int errorCode = 0;
324         result r = E_OBJ_NOT_FOUND;
325
326         if(key == _DEVICE_NAME)
327         {
328                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
329                 r = E_SUCCESS;
330
331                 SysTryReturnResult(NID_SYS, value.GetLength() > 0 && value.GetLength() <= 128, E_INVALID_ARG,  "Device name is too long")
332                 unique_ptr<char []> deviceName(_StringConverter::CopyToCharArrayN(value));
333                 SysTryReturnResult(NID_SYS, deviceName.get() != null, E_SYSTEM, "It is failed to convert String to string");
334
335                 errorCode = vconf_set_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR, deviceName.get());
336                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_DEVICE_NAME_STR vconf");
337         }
338         return r;
339 }
340
341 bool
342 _SettingGeneralProvider::HasKey(const Tizen::Base::String& key)
343 {
344         if(key == _DEVICE_NAME)
345         {
346                 return true;
347         }
348         else if(key == _MOTION_UI)
349         {
350                 return true;
351         }
352         else if(key == _CONTACT_ORDER_FIRSTNAME)
353         {
354                 return true;
355         }
356         else if(key == _DEVELOPER_USBDEBUGGING)
357         {
358 #ifndef _OSP_EMUL_
359                 return true;
360 #else
361                 return false;
362 #endif
363         }
364         else if(key == _GRAPHICS_GPU_RENDERING)
365         {
366                 return true;
367         }
368         else if(key == _BATTERY_FORMAT_PERCENTAGE)
369         {
370                 return true;
371         }
372         return false;
373 }
374
375 void
376 _SettingGeneralProvider::SettingEventSettingInfo(system_settings_key_e key, void* userData)
377 {
378         _SettingInfo* pSettingInfo = _SettingInfo::GetInstance();
379         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_SYSTEM, "_SettingInfo is not ready.");
380         String settingKey;
381
382         if(key == SYSTEM_SETTINGS_KEY_MOTION_ACTIVATION)
383         {
384                 settingKey.Append(_MOTION_UI);
385         }
386         else
387         {
388                 return;
389         }
390
391         result r = pSettingInfo->AnnounceSettingEvent(settingKey);
392         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send the event[%ls].", settingKey.GetPointer());
393 }
394
395 void
396 _SettingGeneralProvider::SettingEventVConf(keynode_t* node, void* userData)
397 {
398         _SettingInfo* pSettingInfo = _SettingInfo::GetInstance();
399         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_SYSTEM, "_SettingInfo is not ready.");
400         String settingKey;
401
402         if (strcmp(VCONFKEY_SETAPPL_DEVICE_NAME_STR, vconf_keynode_get_name(node)) == 0)
403         {
404                 settingKey.Append(_DEVICE_NAME);
405         }
406         else if (strcmp(VCONFKEY_CONTACTS_SVC_NAME_DISPLAY_ORDER, vconf_keynode_get_name(node)) == 0)
407         {
408                 settingKey.Append(_CONTACT_ORDER_FIRSTNAME);
409         }
410         else if (strcmp(VCONFKEY_SETAPPL_USB_DEBUG_MODE_BOOL, vconf_keynode_get_name(node)) == 0)
411         {
412                 settingKey.Append(_DEVELOPER_USBDEBUGGING);
413         }
414         else if (strcmp(VCONFKEY_SETAPPL_BATTERY_PERCENTAGE_BOOL, vconf_keynode_get_name(node)) == 0)
415         {
416                 settingKey.Append(_BATTERY_FORMAT_PERCENTAGE);
417         }
418         else if (strcmp(VCONFKEY_SETAPPL_DEFAULT_RENDERING_ENGINE_TYPE_INT, vconf_keynode_get_name(node)) == 0)
419         {
420                 settingKey.Append(_GRAPHICS_GPU_RENDERING);
421         }
422         else
423         {
424                 return;
425         }
426         result r = pSettingInfo->AnnounceSettingEvent(settingKey);
427         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send the event[%ls].", settingKey.GetPointer());
428 }
429
430 }}