Sync with 2.4
[platform/core/location/lbs-location.git] / location / manager / location-setting.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
7  *          Genie Kim <daejins.kim@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <glib.h>
27 #include <bundle_internal.h>
28 #include <eventsystem.h>
29 #include "location-log.h"
30 #include "location-setting.h"
31
32 static unsigned int g_event_req_id;
33
34
35 gint location_setting_get_key_val(keynode_t *key)
36 {
37         g_return_val_if_fail(key, -1);
38         int val = -1;
39         switch (vconf_keynode_get_type(key)) {
40                 case VCONF_TYPE_INT:
41                         val = vconf_keynode_get_int(key);
42                         LOCATION_SECLOG("changed [%s]:[%d]", vconf_keynode_get_name(key), val);
43                         break;
44                 default:
45                         LOCATION_LOGW("Unused type(%d)", vconf_keynode_get_type(key));
46                         break;
47         }
48         return val;
49 }
50
51 gint location_setting_get_int(const gchar *path)
52 {
53         g_return_val_if_fail(path, 0);
54         int val = 0;
55         if (vconf_get_int(path, &val)) {
56                 LOCATION_SECLOG("failed [%s]", path);
57         }
58         return val;
59 }
60
61 gboolean location_setting_get_bool(const gchar *path)
62 {
63         g_return_val_if_fail(path, FALSE);
64         gboolean val = FALSE;
65         if (vconf_get_bool(path, &val)) {
66                 LOCATION_SECLOG("failed [%s]", path);
67         }
68         return val;
69 }
70
71 gchar *location_setting_get_string(const gchar *path)
72 {
73         g_return_val_if_fail(path, NULL);
74         return vconf_get_str(path);
75 }
76
77 static char *__convert_event_from_vconf(const char *vconf)
78 {
79         char *event = NULL;
80         if (g_strcmp0(vconf, VCONFKEY_LOCATION_USE_MY_LOCATION) == 0) {
81                 event = g_strdup(SYS_EVENT_LOCATION_ENABLE_STATE);
82         } else if (g_strcmp0(vconf, VCONFKEY_LOCATION_ENABLED) == 0) {
83                 event = g_strdup(SYS_EVENT_GPS_ENABLE_STATE);
84         } else if (g_strcmp0(vconf, VCONFKEY_LOCATION_NETWORK_ENABLED) == 0) {
85                 event = g_strdup(SYS_EVENT_NPS_ENABLE_STATE);
86         }
87
88         return event;
89 }
90
91 static char *__convert_key_from_event(const char *event)
92 {
93         char *key = NULL;
94         if (g_strcmp0(event, SYS_EVENT_LOCATION_ENABLE_STATE) == 0) {
95                 key = g_strdup(EVT_KEY_LOCATION_ENABLE_STATE);
96         } else if (g_strcmp0(event, SYS_EVENT_GPS_ENABLE_STATE) == 0) {
97                 key = g_strdup(EVT_KEY_GPS_ENABLE_STATE);
98         } else if (g_strcmp0(event, SYS_EVENT_NPS_ENABLE_STATE) == 0) {
99                 key = g_strdup(EVT_KEY_NPS_ENABLE_STATE);
100         }
101         return key;
102 }
103
104 static char *__convert_event_value(const int val)
105 {
106         char *value = NULL;
107         if (val == 1) {
108                 value = g_strdup(EVT_VAL_GPS_ENABLED);
109         } else {
110                 value = g_strdup(EVT_VAL_GPS_DISABLED);
111         }
112         return value;
113 }
114
115 gint location_setting_send_system_event(const char *path, const int val)
116 {
117         g_return_val_if_fail(path, -1);
118
119         int ret = 0;
120         char *event = NULL;
121         char *key = NULL;
122         char *value = NULL;
123         bundle *b = NULL;
124
125         event = __convert_event_from_vconf(path);
126         key = __convert_key_from_event(event);
127         value = __convert_event_value(val);
128
129         b = bundle_create();
130         bundle_add_str(b, key, value);
131         ret = eventsystem_request_sending_system_event(event, b);
132         bundle_free(b);
133
134         LOCATION_SECLOG("[%s: %s]", key, value);
135
136         g_free(event);
137         g_free(key);
138         g_free(value);
139         return ret;
140 }
141
142 static void __event_handler(const char *event_name, bundle *data, void *self)
143 {
144         const char *value = NULL;
145
146         if (g_strcmp0(event_name, SYS_EVENT_LOCATION_ENABLE_STATE) == 0) {
147                 value = bundle_get_val(data, EVT_KEY_LOCATION_ENABLE_STATE);
148         } else if (g_strcmp0(event_name, SYS_EVENT_GPS_ENABLE_STATE) == 0) {
149                 value = bundle_get_val(data, EVT_KEY_GPS_ENABLE_STATE);
150         } else if (g_strcmp0(event_name, SYS_EVENT_NPS_ENABLE_STATE) == 0) {
151                 value = bundle_get_val(data, EVT_KEY_NPS_ENABLE_STATE);
152         }
153
154         LOCATION_SECLOG("[%s: %s]", event_name, value);
155 }
156
157 gint location_setting_add_notify(const gchar *path, SettingCB setting_cb, gpointer self)
158 {
159         g_return_val_if_fail(path, -1);
160         g_return_val_if_fail(self, -1);
161
162         const char *event_name = NULL;
163         event_name = __convert_event_from_vconf(path);
164
165         if (eventsystem_register_event(event_name,
166                                        &g_event_req_id,
167                                        (eventsystem_handler) __event_handler, NULL) != ES_R_OK) {
168
169                 LOCATION_SECLOG("eventsystem_register_event failed");
170                 return -1;
171         }
172
173         if (vconf_notify_key_changed(path, setting_cb, self)) {
174                 LOCATION_SECLOG("vconf notify add failed [%s]", path);
175                 return -1;
176         }
177         LOCATION_SECLOG("vconf notify added [%s]", path);
178         return 0;
179 }
180
181 gint location_setting_ignore_notify(const gchar *path, SettingCB setting_cb)
182 {
183         g_return_val_if_fail(path, -1);
184         g_return_val_if_fail(setting_cb, -1);
185
186         if (eventsystem_unregister_event(g_event_req_id) != ES_R_OK) {
187                 LOCATION_SECLOG("eventsystem_unregister_event failed");
188                 return -1;
189         }
190
191         if (vconf_ignore_key_changed(path, setting_cb)) {
192                 LOCATION_SECLOG("vconf notify remove failed [%s]", path);
193                 return -1;
194         }
195         LOCATION_SECLOG("vconf notify removed [%s]", path);
196         return 0;
197 }
198
199 gint location_state_add_notify(const gchar *path, SettingCB setting_cb, gpointer self)
200 {
201         g_return_val_if_fail(path, -1);
202         g_return_val_if_fail(self, -1);
203
204         if (vconf_notify_key_changed(path, setting_cb, self)) {
205                 LOCATION_SECLOG("vconf notify add failed [%s]", path);
206                 return -1;
207         }
208         LOCATION_SECLOG("vconf notify added [%s]", path);
209         return 0;
210 }
211
212 gint location_state_ignore_notify(const gchar *path, SettingCB setting_cb)
213 {
214         g_return_val_if_fail(path, -1);
215         g_return_val_if_fail(setting_cb, -1);
216
217         if (vconf_ignore_key_changed(path, setting_cb)) {
218                 LOCATION_SECLOG("vconf notify remove failed [%s]", path);
219                 return -1;
220         }
221         LOCATION_SECLOG("vconf notify removed [%s]", path);
222         return 0;
223 }