Remove dependancy on appman
[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
30 #include "location.h"
31 #include "location-common-util.h"
32 #include "location-setting.h"
33 #include "location-log.h"
34
35 int location_application_get_authority (void)
36 {
37         return LOCATION_APP_ON;
38 }
39
40 int location_application_set_authority (int auth)
41 {
42         return LOCATION_ERROR_NONE;
43 }
44
45 int location_application_add_app_to_applist (void)
46 {
47         return TRUE;
48 }
49
50 int location_application_enabled (void)
51 {
52         return TRUE;
53 }
54
55 static gint compare_position (gconstpointer a, gconstpointer b)
56 {
57         g_return_val_if_fail(a, 1);
58         g_return_val_if_fail(b, -1);
59
60         if(location_position_equal((LocationPosition*) a, (LocationPosition *)b) == TRUE) {
61                 return 0;
62         }
63
64         return -1;
65 }
66
67 static int
68 boundary_compare (gconstpointer comp1, gconstpointer comp2)
69 {
70         g_return_val_if_fail(comp1, 1);
71         g_return_val_if_fail(comp2, -1);
72
73         int ret = -1;
74
75         LocationBoundaryPrivate *priv1 = (LocationBoundaryPrivate *)comp1;
76         LocationBoundaryPrivate *priv2 = (LocationBoundaryPrivate *)comp2;
77
78         if (priv1->boundary->type == priv2->boundary->type) {
79                 switch (priv1->boundary->type) {
80                         case LOCATION_BOUNDARY_CIRCLE: {
81                                 if (location_position_equal(priv1->boundary->circle.center, priv2->boundary->circle.center)
82                                         && priv1->boundary->circle.radius == priv2->boundary->circle.radius) {
83                                         ret = 0;
84                                 }
85                                 break;
86                         }
87                         case LOCATION_BOUNDARY_RECT: {
88                                 if (location_position_equal(priv1->boundary->rect.left_top, priv2->boundary->rect.left_top)
89                                         && location_position_equal(priv1->boundary->rect.right_bottom, priv2->boundary->rect.right_bottom)) {
90                                         ret = 0;
91                                 }
92                                 break;
93                         }
94                         case LOCATION_BOUNDARY_POLYGON: {
95                                 GList *boundary1_next = NULL;
96                                 GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
97                                 if (g_list_length(priv1->boundary->polygon.position_list) != g_list_length(priv2->boundary->polygon.position_list)) {
98                                         return -1;
99                                 }
100
101                                 // Find a matching index of Boundary2 with Boundary1's 1st postion.
102                                 boundary2_start = g_list_find_custom(priv2->boundary->polygon.position_list, g_list_nth_data(priv1->boundary->polygon.position_list, 0), (GCompareFunc) compare_position);
103                                 if (boundary2_start == NULL) return -1;
104
105                                 boundary2_prev = g_list_previous(boundary2_start);
106                                 boundary2_next = g_list_next(boundary2_start);
107                                 if (boundary2_prev == NULL) boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
108                                 if (boundary2_next == NULL) boundary2_next = g_list_first(priv2->boundary->polygon.position_list);
109
110                                 boundary1_next = g_list_next(priv1->boundary->polygon.position_list);
111                                 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_prev->data) == TRUE){
112                                         boundary1_next = g_list_next(boundary1_next);
113                                         while (boundary1_next) {
114                                                 boundary2_prev = g_list_previous(boundary2_prev);
115                                                 if (boundary2_prev == NULL) boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
116                                                 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_prev->data) == FALSE){
117                                                         return -1;
118                                                 }
119                                                 boundary1_next = g_list_next(boundary1_next);
120                                         }
121                                         ret = 0;
122                                 }
123                                 else if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_next->data) == TRUE) {
124                                         boundary1_next = g_list_next(boundary1_next);
125                                         while(boundary1_next) {
126                                                 boundary2_next = g_list_next(boundary2_next);
127                                                 if (boundary2_next == NULL) boundary2_next = g_list_first(priv2->boundary->polygon.position_list);
128                                                 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_next->data) == FALSE){
129                                                         return -1;
130                                                 }
131                                                 boundary1_next = g_list_next(boundary1_next);
132                                         }
133                                         ret = 0;
134                                 }
135                                 else {
136                                         return -1;
137                                 }
138                                 break;
139                         }
140                         default:{
141                                  ret = -1;
142                                  break;
143                         }
144
145                 }
146         }
147
148         return ret;
149 }
150
151 int set_prop_boundary(GList **prev_boundary_priv_list, GList *new_boundary_priv_list)
152 {
153         g_return_val_if_fail(new_boundary_priv_list, LOCATION_ERROR_PARAMETER);
154
155         int index = 0;
156         GList *check_list = NULL;
157
158         LocationBoundaryPrivate *new_priv = NULL;
159         LocationBoundaryPrivate *copy_priv = g_slice_new0(LocationBoundaryPrivate);
160
161         while((new_priv = (LocationBoundaryPrivate *) g_list_nth_data(new_boundary_priv_list, index)) != NULL) {
162                 check_list = g_list_find_custom(*prev_boundary_priv_list, new_priv, (GCompareFunc)boundary_compare);
163                 if (check_list == NULL) {
164                         LOCATION_LOGD("Set Prop >> boundary type: [%d]", new_priv->boundary->type);
165                         copy_priv->boundary = location_boundary_copy(new_priv->boundary);
166                         copy_priv->zone_status = new_priv->zone_status;
167                         *prev_boundary_priv_list = g_list_append(*prev_boundary_priv_list, copy_priv);
168                 }
169                 index++;
170         }
171         *prev_boundary_priv_list = g_list_first(*prev_boundary_priv_list);
172
173         return LOCATION_ERROR_NONE;
174 }
175
176 int set_prop_removal_boundary(GList **prev_boundary_list, LocationBoundary* boundary)
177 {
178         g_return_val_if_fail(*prev_boundary_list, LOCATION_ERROR_PARAMETER);
179
180         GList *check_list = NULL;
181         LocationBoundaryPrivate *remove_priv = g_slice_new0(LocationBoundaryPrivate);
182
183         remove_priv->boundary = location_boundary_copy(boundary);
184
185         check_list = g_list_find_custom (*prev_boundary_list, remove_priv, (GCompareFunc) boundary_compare);
186         if (check_list) {
187                 LOCATION_LOGD("Found");
188                 *prev_boundary_list = g_list_delete_link(*prev_boundary_list, check_list);
189         }
190
191         if (g_list_length(*prev_boundary_list) == 0 ) {
192                 LOCATION_LOGD("Boundary List is empty");
193                 g_list_free(*prev_boundary_list);
194                 *prev_boundary_list = NULL;
195         }
196
197         g_slice_free(LocationBoundaryPrivate, remove_priv);
198         return LOCATION_ERROR_NONE;
199 }
200
201 void free_boundary_list (gpointer data)
202 {
203         LocationBoundaryPrivate *priv= (LocationBoundaryPrivate *)data;
204
205         location_boundary_free(priv->boundary);
206         g_slice_free(LocationBoundaryPrivate, priv);
207 }