diff options
20 files changed, 160 insertions, 284 deletions
diff --git a/plugins/CurrencyRates/src/CommonOptionDlg.cpp b/plugins/CurrencyRates/src/CommonOptionDlg.cpp index d635a54438..335457ed89 100644 --- a/plugins/CurrencyRates/src/CommonOptionDlg.cpp +++ b/plugins/CurrencyRates/src/CommonOptionDlg.cpp @@ -5,7 +5,7 @@ typedef std::map<const ICurrencyRatesProvider*, TAdvSettingsPtr> TAdvSettings; TAdvSettings g_aAdvSettings; -CAdvProviderSettings* get_adv_settings(const ICurrencyRatesProvider* pProvider, bool bCreateIfNonExist) +CAdvProviderSettings* get_adv_settings(const ICurrencyRatesProvider *pProvider, bool bCreateIfNonExist) { TAdvSettings::iterator i = g_aAdvSettings.find(pProvider); if (i != g_aAdvSettings.end()) diff --git a/plugins/CurrencyRates/src/CurrencyConverter.cpp b/plugins/CurrencyRates/src/CurrencyConverter.cpp index 2dcb69ea8b..5a7f4d1487 100644 --- a/plugins/CurrencyRates/src/CurrencyConverter.cpp +++ b/plugins/CurrencyRates/src/CurrencyConverter.cpp @@ -9,9 +9,8 @@ static CCurrencyRatesProviderCurrencyConverter *get_currency_converter_provider() { - CModuleInfo::TCurrencyRatesProvidersPtr pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); - for (auto &it : pProviders->GetProviders()) - if (auto p = dynamic_cast<CCurrencyRatesProviderCurrencyConverter*>(it.get())) + for (auto &it : g_apProviders) + if (auto p = dynamic_cast<CCurrencyRatesProviderCurrencyConverter*>(it)) return p; assert(!"We should never get here!"); diff --git a/plugins/CurrencyRates/src/CurrencyRateInfoDlg.cpp b/plugins/CurrencyRates/src/CurrencyRateInfoDlg.cpp index 4b9efb3067..c05520686e 100644 --- a/plugins/CurrencyRates/src/CurrencyRateInfoDlg.cpp +++ b/plugins/CurrencyRates/src/CurrencyRateInfoDlg.cpp @@ -14,8 +14,7 @@ MCONTACT g_hContact; inline bool IsMyContact(MCONTACT hContact) { - CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact); - return (nullptr != pProvider); + return nullptr != GetContactProviderPtr(hContact); } inline MCONTACT get_contact(HWND hWnd) @@ -66,9 +65,7 @@ INT_PTR CALLBACK CurrencyRateInfoDlgProcImpl(MCONTACT hContact, HWND hdlg, UINT } } - CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact); - - const ICurrencyRatesProvider::CProviderInfo& pi = pProvider->GetInfo(); + const ICurrencyRatesProvider::CProviderInfo& pi = GetContactProviderPtr(hContact)->GetInfo(); tostringstream o; o << TranslateT("Info provided by") << L" <a href=\"" << pi.m_sURL << L"\">" << pi.m_sName << L"</a>"; @@ -152,11 +149,9 @@ INT_PTR CurrencyRatesMenu_RefreshContact(WPARAM wp, LPARAM) if (NULL == hContact) return 0; - CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact); - if (!pProvider) - return 0; - - pProvider->RefreshContact(hContact); + ICurrencyRatesProvider *pProvider = GetContactProviderPtr(hContact); + if (pProvider) + pProvider->RefreshContact(hContact); return 0; } @@ -210,7 +205,7 @@ static INT_PTR CALLBACK CurrencyRateInfoDlgProc1(HWND hdlg, UINT msg, WPARAM wPa int CurrencyRates_OnContactDoubleClick(WPARAM wp, LPARAM/* lp*/) { MCONTACT hContact = MCONTACT(wp); - if (CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact)) { + if (GetContactProviderPtr(hContact)) { MWindowList hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX_INFO, true); assert(hWL); HWND hWnd = WindowList_Find(hWL, hContact); diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp index 6193a85389..93f904b28a 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp @@ -122,8 +122,8 @@ CXMLFileInfo init_xml_info(LPCTSTR pszFileName, bool& rbSucceded) return parse_ini_file(sIniFile, rbSucceded); } -CCurrencyRatesProviderBase::CCurrencyRatesProviderBase() - : m_hEventSettingsChanged(::CreateEvent(nullptr, FALSE, FALSE, nullptr)), +CCurrencyRatesProviderBase::CCurrencyRatesProviderBase() : + m_hEventSettingsChanged(::CreateEvent(nullptr, FALSE, FALSE, nullptr)), m_hEventRefreshContact(::CreateEvent(nullptr, FALSE, FALSE, nullptr)), m_bRefreshInProgress(false) { @@ -131,37 +131,34 @@ CCurrencyRatesProviderBase::CCurrencyRatesProviderBase() CCurrencyRatesProviderBase::~CCurrencyRatesProviderBase() { + delete m_pXMLInfo; + ::CloseHandle(m_hEventSettingsChanged); ::CloseHandle(m_hEventRefreshContact); } bool CCurrencyRatesProviderBase::Init() { - bool bSucceded = m_pXMLInfo != nullptr; + bool bSucceded = (m_pXMLInfo == nullptr); if (!m_pXMLInfo) - m_pXMLInfo.reset(new CXMLFileInfo(init_xml_info(DB_DEF_IniFileName, bSucceded))); + m_pXMLInfo = new CXMLFileInfo(init_xml_info(DB_DEF_IniFileName, bSucceded)); return bSucceded; } -CXMLFileInfo* CCurrencyRatesProviderBase::GetXMLFileInfo() const -{ - return m_pXMLInfo.get(); -} - const CCurrencyRatesProviderBase::CProviderInfo& CCurrencyRatesProviderBase::GetInfo() const { - return GetXMLFileInfo()->m_pi; + return m_pXMLInfo->m_pi; } const CCurrencyRateSection& CCurrencyRatesProviderBase::GetCurrencyRates() const { - return GetXMLFileInfo()->m_qs; + return m_pXMLInfo->m_qs; } const tstring& CCurrencyRatesProviderBase::GetURL() const { - return GetXMLFileInfo()->m_sURL; + return m_pXMLInfo->m_sURL; } bool CCurrencyRatesProviderBase::IsOnline() @@ -391,7 +388,7 @@ tstring format_rate(const ICurrencyRatesProvider *pProvider, MCONTACT hContact, return sResult; } -void log_to_file(const ICurrencyRatesProvider* pProvider, +void log_to_file(const ICurrencyRatesProvider *pProvider, MCONTACT hContact, const tstring& rsLogFileName, const tstring& rsFormat) @@ -406,7 +403,7 @@ void log_to_file(const ICurrencyRatesProvider* pProvider, } } -void log_to_history(const ICurrencyRatesProvider* pProvider, +void log_to_history(const ICurrencyRatesProvider *pProvider, MCONTACT hContact, time_t nTime, const tstring& rsFormat) @@ -440,7 +437,7 @@ bool do_set_contact_extra_icon(MCONTACT hContact, const CTendency& tendency) return false; } -bool show_popup(const ICurrencyRatesProvider* pProvider, +bool show_popup(const ICurrencyRatesProvider *pProvider, MCONTACT hContact, const CTendency& tendency, const tstring& rsFormat, diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderBase.h b/plugins/CurrencyRates/src/CurrencyRatesProviderBase.h index 975d6fd0a3..d407a9a709 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderBase.h +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderBase.h @@ -73,10 +73,8 @@ class CCurrencyRatesProviderBase : public ICurrencyRatesProvider { void OnEndRun(); - struct CXMLFileInfo* GetXMLFileInfo() const; + struct CXMLFileInfo *m_pXMLInfo = nullptr; - typedef boost::scoped_ptr<CXMLFileInfo> TXMLFileInfoPtr; - mutable TXMLFileInfoPtr m_pXMLInfo; HANDLE m_hEventSettingsChanged; HANDLE m_hEventRefreshContact; tstring m_sContactListFormat; diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp index 252271467d..36693d52b1 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderCurrencyConverter.cpp @@ -47,15 +47,11 @@ TWatchedRates g_aWatchedRates; 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())) + for (auto &pProvider : g_apProviders) + if (auto p = dynamic_cast<CCurrencyRatesProviderCurrencyConverter*>(pProvider)) return p; - } assert(!"We should never get here!"); return nullptr; diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviders.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviders.cpp index cc327aa22d..31dd4c771a 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviders.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviders.cpp @@ -3,38 +3,31 @@ #define LAST_RUN_VERSION "LastRunVersion" -CCurrencyRatesProviders::CCurrencyRatesProviders() -{ - InitProviders(); -} - -CCurrencyRatesProviders::~CCurrencyRatesProviders() -{ - ClearProviders(); -} +TCurrencyRatesProviders g_apProviders; -const CCurrencyRatesProviders::TCurrencyRatesProviders& CCurrencyRatesProviders::GetProviders() const -{ - return m_apProviders; -} +///////////////////////////////////////////////////////////////////////////////////////// -template<class T>void create_provider(CCurrencyRatesProviders::TCurrencyRatesProviders& apProviders) +template<class T>void create_provider(TCurrencyRatesProviders& g_apProviders) { - CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider(new T); + ICurrencyRatesProvider *pProvider = new T; if (pProvider->Init()) - apProviders.push_back(pProvider); + g_apProviders.push_back(pProvider); }; -void CCurrencyRatesProviders::CreateProviders() +void CreateProviders() { - create_provider<CCurrencyRatesProviderCurrencyConverter>(m_apProviders); + create_provider<CCurrencyRatesProviderCurrencyConverter>(g_apProviders); } -void CCurrencyRatesProviders::ClearProviders() +///////////////////////////////////////////////////////////////////////////////////////// + +void ClearProviders() { - m_apProviders.clear(); + g_apProviders.clear(); } +///////////////////////////////////////////////////////////////////////////////////////// + void convert_contact_settings(MCONTACT hContact) { WORD dwLogMode = db_get_w(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CURRENCYRATE_LOG, static_cast<WORD>(lmDisabled)); @@ -42,7 +35,7 @@ void convert_contact_settings(MCONTACT hContact) db_set_b(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CONTACT_SPEC_SETTINGS, 1); } -void CCurrencyRatesProviders::InitProviders() +void InitProviders() { CreateProviders(); @@ -50,7 +43,7 @@ void CCurrencyRatesProviders::InitProviders() WORD nVersion = db_get_w(0, CURRENCYRATES_MODULE_NAME, LAST_RUN_VERSION, 1); for (auto &hContact : Contacts(CURRENCYRATES_MODULE_NAME)) { - TCurrencyRatesProviderPtr pProvider = GetContactProviderPtr(hContact); + ICurrencyRatesProvider *pProvider = GetContactProviderPtr(hContact); if (pProvider) { pProvider->AddContact(hContact); if (nVersion < nCurrentVersion) @@ -61,30 +54,30 @@ void CCurrencyRatesProviders::InitProviders() db_set_w(0, CURRENCYRATES_MODULE_NAME, LAST_RUN_VERSION, nCurrentVersion); } -CCurrencyRatesProviders::TCurrencyRatesProviderPtr CCurrencyRatesProviders::GetContactProviderPtr(MCONTACT hContact) const +///////////////////////////////////////////////////////////////////////////////////////// + +ICurrencyRatesProvider* GetContactProviderPtr(MCONTACT hContact) { char* szProto = GetContactProto(hContact); if (nullptr == szProto || 0 != ::_stricmp(szProto, CURRENCYRATES_PROTOCOL_NAME)) - return TCurrencyRatesProviderPtr(); + return nullptr; tstring sProvider = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CURRENCYRATE_PROVIDER); if (true == sProvider.empty()) - return TCurrencyRatesProviderPtr(); + return nullptr; return FindProvider(sProvider); } -CCurrencyRatesProviders::TCurrencyRatesProviderPtr CCurrencyRatesProviders::FindProvider(const tstring& rsName) const +///////////////////////////////////////////////////////////////////////////////////////// + +ICurrencyRatesProvider* FindProvider(const tstring& rsName) { - TCurrencyRatesProviderPtr pResult; - for (TCurrencyRatesProviders::const_iterator i = m_apProviders.begin(); i != m_apProviders.end(); ++i) { - const TCurrencyRatesProviderPtr& pProvider = *i; + for (auto &pProvider : g_apProviders) { const ICurrencyRatesProvider::CProviderInfo& rInfo = pProvider->GetInfo(); - if (0 == ::mir_wstrcmpi(rsName.c_str(), rInfo.m_sName.c_str())) { - pResult = pProvider; - break; - } + if (0 == ::mir_wstrcmpi(rsName.c_str(), rInfo.m_sName.c_str())) + return pProvider; } - return pResult; + return nullptr; } diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviders.h b/plugins/CurrencyRates/src/CurrencyRatesProviders.h deleted file mode 100644 index e4d762d99d..0000000000 --- a/plugins/CurrencyRates/src/CurrencyRatesProviders.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef __148306d1_da2a_43df_b1ad_0cdc8ef8a79e_CurrencyRatesProviders_h__ -#define __148306d1_da2a_43df_b1ad_0cdc8ef8a79e_CurrencyRatesProviders_h__ - -class ICurrencyRatesProvider; - -class CCurrencyRatesProviders -{ -public: - typedef boost::shared_ptr<ICurrencyRatesProvider> TCurrencyRatesProviderPtr; - typedef std::vector<TCurrencyRatesProviderPtr> TCurrencyRatesProviders; - -public: - CCurrencyRatesProviders(); - ~CCurrencyRatesProviders(); - - TCurrencyRatesProviderPtr FindProvider(const tstring& rsName) const; - TCurrencyRatesProviderPtr GetContactProviderPtr(MCONTACT hContact) const; - const TCurrencyRatesProviders& GetProviders() const; - -private: - void InitProviders(); - void CreateProviders(); - void ClearProviders(); - -private: - TCurrencyRatesProviders m_apProviders; -}; - -#endif //__148306d1_da2a_43df_b1ad_0cdc8ef8a79e_CurrencyRatesProviders_h__ diff --git a/plugins/CurrencyRates/src/Forex.cpp b/plugins/CurrencyRates/src/Forex.cpp index b7d22ffcee..b3b2424d48 100644 --- a/plugins/CurrencyRates/src/Forex.cpp +++ b/plugins/CurrencyRates/src/Forex.cpp @@ -37,9 +37,8 @@ void UpdateMenu(bool bAutoUpdate) INT_PTR CurrencyRatesMenu_RefreshAll(WPARAM, LPARAM) { - const CCurrencyRatesProviders::TCurrencyRatesProviders& apProviders = CModuleInfo::GetCurrencyRateProvidersPtr()->GetProviders(); - for (auto &it : apProviders) - it->RefreshAllContacts(); + for (auto &pProvider : g_apProviders) + pProvider->RefreshAllContacts(); return 0; } @@ -48,8 +47,7 @@ INT_PTR CurrencyRatesMenu_EnableDisable(WPARAM, LPARAM) g_bAutoUpdate = (g_bAutoUpdate) ? false : true; db_set_b(0, CURRENCYRATES_MODULE_NAME, DB_STR_AUTO_UPDATE, g_bAutoUpdate); - const CModuleInfo::TCurrencyRatesProvidersPtr& pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); - for (auto &pProvider : pProviders->GetProviders()) { + for (auto &pProvider : g_apProviders) { pProvider->RefreshSettings(); if (g_bAutoUpdate) pProvider->RefreshAllContacts(); @@ -198,22 +196,14 @@ int CurrencyRatesEventFunc_OnModulesLoaded(WPARAM, LPARAM) ::ResetEvent(g_hEventWorkThreadStop); - const CModuleInfo::TCurrencyRatesProvidersPtr& pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); - const CCurrencyRatesProviders::TCurrencyRatesProviders& rapProviders = pProviders->GetProviders(); - for (CCurrencyRatesProviders::TCurrencyRatesProviders::const_iterator i = rapProviders.begin(); i != rapProviders.end(); ++i) { - const CCurrencyRatesProviders::TCurrencyRatesProviderPtr& pProvider = *i; - g_ahThreads.push_back(mir_forkthread(WorkingThread, pProvider.get())); - } - + for (auto &pProvider : g_apProviders) + g_ahThreads.push_back(mir_forkthread(WorkingThread, pProvider)); return 0; } -int CurrencyRatesEventFunc_OnContactDeleted(WPARAM wParam, LPARAM) +int CurrencyRatesEventFunc_OnContactDeleted(WPARAM hContact, LPARAM) { - MCONTACT hContact = MCONTACT(wParam); - - const CModuleInfo::TCurrencyRatesProvidersPtr& pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); - CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = pProviders->GetContactProviderPtr(hContact); + auto pProvider = GetContactProviderPtr(hContact); if (pProvider) pProvider->DeleteContact(hContact); return 0; @@ -260,7 +250,7 @@ int CurrencyRatesEventFunc_OptInitialise(WPARAM wp, LPARAM/* lp*/) odp.szGroup.w = LPGENW("Network"); odp.flags = ODPF_USERINFOTAB | ODPF_UNICODE; - for (auto &it : CModuleInfo::GetCurrencyRateProvidersPtr()->GetProviders()) + for (auto &it : g_apProviders) it->ShowPropertyPage(wp, odp); return 0; } @@ -307,6 +297,8 @@ int CMPlugin::Load(void) CurrencyRates_IconsInit(); CurrencyRates_InitExtraIcons(); + InitProviders(); + CreateProtoServiceFunction(CURRENCYRATES_PROTOCOL_NAME, PS_GETCAPS, CurrencyRateProtoFunc_GetCaps); CreateProtoServiceFunction(CURRENCYRATES_PROTOCOL_NAME, PS_GETSTATUS, CurrencyRateProtoFunc_GetStatus); @@ -327,6 +319,7 @@ int CMPlugin::Unload(void) { WaitForWorkingThreads(); + ClearProviders(); ::CloseHandle(g_hEventWorkThreadStop); return 0; } diff --git a/plugins/CurrencyRates/src/HTTPSession.cpp b/plugins/CurrencyRates/src/HTTPSession.cpp index 2b07d4527e..ad5ddf725c 100644 --- a/plugins/CurrencyRates/src/HTTPSession.cpp +++ b/plugins/CurrencyRates/src/HTTPSession.cpp @@ -1,5 +1,7 @@ #include "StdAfx.h" +HNETLIBUSER CHTTPSession::g_hNetLib = nullptr; + #define ERROR_MSG LPGENW("This plugin requires a personal key. Press Yes to obtain it at the site and then enter the result in the Options dialog, otherwise this plugin will fail") void CALLBACK waitStub() @@ -8,142 +10,82 @@ void CALLBACK waitStub() Utils_OpenUrl("https://free.currencyconverterapi.com/free-api-key"); } -class CHTTPSession::CImpl -{ -public: - CImpl() {} - virtual ~CImpl() {} - - virtual bool OpenURL(const tstring &rsURL) = 0; - virtual bool ReadResponce(tstring &rsResponce) const = 0; -}; - -int find_header(const NETLIBHTTPREQUEST* pRequest, const char* hdr) +static int find_header(const NETLIBHTTPREQUEST* pRequest, const char* hdr) { for (int i = 0; i < pRequest->headersCount; ++i) - { if (0 == _stricmp(pRequest->headers[i].szName, hdr)) - { return i; - } - } return -1; } - -class CImplMI : public CHTTPSession::CImpl -{ -public: - CImplMI() {} - static bool Init() - { - assert(nullptr == g_hNetLib); - - ptrA szApiKey(g_plugin.getStringA(DB_KEY_ApiKey)); - if (szApiKey == nullptr) - Miranda_WaitOnHandle(waitStub); - - NETLIBUSER nlu = {}; - nlu.flags = NUF_OUTGOING | NUF_HTTPCONNS | NUF_NOHTTPSOPTION | NUF_UNICODE; - nlu.szSettingsModule = CURRENCYRATES_PROTOCOL_NAME; - nlu.szDescriptiveName.w = TranslateT("CurrencyRates HTTP connections"); - g_hNetLib = Netlib_RegisterUser(&nlu); - return (nullptr != g_hNetLib); - } +bool CHTTPSession::OpenURL(const tstring &rsURL) +{ + std::string s = currencyrates_t2a(rsURL.c_str()); + m_szUrl = s.c_str(); + return true; +} - static bool IsValid() { return nullptr != g_hNetLib; } +bool CHTTPSession::ReadResponce(tstring& rsResponce) +{ + if (m_szUrl.IsEmpty()) + return false; - virtual bool OpenURL(const tstring& rsURL) + NETLIBHTTPHEADER headers[] = { - m_aURL.clear(); - - std::string s = currencyrates_t2a(rsURL.c_str()); - const char* psz = s.c_str(); - m_aURL.insert(m_aURL.begin(), psz, psz + mir_strlen(psz) + 1); - return true; - } - - virtual bool ReadResponce(tstring &rsResponce) const + { "User-Agent", NETLIB_USER_AGENT }, + { "Connection", "close" }, + { "Cache-Control", "no-cache" }, + { "Pragma", "no-cache" } + }; + + NETLIBHTTPREQUEST nlhr = {}; + nlhr.cbSize = sizeof(nlhr); + nlhr.requestType = REQUEST_GET; + nlhr.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_REDIRECT; + nlhr.szUrl = m_szUrl.GetBuffer(); + nlhr.headersCount = _countof(headers); + nlhr.headers = headers; + + bool bResult = false; + NETLIBHTTPREQUEST *pReply = nullptr; { - if (true == m_aURL.empty()) - return false; - - NETLIBHTTPHEADER headers[] = - { - { "User-Agent", NETLIB_USER_AGENT }, - { "Connection", "close" }, - { "Cache-Control", "no-cache" }, - { "Pragma", "no-cache" } - }; - - NETLIBHTTPREQUEST nlhr = {}; - nlhr.cbSize = sizeof(nlhr); - nlhr.requestType = REQUEST_GET; - nlhr.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11 | NLHRF_REDIRECT; - nlhr.szUrl = &*(m_aURL.begin()); - nlhr.headersCount = _countof(headers); - nlhr.headers = headers; - - bool bResult = false; - NETLIBHTTPREQUEST *pReply = nullptr; - { - mir_cslock lck(m_mx); - pReply = Netlib_HttpTransaction(g_hNetLib, &nlhr); - } - - if (pReply) { - if ((200 == pReply->resultCode) && (pReply->dataLength > 0)) { - TBuffer apBuffer; - apBuffer.insert(apBuffer.begin(), pReply->pData, pReply->pData + pReply->dataLength); - apBuffer.push_back('\0'); - - char* pResult = &*(apBuffer.begin()); - int nIndex = find_header(pReply, "Content-Type"); - if ((-1 != nIndex) && (nullptr != strstr(_strlwr(pReply->headers[nIndex].szValue), "utf-8"))) { - rsResponce = ptrW(mir_utf8decodeW(pResult)); - } - else { - rsResponce = currencyrates_a2t(pResult); - } - - bResult = true; - } - - Netlib_FreeHttpRequest(pReply); - } - return bResult; + mir_cslock lck(m_mx); + pReply = Netlib_HttpTransaction(g_hNetLib, &nlhr); } -private: - static HNETLIBUSER g_hNetLib; - typedef std::vector<char> TBuffer; - mutable TBuffer m_aURL; - mutable mir_cs m_mx; -}; - -HNETLIBUSER CImplMI::g_hNetLib = nullptr; + if (pReply) { + if ((200 == pReply->resultCode) && (pReply->dataLength > 0)) { + CMStringA buf(pReply->pData, pReply->dataLength); + int nIndex = find_header(pReply, "Content-Type"); + if ((-1 != nIndex) && (nullptr != strstr(_strlwr(pReply->headers[nIndex].szValue), "utf-8"))) + rsResponce = ptrW(mir_utf8decodeW(buf)); + else + rsResponce = currencyrates_a2t(buf); -CHTTPSession::CHTTPSession() - : m_pImpl(new CImplMI) -{ -} - -CHTTPSession::~CHTTPSession() -{ -} + bResult = true; + } -bool CHTTPSession::OpenURL(const tstring& rsURL) -{ - return m_pImpl->OpenURL(rsURL); + Netlib_FreeHttpRequest(pReply); + } + return bResult; } -bool CHTTPSession::ReadResponce(tstring& rsResponce) const -{ - return m_pImpl->ReadResponce(rsResponce); -} +///////////////////////////////////////////////////////////////////////////////////////// +// module initialization bool CHTTPSession::Init() { - return CImplMI::Init(); -}
\ No newline at end of file + assert(nullptr == g_hNetLib); + + ptrA szApiKey(g_plugin.getStringA(DB_KEY_ApiKey)); + if (szApiKey == nullptr) + Miranda_WaitOnHandle(waitStub); + + NETLIBUSER nlu = {}; + nlu.flags = NUF_OUTGOING | NUF_HTTPCONNS | NUF_NOHTTPSOPTION | NUF_UNICODE; + nlu.szSettingsModule = CURRENCYRATES_PROTOCOL_NAME; + nlu.szDescriptiveName.w = TranslateT("CurrencyRates HTTP connections"); + g_hNetLib = Netlib_RegisterUser(&nlu); + return (nullptr != g_hNetLib); +} diff --git a/plugins/CurrencyRates/src/HTTPSession.h b/plugins/CurrencyRates/src/HTTPSession.h index e4b434eaec..9928c58304 100644 --- a/plugins/CurrencyRates/src/HTTPSession.h +++ b/plugins/CurrencyRates/src/HTTPSession.h @@ -3,23 +3,18 @@ class CHTTPSession { + static HNETLIBUSER g_hNetLib; + CMStringA m_szUrl; + mir_cs m_mx; + public: - CHTTPSession(); - ~CHTTPSession(); + CHTTPSession() {} + ~CHTTPSession() {} static bool Init(); - bool OpenURL(const tstring& rsURL); - bool ReadResponce(tstring& rsResponce) const; - - -public: - class CImpl; -private: - typedef boost::scoped_ptr<CImpl> TImpl; - -private: - TImpl m_pImpl; + bool OpenURL(const tstring &rsURL); + bool ReadResponce(tstring &rsResponce); }; #endif //__8C9706FF_6B05_4d0d_85B8_5724E5DC0BA4_HTTPSession_h__ diff --git a/plugins/CurrencyRates/src/ICurrencyRatesProvider.h b/plugins/CurrencyRates/src/ICurrencyRatesProvider.h index 506d2695b5..a80cae3088 100644 --- a/plugins/CurrencyRates/src/ICurrencyRatesProvider.h +++ b/plugins/CurrencyRates/src/ICurrencyRatesProvider.h @@ -47,4 +47,16 @@ public: virtual void Run() = 0; }; +///////////////////////////////////////////////////////////////////////////////////////// + +typedef std::vector<ICurrencyRatesProvider*> TCurrencyRatesProviders; +extern TCurrencyRatesProviders g_apProviders; + +ICurrencyRatesProvider* FindProvider(const tstring& rsName); +ICurrencyRatesProvider* GetContactProviderPtr(MCONTACT hContact); + +void InitProviders(); +void CreateProviders(); +void ClearProviders(); + #endif //__ac71e133_786c_41a7_ab07_625b76ff2a8c_CurrencyRatesProvider_h__ diff --git a/plugins/CurrencyRates/src/ImportExport.cpp b/plugins/CurrencyRates/src/ImportExport.cpp index 03d35eadb7..95fb9d95f7 100644 --- a/plugins/CurrencyRates/src/ImportExport.cpp +++ b/plugins/CurrencyRates/src/ImportExport.cpp @@ -206,15 +206,13 @@ INT_PTR CurrencyRates_Export(WPARAM wp, LPARAM lp) } else sFileName = currencyrates_a2t(pszFile); - CModuleInfo::TCurrencyRatesProvidersPtr pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); - TiXmlDocument doc; auto *pRoot = doc.NewElement(g_szXmlContacts); doc.InsertFirstChild(pRoot); MCONTACT hContact = MCONTACT(wp); if (hContact) { - CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = pProviders->GetContactProviderPtr(hContact); + auto pProvider = GetContactProviderPtr(hContact); if (pProvider) { auto *pNode = export_contact(hContact, doc); if (pNode) @@ -223,7 +221,7 @@ INT_PTR CurrencyRates_Export(WPARAM wp, LPARAM lp) } else { for (auto &cc : Contacts(CURRENCYRATES_MODULE_NAME)) { - CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = pProviders->GetContactProviderPtr(cc); + auto pProvider = GetContactProviderPtr(cc); if (pProvider) { auto *pNode = export_contact(cc, doc); if (pNode) @@ -371,7 +369,7 @@ struct CContactState { CContactState() : m_hContact(NULL), m_bNewContact(false) {} MCONTACT m_hContact; - CCurrencyRatesProviders::TCurrencyRatesProviderPtr m_pProvider; + ICurrencyRatesProvider *m_pProvider; bool m_bNewContact; }; @@ -399,15 +397,15 @@ TNameValue parse_setting_node(const TiXmlNode *pXmlSetting) return std::make_pair(sName, sValue); } -CCurrencyRatesProviders::TCurrencyRatesProviderPtr find_provider(const TiXmlNode *pXmlCurrencyRatesModule) +ICurrencyRatesProvider* find_provider(const TiXmlNode *pXmlCurrencyRatesModule) { for (auto *pNode : TiXmlFilter(pXmlCurrencyRatesModule, g_szXmlSetting)) { TNameValue Item = parse_setting_node(pNode); if ((!mir_strcmpi(DB_STR_CURRENCYRATE_PROVIDER, Item.first)) && Item.second) - return CModuleInfo::GetCurrencyRateProvidersPtr()->FindProvider(Utf2T(Item.second).get()); + return FindProvider(Utf2T(Item.second).get()); } - return CCurrencyRatesProviders::TCurrencyRatesProviderPtr(); + return nullptr; } bool get_contact_state(const TiXmlNode *pXmlContact, CContactState& cst) diff --git a/plugins/CurrencyRates/src/ModuleInfo.cpp b/plugins/CurrencyRates/src/ModuleInfo.cpp index 5e0504cdfc..172f2ba884 100644 --- a/plugins/CurrencyRates/src/ModuleInfo.cpp +++ b/plugins/CurrencyRates/src/ModuleInfo.cpp @@ -29,12 +29,6 @@ void CModuleInfo::OnMirandaShutdown() WindowList_Broadcast(p.second, WM_CLOSE, 0, 0); } -CModuleInfo::TCurrencyRatesProvidersPtr CModuleInfo::GetCurrencyRateProvidersPtr() -{ - static TCurrencyRatesProvidersPtr pProviders(new CCurrencyRatesProviders); - return pProviders; -} - CModuleInfo::THTMLEnginePtr CModuleInfo::GetHTMLEngine() { if (!g_pHTMLEngine) { diff --git a/plugins/CurrencyRates/src/ModuleInfo.h b/plugins/CurrencyRates/src/ModuleInfo.h index 8b99a248d6..39399f5c43 100644 --- a/plugins/CurrencyRates/src/ModuleInfo.h +++ b/plugins/CurrencyRates/src/ModuleInfo.h @@ -16,8 +16,6 @@ public: static bool Verify(); - static TCurrencyRatesProvidersPtr GetCurrencyRateProvidersPtr(); - static THTMLEnginePtr GetHTMLEngine(); static void SetHTMLEngine(THTMLEnginePtr pEngine); }; diff --git a/plugins/CurrencyRates/src/SettingsDlg.cpp b/plugins/CurrencyRates/src/SettingsDlg.cpp index 93f8b72e7a..164367450e 100644 --- a/plugins/CurrencyRates/src/SettingsDlg.cpp +++ b/plugins/CurrencyRates/src/SettingsDlg.cpp @@ -259,12 +259,11 @@ INT_PTR CALLBACK EditSettingsPerContactDlgProc(HWND hWnd, UINT msg, WPARAM wp, L tstring sName = GetContactName(hContact); ::SetDlgItemText(hWnd, IDC_EDIT_NAME, sName.c_str()); - CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact); - BYTE bUseContactSpecific = db_get_b(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CONTACT_SPEC_SETTINGS, 0); ::CheckDlgButton(hWnd, IDC_CHECK_CONTACT_SPECIFIC, bUseContactSpecific ? BST_CHECKED : BST_UNCHECKED); - CAdvProviderSettings setGlobal(pProvider.get()); + auto pProvider = GetContactProviderPtr(hContact); + CAdvProviderSettings setGlobal(pProvider); // log to history WORD dwLogMode = db_get_w(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CURRENCYRATE_LOG, setGlobal.GetLogMode()); UINT nCheck = (dwLogMode&lmInternalHistory) ? 1 : 0; @@ -314,10 +313,8 @@ INT_PTR CALLBACK EditSettingsPerContactDlgProc(HWND hWnd, UINT msg, WPARAM wp, L case IDC_BUTTON_HISTORY_DESCRIPTION: case IDC_BUTTON_LOG_FILE_DESCRIPTION: case IDC_BUTTON_POPUP_FORMAT_DESCRIPTION: - if (BN_CLICKED == HIWORD(wp)) { - CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(get_param(hWnd)->m_hContact); - show_variable_list(hWnd, pProvider.get()); - } + if (BN_CLICKED == HIWORD(wp)) + show_variable_list(hWnd, GetContactProviderPtr(get_param(hWnd)->m_hContact)); break; case IDC_CHECK_CONTACT_SPECIFIC: @@ -632,7 +629,7 @@ INT_PTR CALLBACK EditSettingsPerProviderDlgProc(HWND hWnd, UINT msg, WPARAM wp, return FALSE; } -CAdvProviderSettings::CAdvProviderSettings(const ICurrencyRatesProvider* pCurrencyRatesProvider) +CAdvProviderSettings::CAdvProviderSettings(const ICurrencyRatesProvider *pCurrencyRatesProvider) : m_pCurrencyRatesProvider(pCurrencyRatesProvider), m_wLogMode(lmDisabled), m_bIsOnlyChangedHistory(false), @@ -947,14 +944,14 @@ tstring GetContactLogFileName(MCONTACT hContact) { tstring result; - const CCurrencyRatesProviders::TCurrencyRatesProviderPtr& pProvider = CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact); + auto pProvider = GetContactProviderPtr(hContact); if (pProvider) { tstring sPattern; bool bUseContactSpecific = (db_get_b(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CONTACT_SPEC_SETTINGS, 0) > 0); if (bUseContactSpecific) sPattern = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CURRENCYRATE_LOG_FILE); else { - CAdvProviderSettings global_settings(pProvider.get()); + CAdvProviderSettings global_settings(pProvider); sPattern = global_settings.GetLogFileName(); } diff --git a/plugins/CurrencyRates/src/SettingsDlg.h b/plugins/CurrencyRates/src/SettingsDlg.h index 1a02474e9c..b6b49b97bd 100644 --- a/plugins/CurrencyRates/src/SettingsDlg.h +++ b/plugins/CurrencyRates/src/SettingsDlg.h @@ -57,7 +57,7 @@ private: class CAdvProviderSettings { public: - CAdvProviderSettings(const ICurrencyRatesProvider* pCurrencyRatesProvider); + CAdvProviderSettings(const ICurrencyRatesProvider *pCurrencyRatesProvider); ~CAdvProviderSettings(); void SaveToDb() const; @@ -87,7 +87,7 @@ public: CPopupSettings* GetPopupSettingsPtr() const; private: - const ICurrencyRatesProvider* m_pCurrencyRatesProvider; + const ICurrencyRatesProvider *m_pCurrencyRatesProvider; WORD m_wLogMode; tstring m_sFormatHistory; bool m_bIsOnlyChangedHistory; diff --git a/plugins/CurrencyRates/src/WinCtrlHelper.cpp b/plugins/CurrencyRates/src/WinCtrlHelper.cpp index 0d00422146..f45c4db536 100644 --- a/plugins/CurrencyRates/src/WinCtrlHelper.cpp +++ b/plugins/CurrencyRates/src/WinCtrlHelper.cpp @@ -25,7 +25,7 @@ public: } }; -void show_variable_list(HWND hwndParent, const ICurrencyRatesProvider* pProvider) +void show_variable_list(HWND hwndParent, const ICurrencyRatesProvider *pProvider) { CVariableListDlg(hwndParent, pProvider).DoModal(); } diff --git a/plugins/CurrencyRates/src/WinCtrlHelper.h b/plugins/CurrencyRates/src/WinCtrlHelper.h index c9e87e8ae5..a770072bd6 100644 --- a/plugins/CurrencyRates/src/WinCtrlHelper.h +++ b/plugins/CurrencyRates/src/WinCtrlHelper.h @@ -21,7 +21,7 @@ inline void prepare_edit_ctrl_for_error(HWND hwndEdit) ::SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0); } -void show_variable_list(HWND hwndParent, const ICurrencyRatesProvider* pProvider); +void show_variable_list(HWND hwndParent, const ICurrencyRatesProvider *pProvider); inline int CurrencyRates_MessageBox(HWND hWnd, LPCTSTR pszText, UINT nType = MB_OK) { diff --git a/plugins/CurrencyRates/src/stdafx.h b/plugins/CurrencyRates/src/stdafx.h index 4f8d6ace9b..5e9ad0331f 100644 --- a/plugins/CurrencyRates/src/stdafx.h +++ b/plugins/CurrencyRates/src/stdafx.h @@ -38,7 +38,6 @@ #include <m_CurrencyRates.h> #include <m_toptoolbar.h> -#include <boost\scoped_ptr.hpp> #include <boost\date_time\posix_time\posix_time.hpp> #include <boost\date_time\c_local_time_adjustor.hpp> @@ -96,7 +95,6 @@ inline tstring currencyrates_a2t(const char* s) #include "ExtraImages.h" #include "IsWithinAccuracy.h" #include "ICurrencyRatesProvider.h" -#include "CurrencyRatesProviders.h" #include "CurrencyRatesProviderBase.h" #define CHART_IMPLEMENT |