Add LCOV remarkers to increase line coverage rate
[platform/core/api/maps-service.git] / src / api / maps_preference.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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_extra_types.h"
20 #include "maps_util.h"
21 #include "maps_preference.h"
22 #include "maps_condition.h"
23
24 const int _DEFAULT_ALTERNATIVES_SIZE = 2;
25
26 typedef struct _callback_data
27 {
28         maps_preference_properties_cb callback;
29         void *user_data;
30 } callback_data;
31
32 static bool __isnamed_preference(const char *key)
33 {
34         if (!key)
35                 return false;
36
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")
59         };
60         const int size = sizeof(names) / sizeof(names[0]);
61
62         guint key_hash = g_str_hash(key);
63         for (int i = 0; i < size; i++)
64                 if (key_hash == names[i])
65                         return true;
66
67         return false;
68 }
69
70 static bool __maps_preference_properties_helper_cb(int index, int total,
71                                                                 char *key, void *value,
72                                                                 void *user_data)
73 {
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)) {
78                 g_free(key);
79                 g_free(value);
80                 return true;
81         }
82         if (!user_data)
83                 return false;
84         callback_data *cbd = (callback_data *) user_data;
85         if (!cbd->callback)
86                 return false;
87         return cbd->callback(index, total, key, (char *) value, cbd->user_data);
88 }
89
90 EXPORT_API int maps_preference_create(maps_preference_h *preference)
91 {
92         if (!maps_condition_check_maps_feature())
93                 return MAPS_ERROR_NOT_SUPPORTED;
94         if (!preference)
95                 return MAPS_ERROR_INVALID_PARAMETER;
96
97         int error = maps_item_hashtable_create(preference);
98         if (error != MAPS_ERROR_NONE)
99                 return error;
100
101         do {
102                 error = maps_item_hashtable_set_int(*preference,
103                                                                 "MAPS_PREFERENCE_DISTANCE_UNIT", MAPS_DISTANCE_UNIT_M);
104                 if (error != MAPS_ERROR_NONE)
105                         break;
106
107                 error = maps_item_hashtable_set_string(*preference,
108                                                                 "MAPS_PREFERENCE_LANGUAGE", "en-US");
109                 if (error != MAPS_ERROR_NONE)
110                         break;
111
112                 error = maps_item_hashtable_set_int(*preference,
113                                                                 "MAPS_PREFERENCE_MAX_RESULTS", 25);
114                 if (error != MAPS_ERROR_NONE)
115                         break;
116
117                 error = maps_item_hashtable_set_int(*preference,
118                                                                 "MAPS_PREFERENCE_ROUTE_ALTERNATIVES", 0);
119                 if (error != MAPS_ERROR_NONE)
120                         break;
121
122                 return MAPS_ERROR_NONE;
123         } while (0);
124
125         //LCOV_EXCL_START
126         maps_item_hashtable_destroy(preference);
127         return error;
128         //LCOV_EXCL_STOP
129 }
130
131 EXPORT_API int maps_preference_destroy(maps_preference_h preference)
132 {
133         if (!maps_condition_check_maps_feature())
134                 return MAPS_ERROR_NOT_SUPPORTED;
135         if (!preference)
136                 return MAPS_ERROR_INVALID_PARAMETER;
137         return maps_item_hashtable_destroy(preference);
138 }
139
140 EXPORT_API int maps_preference_clone(const maps_preference_h origin,
141                                                                 maps_preference_h *cloned)
142 {
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);
148 }
149
150 /*----------------------------------------------------------------------------*/
151
152 EXPORT_API int maps_preference_get_distance_unit(const maps_preference_h preference,
153                                                                 maps_distance_unit_e *unit)
154 {
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);
161 }
162
163 EXPORT_API int maps_preference_get_language(const maps_preference_h preference,
164                                                                 char **language)
165 {
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);
172 }
173
174 EXPORT_API int maps_preference_get_max_results(const maps_preference_h preference,
175                                                                 int *max_results)
176 {
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);
183 }
184
185 EXPORT_API int maps_preference_get_country_code(const maps_preference_h preference,
186                                                                 char **country_code)
187 {
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);
194 }
195
196 EXPORT_API int maps_preference_get_route_optimization(const maps_preference_h preference,
197                                                                 maps_route_optimization_e *optimization)
198 {
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);
205 }
206
207 EXPORT_API int maps_preference_get_route_transport_mode(const maps_preference_h preference,
208                                                                 maps_route_transport_mode_e *transport_mode)
209 {
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);
216 }
217
218 EXPORT_API int maps_preference_get_route_feature_weight(const maps_preference_h preference,
219                                                                 maps_route_feature_weight_e *feature_weight)
220 {
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);
227 }
228
229 EXPORT_API int maps_preference_get_route_feature(const maps_preference_h preference,
230                                                                 maps_route_feature_e * feature)
231 {
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);
238 }
239
240 EXPORT_API int maps_preference_get_route_alternatives_enabled(
241                                                                 const maps_preference_h preference, bool *enable)
242 {
243         if (!maps_condition_check_maps_feature())
244                 return MAPS_ERROR_NOT_SUPPORTED;
245         if (!preference)
246                 return MAPS_ERROR_INVALID_PARAMETER;
247
248         int alternatives;
249         int error = maps_item_hashtable_get_int(preference, "MAPS_PREFERENCE_ROUTE_ALTERNATIVES", &alternatives);
250         if (error != MAPS_ERROR_NONE)
251                 return error;
252
253         *enable = (alternatives == _DEFAULT_ALTERNATIVES_SIZE) ? true :false;
254         return MAPS_ERROR_NONE;
255 }
256
257 EXPORT_API int maps_preference_get(const maps_preference_h preference,
258                                    const char *key, char **value)
259 {
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);
265 }
266
267
268 EXPORT_API int maps_preference_foreach_property(const maps_preference_h preference,
269                                                                 maps_preference_properties_cb callback, void *user_data)
270 {
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);
278 }
279
280 /*----------------------------------------------------------------------------*/
281
282 EXPORT_API int maps_preference_set_distance_unit(maps_preference_h preference,
283                                                                 const maps_distance_unit_e unit)
284 {
285         if (!maps_condition_check_maps_feature())
286                 return MAPS_ERROR_NOT_SUPPORTED;
287         if (!preference)
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);
293 }
294
295 EXPORT_API int maps_preference_set_language(maps_preference_h preference,
296                                                                 const char *language)
297 {
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);
304 }
305
306 EXPORT_API int maps_preference_set_max_results(maps_preference_h preference,
307                                                                 const int max_results)
308 {
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);
315 }
316
317 EXPORT_API int maps_preference_set_country_code(maps_preference_h preference,
318                                                                 const char *country_code)
319 {
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);
326 }
327
328 EXPORT_API int maps_preference_set_route_optimization(maps_preference_h preference,
329                                                                 const maps_route_optimization_e optimization)
330 {
331         if (!maps_condition_check_maps_feature())
332                 return MAPS_ERROR_NOT_SUPPORTED;
333         if (!preference)
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);
340 }
341
342 EXPORT_API int maps_preference_set_route_transport_mode(maps_preference_h preference,
343                                                                 const maps_route_transport_mode_e transport_mode)
344 {
345         if (!maps_condition_check_maps_feature())
346                 return MAPS_ERROR_NOT_SUPPORTED;
347         if (!preference)
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);
354 }
355
356 EXPORT_API int maps_preference_set_route_feature_weight(maps_preference_h preference,
357                                                                 const maps_route_feature_weight_e feature_weight)
358 {
359         if (!maps_condition_check_maps_feature())
360                 return MAPS_ERROR_NOT_SUPPORTED;
361         if (!preference)
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);
368 }
369
370 EXPORT_API int maps_preference_set_route_feature(maps_preference_h preference,
371                                                                 const maps_route_feature_e feature)
372 {
373         if (!maps_condition_check_maps_feature())
374                 return MAPS_ERROR_NOT_SUPPORTED;
375         if (!preference)
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);
382 }
383
384 EXPORT_API int maps_preference_set_route_alternatives_enabled(maps_preference_h preference,
385                                                                 bool enable)
386 {
387         if (!maps_condition_check_maps_feature())
388                 return MAPS_ERROR_NOT_SUPPORTED;
389         if (!preference)
390                 return MAPS_ERROR_INVALID_PARAMETER;
391
392         int alternatives = 0;
393         if (enable)
394                 alternatives = _DEFAULT_ALTERNATIVES_SIZE;
395
396         return maps_item_hashtable_set_int(preference, "MAPS_PREFERENCE_ROUTE_ALTERNATIVES", alternatives);
397 }
398
399 EXPORT_API int maps_preference_set_property(maps_preference_h preference,
400                                             const char *key, const char *value)
401 {
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);
407 }