diff options
author | George Hazan <ghazan@miranda.im> | 2019-02-15 12:31:26 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2019-02-15 12:31:26 +0300 |
commit | d061852f027e57625d7bdfd445469d9a29b1eb07 (patch) | |
tree | 3911c297149ed0f097c3db91253c0f4120eb0d17 /plugins/CurrencyRates | |
parent | 0ad0b41c6c42e0a346a7755de39dd03d4f25142d (diff) |
CurrencyRates: code cleaning
Diffstat (limited to 'plugins/CurrencyRates')
13 files changed, 251 insertions, 295 deletions
diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp index dda1e9cc18..5a8c617f07 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp @@ -253,7 +253,7 @@ public: default: if (false == std::isspace(chr)) bValid = false; - else + else ++i; break; @@ -363,17 +363,17 @@ tstring format_rate(const ICurrencyRatesProvider *pProvider, MCONTACT hContact, sResult += chr; ++i; break; - + case '\\': ++i; if (i != rsFrmt.end()) { wchar_t t = *i; switch (t) { - case '%': sResult += L"%"; break; - case 't': sResult += L"\t"; break; - case 'n': sResult += L"\n"; break; - case '\\': sResult += L"\\"; break; - default: sResult += chr; sResult += t; break; + case '%': sResult += L"%"; break; + case 't': sResult += L"\t"; break; + case 'n': sResult += L"\n"; break; + case '\\': sResult += L"\\"; break; + default: sResult += chr; sResult += t; break; } ++i; } @@ -781,12 +781,12 @@ void CCurrencyRatesProviderBase::Run() anContacts = m_aContacts; } break; + case WAIT_OBJECT_0 + REFRESH_CONTACT: { DWORD dwTimeRest = ::GetTickCount() - dwBegin; - if (INFINITE != nTimeout && dwTimeRest < nTimeout) { + if (INFINITE != nTimeout && dwTimeRest < nTimeout) nTimeout -= dwTimeRest; - } { mir_cslock lck(m_cs); @@ -800,6 +800,7 @@ void CCurrencyRatesProviderBase::Run() } } break; + case WAIT_TIMEOUT: nTimeout = get_refresh_timeout_miliseconds(visitor); { @@ -830,7 +831,8 @@ void CCurrencyRatesProviderBase::OnEndRun() } CBoolGuard bg(m_bRefreshInProgress); - std::for_each(anContacts.begin(), anContacts.end(), boost::bind(&SetContactStatus, _1, ID_STATUS_OFFLINE)); + for (auto &it : anContacts) + SetContactStatus(it, ID_STATUS_OFFLINE); } void CCurrencyRatesProviderBase::Accept(CCurrencyRatesProviderVisitor &visitor)const @@ -845,10 +847,10 @@ void CCurrencyRatesProviderBase::RefreshSettings() void CCurrencyRatesProviderBase::RefreshAllContacts() { - {// for CCritSection - mir_cslock lck(m_cs); + { mir_cslock lck(m_cs); m_aRefreshingContacts.clear(); - std::for_each(std::begin(m_aContacts), std::end(m_aContacts), [&](MCONTACT hContact) { m_aRefreshingContacts.push_back(hContact); }); + for (auto &hContact : m_aContacts) + m_aRefreshingContacts.push_back(hContact); } ::SetEvent(m_hEventRefreshContact); @@ -856,8 +858,7 @@ void CCurrencyRatesProviderBase::RefreshAllContacts() void CCurrencyRatesProviderBase::RefreshContact(MCONTACT hContact) { - {// for CCritSection - mir_cslock lck(m_cs); + { mir_cslock lck(m_cs); m_aRefreshingContacts.push_back(hContact); } diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp index 2dc1ce8b69..6b4e845b73 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp @@ -3,186 +3,175 @@ #include <boost\property_tree\ptree.hpp> #include <boost\property_tree\json_parser.hpp> -namespace +tstring build_url(const tstring &rsURL, const tstring &from, const tstring &to) { - tstring build_url(const tstring& rsURL, const tstring& from, const tstring& to) - { - tostringstream o; - o << rsURL << L"?q=" << from << L"_" << to << "&compact=ultra"; - return o.str(); - } - - tstring build_url(MCONTACT hContact, const tstring& rsURL) - { - tstring sFrom = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_FROM_ID); - tstring sTo = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_TO_ID); - return build_url(rsURL, sFrom, sTo); - } + tostringstream o; + o << rsURL << L"?q=" << from << L"_" << to << "&compact=ultra&apiKey=" << API_KEY; + return o.str(); +} - bool parse_responce(const tstring& rsJSON, double& dRate) - { - try - { - boost::property_tree::ptree pt; - std::istringstream i_stream(currencyrates_t2a(rsJSON.c_str())); - - boost::property_tree::read_json(i_stream, pt); - if (!pt.empty()) - { - auto pt_nested = pt.begin()->second; - dRate = pt_nested.get_value<double>(); - } - else - { - dRate = pt.get_value<double>(); - } +tstring build_url(MCONTACT hContact, const tstring &rsURL) +{ + tstring sFrom = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_FROM_ID); + tstring sTo = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_TO_ID); + return build_url(rsURL, sFrom, sTo); +} - return true; +bool parse_responce(const tstring &rsJSON, double &dRate) +{ + try { + boost::property_tree::ptree pt; + std::istringstream i_stream(currencyrates_t2a(rsJSON.c_str())); + + boost::property_tree::read_json(i_stream, pt); + if (!pt.empty()) { + auto pt_nested = pt.begin()->second; + dRate = pt_nested.get_value<double>(); } - catch (boost::property_tree::ptree_error& ) - { - } - return false; + else { + dRate = pt.get_value<double>(); + } + + return true; } + catch (boost::property_tree::ptree_error&) { + } + return false; +} - using TWatchedRates = std::vector<CCurrencyRatesProviderCurrencyConverter::TRateInfo>; - TWatchedRates g_aWatchedRates; +using TWatchedRates = std::vector<CCurrencyRatesProviderCurrencyConverter::TRateInfo>; +TWatchedRates g_aWatchedRates; - INT_PTR CALLBACK OptDlgProc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam) +INT_PTR CALLBACK OptDlgProc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam) +{ + auto get_provider = []()->CCurrencyRatesProviderCurrencyConverter* { - auto get_provider = []()->CCurrencyRatesProviderCurrencyConverter* - { - auto pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); - const auto& rapCurrencyRatesProviders = pProviders->GetProviders(); - for (auto i = rapCurrencyRatesProviders.begin(); i != rapCurrencyRatesProviders.end(); ++i) { - const auto& pProvider = *i; - if (auto p = dynamic_cast<CCurrencyRatesProviderCurrencyConverter*>(pProvider.get())) - { - return p; - } + auto pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); + const auto& rapCurrencyRatesProviders = pProviders->GetProviders(); + for (auto i = rapCurrencyRatesProviders.begin(); i != rapCurrencyRatesProviders.end(); ++i) { + const auto& pProvider = *i; + if (auto p = dynamic_cast<CCurrencyRatesProviderCurrencyConverter*>(pProvider.get())) { + return p; } + } - assert(!"We should never get here!"); - return nullptr; - }; + assert(!"We should never get here!"); + return nullptr; + }; - auto make_currencyrate_name = [](const CCurrencyRatesProviderBase::CCurrencyRate& rCurrencyRate)->tstring - { - const tstring& rsDesc = rCurrencyRate.GetName(); - return((false == rsDesc.empty()) ? rsDesc : rCurrencyRate.GetSymbol()); - }; + auto make_currencyrate_name = [](const CCurrencyRatesProviderBase::CCurrencyRate& rCurrencyRate)->tstring + { + const tstring& rsDesc = rCurrencyRate.GetName(); + return((false == rsDesc.empty()) ? rsDesc : rCurrencyRate.GetSymbol()); + }; - auto make_contact_name = [](const tstring& rsSymbolFrom, const tstring& rsSymbolTo)->tstring - { - tostringstream o; - o << rsSymbolFrom << L"/" << rsSymbolTo; - return o.str(); - }; + auto make_contact_name = [](const tstring& rsSymbolFrom, const tstring& rsSymbolTo)->tstring + { + tostringstream o; + o << rsSymbolFrom << L"/" << rsSymbolTo; + return o.str(); + }; - auto make_rate_name = [make_contact_name](const CCurrencyRatesProviderCurrencyConverter::TRateInfo& ri)->tstring - { - if ((false == ri.first.GetName().empty()) && (false == ri.second.GetName().empty())) - return make_contact_name(ri.first.GetName(), ri.second.GetName()); + auto make_rate_name = [make_contact_name](const CCurrencyRatesProviderCurrencyConverter::TRateInfo& ri)->tstring + { + if ((false == ri.first.GetName().empty()) && (false == ri.second.GetName().empty())) + return make_contact_name(ri.first.GetName(), ri.second.GetName()); - return make_contact_name(ri.first.GetSymbol(), ri.second.GetSymbol()); - }; + return make_contact_name(ri.first.GetSymbol(), ri.second.GetSymbol()); + }; - auto pProvider = get_provider(); + auto pProvider = get_provider(); - CCommonDlgProcData d(pProvider); - CommonOptionDlgProc(hdlg, message, wParam, lParam, d); + CCommonDlgProcData d(pProvider); + CommonOptionDlgProc(hdlg, message, wParam, lParam, d); - switch (message) { - case WM_NOTIFY: + switch (message) { + case WM_NOTIFY: { LPNMHDR pNMHDR = reinterpret_cast<LPNMHDR>(lParam); switch (pNMHDR->code) { case PSN_APPLY: - { - if (pProvider) { - TWatchedRates aTemp(g_aWatchedRates); - TWatchedRates aRemove; - size_t cWatchedRates = pProvider->GetWatchedRateCount(); - for (size_t i = 0; i < cWatchedRates; ++i) { - CCurrencyRatesProviderCurrencyConverter::TRateInfo ri; - if (true == pProvider->GetWatchedRateInfo(i, 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())) - && ((0 == mir_wstrcmpi(ri.second.GetID().c_str(), other.second.GetID().c_str())))); - }); - if (it == aTemp.end()) { - aRemove.push_back(ri); - } - else { - aTemp.erase(it); + { + if (pProvider) { + TWatchedRates aTemp(g_aWatchedRates); + TWatchedRates aRemove; + size_t cWatchedRates = pProvider->GetWatchedRateCount(); + for (size_t i = 0; i < cWatchedRates; ++i) { + CCurrencyRatesProviderCurrencyConverter::TRateInfo ri; + if (true == pProvider->GetWatchedRateInfo(i, 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())) + && ((0 == mir_wstrcmpi(ri.second.GetID().c_str(), other.second.GetID().c_str())))); + }); + if (it == aTemp.end()) { + aRemove.push_back(ri); + } + else { + aTemp.erase(it); + } } } - } - std::for_each(aRemove.begin(), aRemove.end(), [pProvider](const auto& ri) {pProvider->WatchForRate(ri, false); }); - std::for_each(aTemp.begin(), aTemp.end(), [pProvider](const auto& ri) {pProvider->WatchForRate(ri, true); }); - pProvider->RefreshSettings(); + for (auto &it : aRemove) pProvider->WatchForRate(it, false); + for (auto &it : aTemp) pProvider->WatchForRate(it, true); + pProvider->RefreshSettings(); + } } - } - break; + break; } } break; - case WM_INITDIALOG: - TranslateDialogDefault(hdlg); - { - g_aWatchedRates.clear(); + case WM_INITDIALOG: + TranslateDialogDefault(hdlg); + { + g_aWatchedRates.clear(); - HWND hcbxFrom = ::GetDlgItem(hdlg, IDC_COMBO_CONVERT_FROM); - HWND hcbxTo = ::GetDlgItem(hdlg, IDC_COMBO_CONVERT_INTO); + HWND hcbxFrom = ::GetDlgItem(hdlg, IDC_COMBO_CONVERT_FROM); + HWND hcbxTo = ::GetDlgItem(hdlg, IDC_COMBO_CONVERT_INTO); - CCurrencyRatesProviderBase::CCurrencyRateSection rSection; - const auto& rCurrencyRates = pProvider->GetCurrencyRates(); - if (rCurrencyRates.GetSectionCount() > 0) - { - rSection = rCurrencyRates.GetSection(0); - } + CCurrencyRatesProviderBase::CCurrencyRateSection rSection; + const auto& rCurrencyRates = pProvider->GetCurrencyRates(); + if (rCurrencyRates.GetSectionCount() > 0) { + rSection = rCurrencyRates.GetSection(0); + } - auto cCurrencyRates = rSection.GetCurrencyRateCount(); - for (auto i = 0u; i < cCurrencyRates; ++i) - { - const auto& rCurrencyRate = rSection.GetCurrencyRate(i); - tstring sName = make_currencyrate_name(rCurrencyRate); - LPCTSTR pszName = sName.c_str(); - ::SendMessage(hcbxFrom, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pszName)); - ::SendMessage(hcbxTo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pszName)); - } + auto cCurrencyRates = rSection.GetCurrencyRateCount(); + for (auto i = 0u; i < cCurrencyRates; ++i) { + const auto& rCurrencyRate = rSection.GetCurrencyRate(i); + tstring sName = make_currencyrate_name(rCurrencyRate); + LPCTSTR pszName = sName.c_str(); + ::SendMessage(hcbxFrom, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pszName)); + ::SendMessage(hcbxTo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pszName)); + } - auto cWatchedRates = pProvider->GetWatchedRateCount(); - for (auto i = 0u; i < cWatchedRates; ++i) - { - CCurrencyRatesProviderCurrencyConverter::TRateInfo ri; - if (true == pProvider->GetWatchedRateInfo(i, ri)) - { - g_aWatchedRates.push_back(ri); - tstring sRate = make_rate_name(ri); - LPCTSTR pszRateName = sRate.c_str(); - ::SendDlgItemMessage(hdlg, IDC_LIST_RATES, LB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pszRateName)); - } + auto cWatchedRates = pProvider->GetWatchedRateCount(); + for (auto i = 0u; i < cWatchedRates; ++i) { + CCurrencyRatesProviderCurrencyConverter::TRateInfo ri; + if (true == pProvider->GetWatchedRateInfo(i, ri)) { + g_aWatchedRates.push_back(ri); + tstring sRate = make_rate_name(ri); + LPCTSTR pszRateName = sRate.c_str(); + ::SendDlgItemMessage(hdlg, IDC_LIST_RATES, LB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pszRateName)); } - - ::EnableWindow(::GetDlgItem(hdlg, IDC_BUTTON_ADD), FALSE); - ::EnableWindow(::GetDlgItem(hdlg, IDC_BUTTON_REMOVE), FALSE); } - return TRUE; - - case WM_COMMAND: - switch (HIWORD(wParam)) { - case CBN_SELCHANGE: - switch (LOWORD(wParam)) { - case IDC_COMBO_REFRESH_RATE: - break; - case IDC_COMBO_CONVERT_FROM: - case IDC_COMBO_CONVERT_INTO: + + ::EnableWindow(::GetDlgItem(hdlg, IDC_BUTTON_ADD), FALSE); + ::EnableWindow(::GetDlgItem(hdlg, IDC_BUTTON_REMOVE), FALSE); + } + return TRUE; + + case WM_COMMAND: + switch (HIWORD(wParam)) { + case CBN_SELCHANGE: + switch (LOWORD(wParam)) { + case IDC_COMBO_REFRESH_RATE: + break; + case IDC_COMBO_CONVERT_FROM: + case IDC_COMBO_CONVERT_INTO: { int nFrom = static_cast<int>(::SendDlgItemMessage(hdlg, IDC_COMBO_CONVERT_FROM, CB_GETCURSEL, 0, 0)); int nTo = static_cast<int>(::SendDlgItemMessage(hdlg, IDC_COMBO_CONVERT_INTO, CB_GETCURSEL, 0, 0)); @@ -190,33 +179,30 @@ namespace EnableWindow(GetDlgItem(hdlg, IDC_BUTTON_ADD), bEnableAddButton); } break; - case IDC_LIST_RATES: + case IDC_LIST_RATES: { int nSel = ::SendDlgItemMessage(hdlg, IDC_LIST_RATES, LB_GETCURSEL, 0, 0); ::EnableWindow(::GetDlgItem(hdlg, IDC_BUTTON_REMOVE), (LB_ERR != nSel)); } break; - } - break; + } + break; - case BN_CLICKED: - switch (LOWORD(wParam)) { - case IDC_BUTTON_ADD: + case BN_CLICKED: + switch (LOWORD(wParam)) { + case IDC_BUTTON_ADD: { 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)) - { + if ((CB_ERR != nFrom) && (CB_ERR != nTo) && (nFrom != nTo)) { CCurrencyRatesProviderBase::CCurrencyRateSection rSection; const auto& rCurrencyRates = pProvider->GetCurrencyRates(); - if (rCurrencyRates.GetSectionCount() > 0) - { + if (rCurrencyRates.GetSectionCount() > 0) { rSection = rCurrencyRates.GetSection(0); } auto cCurrencyRates = rSection.GetCurrencyRateCount(); - if ((nFrom < cCurrencyRates) && (nTo < cCurrencyRates)) - { + if ((nFrom < cCurrencyRates) && (nTo < cCurrencyRates)) { CCurrencyRatesProviderCurrencyConverter::TRateInfo ri; ri.first = rSection.GetCurrencyRate(nFrom); ri.second = rSection.GetCurrencyRate(nTo); @@ -232,40 +218,36 @@ namespace } break; - case IDC_BUTTON_REMOVE: - HWND hWnd = ::GetDlgItem(hdlg, IDC_LIST_RATES); - int nSel = ::SendMessage(hWnd, LB_GETCURSEL, 0, 0); - if (LB_ERR != nSel) { - if ((LB_ERR != ::SendMessage(hWnd, LB_DELETESTRING, nSel, 0)) - && (nSel < static_cast<int>(g_aWatchedRates.size()))) { - - TWatchedRates::iterator i = g_aWatchedRates.begin(); - std::advance(i, nSel); - g_aWatchedRates.erase(i); - PropSheet_Changed(::GetParent(hdlg), hdlg); - } + case IDC_BUTTON_REMOVE: + HWND hWnd = ::GetDlgItem(hdlg, IDC_LIST_RATES); + int nSel = ::SendMessage(hWnd, LB_GETCURSEL, 0, 0); + if (LB_ERR != nSel) { + if ((LB_ERR != ::SendMessage(hWnd, LB_DELETESTRING, nSel, 0)) + && (nSel < static_cast<int>(g_aWatchedRates.size()))) { + + TWatchedRates::iterator i = g_aWatchedRates.begin(); + std::advance(i, nSel); + g_aWatchedRates.erase(i); + PropSheet_Changed(::GetParent(hdlg), hdlg); } - - nSel = ::SendMessage(hWnd, LB_GETCURSEL, 0, 0); - ::EnableWindow(::GetDlgItem(hdlg, IDC_BUTTON_REMOVE), (LB_ERR != nSel)); - break; } + + nSel = ::SendMessage(hWnd, LB_GETCURSEL, 0, 0); + ::EnableWindow(::GetDlgItem(hdlg, IDC_BUTTON_REMOVE), (LB_ERR != nSel)); break; } break; } - - return FALSE; + break; } + return FALSE; } - CCurrencyRatesProviderCurrencyConverter::CCurrencyRatesProviderCurrencyConverter() { } - CCurrencyRatesProviderCurrencyConverter::~CCurrencyRatesProviderCurrencyConverter() { } @@ -318,9 +300,7 @@ double CCurrencyRatesProviderCurrencyConverter::Convert(double dAmount, const CC if ((true == http.ReadResponce(sHTML))) { double dResult = 0.0; if ((true == parse_responce(sHTML, dResult))) - { - return dResult*dAmount; - } + return dResult * dAmount; throw std::runtime_error(Translate("Error occurred during HTML parsing.")); } @@ -338,23 +318,21 @@ size_t CCurrencyRatesProviderCurrencyConverter::GetWatchedRateCount()const bool CCurrencyRatesProviderCurrencyConverter::GetWatchedRateInfo(size_t nIndex, TRateInfo& rRateInfo) { - if(nIndex < m_aContacts.size()) { - MCONTACT hContact = m_aContacts[nIndex]; - tstring sSymbolFrom = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_FROM_ID); - tstring sSymbolTo = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_TO_ID); - tstring sDescFrom = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_FROM_DESCRIPTION); - tstring sDescTo = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_TO_DESCRIPTION); - - rRateInfo.first = CCurrencyRate(sSymbolFrom, sSymbolFrom, sDescFrom); - rRateInfo.second = CCurrencyRate(sSymbolTo, sSymbolTo, sDescTo); - return true; - } - else { + if (nIndex >= m_aContacts.size()) return false; - } + + MCONTACT hContact = m_aContacts[nIndex]; + tstring sSymbolFrom = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_FROM_ID); + tstring sSymbolTo = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_TO_ID); + tstring sDescFrom = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_FROM_DESCRIPTION); + tstring sDescTo = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, 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) +bool CCurrencyRatesProviderCurrencyConverter::WatchForRate(const TRateInfo &ri, bool bWatch) { auto i = std::find_if(m_aContacts.begin(), m_aContacts.end(), [&ri](auto hContact)->bool { @@ -372,28 +350,23 @@ bool CCurrencyRatesProviderCurrencyConverter::WatchForRate(const TRateInfo& ri, }; - if ((true == bWatch) && (i == m_aContacts.end())) - { + if ((true == bWatch) && (i == m_aContacts.end())) { tstring sName = make_contact_name(ri.first.GetSymbol(), ri.second.GetSymbol()); MCONTACT hContact = CreateNewContact(sName); - if (hContact) - { + if (hContact) { db_set_ws(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_FROM_ID, ri.first.GetID().c_str()); db_set_ws(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_TO_ID, ri.second.GetID().c_str()); - if (false == ri.first.GetName().empty()) - { + if (false == ri.first.GetName().empty()) { db_set_ws(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_FROM_DESCRIPTION, ri.first.GetName().c_str()); } - if (false == ri.second.GetName().empty()) - { + if (false == ri.second.GetName().empty()) { db_set_ws(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_TO_DESCRIPTION, ri.second.GetName().c_str()); } return true; } } - else if ((false == bWatch) && (i != m_aContacts.end())) - { + else if ((false == bWatch) && (i != m_aContacts.end())) { MCONTACT hContact = *i; {// for CCritSection mir_cslock lck(m_cs); @@ -416,8 +389,8 @@ MCONTACT CCurrencyRatesProviderCurrencyConverter::GetContactByID(const tstring& tstring sFrom = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_FROM_ID); tstring sTo = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_TO_ID); return ((0 == mir_wstrcmpi(rsFromID.c_str(), sFrom.c_str())) && (0 == mir_wstrcmpi(rsToID.c_str(), sTo.c_str()))); - }); + if (i != m_aContacts.end()) return *i; diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorDbSettings.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorDbSettings.cpp index 72d16ccb17..5b28a6b56e 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorDbSettings.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorDbSettings.cpp @@ -1,7 +1,7 @@ #include "StdAfx.h" -CCurrencyRatesProviderVisitorDbSettings::CCurrencyRatesProviderVisitorDbSettings() - : m_pszDbRefreshRateType(nullptr), +CCurrencyRatesProviderVisitorDbSettings::CCurrencyRatesProviderVisitorDbSettings() : + m_pszDbRefreshRateType(nullptr), m_pszDbRefreshRateValue(nullptr), m_pszDbDisplayNameFormat(nullptr), m_pszDefDisplayFormat(nullptr), diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorFormatSpecificator.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorFormatSpecificator.cpp index 05fceba8ac..056a558702 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorFormatSpecificator.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorFormatSpecificator.cpp @@ -59,4 +59,4 @@ void CCurrencyRatesProviderVisitorFormatSpecificator::Visit(const CCurrencyRates m_aSpecificators.push_back(CFormatSpecificator(L"%I", TranslateT("Into Currency Full Name"))); m_aSpecificators.push_back(CFormatSpecificator(L"%i", TranslateT("Into Currency Short Name"))); m_aSpecificators.push_back(CFormatSpecificator(L"%s", TranslateT("Short notation for \"%f/%i\""))); -}
\ No newline at end of file +} diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorFormater.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorFormater.cpp index 8f299d762f..34bf3afab9 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorFormater.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorFormater.cpp @@ -1,8 +1,8 @@ #include "StdAfx.h" #include "CurrencyRatesProviderCurrencyConverter.h" -CCurrencyRatesProviderVisitorFormater::CCurrencyRatesProviderVisitorFormater(MCONTACT hContact, wchar_t chr, int nWidth) - : m_hContact(hContact), +CCurrencyRatesProviderVisitorFormater::CCurrencyRatesProviderVisitorFormater(MCONTACT hContact, wchar_t chr, int nWidth) : + m_hContact(hContact), m_chr(chr), m_nWidth(nWidth) { @@ -119,4 +119,4 @@ void CCurrencyRatesProviderVisitorFormater::Visit(const CCurrencyRatesProviderCu m_sResult = CurrencyRates_DBGetStringW(m_hContact, CURRENCYRATES_MODULE_NAME, DB_STR_TO_ID); break; } -}
\ No newline at end of file +} diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorTendency.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorTendency.cpp index b9ed9a9d94..2d5260aab5 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorTendency.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorTendency.cpp @@ -1,7 +1,7 @@ #include "stdafx.h" -CCurrencyRatesProviderVisitorTendency::CCurrencyRatesProviderVisitorTendency(MCONTACT hContact, wchar_t chr) - : m_hContact(hContact), m_chr(chr), m_bValid(false), m_dResult(0.0) +CCurrencyRatesProviderVisitorTendency::CCurrencyRatesProviderVisitorTendency(MCONTACT hContact, wchar_t chr) : + m_hContact(hContact), m_chr(chr) { } diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorTendency.h b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorTendency.h index d0497889ae..e1bf320423 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorTendency.h +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderVisitorTendency.h @@ -18,8 +18,8 @@ private: private: MCONTACT m_hContact; wchar_t m_chr; - bool m_bValid; - double m_dResult; + bool m_bValid = false; + double m_dResult = 0.0; }; #endif //__AD721194_E9944366_9CF1_0307460EF32F_CurrencyRatesProviderVisitorTendency_h__ diff --git a/plugins/CurrencyRates/src/DBUtils.cpp b/plugins/CurrencyRates/src/DBUtils.cpp index 571bad0bfb..fa3df0c528 100644 --- a/plugins/CurrencyRates/src/DBUtils.cpp +++ b/plugins/CurrencyRates/src/DBUtils.cpp @@ -9,7 +9,7 @@ std::wstring GetNodeText(const TiXmlElement *pNode) return std::wstring(); } -std::wstring CurrencyRates_DBGetStringW(MCONTACT hContact, const char* szModule, const char* szSetting, const wchar_t* pszDefValue) +std::wstring CurrencyRates_DBGetStringW(MCONTACT hContact, const char *szModule, const char *szSetting, const wchar_t *pszDefValue) { if (pszDefValue == nullptr) pszDefValue = L""; @@ -17,14 +17,14 @@ std::wstring CurrencyRates_DBGetStringW(MCONTACT hContact, const char* szModule, return std::wstring(ptrW(db_get_wsa(hContact, szModule, szSetting, pszDefValue))); } -bool CurrencyRates_DBWriteDouble(MCONTACT hContact, const char* szModule, const char* szSetting, double dValue) +bool CurrencyRates_DBWriteDouble(MCONTACT hContact, const char *szModule, const char *szSetting, double dValue) { return 0 == db_set_blob(hContact, szModule, szSetting, &dValue, sizeof(dValue)); } -bool CurrencyRates_DBReadDouble(MCONTACT hContact, const char* szModule, const char* szSetting, double& rdValue) +bool CurrencyRates_DBReadDouble(MCONTACT hContact, const char *szModule, const char *szSetting, double& rdValue) { - DBVARIANT dbv = { 0 }; + DBVARIANT dbv = {}; dbv.type = DBVT_BLOB; bool bResult = ((0 == db_get(hContact, szModule, szSetting, &dbv)) && (DBVT_BLOB == dbv.type)); diff --git a/plugins/CurrencyRates/src/DBUtils.h b/plugins/CurrencyRates/src/DBUtils.h index 6fcb445936..a38a3725b1 100644 --- a/plugins/CurrencyRates/src/DBUtils.h +++ b/plugins/CurrencyRates/src/DBUtils.h @@ -3,9 +3,9 @@ std::wstring GetNodeText(const TiXmlElement*); -std::wstring CurrencyRates_DBGetStringW(MCONTACT hContact, const char* szModule, const char* szSetting, const wchar_t* pszDefValue = nullptr); +std::wstring CurrencyRates_DBGetStringW(MCONTACT hContact, const char *szModule, const char *szSetting, const wchar_t* pszDefValue = nullptr); -bool CurrencyRates_DBWriteDouble(MCONTACT hContact, const char* szModule, const char* szSetting, double dValue); -bool CurrencyRates_DBReadDouble(MCONTACT hContact, const char* szModule, const char* szSetting, double& rdValue); +bool CurrencyRates_DBWriteDouble(MCONTACT hContact, const char *szModule, const char *szSetting, double dValue); +bool CurrencyRates_DBReadDouble(MCONTACT hContact, const char *szModule, const char *szSetting, double& rdValue); #endif //__54294385_3fdd_4f0c_98c3_c583a96e7fb4_DBUtils_h__ diff --git a/plugins/CurrencyRates/src/Forex.cpp b/plugins/CurrencyRates/src/Forex.cpp index 7d05e4994a..b7d22ffcee 100644 --- a/plugins/CurrencyRates/src/Forex.cpp +++ b/plugins/CurrencyRates/src/Forex.cpp @@ -38,7 +38,8 @@ void UpdateMenu(bool bAutoUpdate) INT_PTR CurrencyRatesMenu_RefreshAll(WPARAM, LPARAM) { const CCurrencyRatesProviders::TCurrencyRatesProviders& apProviders = CModuleInfo::GetCurrencyRateProvidersPtr()->GetProviders(); - std::for_each(apProviders.begin(), apProviders.end(), boost::bind(&ICurrencyRatesProvider::RefreshAllContacts, _1)); + for (auto &it : apProviders) + it->RefreshAllContacts(); return 0; } @@ -48,14 +49,13 @@ INT_PTR CurrencyRatesMenu_EnableDisable(WPARAM, LPARAM) db_set_b(0, CURRENCYRATES_MODULE_NAME, DB_STR_AUTO_UPDATE, g_bAutoUpdate); const CModuleInfo::TCurrencyRatesProvidersPtr& pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); - const CCurrencyRatesProviders::TCurrencyRatesProviders& rapProviders = pProviders->GetProviders(); - std::for_each(std::begin(rapProviders), std::end(rapProviders), [](const CCurrencyRatesProviders::TCurrencyRatesProviderPtr& pProvider) { + for (auto &pProvider : pProviders->GetProviders()) { pProvider->RefreshSettings(); if (g_bAutoUpdate) pProvider->RefreshAllContacts(); - }); - UpdateMenu(g_bAutoUpdate); + } + UpdateMenu(g_bAutoUpdate); return 0; } @@ -126,25 +126,25 @@ void InitMenu() Menu_ConfigureItem(g_hMenuOpenLogFile, MCI_OPT_EXECPARAM, 1); CreateServiceFunction(mi.pszService, CurrencyRatesMenu_OpenLogFile); -#ifdef CHART_IMPLEMENT + #ifdef CHART_IMPLEMENT SET_UID(mi, 0x65da7256, 0x43a2, 0x4857, 0xac, 0x52, 0x1c, 0xb7, 0xff, 0xd7, 0x96, 0xfa); mi.name.w = LPGENW("Chart..."); mi.hIcolibItem = nullptr; mi.pszService = "CurrencyRates/Chart"; g_hMenuChart = Menu_AddContactMenuItem(&mi, CURRENCYRATES_PROTOCOL_NAME); CreateServiceFunction(mi.pszService, CurrencyRatesMenu_Chart); -#endif + #endif SET_UID(mi, 0xac5fc17, 0x5640, 0x4f81, 0xa3, 0x44, 0x8c, 0xb6, 0x9a, 0x5c, 0x98, 0xf); mi.name.w = LPGENW("Edit Settings..."); mi.hIcolibItem = nullptr; mi.pszService = "CurrencyRates/EditSettings"; g_hMenuEditSettings = Menu_AddContactMenuItem(&mi, CURRENCYRATES_PROTOCOL_NAME); -#ifdef CHART_IMPLEMENT + #ifdef CHART_IMPLEMENT Menu_ConfigureItem(g_hMenuEditSettings, MCI_OPT_EXECPARAM, 3); -#else + #else Menu_ConfigureItem(g_hMenuEditSettings, MCI_OPT_EXECPARAM, 2); -#endif + #endif CreateServiceFunction(mi.pszService, CurrencyRatesMenu_EditSettings); } @@ -254,16 +254,14 @@ int CurrencyRatesEventFunc_PreShutdown(WPARAM, LPARAM) int CurrencyRatesEventFunc_OptInitialise(WPARAM wp, LPARAM/* lp*/) { - const CModuleInfo::TCurrencyRatesProvidersPtr& pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); - const CCurrencyRatesProviders::TCurrencyRatesProviders& rapProviders = pProviders->GetProviders(); - OPTIONSDIALOGPAGE odp = {}; odp.position = 910000000; odp.szTitle.w = LPGENW("Currency Rates"); odp.szGroup.w = LPGENW("Network"); odp.flags = ODPF_USERINFOTAB | ODPF_UNICODE; - std::for_each(rapProviders.begin(), rapProviders.end(), boost::bind(&ICurrencyRatesProvider::ShowPropertyPage, _1, wp, boost::ref(odp))); + for (auto &it : CModuleInfo::GetCurrencyRateProvidersPtr()->GetProviders()) + it->ShowPropertyPage(wp, odp); return 0; } diff --git a/plugins/CurrencyRates/src/SettingsDlg.cpp b/plugins/CurrencyRates/src/SettingsDlg.cpp index 14c7dfb1fb..7ac0a52361 100644 --- a/plugins/CurrencyRates/src/SettingsDlg.cpp +++ b/plugins/CurrencyRates/src/SettingsDlg.cpp @@ -2,8 +2,8 @@ #define WINDOW_PREFIX_SETTINGS "Edit Settings_" -LPCTSTR g_pszVariableCurrencyRateName = L"%currencyratename%"; -LPCTSTR g_pszVariableUserProfile = L"%miranda_userdata%"; +const wchar_t g_pszVariableCurrencyRateName[] = L"%currencyratename%"; +const wchar_t g_pszVariableUserProfile[] = L"%miranda_userdata%"; void update_file_controls(HWND hDlg) { @@ -955,18 +955,6 @@ bool ShowSettingsDlg(HWND hWndParent, CAdvProviderSettings* pAdvSettings) reinterpret_cast<LPARAM>(pAdvSettings))); } -static void replace_invalid_char(tstring::value_type& rChar, tstring::value_type repl) -{ - static const wchar_t charInvalidSigns[] = { '\\', '/', ':', '*', '?', '\"', '<', '>', '|' }; - - for (int i = 0; i < sizeof(charInvalidSigns) / sizeof(charInvalidSigns[0]); ++i) { - if (rChar == charInvalidSigns[i]) { - rChar = repl; - break; - } - } -} - tstring GenerateLogFileName(const tstring &rsLogFilePattern, const tstring &rsCurrencyRateSymbol, int nFlags) { tstring sPath = rsLogFilePattern; @@ -976,8 +964,10 @@ tstring GenerateLogFileName(const tstring &rsLogFilePattern, const tstring &rsCu tstring::size_type n = sPath.find(g_pszVariableCurrencyRateName); if (tstring::npos != n) { tstring s = rsCurrencyRateSymbol; - std::for_each(s.begin(), s.end(), boost::bind(replace_invalid_char, _1, '_')); - sPath.replace(n, mir_wstrlen(g_pszVariableCurrencyRateName), s.c_str()); + for (auto &c : s) + if (wcschr(L"\\/:*?\"<>|", c)) + c = '_'; + sPath.replace(n, _countof(g_pszVariableCurrencyRateName)-1, s.c_str()); } } diff --git a/plugins/CurrencyRates/src/WinCtrlHelper.cpp b/plugins/CurrencyRates/src/WinCtrlHelper.cpp index 636b7ae9b1..1b2daf3cb7 100644 --- a/plugins/CurrencyRates/src/WinCtrlHelper.cpp +++ b/plugins/CurrencyRates/src/WinCtrlHelper.cpp @@ -1,40 +1,31 @@ #include "stdafx.h" -static INT_PTR CALLBACK VariableListDlgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) +class CVariableListDlg : public CDlgBase { - switch (msg) { - case WM_INITDIALOG: - TranslateDialogDefault(hWnd); - { - const ICurrencyRatesProvider* pProvider = reinterpret_cast<const ICurrencyRatesProvider*>(lp); - CCurrencyRatesProviderVisitorFormatSpecificator visitor; - pProvider->Accept(visitor); + const ICurrencyRatesProvider *m_pProvider; - tostringstream o; - const CCurrencyRatesProviderVisitorFormatSpecificator::TFormatSpecificators& raSpec = visitor.GetSpecificators(); - std::for_each(raSpec.begin(), raSpec.end(), - [&o](const CCurrencyRatesProviderVisitorFormatSpecificator::CFormatSpecificator& spec) - { - o << spec.m_sSymbol << '\t' << spec.m_sDesc << L"\r\n"; - }); - ::SetDlgItemText(hWnd, IDC_EDIT_VARIABLE, o.str().c_str()); - } - break; - - case WM_COMMAND: - if (BN_CLICKED == HIWORD(wp) && (IDOK == LOWORD(wp) || IDCANCEL == LOWORD(wp))) - ::EndDialog(hWnd, IDOK); - break; +public: + CVariableListDlg(HWND hwndParent, const ICurrencyRatesProvider *pProvider) : + CDlgBase(g_plugin, IDD_DIALOG_VARIABLE_LIST), + m_pProvider(pProvider) + { + SetParent(hwndParent); } - return FALSE; -} + bool OnInitDialog() override + { + CCurrencyRatesProviderVisitorFormatSpecificator visitor; + m_pProvider->Accept(visitor); + + tostringstream o; + for (auto &spec : visitor.GetSpecificators()) + o << spec.m_sSymbol << '\t' << spec.m_sDesc << L"\r\n"; + ::SetDlgItemText(m_hwnd, IDC_EDIT_VARIABLE, o.str().c_str()); + return true; + } +}; void show_variable_list(HWND hwndParent, const ICurrencyRatesProvider* pProvider) { - ::DialogBoxParam(g_plugin.getInst(), - MAKEINTRESOURCE(IDD_DIALOG_VARIABLE_LIST), - hwndParent, - VariableListDlgProc, - reinterpret_cast<LPARAM>(pProvider)); + CVariableListDlg(hwndParent, pProvider).DoModal(); } diff --git a/plugins/CurrencyRates/src/stdafx.h b/plugins/CurrencyRates/src/stdafx.h index 62c7306491..362e0d63e8 100644 --- a/plugins/CurrencyRates/src/stdafx.h +++ b/plugins/CurrencyRates/src/stdafx.h @@ -31,6 +31,7 @@ #include <m_netlib.h> #include <m_popup.h> #include <m_userinfo.h> +#include <m_gui.h> #include <m_variables.h> #include <m_CurrencyRates.h> @@ -42,6 +43,8 @@ #include <boost\date_time\posix_time\posix_time.hpp> #include <boost\date_time\c_local_time_adjustor.hpp> +#include "../../../miranda-private-keys/CurrencyRates/secret_key.h" + typedef std::wstring tstring; typedef std::wostringstream tostringstream; typedef std::wistringstream tistringstream; |