Add LCOV remarkers to increase line coverage rate
[platform/core/api/maps-service.git] / src / api / maps_coordinates.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_plugin_types.h"
20 #include "maps_coordinates.h"
21 #include "maps_util.h"
22 #include "maps_condition.h"
23
24 EXPORT_API int maps_coordinates_create(const double latitude,
25                                        const double longitude,
26                                        maps_coordinates_h *coords)
27 {
28         if (!maps_condition_check_maps_feature())
29                 return MAPS_ERROR_NOT_SUPPORTED;
30         if (!coords)
31                 return MAPS_ERROR_INVALID_PARAMETER;
32
33         MAPS_CHECK_CONDITION(latitude >= -90
34                 && latitude <= 90, MAPS_ERROR_INVALID_PARAMETER,
35                 "MAPS_ERROR_INVALID_PARAMETER");
36         MAPS_CHECK_CONDITION(longitude >= -180
37                 && longitude <= 180, MAPS_ERROR_INVALID_PARAMETER,
38                 "MAPS_ERROR_INVALID_PARAMETER");
39
40         maps_coordinates_s *coord = g_new0(maps_coordinates_s, 1);
41         if (coord == 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         coord->latitude = latitude;
48         coord->longitude = longitude;
49
50         *coords = (maps_coordinates_h) coord;
51         return MAPS_ERROR_NONE;
52 }
53
54 EXPORT_API int maps_coordinates_destroy(maps_coordinates_h coords)
55 {
56         if (!maps_condition_check_maps_feature())
57                 return MAPS_ERROR_NOT_SUPPORTED;
58         if (!coords)
59                 return MAPS_ERROR_INVALID_PARAMETER;
60
61         maps_coordinates_s *handle = (maps_coordinates_s *) coords;
62         g_free(handle);
63         coords = NULL;
64
65         return MAPS_ERROR_NONE;
66 }
67
68 EXPORT_API int maps_coordinates_clone(const maps_coordinates_h origin,
69                                       maps_coordinates_h *cloned)
70 {
71         if (!maps_condition_check_maps_feature())
72                 return MAPS_ERROR_NOT_SUPPORTED;
73         if (!cloned || !origin)
74                 return MAPS_ERROR_INVALID_PARAMETER;
75         int error = MAPS_ERROR_NONE;
76         do {
77                 maps_coordinates_s *c = (maps_coordinates_s *) origin;
78                 error = maps_coordinates_create(c->latitude, c->longitude, cloned);
79                 if (!(*cloned) or(error != MAPS_ERROR_NONE))
80                         break;
81                 return MAPS_ERROR_NONE;
82         } while (false);
83         //LCOV_EXCL_START
84         maps_coordinates_destroy(*cloned);
85         *cloned = NULL;
86         return error;
87         //LCOV_EXCL_STOP
88 }
89
90 /*----------------------------------------------------------------------------*/
91
92 EXPORT_API int maps_coordinates_get_latitude(const maps_coordinates_h coords,
93                                              double *latitude)
94 {
95         if (!maps_condition_check_maps_feature())
96                 return MAPS_ERROR_NOT_SUPPORTED;
97         if (!coords || !latitude)
98                 return MAPS_ERROR_INVALID_PARAMETER;
99         *latitude = ((maps_coordinates_s *) coords)->latitude;
100         return MAPS_ERROR_NONE;
101 }
102
103 EXPORT_API int maps_coordinates_get_longitude(const maps_coordinates_h coords,
104                                               double *longitude)
105 {
106         if (!maps_condition_check_maps_feature())
107                 return MAPS_ERROR_NOT_SUPPORTED;
108         if (!coords || !longitude)
109                 return MAPS_ERROR_INVALID_PARAMETER;
110         *longitude = ((maps_coordinates_s *) coords)->longitude;
111         return MAPS_ERROR_NONE;
112 }
113
114 EXPORT_API int maps_coordinates_get_latitude_longitude(const maps_coordinates_h coords,
115                                               double *latitude,
116                                               double *longitude)
117 {
118         if (!maps_condition_check_maps_feature())
119                 return MAPS_ERROR_NOT_SUPPORTED;
120         if (!coords || !latitude || !longitude)
121                 return MAPS_ERROR_INVALID_PARAMETER;
122         *latitude = ((maps_coordinates_s *) coords)->latitude;
123         *longitude = ((maps_coordinates_s *) coords)->longitude;
124         return MAPS_ERROR_NONE;
125 }
126
127 /*----------------------------------------------------------------------------*/
128
129 EXPORT_API int maps_coordinates_set_latitude(maps_coordinates_h coords,
130                                              const double latitude)
131 {
132         if (!maps_condition_check_maps_feature())
133                 return MAPS_ERROR_NOT_SUPPORTED;
134         if (!coords)
135                 return MAPS_ERROR_INVALID_PARAMETER;
136         MAPS_CHECK_CONDITION(latitude >= -90 && latitude <= 90,
137                 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
138         ((maps_coordinates_s *) coords)->latitude = latitude;
139         return MAPS_ERROR_NONE;
140 }
141
142 EXPORT_API int maps_coordinates_set_longitude(maps_coordinates_h coords,
143         const double longitude)
144 {
145         if (!maps_condition_check_maps_feature())
146                 return MAPS_ERROR_NOT_SUPPORTED;
147         if (!coords)
148                 return MAPS_ERROR_INVALID_PARAMETER;
149         MAPS_CHECK_CONDITION(longitude >= -180 && longitude <= 180,
150                 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
151         ((maps_coordinates_s *) coords)->longitude = longitude;
152         return MAPS_ERROR_NONE;
153 }
154
155 EXPORT_API int maps_coordinates_set_latitude_longitude(maps_coordinates_h coords,
156         const double latitude, const double longitude)
157 {
158         if (!maps_condition_check_maps_feature())
159                 return MAPS_ERROR_NOT_SUPPORTED;
160         if (!coords)
161                 return MAPS_ERROR_INVALID_PARAMETER;
162         MAPS_CHECK_CONDITION(latitude >= -90 && latitude <= 90,
163                 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
164         MAPS_CHECK_CONDITION(longitude >= -180 && longitude <= 180,
165                 MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER");
166         ((maps_coordinates_s *) coords)->latitude = latitude;
167         ((maps_coordinates_s *) coords)->longitude = longitude;
168         return MAPS_ERROR_NONE;
169 }
170
171 int _maps_coordinates_copy(maps_coordinates_h orig, maps_coordinates_h dest)
172 {
173         if (!orig || !dest)
174                 return MAPS_ERROR_INVALID_PARAMETER;
175
176         ((maps_coordinates_s *)dest)->latitude  = ((maps_coordinates_s *)orig)->latitude;
177         ((maps_coordinates_s *)dest)->longitude = ((maps_coordinates_s *)orig)->longitude;
178         return MAPS_ERROR_NONE;
179 }
180
181 /*
182  * Tizen 3.0
183  */
184
185 EXPORT_API int maps_coordinates_list_create(maps_coordinates_list_h *coordinates_list)
186 {
187         if (!maps_condition_check_maps_feature())
188                 return MAPS_ERROR_NOT_SUPPORTED;
189         if (!coordinates_list)
190                 return MAPS_ERROR_INVALID_PARAMETER;
191
192         GList *list = g_list_alloc();
193         MAPS_CHECK_CONDITION(list, MAPS_ERROR_OUT_OF_MEMORY, "MAPS_ERROR_OUT_OF_MEMORY");
194
195         *coordinates_list = (void *) list;
196
197         return MAPS_ERROR_NONE;
198 }
199
200 static void _free_coordinates(gpointer data)
201 {
202         if (!data) return;
203
204         int ret = 0;
205         maps_coordinates_s *coordinates = (maps_coordinates_s *) data;
206         ret = maps_coordinates_destroy(coordinates);
207         if (ret) {
208                 MAPS_LOGI("Failed to maps_coordinates_destroy!!!"); //LCOV_EXCL_LINE
209         }
210 }
211
212 EXPORT_API int maps_coordinates_list_destroy(maps_coordinates_list_h coordinates_list)
213 {
214         if (!maps_condition_check_maps_feature())
215                 return MAPS_ERROR_NOT_SUPPORTED;
216         if (!coordinates_list)
217                 return MAPS_ERROR_INVALID_PARAMETER;
218
219         GList *list = (GList *) coordinates_list;
220
221         g_list_free_full(list, (GDestroyNotify) _free_coordinates);
222         coordinates_list = NULL;
223
224         return MAPS_ERROR_NONE;
225 }
226
227 EXPORT_API int maps_coordinates_list_append(maps_coordinates_list_h coordinates_list, maps_coordinates_h coordinates)
228 {
229         if (!maps_condition_check_maps_feature())
230                 return MAPS_ERROR_NOT_SUPPORTED;
231         if (!coordinates_list || !coordinates)
232                 return MAPS_ERROR_INVALID_PARAMETER;
233
234         GList *list = (GList *)coordinates_list;
235         maps_coordinates_s *coord = (maps_coordinates_s *)coordinates;
236         list = g_list_append(list, coord);
237         coordinates_list = (void *) list;
238
239         return MAPS_ERROR_NONE;
240 }
241
242 EXPORT_API int maps_coordinates_list_remove(maps_coordinates_list_h coordinates_list, maps_coordinates_h coordinates)
243 {
244         if (!maps_condition_check_maps_feature())
245                 return MAPS_ERROR_NOT_SUPPORTED;
246         if (!coordinates_list || !coordinates)
247                 return MAPS_ERROR_INVALID_PARAMETER;
248
249         GList *list = (GList *)coordinates_list;
250         maps_coordinates_s *coord = (maps_coordinates_s *)coordinates;
251         list = g_list_remove(list, coord);
252         coordinates_list = (void *) list;
253
254         return MAPS_ERROR_NONE;
255 }
256
257 EXPORT_API int maps_coordinates_list_get_length(maps_coordinates_list_h coordinates_list, int *length)
258 {
259         if (!maps_condition_check_maps_feature())
260                 return MAPS_ERROR_NOT_SUPPORTED;
261         if (!coordinates_list || !length)
262                 return MAPS_ERROR_INVALID_PARAMETER;
263
264         *length = g_list_length((GList *)coordinates_list) - 1;
265
266         return MAPS_ERROR_NONE;
267 }
268
269 EXPORT_API int maps_coordinates_list_foreach(maps_coordinates_list_h coordinates_list, maps_coordinates_cb callback, void *user_data)
270 {
271         if (!maps_condition_check_maps_feature())
272                 return MAPS_ERROR_NOT_SUPPORTED;
273         if (!coordinates_list || !callback)
274                 return MAPS_ERROR_INVALID_PARAMETER;
275
276         bool ret = true;
277         int index = 0;
278
279         GList *list = g_list_first((GList *)coordinates_list);
280
281         GList *l;
282         for (l = (GList *)list; l != NULL; l = l->next)
283          {
284                 maps_coordinates_s *coordinates = (maps_coordinates_s *)l->data;
285                 if (coordinates) {
286                         ret = callback(index++, coordinates, user_data);
287                         if(!ret) break;
288                 }
289         }
290
291         return MAPS_ERROR_NONE;
292 }
293
294 bool maps_coordinates_is_valid(const maps_coordinates_h coordinates)
295 {
296         if (!coordinates)
297                 return false;
298
299         maps_coordinates_s *coord = (maps_coordinates_s *)coordinates;
300
301         MAPS_CHECK_CONDITION(coord->latitude >= -90 && coord->latitude <= 90, MAPS_ERROR_INVALID_PARAMETER,
302                 "MAPS_ERROR_INVALID_PARAMETER");
303         MAPS_CHECK_CONDITION(coord->longitude >= -180 && coord->longitude <= 180, MAPS_ERROR_INVALID_PARAMETER,
304                 "MAPS_ERROR_INVALID_PARAMETER");
305
306         return true;
307 }