Tizen C++ Coding Rules
[platform/core/location/maps-plugin-here.git] / src / here_geocode.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_geocode.h"
18
19 HERE_PLUGIN_BEGIN_NAMESPACE
20
21 HereGeocode::HereGeocode(void *pCbFunc, void *pUserData, int nReqId)
22 {
23         m_pQuery = NULL;
24
25         m_pCbFunc = pCbFunc;
26         m_pUserData = pUserData;
27         m_nReqId = nReqId;
28 }
29
30 HereGeocode::~HereGeocode()
31 {
32         if (m_pQuery)
33         {
34                 delete m_pQuery;
35                 m_pQuery = NULL;
36         }
37 }
38
39 here_error_e HereGeocode::PrepareQuery()
40 {
41         if (m_pQuery)
42                 return HERE_ERROR_PERMISSION_DENIED;
43
44         m_pQuery = new (std::nothrow) GeoCoderQuery();
45
46         if (!m_pQuery)
47                 return HERE_ERROR_OUT_OF_MEMORY;
48         else
49                 return HERE_ERROR_NONE;
50 }
51
52 here_error_e HereGeocode::PreparePreference(maps_preference_h hPref)
53 {
54         if (!m_pQuery)
55                 return HERE_ERROR_OUT_OF_MEMORY;
56
57         if (!hPref)
58                 return HERE_ERROR_NONE;
59
60         int ret;
61         char *szLanguage = NULL;
62         ret = maps_preference_get_language(hPref, &szLanguage);
63         if (ret == MAPS_ERROR_NONE && szLanguage && *szLanguage)
64         {
65                 m_pQuery->AppendPreferredLanguage(szLanguage);
66                 g_free(szLanguage);
67         }
68
69         int nMaxResults;
70         ret = maps_preference_get_max_results(hPref, &nMaxResults);
71         if (ret == MAPS_ERROR_NONE)
72         {
73                 m_pQuery->SetMaxResults((size_t)nMaxResults);
74         }
75
76         return HERE_ERROR_NONE;
77 }
78
79 here_error_e HereGeocode::StartGeocode(const char* szAddr)
80 {
81         if (!m_pQuery)
82                 return HERE_ERROR_OUT_OF_MEMORY;
83
84         if (!szAddr || (szAddr && strlen(szAddr) <= 0))
85                 return HERE_ERROR_INVALID_PARAMETER;
86
87
88         String sSearch(szAddr);
89         m_pQuery->SetSearchtext(sSearch);
90
91
92         m_nRestReqId = m_pQuery->Execute(*this, NULL);
93
94         return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
95 }
96
97 here_error_e HereGeocode::StartGeocodeInsideArea(const char* szAddr, const maps_area_h hArea)
98 {
99         if (!m_pQuery)
100                 return HERE_ERROR_OUT_OF_MEMORY;
101
102         if (!szAddr || (szAddr && strlen(szAddr) <= 0) || !hArea)
103                 return HERE_ERROR_INVALID_PARAMETER;
104
105
106         String sSearch(szAddr);
107         m_pQuery->SetSearchtext(sSearch);
108
109         maps_area_s *pArea = (maps_area_s *)hArea;
110         if (pArea->type == MAPS_AREA_RECTANGLE) {
111                 double dLatTL = pArea->rect.top_left.latitude;
112                 double dLngTL = pArea->rect.top_left.longitude;
113                 GeoCoordinates geoCoordTL(dLatTL, dLngTL);
114
115                 double dLatBR = pArea->rect.bottom_right.latitude;
116                 double dLngBR = pArea->rect.bottom_right.longitude;
117                 GeoCoordinates geoCoordBR(dLatBR, dLngBR);
118
119                 GeoBoundingBox BoundingBox(geoCoordTL, geoCoordBR);
120
121                 m_pQuery->SetBoundingBox(BoundingBox);
122         } else if (pArea->type == MAPS_AREA_CIRCLE) {
123                 MAPS_LOGD("HERE Maps is not supported circle type in GeocoderQuery");
124                 return HERE_ERROR_NOT_SUPPORTED;
125         } else {
126                 return HERE_ERROR_INVALID_PARAMETER;
127         }
128
129
130         m_nRestReqId = m_pQuery->Execute(*this, NULL);
131
132         return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
133 }
134
135 here_error_e HereGeocode::StartGeocodeByStructuredAddress(const maps_address_h hAddr)
136 {
137         if (!m_pQuery)
138                 return HERE_ERROR_OUT_OF_MEMORY;
139
140         if (!hAddr)
141                 return HERE_ERROR_INVALID_PARAMETER;
142
143
144         Address rAddress;
145
146         int ret;
147         char *szCountry = NULL;
148         ret = maps_address_get_country(hAddr, &szCountry);
149         if (ret == MAPS_ERROR_NONE && szCountry && *szCountry)
150                 rAddress.SetCountry(String(szCountry));
151         g_free(szCountry);
152
153         char *szCountryCode = NULL;
154         ret = maps_address_get_country_code(hAddr, &szCountryCode);
155         if (ret == MAPS_ERROR_NONE && szCountryCode && *szCountryCode)
156                 rAddress.SetCountryCode(String(szCountryCode));
157         g_free(szCountryCode);
158
159         char *szCounty = NULL;
160         ret = maps_address_get_county(hAddr, &szCounty);
161         if (ret == MAPS_ERROR_NONE && szCounty && *szCounty)
162                 rAddress.SetCounty(String(szCounty));
163         g_free(szCounty);
164
165         char *szState = NULL;
166         ret = maps_address_get_state(hAddr, &szState);
167         if (ret == MAPS_ERROR_NONE && szState && *szState)
168                 rAddress.SetState(String(szState));
169         g_free(szState);
170
171         char *szCity = NULL;
172         ret = maps_address_get_city(hAddr, &szCity);
173         if (ret == MAPS_ERROR_NONE && szCity && *szCity)
174                 rAddress.SetCity(String(szCity));
175         g_free(szCity);
176
177         char *szDistrict = NULL;
178         ret = maps_address_get_district(hAddr, &szDistrict);
179         if (ret == MAPS_ERROR_NONE && szDistrict && *szDistrict)
180                 rAddress.SetDistrict(String(szDistrict));
181         g_free(szDistrict);
182
183         char *szStreet = NULL;
184         ret = maps_address_get_street(hAddr, &szStreet);
185         if (ret == MAPS_ERROR_NONE && szStreet && *szStreet)
186                 rAddress.SetStreet(String(szStreet));
187         g_free(szStreet);
188
189         char *szBuildingNumber = NULL;
190         ret = maps_address_get_building_number(hAddr, &szBuildingNumber);
191         if (ret == MAPS_ERROR_NONE && szBuildingNumber && *szBuildingNumber)
192                 rAddress.SetHouseNumber(String(szBuildingNumber));
193         g_free(szBuildingNumber);
194
195         char *szPostalCode = NULL;
196         ret = maps_address_get_postal_code(hAddr, &szPostalCode);
197         if (ret == MAPS_ERROR_NONE && szPostalCode && *szPostalCode)
198                 rAddress.SetPostalCode(String(szPostalCode));
199         g_free(szPostalCode);
200
201         //not defined in maps-service
202         //rAddress.SetLabel(String(sLabel));
203         //rAddress.SetFloor(String(sFloor));
204         //rAddress.SetSuite(String(sSuite));
205
206         m_pQuery->SetAddress(rAddress);
207
208
209
210         m_nRestReqId = m_pQuery->Execute(*this, NULL);
211
212         return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
213 }
214
215 void HereGeocode::OnGeoCoderReply(const GeoCoderReply& Reply)
216 {
217         if (m_bCanceled || !m_pCbFunc) // ignore call back
218         {
219                 delete this;
220                 return;
221         }
222
223         Result* pResult;
224         size_t nResults = Reply.GetNumResults();
225         GeoCoordinates hereCoord;
226         maps_coordinates_h mapsCoord;
227
228         if (nResults == 0) {
229                 ((maps_service_geocode_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND, m_nReqId,
230                         0, 0, NULL, m_pUserData);
231                 delete this;
232                 return;
233         }
234
235         for (size_t i = 0 ; i < nResults; i++)
236         {
237                 pResult = (Result*)Reply.GetResult(i);
238
239                 if (pResult) {
240                         hereCoord = (pResult->GetLocation()).GetDisplayPosition();
241                 } else {
242                         hereCoord.SetLatitude(0.0);
243                         hereCoord.SetLongitude(0.0);
244                 }
245
246                 maps_error_e error = (maps_error_e)maps_coordinates_create(
247                                      hereCoord.GetLatitude(), hereCoord.GetLongitude(), &mapsCoord);
248
249                 if (m_bCanceled || !m_pCbFunc) {
250                         if (mapsCoord) maps_coordinates_destroy(mapsCoord);
251                         break;
252                 } else {
253                         if (((maps_service_geocode_cb)m_pCbFunc)(error, m_nReqId, i,
254                                 nResults, mapsCoord, m_pUserData) == FALSE) {
255                                 delete this;
256                                 return;
257                         }
258                 }
259         }
260
261         delete this;
262 }
263
264 void HereGeocode::OnGeoCoderFailure(const GeoCoderReply& Reply)
265 {
266         if (!m_bCanceled && m_pCbFunc)
267                 ((maps_service_geocode_cb)m_pCbFunc)((maps_error_e)GetErrorCode(Reply), m_nReqId, 0, 0, NULL, m_pUserData);
268         delete this;
269 }
270
271 HERE_PLUGIN_END_NAMESPACE
272