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