2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "here_place.h"
19 HERE_PLUGIN_BEGIN_NAMESPACE
21 HerePlace::HerePlace(void *pCbFunc, void *pUserData, int nReqId)
23 m_pDiscoveryQuery = NULL;
24 m_pPlaceDetailsQuery = NULL;
27 m_pUserData = pUserData;
32 m_bReplyFlushed = false;
34 m_bPlaceDetails = false;
35 m_bPlaceDetailsInternal = false;
36 m_bReplyWithList = false;
37 m_eDistanceUnit = MAPS_DISTANCE_UNIT_M;
40 HerePlace::~HerePlace()
42 if (m_pDiscoveryQuery) {
43 delete m_pDiscoveryQuery;
44 m_pDiscoveryQuery = NULL;
47 if (m_pPlaceDetailsQuery) {
48 delete m_pPlaceDetailsQuery;
49 m_pPlaceDetailsQuery = NULL;
52 while (!m_PlaceList.empty()) {
53 maps_place_destroy(m_PlaceList.front());
54 m_PlaceList.pop_front();
58 here_error_e HerePlace::PrepareDiscoveryQuery()
60 if (m_pDiscoveryQuery)
61 return HERE_ERROR_PERMISSION_DENIED;
63 m_pDiscoveryQuery = new (std::nothrow) DiscoveryQuery();
65 if (!m_pDiscoveryQuery)
66 return HERE_ERROR_OUT_OF_MEMORY;
68 return HERE_ERROR_NONE;
71 here_error_e HerePlace::PrepareDiscoveryPreference(maps_preference_h hPref)
73 if (!m_pDiscoveryQuery)
74 return HERE_ERROR_OUT_OF_MEMORY;
77 return HERE_ERROR_NONE;
81 char *szLanguage = NULL;
82 ret = maps_preference_get_language(hPref, &szLanguage);
83 if (ret == MAPS_ERROR_NONE && szLanguage && *szLanguage)
84 m_pDiscoveryQuery->SetLanguage(szLanguage);
88 ret = maps_preference_get_max_results(hPref, &nMaxResults);
89 if (ret == MAPS_ERROR_NONE)
90 m_pDiscoveryQuery->SetMaxResults((size_t)nMaxResults);
92 maps_distance_unit_e eUnit;
93 ret = maps_preference_get_distance_unit(hPref, &eUnit);
94 if (ret == MAPS_ERROR_NONE)
95 m_eDistanceUnit = eUnit;
98 ret = maps_preference_get(hPref, MAPS_PLACE_FILTER_SORT_BY, &szSortBy);
99 if (ret == MAPS_ERROR_NONE)
100 m_szSortBy = szSortBy;
102 return HERE_ERROR_NONE;
105 here_error_e HerePlace::PrepareDiscoveryFilter(maps_place_filter_h hFilter)
107 if (!m_pDiscoveryQuery)
108 return HERE_ERROR_OUT_OF_MEMORY;
111 return HERE_ERROR_INVALID_PARAMETER;
114 maps_place_category_h mapsCate = NULL;
115 if (maps_place_filter_get_category(hFilter, &mapsCate) == MAPS_ERROR_NONE) {
116 CategoryList hereCateList;
118 char *szId = NULL, *szName = NULL, *szUrl = NULL;
120 ret = maps_place_category_get_name(mapsCate, &szName);
121 if (ret == MAPS_ERROR_NONE && szName && *szName)
122 hereCate.SetTitle(szName);
125 ret = maps_place_category_get_url(mapsCate, &szUrl);
126 if (ret == MAPS_ERROR_NONE && szUrl && *szUrl)
127 hereCate.SetHref(szUrl);
130 ret = maps_place_category_get_id(mapsCate, &szId);
131 if (ret == MAPS_ERROR_NONE && szId && *szId) {
132 hereCate.SetCategoryId(CategoryId(szId));
133 } else if (hereCate.GetTitle().size() > 0) {
134 hereCate.SetCategoryId(CategoryId(hereCate.GetTitle()));
135 hereCate.SetTitle("");
139 hereCateList.push_back(hereCate);
140 m_pDiscoveryQuery->SetCategoriesFilter(hereCateList);
141 maps_place_category_destroy(mapsCate);
145 ret = maps_place_filter_get_place_name(hFilter, &szName);
146 if (ret == MAPS_ERROR_NONE && szName && *szName)
147 m_pDiscoveryQuery->SetSearchText(szName);
150 char *szKeyword = NULL;
151 ret = maps_place_filter_get_keyword(hFilter, &szKeyword);
152 if (ret == MAPS_ERROR_NONE && szKeyword && *szKeyword) {
153 String szSearchText = m_pDiscoveryQuery->GetSearchText();
154 if (szSearchText.size() > 0) szSearchText += " ";
155 szSearchText += szKeyword;
156 m_pDiscoveryQuery->SetSearchText(szSearchText);
160 char *szAddress = NULL;
161 ret = maps_place_filter_get_place_address(hFilter, &szAddress);
162 if (ret == MAPS_ERROR_NONE && szAddress && *szAddress) {
163 String szSearchText = szAddress;
164 if (m_pDiscoveryQuery->GetSearchText().size() > 0)
165 szSearchText += " " + m_pDiscoveryQuery->GetSearchText();
166 m_pDiscoveryQuery->SetSearchText(szSearchText);
170 return HERE_ERROR_NONE;
173 here_error_e HerePlace::StartDiscoveryPlace(maps_coordinates_h hCoord, int nDistance)
175 if (!hCoord || nDistance < 0)
176 return HERE_ERROR_INVALID_PARAMETER;
178 int meters = (int)(HereUtils::ConvertDistance(nDistance, m_eDistanceUnit, MAPS_DISTANCE_UNIT_M) + 0.5);
179 maps_area_h area = NULL;
180 maps_area_create_circle(hCoord, meters, &area);
181 here_error_e error = StartDiscoveryPlace(area);
182 maps_area_destroy(area);
186 here_error_e HerePlace::StartDiscoveryPlace(maps_area_h hArea, const char *szAddr)
189 return HERE_ERROR_INVALID_PARAMETER;
191 if (!m_pDiscoveryQuery)
192 return HERE_ERROR_OUT_OF_MEMORY;
201 PlaceCmdType cmdType;
202 maps_area_s *pArea = (maps_area_s*)hArea;
203 GeoCoordinates geoCoord;
204 GeoBoundingArea *geoArea = NULL;
205 GeoBoundingBox geoBox;
206 GeoBoundingCircle geoCircle;
208 /* Merge search text with other search text being in preference */
209 String szSearchText = (szAddr ? szAddr : "");
210 if (m_pDiscoveryQuery->GetSearchText().size() > 0) {
211 if (szSearchText.size() > 0) szSearchText += " ";
212 szSearchText += m_pDiscoveryQuery->GetSearchText();
214 m_pDiscoveryQuery->SetSearchText(szSearchText);
216 /* Decide command type */
217 if (!szSearchText.empty()) {
218 cmdType = PLACE_CMD_TEXT;
219 } else if (pArea->type == MAPS_AREA_CIRCLE && pArea->circle.radius == 0) {
220 cmdType = PLACE_CMD_CENTER;
222 cmdType = PLACE_CMD_AREA;
225 /* Get proximity with area */
226 if (cmdType == PLACE_CMD_TEXT || cmdType == PLACE_CMD_CENTER) {
227 if (pArea->type == MAPS_AREA_RECTANGLE) {
228 double dLat1 = pArea->rect.top_left.latitude;
229 double dLng1 = pArea->rect.top_left.longitude;
230 double dLat2 = pArea->rect.bottom_right.latitude;
231 double dLng2 = pArea->rect.bottom_right.longitude;
232 double dLat = (dLat1 + dLat2) / 2;
233 double dLng = (dLng1 + dLng2) / 2;
235 geoCoord.SetLatitude(dLat);
236 geoCoord.SetLongitude(dLng);
237 } else if (pArea->type == MAPS_AREA_CIRCLE) {
238 double dLat = pArea->circle.center.latitude;
239 double dLng = pArea->circle.center.longitude;
241 geoCoord.SetLatitude(dLat);
242 geoCoord.SetLongitude(dLng);
244 return HERE_ERROR_INVALID_PARAMETER;
246 } else if (cmdType == PLACE_CMD_AREA) {
247 if (pArea->type == MAPS_AREA_RECTANGLE) {
248 HereUtils::Convert(hArea, geoBox);
250 } else if (pArea->type == MAPS_AREA_CIRCLE) {
251 HereUtils::Convert(hArea, geoCircle);
252 geoArea = &geoCircle;
254 return HERE_ERROR_INVALID_PARAMETER;
259 if (cmdType == PLACE_CMD_TEXT) {
260 m_pDiscoveryQuery->SetType(DiscoveryQuery::QT_SEARCH);
261 m_pDiscoveryQuery->SetProximity(geoCoord);
262 } else if (cmdType == PLACE_CMD_CENTER) {
263 m_pDiscoveryQuery->SetType(DiscoveryQuery::QT_EXPLORE);
264 m_pDiscoveryQuery->SetProximity(geoCoord);
266 m_pDiscoveryQuery->SetType(DiscoveryQuery::QT_EXPLORE);
268 m_pDiscoveryQuery->SetArea(*geoArea);
272 m_nRestReqId = m_pDiscoveryQuery->Execute(*this, NULL);
274 return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
277 here_error_e HerePlace::StartDiscoveryPlaceList(maps_area_h hArea)
279 m_bReplyWithList = true;
280 return StartDiscoveryPlace(hArea);
283 here_error_e HerePlace::PreparePlaceDetailsQuery()
285 if (m_pPlaceDetailsQuery)
286 return HERE_ERROR_PERMISSION_DENIED;
288 m_pPlaceDetailsQuery = new (std::nothrow) PlaceDetailsQuery();
290 if (!m_pPlaceDetailsQuery)
291 return HERE_ERROR_OUT_OF_MEMORY;
293 return HERE_ERROR_NONE;
296 here_error_e HerePlace::PreparePlaceDetailsPreference(maps_preference_h hPref)
298 if (!m_pPlaceDetailsQuery)
299 return HERE_ERROR_OUT_OF_MEMORY;
302 return HERE_ERROR_NONE;
305 char *szLanguage = NULL;
306 ret = maps_preference_get_language(hPref, &szLanguage);
307 if (ret == MAPS_ERROR_NONE && szLanguage && *szLanguage)
308 m_pPlaceDetailsQuery->SetLanguage(szLanguage);
311 maps_distance_unit_e eUnit;
312 ret = maps_preference_get_distance_unit(hPref, &eUnit);
313 if (ret == MAPS_ERROR_NONE)
314 m_eDistanceUnit = eUnit;
316 return HERE_ERROR_NONE;
319 here_error_e HerePlace::StartPlaceDetails(const char *szUrl)
321 if (!m_pPlaceDetailsQuery)
322 return HERE_ERROR_OUT_OF_MEMORY;
324 if (!szUrl || (szUrl && strlen(szUrl) <= 0))
325 return HERE_ERROR_INVALID_PARAMETER;
327 m_bPlaceDetails = true;
329 m_nRestReqId = m_pPlaceDetailsQuery->Execute(*this, NULL, szUrl);
331 return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
334 here_error_e HerePlace::StartPlaceDetailsInternal(const char *szUrl)
336 if (!szUrl || (szUrl && strlen(szUrl) <= 0))
337 return HERE_ERROR_INVALID_PARAMETER;
339 std::unique_ptr<PlaceDetailsQuery> pPlaceDetailsQuery (new (std::nothrow)PlaceDetailsQuery());
341 m_bPlaceDetailsInternal = true;
342 bool bExcuted = (int)(pPlaceDetailsQuery->Execute(*this, NULL, szUrl) > 0);
344 return (bExcuted ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
347 void HerePlace::OnDiscoverReply(const DiscoveryReply &Reply)
349 maps_place_h mapsPlace;
350 PlaceItemList herePlaceList = Reply.GetPlaceItems();
351 PlaceItemList::iterator herePlaceIt;
352 SearchItemList hereSearchList = Reply.GetSearchItems();
353 SearchItemList::iterator hereSearchIt;
354 LinkObject hereLinkObj;
355 GeoCoordinates hereCoord;
356 maps_coordinates_h mapsCoord;
358 maps_place_category_h mapsCate;
359 maps_place_rating_h mapsRating;
360 int error = MAPS_ERROR_UNKNOWN, sub_error;
361 bool is_valid, isPending;
363 if (m_bCanceled || !m_pCbFunc) {
369 m_nReplyCnt = herePlaceList.size() + hereSearchList.size();
371 for (herePlaceIt = herePlaceList.begin();
372 herePlaceIt != herePlaceList.end();
374 if ((error = maps_place_create(&mapsPlace)) == MAPS_ERROR_NONE) {
378 hereLinkObj = herePlaceIt->GetLinkObject();
380 if (!hereLinkObj.GetTitle().empty())
381 maps_place_set_name(mapsPlace, (char*)hereLinkObj.GetTitle().c_str());
383 if (!hereLinkObj.GetHref().empty())
384 maps_place_set_uri(mapsPlace, (char*)hereLinkObj.GetHref().c_str());
386 if (!hereLinkObj.GetId().empty())
387 maps_place_set_id(mapsPlace, (char*)hereLinkObj.GetId().c_str());
393 hereCoord = herePlaceIt->GetPosition();
394 if (maps_coordinates_create(hereCoord.GetLatitude(), hereCoord.GetLongitude(),
395 &mapsCoord) == MAPS_ERROR_NONE) {
396 maps_place_set_location(mapsPlace, mapsCoord);
397 maps_coordinates_destroy(mapsCoord);
400 /* rating (optional) */
401 if (maps_place_rating_create(&mapsRating) == MAPS_ERROR_NONE) {
402 maps_place_rating_set_average(mapsRating, herePlaceIt->GetAverageRating());
403 maps_place_set_rating(mapsPlace, mapsRating);
404 maps_place_rating_destroy(mapsRating);
407 /* category (optional) */
408 hereCate = herePlaceIt->GetCategory();
410 maps_item_list_h mapsCateList;
411 if (maps_item_list_create(&mapsCateList) == MAPS_ERROR_NONE) {
412 if (maps_place_category_create(&mapsCate) == MAPS_ERROR_NONE) {
415 if (!hereCate.GetCategoryId().ToString().empty()) {
416 sub_error = maps_place_category_set_id(mapsCate,
417 (char*)hereCate.GetCategoryId().ToString().c_str());
418 is_valid |= (sub_error == MAPS_ERROR_NONE);
421 if (!hereCate.GetTitle().empty()) {
422 sub_error = maps_place_category_set_name(mapsCate,
423 (char*)hereCate.GetTitle().c_str());
424 is_valid |= (sub_error == MAPS_ERROR_NONE);
427 if (!hereCate.GetHref().empty()) {
428 sub_error = maps_place_category_set_url(mapsCate,
429 (char*)hereCate.GetHref().c_str());
430 is_valid |= (sub_error == MAPS_ERROR_NONE);
434 maps_item_list_append(mapsCateList, mapsCate, maps_place_category_clone);
435 maps_place_set_categories(mapsPlace, mapsCateList);
436 maps_item_list_remove_all(mapsCateList, maps_place_category_destroy);
438 maps_place_category_destroy(mapsCate);
440 maps_item_list_destroy(mapsCateList);
444 maps_place_set_distance(mapsPlace,
445 HereUtils::ConvertDistance((int)herePlaceIt->GetDistance(), m_eDistanceUnit) + 0.5);
448 /* herePlaceList.GetIsSponsored() */
452 /* If needed PlaceDetails information, postpone to send a reply */
453 if(__sending_place_details_query_automatically && !m_bReplyWithList) {
454 hereLinkObj = herePlaceIt->GetLinkObject();
455 if (!hereLinkObj.GetHref().empty() && !hereLinkObj.GetId().empty()) {
456 if (StartPlaceDetailsInternal(hereLinkObj.GetHref().c_str()) == HERE_ERROR_NONE) {
457 m_PlaceList.push_back(mapsPlace);
459 MAPS_LOGD("Add maps_place_h to the pending list. id=%s", hereLinkObj.GetId().data());
466 m_PlaceList.push_back(mapsPlace);
473 for (hereSearchIt = hereSearchList.begin();
474 hereSearchIt != hereSearchList.end();
476 error = maps_place_create(&mapsPlace);
478 if(error == MAPS_ERROR_NONE) {
482 hereLinkObj = hereSearchIt->GetLinkObject();
484 if (!hereLinkObj.GetTitle().empty()) {
485 sub_error = maps_place_set_name(mapsPlace,
486 (char*)hereLinkObj.GetTitle().c_str());
487 is_valid |= (sub_error == MAPS_ERROR_NONE);
490 if (!hereLinkObj.GetHref().empty()) {
491 sub_error = maps_place_set_uri(mapsPlace,
492 (char*)hereLinkObj.GetHref().c_str());
493 is_valid |= (sub_error == MAPS_ERROR_NONE);
496 if (!hereLinkObj.GetId().empty()) {
497 sub_error = maps_place_set_id(mapsPlace,
498 (char*)hereLinkObj.GetId().c_str());
499 is_valid |= (sub_error == MAPS_ERROR_NONE);
506 error = MAPS_ERROR_NOT_FOUND;
507 maps_place_destroy(mapsPlace);
510 m_PlaceList.push_back(mapsPlace);
519 if (!m_bReplyFlushed && (m_nReplyCnt == 0 || m_nReplyIdx == m_nReplyCnt)) {
520 if (m_nReplyCnt == 0)
521 error = MAPS_ERROR_NOT_FOUND;
523 __flushReplies(error);
528 void HerePlace::OnDiscoverFailure(const DiscoveryReply& Reply)
530 if (!m_bReplyFlushed) {
533 __flushReplies((maps_error_e)GetErrorCode(Reply));
538 void HerePlace::OnPlaceDetailsReply(const PlaceDetailsReply &Reply)
540 if (m_nReplyCnt == 0) {
541 if (m_bCanceled || !m_pCbFunc) {
548 PlaceDetails herePlace = Reply.GetPlaceDetails();
549 maps_place_h mapsPlace = NULL;
550 int error = MAPS_ERROR_NONE, sub_error;
551 bool is_valid, isPending = false;
555 /* Finding maps_place_h which is already pending since DiscoverReply */
556 PlaceList::iterator it;
557 for (it = m_PlaceList.begin(); it != m_PlaceList.end(); it++) {
558 if (maps_place_get_id(*it, &placeId) == MAPS_ERROR_NONE) {
559 placeIdLen = strlen(placeId);
560 if(!herePlace.GetPlaceId().compare(0, placeIdLen, placeId)) {
563 MAPS_LOGD("Found maps_place_h in the pending list. id=%s", placeId);
571 /* If not found, create new handle */
573 error = maps_place_create(&mapsPlace);
575 if (error == MAPS_ERROR_NONE) {
579 if (!herePlace.GetName().empty()) {
580 sub_error = maps_place_set_name(mapsPlace, (char*)herePlace.GetName().c_str());
581 is_valid |= (sub_error == MAPS_ERROR_NONE);
585 if (!herePlace.GetPlaceId().empty()) {
586 sub_error = maps_place_set_id(mapsPlace, (char*)herePlace.GetPlaceId().c_str());
587 is_valid |= (sub_error == MAPS_ERROR_NONE);
591 if (!herePlace.GetView().empty()) {
592 sub_error = maps_place_set_uri(mapsPlace, (char*)herePlace.GetView().c_str());
593 is_valid |= (sub_error == MAPS_ERROR_NONE);
598 /* maps not supported // herePlace.GetIconPath(); */
601 ProcessPlaceLocation(herePlace, mapsPlace);
603 ProcessPlaceContact(herePlace, mapsPlace);
605 ProcessPlaceCategory(herePlace, mapsPlace);
608 /* maps & here not supported */
610 ProcessPlaceImage(herePlace, mapsPlace);
612 ProcessPlaceDetails(herePlace, mapsPlace);
614 ProcessPlaceReviews(herePlace, mapsPlace);
616 ProcessPlaceRatings(herePlace, mapsPlace);
618 ProcessPlaceRated(herePlace, mapsPlace);
621 m_PlaceList.push_back(mapsPlace);
626 error = MAPS_ERROR_NOT_FOUND;
627 maps_place_destroy(mapsPlace);
634 if (!m_bReplyFlushed && (m_nReplyCnt == 0 || m_nReplyIdx == m_nReplyCnt)) {
635 if (m_nReplyCnt == 0)
636 error = MAPS_ERROR_NOT_FOUND;
638 __flushReplies(error);
643 void HerePlace::OnPlaceDetailsFailure(const PlaceDetailsReply& Reply)
645 if (!m_bPlaceDetailsInternal) {
646 if (!m_bReplyFlushed) {
649 __flushReplies(GetErrorCode(Reply));
654 MAPS_LOGD("Internal error during updating detailed information for the place. (%d/%d)", m_nReplyIdx, m_nReplyCnt);
655 if (!m_bReplyFlushed && (m_nReplyIdx == m_nReplyCnt))
657 __flushReplies(MAPS_ERROR_NONE);
663 void HerePlace::ProcessPlaceLocation(PlaceDetails herePlace, maps_place_h mapsPlace)
665 GeoLocation hereLocation = herePlace.GetLocation();
668 GeoCoordinates hereCoord = hereLocation.GetDisplayPosition();
669 maps_coordinates_h mapsCoord;
671 if (maps_coordinates_create(hereCoord.GetLatitude(),
672 hereCoord.GetLongitude(), &mapsCoord) == MAPS_ERROR_NONE) {
673 maps_place_set_location(mapsPlace, mapsCoord);
674 maps_coordinates_destroy(mapsCoord);
678 Address hereAddr = hereLocation.GetAddress();
679 maps_address_h mapsAddr;
683 if (maps_address_create(&mapsAddr) == MAPS_ERROR_NONE) {
686 if (!hereAddr.GetHouseNumber().empty()) {
687 error = maps_address_set_building_number(mapsAddr,
688 hereAddr.GetHouseNumber().c_str());
689 is_valid |= (error == MAPS_ERROR_NONE);
692 if (!hereAddr.GetStreet().empty()) {
693 error = maps_address_set_street(mapsAddr, hereAddr.GetStreet().c_str());
694 is_valid |= (error == MAPS_ERROR_NONE);
697 if (!hereAddr.GetDistrict().empty()) {
698 error = maps_address_set_district(mapsAddr, hereAddr.GetDistrict().c_str());
699 is_valid |= (error == MAPS_ERROR_NONE);
702 if (!hereAddr.GetCity().empty()) {
703 error = maps_address_set_city(mapsAddr, hereAddr.GetCity().c_str());
704 is_valid |= (error == MAPS_ERROR_NONE);
707 if (!hereAddr.GetState().empty()) {
708 error = maps_address_set_state(mapsAddr, hereAddr.GetState().c_str());
709 is_valid |= (error == MAPS_ERROR_NONE);
712 if (!hereAddr.GetCountry().empty()) {
713 error = maps_address_set_country(mapsAddr, hereAddr.GetCountry().c_str());
714 is_valid |= (error == MAPS_ERROR_NONE);
717 if (!hereAddr.GetCountryCode().empty()) {
718 error = maps_address_set_country_code(mapsAddr, hereAddr.GetCountryCode().c_str());
719 is_valid |= (error == MAPS_ERROR_NONE);
722 if (!hereAddr.GetCounty().empty()) {
723 error = maps_address_set_county(mapsAddr, hereAddr.GetCounty().c_str());
724 is_valid |= (error == MAPS_ERROR_NONE);
727 if (!hereAddr.GetPostalCode().empty()) {
728 error = maps_address_set_postal_code(mapsAddr, hereAddr.GetPostalCode().c_str());
729 is_valid |= (error == MAPS_ERROR_NONE);
732 if (!hereAddr.GetLabel().empty()) {
733 error = maps_address_set_freetext(mapsAddr, hereAddr.GetLabel().c_str());
734 is_valid |= (error == MAPS_ERROR_NONE);
738 maps_place_set_address(mapsPlace, mapsAddr);
740 maps_address_destroy(mapsAddr);
745 void HerePlace::ProcessPlaceContact(PlaceDetails herePlace, maps_place_h mapsPlace)
748 ContactDetailsList hereContList = herePlace.GetContactDetails();
749 ContactDetailsList::iterator hereCont;
750 maps_item_list_h mapsContList;
751 maps_place_contact_h mapsCont;
755 if (hereContList.empty()) return;
757 if (maps_item_list_create(&mapsContList) != MAPS_ERROR_NONE) return;
759 for (hereCont = hereContList.begin(); hereCont != hereContList.end(); hereCont++) {
760 if (maps_place_contact_create(&mapsCont) != MAPS_ERROR_NONE) continue;
764 if (!hereCont->GetLabel().empty()) {
765 error = maps_place_contact_set_label(mapsCont,
766 (char*)hereCont->GetLabel().c_str());
767 is_valid |= (error == MAPS_ERROR_NONE);
770 if (!hereCont->GetValue().empty()) {
771 error = maps_place_contact_set_value(mapsCont,
772 (char*)hereCont->GetValue().c_str());
773 is_valid |= (error == MAPS_ERROR_NONE);
776 if (!hereCont->GetContactType().empty()) {
777 error = maps_place_contact_set_type(mapsCont,
778 (char*)hereCont->GetContactType().c_str());
779 is_valid |= (error == MAPS_ERROR_NONE);
783 maps_item_list_append(mapsContList, mapsCont,
784 maps_place_contact_clone);
786 maps_place_contact_destroy(mapsCont);
789 if (maps_item_list_items(mapsContList)) {
790 maps_place_set_contacts(mapsPlace, mapsContList);
791 maps_item_list_remove_all(mapsContList, maps_place_contact_destroy);
793 maps_item_list_destroy(mapsContList);
796 void HerePlace::ProcessPlaceCategory(PlaceDetails herePlace, maps_place_h mapsPlace)
798 CategoryList hereCateList = herePlace.GetCategories();
799 CategoryList::iterator hereCate;
800 maps_item_list_h mapsCateList;
801 maps_place_category_h mapsCate;
803 bool is_valid = false;
805 if (hereCateList.empty()) return;
807 if (maps_item_list_create(&mapsCateList) != MAPS_ERROR_NONE) return;
809 // maps-service supports only one category
810 hereCate = hereCateList.begin();
811 if (maps_place_category_create(&mapsCate) == MAPS_ERROR_NONE)
813 if (!hereCate->GetCategoryId().ToString().empty()) {
814 error = maps_place_category_set_id(mapsCate,
815 hereCate->GetCategoryId().ToString().c_str());
816 is_valid |= (error == MAPS_ERROR_NONE);
819 if (!hereCate->GetTitle().empty()) {
820 error = maps_place_category_set_name(mapsCate,
821 hereCate->GetTitle().c_str());
822 is_valid |= (error == MAPS_ERROR_NONE);
825 if (!hereCate->GetHref().empty()) {
826 error = maps_place_category_set_url(mapsCate,
827 hereCate->GetHref().c_str());
828 is_valid |= (error == MAPS_ERROR_NONE);
832 maps_item_list_append(mapsCateList, mapsCate, maps_place_category_clone);
834 maps_place_category_destroy(mapsCate);
837 if (maps_item_list_items(mapsCateList)) {
838 maps_place_set_categories(mapsPlace, mapsCateList);
839 maps_item_list_remove_all(mapsCateList, maps_place_category_destroy);
841 maps_item_list_destroy(mapsCateList);
844 void HerePlace::ProcessPlaceImage(PlaceDetails herePlace, maps_place_h mapsPlace)
846 ImageContentList hereImageList = herePlace.GetImageContent();
847 ImageContentList::iterator hereImage;
848 maps_item_list_h mapsImageList;
849 maps_place_image_h mapsImage;
850 maps_place_link_object_h mapsImageUser;
851 LinkObject hereImageUser;
853 bool is_valid, is_valid2;
855 if (hereImageList.empty()) return;
857 if (maps_item_list_create(&mapsImageList) != MAPS_ERROR_NONE) return;
859 for (hereImage = hereImageList.begin(); hereImage != hereImageList.end(); hereImage++) {
860 if (maps_place_image_create(&mapsImage) != MAPS_ERROR_NONE) continue;
864 /* here not supported
865 // maps_place_image_set_height(maps_place_image_h mapsPlace, const int height); */
867 /* here not supported
868 // maps_place_image_set_media(maps_place_image_h mapsPlace, maps_place_media_h media); */
870 if (!hereImage->GetSource().empty()) {
871 error = maps_place_image_set_url(mapsImage, (char*)hereImage->GetSource().c_str());
872 is_valid |= (error == MAPS_ERROR_NONE);
875 if (!hereImage->GetImageId().empty()) {
876 error = maps_place_image_set_id(mapsImage, (char*)hereImage->GetImageId().c_str());
877 is_valid |= (error == MAPS_ERROR_NONE);
880 hereImageUser = hereImage->GetUser();
881 if (maps_place_link_object_create(&mapsImageUser) == MAPS_ERROR_NONE) {
884 if (!hereImageUser.GetId().empty()) {
885 error = maps_place_link_object_set_id(mapsImageUser,
886 (char*)hereImageUser.GetId().c_str());
887 is_valid2 |= (error == MAPS_ERROR_NONE);
890 if (!hereImageUser.GetTitle().empty()) {
891 error = maps_place_link_object_set_name(mapsImageUser,
892 (char*)hereImageUser.GetTitle().c_str());
893 is_valid2 |= (error == MAPS_ERROR_NONE);
896 if (!hereImageUser.GetHref().empty()) {
897 error = maps_place_link_object_set_string(mapsImageUser,
898 (char*)hereImageUser.GetHref().c_str());
899 is_valid2 |= (error == MAPS_ERROR_NONE);
902 if (!hereImageUser.GetType().empty()) {
903 error = maps_place_link_object_set_type(mapsImageUser,
904 (char*)hereImageUser.GetType().c_str());
905 is_valid2 |= (error == MAPS_ERROR_NONE);
909 maps_place_image_set_user_link(mapsImage, mapsImageUser);
910 is_valid |= is_valid2;
912 maps_place_link_object_destroy(mapsImageUser);
916 maps_item_list_append(mapsImageList, mapsImage, maps_place_image_clone);
918 maps_place_image_destroy(mapsImage);
921 if (maps_item_list_items(mapsImageList)) {
922 maps_place_set_images(mapsPlace, mapsImageList);
923 maps_item_list_remove_all(mapsImageList, maps_place_image_destroy);
925 maps_item_list_destroy(mapsImageList);
928 void HerePlace::ProcessPlaceDetails(PlaceDetails herePlace, maps_place_h mapsPlace)
930 EditorialContentList hereEditList = herePlace.GetEditorialContent();
931 EditorialContentList::iterator hereEdit;
932 maps_item_list_h mapsEditList;
933 maps_place_editorial_h mapsEdit;
937 if (hereEditList.empty()) return;
939 if (maps_item_list_create(&mapsEditList) != MAPS_ERROR_NONE) return;
941 for (hereEdit = hereEditList.begin(); hereEdit != hereEditList.end(); hereEdit++) {
942 if (maps_place_editorial_create(&mapsEdit) != MAPS_ERROR_NONE) continue;
946 if (!hereEdit->GetDescription().empty()) {
947 error = maps_place_editorial_set_description(mapsEdit,
948 (char*)hereEdit->GetDescription().c_str());
949 is_valid |= (error == MAPS_ERROR_NONE);
952 if (!hereEdit->GetLanguage().empty()) {
953 error = maps_place_editorial_set_language(mapsEdit,
954 (char*)hereEdit->GetLanguage().c_str());
955 is_valid |= (error == MAPS_ERROR_NONE);
958 /* maps_place_editorial_set_media(mapsEdit, maps_place_media_h media); */
961 maps_item_list_append(mapsEditList, mapsEdit, maps_place_editorial_clone);
963 maps_place_editorial_destroy(mapsEdit);
966 if (maps_item_list_items(mapsEditList)) {
967 maps_place_set_editorials(mapsPlace, mapsEditList);
968 maps_item_list_remove_all(mapsEditList, maps_place_editorial_destroy);
970 maps_item_list_destroy(mapsEditList);
973 void HerePlace::ProcessPlaceReviews(PlaceDetails herePlace, maps_place_h mapsPlace)
975 ReviewContentList hereReviewList = herePlace.GetReviewContent();
976 ReviewContentList::iterator hereReview;
977 maps_place_review_h mapsReview;
978 maps_item_list_h mapsReviewList;
979 LinkObject hereReviewUser;
980 maps_place_link_object_h mapsReviewUser;
982 bool is_valid, is_valid2;
984 if (hereReviewList.empty()) return;
986 if (maps_item_list_create(&mapsReviewList) != MAPS_ERROR_NONE) return;
988 for (hereReview = hereReviewList.begin(); hereReview != hereReviewList.end(); hereReview++) {
989 if (maps_place_review_create(&mapsReview) != MAPS_ERROR_NONE) continue;
993 if (!hereReview->GetDateTime().empty()) {
994 error = maps_place_review_set_date(mapsReview,
995 (char*)hereReview->GetDateTime().c_str());
996 is_valid |= (error == MAPS_ERROR_NONE);
999 if (!hereReview->GetDescription().empty()) {
1000 error = maps_place_review_set_description(mapsReview,
1001 (char*)hereReview->GetDescription().c_str());
1002 is_valid |= (error == MAPS_ERROR_NONE);
1005 if (!hereReview->GetLanguage().empty()) {
1006 error = maps_place_review_set_language(mapsReview,
1007 (char*)hereReview->GetLanguage().c_str());
1008 is_valid |= (error == MAPS_ERROR_NONE);
1011 /* maps_place_review_set_media(mapsReview, maps_place_media_h media) */
1013 maps_place_review_set_rating(mapsReview, hereReview->GetRating());
1015 if (!hereReview->GetTitle().empty()) {
1016 error = maps_place_review_set_title(mapsReview,
1017 (char*)hereReview->GetTitle().c_str());
1018 is_valid |= (error == MAPS_ERROR_NONE);
1021 hereReviewUser = hereReview->GetUser();
1022 if (maps_place_link_object_create(&mapsReviewUser) == MAPS_ERROR_NONE) {
1025 if (!hereReviewUser.GetId().empty()) {
1026 error = maps_place_link_object_set_id(mapsReviewUser,
1027 (char*)hereReviewUser.GetId().c_str());
1028 is_valid2 |= (error == MAPS_ERROR_NONE);
1031 if (!hereReviewUser.GetTitle().empty()) {
1032 error = maps_place_link_object_set_name(mapsReviewUser,
1033 (char*)hereReviewUser.GetTitle().c_str());
1034 is_valid2 |= (error == MAPS_ERROR_NONE);
1037 if (!hereReviewUser.GetHref().empty()) {
1038 error = maps_place_link_object_set_string(mapsReviewUser,
1039 (char*)hereReviewUser.GetHref().c_str());
1040 is_valid2 |= (error == MAPS_ERROR_NONE);
1043 if (!hereReviewUser.GetType().empty()) {
1044 error = maps_place_link_object_set_type(mapsReviewUser,
1045 (char*)hereReviewUser.GetType().c_str());
1046 is_valid2 |= (error == MAPS_ERROR_NONE);
1050 maps_place_review_set_user_link(mapsReview, mapsReviewUser);
1051 is_valid |= is_valid2;
1054 maps_place_link_object_destroy(mapsReviewUser);
1058 maps_item_list_append(mapsReviewList, mapsReview, maps_place_review_clone);
1060 maps_place_review_destroy(mapsReview);
1063 if (maps_item_list_items(mapsReviewList)) {
1064 maps_place_set_reviews(mapsPlace, mapsReviewList);
1065 maps_item_list_remove_all(mapsReviewList, maps_place_review_destroy);
1067 maps_item_list_destroy(mapsReviewList);
1070 void HerePlace::ProcessPlaceRatings(PlaceDetails herePlace, maps_place_h mapsPlace)
1072 Ratings hereRating = herePlace.GetRatings();
1073 maps_place_rating_h mapsRating;
1075 if (maps_place_rating_create(&mapsRating) != MAPS_ERROR_NONE) return;
1077 maps_place_rating_set_average(mapsRating, hereRating.GetAverage());
1078 maps_place_rating_set_count(mapsRating, hereRating.GetCount());
1079 maps_place_set_rating(mapsPlace, mapsRating);
1080 maps_place_rating_destroy(mapsRating);
1083 void HerePlace::ProcessPlaceRated(PlaceDetails herePlace, maps_place_h mapsPlace)
1085 RelatedItem hereRelated = herePlace.GetRelatedItem();
1086 maps_place_link_object_h mapsRelated;
1088 bool is_valid = false;
1090 if (maps_place_link_object_create(&mapsRelated) != MAPS_ERROR_NONE) return;
1092 //need to check if GetId() exist
1093 //maps_place_link_object_set_id(mapsRelated, hereRelated.GetId());
1095 if (!hereRelated.GetTitle().empty()) {
1096 error = maps_place_link_object_set_name(mapsRelated,
1097 (char*)hereRelated.GetTitle().c_str());
1098 is_valid |= (error == MAPS_ERROR_NONE);
1101 if (!hereRelated.GetHref().empty()) {
1102 error = maps_place_link_object_set_string(mapsRelated,
1103 (char*)hereRelated.GetHref().c_str());
1104 is_valid |= (error == MAPS_ERROR_NONE);
1107 if (!hereRelated.GetType().empty()) {
1108 error = maps_place_link_object_set_type(mapsRelated,
1109 (char*)hereRelated.GetType().c_str());
1110 is_valid |= (error == MAPS_ERROR_NONE);
1114 maps_place_set_related_link(mapsPlace, mapsRelated);
1116 maps_place_link_object_destroy(mapsRelated);
1119 void HerePlace::__flushReplies(int error)
1121 maps_place_h mapsPlace;
1122 maps_item_list_h placeList = NULL;
1125 __sortList(m_PlaceList);
1127 if (m_bPlaceDetails) {
1128 if (error != MAPS_ERROR_NONE) {
1129 ((maps_service_get_place_details_cb)m_pCbFunc)((maps_error_e)error, m_nReqId, NULL, m_pUserData);
1133 mapsPlace = m_PlaceList.front();
1134 m_PlaceList.pop_front();
1136 ((maps_service_get_place_details_cb)m_pCbFunc)((maps_error_e)error, m_nReqId, mapsPlace, m_pUserData);
1137 } else if (m_bReplyWithList) {
1138 if (error == MAPS_ERROR_NONE)
1139 error = maps_place_list_create(&placeList);
1141 if (error != MAPS_ERROR_NONE) {
1143 maps_place_list_destroy(placeList);
1144 ((maps_service_search_place_list_cb)m_pCbFunc)((maps_error_e)error, m_nReqId, 0, NULL, m_pUserData);
1148 while (m_nReplyIdx < m_nReplyCnt && !m_bCanceled && !m_PlaceList.empty()) {
1149 mapsPlace = m_PlaceList.front();
1150 m_PlaceList.pop_front();
1152 maps_item_list_append(placeList, mapsPlace, maps_place_clone);
1156 ((maps_service_search_place_list_cb)m_pCbFunc)((maps_error_e)error, m_nReqId, maps_item_list_items(placeList), placeList, m_pUserData);
1158 maps_place_list_destroy(placeList);
1160 if (error != MAPS_ERROR_NONE) {
1161 ((maps_service_search_place_cb)m_pCbFunc)((maps_error_e)error, m_nReqId, 0, 0, NULL, m_pUserData);
1165 while (m_nReplyIdx < m_nReplyCnt && !m_bCanceled && !m_PlaceList.empty()) {
1166 mapsPlace = m_PlaceList.front();
1167 m_PlaceList.pop_front();
1169 /* callback function */
1170 if (((maps_service_search_place_cb)m_pCbFunc)((maps_error_e)error, m_nReqId,
1171 m_nReplyIdx++, m_nReplyCnt, mapsPlace, m_pUserData) == FALSE)
1175 m_bReplyFlushed = true;
1178 bool HerePlace::__compareWithTitle(const maps_place_h &item1, const maps_place_h &item2)
1180 bool result = false;
1181 char *str1 = NULL, *str2 = NULL;
1183 if (maps_place_get_name(item1, &str1) == MAPS_ERROR_NONE &&
1184 maps_place_get_name(item2, &str2) == MAPS_ERROR_NONE)
1185 result = (strcmp(str1, str2) < 0);
1192 bool HerePlace::__compareWithId(const maps_place_h &item1, const maps_place_h &item2)
1194 bool result = false;
1195 char *str1 = NULL, *str2 = NULL;
1197 if (maps_place_get_id(item1, &str1) == MAPS_ERROR_NONE &&
1198 maps_place_get_id(item2, &str2) == MAPS_ERROR_NONE)
1199 result = (strcmp(str1, str2) < 0);
1206 bool HerePlace::__compareWithDistance(const maps_place_h &item1, const maps_place_h &item2)
1208 bool result = false;
1209 int num1 = 0, num2 = 0;
1211 if (maps_place_get_distance(item1, &num1) == MAPS_ERROR_NONE &&
1212 maps_place_get_distance(item2, &num2) == MAPS_ERROR_NONE)
1213 result = (num1 < num2);
1218 bool HerePlace::__compareWithRating(const maps_place_h &item1, const maps_place_h &item2)
1220 bool result = false;
1221 maps_place_rating_h rat1 = NULL, rat2 = NULL;
1222 double num1 = 0, num2 = 0;
1224 if (maps_place_get_rating(item1, &rat1) == MAPS_ERROR_NONE &&
1225 maps_place_get_rating(item2, &rat2) == MAPS_ERROR_NONE) {
1226 if (maps_place_rating_get_average(rat1, &num1) == MAPS_ERROR_NONE &&
1227 maps_place_rating_get_average(rat2, &num2) == MAPS_ERROR_NONE)
1228 result = (num1 > num2);
1230 maps_place_rating_destroy(rat1);
1231 maps_place_rating_destroy(rat2);
1235 bool HerePlace::__compareWithCategoryCb(int index, int total,
1236 maps_place_category_h category,
1239 user_data = category;
1243 bool HerePlace::__compareWithCategory(const maps_place_h &item1, const maps_place_h &item2)
1245 bool result = false;
1246 maps_place_category_h cat1 = NULL, cat2 = NULL;
1247 char *str1 = NULL, *str2 = NULL;
1249 maps_place_foreach_category(item1, __compareWithCategoryCb, &cat1);
1250 maps_place_foreach_category(item2, __compareWithCategoryCb, &cat2);
1252 if (maps_place_category_get_id(item1, &str1) == MAPS_ERROR_NONE &&
1253 maps_place_category_get_id(item2, &str2) == MAPS_ERROR_NONE)
1254 result = (strcmp(str1, str2) < 0);
1256 maps_place_category_destroy(item1);
1257 maps_place_category_destroy(item2);
1263 void HerePlace::__sortList(PlaceList &list)
1265 if (!m_szSortBy) return;
1267 if (!strcmp(m_szSortBy, "name") || !strcmp(m_szSortBy, "title")) {
1268 std::sort(list.begin(), list.end(), __compareWithTitle);
1269 } else if (!strcmp(m_szSortBy, "id")) {
1270 std::sort(list.begin(), list.end(), __compareWithId);
1271 } else if (!strcmp(m_szSortBy, "distance")) {
1272 std::sort(list.begin(), list.end(), __compareWithDistance);
1273 } else if (!strcmp(m_szSortBy, "rate") || !strcmp(m_szSortBy, "rating")) {
1274 std::sort(list.begin(), list.end(), __compareWithRating);
1275 } else if (!strcmp(m_szSortBy, "category")) {
1276 std::sort(list.begin(), list.end(), __compareWithCategory);
1280 HERE_PLUGIN_END_NAMESPACE