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