From dea6c2e2dea0324f800de4e6605de403d097b4a9 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Mon, 1 Feb 2016 18:09:26 +0300 Subject: [PATCH] [new downloader] Interface of new map downloader. --- storage/index.cpp | 7 +++ storage/index.hpp | 14 ++++- storage/storage.hpp | 117 ++++++++++++++++++++++++++++++++++++ storage/storage_defines.cpp | 36 ++++++----- storage/storage_defines.hpp | 18 +++--- 5 files changed, 166 insertions(+), 26 deletions(-) diff --git a/storage/index.cpp b/storage/index.cpp index ab10cf0a19..271da1d8a9 100644 --- a/storage/index.cpp +++ b/storage/index.cpp @@ -14,4 +14,11 @@ namespace storage out << "storage::TIndex(" << r.m_group << ", " << r.m_country << ", " << r.m_region << ")"; return out.str(); } + + storage::TCountryId const kInvalidCountryId; + + bool IsCountryIdValid(TCountryId const & countryId) + { + return countryId != kInvalidCountryId; + } } diff --git a/storage/index.hpp b/storage/index.hpp index 2e1bc17577..0d8bf0cb66 100644 --- a/storage/index.hpp +++ b/storage/index.hpp @@ -1,9 +1,19 @@ #pragma once +#include "std/set.hpp" #include "std/string.hpp" - +#include "std/vector.hpp" namespace storage { + using TCountryId = string; + using TCountriesSet = set; + using TCountriesVec = vector; + + extern const storage::TCountryId kInvalidCountryId; + + // @TODO(bykoianko) Check in counrtry tree if the countryId valid. + bool IsCountryIdValid(TCountryId const & countryId); + struct TIndex { static int const INVALID; @@ -40,4 +50,4 @@ namespace storage }; string DebugPrint(TIndex const & r); -} +} // namespace storage diff --git a/storage/storage.hpp b/storage/storage.hpp index 2ccc3e111f..06e95092a5 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -18,10 +18,59 @@ namespace storage { +/// \brief Contains all properties for a node in the country tree. +/// It's applicable for expandable and not expandable node id. +struct NodeAttrs +{ + NodeAttrs() : m_mwmCounter(0), m_localMwmCounter(0), m_mwmSize(0), m_localMwmSize(0), + m_downloadingMwmSize(0), m_downloadingProgress(0), m_status(TStatus::EUndefined) {} + /// If the node is expandable (a big country) |m_mwmCounter| is number of mwm files (leaves) + /// belongs to the node. If the node isn't expandable |m_mapsDownloaded| == 1. + uint32_t m_mwmCounter; + + /// Number of mwms belonging to the node which has been donwloaded. + uint32_t m_localMwmCounter; + + /// If it's not an expandable node, |m_nodeSize| is size of one mwm according to countries.txt. + /// Otherwise |m_nodeSize| is the sum of all mwm file sizes which belong to the group + /// according to countries.txt. + size_t m_mwmSize; + + /// If it's not an expandable node, |m_localMwmSize| is size of one downloaded mwm. + /// Otherwise |m_localNodeSize| is the sum of all mwm file sizes which belong to the group and + /// have been downloaded. + size_t m_localMwmSize; + + /// If downloading or updating an mwm is in progress apart from a local mwm + /// which is currently used there's a partly downloading mwm of the same region. + /// |m_downloadingMwmSize| is size of partly downloaded mwm if downloading is in progress. + /// And |m_downloadingMwmSize| == 0 otherwise. + size_t m_downloadingMwmSize; + + /// The name of the node in a local language. That means the language dependent on + /// a device locale. + string m_nodeLocalName; + + /// The name of the parent in a local language. That means the language dependent on + /// a device locale. For countries and for the root m_parentLocalName == "". + string m_parentLocalName; + + /// Node id of the parent of the node. For the root m_parentLocalName == "". + TCountryId m_parentCountryId; + + /// A number for 0 to 99. It reflects downloading progress in case of + /// downloading and updating mwm. If downloading or updating is not in progress + /// |m_downloadingProgress| == 0. + uint8_t m_downloadingProgress; + + TStatus m_status; +}; + /// Can be used to store local maps and/or maps available for download class Storage { public: + struct StatusCallback; using TUpdate = function; private: @@ -109,12 +158,80 @@ public: void Init(TUpdate const & update); + /// \brief Information for "Update all mwms" button. + struct UpdateInfo + { + size_t m_numberOfMwmFilesToUpdate; + size_t m_totalUpdateSizeInBytes; + }; + /// Switch on new storage version, remove old mwm /// and add required mwm's into download queue. void Migrate(); bool HaveDownloadedCountries(); void DeleteAllLocalMaps(vector * existedCountries = nullptr); + // New downloader interface. + /// \brief Returns root country id of the county tree. + TCountryId const GetRootId() const; + /// \param childrenId is filled with children node ids by a parent. For example GetChildren(GetRootId()) + /// returns in param all countries ids. It's content of map downloader list by default. + void GetChildren(TCountryId const & parent, TCountriesVec & childrenId) const; + /// \brief Fills localChildren with children of parent. + /// The result of the method is composed in a special way because of design requirements. + /// If a direct child (of parent) contains two or more downloaded mwms the direct child id will be added to result. + /// If a direct child (of parent) contains one downloaded mwm the mwm id will be added to result. + /// If there's no downloaded mwms contained by a direct child the direct child id will not be added to result. + /// \param parent is a parent acoording to countries.txt. + /// \note. This method puts to localChildren only real maps which have been written in coutries.txt. It means + /// the method does not put to localChildren neither custom maps generated by user + /// nor World.mwm and WorldCoasts.mwm. + void GetDownloadedChildren(TCountryId const & parent, TCountriesVec & localChildren) const; + /// \brief Gets list of available countries. + /// \param countryList is filled with a list of node id which an end user will see in + /// a list of available maps. They are all available countries expect for fully downloaded + /// countries. That means all mwm of the countries have been downloaded. + void GetCountyListToDownload(TCountriesVec & countryList) const; + + /// \brief Returns true if the node with countryId has been downloaded and false othewise. + /// If countryId is a expandable returns true if all mwms which belongs to it have downloaded. + /// Returns false if countryId is an unknown string. + /// \note The method return false for custom maps generated by user + /// and World.mwm and WorldCoasts.mwm. + bool IsNodeDownloaded(TCountryId const & countryId) const; + + /// \brief Gets all the attributes for a node by its countryId. + /// \param |nodeAttrs| is filled with attributes in this method. + void GetNodeAttrs(TCountryId const & countryId, NodeAttrs & nodeAttrs) const; + + /// \brief Downloads one node (expandable or not) by countryId. + /// If node is expandable downloads all children (grandchildren) by the node + /// until they havn't been downloaded before. Update all downloaded mwm if it's necessary. + bool DownloadNode(TCountryId const & countryId); + /// \brief Delete one node (expandable or not). + bool DeleteNode(TCountryId const & countryId); + /// \brief Updates one node (expandable or not). + /// \note If you want to update all the maps and this update is without changing + /// borders or hierarchy just call UpdateNode(GetRootId()). + /// \return false in case of error and true otherwise. + bool UpdateNode(TCountryId const & countryId); + /// \brief Cancels downloading a node if the downloading is in process. + /// \return false in case of error and true otherwise. + bool CancelDownloadNode(TCountryId const & countryId) { return true; } + /// \brief Downloading process could be interupted because of bad internet connection. + /// In that case user could want to recover it. This method is done for it. + /// This method works with expandable and not expandable countryId. + /// \return false in case of error and true otherwise. + bool RetryDownloadNode(TCountryId const & countryId) { return true; } + + /// \brief Shows a node (expandable or not) on the map. + /// \return false in case of error and true otherwise. + bool ShowNode(TCountryId const & countryId); + + /// \brief Get information for mwm update button. + /// \return true if updateInfo is filled correctly and false otherwise. + bool GetUpdateInfo(TCountryId const & countryId, UpdateInfo & updateInfo) const { return true; } + // Clears local files registry and downloader's queue. void Clear(); diff --git a/storage/storage_defines.cpp b/storage/storage_defines.cpp index cb50d6c432..8f22f67e20 100644 --- a/storage/storage_defines.cpp +++ b/storage/storage_defines.cpp @@ -6,22 +6,26 @@ string DebugPrint(TStatus status) { switch (status) { - case TStatus::EOnDisk: - return "OnDisk"; - case TStatus::ENotDownloaded: - return "NotDownloaded"; - case TStatus::EDownloadFailed: - return "DownloadFailed"; - case TStatus::EDownloading: - return "Downloading"; - case TStatus::EInQueue: - return "InQueue"; - case TStatus::EUnknown: - return "Unknown"; - case TStatus::EOnDiskOutOfDate: - return "OnDiskOutOfDate"; - case TStatus::EOutOfMemFailed: - return "OutOfMemFailed"; + case TStatus::EUndefined: + return string("EUndefined"); + case TStatus::EOnDisk: + return string("OnDisk"); + case TStatus::ENotDownloaded: + return string("NotDownloaded"); + case TStatus::EDownloadFailed: + return string("DownloadFailed"); + case TStatus::EDownloading: + return string("Downloading"); + case TStatus::EInQueue: + return string("InQueue"); + case TStatus::EUnknown: + return string("Unknown"); + case TStatus::EOnDiskOutOfDate: + return string("OnDiskOutOfDate"); + case TStatus::EOutOfMemFailed: + return string("OutOfMemFailed"); + case TStatus::EMixed: + return string("EMixed"); } } } // namespace storage diff --git a/storage/storage_defines.hpp b/storage/storage_defines.hpp index 901ecc10d0..bf2555c4ae 100644 --- a/storage/storage_defines.hpp +++ b/storage/storage_defines.hpp @@ -9,14 +9,16 @@ namespace storage /// Used in GUI enum class TStatus : uint8_t { - EOnDisk = 0, - ENotDownloaded, - EDownloadFailed, - EDownloading, - EInQueue, - EUnknown, - EOnDiskOutOfDate, - EOutOfMemFailed // EDownloadFailed because not enough memory + EUndefined = 0, + EOnDisk, /**< Downloaded mwm(s) is up to date. No need to update it. */ + ENotDownloaded, /**< Mwm can be download but not downloaded yet. */ + EDownloadFailed, /**< Downloading failed because no internet connection. */ + EDownloading, /**< Downloading a new mwm or updating an old one. */ + EInQueue, /**< A mwm is waiting for downloading in the queue. */ + EUnknown, /**< Downloading failed because of unknown error. */ + EOnDiskOutOfDate, /**< An update for a downloaded mwm is ready according to counties.txt. */ + EOutOfMemFailed, /**< Downloading failed because it's not enough memory */ + EMixed, /**< Descendants of a group node have different statuses. */ }; string DebugPrint(TStatus status);