43e6150b82988b7d338dc47ab7de3d74031006f7
[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 #ifdef EVENT_SYSTEM
33 static unsigned int g_event_req_id;
34 #endif
35
36 gint location_setting_get_key_val(keynode_t *key)
37 {
38         g_return_val_if_fail(key, -1);
39         int val = -1;
40         switch (vconf_keynode_get_type(key)) {
41         case VCONF_TYPE_INT:
42                 val = vconf_keynode_get_int(key);
43                 LOCATION_SECLOG("changed [%s]:[%d]", vconf_keynode_get_name(key), val);
44                 break;
45         default:
46                 LOCATION_LOGW("Unused type(%d)", vconf_keynode_get_type(key));
47                 break;
48         }
49         return val;
50 }
51
52 gint location_setting_get_int(const gchar *path)
53 {
54         g_return_val_if_fail(path, 0);
55         int val = 0;
56         if (vconf_get_int(path, &val))
57                 LOCATION_SECLOG("failed [%s]", path);
58
59         return val;
60 }
61
62 gboolean location_setting_get_bool(const gchar *path)
63 {
64         g_return_val_if_fail(path, FALSE);
65         gboolean val = FALSE;
66         if (vconf_get_bool(path, &val))
67                 LOCATION_SECLOG("failed [%s]", path);
68
69         return val;
70 }
71
72 #ifdef EVENT_SYSTEM
73 static char *__convert_event_from_vconf(const char *vconf)
74 {
75         char *event = NULL;
76         if (g_strcmp0(vconf, VCONFKEY_LOCATION_USE_MY_LOCATION) == 0)
77                 event = g_strdup(SYS_EVENT_LOCATION_ENABLE_STATE);
78         else if (g_strcmp0(vconf, VCONFKEY_LOCATION_ENABLED) == 0)
79                 event = g_strdup(SYS_EVENT_GPS_ENABLE_STATE);
80         else if (g_strcmp0(vconf, VCONFKEY_LOCATION_NETWORK_ENABLED) == 0)
81                 event = g_strdup(SYS_EVENT_NPS_ENABLE_STATE);
82
83         return event;
84 }
85
86 static void __event_handler(const char *event_name, bundle *data, void *self)
87 {
88         const char *value = NULL;
89
90         if (g_strcmp0(event_name, SYS_EVENT_LOCATION_ENABLE_STATE) == 0)
91                 value = bundle_get_val(data, EVT_KEY_LOCATION_ENABLE_STATE);
92         else if (g_strcmp0(event_name, SYS_EVENT_GPS_ENABLE_STATE) == 0)
93                 value = bundle_get_val(data, EVT_KEY_GPS_ENABLE_STATE);
94         else if (g_strcmp0(event_name, SYS_EVENT_NPS_ENABLE_STATE) == 0)
95                 value = bundle_get_val(data, EVT_KEY_NPS_ENABLE_STATE);
96
97         LOCATION_SECLOG("[%s: %s]", event_name, value);
98 }
99 #endif
100
101 gint location_setting_add_notify(const gchar *path, SettingCB setting_cb, gpointer self)
102 {
103         g_return_val_if_fail(path, -1);
104         g_return_val_if_fail(self, -1);
105
106 #ifdef EVENT_SYSTEM
107         const char *event_name = NULL;
108         event_name = __convert_event_from_vconf(path);
109
110         if (eventsystem_register_event(event_name, &g_event_req_id, (eventsystem_handler) __event_handler, NULL) != ES_R_OK)
111                 LOCATION_SECLOG("eventsystem_register_event failed");
112 #endif
113         if (vconf_notify_key_changed(path, setting_cb, self)) {
114                 LOCATION_SECLOG("vconf notify add failed [%s]", path);
115                 return -1;
116         }
117         LOCATION_SECLOG("vconf notify added [%s]", path);
118         return 0;
119 }
120
121 gint location_setting_ignore_notify(const gchar *path, SettingCB setting_cb)
122 {
123         g_return_val_if_fail(path, -1);
124         g_return_val_if_fail(setting_cb, -1);
125
126 #ifdef EVENT_SYSTEM
127         if (eventsystem_unregister_event(g_event_req_id) != ES_R_OK)
128                 LOCATION_SECLOG("eventsystem_unregister_event failed");
129 #endif
130
131         if (vconf_ignore_key_changed(path, setting_cb)) {
132                 LOCATION_SECLOG("vconf notify remove failed [%s]", path);
133                 return -1;
134         }
135         LOCATION_SECLOG("vconf notify removed [%s]", path);
136         return 0;
137 }
138
139 gint location_state_add_notify(const gchar *path, SettingCB setting_cb, gpointer self)
140 {
141         g_return_val_if_fail(path, -1);
142         g_return_val_if_fail(self, -1);
143
144         if (vconf_notify_key_changed(path, setting_cb, self)) {
145                 LOCATION_SECLOG("vconf notify add failed [%s]", path);
146                 return -1;
147         }
148         LOCATION_SECLOG("vconf notify added [%s]", path);
149         return 0;
150 }
151
152 gint location_state_ignore_notify(const gchar *path, SettingCB setting_cb)
153 {
154         g_return_val_if_fail(path, -1);
155         g_return_val_if_fail(setting_cb, -1);
156
157         if (vconf_ignore_key_changed(path, setting_cb)) {
158                 LOCATION_SECLOG("vconf notify remove failed [%s]", path);
159                 return -1;
160         }
161         LOCATION_SECLOG("vconf notify removed [%s]", path);
162         return 0;
163 }