Merge "Apply string localization for web" into tizen_2.2
[platform/framework/native/appfw.git] / src / server / system / setting / providers / FSys_SettingVibrationProvider.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_SettingVibrationProvider.cpp
19  * @brief       This is the implementation for the _SettingVibrationProvider class.
20  */
21
22 #include <runtime_info.h>
23 #include <haptic/haptic.h>
24
25 #include <FApp.h>
26 #include <FBase.h>
27 #include <FBaseSysLog.h>
28 #include <FIo.h>
29
30 #include "FSys_SettingInfo.h"
31 #include "FSys_SettingVibrationProvider.h"
32
33 using namespace Tizen::App;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Utility;
36 using namespace Tizen::Io;
37
38 namespace Tizen { namespace System
39 {
40 static const wchar_t* _VIBRATOR = L"http://tizen.org/setting/vibrator";
41 static const wchar_t* _VIBRATOR_LEVEL = L"http://tizen.org/setting/vibrator.level";
42 static const wchar_t* _VIBRATOR_LEVEL_NOTIFICATION = L"http://tizen.org/setting/vibrator.level.notification";
43
44 _SettingVibrationProvider::_SettingVibrationProvider()
45 {
46         int errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, SettingEventVConf, null);
47         if(errorCode != 0)
48         {
49                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL event listener");
50         }
51
52         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, SettingEventVConf, null);
53         if(errorCode != 0)
54         {
55                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT event listener");
56         }
57
58         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT, SettingEventVConf, null);
59         if(errorCode != 0)
60         {
61                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register VCONFKEY_SETAPPL_DEFAULT_MEM_CAMERA_INT event listener");
62         }
63 }
64
65
66 _SettingVibrationProvider::~_SettingVibrationProvider()
67 {
68         int errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, SettingEventVConf);
69         if(errorCode != 0)
70         {
71                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL event listener");
72         }
73
74         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, SettingEventVConf);
75         if(errorCode != 0)
76         {
77                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT event listener");
78         }
79
80         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT, SettingEventVConf);
81         if(errorCode != 0)
82         {
83                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT event listener");
84         }
85 }
86
87 result
88 _SettingVibrationProvider::GetValue(const String& key, bool& value)
89 {
90         int enabled = false;
91         int errorCode = 0;
92         result r = E_OBJ_NOT_FOUND;
93
94         if(key == _VIBRATOR)
95         {
96                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
97                 r = E_SUCCESS;
98                 errorCode = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &enabled);
99                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get on VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL vconf.");
100                 value = enabled ? true : false;
101         }
102         return r;
103 }
104
105 result
106 _SettingVibrationProvider::SetValue(const String& key, const bool value)
107 {
108         int errorCode = 0;
109         result r = E_OBJ_NOT_FOUND;
110         haptic_device_h __hapticHandle;
111
112         if(key == _VIBRATOR)
113         {
114                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
115                 r = E_SUCCESS;
116                 if(!value)
117                 {
118                         errorCode = haptic_open(HAPTIC_DEVICE_0, &__hapticHandle);
119                         SysTryReturnResult(NID_SYS, errorCode == HAPTIC_ERROR_NONE, E_SYSTEM, "It is failed to open haptic library.");
120                         haptic_stop_all_effects(__hapticHandle);
121                         errorCode = haptic_close(__hapticHandle);
122                         SysTryReturnResult(NID_SYS, errorCode == HAPTIC_ERROR_NONE, E_SYSTEM, "It is failed to close haptic library.");
123                         __hapticHandle = null;
124                 }
125                 errorCode = vconf_set_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, value);
126                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL vconf.");
127         }
128         return r;
129 }
130
131 result
132 _SettingVibrationProvider::GetValue(const String& key, int& value)
133 {
134         int level = 0;
135         int errorCode = 0;
136         result r = E_OBJ_NOT_FOUND;
137
138         if(key == _VIBRATOR_LEVEL)
139         {
140                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
141                 r = E_SUCCESS;
142
143                 errorCode = vconf_get_int(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, &level);
144                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get on VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT vconf");
145                 value = level;
146         }
147         else if (key == _VIBRATOR_LEVEL_NOTIFICATION)
148         {
149                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
150                 r = E_SUCCESS;
151
152                 int errorCode = vconf_get_int(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT, &level);
153                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to get on VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT vconf");
154                 value = level;
155         }
156         return r;
157 }
158
159 result
160 _SettingVibrationProvider::SetValue(const String& key, const int value)
161 {
162         int errorCode = 0;
163         result r = E_OBJ_NOT_FOUND;
164         if(key == _VIBRATOR_LEVEL)
165         {
166                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
167                 r = E_SUCCESS;
168
169                 SysTryReturnResult(NID_SYS, value >= 0 && value <= 5, E_INVALID_ARG, "Speicified value is not valid");
170                 errorCode = vconf_set_int(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, value);
171                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT vconf");
172         }
173         else if(key == _VIBRATOR_LEVEL_NOTIFICATION)
174         {
175                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support vibrator feature.");
176                 r = E_SUCCESS;
177
178                 SysTryReturnResult(NID_SYS, value >= 0 && value <= 5, E_INVALID_ARG, "Speicified value is not valid");
179                 errorCode = vconf_set_int(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT, value);
180                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set on VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT vconf");
181         }
182         return r;
183 }
184
185 bool
186 _SettingVibrationProvider::HasKey(const Tizen::Base::String& key)
187 {
188
189         if(key == _VIBRATOR_LEVEL_NOTIFICATION
190                 || key == _VIBRATOR_LEVEL
191                 || key == _VIBRATOR)
192         {
193                 return true;
194         }
195         return false;
196 }
197
198 void
199 _SettingVibrationProvider::SettingEventVConf(keynode_t* node, void* userData)
200 {
201         String settingKey;
202
203         if (strcmp(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, vconf_keynode_get_name(node)) == 0)
204         {
205                 settingKey.Append(_VIBRATOR);
206         }
207         else if (strcmp(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, vconf_keynode_get_name(node)) == 0)
208         {
209                 settingKey.Append(_VIBRATOR_LEVEL);
210         }
211         else if (strcmp(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT, vconf_keynode_get_name(node)) == 0)
212         {
213                 settingKey.Append(_VIBRATOR_LEVEL_NOTIFICATION);
214         }
215         else
216         {
217                 return;
218         }
219         
220         _SettingInfo* pSettingInfo = _SettingInfo::GetInstance();
221         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_SYSTEM, "_SettingInfo is not ready.");
222
223         result r = pSettingInfo->AnnounceSettingEvent(settingKey);
224         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send the event[%ls].", settingKey.GetPointer());
225 }
226
227 }}