2 * Copyright (c) 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_extra_types.h"
20 #include "maps_util.h"
21 #include "maps_preference.h"
22 #include "maps_condition.h"
24 const int _DEFAULT_ALTERNATIVES_SIZE = 2;
26 typedef struct _callback_data
28 maps_preference_properties_cb callback;
32 static bool __isnamed_preference(const char *key)
37 static guint names[] = {
38 g_str_hash(MAPS_PLACE_FILTER_TYPE),
39 g_str_hash(MAPS_PLACE_FILTER_SORT_BY),
40 g_str_hash(MAPS_ROUTE_FREEFORM_ADDR_TO_AVOID),
41 g_str_hash(MAPS_ROUTE_STRUCTED_ADDR_TO_AVOID),
42 g_str_hash(MAPS_ROUTE_CIRCLE_AREA_TO_AVOID),
43 g_str_hash(MAPS_ROUTE_RECT_AREA_TO_AVOID),
44 g_str_hash(MAPS_ROUTE_GEOMETRY_BOUNDING_BOX),
45 g_str_hash(MAPS_ROUTE_GEOMETRY_RETRIEVAL),
46 g_str_hash(MAPS_ROUTE_INSTRUCTION_GEOMETRY),
47 g_str_hash(MAPS_ROUTE_INSTRUCTION_BOUNDING_BOX),
48 g_str_hash(MAPS_ROUTE_INSTRUCTION_RETRIEVAL),
49 g_str_hash(MAPS_ROUTE_REALTIME_TRAFFIC),
50 g_str_hash("MAPS_PREFERENCE_DISTANCE_UNIT"),
51 g_str_hash("MAPS_PREFERENCE_LANGUAGE"),
52 g_str_hash("MAPS_PREFERENCE_MAX_RESULTS"),
53 g_str_hash("MAPS_PREFERENCE_COUNTRY_CODE"),
54 g_str_hash("MAPS_PREFERENCE_ROUTE_OPTIMIZATION"),
55 g_str_hash("MAPS_PREFERENCE_ROUTE_TRANSPORT_MODE"),
56 g_str_hash("MAPS_PREFERENCE_ROUTE_FEATURE_WEIGHT"),
57 g_str_hash("MAPS_PREFERENCE_ROUTE_FEATURE"),
58 g_str_hash("MAPS_PREFERENCE_ROUTE_ALTERNATIVES")
60 const int size = sizeof(names) / sizeof(names[0]);
62 guint key_hash = g_str_hash(key);
63 for (int i = 0; i < size; i++)
64 if (key_hash == names[i])
70 static bool __maps_preference_properties_helper_cb(int index, int total,
71 char *key, void *value,
74 /* Do not return the "named" preference, which can be obtained, */
75 /* with other functions from this module. */
76 /* The reason is that "named" preferences may be not only string type */
77 if (__isnamed_preference(key)) {
84 callback_data *cbd = (callback_data *) user_data;
87 return cbd->callback(index, total, key, (char *) value, cbd->user_data);
90 EXPORT_API int maps_preference_create(maps_preference_h *preference)
92 if (!maps_condition_check_maps_feature())
93 return MAPS_ERROR_NOT_SUPPORTED;
95 return MAPS_ERROR_INVALID_PARAMETER;
97 int error = maps_item_hashtable_create(preference);
98 if (error != MAPS_ERROR_NONE)
102 error = maps_item_hashtable_set_int(*preference,
103 "MAPS_PREFERENCE_DISTANCE_UNIT", MAPS_DISTANCE_UNIT_M);
104 if (error != MAPS_ERROR_NONE)
107 error = maps_item_hashtable_set_string(*preference,
108 "MAPS_PREFERENCE_LANGUAGE", "en-US");
109 if (error != MAPS_ERROR_NONE)
112 error = maps_item_hashtable_set_int(*preference,
113 "MAPS_PREFERENCE_MAX_RESULTS", 25);
114 if (error != MAPS_ERROR_NONE)
117 error = maps_item_hashtable_set_int(*preference,
118 "MAPS_PREFERENCE_ROUTE_ALTERNATIVES", 0);
119 if (error != MAPS_ERROR_NONE)
122 return MAPS_ERROR_NONE;
126 maps_item_hashtable_destroy(preference);
131 EXPORT_API int maps_preference_destroy(maps_preference_h preference)
133 if (!maps_condition_check_maps_feature())
134 return MAPS_ERROR_NOT_SUPPORTED;
136 return MAPS_ERROR_INVALID_PARAMETER;
137 return maps_item_hashtable_destroy(preference);
140 EXPORT_API int maps_preference_clone(const maps_preference_h origin,
141 maps_preference_h *cloned)
143 if (!maps_condition_check_maps_feature())
144 return MAPS_ERROR_NOT_SUPPORTED;
145 if (!cloned || !origin)
146 return MAPS_ERROR_INVALID_PARAMETER;
147 return maps_item_hashtable_clone(origin, cloned);
150 /*----------------------------------------------------------------------------*/
152 EXPORT_API int maps_preference_get_distance_unit(const maps_preference_h preference,
153 maps_distance_unit_e *unit)
155 if (!maps_condition_check_maps_feature())
156 return MAPS_ERROR_NOT_SUPPORTED;
157 if (!preference || !unit)
158 return MAPS_ERROR_INVALID_PARAMETER;
159 return maps_item_hashtable_get_int(preference,
160 "MAPS_PREFERENCE_DISTANCE_UNIT", (int *) unit);
163 EXPORT_API int maps_preference_get_language(const maps_preference_h preference,
166 if (!maps_condition_check_maps_feature())
167 return MAPS_ERROR_NOT_SUPPORTED;
168 if (!preference || !language)
169 return MAPS_ERROR_INVALID_PARAMETER;
170 return maps_item_hashtable_get_string(preference,
171 "MAPS_PREFERENCE_LANGUAGE", language);
174 EXPORT_API int maps_preference_get_max_results(const maps_preference_h preference,
177 if (!maps_condition_check_maps_feature())
178 return MAPS_ERROR_NOT_SUPPORTED;
179 if (!preference || !max_results)
180 return MAPS_ERROR_INVALID_PARAMETER;
181 return maps_item_hashtable_get_int(preference,
182 "MAPS_PREFERENCE_MAX_RESULTS", max_results);
185 EXPORT_API int maps_preference_get_country_code(const maps_preference_h preference,
188 if (!maps_condition_check_maps_feature())
189 return MAPS_ERROR_NOT_SUPPORTED;
190 if (!preference || !country_code)
191 return MAPS_ERROR_INVALID_PARAMETER;
192 return maps_item_hashtable_get_string(preference,
193 "MAPS_PREFERENCE_COUNTRY_CODE", country_code);
196 EXPORT_API int maps_preference_get_route_optimization(const maps_preference_h preference,
197 maps_route_optimization_e *optimization)
199 if (!maps_condition_check_maps_feature())
200 return MAPS_ERROR_NOT_SUPPORTED;
201 if (!preference || !optimization)
202 return MAPS_ERROR_INVALID_PARAMETER;
203 return maps_item_hashtable_get_int(preference,
204 "MAPS_PREFERENCE_ROUTE_OPTIMIZATION", (int *) optimization);
207 EXPORT_API int maps_preference_get_route_transport_mode(const maps_preference_h preference,
208 maps_route_transport_mode_e *transport_mode)
210 if (!maps_condition_check_maps_feature())
211 return MAPS_ERROR_NOT_SUPPORTED;
212 if (!preference || !transport_mode)
213 return MAPS_ERROR_INVALID_PARAMETER;
214 return maps_item_hashtable_get_int(preference,
215 "MAPS_PREFERENCE_ROUTE_TRANSPORT_MODE", (int *) transport_mode);
218 EXPORT_API int maps_preference_get_route_feature_weight(const maps_preference_h preference,
219 maps_route_feature_weight_e *feature_weight)
221 if (!maps_condition_check_maps_feature())
222 return MAPS_ERROR_NOT_SUPPORTED;
223 if (!preference || !feature_weight)
224 return MAPS_ERROR_INVALID_PARAMETER;
225 return maps_item_hashtable_get_int(preference,
226 "MAPS_PREFERENCE_ROUTE_FEATURE_WEIGHT", (int *) feature_weight);
229 EXPORT_API int maps_preference_get_route_feature(const maps_preference_h preference,
230 maps_route_feature_e * feature)
232 if (!maps_condition_check_maps_feature())
233 return MAPS_ERROR_NOT_SUPPORTED;
234 if (!preference || !feature)
235 return MAPS_ERROR_INVALID_PARAMETER;
236 return maps_item_hashtable_get_int(preference,
237 "MAPS_PREFERENCE_ROUTE_FEATURE", (int *) feature);
240 EXPORT_API int maps_preference_get_route_alternatives_enabled(
241 const maps_preference_h preference, bool *enable)
243 if (!maps_condition_check_maps_feature())
244 return MAPS_ERROR_NOT_SUPPORTED;
246 return MAPS_ERROR_INVALID_PARAMETER;
249 int error = maps_item_hashtable_get_int(preference, "MAPS_PREFERENCE_ROUTE_ALTERNATIVES", &alternatives);
250 if (error != MAPS_ERROR_NONE)
253 *enable = (alternatives == _DEFAULT_ALTERNATIVES_SIZE) ? true :false;
254 return MAPS_ERROR_NONE;
257 EXPORT_API int maps_preference_get(const maps_preference_h preference,
258 const char *key, char **value)
260 if (!maps_condition_check_maps_feature())
261 return MAPS_ERROR_NOT_SUPPORTED;
262 if (!preference || !key || !value)
263 return MAPS_ERROR_INVALID_PARAMETER;
264 return maps_item_hashtable_get_string(preference, key, value);
268 EXPORT_API int maps_preference_foreach_property(const maps_preference_h preference,
269 maps_preference_properties_cb callback, void *user_data)
271 if (!maps_condition_check_maps_feature())
272 return MAPS_ERROR_NOT_SUPPORTED;
273 if (!preference || !callback)
274 return MAPS_ERROR_INVALID_PARAMETER;
275 callback_data cd = { callback, user_data };
276 return maps_item_hashtable_foreach(preference,
277 __maps_preference_properties_helper_cb, &cd);
280 /*----------------------------------------------------------------------------*/
282 EXPORT_API int maps_preference_set_distance_unit(maps_preference_h preference,
283 const maps_distance_unit_e unit)
285 if (!maps_condition_check_maps_feature())
286 return MAPS_ERROR_NOT_SUPPORTED;
288 return MAPS_ERROR_INVALID_PARAMETER;
289 if ((unit < MAPS_DISTANCE_UNIT_M) || (unit > MAPS_DISTANCE_UNIT_YD))
290 return MAPS_ERROR_INVALID_PARAMETER;
291 return maps_item_hashtable_set_int(preference,
292 "MAPS_PREFERENCE_DISTANCE_UNIT", unit);
295 EXPORT_API int maps_preference_set_language(maps_preference_h preference,
296 const char *language)
298 if (!maps_condition_check_maps_feature())
299 return MAPS_ERROR_NOT_SUPPORTED;
300 if (!preference || !language)
301 return MAPS_ERROR_INVALID_PARAMETER;
302 return maps_item_hashtable_set_string(preference,
303 "MAPS_PREFERENCE_LANGUAGE", language);
306 EXPORT_API int maps_preference_set_max_results(maps_preference_h preference,
307 const int max_results)
309 if (!maps_condition_check_maps_feature())
310 return MAPS_ERROR_NOT_SUPPORTED;
311 if (!preference || max_results <= 0)
312 return MAPS_ERROR_INVALID_PARAMETER;
313 return maps_item_hashtable_set_int(preference,
314 "MAPS_PREFERENCE_MAX_RESULTS", max_results);
317 EXPORT_API int maps_preference_set_country_code(maps_preference_h preference,
318 const char *country_code)
320 if (!maps_condition_check_maps_feature())
321 return MAPS_ERROR_NOT_SUPPORTED;
322 if (!preference || !country_code)
323 return MAPS_ERROR_INVALID_PARAMETER;
324 return maps_item_hashtable_set_string(preference,
325 "MAPS_PREFERENCE_COUNTRY_CODE", country_code);
328 EXPORT_API int maps_preference_set_route_optimization(maps_preference_h preference,
329 const maps_route_optimization_e optimization)
331 if (!maps_condition_check_maps_feature())
332 return MAPS_ERROR_NOT_SUPPORTED;
334 return MAPS_ERROR_INVALID_PARAMETER;
335 if ((optimization < MAPS_ROUTE_TYPE_FASTEST)
336 || (optimization > MAPS_ROUTE_TYPE_DIRECTDRIVE))
337 return MAPS_ERROR_INVALID_PARAMETER;
338 return maps_item_hashtable_set_int(preference,
339 "MAPS_PREFERENCE_ROUTE_OPTIMIZATION", optimization);
342 EXPORT_API int maps_preference_set_route_transport_mode(maps_preference_h preference,
343 const maps_route_transport_mode_e transport_mode)
345 if (!maps_condition_check_maps_feature())
346 return MAPS_ERROR_NOT_SUPPORTED;
348 return MAPS_ERROR_INVALID_PARAMETER;
349 if ((transport_mode < MAPS_ROUTE_TRANSPORT_MODE_CAR)
350 || (transport_mode > MAPS_ROUTE_TRANSPORT_MODE_TRUCK))
351 return MAPS_ERROR_INVALID_PARAMETER;
352 return maps_item_hashtable_set_int(preference,
353 "MAPS_PREFERENCE_ROUTE_TRANSPORT_MODE", transport_mode);
356 EXPORT_API int maps_preference_set_route_feature_weight(maps_preference_h preference,
357 const maps_route_feature_weight_e feature_weight)
359 if (!maps_condition_check_maps_feature())
360 return MAPS_ERROR_NOT_SUPPORTED;
362 return MAPS_ERROR_INVALID_PARAMETER;
363 if ((feature_weight < MAPS_ROUTE_FEATURE_WEIGHT_NORMAL)
364 || (feature_weight > MAPS_ROUTE_FEATURE_WEIGHT_STRICTEXCLUDE))
365 return MAPS_ERROR_INVALID_PARAMETER;
366 return maps_item_hashtable_set_int(preference,
367 "MAPS_PREFERENCE_ROUTE_FEATURE_WEIGHT", feature_weight);
370 EXPORT_API int maps_preference_set_route_feature(maps_preference_h preference,
371 const maps_route_feature_e feature)
373 if (!maps_condition_check_maps_feature())
374 return MAPS_ERROR_NOT_SUPPORTED;
376 return MAPS_ERROR_INVALID_PARAMETER;
377 if ((feature < MAPS_ROUTE_FEATURE_NO)
378 || (feature > MAPS_ROUTE_FEATURE_STAIRS))
379 return MAPS_ERROR_INVALID_PARAMETER;
380 return maps_item_hashtable_set_int(preference,
381 "MAPS_PREFERENCE_ROUTE_FEATURE", feature);
384 EXPORT_API int maps_preference_set_route_alternatives_enabled(maps_preference_h preference,
387 if (!maps_condition_check_maps_feature())
388 return MAPS_ERROR_NOT_SUPPORTED;
390 return MAPS_ERROR_INVALID_PARAMETER;
392 int alternatives = 0;
394 alternatives = _DEFAULT_ALTERNATIVES_SIZE;
396 return maps_item_hashtable_set_int(preference, "MAPS_PREFERENCE_ROUTE_ALTERNATIVES", alternatives);
399 EXPORT_API int maps_preference_set_property(maps_preference_h preference,
400 const char *key, const char *value)
402 if (!maps_condition_check_maps_feature())
403 return MAPS_ERROR_NOT_SUPPORTED;
404 if (!preference || !key || !value)
405 return MAPS_ERROR_INVALID_PARAMETER;
406 return maps_item_hashtable_set_string(preference, key, value);