900bf307010f14f16a6093a1c122cb3e06e3a1a7
[framework/location/libslp-location.git] / location / location-common-util.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.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 #include "location.h"
23 #include "location-common-util.h"
24 #include "location-log.h"
25
26 static gint compare_position (gconstpointer a, gconstpointer b)
27 {
28         g_return_val_if_fail(a, 1);
29         g_return_val_if_fail(b, -1);
30
31         if(location_position_equal((LocationPosition*) a, (LocationPosition *)b) == TRUE) {
32                 return 0;
33         }
34
35         return -1;
36 }
37
38 static int
39 boundary_compare (gconstpointer comp1, gconstpointer comp2)
40 {
41         g_return_val_if_fail(comp1, 1);
42         g_return_val_if_fail(comp2, -1);
43
44         int ret = -1;
45
46         LocationBoundary *boundary1 = (LocationBoundary *)comp1;
47         LocationBoundary *boundary2 = (LocationBoundary *)comp2;
48
49         if (boundary1->type == boundary2->type) {
50                 switch (boundary1->type) {
51                         case LOCATION_BOUNDARY_CIRCLE: {
52                                 if (location_position_equal(boundary1->circle.center, boundary2->circle.center)
53                                         && boundary1->circle.radius == boundary2->circle.radius) {
54                                         ret = 0;
55                                 }
56                                 break;
57                         }
58                         case LOCATION_BOUNDARY_RECT: {
59                                 if (location_position_equal(boundary1->rect.left_top, boundary2->rect.left_top)
60                                         && location_position_equal(boundary1->rect.right_bottom, boundary2->rect.right_bottom)) {
61                                         ret = 0;
62                                 }
63                                 break;
64                         }
65                         case LOCATION_BOUNDARY_POLYGON: {
66
67                                 GList *boundary1_next = NULL;
68                                 GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
69                                 if (g_list_length(boundary1->polygon.position_list) != g_list_length(boundary2->polygon.position_list)) {
70                                         return -1;
71                                 }
72
73                                 // Find a matching index of Boundary2 with Boundary1's 1st postion.
74                                 boundary2_start = g_list_find_custom(boundary2->polygon.position_list, g_list_nth_data(boundary1->polygon.position_list, 0), (GCompareFunc) compare_position);
75                                 if (boundary2_start == NULL) return -1;
76
77                                 boundary2_prev = g_list_previous(boundary2_start);
78                                 boundary2_next = g_list_next(boundary2_start);
79                                 if (boundary2_prev == NULL) boundary2_prev = g_list_last(boundary2->polygon.position_list);
80                                 if (boundary2_next == NULL) boundary2_next = g_list_first(boundary2->polygon.position_list);
81
82                                 boundary1_next = g_list_next(boundary1->polygon.position_list);
83                                 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_prev->data) == TRUE){
84                                         boundary1_next = g_list_next(boundary1_next);
85                                         while (boundary1_next) {
86                                                 boundary2_prev = g_list_previous(boundary2_prev);
87                                                 if (boundary2_prev == NULL) boundary2_prev = g_list_last(boundary2->polygon.position_list);
88                                                 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_prev->data) == FALSE){
89                                                         return -1;
90                                                 }
91                                                 boundary1_next = g_list_next(boundary1_next);
92                                         }
93                                         ret = 0;
94                                 }
95                                 else if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_next->data) == TRUE) {
96                                         boundary1_next = g_list_next(boundary1_next);
97                                         while(boundary1_next) {
98                                                 boundary2_next = g_list_next(boundary2_next);
99                                                 if (boundary2_next == NULL) boundary2_next = g_list_first(boundary2->polygon.position_list);
100                                                 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_next->data) == FALSE){
101                                                         return -1;
102                                                 }
103                                                 boundary1_next = g_list_next(boundary1_next);
104                                         }
105                                         ret = 0;
106                                 }
107                                 else {
108                                         return -1;
109                                 }
110                                 break;
111                         }
112                         default:{
113                                  ret = -1;
114                                  break;
115                         }
116
117                 }
118         }
119
120         return ret;
121 }
122
123 int set_prop_boundary(GList **prev_boundary_list, GList *new_boundary_list)
124 {
125         g_return_val_if_fail(new_boundary_list, LOCATION_ERROR_PARAMETER);
126
127         int index;
128         GList *check_list = NULL;
129
130         LocationBoundary *new_boundary = NULL;
131         LocationBoundary *copy_boundary = NULL;
132
133         index = 0;
134         while((new_boundary = (LocationBoundary*) g_list_nth_data(new_boundary_list, index)) != NULL) {
135
136                 check_list = g_list_find_custom(*prev_boundary_list, new_boundary, (GCompareFunc)boundary_compare);
137                 if (check_list == NULL) {
138                         LOCATION_LOGD("Set Prop >> boundary type: [%d]", new_boundary->type);
139                         copy_boundary = location_boundary_copy(new_boundary);
140                         *prev_boundary_list = g_list_append(*prev_boundary_list, copy_boundary);
141                 }
142                 index++;
143         }
144         *prev_boundary_list = g_list_first(*prev_boundary_list);
145
146         return LOCATION_ERROR_NONE;
147 }
148
149
150
151 int set_prop_removal_boundary(GList **prev_boundary_list, LocationBoundary* boundary)
152 {
153         g_return_val_if_fail(*prev_boundary_list, LOCATION_ERROR_PARAMETER);
154
155         GList *check_list = NULL;
156
157         check_list = g_list_find_custom (*prev_boundary_list, boundary, (GCompareFunc) boundary_compare);
158         if (check_list) {
159                 LOCATION_LOGD("Found");
160                 *prev_boundary_list = g_list_delete_link(*prev_boundary_list, check_list);
161         }
162
163         if (g_list_length(*prev_boundary_list) == 0 ) {
164                 LOCATION_LOGD("Boundary List is empty");
165                 g_list_free(*prev_boundary_list);
166                 *prev_boundary_list = NULL;
167         }
168
169         return LOCATION_ERROR_NONE;
170 }
171
172