Fixed svace defects
[platform/core/location/maps-plugin-mapquest.git] / src / mapquest / mapquest_route.c
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 <stdio.h>
18 #include <glib.h>
19 #include <pthread.h>
20 #include "mapquest_route.h"
21 #include "mapquest_types.h"
22 #include "mapquest_server_private.h"
23 #include "mapquest_debug.h"
24 #include "mapquest_queue.h"
25 #include "mapquest_restcurl.h"
26 #include "mapquest_util.h"
27
28 #define ROUTE_URL       "https://open.mapquestapi.com/directions/v2/route?key=%s&ambiguities=ignore&outFormat=json&shapeFormat=raw&generalize=0"
29
30 int query_route(gchar *maps_key, coords_s startPoint, coords_s endPoint, route_type type, route_feature_avoids avoids, route_driving_style style, GList *waypoints, gpointer user_data)
31 {
32         char url[1024] = "";
33         char tmpStr[512];
34
35         if (maps_key != NULL)
36                 snprintf(tmpStr, sizeof(tmpStr), ROUTE_URL, maps_key);
37         else
38                 snprintf(tmpStr, sizeof(tmpStr), ROUTE_URL, "null");
39
40         STRCPY(url, tmpStr);
41
42         STRCAT(url, "&unit=m"); /* Keeping default as miles and conversion will be done later */
43
44         if (type == ROUTE_TYPE_FASTEST)
45                 STRCAT(url, "&routeType=fastest");
46         else if (type == ROUTE_TYPE_SHORTEST)
47                 STRCAT(url, "&routeType=shortest");
48         else if (type == ROUTE_TYPE_PEDESTRIAN)
49                 STRCAT(url, "&routeType=pedestrian");
50         else if (type == ROUTE_TYPE_MULTIMODAL)
51                 STRCAT(url, "&routeType=multimodal");
52         else if (type == ROUTE_TYPE_BICYCLE)
53                 STRCAT(url, "&routeType=bicycle");
54
55         if (avoids == ROUTE_AVOID_LIMITED_ACCESS)
56                 STRCAT(url, "&avoids=Limited Access");
57         else if (avoids == ROUTE_AVOID_TOLL_ROAD)
58                 STRCAT(url, "&avoids=Tollroad");
59         else if (avoids == ROUTE_AVOID_FERRY)
60                 STRCAT(url, "&avoids=Ferry");
61         else if (avoids == ROUTE_AVOID_UNPAVED)
62                 STRCAT(url, "&avoids=Unpaved");
63         else if (avoids == ROUTE_AVOID_SEASONAL_CLOSURE)
64                 STRCAT(url, "&avoids=Approximate Seasonal Closure");
65         else if (avoids == ROUTE_AVOID_COUNTRY_BORDER_CROSSING)
66                 STRCAT(url, "&avoids=Country border crossing");
67
68         if (style == DRIVING_STYLE_NORMAL)
69                 STRCAT(url, "&drivingStyle=2");
70         else if (style == DRIVING_STYLE_CAUTIOUS)
71                 STRCAT(url, "&drivingStyle=1");
72         else if (style == DRIVING_STYLE_AGGRESSIVE)
73                 STRCAT(url, "&drivingStyle=3");
74
75         int length = g_list_length(waypoints);
76         if (length != 0) {
77                 int index = 0;
78                 GList *waypoints_list = NULL;
79                 waypoints_list = g_list_first(waypoints);
80
81                 while (waypoints_list) {
82
83                         coords_s *data = (coords_s *) waypoints_list->data;
84
85                         if (data) {
86                                 if (index == 0)
87                                         snprintf(tmpStr, sizeof(tmpStr), "&from=%f,%f", data->latitude, data->longitude);
88                                 else
89                                         snprintf(tmpStr, sizeof(tmpStr), "&to=%f,%f", data->latitude, data->longitude);
90                                 STRCAT(url, tmpStr);
91                         }
92
93                         waypoints_list = g_list_next(waypoints_list);
94                         index++;
95                 }
96         } else {
97                 snprintf(tmpStr, sizeof(tmpStr), "&from=%f,%f", startPoint.latitude, startPoint.longitude);
98                 STRCAT(url, tmpStr);
99
100                 snprintf(tmpStr, sizeof(tmpStr), "&to=%f,%f", endPoint.latitude, endPoint.longitude);
101                 STRCAT(url, tmpStr);
102         }
103
104         add_handle(url, REQ_TYPE_ROUTE, user_data);
105
106         return 0;
107 }