diff options
author | George Hazan <ghazan@miranda.im> | 2019-03-02 12:32:44 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2019-03-02 12:32:55 +0300 |
commit | 931a7dc1ac0dbc7e6c1083583ced915e572f5b47 (patch) | |
tree | 9fe9a6448d44030e26aa7107ce16044ed413e0d0 /protocols/CurrencyRates/src/CurrencyRatesProviders.cpp | |
parent | dd7d9954042254e66e3bbbec7195c6be8b1a0663 (diff) |
all protocols (even virtual ones) moved to the Protocols folder
Diffstat (limited to 'protocols/CurrencyRates/src/CurrencyRatesProviders.cpp')
-rw-r--r-- | protocols/CurrencyRates/src/CurrencyRatesProviders.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp b/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp new file mode 100644 index 0000000000..31dd4c771a --- /dev/null +++ b/protocols/CurrencyRates/src/CurrencyRatesProviders.cpp @@ -0,0 +1,83 @@ +#include "StdAfx.h" +#include "CurrencyRatesProviderCurrencyConverter.h" + +#define LAST_RUN_VERSION "LastRunVersion" + +TCurrencyRatesProviders g_apProviders; + +///////////////////////////////////////////////////////////////////////////////////////// + +template<class T>void create_provider(TCurrencyRatesProviders& g_apProviders) +{ + ICurrencyRatesProvider *pProvider = new T; + if (pProvider->Init()) + g_apProviders.push_back(pProvider); +}; + +void CreateProviders() +{ + create_provider<CCurrencyRatesProviderCurrencyConverter>(g_apProviders); +} + +///////////////////////////////////////////////////////////////////////////////////////// + +void ClearProviders() +{ + 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)); + if ((dwLogMode&lmInternalHistory) || (dwLogMode&lmExternalFile)) + db_set_b(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CONTACT_SPEC_SETTINGS, 1); +} + +void InitProviders() +{ + CreateProviders(); + + const WORD nCurrentVersion = 17; + WORD nVersion = db_get_w(0, CURRENCYRATES_MODULE_NAME, LAST_RUN_VERSION, 1); + + for (auto &hContact : Contacts(CURRENCYRATES_MODULE_NAME)) { + ICurrencyRatesProvider *pProvider = GetContactProviderPtr(hContact); + if (pProvider) { + pProvider->AddContact(hContact); + if (nVersion < nCurrentVersion) + convert_contact_settings(hContact); + } + } + + db_set_w(0, CURRENCYRATES_MODULE_NAME, LAST_RUN_VERSION, nCurrentVersion); +} + +///////////////////////////////////////////////////////////////////////////////////////// + +ICurrencyRatesProvider* GetContactProviderPtr(MCONTACT hContact) +{ + char* szProto = GetContactProto(hContact); + if (nullptr == szProto || 0 != ::_stricmp(szProto, CURRENCYRATES_PROTOCOL_NAME)) + return nullptr; + + tstring sProvider = CurrencyRates_DBGetStringW(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CURRENCYRATE_PROVIDER); + if (true == sProvider.empty()) + return nullptr; + + return FindProvider(sProvider); +} + +///////////////////////////////////////////////////////////////////////////////////////// + +ICurrencyRatesProvider* FindProvider(const tstring& rsName) +{ + for (auto &pProvider : g_apProviders) { + const ICurrencyRatesProvider::CProviderInfo& rInfo = pProvider->GetInfo(); + if (0 == ::mir_wstrcmpi(rsName.c_str(), rInfo.m_sName.c_str())) + return pProvider; + } + + return nullptr; +} |