b64e1bf3617c72db7dfbfc6a68bfc8da081c9b43
[platform/framework/native/appfw.git] / src / system-server / setting / providers / FSys_SettingScreenProvider.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_SettingScreenProvider.cpp
19  * @brief       This is the implementation for the _SettingScreenProvider class.
20  */
21
22 #include <unique_ptr.h>
23 #include <device.h>
24
25 #include <FApp.h>
26 #include <FIo.h>
27 #include <FBase.h>
28 #include <FBaseSysLog.h>
29
30 #include <FBase_StringConverter.h>
31
32 #include "FSys_SettingInfo.h"
33 #include "FSys_SettingScreenProvider.h"
34
35 using namespace std;
36
37 using namespace Tizen::App;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Utility;
40 using namespace Tizen::Io;
41
42 namespace Tizen { namespace System
43 {
44
45 static const wchar_t* _SCREEN_WALLPAPER = L"http://tizen.org/setting/screen.wallpaper";
46 static const wchar_t* _SCREEN_WALLPAPER_LOCK = L"http://tizen.org/setting/screen.wallpaper.lock";
47 static const wchar_t* _SCREEN_BRIGHTNESS = L"http://tizen.org/setting/screen.brightness";
48 static const wchar_t* _SCREEN_BRIGHTNESS_AUTO = L"http://tizen.org/setting/screen.brightness.auto";
49 static const wchar_t* _SCREEN_BACKLIGHT_TIME = L"http://tizen.org/setting/screen.backlight.time";
50 static const wchar_t* _SCREEN_ROTATION_AUTO = L"http://tizen.org/setting/screen.rotation.auto";
51 static const wchar_t* _SCREEN_MODE = L"http://tizen.org/setting/screen.mode";
52
53 static const wchar_t* _SCREEN_MODE_DYNAMIC = L"Dynamic";
54 static const wchar_t* _SCREEN_MODE_STANDARD = L"Standard";
55 static const wchar_t* _SCREEN_MODE_NATURAL = L"Natural";
56 static const wchar_t* _SCREEN_MODE_MOVIE = L"Movie";
57 static const char* _SCREEN_MODE_VCONFKEY = VCONFKEY_SETAPPL_PREFIX"/screenmode/selected_name";
58
59 struct charDeleter
60 {
61         void operator()(char* pValue)
62         {
63                 if(pValue != null)
64                 {
65                         free(pValue);
66                         pValue = null;
67                 }
68         }
69 };
70
71 _SettingScreenProvider::_SettingScreenProvider()
72 {
73         int errorCode = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, SettingEventSettingInfo, null);
74         if(errorCode != 0)
75         {
76                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register wallpaper event listener");
77         }
78
79         errorCode = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, SettingEventSettingInfo, null);
80         if(errorCode != 0)
81         {
82                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register lock wallpaper event listener");
83         }
84
85         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, SettingEventVConf, null);
86         if(errorCode != 0)
87         {
88                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_LCD_BRIGHTNESS event listener");
89         }
90
91         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, SettingEventVConf, null);
92         if(errorCode != 0)
93         {
94                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT event listener");
95         }
96
97         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL, SettingEventVConf, null);
98         if(errorCode != 0)
99         {
100                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL event listener");
101         }
102
103         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, SettingEventVConf, null);
104         if(errorCode != 0)
105         {
106                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL event listener");
107         }
108
109         errorCode = vconf_notify_key_changed(_SCREEN_MODE_VCONFKEY, SettingEventVConf, null);
110         if(errorCode != 0)
111         {
112                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register _SCREEN_MODE_VCONFKEY event listener");
113         }
114 }
115
116
117 _SettingScreenProvider::~_SettingScreenProvider()
118 {
119         int errorCode = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN);
120         if(errorCode != 0)
121         {
122                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister wallpaper event listener");
123         }
124
125         errorCode = system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN);
126         if(errorCode != 0)
127         {
128                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister lock wallpaper event listener");
129         }
130
131         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, SettingEventVConf);
132         if(errorCode != 0)
133         {
134                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_LCD_BRIGHTNESS event listener");
135         }
136
137         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, SettingEventVConf);
138         if(errorCode != 0)
139         {
140                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT event listener");
141         }
142
143         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL, SettingEventVConf);
144         if(errorCode != 0)
145         {
146                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL event listener");
147         }
148
149         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, SettingEventVConf);
150         if(errorCode != 0)
151         {
152                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL event listener");
153         }
154
155         errorCode = vconf_ignore_key_changed(_SCREEN_MODE_VCONFKEY, SettingEventVConf);
156         if(errorCode != 0)
157         {
158                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister _SCREEN_MODE_VCONFKEY event listener");
159         }
160 }
161
162 result
163 _SettingScreenProvider::GetValue(const String& key, bool& value)
164 {
165         int errorCode = 0;
166         result r = E_OBJ_NOT_FOUND;
167         if (key == _SCREEN_BRIGHTNESS_AUTO)
168         {
169                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support brightness auto control feature.");
170                 r = E_SUCCESS;
171                 int brightnessAuto = 0;
172                 errorCode = vconf_get_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, &brightnessAuto);
173                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get on SETAPPL_BRIGHTNESS_AUTOMATIC_INT vconf");
174
175                 if(brightnessAuto == 0)
176                 {
177                         value = false;
178                 }
179                 else
180                 {
181                         value = true;
182                 }
183         }
184         else if (key == _SCREEN_ROTATION_AUTO)
185         {
186                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support auto ratation feature.");
187                 r = E_SUCCESS;
188                 int autoRotate = false;
189                 errorCode = vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &autoRotate);
190                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get on VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL vconf");
191                 if(autoRotate == 1)
192                 {
193                         value = true;
194                 }
195                 else
196                 {
197                         value = false;
198                 }
199         }
200         return r;
201 }
202
203 result
204 _SettingScreenProvider::SetValue(const String& key, const bool value)
205 {
206         int errorCode = 0;
207         result r = E_OBJ_NOT_FOUND;
208
209         if(key == _SCREEN_BRIGHTNESS_AUTO)
210         {
211                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support brightness auto control feature.");
212                 r = E_SUCCESS;
213                 if(value == true)
214                 {
215                         errorCode = vconf_set_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, SETTING_BRIGHTNESS_AUTOMATIC_ON);
216                 }
217                 else
218                 {
219                         errorCode = vconf_set_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, SETTING_BRIGHTNESS_AUTOMATIC_OFF);
220                 }
221                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on SETAPPL_BRIGHTNESS_AUTOMATIC_INT vconf");
222         }
223         else if(key == _SCREEN_ROTATION_AUTO)
224         {
225                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support auto ratation feature.");
226                 r = E_SUCCESS;
227                 errorCode = vconf_set_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, value);
228                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL vconf");
229         }
230         return r;
231 }
232
233 result
234 _SettingScreenProvider::GetValue(const String& key, int& value)
235 {
236         int errorCode = 0;
237         result r = E_OBJ_NOT_FOUND;
238         if (key == _SCREEN_BRIGHTNESS)
239         {
240                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support brightness feature.");
241                 r = E_SUCCESS;
242                 int brightness = 0;
243                 errorCode = vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, &brightness);
244                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get on VCONFKEY_SETAPPL_LCD_BRIGHTNESS vconf");
245                 value = brightness;
246         }
247         else if (key == _SCREEN_BACKLIGHT_TIME)
248         {
249                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support screen backlight time feature.");
250                 r = E_SUCCESS;
251                 int timeout = 0;
252                 errorCode = vconf_get_int(VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL, &timeout);
253                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get on VCONFKEY_SETAPPL_TIMEOUT_NORMAL vconf");
254                 value = timeout;
255         }
256         return r;
257 }
258
259 result
260 _SettingScreenProvider::SetValue(const String& key, const int value)
261 {
262         int errorCode = 0;
263         result r = E_OBJ_NOT_FOUND;
264
265         if(key == _SCREEN_BRIGHTNESS)
266         {
267                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support brightness feature.");
268                 r = E_SUCCESS;
269                 SysTryReturnResult(NID_SYS, value > 0 && value <= 100, E_INVALID_ARG, "Out of range");
270
271                 errorCode = vconf_set_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, value);
272                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_LCD_BRIGHTNESS vconf");
273
274                 errorCode = device_set_brightness_to_settings(0, value);
275                 if(errorCode != 0)
276                 {
277                         SysLog(NID_SYS, "It is failed to update screen brightness.");
278                 }
279         }
280         else if(key == _SCREEN_BACKLIGHT_TIME)
281         {
282                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support screen backlight time feature.");
283                 r = E_SUCCESS;
284                 SysTryReturnResult(NID_SYS, value == 15 || value == 30 || value == 60 || value == 120 || value == 300 || value == 600, E_INVALID_ARG, "Out of range");
285
286                 errorCode = vconf_set_int(VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL, value);
287                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL vconf");
288         }
289         return r;
290 }
291
292 result
293 _SettingScreenProvider::GetValue(const String& key, String& value)
294 {
295         int errorCode = 0;
296         result r = E_SUCCESS;
297
298         if(key == _SCREEN_WALLPAPER)
299         {
300                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support wallpaper feature.");
301                 r = E_SUCCESS;
302                 char* pWallpaper = null;
303                 errorCode = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, &pWallpaper);
304                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get wallpaper of home screen");
305
306                 r = StringUtil::Utf8ToString(pWallpaper, value);
307                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "[%s] StringUtil::Utf8ToString failed", GetErrorMessage(r));
308
309                 free(pWallpaper);
310         }
311         else if (key == _SCREEN_WALLPAPER_LOCK)
312         {
313                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support wallpaper feature.");
314                 r = E_SUCCESS;
315                 unique_ptr<char, charDeleter> pWallpaper(null);
316                 char* pTemp = null;
317
318                 errorCode = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, &pTemp);
319                 SysTryReturnResult(NID_SYS, errorCode == 0 && pTemp != null, E_SYSTEM,  "It is failed to get wallpaper of lock screen");
320
321                 pWallpaper.reset(pTemp);
322
323                 r = StringUtil::Utf8ToString(pWallpaper.get(), value);
324                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "[%s] StringUtil::Utf8ToString failed", GetErrorMessage(r));
325         }
326         else if (key == _SCREEN_MODE)
327         {
328                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support screen mode feature.");
329                 r = E_SUCCESS;
330                 unique_ptr<char, charDeleter> curmode(vconf_get_str(_SCREEN_MODE_VCONFKEY));
331                 value.Clear();
332                 value.Append(curmode.get());
333         }
334         return r;
335 }
336
337 result
338 _SettingScreenProvider::SetValue(const String& key, const String value)
339 {
340         int errorCode = 0;
341         result r = E_SUCCESS;
342
343         if(key == _SCREEN_WALLPAPER)
344         {
345                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support wallpaper feature.");
346                 r = E_SUCCESS;
347                 SysTryReturnResult(NID_SYS, File::IsFileExist(value) == true, E_INVALID_ARG, "The entry for the specified wallpaper file or the file path cannot be found");
348                 unique_ptr<char []> pFilePath(_StringConverter::CopyToCharArrayN(value));
349
350                 SysTryReturnResult(NID_SYS, pFilePath.get() != null, E_SYSTEM, "It is failed to convert type of filePath");
351
352                 errorCode = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, pFilePath.get());
353                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set wallpaper of home screen");
354         }
355         else if(key == _SCREEN_WALLPAPER_LOCK)
356         {
357                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support wallpaper feature.");
358                 r = E_SUCCESS;
359                 SysTryReturnResult(NID_SYS, File::IsFileExist(value) == true, E_INVALID_ARG, "The entry for the specified wallpaper lock file or the file path cannot be found");
360                 unique_ptr<char []> pFilePath(_StringConverter::CopyToCharArrayN(value));
361                 SysTryReturnResult(NID_SYS, pFilePath.get() != null, E_SYSTEM, "It is failed to convert type of filePath");
362
363                 errorCode = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, pFilePath.get());
364                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set wallpaper of home screen");
365         }
366         else if(key == _SCREEN_MODE)
367         {
368                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support screen mode feature.");
369                 r = E_SUCCESS;
370                 SysTryReturnResult(NID_SYS, value == _SCREEN_MODE_DYNAMIC || value == _SCREEN_MODE_STANDARD || value == _SCREEN_MODE_NATURAL || value == _SCREEN_MODE_MOVIE, E_INVALID_ARG, "designated value(%ls) is invalid", value.GetPointer());
371                 unique_ptr<char []> screenMode(_StringConverter::CopyToCharArrayN(value));
372                 errorCode  = vconf_set_str(_SCREEN_MODE_VCONFKEY, screenMode.get());
373
374                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to change screen mode.");
375         }
376         return r;
377 }
378
379 bool
380 _SettingScreenProvider::HasKey(const Tizen::Base::String& key)
381 {
382
383         if(key == _SCREEN_WALLPAPER || key == _SCREEN_WALLPAPER_LOCK)
384         {
385                 return true;
386         }
387         else if(key == _SCREEN_BRIGHTNESS || key ==_SCREEN_BRIGHTNESS_AUTO)
388         {
389                 return true;
390         }
391         else if(key == _SCREEN_BACKLIGHT_TIME)
392         {
393                 return true;
394         }
395         else if(key == _SCREEN_ROTATION_AUTO)
396         {
397                 return true;
398         }
399         else if(key == _SCREEN_MODE)
400         {
401                 return true;
402         }
403         return false;
404 }
405
406 void
407 _SettingScreenProvider::SettingEventVConf(keynode_t* node, void* userData)
408 {
409         String settingKey;
410
411         if (strcmp(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, vconf_keynode_get_name(node)) == 0)
412         {
413                 settingKey.Append(_SCREEN_BRIGHTNESS);
414         }
415         else if (strcmp(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, vconf_keynode_get_name(node)) == 0)
416         {
417                 settingKey.Append(_SCREEN_BRIGHTNESS_AUTO);
418         }
419         else if (strcmp(VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL, vconf_keynode_get_name(node)) == 0)
420         {
421                 settingKey.Append(_SCREEN_BACKLIGHT_TIME);
422         }
423         else if (strcmp(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, vconf_keynode_get_name(node)) == 0)
424         {
425                 settingKey.Append(_SCREEN_ROTATION_AUTO);
426         }
427         else if (strcmp(_SCREEN_MODE_VCONFKEY, vconf_keynode_get_name(node)) == 0)
428         {
429                 settingKey.Append(_SCREEN_MODE);
430         }
431         else
432         {
433                 return;
434         }
435         _SettingInfo* pSettingInfo = _SettingInfo::GetInstance();
436         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_SYSTEM, "_SettingInfo is not ready.");
437
438         result r = pSettingInfo->AnnounceSettingEvent(settingKey);
439         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send the event[%ls].", settingKey.GetPointer());
440 }
441
442 void
443 _SettingScreenProvider::SettingEventSettingInfo(system_settings_key_e key, void* userData)
444 {
445         String settingKey;
446
447         if(key == SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN)
448         {
449                 settingKey.Append(_SCREEN_WALLPAPER);
450         }
451         else if(key == SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN)
452         {
453                 settingKey.Append(_SCREEN_WALLPAPER_LOCK);
454         }
455         else
456         {
457                 return;
458         }
459         _SettingInfo* pSettingInfo = _SettingInfo::GetInstance();
460         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_SYSTEM, "_SettingInfo is not ready.");
461
462         result r = pSettingInfo->AnnounceSettingEvent(settingKey);
463         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send the event[%ls].", settingKey.GetPointer());
464 }
465
466 }}