diff options
author | George Hazan <george.hazan@gmail.com> | 2023-11-17 14:55:45 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2023-11-17 14:55:45 +0300 |
commit | 0353de737783a61cc1381d26dcdeda3111b61cb8 (patch) | |
tree | 44244b70514e95860c15d5e2642b0c67457925c5 | |
parent | f163ebca6e34e418978e30a83989d001186eff53 (diff) |
final version of CurrencyRates before adding second provider
-rw-r--r-- | protocols/CurrencyRates/Forex.vcxproj | 1 | ||||
-rw-r--r-- | protocols/CurrencyRates/Forex.vcxproj.filters | 3 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/CurrencyConverter.cpp | 19 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/CurrencyRatesProviderBase.cpp | 131 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/CurrencyRatesProviderBase.h | 18 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp | 191 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.h | 23 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/CurrencyRatesProviders.cpp | 13 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/Forex.cpp | 9 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/ICurrencyRatesProvider.h | 2 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/ImportExport.cpp | 5 | ||||
-rw-r--r-- | protocols/CurrencyRates/src/Options.cpp | 28 |
12 files changed, 180 insertions, 263 deletions
diff --git a/protocols/CurrencyRates/Forex.vcxproj b/protocols/CurrencyRates/Forex.vcxproj index 99c68f8138..e2a771d55e 100644 --- a/protocols/CurrencyRates/Forex.vcxproj +++ b/protocols/CurrencyRates/Forex.vcxproj @@ -51,7 +51,6 @@ <ClInclude Include="src\CurrencyRateChart.h" /> <ClInclude Include="src\CurrencyRateInfoDlg.h" /> <ClInclude Include="src\CurrencyRatesProviderBase.h" /> - <ClInclude Include="src\CurrencyRatesProviderCurrencyConverter.h" /> <ClInclude Include="src\DBUtils.h" /> <ClInclude Include="src\EconomicRateInfo.h" /> <ClInclude Include="src\ExtraImages.h" /> diff --git a/protocols/CurrencyRates/Forex.vcxproj.filters b/protocols/CurrencyRates/Forex.vcxproj.filters index aeda748bf4..247512c02b 100644 --- a/protocols/CurrencyRates/Forex.vcxproj.filters +++ b/protocols/CurrencyRates/Forex.vcxproj.filters @@ -73,9 +73,6 @@ <ClInclude Include="src\CurrencyRatesProviderBase.h"> <Filter>Header Files</Filter> </ClInclude> - <ClInclude Include="src\CurrencyRatesProviderCurrencyConverter.h"> - <Filter>Header Files</Filter> - </ClInclude> <ClInclude Include="src\DBUtils.h"> <Filter>Header Files</Filter> </ClInclude> diff --git a/protocols/CurrencyRates/src/CurrencyConverter.cpp b/protocols/CurrencyRates/src/CurrencyConverter.cpp index cb9f86e8b3..16b6cd6618 100644 --- a/protocols/CurrencyRates/src/CurrencyConverter.cpp +++ b/protocols/CurrencyRates/src/CurrencyConverter.cpp @@ -1,5 +1,4 @@ #include "StdAfx.h" -#include "CurrencyRatesProviderCurrencyConverter.h" #define WINDOW_PREFIX "CurrenyConverter_" @@ -7,23 +6,23 @@ #define DB_STR_CC_CURRENCYRATE_TO_ID "CurrencyConverter_ToID" #define DB_STR_CC_AMOUNT "CurrencyConverter_Amount" -static CCurrencyRatesProviderCurrencyConverter *get_currency_converter_provider() +static CCurrencyRatesProviderBase *get_currency_converter_provider() { for (auto &it : g_apProviders) - if (auto p = dynamic_cast<CCurrencyRatesProviderCurrencyConverter*>(it)) + if (auto p = dynamic_cast<CCurrencyRatesProviderBase *>(it)) return p; assert(!"We should never get here!"); return nullptr; } -CCurrencyRateSection get_currencyrates(const CCurrencyRatesProviderCurrencyConverter* pProvider = nullptr) +CCurrencyRateSection get_currencyrates(const CCurrencyRatesProviderBase *pProvider = nullptr) { if (nullptr == pProvider) pProvider = get_currency_converter_provider(); if (pProvider) { - const auto& rCurrencyRates = pProvider->GetCurrencyRates(); + const auto &rCurrencyRates = pProvider->GetCurrencyRates(); if (rCurrencyRates.GetSectionCount() > 0) return rCurrencyRates.GetSection(0); } @@ -77,10 +76,10 @@ INT_PTR CALLBACK CurrencyConverterDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM CMStringW sToCurrencyRateID = g_plugin.getMStringW(DB_STR_CC_CURRENCYRATE_TO_ID); const auto pProvider = get_currency_converter_provider(); - const auto& rSection = get_currencyrates(pProvider); + const auto &rSection = get_currencyrates(pProvider); auto cCurrencyRates = rSection.GetCurrencyRateCount(); for (auto i = 0u; i < cCurrencyRates; ++i) { - const auto& rCurrencyRate = rSection.GetCurrencyRate(i); + const auto &rCurrencyRate = rSection.GetCurrencyRate(i); CMStringW sName = make_currencyrate_name(rCurrencyRate); LRESULT nFrom = ::SendMessage(hcbxFrom, CB_ADDSTRING, 0, LPARAM(sName.c_str())); LRESULT nTo = ::SendMessage(hcbxTo, CB_ADDSTRING, 0, LPARAM(sName.c_str())); @@ -137,7 +136,7 @@ INT_PTR CALLBACK CurrencyConverterDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM update_swap_button(hDlg); } return TRUE; - + case IDC_EDIT_VALUE: if (EN_CHANGE == HIWORD(wp)) update_convert_button(hDlg); @@ -169,7 +168,7 @@ INT_PTR CALLBACK CurrencyConverterDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM size_t nFrom = static_cast<size_t>(::SendDlgItemMessage(hDlg, IDC_COMBO_CONVERT_FROM, CB_GETCURSEL, 0, 0)); size_t nTo = static_cast<size_t>(::SendDlgItemMessage(hDlg, IDC_COMBO_CONVERT_INTO, CB_GETCURSEL, 0, 0)); if ((CB_ERR != nFrom) && (CB_ERR != nTo) && (nFrom != nTo)) { - const auto& rSection = get_currencyrates(); + const auto &rSection = get_currencyrates(); size_t cCurrencyRates = rSection.GetCurrencyRateCount(); if ((nFrom < cCurrencyRates) && (nTo < cCurrencyRates)) { auto from = rSection.GetCurrencyRate(nFrom); @@ -187,7 +186,7 @@ INT_PTR CALLBACK CurrencyConverterDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM double dResult = pProvider->Convert(dAmount, from, to); sResult.Format(L"%.2lf %s = %.2lf %s", dAmount, from.GetName().c_str(), dResult, to.GetName().c_str()); } - catch (std::exception& e) { + catch (std::exception &e) { sResult = e.what(); } SetDlgItemText(hDlg, IDC_EDIT_RESULT, sResult); diff --git a/protocols/CurrencyRates/src/CurrencyRatesProviderBase.cpp b/protocols/CurrencyRates/src/CurrencyRatesProviderBase.cpp index 70eef9954e..adf19de68a 100644 --- a/protocols/CurrencyRates/src/CurrencyRatesProviderBase.cpp +++ b/protocols/CurrencyRates/src/CurrencyRatesProviderBase.cpp @@ -127,7 +127,7 @@ CCurrencyRatesProviderBase::~CCurrencyRatesProviderBase() { delete m_pXMLInfo; - for (auto &it : m_aContacts) + for (auto &it : Contacts(MODULENAME)) SetContactStatus(it, ID_STATUS_OFFLINE); ::CloseHandle(m_hEventSettingsChanged); @@ -158,28 +158,6 @@ const CMStringW &CCurrencyRatesProviderBase::GetURL() const return m_pXMLInfo->m_sURL; } -bool CCurrencyRatesProviderBase::IsOnline() -{ - return /*g_bAutoUpdate*/true; -} - -void CCurrencyRatesProviderBase::AddContact(MCONTACT hContact) -{ - // CCritSection cs(m_cs); - assert(m_aContacts.end() == std::find(m_aContacts.begin(), m_aContacts.end(), hContact)); - - m_aContacts.push_back(hContact); -} - -void CCurrencyRatesProviderBase::DeleteContact(MCONTACT hContact) -{ - mir_cslock lck(m_cs); - - TContacts::iterator i = std::find(m_aContacts.begin(), m_aContacts.end(), hContact); - if (i != m_aContacts.end()) - m_aContacts.erase(i); -} - void CCurrencyRatesProviderBase::SetContactStatus(MCONTACT hContact, int nNewStatus) { int nStatus = g_plugin.getWord(hContact, DB_STR_STATUS, ID_STATUS_OFFLINE); @@ -604,8 +582,6 @@ MCONTACT CCurrencyRatesProviderBase::CreateNewContact(const CMStringW &rsName) g_plugin.setWString(hContact, DB_STR_CURRENCYRATE_SYMBOL, rsName); db_set_ws(hContact, LIST_MODULE_NAME, CONTACT_LIST_NAME, rsName); - mir_cslock lck(m_cs); - m_aContacts.push_back(hContact); return hContact; } @@ -654,6 +630,15 @@ private: bool m_b; }; +///////////////////////////////////////////////////////////////////////////////////////// + +static void refreshContacts(TContacts &list) +{ + list.clear(); + for (auto &cc : Contacts(MODULENAME)) + list.push_back(cc); +} + void CCurrencyRatesProviderBase::Run() { uint32_t nTimeout = get_refresh_timeout_miliseconds(); @@ -675,10 +660,7 @@ void CCurrencyRatesProviderBase::Run() anEvents[REFRESH_CONTACT] = m_hEventRefreshContact; TContacts anContacts; - { - mir_cslock lck(m_cs); - anContacts = m_aContacts; - } + refreshContacts(anContacts); bool bGoToBed = false; @@ -713,10 +695,8 @@ void CCurrencyRatesProviderBase::Run() m_sContactListFormat = g_plugin.getMStringW(DB_KEY_DisplayNameFormat, DB_DEF_DisplayNameFormat); m_sStatusMsgFormat = g_plugin.getMStringW(DB_KEY_StatusMsgFormat, DB_DEF_StatusMsgFormat); m_sTendencyFormat = g_plugin.getMStringW(DB_KEY_TendencyFormat, DB_DEF_TendencyFormat); - { - mir_cslock lck(m_cs); - anContacts = m_aContacts; - } + + refreshContacts(anContacts); break; case WAIT_OBJECT_0 + REFRESH_CONTACT: @@ -738,10 +718,7 @@ void CCurrencyRatesProviderBase::Run() case WAIT_TIMEOUT: nTimeout = get_refresh_timeout_miliseconds(); - { - mir_cslock lck(m_cs); - anContacts = m_aContacts; - } + refreshContacts(anContacts); { CBoolGuard bg(m_bRefreshInProgress); RefreshCurrencyRates(anContacts); @@ -764,11 +741,8 @@ void CCurrencyRatesProviderBase::RefreshSettings() void CCurrencyRatesProviderBase::RefreshAllContacts() { - { - mir_cslock lck(m_cs); - m_aRefreshingContacts.clear(); - for (auto &hContact : m_aContacts) - m_aRefreshingContacts.push_back(hContact); + { mir_cslock lck(m_cs); + refreshContacts(m_aRefreshingContacts); } ::SetEvent(m_hEventRefreshContact); @@ -776,8 +750,7 @@ void CCurrencyRatesProviderBase::RefreshAllContacts() void CCurrencyRatesProviderBase::RefreshContact(MCONTACT hContact) { - { - mir_cslock lck(m_cs); + { mir_cslock lck(m_cs); m_aRefreshingContacts.push_back(hContact); } @@ -912,3 +885,73 @@ CMStringW CCurrencyRatesProviderBase::FormatSymbol(MCONTACT hContact, wchar_t c, return ret; } + +bool CCurrencyRatesProviderBase::GetWatchedRateInfo(MCONTACT hContact, TRateInfo &rRateInfo) +{ + CMStringW sSymbolFrom = g_plugin.getMStringW(hContact, DB_STR_FROM_ID); + CMStringW sSymbolTo = g_plugin.getMStringW(hContact, DB_STR_TO_ID); + CMStringW sDescFrom = g_plugin.getMStringW(hContact, DB_STR_FROM_DESCRIPTION); + CMStringW sDescTo = g_plugin.getMStringW(hContact, DB_STR_TO_DESCRIPTION); + + rRateInfo.first = CCurrencyRate(sSymbolFrom, sSymbolFrom, sDescFrom); + rRateInfo.second = CCurrencyRate(sSymbolTo, sSymbolTo, sDescTo); + return true; +} + +bool CCurrencyRatesProviderBase::WatchForRate(const TRateInfo &ri, bool bWatch) +{ + MCONTACT hContact = GetContactByID(ri.first.GetID(), ri.second.GetID()); + + if (bWatch && hContact == 0) { + CMStringW sName = ri.first.GetSymbol() + L"/" + ri.second.GetSymbol(); + hContact = CreateNewContact(sName); + if (hContact) { + g_plugin.setWString(hContact, DB_STR_FROM_ID, ri.first.GetID().c_str()); + g_plugin.setWString(hContact, DB_STR_TO_ID, ri.second.GetID().c_str()); + if (false == ri.first.GetName().IsEmpty()) { + g_plugin.setWString(hContact, DB_STR_FROM_DESCRIPTION, ri.first.GetName().c_str()); + } + if (false == ri.second.GetName().IsEmpty()) { + g_plugin.setWString(hContact, DB_STR_TO_DESCRIPTION, ri.second.GetName().c_str()); + } + + return true; + } + } + else if (!bWatch && hContact) { + db_delete_contact(hContact, true); + return true; + } + + return false; +} + +MCONTACT CCurrencyRatesProviderBase::GetContactByID(const CMStringW &rsFromID, const CMStringW &rsToID) const +{ + for (auto &hContact : Contacts(MODULENAME)) { + CMStringW sFrom = g_plugin.getMStringW(hContact, DB_STR_FROM_ID); + CMStringW sTo = g_plugin.getMStringW(hContact, DB_STR_TO_ID); + if (!mir_wstrcmpi(rsFromID.c_str(), sFrom.c_str()) && !mir_wstrcmpi(rsToID.c_str(), sTo.c_str())) + return hContact; + } + + return 0; +} + +MCONTACT CCurrencyRatesProviderBase::ImportContact(const TiXmlNode *pRoot) +{ + const char *sFromID = nullptr, *sToID = nullptr; + + for (auto *pNode : TiXmlFilter(pRoot, "Setting")) { + TNameValue Item = parse_setting_node(pNode); + if (!mir_strcmpi(Item.first, DB_STR_FROM_ID)) + sFromID = Item.second; + else if (!mir_strcmpi(Item.first, DB_STR_TO_ID)) + sToID = Item.second; + } + + if (sFromID && sToID) + return GetContactByID(Utf2T(sFromID).get(), Utf2T(sToID).get()); + + return 0; +} diff --git a/protocols/CurrencyRates/src/CurrencyRatesProviderBase.h b/protocols/CurrencyRates/src/CurrencyRatesProviderBase.h index 034efff5d6..d17795afed 100644 --- a/protocols/CurrencyRates/src/CurrencyRatesProviderBase.h +++ b/protocols/CurrencyRates/src/CurrencyRatesProviderBase.h @@ -88,16 +88,22 @@ public: bool Init() override; const CProviderInfo& GetInfo() const override; - - void AddContact(MCONTACT hContact) override; - void DeleteContact(MCONTACT hContact) override; - + void Run() override; void RefreshAllContacts() override; void RefreshSettings() override; void RefreshContact(MCONTACT hContact) override; - + + MCONTACT ImportContact(const TiXmlNode *) override; + + using TRateInfo = std::pair<CCurrencyRate, CCurrencyRate>; + bool GetWatchedRateInfo(MCONTACT hContact, TRateInfo &rRateInfo); + bool WatchForRate(const TRateInfo &ri, bool bWatch); + MCONTACT GetContactByID(const CMStringW &rsFromID, const CMStringW &rsToID) const; + + virtual double Convert(double dAmount, const CCurrencyRate &from, const CCurrencyRate &to) const = 0; + void FillFormat(TFormatSpecificators&) const override; bool ParseSymbol(MCONTACT hContact, wchar_t c, double &d) const override; CMStringW FormatSymbol(MCONTACT hContact, wchar_t c, int nWidth = 0) const override; @@ -105,7 +111,6 @@ public: protected: const CMStringW& GetURL() const; MCONTACT CreateNewContact(const CMStringW &rsName); - static bool IsOnline(); static void SetContactStatus(MCONTACT hContact, int nNewStatus); void WriteContactRate(MCONTACT hContact, double dRate, const CMStringW &rsSymbol = L""); @@ -113,7 +118,6 @@ protected: virtual void RefreshCurrencyRates(TContacts &anContacts) = 0; protected: - TContacts m_aContacts; mutable mir_cs m_cs; }; diff --git a/protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp b/protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp index 01c06810f7..a34d272052 100644 --- a/protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp +++ b/protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp @@ -1,7 +1,6 @@ #include "stdafx.h" -#include "CurrencyRatesProviderCurrencyConverter.h" -CMStringW build_url(const CMStringW &rsURL, const CMStringW &from, const CMStringW &to) +static CMStringW build_url(const CMStringW &rsURL, const CMStringW &from, const CMStringW &to) { CMStringW res = rsURL + L"?q=" + from + L"_" + to + L"&compact=ultra"; ptrA szApiKey(g_plugin.getStringA(DB_KEY_ApiKey)); @@ -10,14 +9,14 @@ CMStringW build_url(const CMStringW &rsURL, const CMStringW &from, const CMStrin return res; } -CMStringW build_url(MCONTACT hContact, const CMStringW &rsURL) +static CMStringW build_url(MCONTACT hContact, const CMStringW &rsURL) { CMStringW sFrom = g_plugin.getMStringW(hContact, DB_STR_FROM_ID); CMStringW sTo = g_plugin.getMStringW(hContact, DB_STR_TO_ID); return build_url(rsURL, sFrom, sTo); } -bool parse_response(const CMStringW &rsJSON, double &dRate) +static bool parse_response(const CMStringW &rsJSON, double &dRate) { JSONNode root = JSONNode::parse(_T2A(rsJSON)); if (!root) @@ -27,160 +26,74 @@ bool parse_response(const CMStringW &rsJSON, double &dRate) return true; } -CCurrencyRatesProviderCurrencyConverter::CCurrencyRatesProviderCurrencyConverter() -{ -} +///////////////////////////////////////////////////////////////////////////////////////// +// CCurrencyRatesProviderCurrencyConverter implementation -CCurrencyRatesProviderCurrencyConverter::~CCurrencyRatesProviderCurrencyConverter() +class CCurrencyRatesProviderCurrencyConverter : public CCurrencyRatesProviderBase { -} +public: + typedef CCurrencyRatesProviderBase CSuper; -wchar_t *CCurrencyRatesProviderCurrencyConverter::GetXmlFilename() const -{ - return L"CC.xml"; -} - -void CCurrencyRatesProviderCurrencyConverter::RefreshCurrencyRates(TContacts &anContacts) -{ - CHTTPSession http; - CMStringW sURL = GetURL(); +public: + CCurrencyRatesProviderCurrencyConverter() + {} - for (TContacts::const_iterator i = anContacts.begin(); i != anContacts.end() && IsOnline(); ++i) { - MCONTACT hContact = *i; + double Convert(double dAmount, const CCurrencyRate &from, const CCurrencyRate &to) const override + { + CMStringW sFullURL = build_url(GetURL(), from.GetID(), to.GetID()); - CMStringW sFullURL = build_url(hContact, sURL); - if ((true == http.OpenURL(sFullURL)) && (true == IsOnline())) { + CHTTPSession http; + if ((true == http.OpenURL(sFullURL))) { CMStringW sHTML; - if ((true == http.ReadResponce(sHTML)) && (true == IsOnline())) { - double dRate = 0.0; - if ((true == parse_response(sHTML, dRate)) && (true == IsOnline())) { - WriteContactRate(hContact, dRate); - continue; - } - } - } + if ((true == http.ReadResponce(sHTML))) { + double dResult = 0.0; + if ((true == parse_response(sHTML, dResult))) + return dResult * dAmount; - SetContactStatus(hContact, ID_STATUS_NA); - } -} - -double CCurrencyRatesProviderCurrencyConverter::Convert(double dAmount, const CCurrencyRate &from, const CCurrencyRate &to) const -{ - CMStringW sFullURL = build_url(GetURL(), from.GetID(), to.GetID()); - - CHTTPSession http; - if ((true == http.OpenURL(sFullURL))) { - CMStringW sHTML; - if ((true == http.ReadResponce(sHTML))) { - double dResult = 0.0; - if ((true == parse_response(sHTML, dResult))) - return dResult * dAmount; - - throw std::runtime_error(Translate("Error occurred during HTML parsing.")); + throw std::runtime_error(Translate("Error occurred during HTML parsing.")); + } + else throw std::runtime_error(Translate("Error occurred during site access.")); } else throw std::runtime_error(Translate("Error occurred during site access.")); - } - else throw std::runtime_error(Translate("Error occurred during site access.")); - return 0.0; -} - -size_t CCurrencyRatesProviderCurrencyConverter::GetWatchedRateCount() const -{ - return m_aContacts.size(); -} - -bool CCurrencyRatesProviderCurrencyConverter::GetWatchedRateInfo(size_t nIndex, TRateInfo &rRateInfo) -{ - if (nIndex >= m_aContacts.size()) - return false; - - MCONTACT hContact = m_aContacts[nIndex]; - CMStringW sSymbolFrom = g_plugin.getMStringW(hContact, DB_STR_FROM_ID); - CMStringW sSymbolTo = g_plugin.getMStringW(hContact, DB_STR_TO_ID); - CMStringW sDescFrom = g_plugin.getMStringW(hContact, DB_STR_FROM_DESCRIPTION); - CMStringW sDescTo = g_plugin.getMStringW(hContact, DB_STR_TO_DESCRIPTION); - - rRateInfo.first = CCurrencyRate(sSymbolFrom, sSymbolFrom, sDescFrom); - rRateInfo.second = CCurrencyRate(sSymbolTo, sSymbolTo, sDescTo); - return true; -} - -bool CCurrencyRatesProviderCurrencyConverter::WatchForRate(const TRateInfo &ri, bool bWatch) -{ - auto i = std::find_if(m_aContacts.begin(), m_aContacts.end(), [&ri](auto hContact)->bool - { - CMStringW sFrom = g_plugin.getMStringW(hContact, DB_STR_FROM_ID); - CMStringW sTo = g_plugin.getMStringW(hContact, DB_STR_TO_ID); - return !mir_wstrcmpi(ri.first.GetID().c_str(), sFrom.c_str()) && !mir_wstrcmpi(ri.second.GetID().c_str(), sTo.c_str()); - }); + return 0.0; + } - auto make_contact_name = [](const CMStringW &rsSymbolFrom, const CMStringW &rsSymbolTo)->CMStringW +private: + void RefreshCurrencyRates(TContacts &anContacts) override { - return rsSymbolFrom + L"/" + rsSymbolTo; - }; - - if ((true == bWatch) && (i == m_aContacts.end())) { - CMStringW sName = make_contact_name(ri.first.GetSymbol(), ri.second.GetSymbol()); - MCONTACT hContact = CreateNewContact(sName); - if (hContact) { - g_plugin.setWString(hContact, DB_STR_FROM_ID, ri.first.GetID().c_str()); - g_plugin.setWString(hContact, DB_STR_TO_ID, ri.second.GetID().c_str()); - if (false == ri.first.GetName().IsEmpty()) { - g_plugin.setWString(hContact, DB_STR_FROM_DESCRIPTION, ri.first.GetName().c_str()); - } - if (false == ri.second.GetName().IsEmpty()) { - g_plugin.setWString(hContact, DB_STR_TO_DESCRIPTION, ri.second.GetName().c_str()); + CHTTPSession http; + CMStringW sURL = GetURL(); + + for (TContacts::const_iterator i = anContacts.begin(); i != anContacts.end(); ++i) { + MCONTACT hContact = *i; + + CMStringW sFullURL = build_url(hContact, sURL); + if (true == http.OpenURL(sFullURL)) { + CMStringW sHTML; + if (true == http.ReadResponce(sHTML)) { + double dRate = 0.0; + if (true == parse_response(sHTML, dRate)) { + WriteContactRate(hContact, dRate); + continue; + } + } } - return true; - } - } - else if ((false == bWatch) && (i != m_aContacts.end())) { - MCONTACT hContact = *i; - {// for CCritSection - mir_cslock lck(m_cs); - m_aContacts.erase(i); + SetContactStatus(hContact, ID_STATUS_NA); } - - db_delete_contact(hContact, true); - return true; } - return false; -} - -MCONTACT CCurrencyRatesProviderCurrencyConverter::GetContactByID(const CMStringW &rsFromID, const CMStringW &rsToID) const -{ - mir_cslock lck(m_cs); - - auto i = std::find_if(m_aContacts.begin(), m_aContacts.end(), [rsFromID, rsToID](MCONTACT hContact)->bool + wchar_t *GetXmlFilename() const override { - CMStringW sFrom = g_plugin.getMStringW(hContact, DB_STR_FROM_ID); - CMStringW sTo = g_plugin.getMStringW(hContact, DB_STR_TO_ID); - return !mir_wstrcmpi(rsFromID.c_str(), sFrom.c_str()) && !mir_wstrcmpi(rsToID.c_str(), sTo.c_str()); - }); - - if (i != m_aContacts.end()) - return *i; - - return NULL; -} - -MCONTACT CCurrencyRatesProviderCurrencyConverter::ImportContact(const TiXmlNode *pRoot) -{ - const char *sFromID = nullptr, *sToID = nullptr; - - for (auto *pNode : TiXmlFilter(pRoot, "Setting")) { - TNameValue Item = parse_setting_node(pNode); - if (!mir_strcmpi(Item.first, DB_STR_FROM_ID)) - sFromID = Item.second; - else if (!mir_strcmpi(Item.first, DB_STR_TO_ID)) - sToID = Item.second; + return L"CC.xml"; } +}; - if (sFromID && sToID) - return GetContactByID(Utf2T(sFromID).get(), Utf2T(sToID).get()); +///////////////////////////////////////////////////////////////////////////////////////// +// Module entry point - return 0; +void InitCC() +{ + g_apProviders.push(new CCurrencyRatesProviderCurrencyConverter()); } diff --git a/protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.h b/protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.h index fdf22be32c..3f59c932d3 100644 --- a/protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.h +++ b/protocols/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.h @@ -1,25 +1,2 @@ #pragma once -class CCurrencyRatesProviderCurrencyConverter : public CCurrencyRatesProviderBase -{ -public: - typedef CCurrencyRatesProviderBase CSuper; - using TRateInfo = std::pair<CCurrencyRate, CCurrencyRate>; - -public: - CCurrencyRatesProviderCurrencyConverter(); - ~CCurrencyRatesProviderCurrencyConverter(); - - double Convert(double dAmount, const CCurrencyRate &from, const CCurrencyRate &to) const; - size_t GetWatchedRateCount() const; - bool GetWatchedRateInfo(size_t nIndex, TRateInfo &rRateInfo); - bool WatchForRate(const TRateInfo &ri, bool bWatch); - MCONTACT GetContactByID(const CMStringW &rsFromID, const CMStringW &rsToID) const; - -private: - void RefreshCurrencyRates(TContacts &anContacts) override; - - wchar_t* GetXmlFilename() const override; - - MCONTACT ImportContact(const TiXmlNode*) override; -}; diff --git a/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp b/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp index 1b744d01ec..14604d63f7 100644 --- a/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp +++ b/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp @@ -1,15 +1,17 @@ #include "StdAfx.h" -#include "CurrencyRatesProviderCurrencyConverter.h" #define LAST_RUN_VERSION "LastRunVersion" +void InitCC(); +void InitExchangeRates(); + TCurrencyRatesProviders g_apProviders; ///////////////////////////////////////////////////////////////////////////////////////// void CreateProviders() { - g_apProviders.push(new CCurrencyRatesProviderCurrencyConverter()); + InitCC(); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -37,11 +39,8 @@ void InitProviders() for (auto &hContact : Contacts(MODULENAME)) { ICurrencyRatesProvider *pProvider = GetContactProviderPtr(hContact); - if (pProvider) { - pProvider->AddContact(hContact); - if (nVersion < nCurrentVersion) - convert_contact_settings(hContact); - } + if (pProvider && nVersion < nCurrentVersion) + convert_contact_settings(hContact); } g_plugin.setWord(LAST_RUN_VERSION, nCurrentVersion); diff --git a/protocols/CurrencyRates/src/Forex.cpp b/protocols/CurrencyRates/src/Forex.cpp index 8006a6e448..64588d865e 100644 --- a/protocols/CurrencyRates/src/Forex.cpp +++ b/protocols/CurrencyRates/src/Forex.cpp @@ -204,14 +204,6 @@ int CurrencyRatesEventFunc_OnModulesLoaded(WPARAM, LPARAM) return 0; } -int CurrencyRatesEventFunc_OnContactDeleted(WPARAM hContact, LPARAM) -{ - auto pProvider = GetContactProviderPtr(hContact); - if (pProvider) - pProvider->DeleteContact(hContact); - return 0; -} - INT_PTR CurrencyRateProtoFunc_GetCaps(WPARAM wParam, LPARAM) { switch (wParam) { @@ -306,7 +298,6 @@ int CMPlugin::Load(void) CreateProtoServiceFunction(MODULENAME, PS_GETSTATUS, CurrencyRateProtoFunc_GetStatus); HookEvent(ME_SYSTEM_MODULESLOADED, CurrencyRatesEventFunc_OnModulesLoaded); - HookEvent(ME_DB_CONTACT_DELETED, CurrencyRatesEventFunc_OnContactDeleted); HookEvent(ME_SYSTEM_PRESHUTDOWN, CurrencyRatesEventFunc_PreShutdown); HookEvent(ME_OPT_INITIALISE, CurrencyRatesEventFunc_OptInitialise); diff --git a/protocols/CurrencyRates/src/ICurrencyRatesProvider.h b/protocols/CurrencyRates/src/ICurrencyRatesProvider.h index 3d8d07ca92..7ceec2aa8c 100644 --- a/protocols/CurrencyRates/src/ICurrencyRatesProvider.h +++ b/protocols/CurrencyRates/src/ICurrencyRatesProvider.h @@ -30,8 +30,6 @@ public: virtual bool Init() = 0; virtual const CProviderInfo& GetInfo() const = 0; - virtual void AddContact(MCONTACT hContact) = 0; - virtual void DeleteContact(MCONTACT hContact) = 0; virtual MCONTACT ImportContact(const TiXmlNode*) = 0; virtual void RefreshAllContacts() = 0; diff --git a/protocols/CurrencyRates/src/ImportExport.cpp b/protocols/CurrencyRates/src/ImportExport.cpp index dce9989367..8d6804713a 100644 --- a/protocols/CurrencyRates/src/ImportExport.cpp +++ b/protocols/CurrencyRates/src/ImportExport.cpp @@ -443,10 +443,9 @@ bool import_contact(const TiXmlNode *pXmlContact, CImportContext &impctx) if (!handle_module(cst.m_hContact, pNode)) return false; - if (cst.m_bNewContact) { - cst.m_pProvider->AddContact(cst.m_hContact); + if (cst.m_bNewContact) cst.m_pProvider->RefreshContact(cst.m_hContact); - } + return true; } diff --git a/protocols/CurrencyRates/src/Options.cpp b/protocols/CurrencyRates/src/Options.cpp index 50e770faed..ee4db5a1b9 100644 --- a/protocols/CurrencyRates/src/Options.cpp +++ b/protocols/CurrencyRates/src/Options.cpp @@ -16,7 +16,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "stdafx.h" -#include "CurrencyRatesProviderCurrencyConverter.h" typedef boost::shared_ptr<CAdvProviderSettings> TAdvSettingsPtr; typedef std::map<const ICurrencyRatesProvider *, TAdvSettingsPtr> TAdvSettings; @@ -47,10 +46,10 @@ void remove_adv_settings(const ICurrencyRatesProvider *m_pProvider) class COptionsDlg : public CDlgBase { - CCurrencyRatesProviderCurrencyConverter *get_provider() + CCurrencyRatesProviderBase* get_provider() { for (auto &it : g_apProviders) - if (auto p = dynamic_cast<CCurrencyRatesProviderCurrencyConverter *>(it)) + if (auto p = dynamic_cast<CCurrencyRatesProviderBase *>(it)) return p; assert(!"We should never get here!"); @@ -68,7 +67,7 @@ class COptionsDlg : public CDlgBase return rsSymbolFrom + L"/" + rsSymbolTo; }; - CMStringW make_rate_name(const CCurrencyRatesProviderCurrencyConverter::TRateInfo &ri) + CMStringW make_rate_name(const CCurrencyRatesProviderBase::TRateInfo &ri) { if ((false == ri.first.GetName().IsEmpty()) && (false == ri.second.GetName().IsEmpty())) return make_contact_name(ri.first.GetName(), ri.second.GetName()); @@ -76,10 +75,10 @@ class COptionsDlg : public CDlgBase return make_contact_name(ri.first.GetSymbol(), ri.second.GetSymbol()); }; - using TWatchedRates = std::vector<CCurrencyRatesProviderCurrencyConverter::TRateInfo>; + using TWatchedRates = std::vector<CCurrencyRatesProviderBase::TRateInfo>; TWatchedRates g_aWatchedRates; - CCurrencyRatesProviderCurrencyConverter *m_pProvider; + CCurrencyRatesProviderBase *m_pProvider; CCtrlCombo cmbFrom, cmbTo; CCtrlButton btnAdd, btnRemove, btnDescr, btnAdvanced; @@ -166,10 +165,9 @@ public: cmbTo.AddString(sName); } - auto cWatchedRates = m_pProvider->GetWatchedRateCount(); - for (auto i = 0u; i < cWatchedRates; ++i) { - CCurrencyRatesProviderCurrencyConverter::TRateInfo ri; - if (true == m_pProvider->GetWatchedRateInfo(i, ri)) { + for (auto &cc : Contacts(MODULENAME)) { + CCurrencyRatesProviderBase::TRateInfo ri; + if (true == m_pProvider->GetWatchedRateInfo(cc, ri)) { g_aWatchedRates.push_back(ri); CMStringW sRate = make_rate_name(ri); m_list.AddString(sRate); @@ -201,10 +199,10 @@ public: TWatchedRates aTemp(g_aWatchedRates); TWatchedRates aRemove; - size_t cWatchedRates = m_pProvider->GetWatchedRateCount(); - for (size_t i = 0; i < cWatchedRates; ++i) { - CCurrencyRatesProviderCurrencyConverter::TRateInfo ri; - if (true == m_pProvider->GetWatchedRateInfo(i, ri)) { + + for (auto &cc : Contacts(MODULENAME)) { + CCurrencyRatesProviderBase::TRateInfo ri; + if (true == m_pProvider->GetWatchedRateInfo(cc, ri)) { auto it = std::find_if(aTemp.begin(), aTemp.end(), [&ri](const auto &other)->bool { return ((0 == mir_wstrcmpi(ri.first.GetID().c_str(), other.first.GetID().c_str())) @@ -318,7 +316,7 @@ public: auto cCurrencyRates = rSection.GetCurrencyRateCount(); if ((nFrom < cCurrencyRates) && (nTo < cCurrencyRates)) { - CCurrencyRatesProviderCurrencyConverter::TRateInfo ri; + CCurrencyRatesProviderBase::TRateInfo ri; ri.first = rSection.GetCurrencyRate(nFrom); ri.second = rSection.GetCurrencyRate(nTo); |