summaryrefslogtreecommitdiff
path: root/location/manager/location-common-util.c
blob: a96362cdcce361efaba4cb0c9e8b6eb17a83f15e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * libslp-location
 *
 * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
 *          Genie Kim <daejins.kim@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#include "location.h"
#include "location-common-util.h"
#include "location-setting.h"
#include "location-log.h"

int location_application_get_authority (void)
{
	return LOCATION_APP_ON;
}

int location_application_set_authority (int auth)
{
	return LOCATION_ERROR_NONE;
}

int location_application_add_app_to_applist (void)
{
	return TRUE;
}

int location_application_enabled (void)
{
	return TRUE;
}

static gint compare_position (gconstpointer a, gconstpointer b)
{
	g_return_val_if_fail(a, 1);
	g_return_val_if_fail(b, -1);

	if(location_position_equal((LocationPosition*) a, (LocationPosition *)b) == TRUE) {
		return 0;
	}

	return -1;
}

static int
boundary_compare (gconstpointer comp1, gconstpointer comp2)
{
	g_return_val_if_fail(comp1, 1);
	g_return_val_if_fail(comp2, -1);

	int ret = -1;

	LocationBoundaryPrivate *priv1 = (LocationBoundaryPrivate *)comp1;
	LocationBoundaryPrivate *priv2 = (LocationBoundaryPrivate *)comp2;

	if (priv1->boundary->type == priv2->boundary->type) {
		switch (priv1->boundary->type) {
			case LOCATION_BOUNDARY_CIRCLE: {
				if (location_position_equal(priv1->boundary->circle.center, priv2->boundary->circle.center)
					&& priv1->boundary->circle.radius == priv2->boundary->circle.radius) {
					ret = 0;
				}
				break;
			}
			case LOCATION_BOUNDARY_RECT: {
				if (location_position_equal(priv1->boundary->rect.left_top, priv2->boundary->rect.left_top)
					&& location_position_equal(priv1->boundary->rect.right_bottom, priv2->boundary->rect.right_bottom)) {
					ret = 0;
				}
				break;
			}
			case LOCATION_BOUNDARY_POLYGON: {
				GList *boundary1_next = NULL;
				GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
				if (g_list_length(priv1->boundary->polygon.position_list) != g_list_length(priv2->boundary->polygon.position_list)) {
					return -1;
				}

				// Find a matching index of Boundary2 with Boundary1's 1st postion.
				boundary2_start = g_list_find_custom(priv2->boundary->polygon.position_list, g_list_nth_data(priv1->boundary->polygon.position_list, 0), (GCompareFunc) compare_position);
				if (boundary2_start == NULL) return -1;

				boundary2_prev = g_list_previous(boundary2_start);
				boundary2_next = g_list_next(boundary2_start);
				if (boundary2_prev == NULL) boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
				if (boundary2_next == NULL) boundary2_next = g_list_first(priv2->boundary->polygon.position_list);

				boundary1_next = g_list_next(priv1->boundary->polygon.position_list);
				if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_prev->data) == TRUE){
					boundary1_next = g_list_next(boundary1_next);
					while (boundary1_next) {
						boundary2_prev = g_list_previous(boundary2_prev);
						if (boundary2_prev == NULL) boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
						if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_prev->data) == FALSE){
							return -1;
						}
						boundary1_next = g_list_next(boundary1_next);
					}
					ret = 0;
				}
				else if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_next->data) == TRUE) {
					boundary1_next = g_list_next(boundary1_next);
					while(boundary1_next) {
						boundary2_next = g_list_next(boundary2_next);
						if (boundary2_next == NULL) boundary2_next = g_list_first(priv2->boundary->polygon.position_list);
						if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_next->data) == FALSE){
							return -1;
						}
						boundary1_next = g_list_next(boundary1_next);
					}
					ret = 0;
				}
				else {
					return -1;
				}
				break;
			}
			default:{
				 ret = -1;
				 break;
			}

		}
	}

	return ret;
}

int set_prop_boundary(GList **prev_boundary_priv_list, GList *new_boundary_priv_list)
{
	g_return_val_if_fail(new_boundary_priv_list, LOCATION_ERROR_PARAMETER);

	int index = 0;
	GList *check_list = NULL;

	LocationBoundaryPrivate *new_priv = NULL;
	LocationBoundaryPrivate *copy_priv = g_slice_new0(LocationBoundaryPrivate);

	while((new_priv = (LocationBoundaryPrivate *) g_list_nth_data(new_boundary_priv_list, index)) != NULL) {
		check_list = g_list_find_custom(*prev_boundary_priv_list, new_priv, (GCompareFunc)boundary_compare);
		if (check_list == NULL) {
			LOCATION_LOGD("Set Prop >> boundary type: [%d]", new_priv->boundary->type);
			copy_priv->boundary = location_boundary_copy(new_priv->boundary);
			copy_priv->zone_status = new_priv->zone_status;
			*prev_boundary_priv_list = g_list_append(*prev_boundary_priv_list, copy_priv);
		}
		index++;
	}
	*prev_boundary_priv_list = g_list_first(*prev_boundary_priv_list);

	return LOCATION_ERROR_NONE;
}

int set_prop_removal_boundary(GList **prev_boundary_list, LocationBoundary* boundary)
{
	g_return_val_if_fail(*prev_boundary_list, LOCATION_ERROR_PARAMETER);

	GList *check_list = NULL;
	LocationBoundaryPrivate *remove_priv = g_slice_new0(LocationBoundaryPrivate);

	remove_priv->boundary = location_boundary_copy(boundary);

	check_list = g_list_find_custom (*prev_boundary_list, remove_priv, (GCompareFunc) boundary_compare);
	if (check_list) {
		LOCATION_LOGD("Found");
		*prev_boundary_list = g_list_delete_link(*prev_boundary_list, check_list);
	}

	if (g_list_length(*prev_boundary_list) == 0 ) {
		LOCATION_LOGD("Boundary List is empty");
		g_list_free(*prev_boundary_list);
		*prev_boundary_list = NULL;
	}

	g_slice_free(LocationBoundaryPrivate, remove_priv);
	return LOCATION_ERROR_NONE;
}

void free_boundary_list (gpointer data)
{
	LocationBoundaryPrivate *priv= (LocationBoundaryPrivate *)data;

	location_boundary_free(priv->boundary);
	g_slice_free(LocationBoundaryPrivate, priv);
}