Maps Deadlock Issue Fix
[platform/core/location/maps-plugin-here.git] / src / here_multirevgeocode.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_multirevgeocode.h"
18
19 HERE_PLUGIN_BEGIN_NAMESPACE
20
21 HereMultiRevGeocode::HereMultiRevGeocode(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 HereMultiRevGeocode::~HereMultiRevGeocode()
31 {
32         if (m_pQuery) {
33                 delete m_pQuery;
34                 m_pQuery = NULL;
35         }
36 }
37
38 here_error_e HereMultiRevGeocode::PrepareQuery()
39 {
40         if (m_pQuery)
41                 return HERE_ERROR_PERMISSION_DENIED;
42
43         m_pQuery = new (std::nothrow) MultiReverseQuery();
44
45         if (!m_pQuery)
46                 return HERE_ERROR_OUT_OF_MEMORY;
47         else
48                 return HERE_ERROR_NONE;
49 }
50
51 static bool geocoder_coordinates_cb(int index, maps_coordinates_h coordinates, void *user_data)
52 {
53         double dLatitude = 0.0;
54         double dLongitude = 0.0;
55         GeoCoordinates hereCoord;
56         maps_coordinates_get_latitude(coordinates, &dLatitude);
57         maps_coordinates_get_longitude(coordinates, &dLongitude);
58         hereCoord = GeoCoordinates(dLatitude, dLongitude);
59
60         if (!HereUtils::IsValid(hereCoord))
61                 return false;
62
63         GeoCoordinateList *pList =(GeoCoordinateList *) user_data;
64         pList->push_back(hereCoord);
65
66         return true;
67 }
68
69 here_error_e HereMultiRevGeocode::PreparePositionList(const maps_coordinates_list_h hGeocodeList)
70 {
71         if (!m_pQuery)
72                 return HERE_ERROR_OUT_OF_MEMORY;
73
74         if (!hGeocodeList)
75                 return HERE_ERROR_INVALID_PARAMETER;
76
77         GeoCoordinateList hereCoordList;
78         maps_coordinates_list_foreach(hGeocodeList, geocoder_coordinates_cb, &hereCoordList);
79
80         m_pQuery->SetMode(MultiReverseQuery::RM_RetrieveAddresses);
81         m_pQuery->SetGeocodeList(hereCoordList);
82         m_pQuery->SetMaxResults(1);
83         m_pQuery->SetGenParameter(8);
84
85         return HERE_ERROR_NONE;
86 }
87
88 here_error_e HereMultiRevGeocode::StartMultiReverse(maps_item_hashtable_h hPref)
89 {
90         if (!m_pQuery)
91                 return HERE_ERROR_OUT_OF_MEMORY;
92
93         m_nRestReqId = m_pQuery->Execute(*this, NULL);
94
95         return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_CANCELED);
96 }
97
98 void HereMultiRevGeocode::OnMultiReverseReply(const MultiReverseReply& Reply)
99 {
100         if (m_bCanceled || !m_pCbFunc) // ignore call back
101         {
102                 delete this;
103                 return;
104         }
105
106         int nResults = Reply.GetNumResults();
107         Result* pResult;
108
109         maps_address_list_h address_list = NULL;
110         maps_address_list_create(&address_list);
111         maps_address_h pAddr = NULL;
112         String *additionalDataValue = NULL;
113
114         for (size_t i = 0; i < (size_t)nResults; i++)
115         {
116                 if ((pResult = (Result*)Reply.GetResult(i)) == NULL) continue;
117                 if (maps_address_create(&pAddr) != MAPS_ERROR_NONE) continue;
118
119
120                 Address tmpAddr = (pResult->GetLocation()).GetAddress();
121
122                 if(!tmpAddr.GetHouseNumber().empty())
123                         maps_address_set_building_number(pAddr, tmpAddr.GetHouseNumber().c_str());
124
125                 if(!tmpAddr.GetStreet().empty())
126                         maps_address_set_street(pAddr, tmpAddr.GetStreet().c_str());
127
128                 if(!tmpAddr.GetDistrict().empty())
129                         maps_address_set_district(pAddr, tmpAddr.GetDistrict().c_str());
130
131                 if(!tmpAddr.GetCity().empty())
132                         maps_address_set_city(pAddr, tmpAddr.GetCity().c_str());
133
134                 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountyName");
135                 if (additionalDataValue && !additionalDataValue->empty())
136                         maps_address_set_county(pAddr, additionalDataValue->c_str());
137                 else if (!tmpAddr.GetCounty().empty())
138                         maps_address_set_county(pAddr, tmpAddr.GetCounty().c_str());
139
140                 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("StateName");
141                 if (additionalDataValue && !additionalDataValue->empty())
142                         maps_address_set_state(pAddr, additionalDataValue->c_str());
143                 else if (!tmpAddr.GetState().empty())
144                         maps_address_set_state(pAddr, tmpAddr.GetState().c_str());
145
146                 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountryName");
147                 if (additionalDataValue && !additionalDataValue->empty())
148                         maps_address_set_country(pAddr, additionalDataValue->c_str());
149                 else if (!tmpAddr.GetCountry().empty())
150                         maps_address_set_country(pAddr, tmpAddr.GetCountry().c_str());
151
152                 if(!tmpAddr.GetCountry().empty())
153                         maps_address_set_country_code(pAddr, tmpAddr.GetCountry().c_str());
154
155                 if(!tmpAddr.GetPostalCode().empty())
156                         maps_address_set_postal_code(pAddr, tmpAddr.GetPostalCode().c_str());
157
158                 if(!tmpAddr.GetLabel().empty())
159                         maps_address_set_freetext(pAddr, tmpAddr.GetLabel().c_str());
160
161                 if (maps_address_list_append(address_list, pAddr) != MAPS_ERROR_NONE)
162                         maps_address_destroy(pAddr);
163         }
164
165
166         int address_list_length = 0;
167         maps_address_list_get_length(address_list, &address_list_length);
168
169         if (m_bCanceled || !m_pCbFunc) {
170                 maps_address_list_destroy(address_list);
171         } else if (address_list_length <= 0) {
172                 maps_address_list_destroy(address_list);
173                 ((maps_service_multi_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND, m_nReqId, 0, NULL, m_pUserData);
174         } else {
175                 ((maps_service_multi_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NONE, m_nReqId, address_list_length, address_list, m_pUserData);
176         }
177
178         delete this;
179 }
180
181 void HereMultiRevGeocode::OnMultiReverseFailure(const MultiReverseReply& Reply)
182 {
183         if (!m_bCanceled && m_pCbFunc)
184                 ((maps_service_multi_reverse_geocode_cb)m_pCbFunc)((maps_error_e)GetErrorCode(Reply), m_nReqId, 0, NULL, m_pUserData);
185         delete this;
186 }
187
188 HERE_PLUGIN_END_NAMESPACE
189