382691ac3259aef224b6f122559a499e1ad54d5e
[platform/core/api/maps-service.git] / src / api / maps_area.cpp
1 /* Copyright (c) 2010-2014 Samsung Electronics Co., Ltd. All rights reserved.
2  *
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <glib.h>
18 #include "maps_error.h"
19 #include "maps_area.h"
20 #include "maps_util.h"
21 #include "maps_condition.h"
22
23 extern bool maps_coordinates_is_valid(const maps_coordinates_h coordinates);
24
25 EXPORT_API int maps_area_create_rectangle(const maps_coordinates_h top_left,
26                                           const maps_coordinates_h bottom_right,
27                                           maps_area_h *area)
28 {
29         if (!maps_condition_check_maps_feature())
30                 return MAPS_ERROR_NOT_SUPPORTED;
31
32         if (!top_left || !bottom_right || !area)
33                 return MAPS_ERROR_INVALID_PARAMETER;
34
35         if (!maps_coordinates_is_valid(top_left) ||
36                 !maps_coordinates_is_valid(bottom_right))
37                 return MAPS_ERROR_INVALID_PARAMETER;
38
39         maps_area_s *bound = g_new0(maps_area_s, 1);
40
41         if (bound == NULL) {
42                 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
43                 return MAPS_ERROR_OUT_OF_MEMORY;
44         }
45
46         bound->type = MAPS_AREA_RECTANGLE;
47
48         bound->rect.top_left = *((maps_coordinates_s *) top_left);
49         bound->rect.bottom_right = *((maps_coordinates_s *) bottom_right);
50
51         *area = (maps_area_h) bound;
52
53         return MAPS_ERROR_NONE;
54 }
55
56 EXPORT_API int maps_area_create_circle(const maps_coordinates_h center,
57                                        const double radius, maps_area_h *area)
58 {
59         if (!maps_condition_check_maps_feature())
60                 return MAPS_ERROR_NOT_SUPPORTED;
61
62         if (!center || !area || radius <= 0)
63                 return MAPS_ERROR_INVALID_PARAMETER;
64
65         if (!maps_coordinates_is_valid(center))
66                 return MAPS_ERROR_INVALID_PARAMETER;
67
68         maps_area_s *bound = g_new0(maps_area_s, 1);
69
70         if (bound == NULL) {
71                 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
72                 return MAPS_ERROR_OUT_OF_MEMORY;
73         }
74
75         bound->type = MAPS_AREA_CIRCLE;
76         bound->circle.center = *((maps_coordinates_s *) center);
77         bound->circle.radius = radius;
78         *area = (maps_area_h) bound;
79
80         return MAPS_ERROR_NONE;
81 }
82
83 EXPORT_API int maps_area_destroy(maps_area_h area)
84 {
85         if (!maps_condition_check_maps_feature())
86                 return MAPS_ERROR_NOT_SUPPORTED;
87         if (!area)
88                 return MAPS_ERROR_INVALID_PARAMETER;
89
90         maps_area_s *handle = (maps_area_s *) area;
91         g_free(handle);
92         return MAPS_ERROR_NONE;
93 }
94
95 EXPORT_API int maps_area_clone(const maps_area_h origin, maps_area_h *cloned)
96 {
97         if (!maps_condition_check_maps_feature())
98                 return MAPS_ERROR_NOT_SUPPORTED;
99         if (!cloned || !origin)
100                 return MAPS_ERROR_INVALID_PARAMETER;
101
102         maps_area_s * origin_handle = (maps_area_s *) origin;
103         if (origin_handle->type == MAPS_AREA_RECTANGLE) {
104                 maps_area_h new_rect = NULL;
105                 maps_area_rectangle_s rect = origin_handle->rect;
106                 maps_coordinates_s rec_tl = rect.top_left;
107                 maps_coordinates_s rec_br = rect.bottom_right;
108                 maps_area_create_rectangle((maps_coordinates_h) & rec_tl,
109                         (maps_coordinates_h) & rec_br, &new_rect);
110                 if (new_rect) {
111                         *cloned = new_rect;
112                 } else {
113                         return MAPS_ERROR_INVALID_PARAMETER;
114                 }
115         } else if (origin_handle->type == MAPS_AREA_CIRCLE) {
116                 maps_area_h new_circle = NULL;
117                 maps_area_circle_s cir = origin_handle->circle;
118                 maps_coordinates_s center = cir.center;
119                 double radius = cir.radius;
120                 maps_area_create_circle((maps_coordinates_h)&center, radius, &new_circle);
121                 if (new_circle) {
122                         *cloned = new_circle;
123                 } else {
124                         return MAPS_ERROR_INVALID_PARAMETER;
125                 }
126         }
127
128         return MAPS_ERROR_NONE;
129 }
130
131 bool maps_area_is_valid(const maps_area_h area)
132 {
133         if (!maps_condition_check_maps_feature())
134                 return MAPS_ERROR_NOT_SUPPORTED;
135         if (!area) return false;
136
137         bool ret = true;
138         maps_area_s *handle = (maps_area_s *) area;
139
140         do {
141                 if (handle->type == MAPS_AREA_RECTANGLE) {
142                         maps_area_rectangle_s rect = handle->rect;
143
144                         if (!maps_coordinates_is_valid(&rect.top_left) ||
145                                 !maps_coordinates_is_valid(&rect.bottom_right)) {
146                                 ret = false;
147                                 break;
148                         }
149                 } else if (handle->type == MAPS_AREA_CIRCLE) {
150                         maps_area_circle_s cir = handle->circle;
151                         maps_coordinates_s center = cir.center;
152
153                         if (cir.radius <= 0) {
154                                 ret = false;
155                                 break;
156                         }
157                         if (!maps_coordinates_is_valid(&center)) {
158                                 ret = false;
159                                 break;
160                         }
161                 } else {
162                         ret = false;
163                 }
164         } while (false);
165
166         return ret;
167 }