1852e3e6c7b017f567c9db154970890a37b9435a
[platform/core/api/maps-service.git] / src / api / maps_route_maneuver.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_route_maneuver_plugin.h"
20 #include "maps_extra_types.h"
21 #include "maps_util.h"
22 #include "maps_condition.h"
23
24 typedef struct _maps_route_maneuver_s
25 {
26         maps_route_direction_e direction_id;
27         maps_route_turn_type_e turn_type;
28         maps_coordinates_h position;
29         char *instruction_text;
30         char *road_name;
31         char *locale;           /* locale of instruction_text; */
32         int time_to_next_instruction;   /* duration */
33         double distance_to_next_instruction;    /* distance */
34 } maps_route_maneuver_s;
35
36 const gsize _MAPS_ROUTE_MANEUVER_ROAD_NAME_MAX_LENGTH = MAPS_BASE_NAME_MAX_LEN;
37 const gsize _MAPS_ROUTE_MANEUVER_INSTRUCTION_TEXT_MAX_LENGTH = MAPS_BASE_DESC_MAX_LEN;
38 const gsize _MAPS_ROUTE_MANEUVER_LOCALE_MAX_LENGTH = MAPS_BASE_TYPE_MAX_LEN;
39
40 /*----------------------------------------------------------------------------*/
41
42 EXPORT_API int maps_route_maneuver_create(maps_route_maneuver_h *maneuver)
43 {
44         if (!maps_condition_check_maps_feature())
45                 return MAPS_ERROR_NOT_SUPPORTED;
46         if (!maneuver)
47                 return MAPS_ERROR_INVALID_PARAMETER;
48         *maneuver = (maps_route_maneuver_h) g_slice_new0(maps_route_maneuver_s);
49
50         if (*maneuver == NULL) {
51                 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
52                 return MAPS_ERROR_OUT_OF_MEMORY;
53         }
54
55         return MAPS_ERROR_NONE;
56 }
57
58 EXPORT_API int maps_route_maneuver_destroy(maps_route_maneuver_h maneuver)
59 {
60         if (!maps_condition_check_maps_feature())
61                 return MAPS_ERROR_NOT_SUPPORTED;
62         if (!maneuver)
63                 return MAPS_ERROR_INVALID_PARAMETER;
64
65         maps_route_maneuver_s *p = (maps_route_maneuver_s *) maneuver;
66
67         if (p->position)
68                 maps_coordinates_destroy(p->position);
69         if (p->instruction_text)
70                 g_free(p->instruction_text);
71         if (p->road_name)
72                 g_free(p->road_name);
73         if (p->locale)
74                 g_free(p->locale);
75
76         g_slice_free(maps_route_maneuver_s, maneuver);
77         return MAPS_ERROR_NONE;
78 }
79
80 EXPORT_API int maps_route_maneuver_clone(const maps_route_maneuver_h origin,
81                                                                 maps_route_maneuver_h *cloned)
82 {
83         if (!maps_condition_check_maps_feature())
84                 return MAPS_ERROR_NOT_SUPPORTED;
85         if (!cloned || !origin)
86                 return MAPS_ERROR_INVALID_PARAMETER;
87
88         int error = MAPS_ERROR_NONE;
89         do {
90                 error = maps_route_maneuver_create(cloned);
91                 if (!(*cloned) || (error != MAPS_ERROR_NONE))
92                         break;
93
94                 maps_route_maneuver_s *p = (maps_route_maneuver_s *) origin;
95
96                 error = maps_route_maneuver_set_direction_id(*cloned, p->direction_id);
97                 if (error != MAPS_ERROR_NONE)
98                         break;
99
100                 error = maps_route_maneuver_set_turn_type(*cloned, p->turn_type);
101                 if (error != MAPS_ERROR_NONE)
102                         break;
103
104                 if (p->position) {
105                         error = maps_route_maneuver_set_position(*cloned, p->position);
106                         if (error != MAPS_ERROR_NONE)
107                                 break;
108                 }
109
110                 if (p->instruction_text) {
111                         error = maps_route_maneuver_set_instruction_text(*cloned, p->instruction_text);
112                         if (error != MAPS_ERROR_NONE)
113                                 break;
114                 }
115
116                 if (p->road_name) {
117                         error = maps_route_maneuver_set_road_name(*cloned, p->road_name);
118                         if (error != MAPS_ERROR_NONE)
119                                 break;
120                 }
121
122                 if (p->locale) {
123                         error = maps_route_maneuver_set_locale(*cloned, p->locale);
124                         if (error != MAPS_ERROR_NONE)
125                                 break;
126                 }
127
128                 error = maps_route_maneuver_set_time_to_next_instruction
129                         (*cloned, p->time_to_next_instruction);
130                 if (error != MAPS_ERROR_NONE)
131                         break;
132
133                 error = maps_route_maneuver_set_distance_to_next_instruction
134                         (*cloned, p->distance_to_next_instruction);
135                 if (error != MAPS_ERROR_NONE)
136                         break;
137
138                 return MAPS_ERROR_NONE;
139         } while (false);
140
141         maps_route_maneuver_destroy(*cloned);
142         *cloned = NULL;
143         return error;
144 }
145
146 /*----------------------------------------------------------------------------*/
147
148 EXPORT_API int maps_route_maneuver_get_direction_id(const maps_route_maneuver_h maneuver,
149                                                                 maps_route_direction_e *direction_id)
150 {
151         if (!maps_condition_check_maps_feature())
152                 return MAPS_ERROR_NOT_SUPPORTED;
153         if (!maneuver || !direction_id)
154                 return MAPS_ERROR_INVALID_PARAMETER;
155         *direction_id = ((maps_route_maneuver_s *) maneuver)->direction_id;
156         return MAPS_ERROR_NONE;
157 }
158
159 EXPORT_API int maps_route_maneuver_get_turn_type(const maps_route_maneuver_h maneuver,
160                                                                 maps_route_turn_type_e *turn_type)
161 {
162         if (!maps_condition_check_maps_feature())
163                 return MAPS_ERROR_NOT_SUPPORTED;
164         if (!maneuver || !turn_type)
165                 return MAPS_ERROR_INVALID_PARAMETER;
166         *turn_type = ((maps_route_maneuver_s *) maneuver)->turn_type;
167         return MAPS_ERROR_NONE;
168 }
169
170 EXPORT_API int maps_route_maneuver_get_position(const maps_route_maneuver_h maneuver,
171                                                                 maps_coordinates_h *position)
172 {
173         if (!maps_condition_check_maps_feature())
174                 return MAPS_ERROR_NOT_SUPPORTED;
175         if (!maneuver || !position)
176                 return MAPS_ERROR_INVALID_PARAMETER;
177         if (!((maps_route_maneuver_s *) maneuver)->position)
178                 return MAPS_ERROR_NOT_FOUND;
179         return maps_coordinates_clone(((maps_route_maneuver_s *) maneuver)->position, position);
180 }
181
182 EXPORT_API int maps_route_maneuver_get_road_name(const maps_route_maneuver_h maneuver,
183                                                                 char ** road_name)
184 {
185         if (!maps_condition_check_maps_feature())
186                 return MAPS_ERROR_NOT_SUPPORTED;
187         if (!maneuver || !road_name)
188                 return MAPS_ERROR_INVALID_PARAMETER;
189         if (!((maps_route_maneuver_s *) maneuver)->road_name)
190                 return MAPS_ERROR_NOT_FOUND;
191         return maps_get_string(((maps_route_maneuver_s *) maneuver)->road_name,
192                 _MAPS_ROUTE_MANEUVER_ROAD_NAME_MAX_LENGTH, road_name);
193 }
194
195 EXPORT_API int maps_route_maneuver_get_instruction_text(const maps_route_maneuver_h maneuver,
196                                                                 char ** instruction_text)
197 {
198         if (!maps_condition_check_maps_feature())
199                 return MAPS_ERROR_NOT_SUPPORTED;
200         if (!maneuver || !instruction_text)
201                 return MAPS_ERROR_INVALID_PARAMETER;
202         if (!((maps_route_maneuver_s *) maneuver)->instruction_text)
203                 return MAPS_ERROR_NOT_FOUND;
204         return maps_get_string(((maps_route_maneuver_s *) maneuver)->instruction_text,
205                 _MAPS_ROUTE_MANEUVER_INSTRUCTION_TEXT_MAX_LENGTH, instruction_text);
206 }
207
208 EXPORT_API int maps_route_maneuver_get_locale(const maps_route_maneuver_h maneuver,
209                                                                 char **locale)
210 {
211         if (!maps_condition_check_maps_feature())
212                 return MAPS_ERROR_NOT_SUPPORTED;
213         if (!maneuver || !locale)
214                 return MAPS_ERROR_INVALID_PARAMETER;
215         if (!((maps_route_maneuver_s *) maneuver)->locale)
216                 return MAPS_ERROR_NOT_FOUND;
217         return maps_get_string(((maps_route_maneuver_s *) maneuver)->locale,
218                 _MAPS_ROUTE_MANEUVER_LOCALE_MAX_LENGTH, locale);
219 }
220
221 EXPORT_API int maps_route_maneuver_get_time_to_next_instruction(const
222                                                                 maps_route_maneuver_h maneuver,
223                                                                 int *time_to_next_instruction)
224 {
225         if (!maps_condition_check_maps_feature())
226                 return MAPS_ERROR_NOT_SUPPORTED;
227         if (!maneuver || !time_to_next_instruction)
228                 return MAPS_ERROR_INVALID_PARAMETER;
229         *time_to_next_instruction =
230                 ((maps_route_maneuver_s *) maneuver)->time_to_next_instruction;
231         return MAPS_ERROR_NONE;
232 }
233
234 EXPORT_API int maps_route_maneuver_get_distance_to_next_instruction(const
235                                                                 maps_route_maneuver_h maneuver,
236                                                                 double *distance_to_next_instruction)
237 {
238         if (!maps_condition_check_maps_feature())
239                 return MAPS_ERROR_NOT_SUPPORTED;
240         if (!maneuver || !distance_to_next_instruction)
241                 return MAPS_ERROR_INVALID_PARAMETER;
242         *distance_to_next_instruction =
243                 ((maps_route_maneuver_s *) maneuver)->distance_to_next_instruction;
244         return MAPS_ERROR_NONE;
245 }
246
247 /*----------------------------------------------------------------------------*/
248
249 EXPORT_API int maps_route_maneuver_set_direction_id(maps_route_maneuver_h maneuver,
250                                                                 const maps_route_direction_e direction_id)
251 {
252         if (!maps_condition_check_maps_feature())
253                 return MAPS_ERROR_NOT_SUPPORTED;
254         if (!maneuver)
255                 return MAPS_ERROR_INVALID_PARAMETER;
256         if ((direction_id < MAPS_ROUTE_DIRECTION_NONE)
257            || (direction_id > MAPS_ROUTE_DIRECTION_EAST))
258                 return MAPS_ERROR_INVALID_PARAMETER;
259         ((maps_route_maneuver_s *) maneuver)->direction_id = direction_id;
260         return MAPS_ERROR_NONE;
261 }
262
263 EXPORT_API int maps_route_maneuver_set_turn_type(maps_route_maneuver_h maneuver,
264                                                                 const maps_route_turn_type_e turn_type)
265 {
266         if (!maps_condition_check_maps_feature())
267                 return MAPS_ERROR_NOT_SUPPORTED;
268         if (!maneuver)
269                 return MAPS_ERROR_INVALID_PARAMETER;
270         if ((turn_type < MAPS_ROUTE_TURN_TYPE_NONE)
271            || (turn_type > MAPS_ROUTE_TURN_TYPE_STRAIGHT_FORK))
272                 return MAPS_ERROR_INVALID_PARAMETER;
273         ((maps_route_maneuver_s *) maneuver)->turn_type = turn_type;
274         return MAPS_ERROR_NONE;
275 }
276
277 EXPORT_API int maps_route_maneuver_set_position(maps_route_maneuver_h maneuver,
278                                                                 const maps_coordinates_h position)
279 {
280         if (!maps_condition_check_maps_feature())
281                 return MAPS_ERROR_NOT_SUPPORTED;
282         if (!maneuver || !position)
283                 return MAPS_ERROR_INVALID_PARAMETER;
284         maps_route_maneuver_s *p = (maps_route_maneuver_s *) maneuver;
285         if (p->position)
286                 maps_coordinates_destroy(p->position);
287         return maps_coordinates_clone(position, &p->position);
288 }
289
290 EXPORT_API int maps_route_maneuver_set_road_name(maps_route_maneuver_h maneuver,
291                                                                 const char *road_name)
292 {
293         if (!maps_condition_check_maps_feature())
294                 return MAPS_ERROR_NOT_SUPPORTED;
295         if (!maneuver || !road_name)
296                 return MAPS_ERROR_INVALID_PARAMETER;
297         return maps_set_string(road_name,
298                 _MAPS_ROUTE_MANEUVER_ROAD_NAME_MAX_LENGTH,
299                 &((maps_route_maneuver_s *) maneuver)->road_name);
300 }
301
302 EXPORT_API int maps_route_maneuver_set_instruction_text(maps_route_maneuver_h maneuver,
303                                                                 const char * instruction_text)
304 {
305         if (!maps_condition_check_maps_feature())
306                 return MAPS_ERROR_NOT_SUPPORTED;
307         if (!maneuver || !instruction_text)
308                 return MAPS_ERROR_INVALID_PARAMETER;
309         return maps_set_string(instruction_text,
310                 _MAPS_ROUTE_MANEUVER_INSTRUCTION_TEXT_MAX_LENGTH,
311                 &((maps_route_maneuver_s *) maneuver)->instruction_text);
312 }
313
314 EXPORT_API int maps_route_maneuver_set_locale(maps_route_maneuver_h maneuver,
315                                                                 const char *locale)
316 {
317         if (!maps_condition_check_maps_feature())
318                 return MAPS_ERROR_NOT_SUPPORTED;
319         if (!maneuver || !locale)
320                 return MAPS_ERROR_INVALID_PARAMETER;
321         maps_route_maneuver_s *p = (maps_route_maneuver_s *) maneuver;
322         return maps_set_string(locale, _MAPS_ROUTE_MANEUVER_LOCALE_MAX_LENGTH, &p->locale);
323 }
324
325 EXPORT_API int maps_route_maneuver_set_time_to_next_instruction(
326                                                                 maps_route_maneuver_h maneuver,
327                                                                 const int time_to_next_instruction)
328 {
329         if (!maps_condition_check_maps_feature())
330                 return MAPS_ERROR_NOT_SUPPORTED;
331         if (!maneuver || time_to_next_instruction < 0)
332                 return MAPS_ERROR_INVALID_PARAMETER;
333         ((maps_route_maneuver_s *) maneuver)->time_to_next_instruction =
334                 time_to_next_instruction;
335         return MAPS_ERROR_NONE;
336 }
337
338 EXPORT_API int maps_route_maneuver_set_distance_to_next_instruction(
339                                                                 maps_route_maneuver_h maneuver,
340                                                                 const double distance_to_next_instruction)
341 {
342         if (!maps_condition_check_maps_feature())
343                 return MAPS_ERROR_NOT_SUPPORTED;
344         if (!maneuver || distance_to_next_instruction < 0)
345                 return MAPS_ERROR_INVALID_PARAMETER;
346         ((maps_route_maneuver_s *) maneuver)->distance_to_next_instruction =
347                 distance_to_next_instruction;
348         return MAPS_ERROR_NONE;
349 }