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

storage.hpp « storage - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e8a482ebe5ddf7d98078459201891211053e60b (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once

#include "../platform/download_manager.hpp"
#include "../platform/platform.hpp"

#include "../defines.hpp"
#include "../storage/country.hpp"

#include "../std/vector.hpp"
#include "../std/map.hpp"
#include "../std/list.hpp"
#include "../std/string.hpp"
#include "../std/set.hpp"
#include "../std/function.hpp"

namespace storage
{
  /// Used in GUI
  enum TStatus
  {
    EOnDisk,
    ENotDownloaded,
    EDownloadFailed,
    EDownloading,
    EInQueue,
    EUnknown
  };

  enum TUpdateResult
  {
    ENoAnyUpdateAvailable = 0,
    ENewBinaryAvailable = 0x01,
    EBinaryCheckFailed = 0x02,
    EBinaryUpdateFailed = 0x04,
    ENewDataAvailable = 0x08,
    EDataCheckFailed = 0x10,
    EDataUpdateFailed = 0x20
  };

  struct TIndex
  {
    static int const INVALID;
    int m_group;
    int m_country;
    int m_region;
    TIndex(int group = INVALID, int country = INVALID, int region = INVALID)
      : m_group(group), m_country(country), m_region(region) {}
    bool operator==(TIndex const & other) const
    {
      return m_group == other.m_group && m_country == other.m_country && m_region == other.m_region;
    }
    bool operator<(TIndex const & other) const
    {
      if (m_group != other.m_group)
        return m_group < other.m_group;
      else if (m_country != other.m_country)
        return m_country < other.m_country;
      return m_region < other.m_region;
    }
  };

  /// Can be used to store local maps and/or maps available for download
  class Storage
  {
    /// stores timestamp for update checks
    uint32_t m_currentVersion;

    TCountriesContainer m_countries;

    typedef list<TIndex> TQueue;
    TQueue m_queue;
    /// used to correctly calculate total country download progress
    HttpProgressT m_countryProgress;

    typedef set<TIndex> TFailedCountries;
    /// stores countries which download has failed recently
    TFailedCountries m_failedCountries;

    /// @name Communicate with GUI
    //@{
    typedef function<void (TIndex const &)> TObserverChangeCountryFunction;
    typedef function<void (TIndex const &, HttpProgressT const &)> TObserverProgressFunction;
    typedef function<void (TUpdateResult, string const &)> TUpdateRequestFunction;
    TObserverChangeCountryFunction m_observerChange;
    TObserverProgressFunction m_observerProgress;
    TUpdateRequestFunction m_observerUpdateRequest;
    //@}

    /// @name Communicate with Framework
    //@{
  public:
    typedef function<void (ModelReaderPtr const &)> TAddMapFunction;
    typedef function<void (string const &)> TRemoveMapFunction;
    typedef function<void (m2::RectD const & r)> TUpdateRectFunction;
    typedef function<void (vector<ModelReaderPtr> &)> TEnumMapsFunction;

  private:
    TAddMapFunction m_addMap;
    TRemoveMapFunction m_removeMap;
    TUpdateRectFunction m_updateRect;
    //@}

    void DownloadNextCountryFromQueue();
    Country const & CountryByIndex(TIndex const & index) const;
    string UpdateBaseUrl() const;
    void ReInitCountries(bool forceReload);

  public:
    Storage() {}

    /// Adds all locally downloaded maps to the model
    void Init(TAddMapFunction addFunc, TRemoveMapFunction removeFunc, TUpdateRectFunction updateRectFunc, TEnumMapsFunction enumMapFunction);

    /// @name Called from DownloadManager
    //@{
    void OnMapDownloadFinished(HttpFinishedParams const & params);
    void OnMapDownloadProgress(HttpProgressT const & progress);
    void OnDataUpdateCheckFinished(HttpFinishedParams const & params);
    void OnBinaryUpdateCheckFinished(HttpFinishedParams const & params);
    //@}

    /// @name Current impl supports only one observer
    //@{
    void Subscribe(TObserverChangeCountryFunction change,
                   TObserverProgressFunction progress,
                   TUpdateRequestFunction dataCheck);
    void Unsubscribe();
    //@}

    size_t CountriesCount(TIndex const & index) const;
    string CountryName(TIndex const & index) const;
    TLocalAndRemoteSize CountrySizeInBytes(TIndex const & index) const;
    TStatus CountryStatus(TIndex const & index) const;
    m2::RectD CountryBounds(TIndex const & index) const;

    void DownloadCountry(TIndex const & index);
    void DeleteCountry(TIndex const & index);

    void CheckForUpdate();
  };
}