Add LCOV remarkers to increase line coverage rate
[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                 //LCOV_EXCL_START
43                 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
44                 return MAPS_ERROR_OUT_OF_MEMORY;
45                 //LCOV_EXCL_STOP
46         }
47
48         bound->type = MAPS_AREA_RECTANGLE;
49
50         bound->rect.top_left = *((maps_coordinates_s *) top_left);
51         bound->rect.bottom_right = *((maps_coordinates_s *) bottom_right);
52
53         *area = (maps_area_h) bound;
54
55         return MAPS_ERROR_NONE;
56 }
57
58 EXPORT_API int maps_area_create_circle(const maps_coordinates_h center,
59                                        const double radius, maps_area_h *area)
60 {
61         if (!maps_condition_check_maps_feature())
62                 return MAPS_ERROR_NOT_SUPPORTED;
63
64         if (!center || !area || radius <= 0)
65                 return MAPS_ERROR_INVALID_PARAMETER;
66
67         if (!maps_coordinates_is_valid(center))
68                 return MAPS_ERROR_INVALID_PARAMETER;
69
70         maps_area_s *bound = g_new0(maps_area_s, 1);
71
72         if (bound == NULL) {
73                 //LCOV_EXCL_START
74                 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
75                 return MAPS_ERROR_OUT_OF_MEMORY;
76                 //LCOV_EXCL_STOP
77         }
78
79         bound->type = MAPS_AREA_CIRCLE;
80         bound->circle.center = *((maps_coordinates_s *) center);
81         bound->circle.radius = radius;
82         *area = (maps_area_h) bound;
83
84         return MAPS_ERROR_NONE;
85 }
86
87 EXPORT_API int maps_area_destroy(maps_area_h area)
88 {
89         if (!maps_condition_check_maps_feature())
90                 return MAPS_ERROR_NOT_SUPPORTED;
91         if (!area)
92                 return MAPS_ERROR_INVALID_PARAMETER;
93
94         maps_area_s *handle = (maps_area_s *) area;
95         g_free(handle);
96         return MAPS_ERROR_NONE;
97 }
98
99 EXPORT_API int maps_area_clone(const maps_area_h origin, maps_area_h *cloned)
100 {
101         if (!maps_condition_check_maps_feature())
102                 return MAPS_ERROR_NOT_SUPPORTED;
103         if (!cloned || !origin)
104                 return MAPS_ERROR_INVALID_PARAMETER;
105
106         maps_area_s * origin_handle = (maps_area_s *) origin;
107         if (origin_handle->type == MAPS_AREA_RECTANGLE) {
108                 maps_area_h new_rect = NULL;
109                 maps_area_rectangle_s rect = origin_handle->rect;
110                 maps_coordinates_s rec_tl = rect.top_left;
111                 maps_coordinates_s rec_br = rect.bottom_right;
112                 maps_area_create_rectangle((maps_coordinates_h) & rec_tl,
113                         (maps_coordinates_h) & rec_br, &new_rect);
114                 if (new_rect) {
115                         *cloned = new_rect;
116                 } else {
117                         return MAPS_ERROR_INVALID_PARAMETER;
118                 }
119         } else if (origin_handle->type == MAPS_AREA_CIRCLE) {
120                 maps_area_h new_circle = NULL;
121                 maps_area_circle_s cir = origin_handle->circle;
122                 maps_coordinates_s center = cir.center;
123                 double radius = cir.radius;
124                 maps_area_create_circle((maps_coordinates_h)&center, radius, &new_circle);
125                 if (new_circle) {
126                         *cloned = new_circle;
127                 } else {
128                         return MAPS_ERROR_INVALID_PARAMETER;
129                 }
130         }
131
132         return MAPS_ERROR_NONE;
133 }
134
135 bool maps_area_is_valid(const maps_area_h area)
136 {
137         if (!maps_condition_check_maps_feature())
138                 return MAPS_ERROR_NOT_SUPPORTED;
139         if (!area) return false;
140
141         bool ret = true;
142         maps_area_s *handle = (maps_area_s *) area;
143
144         do {
145                 if (handle->type == MAPS_AREA_RECTANGLE) {
146                         maps_area_rectangle_s rect = handle->rect;
147
148                         if (!maps_coordinates_is_valid(&rect.top_left) ||
149                                 !maps_coordinates_is_valid(&rect.bottom_right)) {
150                                 ret = false;
151                                 break;
152                         }
153                 } else if (handle->type == MAPS_AREA_CIRCLE) {
154                         maps_area_circle_s cir = handle->circle;
155                         maps_coordinates_s center = cir.center;
156
157                         if (cir.radius <= 0) {
158                                 ret = false;
159                                 break;
160                         }
161                         if (!maps_coordinates_is_valid(&center)) {
162                                 ret = false;
163                                 break;
164                         }
165                 } else {
166                         ret = false;
167                 }
168         } while (false);
169
170         return ret;
171 }