4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "location-common-util.h"
24 #include "location-log.h"
26 static gint compare_position (gconstpointer a, gconstpointer b)
28 g_return_val_if_fail(a, 1);
29 g_return_val_if_fail(b, -1);
31 if(location_position_equal((LocationPosition*) a, (LocationPosition *)b) == TRUE) {
39 boundary_compare (gconstpointer comp1, gconstpointer comp2)
41 g_return_val_if_fail(comp1, 1);
42 g_return_val_if_fail(comp2, -1);
46 LocationBoundary *boundary1 = (LocationBoundary *)comp1;
47 LocationBoundary *boundary2 = (LocationBoundary *)comp2;
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) {
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)) {
65 case LOCATION_BOUNDARY_POLYGON: {
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)) {
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;
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);
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){
91 boundary1_next = g_list_next(boundary1_next);
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){
103 boundary1_next = g_list_next(boundary1_next);
123 int set_prop_boundary(GList **prev_boundary_list, GList *new_boundary_list)
125 g_return_val_if_fail(new_boundary_list, LOCATION_ERROR_PARAMETER);
128 GList *check_list = NULL;
130 LocationBoundary *new_boundary = NULL;
131 LocationBoundary *copy_boundary = NULL;
134 while((new_boundary = (LocationBoundary*) g_list_nth_data(new_boundary_list, index)) != NULL) {
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);
144 *prev_boundary_list = g_list_first(*prev_boundary_list);
146 return LOCATION_ERROR_NONE;
151 int set_prop_removal_boundary(GList **prev_boundary_list, LocationBoundary* boundary)
153 g_return_val_if_fail(*prev_boundary_list, LOCATION_ERROR_PARAMETER);
155 GList *check_list = NULL;
157 check_list = g_list_find_custom (*prev_boundary_list, boundary, (GCompareFunc) boundary_compare);
159 LOCATION_LOGD("Found");
160 *prev_boundary_list = g_list_delete_link(*prev_boundary_list, check_list);
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;
169 return LOCATION_ERROR_NONE;