1 /* Copyright (c) 2010-2014 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "maps_error.h"
19 #include "maps_route_segment_plugin.h"
20 #include "maps_route_maneuver_plugin.h"
21 #include "maps_extra_types.h"
22 #include "maps_util.h"
23 #include "maps_route_segment_private.h"
24 #include "maps_condition.h"
26 static bool __is_supported(const maps_route_segment_h route_segment,
27 maps_service_data_e data)
29 bool supported = false;
30 _maps_route_segment_is_data_supported(route_segment, data, &supported);
34 /*----------------------------------------------------------------------------*/
36 typedef struct _maps_route_segment_s
38 maps_coordinates_h origin;
39 maps_coordinates_h destination;
40 maps_area_h bounding_box;
43 maps_item_list_h path; /*< path, list of maps_coordinates_h */
44 maps_item_list_h maneuvers; /*< list of maneuvers,
45 maps_route_maneuver_h */
47 /* The table of available data features */
48 maps_int_hashtable_h supported_data;
49 } maps_route_segment_s;
51 /*----------------------------------------------------------------------------*/
53 EXPORT_API int maps_route_segment_create(maps_route_segment_h *segment)
55 if (!maps_condition_check_maps_feature())
56 return MAPS_ERROR_NOT_SUPPORTED;
58 return MAPS_ERROR_INVALID_PARAMETER;
59 *segment = (maps_route_segment_h) g_slice_new0(maps_route_segment_s);
61 if (*segment == NULL) {
62 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
63 return MAPS_ERROR_OUT_OF_MEMORY;
66 return MAPS_ERROR_NONE;
69 EXPORT_API int maps_route_segment_destroy(maps_route_segment_h segment)
71 if (!maps_condition_check_maps_feature())
72 return MAPS_ERROR_NOT_SUPPORTED;
74 return MAPS_ERROR_INVALID_PARAMETER;
76 maps_route_segment_s *p = (maps_route_segment_s *) segment;
79 maps_coordinates_destroy(p->origin);
81 maps_coordinates_destroy(p->destination);
83 maps_area_destroy(p->bounding_box);
85 maps_item_list_remove_all(p->path, maps_coordinates_destroy);
86 maps_item_list_destroy(p->path);
89 maps_item_list_remove_all(p->maneuvers, maps_route_maneuver_destroy);
90 maps_item_list_destroy(p->maneuvers);
93 if (p->supported_data)
94 maps_int_hashtable_destroy(p->supported_data);
96 g_slice_free(maps_route_segment_s, segment);
97 return MAPS_ERROR_NONE;
100 EXPORT_API int maps_route_segment_clone(const maps_route_segment_h segment,
101 maps_route_segment_h *cloned)
103 if (!maps_condition_check_maps_feature())
104 return MAPS_ERROR_NOT_SUPPORTED;
105 if (!cloned || !segment)
106 return MAPS_ERROR_INVALID_PARAMETER;
108 int error = MAPS_ERROR_NONE;
110 error = maps_route_segment_create(cloned);
111 if (!(*cloned) || (error != MAPS_ERROR_NONE))
114 maps_route_segment_s *p = (maps_route_segment_s *) segment;
117 error = maps_route_segment_set_origin(*cloned, p->origin);
118 if (error != MAPS_ERROR_NONE)
122 if (p->destination) {
123 error = maps_route_segment_set_destination(*cloned, p->destination);
124 if (error != MAPS_ERROR_NONE)
128 if (p->bounding_box) {
129 error = maps_route_segment_set_bounding_box(*cloned, p->bounding_box);
130 if (error != MAPS_ERROR_NONE)
134 error = maps_route_segment_set_distance(*cloned, p->distance);
135 if (error != MAPS_ERROR_NONE)
138 error = maps_route_segment_set_duration(*cloned, p->duration);
139 if (error != MAPS_ERROR_NONE)
143 error = maps_route_segment_set_path(*cloned, p->path);
144 if (error != MAPS_ERROR_NONE)
149 error = maps_route_segment_set_maneuvers(*cloned, p->maneuvers);
150 if (error != MAPS_ERROR_NONE)
154 if (p->supported_data) {
155 error = _maps_route_segment_set_supported_data(*cloned, p->supported_data);
156 if (error != MAPS_ERROR_NONE)
160 return MAPS_ERROR_NONE;
163 maps_route_segment_destroy(*cloned);
168 /*----------------------------------------------------------------------------*/
170 EXPORT_API int maps_route_segment_get_origin(const maps_route_segment_h segment,
171 maps_coordinates_h *origin)
173 if (!maps_condition_check_maps_feature())
174 return MAPS_ERROR_NOT_SUPPORTED;
175 if (!segment || !origin)
176 return MAPS_ERROR_INVALID_PARAMETER;
177 if (!((maps_route_segment_s *) segment)->origin)
178 return MAPS_ERROR_NOT_FOUND;
179 return maps_coordinates_clone(((maps_route_segment_s *) segment)->origin, origin);
182 EXPORT_API int maps_route_segment_get_destination(const maps_route_segment_h segment,
183 maps_coordinates_h *destination)
185 if (!maps_condition_check_maps_feature())
186 return MAPS_ERROR_NOT_SUPPORTED;
187 if (!segment || !destination)
188 return MAPS_ERROR_INVALID_PARAMETER;
189 if (!((maps_route_segment_s *) segment)->destination)
190 return MAPS_ERROR_NOT_FOUND;
191 return maps_coordinates_clone(((maps_route_segment_s *) segment)->destination, destination);
194 EXPORT_API int maps_route_segment_get_bounding_box(const maps_route_segment_h segment,
195 maps_area_h *bounding_box)
197 if (!maps_condition_check_maps_feature())
198 return MAPS_ERROR_NOT_SUPPORTED;
199 if (!segment || !bounding_box)
200 return MAPS_ERROR_INVALID_PARAMETER;
201 if (!((maps_route_segment_s *) segment)->bounding_box)
202 return MAPS_ERROR_NOT_FOUND;
203 return maps_area_clone(((maps_route_segment_s *) segment)->bounding_box, bounding_box);
206 EXPORT_API int maps_route_segment_get_distance(const maps_route_segment_h segment,
209 if (!maps_condition_check_maps_feature())
210 return MAPS_ERROR_NOT_SUPPORTED;
211 if (!segment || !distance)
212 return MAPS_ERROR_INVALID_PARAMETER;
213 *distance = ((maps_route_segment_s *) segment)->distance;
214 return MAPS_ERROR_NONE;
217 EXPORT_API int maps_route_segment_get_duration(const maps_route_segment_h segment,
220 if (!maps_condition_check_maps_feature())
221 return MAPS_ERROR_NOT_SUPPORTED;
222 if (!segment || !duration)
223 return MAPS_ERROR_INVALID_PARAMETER;
224 *duration = ((maps_route_segment_s *) segment)->duration;
225 return MAPS_ERROR_NONE;
228 EXPORT_API int maps_route_segment_foreach_path(const maps_route_segment_h segment,
229 maps_route_segment_path_cb callback,
232 if (!maps_condition_check_maps_feature())
233 return MAPS_ERROR_NOT_SUPPORTED;
235 return MAPS_ERROR_INVALID_PARAMETER;
236 if (!__is_supported(segment, MAPS_ROUTE_SEGMENTS_PATH))
237 return MAPS_ERROR_NOT_SUPPORTED;
239 return MAPS_ERROR_INVALID_PARAMETER;
240 if (!((maps_route_segment_s *) segment)->path)
241 return MAPS_ERROR_NOT_FOUND;
243 return maps_item_list_foreach(((maps_route_segment_s *) segment)->path,
244 maps_coordinates_clone, callback, user_data);
247 EXPORT_API int maps_route_segment_foreach_maneuver(const maps_route_segment_h segment,
248 maps_route_segment_maneuver_cb callback,
251 if (!maps_condition_check_maps_feature())
252 return MAPS_ERROR_NOT_SUPPORTED;
254 return MAPS_ERROR_INVALID_PARAMETER;
255 if (!__is_supported(segment, MAPS_ROUTE_SEGMENTS_MANEUVERS))
256 return MAPS_ERROR_NOT_SUPPORTED;
258 return MAPS_ERROR_INVALID_PARAMETER;
259 if (!((maps_route_segment_s *) segment)->maneuvers)
260 return MAPS_ERROR_NOT_FOUND;
262 return maps_item_list_foreach(((maps_route_segment_s *) segment)->maneuvers,
263 maps_route_maneuver_clone, callback, user_data);
266 int _maps_route_segment_is_data_supported(const maps_route_segment_h segment,
267 maps_service_data_e data,
270 if (!segment || !supported)
271 return MAPS_ERROR_INVALID_PARAMETER;
273 maps_route_segment_s *s = (maps_route_segment_s *)segment;
274 if (!s->supported_data) {
275 /* This is a case when the "supported" flags are not set yet */
276 /* No need to limit access to fields */
278 return MAPS_ERROR_NONE;
282 return maps_int_hashtable_contains(s->supported_data, data, supported);
285 /*----------------------------------------------------------------------------*/
287 EXPORT_API int maps_route_segment_set_origin(maps_route_segment_h segment,
288 const maps_coordinates_h origin)
290 if (!maps_condition_check_maps_feature())
291 return MAPS_ERROR_NOT_SUPPORTED;
292 if (!segment || !origin)
293 return MAPS_ERROR_INVALID_PARAMETER;
294 maps_route_segment_s *p = (maps_route_segment_s *) segment;
296 maps_coordinates_destroy(p->origin);
297 return maps_coordinates_clone(origin, &p->origin);
300 EXPORT_API int maps_route_segment_set_destination(maps_route_segment_h segment,
301 const maps_coordinates_h destination)
303 if (!maps_condition_check_maps_feature())
304 return MAPS_ERROR_NOT_SUPPORTED;
305 if (!segment || !destination)
306 return MAPS_ERROR_INVALID_PARAMETER;
307 maps_route_segment_s *p = (maps_route_segment_s *) segment;
309 maps_coordinates_destroy(p->destination);
310 return maps_coordinates_clone(destination, &p->destination);
313 EXPORT_API int maps_route_segment_set_bounding_box(maps_route_segment_h segment,
314 const maps_area_h bounding_box)
316 if (!maps_condition_check_maps_feature())
317 return MAPS_ERROR_NOT_SUPPORTED;
318 if (!segment || !bounding_box)
319 return MAPS_ERROR_INVALID_PARAMETER;
320 maps_route_segment_s *p = (maps_route_segment_s *) segment;
322 maps_area_destroy(p->bounding_box);
323 return maps_area_clone(bounding_box, &p->bounding_box);
326 EXPORT_API int maps_route_segment_set_distance(maps_route_segment_h segment,
327 const double distance)
329 if (!maps_condition_check_maps_feature())
330 return MAPS_ERROR_NOT_SUPPORTED;
331 if (!segment || distance < 0)
332 return MAPS_ERROR_INVALID_PARAMETER;
333 ((maps_route_segment_s *) segment)->distance = distance;
334 return MAPS_ERROR_NONE;
337 EXPORT_API int maps_route_segment_set_duration(maps_route_segment_h segment,
340 if (!maps_condition_check_maps_feature())
341 return MAPS_ERROR_NOT_SUPPORTED;
342 if (!segment || duration < 0)
343 return MAPS_ERROR_INVALID_PARAMETER;
344 ((maps_route_segment_s *) segment)->duration = duration;
345 return MAPS_ERROR_NONE;
348 EXPORT_API int maps_route_segment_set_path(maps_route_segment_h segment,
349 const maps_item_list_h path)
351 if (!maps_condition_check_maps_feature())
352 return MAPS_ERROR_NOT_SUPPORTED;
353 if (!segment || !path)
354 return MAPS_ERROR_INVALID_PARAMETER;
355 maps_route_segment_s *s = (maps_route_segment_s *) segment;
357 maps_item_list_remove_all(s->path, maps_coordinates_destroy);
358 maps_item_list_destroy(s->path);
360 maps_item_list_clone(path, maps_coordinates_clone, &s->path);
361 return MAPS_ERROR_NONE;
364 EXPORT_API int maps_route_segment_set_maneuvers(maps_route_segment_h segment,
365 const maps_item_list_h maneuvers)
367 if (!maps_condition_check_maps_feature())
368 return MAPS_ERROR_NOT_SUPPORTED;
369 if (!segment || !maneuvers)
370 return MAPS_ERROR_INVALID_PARAMETER;
371 maps_route_segment_s *s = (maps_route_segment_s *) segment;
373 maps_item_list_remove_all(s->maneuvers,
374 maps_route_maneuver_destroy);
375 maps_item_list_destroy(s->maneuvers);
377 maps_item_list_clone(maneuvers, maps_route_maneuver_clone,
379 return MAPS_ERROR_NONE;
382 int _maps_route_segment_set_supported_data(maps_route_segment_h segment,
383 const maps_int_hashtable_h supported_data)
385 if (!maps_condition_check_maps_feature())
386 return MAPS_ERROR_NOT_SUPPORTED;
387 if (!segment || !supported_data)
388 return MAPS_ERROR_INVALID_PARAMETER;
389 maps_route_segment_s *p = (maps_route_segment_s *) segment;
390 if (p->supported_data)
391 maps_int_hashtable_destroy(p->supported_data);
392 return maps_int_hashtable_clone(supported_data, &p->supported_data);