removed wrong contacts, added authors
[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  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <glib.h>
24 #include <bundle_internal.h>
25 #include <eventsystem.h>
26 #include "location-log.h"
27 #include "location-setting.h"
28
29 #ifdef EVENT_SYSTEM
30 static unsigned int g_event_req_id;
31 #endif
32
33 gint location_setting_get_key_val(keynode_t *key)
34 {
35         g_return_val_if_fail(key, -1);
36         int val = -1;
37         switch (vconf_keynode_get_type(key)) {
38         case VCONF_TYPE_INT:
39                 val = vconf_keynode_get_int(key);
40                 LOCATION_SECLOG("changed [%s]:[%d]", vconf_keynode_get_name(key), val);
41                 break;
42         default:
43                 LOCATION_LOGW("Unused type(%d)", vconf_keynode_get_type(key));
44                 break;
45         }
46         return val;
47 }
48
49 gint location_setting_get_int(const gchar *path)
50 {
51         g_return_val_if_fail(path, 0);
52         int val = 0;
53         if (vconf_get_int(path, &val))
54                 LOCATION_SECLOG("failed [%s]", path);
55
56         return val;
57 }
58
59 gboolean location_setting_get_bool(const gchar *path)
60 {
61         g_return_val_if_fail(path, FALSE);
62         gboolean val = FALSE;
63         if (vconf_get_bool(path, &val))
64                 LOCATION_SECLOG("failed [%s]", path);
65
66         return val;
67 }
68
69 #ifdef EVENT_SYSTEM
70 static char *__convert_event_from_vconf(const char *vconf)
71 {
72         char *event = NULL;
73         if (g_strcmp0(vconf, VCONFKEY_LOCATION_USE_MY_LOCATION) == 0)
74                 event = g_strdup(SYS_EVENT_LOCATION_ENABLE_STATE);
75         else if (g_strcmp0(vconf, VCONFKEY_LOCATION_ENABLED) == 0)
76                 event = g_strdup(SYS_EVENT_GPS_ENABLE_STATE);
77         else if (g_strcmp0(vconf, VCONFKEY_LOCATION_NETWORK_ENABLED) == 0)
78                 event = g_strdup(SYS_EVENT_NPS_ENABLE_STATE);
79
80         return event;
81 }
82
83 static void __event_handler(const char *event_name, bundle *data, void *self)
84 {
85         const char *value = NULL;
86
87         if (g_strcmp0(event_name, SYS_EVENT_LOCATION_ENABLE_STATE) == 0)
88                 value = bundle_get_val(data, EVT_KEY_LOCATION_ENABLE_STATE);
89         else if (g_strcmp0(event_name, SYS_EVENT_GPS_ENABLE_STATE) == 0)
90                 value = bundle_get_val(data, EVT_KEY_GPS_ENABLE_STATE);
91         else if (g_strcmp0(event_name, SYS_EVENT_NPS_ENABLE_STATE) == 0)
92                 value = bundle_get_val(data, EVT_KEY_NPS_ENABLE_STATE);
93
94         LOCATION_SECLOG("[%s: %s]", event_name, value);
95 }
96 #endif
97
98 gint location_setting_add_notify(const gchar *path, SettingCB setting_cb, gpointer self)
99 {
100         g_return_val_if_fail(path, -1);
101         g_return_val_if_fail(self, -1);
102
103 #ifdef EVENT_SYSTEM
104         const char *event_name = NULL;
105         event_name = __convert_event_from_vconf(path);
106
107         if (eventsystem_register_event(event_name, &g_event_req_id, (eventsystem_handler) __event_handler, NULL) != ES_R_OK)
108                 LOCATION_SECLOG("eventsystem_register_event failed");
109 #endif
110         if (vconf_notify_key_changed(path, setting_cb, self)) {
111                 LOCATION_SECLOG("vconf notify add failed [%s]", path);
112                 return -1;
113         }
114         LOCATION_SECLOG("vconf notify added [%s]", path);
115         return 0;
116 }
117
118 gint location_setting_ignore_notify(const gchar *path, SettingCB setting_cb)
119 {
120         g_return_val_if_fail(path, -1);
121         g_return_val_if_fail(setting_cb, -1);
122
123 #ifdef EVENT_SYSTEM
124         if (eventsystem_unregister_event(g_event_req_id) != ES_R_OK)
125                 LOCATION_SECLOG("eventsystem_unregister_event failed");
126 #endif
127
128         if (vconf_ignore_key_changed(path, setting_cb)) {
129                 LOCATION_SECLOG("vconf notify remove failed [%s]", path);
130                 return -1;
131         }
132         LOCATION_SECLOG("vconf notify removed [%s]", path);
133         return 0;
134 }
135
136 gint location_state_add_notify(const gchar *path, SettingCB setting_cb, gpointer self)
137 {
138         g_return_val_if_fail(path, -1);
139         g_return_val_if_fail(self, -1);
140
141         if (vconf_notify_key_changed(path, setting_cb, self)) {
142                 LOCATION_SECLOG("vconf notify add failed [%s]", path);
143                 return -1;
144         }
145         LOCATION_SECLOG("vconf notify added [%s]", path);
146         return 0;
147 }
148
149 gint location_state_ignore_notify(const gchar *path, SettingCB setting_cb)
150 {
151         g_return_val_if_fail(path, -1);
152         g_return_val_if_fail(setting_cb, -1);
153
154         if (vconf_ignore_key_changed(path, setting_cb)) {
155                 LOCATION_SECLOG("vconf notify remove failed [%s]", path);
156                 return -1;
157         }
158         LOCATION_SECLOG("vconf notify removed [%s]", path);
159         return 0;
160 }