Welcome to mirror list, hosted at ThFree Co, Russian Federation.

downloader_search_callback.cpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a965aaf065ef1ca422d74bef17378f7852c3b3c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "search/downloader_search_callback.hpp"

#include "search/result.hpp"

#include "editor/editable_data_source.hpp"

#include "storage/country_info_getter.hpp"
#include "storage/storage.hpp"

#include "base/logging.hpp"
#include "base/string_utils.hpp"

#include <set>
#include <string>

namespace
{
bool GetGroupCountryIdFromFeature(storage::Storage const & storage, FeatureType const & ft,
                                  std::string & name)
{
  int8_t const langIndices[] = {StringUtf8Multilang::kEnglishCode,
                                StringUtf8Multilang::kDefaultCode,
                                StringUtf8Multilang::kInternationalCode};

  for (auto const langIndex : langIndices)
  {
    if (!ft.GetName(langIndex, name))
      continue;
    if (storage.IsInnerNode(name))
      return true;
  }
  return false;
}
}  // namespace

namespace search
{
DownloaderSearchCallback::DownloaderSearchCallback(Delegate & delegate, DataSourceBase const & dataSource,
                                                   storage::CountryInfoGetter const & infoGetter,
                                                   storage::Storage const & storage,
                                                   storage::DownloaderSearchParams params)
  : m_delegate(delegate)
  , m_dataSource(dataSource)
  , m_infoGetter(infoGetter)
  , m_storage(storage)
  , m_params(move(params))
{
}

void DownloaderSearchCallback::operator()(search::Results const & results)
{
  storage::DownloaderSearchResults downloaderSearchResults;
  std::set<storage::DownloaderSearchResult> uniqueResults;

  for (auto const & result : results)
  {
    if (!result.HasPoint())
      continue;

    if (result.GetResultType() != search::Result::Type::LatLon)
    {
      FeatureID const & fid = result.GetFeatureID();
      EditableDataSource::FeaturesLoaderGuard loader(m_dataSource, fid.m_mwmId);
      FeatureType ft;
      if (!loader.GetFeatureByIndex(fid.m_index, ft))
      {
        LOG(LERROR, ("Feature can't be loaded:", fid));
        continue;
      }

      ftypes::Type const type = ftypes::IsLocalityChecker::Instance().GetType(ft);

      if (type == ftypes::COUNTRY || type == ftypes::STATE)
      {
        std::string groupFeatureName;
        if (GetGroupCountryIdFromFeature(m_storage, ft, groupFeatureName))
        {
          storage::DownloaderSearchResult downloaderResult(groupFeatureName,
                                                           result.GetString() /* m_matchedName */);
          if (uniqueResults.find(downloaderResult) == uniqueResults.end())
          {
            uniqueResults.insert(downloaderResult);
            downloaderSearchResults.m_results.push_back(downloaderResult);
          }
          continue;
        }
      }
    }
    auto const & mercator = result.GetFeatureCenter();
    storage::TCountryId const & countryId = m_infoGetter.GetRegionCountryId(mercator);
    if (countryId == storage::kInvalidCountryId)
      continue;

    storage::DownloaderSearchResult downloaderResult(countryId,
                                                     result.GetString() /* m_matchedName */);
    if (uniqueResults.find(downloaderResult) == uniqueResults.end())
    {
      uniqueResults.insert(downloaderResult);
      downloaderSearchResults.m_results.push_back(downloaderResult);
    }
  }

  downloaderSearchResults.m_query = m_params.m_query;
  downloaderSearchResults.m_endMarker = results.IsEndMarker();

  if (m_params.m_onResults)
  {
    auto onResults = m_params.m_onResults;
    m_delegate.RunUITask(
        [onResults, downloaderSearchResults]() { onResults(downloaderSearchResults); });
  }
}
}  // namespace search