diff options
Diffstat (limited to 'protocols/CurrencyRates/src/CurrencyRatesProviderCbrf.cpp')
-rw-r--r-- | protocols/CurrencyRates/src/CurrencyRatesProviderCbrf.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/protocols/CurrencyRates/src/CurrencyRatesProviderCbrf.cpp b/protocols/CurrencyRates/src/CurrencyRatesProviderCbrf.cpp new file mode 100644 index 0000000000..ed7a94bb3a --- /dev/null +++ b/protocols/CurrencyRates/src/CurrencyRatesProviderCbrf.cpp @@ -0,0 +1,77 @@ +#include "stdafx.h" + +///////////////////////////////////////////////////////////////////////////////////////// +// CCurrencyRatesProviderExchangeRates implementation + +class CCurrencyRatesProviderCbrf : public CCurrencyRatesProviderBase +{ + std::map<CMStringA, double> m_lastRates; + + double GetRate(const CMStringA &id1, const CMStringA &id2) const + { + if (!id1.IsEmpty() && !id2.IsEmpty()) { + auto r1 = m_lastRates.find(id1), r2 = m_lastRates.find(id2); + if (r1 != m_lastRates.end() && r2 != m_lastRates.end()) + return r2->second / r1->second; + } + + return 0.0; + } + +public: + CCurrencyRatesProviderCbrf() + {} + + void RefreshCurrencyRates(TContacts &anContacts) override + { + CHTTPSession http; + if (true == http.OpenURL(GetURL())) { + CMStringW sHTML; + if (true == http.ReadResponce(sHTML)) { + JSONNode root = JSONNode::parse(_T2A(sHTML)); + if (!root) + return; + + auto rates = root["rates"]; + if (!rates) + return; + + auto &qs = GetSection(); + + m_lastRates.clear(); + for (auto &it : qs.GetCurrencyRates()) { + CMStringA id = it.GetID(); + m_lastRates[id] = (id == "RUB") ? 1.0 : rates[id].as_float(); + } + } + } + + for (auto &hContact : anContacts) { + double rate = GetRate(g_plugin.getMStringA(hContact, DB_STR_FROM_ID), g_plugin.getMStringA(hContact, DB_STR_TO_ID)); + if (rate != 0.0) { + WriteContactRate(hContact, rate); + continue; + } + SetContactStatus(hContact, ID_STATUS_NA); + } + } + + double Convert(double dAmount, const CCurrencyRate &from, const CCurrencyRate &to) const override + { + CMStringA id1 = from.GetID(), id2 = to.GetID(); + return dAmount * GetRate(id1, id2); + } + + wchar_t* GetXmlFilename() const override + { + return L"cbrf.xml"; + } +}; + +///////////////////////////////////////////////////////////////////////////////////////// +// Module entry point + +void InitCbrf() +{ + g_apProviders.push(new CCurrencyRatesProviderCbrf()); +} |