From 3ceb853fc80922dc01e3308c9289f81b71257c8e Mon Sep 17 00:00:00 2001 From: Dmitry Kunin Date: Wed, 21 Aug 2013 20:21:30 +0300 Subject: [PATCH] [core] Guides management. --- defines.hpp | 1 + map/guides.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ map/guides.hpp | 54 +++++++++++++++++++++++++++++ map/map.pro | 9 +++-- 4 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 map/guides.cpp create mode 100644 map/guides.hpp diff --git a/defines.hpp b/defines.hpp index 8efc3fa219..37ddc33508 100644 --- a/defines.hpp +++ b/defines.hpp @@ -17,6 +17,7 @@ #define BOOKMARKS_FILE_EXTENSION ".kml" #define COUNTRIES_FILE "countries.txt" +#define GUIDES_DATA_FILE "guidesData.txt" #define WORLD_FILE_NAME "World" #define WORLD_COASTS_FILE_NAME "WorldCoasts" diff --git a/map/guides.cpp b/map/guides.cpp new file mode 100644 index 0000000000..a440bdb42f --- /dev/null +++ b/map/guides.cpp @@ -0,0 +1,93 @@ +#include "guides.hpp" + +#include "../../coding/file_writer.hpp" +#include "../../coding/file_reader.hpp" +#include "../../platform/platform.hpp" + +#include "../base/logging.hpp" +#include "../std/bind.hpp" + +#include "../../3party/jansson/myjansson.hpp" + +using namespace guides; + +void GuidesManager::RestoreFromFile() +{ + string data; + ReaderPtr r(GetPlatform().GetReader(GUIDES_DATA_FILE)); + r.ReadAsString(data); + + ValidateAndParseGuidesData(data); +} + +void GuidesManager::UpdateGuidesData() +{ + downloader::HttpRequest::CallbackT onFinish = bind(&GuidesManager::OnFinish, this, _1); + m_httpRequest.reset(downloader::HttpRequest::Get(GetGuidesDataUrl(), onFinish)); +} + +bool GuidesManager::GetGuideInfo(string const & countryId, GuideInfo & appInfo) +{ + map::iterator const it = m_countryToUrl.find(countryId); + + if (it != m_countryToUrl.end()) + { + appInfo = it->second; + return true; + } + return false; +} + +string GuidesManager::GetGuidesDataUrl() +{ + /// @todo add platform parametr + return "http://third.server/guides.json"; +} + +void GuidesManager::OnFinish(downloader::HttpRequest & request) +{ + if (request.Status() == downloader::HttpRequest::ECompleted) + { + string const & data = request.Data(); + if(ValidateAndParseGuidesData(data)) + SaveToFile(data); + else + LOG(LWARNING, ("Request data is invalid ", request.Data())); + } + else + LOG(LWARNING, ("Request is failed to complete", request.Status())); +} + +bool GuidesManager::ValidateAndParseGuidesData(string const & jsonData) +{ + try + { + my::Json root(jsonData.c_str()); + void * iter = json_object_iter(root.get()); + while (iter) + { + char const * key = json_object_iter_key(iter); + json_t * value = json_object_get(root.get(), key); + + GuideInfo info; + info.m_appId = json_string_value(json_object_get(value, "appId")); + info.m_appName = json_string_value(json_object_get(value, "name")); + info.m_appUrl = json_string_value(json_object_get(value, "url")); + m_countryToUrl[key] = info; + + iter = json_object_iter_next(root.get(), iter); + } + return true; + } + catch (my::Json::Exception const &) + { + return false; + } +} + +void GuidesManager::SaveToFile(string const & jsonData) +{ + string const path = GetPlatform().WritableDir() + GUIDES_DATA_FILE; + FileWriter writer(path); + writer.Write(jsonData.data(), jsonData.size()); +} diff --git a/map/guides.hpp b/map/guides.hpp new file mode 100644 index 0000000000..9f35214827 --- /dev/null +++ b/map/guides.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include "../std/string.hpp" +#include "../std/map.hpp" +#include "../std/scoped_ptr.hpp" + +#include "../platform/http_request.hpp" + + +namespace guides { + +struct GuideInfo +{ + GuideInfo() + : m_appName(""), m_appUrl(""), m_appId("") + {} + + GuideInfo(string const & appName, string const & appUrl, string const & appId) + : m_appName(appName), m_appUrl(appUrl), m_appId(appId) + {} + + string m_appName; + string m_appUrl; + string m_appId; + + bool operator == (GuideInfo const & other) const + { + return (m_appName == other.m_appName + && m_appId == other.m_appId + && m_appUrl == other.m_appUrl); + } +}; + +class GuidesManager +{ +/// @name Guides managment +//@{ +public: + void UpdateGuidesData(); + void RestoreFromFile(); + bool GetGuideInfo(string const & countryId, GuideInfo & appInfo); + bool ValidateAndParseGuidesData(string const & jsonData); + +private: + void SaveToFile(string const & jsonData); + void OnFinish(downloader::HttpRequest & request); + string GetGuidesDataUrl(); + + map m_countryToUrl; + scoped_ptr m_httpRequest; +//@} +}; + +} diff --git a/map/map.pro b/map/map.pro index 8816e2d440..d005fbed6b 100644 --- a/map/map.pro +++ b/map/map.pro @@ -3,12 +3,15 @@ TARGET = map TEMPLATE = lib CONFIG += staticlib warn_on -INCLUDEPATH += ../3party/protobuf/src ROOT_DIR = .. + +INCLUDEPATH *= $$ROOT_DIR/3party/protobuf/src # use expat from the system on linux -!linux*: INCLUDEPATH *= ../3party/expat/lib +!linux*: INCLUDEPATH *= $$ROOT_DIR/3party/expat/lib +INCLUDEPATH *= $$ROOT_DIR/3party/jansson/src + include($$ROOT_DIR/common.pri) @@ -60,6 +63,7 @@ HEADERS += \ ge0_parser.hpp \ balloon_manager.hpp \ scales_processor.hpp \ + guides.hpp \ SOURCES += \ feature_vec_model.cpp \ @@ -109,6 +113,7 @@ SOURCES += \ ../api/src/c/api-client.c \ balloon_manager.cpp \ scales_processor.cpp \ + guides.cpp \ !iphone*:!bada*:!android* { HEADERS += qgl_render_context.hpp