Tizen C++ Coding Rules
[platform/core/location/maps-plugin-here.git] / src / here_revgeocode.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_revgeocode.h"
18
19 HERE_PLUGIN_BEGIN_NAMESPACE
20
21 HereRevGeocode::HereRevGeocode(void* pCbFunc, void* pUserData, int nReqId)
22         : m_geoCoord(0, 0, 0)
23 {
24         m_pQuery = NULL;
25         m_pCbFunc = pCbFunc;
26         m_pUserData = pUserData;
27         m_nReqId = nReqId;
28 }
29
30 HereRevGeocode::~HereRevGeocode()
31 {
32         if (m_pQuery) {
33                 delete m_pQuery;
34                 m_pQuery = NULL;
35         }
36 }
37
38 here_error_e HereRevGeocode::PrepareQuery()
39 {
40         if (m_pQuery)
41                 return HERE_ERROR_PERMISSION_DENIED;
42
43         m_pQuery = new (std::nothrow) ReverseGeoCoderQuery();
44
45         if (!m_pQuery)
46                 return HERE_ERROR_OUT_OF_MEMORY;
47         else
48                 return HERE_ERROR_NONE;
49 }
50
51 here_error_e HereRevGeocode::PreparePreference(maps_preference_h hPref)
52 {
53         if (!m_pQuery)
54                 return HERE_ERROR_OUT_OF_MEMORY;
55
56         if (!hPref)
57                 return HERE_ERROR_NONE;
58
59         int ret;
60         char *szLanguage;
61         ret = maps_preference_get_language(hPref, &szLanguage);
62         if (ret == MAPS_ERROR_NONE && szLanguage && *szLanguage)
63                 m_pQuery->AppendPreferredLanguage(szLanguage);
64         g_free(szLanguage);
65
66         int nMaxResults;
67         ret = maps_preference_get_max_results(hPref, &nMaxResults);
68         if (ret == MAPS_ERROR_NONE)
69                 m_pQuery->SetMaxResults((size_t)nMaxResults);
70
71         return HERE_ERROR_NONE;
72 }
73
74 here_error_e HereRevGeocode::PreparePosition(double dLat, double dLng)
75 {
76         if (!m_pQuery)
77                 return HERE_ERROR_OUT_OF_MEMORY;
78
79         GeoCoordinates geoCoord(dLat, dLng);
80         if (!HereUtils::IsValid(geoCoord))
81                 return HERE_ERROR_INVALID_PARAMETER;
82
83         m_pQuery->SetProximity(geoCoord, 0);
84         m_pQuery->SetMode(ReverseGeoCoderQuery::RM_RetrieveAddresses);
85
86         return HERE_ERROR_NONE;
87 }
88
89 here_error_e HereRevGeocode::StartRevGeocode(maps_item_hashtable_h hPref)
90 {
91         if (!m_pQuery)
92                 return HERE_ERROR_OUT_OF_MEMORY;
93
94         m_nRestReqId = m_pQuery->Execute(*this, NULL);
95
96         return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
97 }
98
99 void HereRevGeocode::OnGeoCoderReply(const GeoCoderReply& Reply)
100 {
101         if (m_bCanceled || !m_pCbFunc) { // ignore call back
102                 delete this;
103                 return;
104         }
105
106
107         int nResults = Reply.GetNumResults();
108         Result* pResult;
109         float fDistance, fShortestDistance = 0;
110         int nShortestIdx = -1;
111
112         for (size_t i = 0; i < (size_t)nResults; i++) {
113                 pResult = (Result*)Reply.GetResult(i);
114
115                 if(pResult) {
116                         fDistance = pResult->GetDistance();
117
118                         if (nShortestIdx < 0 || fDistance < fShortestDistance) {
119                                 fShortestDistance = fDistance;
120                                 nShortestIdx = i;
121                         }
122                 }
123         }
124
125
126         if (nShortestIdx < 0) {
127                 ((maps_service_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND,
128                         m_nReqId, 0, 0, NULL, m_pUserData);
129                 delete this;
130                 return;
131         }
132
133         maps_address_h hAddr = NULL;
134         maps_error_e error = (maps_error_e)maps_address_create(&hAddr);
135         String *additionalDataValue = NULL;
136
137         if(error == MAPS_ERROR_NONE) {
138                 pResult = (Result*)Reply.GetResult(nShortestIdx);
139
140                 if (pResult) {
141                         Address tmpAddr = (pResult->GetLocation()).GetAddress();
142
143                         if (!tmpAddr.GetHouseNumber().empty())
144                                 maps_address_set_building_number(hAddr, tmpAddr.GetHouseNumber().c_str());
145
146                         if (!tmpAddr.GetStreet().empty())
147                                 maps_address_set_street(hAddr, tmpAddr.GetStreet().c_str());
148
149                         if (!tmpAddr.GetDistrict().empty())
150                                 maps_address_set_district(hAddr, tmpAddr.GetDistrict().c_str());
151
152                         if (!tmpAddr.GetCity().empty())
153                                 maps_address_set_city(hAddr, tmpAddr.GetCity().c_str());
154
155                         additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountyName");
156                         if (additionalDataValue && !additionalDataValue->empty())
157                                 maps_address_set_county(hAddr, additionalDataValue->c_str());
158                         else if (!tmpAddr.GetCounty().empty())
159                                 maps_address_set_county(hAddr, tmpAddr.GetCounty().c_str());
160
161                         additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("StateName");
162                         if (additionalDataValue && !additionalDataValue->empty())
163                                 maps_address_set_state(hAddr, additionalDataValue->c_str());
164                         else if (!tmpAddr.GetState().empty())
165                                 maps_address_set_state(hAddr, tmpAddr.GetState().c_str());
166
167                         additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountryName");
168                         if (additionalDataValue && !additionalDataValue->empty())
169                                 maps_address_set_country(hAddr, additionalDataValue->c_str());
170                         else if (!tmpAddr.GetCountry().empty())
171                                 maps_address_set_country(hAddr, tmpAddr.GetCountry().c_str());
172
173                         if (!tmpAddr.GetCountry().empty())
174                                 maps_address_set_country_code(hAddr, tmpAddr.GetCountry().c_str());
175
176                         if (!tmpAddr.GetPostalCode().empty())
177                                 maps_address_set_postal_code(hAddr, tmpAddr.GetPostalCode().c_str());
178
179                         if (!tmpAddr.GetLabel().empty())
180                                 maps_address_set_freetext(hAddr, tmpAddr.GetLabel().c_str());
181                 }
182         }
183
184         if (m_bCanceled) {
185                 maps_address_destroy(hAddr);
186         } else {
187                 int nResult = (error == MAPS_ERROR_NONE ? 1 : 0);
188                 ((maps_service_reverse_geocode_cb)m_pCbFunc)(error, m_nReqId, 0, nResult, hAddr, m_pUserData);
189         }
190
191         delete this;
192 }
193
194 void HereRevGeocode::OnGeoCoderFailure(const GeoCoderReply& Reply)
195 {
196         if (!m_bCanceled && m_pCbFunc)
197                 ((maps_service_reverse_geocode_cb)m_pCbFunc)((maps_error_e)GetErrorCode(Reply), m_nReqId, 0, 0, NULL, m_pUserData);
198         delete this;
199 }
200
201 HERE_PLUGIN_END_NAMESPACE