Merge remote-tracking branch 'origin/tizen' into tizen_3.0
[platform/core/location/maps-plugin-here.git] / src / here_utils.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 "here_utils.h"
18 #include <common/CommunicationError.h>
19
20
21 static const double LATITUDE_RANGE = 85.05113;
22 static const double LONGITUDE_RANGE = 180.0;
23
24
25 extern "C"
26 {
27 int ConvertToMapsError(int nErr)
28 {
29         switch (nErr) {
30         case HERE_ERROR_NONE:                  return MAPS_ERROR_NONE;
31         case HERE_ERROR_PERMISSION_DENIED:     return MAPS_ERROR_PERMISSION_DENIED;
32         case HERE_ERROR_OUT_OF_MEMORY:         return MAPS_ERROR_OUT_OF_MEMORY;
33         case HERE_ERROR_INVALID_PARAMETER:     return MAPS_ERROR_INVALID_PARAMETER;
34         case HERE_ERROR_NOT_SUPPORTED:         return MAPS_ERROR_NOT_SUPPORTED;
35         case HERE_ERROR_CONNECTION_TIME_OUT:   return MAPS_ERROR_CONNECTION_TIME_OUT;
36         case HERE_ERROR_NETWORK_UNREACHABLE:   return MAPS_ERROR_NETWORK_UNREACHABLE;
37         case HERE_ERROR_INVALID_OPERATION:     return MAPS_ERROR_INVALID_OPERATION;
38         case HERE_ERROR_KEY_NOT_AVAILABLE:     return MAPS_ERROR_KEY_NOT_AVAILABLE;
39         case HERE_ERROR_RESOURCE_BUSY:         return MAPS_ERROR_RESOURCE_BUSY;
40         case HERE_ERROR_CANCELED:              return MAPS_ERROR_CANCELED;
41         case HERE_ERROR_UNKNOWN:               return MAPS_ERROR_UNKNOWN;
42         case HERE_ERROR_SERVICE_NOT_AVAILABLE: return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
43         case HERE_ERROR_NOT_FOUND:             return MAPS_ERROR_NOT_FOUND;
44         case HERE_ERROR_USER_NOT_CONSENTED:    return MAPS_ERROR_USER_NOT_CONSENTED;
45         }
46         return MAPS_ERROR_UNKNOWN;
47 }
48
49 int ConvertToHereError(int nErr)
50 {
51         switch (nErr) {
52         case MAPS_ERROR_NONE:                  return HERE_ERROR_NONE;
53         case MAPS_ERROR_PERMISSION_DENIED:     return HERE_ERROR_PERMISSION_DENIED;
54         case MAPS_ERROR_OUT_OF_MEMORY:         return HERE_ERROR_OUT_OF_MEMORY;
55         case MAPS_ERROR_INVALID_PARAMETER:     return HERE_ERROR_INVALID_PARAMETER;
56         case MAPS_ERROR_NOT_SUPPORTED:         return HERE_ERROR_NOT_SUPPORTED;
57         case MAPS_ERROR_CONNECTION_TIME_OUT:   return HERE_ERROR_CONNECTION_TIME_OUT;
58         case MAPS_ERROR_NETWORK_UNREACHABLE:   return HERE_ERROR_NETWORK_UNREACHABLE;
59         case MAPS_ERROR_INVALID_OPERATION:     return HERE_ERROR_INVALID_OPERATION;
60         case MAPS_ERROR_KEY_NOT_AVAILABLE:     return HERE_ERROR_KEY_NOT_AVAILABLE;
61         case MAPS_ERROR_RESOURCE_BUSY:         return HERE_ERROR_RESOURCE_BUSY;
62         case MAPS_ERROR_CANCELED:              return HERE_ERROR_CANCELED;
63         case MAPS_ERROR_UNKNOWN:               return HERE_ERROR_UNKNOWN;
64         case MAPS_ERROR_SERVICE_NOT_AVAILABLE: return HERE_ERROR_SERVICE_NOT_AVAILABLE;
65         case MAPS_ERROR_NOT_FOUND:             return HERE_ERROR_NOT_FOUND;
66         case MAPS_ERROR_USER_NOT_CONSENTED:    return HERE_ERROR_USER_NOT_CONSENTED;
67         }
68         return HERE_ERROR_UNKNOWN;
69 }
70
71 const char* ConverHereErrorToString(int nErr)
72 {
73         switch (nErr) {
74         case HERE_ERROR_NONE:                  return "No errors";
75         case HERE_ERROR_PERMISSION_DENIED:     return "Permission denied";
76         case HERE_ERROR_OUT_OF_MEMORY:         return "Out of memory";
77         case HERE_ERROR_INVALID_PARAMETER:     return "Invalid Parameter";
78         case HERE_ERROR_NOT_SUPPORTED:         return "Not suppoerted";
79         case HERE_ERROR_CONNECTION_TIME_OUT:   return "Connection time out";
80         case HERE_ERROR_NETWORK_UNREACHABLE:   return "Network unreachable";
81         case HERE_ERROR_INVALID_OPERATION:     return "Invalid operation";
82         case HERE_ERROR_KEY_NOT_AVAILABLE:     return "Key not available";
83         case HERE_ERROR_RESOURCE_BUSY:         return "Resource busy";
84         case HERE_ERROR_CANCELED:              return "Canceled";
85         case HERE_ERROR_UNKNOWN:               return "Unknown";
86         case HERE_ERROR_SERVICE_NOT_AVAILABLE: return "Service not available";
87         case HERE_ERROR_NOT_FOUND:             return "Not found";
88         case HERE_ERROR_USER_NOT_CONSENTED:    return "Not consented";
89         }
90         return "Unknown";
91 }
92
93 const char* ConvertMapsErrorToChar(int nErr)
94 {
95         switch (nErr) {
96         case MAPS_ERROR_NONE:                  return "MAPS_ERROR_NONE";
97         case MAPS_ERROR_PERMISSION_DENIED:     return "MAPS_ERROR_PERMISSION_DENIED";
98         case MAPS_ERROR_OUT_OF_MEMORY:         return "MAPS_ERROR_OUT_OF_MEMORY";
99         case MAPS_ERROR_INVALID_PARAMETER:     return "MAPS_ERROR_INVALID_PARAMETER";
100         case MAPS_ERROR_NOT_SUPPORTED:         return "MAPS_ERROR_NOT_SUPPORTED";
101         case MAPS_ERROR_CONNECTION_TIME_OUT:   return "MAPS_ERROR_CONNECTION_TIME_OUT";
102         case MAPS_ERROR_NETWORK_UNREACHABLE:   return "MAPS_ERROR_NETWORK_UNREACHABLE";
103         case MAPS_ERROR_INVALID_OPERATION:     return "MAPS_ERROR_INVALID_OPERATION";
104         case MAPS_ERROR_KEY_NOT_AVAILABLE:     return "MAPS_ERROR_KEY_NOT_AVAILABLE";
105         case MAPS_ERROR_RESOURCE_BUSY:         return "MAPS_ERROR_RESOURCE_BUSY";
106         case MAPS_ERROR_CANCELED:              return "MAPS_ERROR_CANCELED";
107         case MAPS_ERROR_UNKNOWN:               return "MAPS_ERROR_UNKNOWN";
108         case MAPS_ERROR_SERVICE_NOT_AVAILABLE: return "MAPS_ERROR_SERVICE_NOT_AVAILABLE";
109         case MAPS_ERROR_NOT_FOUND:             return "MAPS_ERROR_NOT_FOUND";
110         case MAPS_ERROR_USER_NOT_CONSENTED:    return "MAPS_ERROR_USER_NOT_CONSENTED";
111         }
112         return "MAPS_ERROR_UNKNOWN";
113 }
114
115 int GetMapsItemListLength(maps_item_list_h list)
116 {
117         int length = 0;
118         maps_item_list_get_length(list, &length);
119         return length;
120 }
121 }
122
123
124 HERE_PLUGIN_BEGIN_NAMESPACE
125
126 HereUtils::HereUtils()
127 {
128 }
129
130 HereUtils::~HereUtils()
131 {
132 }
133
134 GeoRouteQuery::TravelMode HereUtils::Convert(maps_route_transport_mode_e nVal)
135 {
136         switch (nVal) {
137         case MAPS_ROUTE_TRANSPORT_MODE_CAR:           return GeoRouteQuery::TM_CarTravel;
138         case MAPS_ROUTE_TRANSPORT_MODE_PEDESTRIAN:    return GeoRouteQuery::TM_PedestrianTravel;
139         case MAPS_ROUTE_TRANSPORT_MODE_PUBLICTRANSIT: return GeoRouteQuery::TM_PublicTransitTravel;
140         default: break;
141         }
142         return GeoRouteQuery::TM_CarTravel;
143 }
144
145 maps_route_transport_mode_e HereUtils::Convert(GeoRouteQuery::TravelMode nVal)
146 {
147         switch (nVal) {
148         case GeoRouteQuery::TM_CarTravel:           return MAPS_ROUTE_TRANSPORT_MODE_CAR;
149         case GeoRouteQuery::TM_PedestrianTravel:    return MAPS_ROUTE_TRANSPORT_MODE_PEDESTRIAN;
150         case GeoRouteQuery::TM_PublicTransitTravel: return MAPS_ROUTE_TRANSPORT_MODE_PUBLICTRANSIT;
151         default: break;
152         }
153         return MAPS_ROUTE_TRANSPORT_MODE_CAR;
154 }
155
156 GeoRouteQuery::FeatureType HereUtils::Convert(maps_route_feature_e nVal)
157 {
158         switch (nVal) {
159         case MAPS_ROUTE_FEATURE_NO:             return GeoRouteQuery::FT_NoFeature;
160         case MAPS_ROUTE_FEATURE_TOLL:           return GeoRouteQuery::FT_TollFeature;
161         case MAPS_ROUTE_FEATURE_MOTORWAY:       return GeoRouteQuery::FT_MotorwayFeature;
162         case MAPS_ROUTE_FEATURE_BOATFERRY:      return GeoRouteQuery::FT_BoatFerryFeature;
163         case MAPS_ROUTE_FEATURE_RAILFERRY:      return GeoRouteQuery::FT_RailFerryFeature;
164         case MAPS_ROUTE_FEATURE_PUBLICTTRANSIT: return GeoRouteQuery::FT_PublicTransitFeature;
165         case MAPS_ROUTE_FEATURE_TUNNEL:         return GeoRouteQuery::FT_TunnelFeature;
166         case MAPS_ROUTE_FEATURE_DIRTROAD:       return GeoRouteQuery::FT_DirtRoadFeature;
167         case MAPS_ROUTE_FEATURE_PARKS:          return GeoRouteQuery::FT_ParksFeature;
168         case MAPS_ROUTE_FEATURE_HOVLANE:        return GeoRouteQuery::FT_HOVLane;
169         case MAPS_ROUTE_FEATURE_STAIRS:         return GeoRouteQuery::FT_Stairs;
170         default: break;
171         }
172         return GeoRouteQuery::FT_NoFeature;
173 }
174
175 GeoRouteQuery::FeatureWeight HereUtils::Convert(maps_route_feature_weight_e nVal)
176 {
177         switch (nVal) {
178         case MAPS_ROUTE_FEATURE_WEIGHT_NORMAL:        return GeoRouteQuery::FW_NormalFeatureWeight;
179         case MAPS_ROUTE_FEATURE_WEIGHT_PREFER:        return GeoRouteQuery::FW_PreferFeatureWeight;
180         case MAPS_ROUTE_FEATURE_WEIGHT_AVOID:         return GeoRouteQuery::FW_AvoidFeatureWeight;
181         case MAPS_ROUTE_FEATURE_WEIGHT_SOFTEXCLUDE:   return GeoRouteQuery::FW_SoftExcludeFeatureWeight;
182         case MAPS_ROUTE_FEATURE_WEIGHT_STRICTEXCLUDE: return GeoRouteQuery::FW_StrictExcludeFeatureWeight;
183         default: break;
184         }
185         return GeoRouteQuery::FW_NormalFeatureWeight;
186 }
187
188 maps_route_turn_type_e HereUtils::Convert(Maneuver::InstructionDirection nVal)
189 {
190         switch (nVal) {
191         case Maneuver::ID_NoDirection:          return MAPS_ROUTE_TURN_TYPE_NONE;
192         case Maneuver::ID_DirectionForward:     return MAPS_ROUTE_TURN_TYPE_STRAIGHT;
193         case Maneuver::ID_DirectionBearRight:   return MAPS_ROUTE_TURN_TYPE_BEAR_RIGHT;
194         case Maneuver::ID_DirectionLightRight:  return MAPS_ROUTE_TURN_TYPE_LIGHT_RIGHT;
195         case Maneuver::ID_DirectionRight:       return MAPS_ROUTE_TURN_TYPE_RIGHT;
196         case Maneuver::ID_DirectionHardRight:   return MAPS_ROUTE_TURN_TYPE_HARD_RIGHT;
197         case Maneuver::ID_DirectionUTurnRight:  return MAPS_ROUTE_TURN_TYPE_UTURN_RIGHT;
198         case Maneuver::ID_DirectionUTurnLeft:   return MAPS_ROUTE_TURN_TYPE_UTURN_LEFT;
199         case Maneuver::ID_DirectionHardLeft:    return MAPS_ROUTE_TURN_TYPE_HARD_LEFT;
200         case Maneuver::ID_DirectionLeft:        return MAPS_ROUTE_TURN_TYPE_LEFT;
201         case Maneuver::ID_DirectionLightLeft:   return MAPS_ROUTE_TURN_TYPE_LIGHT_LEFT;
202         case Maneuver::ID_DirectionBearLeft:    return MAPS_ROUTE_TURN_TYPE_BEAR_LEFT;
203         default: break;
204         }
205         return MAPS_ROUTE_TURN_TYPE_NONE;
206 }
207
208 Maneuver::InstructionDirection HereUtils::Convert(maps_route_turn_type_e nVal)
209 {
210         switch (nVal) {
211         case MAPS_ROUTE_TURN_TYPE_NONE:         return Maneuver::ID_NoDirection;
212         case MAPS_ROUTE_TURN_TYPE_STRAIGHT:     return Maneuver::ID_DirectionForward;
213         case MAPS_ROUTE_TURN_TYPE_BEAR_RIGHT:   return Maneuver::ID_DirectionBearRight;
214         case MAPS_ROUTE_TURN_TYPE_LIGHT_RIGHT:  return Maneuver::ID_DirectionLightRight;
215         case MAPS_ROUTE_TURN_TYPE_RIGHT:        return Maneuver::ID_DirectionRight;
216         case MAPS_ROUTE_TURN_TYPE_HARD_RIGHT:   return Maneuver::ID_DirectionHardRight;
217         case MAPS_ROUTE_TURN_TYPE_UTURN_RIGHT:  return Maneuver::ID_DirectionUTurnRight;
218         case MAPS_ROUTE_TURN_TYPE_UTURN_LEFT:   return Maneuver::ID_DirectionUTurnLeft;
219         case MAPS_ROUTE_TURN_TYPE_HARD_LEFT:    return Maneuver::ID_DirectionHardLeft;
220         case MAPS_ROUTE_TURN_TYPE_LEFT:         return Maneuver::ID_DirectionLeft;
221         case MAPS_ROUTE_TURN_TYPE_LIGHT_LEFT:   return Maneuver::ID_DirectionLightLeft;
222         case MAPS_ROUTE_TURN_TYPE_BEAR_LEFT:    return Maneuver::ID_DirectionBearLeft;
223         default: break;
224         }
225         return Maneuver::ID_NoDirection;
226 }
227
228 GeoTiledMap::MapType HereUtils::Convert(maps_view_type_e nMapType, bool bBuildings, bool bTraffic, bool bPublicTransit)
229 {
230         switch (nMapType) {
231         case MAPS_VIEW_TYPE_NORMAL:
232                 if (bPublicTransit)     return GeoTiledMap::MT_Normal_Day_Transit;
233                 if (bTraffic)           return GeoTiledMap::MT_Normal_Traffic_Day;
234                 return GeoTiledMap::MT_Normal_Day;
235         case MAPS_VIEW_TYPE_SATELLITE:
236                 return GeoTiledMap::MT_Satellite_Day;
237         case MAPS_VIEW_TYPE_TERRAIN:
238                 return GeoTiledMap::MT_Terrain_Day;
239         case MAPS_VIEW_TYPE_HYBRID:
240                 if (bPublicTransit)     return GeoTiledMap::MT_Hybrid_Day_Transit;
241                 if (bTraffic)           return GeoTiledMap::MT_Hybrid_Traffic_Day;
242                 return GeoTiledMap::MT_Hybrid_Day;
243         default: break;
244         }
245         return GeoTiledMap::MT_Normal_Day;
246 }
247
248 maps_error_e HereUtils::ConvertHttpCodeToMapsError(int nVal)
249 {
250         switch (nVal) {
251         case 200:/*Ok*/                         return MAPS_ERROR_NONE;
252         case 408:/*Request timeout*/
253         case 504:/*Gateway timeout*/
254         case 598:/*Network reading timeout*/
255         case 599:/*Network connection timeout*/ return MAPS_ERROR_CONNECTION_TIME_OUT;
256
257         case 404:/*Not found*/
258         case 407:/*Proxy auth. required*/
259         case 502:/*Bad gateway*/                return MAPS_ERROR_NETWORK_UNREACHABLE;
260
261         case 401:/*Unauthorized*/
262         case 402:/*Payment required*/           return MAPS_ERROR_KEY_NOT_AVAILABLE;
263
264         case 405:/*Method not allowed*/
265         case 413:/*Request entity too larget*/
266         case 414:/*Request uri too large*/      return MAPS_ERROR_INVALID_OPERATION;
267
268         case 403:/*Forbidden*/
269         case 500:/*Server internal error*/
270         case 501:/*Not implemented*/
271         case 503:/*Service unavailable*/        return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
272         }
273
274         if (nVal > 0 && nVal < 100) // curl error code
275                 return MAPS_ERROR_NETWORK_UNREACHABLE;
276
277         if (nVal >= 400 && nVal < 500) // http code 4xx (client-side error)
278                 return MAPS_ERROR_INVALID_OPERATION;
279
280         if (nVal >= 500 && nVal < 600) // http code 5xx (server-side error)
281                 return MAPS_ERROR_SERVICE_NOT_AVAILABLE;
282
283         return MAPS_ERROR_UNKNOWN;
284 }
285
286 GeoBoundingBox& HereUtils::Convert(maps_area_h hArea, GeoBoundingBox& Box)
287 {
288         maps_area_s* area_s = (maps_area_s*)hArea;
289
290         if (!area_s || area_s->type != MAPS_AREA_RECTANGLE) return Box;
291
292         GeoCoordinates hereCoordLT(area_s->rect.top_left.latitude, area_s->rect.top_left.longitude);
293         GeoCoordinates hereCoordRB(area_s->rect.bottom_right.latitude, area_s->rect.bottom_right.longitude);
294
295         Box.SetTopLeft(hereCoordLT);
296         Box.SetBottomRight(hereCoordRB);
297
298         return Box;
299 }
300
301 maps_area_h& HereUtils::Convert(GeoBoundingBox Box, maps_area_h& hArea)
302 {
303         maps_coordinates_h mapsCoordLT, mapsCoordRB;
304         GeoCoordinates hereCoordLT, hereCoordRB;
305
306         hereCoordLT = Box.GetTopLeft();
307         hereCoordRB = Box.GetBottomRight();
308
309         maps_coordinates_create(hereCoordLT.GetLatitude(), hereCoordLT.GetLongitude(), &mapsCoordLT);
310         maps_coordinates_create(hereCoordRB.GetLatitude(), hereCoordRB.GetLongitude(), &mapsCoordRB);
311
312         maps_area_create_rectangle(mapsCoordLT, mapsCoordRB, &hArea);
313
314         maps_coordinates_destroy(mapsCoordLT);
315         maps_coordinates_destroy(mapsCoordRB);
316
317         return hArea;
318 }
319
320 GeoBoundingCircle& HereUtils::Convert(maps_area_h hArea, GeoBoundingCircle& circle)
321 {
322         maps_area_s* area_s = (maps_area_s*)hArea;
323
324         if (!area_s || area_s->type != MAPS_AREA_CIRCLE) return circle;
325
326         GeoCoordinates hereCoord(area_s->circle.center.latitude, area_s->circle.center.longitude);
327
328         circle.SetCenter(hereCoord);
329         circle.SetRadius(area_s->circle.radius);
330
331         return circle;
332 }
333
334 void HereUtils::Convert(String strUtf8, WString& strUtf16)
335 {
336         strUtf16.assign(strUtf8.begin(), strUtf8.end());
337 }
338
339 void HereUtils::Convert(WString strUtf16, String& strUtf8)
340 {
341         strUtf8.assign(strUtf16.begin(), strUtf16.end());
342 }
343
344 GeoBoundingBox& HereUtils::Convert(const char *src, GeoBoundingBox &box)
345 {
346         int i = 0;
347         char *token, *next;
348         double coord[4] = { 0.0, };
349
350         token = strtok_r((char*)src, ",;", &next);
351         while (token && i < 4) {
352                 coord[i++] = atof(token);
353                 token = strtok_r(NULL, ",;", &next);
354         }
355         box.SetTopLeft(GeoCoordinates(coord[0], coord[1]));
356         box.SetBottomRight(GeoCoordinates(coord[2], coord[3]));
357         return box;
358 }
359
360 bool HereUtils::IsValid(GeoCoordinates geoCoord)
361 {
362         return IsValidCoord(geoCoord.GetLatitude(), geoCoord.GetLongitude());
363 }
364
365 bool HereUtils::IsValid(maps_coordinates_s geoCoord)
366 {
367         return IsValidCoord(geoCoord.latitude, geoCoord.longitude);
368 }
369
370 bool HereUtils::IsValidCoord(double dLat, double dLng)
371 {
372         return ((dLat <= LATITUDE_RANGE && dLat >= -LATITUDE_RANGE) &&
373                 (dLng <= LONGITUDE_RANGE && dLng >= -LONGITUDE_RANGE));
374 }
375
376 bool HereUtils::IsValid(maps_area_s hArea)
377 {
378         if (hArea.type == MAPS_AREA_RECTANGLE) {
379                 return (HereUtils::IsValid(hArea.rect.top_left) &&
380                         HereUtils::IsValid(hArea.rect.bottom_right));
381         } else if (hArea.type == MAPS_AREA_CIRCLE) {
382                 return HereUtils::IsValid(hArea.circle.center);
383         } else {
384                 return false;
385         }
386 }
387
388 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e destUnit)
389 {
390         return ConvertDistance(originValue, MAPS_DISTANCE_UNIT_M, destUnit);
391 }
392
393 const double HereUtils::ConvertDistance(const double originValue, maps_distance_unit_e originUnit, maps_distance_unit_e destUnit)
394 {
395         double meterConstant[MAPS_DISTANCE_UNIT_YD+1] = { 1.0, 0.001, 3.2808399, 1.0936133 };
396         return originValue / meterConstant[originUnit] * meterConstant[destUnit];
397 }
398
399 HERE_PLUGIN_END_NAMESPACE