Tizen 2.1 base
[platform/core/location/lbs-location.git] / location / manager / location-common-util.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 <stdio.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <location-appman.h>
30
31 #include "location.h"
32 #include "location-common-util.h"
33 #include "location-setting.h"
34 #include "location-log.h"
35
36
37 int location_application_get_authority (void)
38 {
39         pid_t pid = getpid();
40         location_appman_s *appman;
41         int enabled;
42         int found;
43
44         if (location_appman_get_package_by_pid(pid, &appman) != LOCATION_APPMAN_ERROR_NONE) {
45                 LOCATION_LOGE("Fail to location_appman_get_package_by_pid");
46                 return LOCATION_APP_NOT_FOUND;
47         }
48
49         if (location_appman_find_package(appman->package, &found) != LOCATION_APPMAN_ERROR_NONE || found == LOCATION_APPMAN_PACKAGE_NOTFOUND) {
50                 LOCATION_LOGE("Cannot find package [%s]", appman->package);
51                 return LOCATION_APP_NOT_FOUND;
52         }
53
54         if (location_appman_is_enabled(appman->package, &enabled) != LOCATION_APPMAN_ERROR_NONE) {
55                 LOCATION_LOGE("Fail to location_appman_is_enabled");
56                 return LOCATION_APP_NOT_FOUND;
57         }
58
59         if (enabled) {
60                 return LOCATION_APP_ON;
61         }
62         else {
63                 return LOCATION_APP_OFF;
64
65         }
66
67         return LOCATION_APP_NOT_FOUND;
68 }
69
70 int location_application_set_authority (int auth)
71 {
72         pid_t pid = getpid();
73         location_appman_s *appman;
74         int enabled;
75         int found;
76
77         if (location_appman_get_package_by_pid(pid, &appman) != LOCATION_APPMAN_ERROR_NONE) {
78                 LOCATION_LOGE("Fail to location_appman_get_package_by_pid");
79                 return LOCATION_ERROR_UNKNOWN;
80         }
81
82         if (location_appman_find_package(appman->package, &found) != LOCATION_APPMAN_ERROR_NONE || found == LOCATION_APPMAN_PACKAGE_NOTFOUND) {
83                 LOCATION_LOGE("Cannot find package [%s]", appman->package);
84                 return LOCATION_ERROR_UNKNOWN;
85         }
86
87         if (location_appman_set_on(appman->package, auth) != LOCATION_APPMAN_ERROR_NONE) {
88                 LOCATION_LOGE("Fail to location_appman_is_enabled");
89                 return LOCATION_ERROR_UNKNOWN;
90         }
91
92         return LOCATION_ERROR_NONE;
93 }
94
95 int location_application_add_app_to_applist (void)
96 {
97         pid_t pid = getpid();
98         location_appman_s *appman;
99         int found;
100         time_t timestamp;
101
102         if (location_appman_get_package_by_pid(pid, &appman) != LOCATION_APPMAN_ERROR_NONE) {
103                 LOCATION_LOGE("Fail to location_appman_get_package_by_pid");
104                 return FALSE;
105         }
106
107         if (location_appman_find_package(appman->package, &found) != LOCATION_APPMAN_ERROR_NONE) {
108                 LOCATION_LOGE("Cannot find package [%s]", appman->package);
109                 if (appman) g_free(appman);
110                 return FALSE;
111         }
112
113         if (found == LOCATION_APPMAN_PACKAGE_NOTFOUND) {
114                 LOCATION_LOGD("First time to use location [%s]", appman->package);
115                 if (location_appman_register_package(appman) != LOCATION_APPMAN_ERROR_NONE) {
116                         LOCATION_LOGE("Fail to register [%s]", appman->package);
117                         if (appman) g_free(appman);
118                         return FALSE;
119                 }
120         } else {
121                 LOCATION_LOGD("[%s] is already registered. Update recently used time", appman->package);
122                 if (appman) g_free(appman);
123                 return FALSE;
124         }
125
126         if (appman) g_free(appman);
127         return TRUE;
128 }
129
130 int location_application_enabled (void)
131 {
132         pid_t pid = getpid();
133         location_appman_s *appman;
134         int enabled;
135         int found;
136         time_t timestamp;
137
138         if (TRUE == location_appman_check_developer_mode()) {
139                 LOCATION_LOGE("Location is Enabled");
140                 return TRUE;
141         }
142
143         if (location_appman_get_package_by_pid(pid, &appman) != LOCATION_APPMAN_ERROR_NONE) {
144                 LOCATION_LOGE("Fail to location_appman_get_package_by_pid");
145                 return FALSE;
146         }
147
148         if (location_appman_find_package(appman->package, &found) != LOCATION_APPMAN_ERROR_NONE) {
149                 LOCATION_LOGE("Cannot find package [%s]", appman->package);
150                 if (appman) g_free(appman);
151                 return FALSE;
152         }
153
154         if (found == LOCATION_APPMAN_PACKAGE_NOTFOUND) {
155                 LOCATION_LOGD("First time to use location [%s]", appman->package);
156                 if (location_appman_register_package(appman) != LOCATION_APPMAN_ERROR_NONE) {
157                         LOCATION_LOGE("Fail to register [%s]", appman->package);
158                         if (appman) g_free(appman);
159                         return FALSE;
160                 }
161         } else {
162                 LOCATION_LOGD("[%s] is already registered. Update recently used time", appman->package);
163                 time(&timestamp);
164                 if (location_appman_set_recently_used(appman->package, timestamp) != LOCATION_APPMAN_ERROR_NONE) {
165                         LOCATION_LOGD("Cannot update recently used time");
166                 }
167         }
168
169         if (location_appman_is_enabled(appman->package, &enabled) != LOCATION_APPMAN_ERROR_NONE) {
170                 LOCATION_LOGE("Fail to location_appman_is_enabled");
171                 if (appman) g_free(appman);
172                 return FALSE;
173         }
174
175         if (appman) g_free(appman);
176         return enabled;
177 }
178
179 static gint compare_position (gconstpointer a, gconstpointer b)
180 {
181         g_return_val_if_fail(a, 1);
182         g_return_val_if_fail(b, -1);
183
184         if(location_position_equal((LocationPosition*) a, (LocationPosition *)b) == TRUE) {
185                 return 0;
186         }
187
188         return -1;
189 }
190
191 static int
192 boundary_compare (gconstpointer comp1, gconstpointer comp2)
193 {
194         g_return_val_if_fail(comp1, 1);
195         g_return_val_if_fail(comp2, -1);
196
197         int ret = -1;
198
199         LocationBoundaryPrivate *priv1 = (LocationBoundaryPrivate *)comp1;
200         LocationBoundaryPrivate *priv2 = (LocationBoundaryPrivate *)comp2;
201
202         if (priv1->boundary->type == priv2->boundary->type) {
203                 switch (priv1->boundary->type) {
204                         case LOCATION_BOUNDARY_CIRCLE: {
205                                 if (location_position_equal(priv1->boundary->circle.center, priv2->boundary->circle.center)
206                                         && priv1->boundary->circle.radius == priv2->boundary->circle.radius) {
207                                         ret = 0;
208                                 }
209                                 break;
210                         }
211                         case LOCATION_BOUNDARY_RECT: {
212                                 if (location_position_equal(priv1->boundary->rect.left_top, priv2->boundary->rect.left_top)
213                                         && location_position_equal(priv1->boundary->rect.right_bottom, priv2->boundary->rect.right_bottom)) {
214                                         ret = 0;
215                                 }
216                                 break;
217                         }
218                         case LOCATION_BOUNDARY_POLYGON: {
219                                 GList *boundary1_next = NULL;
220                                 GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
221                                 if (g_list_length(priv1->boundary->polygon.position_list) != g_list_length(priv2->boundary->polygon.position_list)) {
222                                         return -1;
223                                 }
224
225                                 // Find a matching index of Boundary2 with Boundary1's 1st postion.
226                                 boundary2_start = g_list_find_custom(priv2->boundary->polygon.position_list, g_list_nth_data(priv1->boundary->polygon.position_list, 0), (GCompareFunc) compare_position);
227                                 if (boundary2_start == NULL) return -1;
228
229                                 boundary2_prev = g_list_previous(boundary2_start);
230                                 boundary2_next = g_list_next(boundary2_start);
231                                 if (boundary2_prev == NULL) boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
232                                 if (boundary2_next == NULL) boundary2_next = g_list_first(priv2->boundary->polygon.position_list);
233
234                                 boundary1_next = g_list_next(priv1->boundary->polygon.position_list);
235                                 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_prev->data) == TRUE){
236                                         boundary1_next = g_list_next(boundary1_next);
237                                         while (boundary1_next) {
238                                                 boundary2_prev = g_list_previous(boundary2_prev);
239                                                 if (boundary2_prev == NULL) boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
240                                                 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_prev->data) == FALSE){
241                                                         return -1;
242                                                 }
243                                                 boundary1_next = g_list_next(boundary1_next);
244                                         }
245                                         ret = 0;
246                                 }
247                                 else if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_next->data) == TRUE) {
248                                         boundary1_next = g_list_next(boundary1_next);
249                                         while(boundary1_next) {
250                                                 boundary2_next = g_list_next(boundary2_next);
251                                                 if (boundary2_next == NULL) boundary2_next = g_list_first(priv2->boundary->polygon.position_list);
252                                                 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_next->data) == FALSE){
253                                                         return -1;
254                                                 }
255                                                 boundary1_next = g_list_next(boundary1_next);
256                                         }
257                                         ret = 0;
258                                 }
259                                 else {
260                                         return -1;
261                                 }
262                                 break;
263                         }
264                         default:{
265                                  ret = -1;
266                                  break;
267                         }
268
269                 }
270         }
271
272         return ret;
273 }
274
275 int set_prop_boundary(GList **prev_boundary_priv_list, GList *new_boundary_priv_list)
276 {
277         g_return_val_if_fail(new_boundary_priv_list, LOCATION_ERROR_PARAMETER);
278
279         int index = 0;
280         GList *check_list = NULL;
281
282         LocationBoundaryPrivate *new_priv = NULL;
283         LocationBoundaryPrivate *copy_priv = g_slice_new0(LocationBoundaryPrivate);
284
285         while((new_priv = (LocationBoundaryPrivate *) g_list_nth_data(new_boundary_priv_list, index)) != NULL) {
286                 check_list = g_list_find_custom(*prev_boundary_priv_list, new_priv, (GCompareFunc)boundary_compare);
287                 if (check_list == NULL) {
288                         LOCATION_LOGD("Set Prop >> boundary type: [%d]", new_priv->boundary->type);
289                         copy_priv->boundary = location_boundary_copy(new_priv->boundary);
290                         copy_priv->zone_status = new_priv->zone_status;
291                         *prev_boundary_priv_list = g_list_append(*prev_boundary_priv_list, copy_priv);
292                 }
293                 index++;
294         }
295         *prev_boundary_priv_list = g_list_first(*prev_boundary_priv_list);
296
297         return LOCATION_ERROR_NONE;
298 }
299
300
301 int set_prop_removal_boundary(GList **prev_boundary_list, LocationBoundary* boundary)
302 {
303         g_return_val_if_fail(*prev_boundary_list, LOCATION_ERROR_PARAMETER);
304
305         GList *check_list = NULL;
306         LocationBoundaryPrivate *remove_priv = g_slice_new0(LocationBoundaryPrivate);
307
308         remove_priv->boundary = location_boundary_copy(boundary);
309
310         check_list = g_list_find_custom (*prev_boundary_list, remove_priv, (GCompareFunc) boundary_compare);
311         if (check_list) {
312                 LOCATION_LOGD("Found");
313                 *prev_boundary_list = g_list_delete_link(*prev_boundary_list, check_list);
314                 g_slice_free(LocationBoundaryPrivate, remove_priv);
315         }
316
317         if (g_list_length(*prev_boundary_list) == 0 ) {
318                 LOCATION_LOGD("Boundary List is empty");
319                 g_list_free(*prev_boundary_list);
320                 *prev_boundary_list = NULL;
321         }
322
323         g_slice_free(LocationBoundaryPrivate, remove_priv);
324         return LOCATION_ERROR_NONE;
325 }
326
327 void free_boundary_list (gpointer data)
328 {
329         LocationBoundaryPrivate *priv= (LocationBoundaryPrivate *)data;
330
331         location_boundary_free(priv->boundary);
332         g_slice_free(LocationBoundaryPrivate, priv);
333 }
334