diff options
-rw-r--r-- | include/m_system_cpp.h | 10 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/CurrencyConverter.cpp | 6 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/CurrencyRateInfoDlg.cpp | 6 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp | 94 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/DBUtils.cpp | 9 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/DBUtils.h | 3 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/Forex.cpp | 8 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/HTTPSession.cpp | 45 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/IXMLEngine.h | 43 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/ImportExport.cpp | 579 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/ImportExport.h | 2 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/ModuleInfo.cpp | 43 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/ModuleInfo.h | 20 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/SettingsDlg.cpp | 6 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/XMLEngineMI.cpp | 163 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/XMLEngineMI.h | 15 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/stdafx.h | 2 | ||||
-rw-r--r-- | plugins/CurrencyRates/src/version.h | 4 |
18 files changed, 350 insertions, 708 deletions
diff --git a/include/m_system_cpp.h b/include/m_system_cpp.h index 35392c78e2..1cd2163f10 100644 --- a/include/m_system_cpp.h +++ b/include/m_system_cpp.h @@ -352,6 +352,16 @@ public: #endif
};
+class Utf2T : public ptrW
+{
+public:
+ __forceinline Utf2T(const char *str) : ptrW(mir_utf8decodeW(str)) {}
+ __forceinline operator wchar_t* () const { return data; }
+ #ifdef _XSTRING_
+ std::wstring str() const { return std::wstring(data); }
+ #endif
+};
+
///////////////////////////////////////////////////////////////////////////////
// basic class for classes that should be cleared inside new()
diff --git a/plugins/CurrencyRates/src/CurrencyConverter.cpp b/plugins/CurrencyRates/src/CurrencyConverter.cpp index 91c2ae989e..ceb7dc1511 100644 --- a/plugins/CurrencyRates/src/CurrencyConverter.cpp +++ b/plugins/CurrencyRates/src/CurrencyConverter.cpp @@ -87,7 +87,7 @@ INT_PTR CALLBACK CurrencyConverterDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM case WM_INITDIALOG: TranslateDialogDefault(hDlg); { - MWindowList hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX, false); + MWindowList hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX, false); assert(hWL); WindowList_Add(hWL, hDlg); @@ -140,7 +140,7 @@ INT_PTR CALLBACK CurrencyConverterDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM case WM_CLOSE: { - MWindowList hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX, false); + MWindowList hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX, false); assert(hWL); WindowList_Remove(hWL, hDlg); Utils_SaveWindowPosition(hDlg, NULL, CURRENCYRATES_MODULE_NAME, WINDOW_PREFIX); @@ -254,7 +254,7 @@ INT_PTR CALLBACK CurrencyConverterDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM INT_PTR CurrencyRatesMenu_CurrencyConverter(WPARAM, LPARAM) { - MWindowList hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX, true); + MWindowList hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX, true); HWND hWnd = WindowList_Find(hWL, NULL); if (nullptr != hWnd) { SetForegroundWindow(hWnd); diff --git a/plugins/CurrencyRates/src/CurrencyRateInfoDlg.cpp b/plugins/CurrencyRates/src/CurrencyRateInfoDlg.cpp index 8ad8e1cd7c..4b9efb3067 100644 --- a/plugins/CurrencyRates/src/CurrencyRateInfoDlg.cpp +++ b/plugins/CurrencyRates/src/CurrencyRateInfoDlg.cpp @@ -168,7 +168,7 @@ static INT_PTR CALLBACK CurrencyRateInfoDlgProc1(HWND hdlg, UINT msg, WPARAM wPa switch (msg) { case WM_INITDIALOG: hContact = MCONTACT(lParam); - hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_INFO, false); + hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX_INFO, false); assert(hWL); WindowList_Add(hWL, hdlg, hContact); @@ -186,7 +186,7 @@ static INT_PTR CALLBACK CurrencyRateInfoDlgProc1(HWND hdlg, UINT msg, WPARAM wPa if (hContact) { SetWindowLongPtr(hdlg, GWLP_USERDATA, 0); - hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_INFO, false); + hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX_INFO, false); assert(hWL); WindowList_Remove(hWL, hdlg); Utils_SaveWindowPosition(hdlg, hContact, CURRENCYRATES_MODULE_NAME, WINDOW_PREFIX_INFO); @@ -211,7 +211,7 @@ int CurrencyRates_OnContactDoubleClick(WPARAM wp, LPARAM/* lp*/) { MCONTACT hContact = MCONTACT(wp); if (CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact)) { - MWindowList hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_INFO, true); + MWindowList hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX_INFO, true); assert(hWL); HWND hWnd = WindowList_Find(hWL, hContact); if (nullptr != hWnd) { diff --git a/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp b/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp index 69c6a1d892..41e9b00c88 100644 --- a/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp +++ b/plugins/CurrencyRates/src/CurrencyRatesProviderBase.cpp @@ -16,27 +16,23 @@ inline tstring get_ini_file_name(LPCTSTR pszFileName) return CreateFilePath(pszFileName); } -bool parse_currencyrate(const IXMLNode::TXMLNodePtr& pTop, CCurrencyRatesProviderBase::CCurrencyRate& q) -{ - tstring sSymbol; - tstring sDescription; - tstring sID; - - size_t cChild = pTop->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = pTop->GetChildNode(i); - tstring sName = pNode->GetName(); - if (0 == mir_wstrcmpi(L"symbol", sName.c_str())) { - sSymbol = pNode->GetText(); - if (true == sSymbol.empty()) +bool parse_currencyrate(const TiXmlNode *pTop, CCurrencyRatesProviderBase::CCurrencyRate &q) +{ + tstring sSymbol, sDescription, sID; + + for (auto *pNode = pTop->FirstChildElement(); pNode != nullptr; pNode = pNode->NextSiblingElement()) { + const char *sName = pNode->Value(); + if (!mir_strcmpi(sName, "symbol")) { + sSymbol = GetNodeText(pNode); + if (sSymbol.empty()) return false; } - else if (0 == mir_wstrcmpi(L"description", sName.c_str())) { - sDescription = pNode->GetText(); + else if (!mir_strcmpi(sName, "description")) { + sDescription = GetNodeText(pNode); } - else if (0 == mir_wstrcmpi(L"id", sName.c_str())) { - sID = pNode->GetText(); - if (true == sID.empty()) + else if (!mir_strcmpi(sName, "id")) { + sID = GetNodeText(pNode); + if (sID.empty()) return false; } } @@ -45,29 +41,27 @@ bool parse_currencyrate(const IXMLNode::TXMLNodePtr& pTop, CCurrencyRatesProvide return true; } -bool parse_section(const IXMLNode::TXMLNodePtr& pTop, CCurrencyRatesProviderBase::CCurrencyRateSection& qs) +bool parse_section(const TiXmlNode *pTop, CCurrencyRatesProviderBase::CCurrencyRateSection &qs) { CCurrencyRatesProviderBase::CCurrencyRateSection::TSections aSections; CCurrencyRatesProviderBase::CCurrencyRateSection::TCurrencyRates aCurrencyRates; tstring sSectionName; - size_t cChild = pTop->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = pTop->GetChildNode(i); - tstring sName = pNode->GetName(); - if (0 == mir_wstrcmpi(L"section", sName.c_str())) { + for (auto *pNode = pTop->FirstChildElement(); pNode != nullptr; pNode = pNode->NextSiblingElement()) { + const char *sName = pNode->Value(); + if (!mir_strcmpi(sName, "section")) { CCurrencyRatesProviderBase::CCurrencyRateSection qs1; if (true == parse_section(pNode, qs1)) aSections.push_back(qs1); } - else if (0 == mir_wstrcmpi(L"currencyrate", sName.c_str())) { + else if (!mir_strcmpi(sName, "currencyrate")) { CCurrencyRatesProviderBase::CCurrencyRate q; if (true == parse_currencyrate(pNode, q)) aCurrencyRates.push_back(q); } - else if (0 == mir_wstrcmpi(L"name", sName.c_str())) { - sSectionName = pNode->GetText(); - if (true == sSectionName.empty()) + else if (!mir_strcmpi(sName, "name")) { + sSectionName = GetNodeText(pNode); + if (sSectionName.empty()) return false; } } @@ -76,14 +70,13 @@ bool parse_section(const IXMLNode::TXMLNodePtr& pTop, CCurrencyRatesProviderBase return true; } -IXMLNode::TXMLNodePtr find_provider(const IXMLNode::TXMLNodePtr& pRoot) +const TiXmlNode* find_provider(const TiXmlNode *pRoot) { - IXMLNode::TXMLNodePtr pProvider; - size_t cChild = pRoot->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = pRoot->GetChildNode(i); - tstring sName = pNode->GetName(); - if (0 == mir_wstrcmpi(L"Provider", sName.c_str())) { + const TiXmlNode *pProvider = nullptr; + + for (auto *pNode = pRoot->FirstChildElement(); pNode != nullptr; pNode = pNode->NextSiblingElement()) { + const char *sName = pNode->Value(); + if (!mir_strcmpi(sName, "Provider")) { pProvider = pNode; break; } @@ -96,32 +89,29 @@ IXMLNode::TXMLNodePtr find_provider(const IXMLNode::TXMLNodePtr& pRoot) return pProvider; } -CCurrencyRatesProviderBase::CXMLFileInfo parse_ini_file(const tstring& rsXMLFile, bool& rbSucceded) +CCurrencyRatesProviderBase::CXMLFileInfo parse_ini_file(const tstring &rsXMLFile, bool &rbSucceded) { CCurrencyRatesProviderBase::CXMLFileInfo res; CCurrencyRatesProviderBase::CCurrencyRateSection::TSections aSections; - const CModuleInfo::TXMLEnginePtr& pXMLEngine = CModuleInfo::GetXMLEnginePtr(); - IXMLNode::TXMLNodePtr pRoot = pXMLEngine->LoadFile(rsXMLFile); - if (pRoot) { - IXMLNode::TXMLNodePtr pProvider = find_provider(pRoot); + TiXmlDocument doc; + if (doc.LoadFile(_T2A(rsXMLFile.c_str())) == tinyxml2::XML_SUCCESS) { + const TiXmlNode *pProvider = find_provider(&doc); if (pProvider) { rbSucceded = true; - size_t cChild = pProvider->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = pProvider->GetChildNode(i); - tstring sName = pNode->GetName(); - if (0 == mir_wstrcmpi(L"section", sName.c_str())) { + for (auto *pNode = pProvider->FirstChildElement(); pNode != nullptr; pNode = pNode->NextSiblingElement()) { + const char *sName = pNode->Value(); + if (!mir_strcmpi(sName, "section")) { CCurrencyRatesProviderBase::CCurrencyRateSection qs; - if (true == parse_section(pNode, qs)) + if (parse_section(pNode, qs)) aSections.push_back(qs); } - else if (0 == mir_wstrcmpi(L"Name", sName.c_str())) - res.m_pi.m_sName = pNode->GetText(); - else if (0 == mir_wstrcmpi(L"ref", sName.c_str())) - res.m_pi.m_sURL = pNode->GetText(); - else if (0 == mir_wstrcmpi(L"url", sName.c_str())) - res.m_sURL = pNode->GetText(); + else if (!mir_strcmpi(sName, "Name")) + res.m_pi.m_sName = GetNodeText(pNode); + else if (!mir_strcmpi(sName, "ref")) + res.m_pi.m_sURL = GetNodeText(pNode); + else if (!mir_strcmpi(sName, "url")) + res.m_sURL = GetNodeText(pNode); } } } diff --git a/plugins/CurrencyRates/src/DBUtils.cpp b/plugins/CurrencyRates/src/DBUtils.cpp index 0645387d9e..53e1f211b3 100644 --- a/plugins/CurrencyRates/src/DBUtils.cpp +++ b/plugins/CurrencyRates/src/DBUtils.cpp @@ -1,11 +1,12 @@ #include "StdAfx.h" -std::string CurrencyRates_DBGetStringA(MCONTACT hContact, const char* szModule, const char* szSetting, const char* pszDefValue) +std::wstring GetNodeText(const TiXmlElement *pNode) { - if (pszDefValue == nullptr) - pszDefValue = ""; + auto *pszText = pNode->GetText(); + if (pszText) + return Utf2T(pszText); - return std::string(ptrA(db_get_sa(hContact, szModule, szSetting, pszDefValue))); + return std::wstring(); } std::wstring CurrencyRates_DBGetStringW(MCONTACT hContact, const char* szModule, const char* szSetting, const wchar_t* pszDefValue) diff --git a/plugins/CurrencyRates/src/DBUtils.h b/plugins/CurrencyRates/src/DBUtils.h index 3e5f10e841..6fcb445936 100644 --- a/plugins/CurrencyRates/src/DBUtils.h +++ b/plugins/CurrencyRates/src/DBUtils.h @@ -1,7 +1,8 @@ #ifndef __54294385_3fdd_4f0c_98c3_c583a96e7fb4_DBUtils_h__ #define __54294385_3fdd_4f0c_98c3_c583a96e7fb4_DBUtils_h__ -std::string CurrencyRates_DBGetStringA(MCONTACT hContact, const char* szModule, const char* szSetting, const char* pszDefValue = nullptr); +std::wstring GetNodeText(const TiXmlElement*); + 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); diff --git a/plugins/CurrencyRates/src/Forex.cpp b/plugins/CurrencyRates/src/Forex.cpp index 691c13a240..7d05e4994a 100644 --- a/plugins/CurrencyRates/src/Forex.cpp +++ b/plugins/CurrencyRates/src/Forex.cpp @@ -94,18 +94,16 @@ void InitMenu() SET_UID(mi, 0x7cca4fd9, 0x903f, 0x4b7d, 0x93, 0x7a, 0x18, 0x63, 0x23, 0xd4, 0xa9, 0xa9); mi.name.w = LPGENW("Export All Currency Rates"); mi.hIcolibItem = CurrencyRates_GetIconHandle(IDI_ICON_EXPORT); - mi.pszService = "CurrencyRates/ExportAll"; + mi.pszService = MS_CURRENCYRATES_EXPORT; mi.position = 20100003; Menu_AddMainMenuItem(&mi); - CreateServiceFunction(mi.pszService, CurrencyRatesMenu_ExportAll); SET_UID(mi, 0xa994d3b, 0x77c2, 0x4612, 0x8d, 0x5, 0x6a, 0xae, 0x8c, 0x21, 0xbd, 0xc9); mi.name.w = LPGENW("Import All Currency Rates"); mi.hIcolibItem = CurrencyRates_GetIconHandle(IDI_ICON_IMPORT); - mi.pszService = "CurrencyRates/ImportAll"; + mi.pszService = MS_CURRENCYRATES_IMPORT; mi.position = 20100004; Menu_AddMainMenuItem(&mi); - CreateServiceFunction(mi.pszService, CurrencyRatesMenu_ImportAll); HookEvent(ME_CLIST_PREBUILDCONTACTMENU, CurrencyRates_PrebuildContactMenu); @@ -250,7 +248,7 @@ int CurrencyRatesEventFunc_PreShutdown(WPARAM, LPARAM) { ::SetEvent(g_hEventWorkThreadStop); - CModuleInfo::GetInstance().OnMirandaShutdown(); + CModuleInfo::OnMirandaShutdown(); return 0; } diff --git a/plugins/CurrencyRates/src/HTTPSession.cpp b/plugins/CurrencyRates/src/HTTPSession.cpp index f7cb9cfb85..06f1d5409d 100644 --- a/plugins/CurrencyRates/src/HTTPSession.cpp +++ b/plugins/CurrencyRates/src/HTTPSession.cpp @@ -52,35 +52,29 @@ public: return true; } - virtual bool ReadResponce(tstring& rsResponce)const + virtual bool ReadResponce(tstring &rsResponce)const { if (true == m_aURL.empty()) return false; - NETLIBHTTPREQUEST nlhr = { 0 }; + 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; - char* pURL = &*(m_aURL.begin()); - nlhr.szUrl = pURL; - - nlhr.headersCount = 4; - nlhr.headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)*nlhr.headersCount); - nlhr.headers[0].szName = "User-Agent"; - nlhr.headers[0].szValue = NETLIB_USER_AGENT; - nlhr.headers[1].szName = "Connection"; - nlhr.headers[1].szValue = "close"; - nlhr.headers[2].szName = "Cache-Control"; - nlhr.headers[2].szValue = "no-cache"; - nlhr.headers[3].szName = "Pragma"; - nlhr.headers[3].szValue = "no-cache"; - // nlhr.headers[4].szName = "Accept-Encoding"; - // nlhr.headers[4].szValue = "deflate, gzip"; - // nlhr.headers[5].szName = "Cookie"; - // nlhr.headers[5].szValue = cookie; + nlhr.szUrl = &*(m_aURL.begin()); + nlhr.headersCount = _countof(headers); + nlhr.headers = headers; bool bResult = false; - NETLIBHTTPREQUEST* pReply = nullptr; + NETLIBHTTPREQUEST *pReply = nullptr; { mir_cslock lck(m_mx); pReply = Netlib_HttpTransaction(g_hNetLib, &nlhr); @@ -95,14 +89,10 @@ public: char* pResult = &*(apBuffer.begin()); int nIndex = find_header(pReply, "Content-Type"); if ((-1 != nIndex) && (nullptr != strstr(_strlwr(pReply->headers[nIndex].szValue), "utf-8"))) { - wchar_t* p = mir_utf8decodeW(pResult); - rsResponce = p; - mir_free(p); + rsResponce = ptrW(mir_utf8decodeW(pResult)); } else { - // USES_CONVERSION; - // LPCTSTR p = A2CT(pResult); - rsResponce = currencyrates_a2t(pResult);//p; + rsResponce = currencyrates_a2t(pResult); } bResult = true; @@ -110,9 +100,6 @@ public: Netlib_FreeHttpRequest(pReply); } - - mir_free(nlhr.headers); - return bResult; } diff --git a/plugins/CurrencyRates/src/IXMLEngine.h b/plugins/CurrencyRates/src/IXMLEngine.h deleted file mode 100644 index 4b87a17166..0000000000 --- a/plugins/CurrencyRates/src/IXMLEngine.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef __f88e20d7_5e65_40fb_a7b5_7c7af1ee1c78_IXMLEngine_h__ -#define __f88e20d7_5e65_40fb_a7b5_7c7af1ee1c78_IXMLEngine_h__ - -class IXMLNode -{ -public: - typedef boost::shared_ptr<IXMLNode> TXMLNodePtr; - -public: - IXMLNode() {} - virtual ~IXMLNode() {} - - virtual size_t GetChildCount()const = 0; - virtual TXMLNodePtr GetChildNode(size_t nIndex)const = 0; - - virtual tstring GetText()const = 0; - virtual tstring GetName()const = 0; - - virtual bool AddChild(const TXMLNodePtr& pNode) = 0; - virtual bool AddAttribute(const tstring& rsName, const tstring& rsValue) = 0; - virtual tstring GetAttributeValue(const tstring& rsAttrName) = 0; - virtual void Write(tostream& o)const = 0; -}; - -inline tostream& operator<<(tostream& o, const IXMLNode& node) -{ - node.Write(o); - return o; -} - -class IXMLEngine -{ -public: - IXMLEngine() {} - - virtual ~IXMLEngine() {} - - virtual IXMLNode::TXMLNodePtr LoadFile(const tstring& rsFileName)const = 0; - virtual bool SaveFile(const tstring& rsFileName, const IXMLNode::TXMLNodePtr& pNode)const = 0; - virtual IXMLNode::TXMLNodePtr CreateNode(const tstring& rsName, const tstring& rsText)const = 0; -}; - -#endif //__f88e20d7_5e65_40fb_a7b5_7c7af1ee1c78_IXMLEngine_h__ diff --git a/plugins/CurrencyRates/src/ImportExport.cpp b/plugins/CurrencyRates/src/ImportExport.cpp index a01e68349b..3087670e18 100644 --- a/plugins/CurrencyRates/src/ImportExport.cpp +++ b/plugins/CurrencyRates/src/ImportExport.cpp @@ -1,25 +1,29 @@ #include "StdAfx.h" #include "CurrencyRatesProviderCurrencyConverter.h" -LPCTSTR g_pszXmlValue = L"Value"; -LPCTSTR g_pszXmlName = L"Name"; -LPCTSTR g_pszXmlSetting = L"Setting"; -LPCTSTR g_pszXmlModule = L"Module"; -LPCTSTR g_pszXmlContact = L"Contact"; -LPCTSTR g_pszXmlContacts = L"Contacts"; -LPCTSTR g_pszXmlType = L"type"; -LPCTSTR g_pszXmlTypeByte = L"byte"; -LPCTSTR g_pszXmlTypeWord = L"word"; -LPCTSTR g_pszXmlTypeDword = L"dword"; -LPCTSTR g_pszXmlTypeAsciiz = L"asciiz"; -LPCTSTR g_pszXmlTypeWchar = L"wchar"; -LPCTSTR g_pszXmlTypeUtf8 = L"utf8"; -LPCTSTR g_pszXmlTypeBlob = L"blob"; +const char g_szXmlValue[] = "Value"; +const char g_szXmlName[] = "Name"; +const char g_szXmlSetting[] = "Setting"; +const char g_szXmlModule[] = "Module"; +const char g_szXmlContact[] = "Contact"; +const char g_szXmlContacts[] = "Contacts"; +const char g_szXmlType[] = "type"; +const char g_szXmlTypeByte[] = "byte"; +const char g_szXmlTypeWord[] = "word"; +const char g_szXmlTypeDword[] = "dword"; +const char g_szXmlTypeAsciiz[] = "asciiz"; +const char g_szXmlTypeWchar[] = "wchar"; +const char g_szXmlTypeUtf8[] = "utf8"; +const char g_szXmlTypeBlob[] = "blob"; struct CEnumContext { - CModuleInfo::TXMLEnginePtr m_pXmlEngine; - IXMLNode::TXMLNodePtr m_pNode; + CEnumContext(TiXmlDocument &doc) : + m_xmlDoc(doc) + {} + + TiXmlDocument &m_xmlDoc; + TiXmlNode *m_pNode; MCONTACT m_hContact; LPCSTR m_pszModule; }; @@ -31,48 +35,48 @@ struct mir_safety_dbvar DBVARIANT* m_p; }; -static int enum_contact_settings(const char* szSetting, void *lp) +static int enum_contact_settings(const char *szSetting, void *lp) { - CEnumContext* ctx = reinterpret_cast<CEnumContext*>(lp); + CEnumContext *ctx = reinterpret_cast<CEnumContext*>(lp); DBVARIANT dbv; if (0 == db_get(ctx->m_hContact, ctx->m_pszModule, szSetting, &dbv)) { mir_safety_dbvar sdbvar(&dbv); - tstring sType; - tostringstream sValue; + std::string sType; + std::wostringstream sValue; sValue.imbue(GetSystemLocale()); switch (dbv.type) { case DBVT_BYTE: sValue << dbv.bVal; - sType = g_pszXmlTypeByte; + sType = g_szXmlTypeByte; break; case DBVT_WORD: sValue << dbv.wVal; - sType = g_pszXmlTypeWord; + sType = g_szXmlTypeWord; break; case DBVT_DWORD: sValue << dbv.dVal; - sType = g_pszXmlTypeDword; + sType = g_szXmlTypeDword; break; case DBVT_ASCIIZ: - sType = g_pszXmlTypeAsciiz; + sType = g_szXmlTypeAsciiz; if (dbv.pszVal) sValue << dbv.pszVal; break; case DBVT_WCHAR: - sType = g_pszXmlTypeWchar; + sType = g_szXmlTypeWchar; if (dbv.pwszVal) sValue << dbv.pwszVal; break; case DBVT_UTF8: - sType = g_pszXmlTypeUtf8; + sType = g_szXmlTypeUtf8; if (dbv.pszVal) sValue << dbv.pszVal; break; case DBVT_BLOB: - sType = g_pszXmlTypeBlob; + sType = g_szXmlTypeBlob; if (dbv.pbVal) { ptrA buf(mir_base64_encode(dbv.pbVal, dbv.cpbVal)); if (buf) @@ -81,19 +85,17 @@ static int enum_contact_settings(const char* szSetting, void *lp) break; } - IXMLNode::TXMLNodePtr pXmlSet = ctx->m_pXmlEngine->CreateNode(g_pszXmlSetting, tstring()); - if (pXmlSet) { - IXMLNode::TXMLNodePtr pXmlName = ctx->m_pXmlEngine->CreateNode(g_pszXmlName, currencyrates_a2t(szSetting)); + auto *pXmlName = ctx->m_xmlDoc.NewElement(g_szXmlName); + pXmlName->SetText(szSetting); - IXMLNode::TXMLNodePtr pXmlValue = ctx->m_pXmlEngine->CreateNode(g_pszXmlValue, sValue.str()); - if (pXmlName && pXmlValue) { - pXmlValue->AddAttribute(g_pszXmlType, sType); + auto *pXmlValue = ctx->m_xmlDoc.NewElement(g_szXmlValue); + pXmlValue->SetText(T2Utf(sValue.str().c_str()).get()); + pXmlValue->SetAttribute(g_szXmlType, sType.c_str()); - pXmlSet->AddChild(pXmlName); - pXmlSet->AddChild(pXmlValue); - ctx->m_pNode->AddChild(pXmlSet); - } - } + auto *pXmlSet = ctx->m_xmlDoc.NewElement(g_szXmlSetting); + pXmlSet->InsertEndChild(pXmlName); + pXmlSet->InsertEndChild(pXmlValue); + ctx->m_pNode->InsertEndChild(pXmlSet); } return 0; @@ -102,34 +104,29 @@ static int enum_contact_settings(const char* szSetting, void *lp) int EnumDbModules(const char *szModuleName, void *lp) { CEnumContext *ctx = (CEnumContext*)lp; - IXMLNode::TXMLNodePtr pXml = ctx->m_pNode; - IXMLNode::TXMLNodePtr pModule = ctx->m_pXmlEngine->CreateNode(g_pszXmlModule, currencyrates_a2t(szModuleName)/*A2CT(szModuleName)*/); - if (pModule) { - ctx->m_pszModule = szModuleName; - ctx->m_pNode = pModule; - db_enum_settings(ctx->m_hContact, &enum_contact_settings, szModuleName, ctx); + auto *pXml = ctx->m_pNode; + auto *pModule = ctx->m_xmlDoc.NewElement(g_szXmlModule); + pModule->SetText(szModuleName); - if (pModule->GetChildCount() > 0) - pXml->AddChild(pModule); + ctx->m_pszModule = szModuleName; + ctx->m_pNode = pModule; + db_enum_settings(ctx->m_hContact, &enum_contact_settings, szModuleName, ctx); - ctx->m_pNode = pXml; - } + if (pModule->FirstChildElement(g_szXmlSetting)) + pXml->InsertEndChild(pModule); + ctx->m_pNode = pXml; return 0; } -IXMLNode::TXMLNodePtr export_contact(MCONTACT hContact, const CModuleInfo::TXMLEnginePtr& pXmlEngine) +TiXmlNode* export_contact(MCONTACT hContact, TiXmlDocument &pDoc) { - IXMLNode::TXMLNodePtr pNode = pXmlEngine->CreateNode(g_pszXmlContact, tstring()); - if (pNode) { - CEnumContext ctx; - ctx.m_pXmlEngine = pXmlEngine; - ctx.m_pNode = pNode; - ctx.m_hContact = hContact; - - db_enum_modules(EnumDbModules, &ctx); - } - return pNode; + CEnumContext ctx(pDoc); + ctx.m_pNode = pDoc.NewElement(g_szXmlContact); + ctx.m_hContact = hContact; + db_enum_modules(EnumDbModules, &ctx); + + return ctx.m_pNode; } LPCTSTR prepare_filter(LPTSTR pszBuffer, size_t cBuffer) @@ -208,32 +205,35 @@ INT_PTR CurrencyRates_Export(WPARAM wp, LPARAM lp) if (false == show_open_file_dialog(false, sFileName)) return -1; } - else sFileName = currencyrates_a2t(pszFile);//A2CT(pszFile); + else sFileName = currencyrates_a2t(pszFile); + + CModuleInfo::TCurrencyRatesProvidersPtr pProviders = CModuleInfo::GetCurrencyRateProvidersPtr(); + + TiXmlDocument doc; + auto *pRoot = doc.NewElement(g_szXmlContacts); + doc.InsertFirstChild(pRoot); - CModuleInfo::TXMLEnginePtr pXmlEngine = CModuleInfo::GetInstance().GetXMLEnginePtr(); - CModuleInfo::TCurrencyRatesProvidersPtr pProviders = CModuleInfo::GetInstance().GetCurrencyRateProvidersPtr(); - IXMLNode::TXMLNodePtr pRoot = pXmlEngine->CreateNode(g_pszXmlContacts, tstring()); MCONTACT hContact = MCONTACT(wp); if (hContact) { CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = pProviders->GetContactProviderPtr(hContact); if (pProvider) { - IXMLNode::TXMLNodePtr pNode = export_contact(hContact, pXmlEngine); + auto *pNode = export_contact(hContact, doc); if (pNode) - pRoot->AddChild(pNode); + pRoot->InsertEndChild(pNode); } } else { for (auto &cc : Contacts(CURRENCYRATES_MODULE_NAME)) { CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = pProviders->GetContactProviderPtr(cc); if (pProvider) { - IXMLNode::TXMLNodePtr pNode = export_contact(cc, pXmlEngine); + auto *pNode = export_contact(cc, doc); if (pNode) - pRoot->AddChild(pNode); + pRoot->InsertEndChild(pNode); } } } - return ((true == pXmlEngine->SaveFile(sFileName, pRoot)) ? 0 : 1); + return doc.SaveFile(_T2A(sFileName.c_str()), true); } bool set_contact_settings(MCONTACT hContact, DBCONTACTWRITESETTING& dbs) @@ -242,132 +242,116 @@ bool set_contact_settings(MCONTACT hContact, DBCONTACTWRITESETTING& dbs) return (0 == db_set(hContact, dbs.szModule, dbs.szSetting, &dbs.value)); } -bool handle_module(MCONTACT hContact, const IXMLNode::TXMLNodePtr& pXmlModule) +bool handle_module(MCONTACT hContact, const TiXmlElement *pXmlModule) { + const char *szModuleName = pXmlModule->GetText(); + if (szModuleName == nullptr) + return false; + size_t cCreatedRecords = 0; - tstring sModuleName = pXmlModule->GetText(); - if (false == sModuleName.empty()) { - DBCONTACTWRITESETTING dbs; - std::string s = currencyrates_t2a(sModuleName.c_str()); - dbs.szModule = s.c_str();//T2CA(sModuleName.c_str()); - - bool bCListModule = 0 == mir_wstrcmpi(sModuleName.c_str(), L"CList"); - - size_t cChild = pXmlModule->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pSetting = pXmlModule->GetChildNode(i); - tstring sSetting = pSetting->GetName(); - if (0 == mir_wstrcmpi(g_pszXmlSetting, sSetting.c_str())) { - size_t cSetChild = pSetting->GetChildCount(); - if (cSetChild >= 2) { - tstring sName; - tstring sValue; - tstring sType; - for (size_t j = 0; j < cSetChild; ++j) { - IXMLNode::TXMLNodePtr pNode = pSetting->GetChildNode(j); - tstring sNode = pNode->GetName(); - if (0 == mir_wstrcmpi(g_pszXmlName, sNode.c_str())) { - sName = pNode->GetText(); - } - else if (0 == mir_wstrcmpi(g_pszXmlValue, sNode.c_str())) { - sValue = pNode->GetText(); - sType = pNode->GetAttributeValue(g_pszXmlType); - } - } - - if ((false == sName.empty()) && (false == sType.empty())) { - std::string s1 = currencyrates_t2a(sName.c_str()); - dbs.szSetting = s1.c_str(); - if (0 == mir_wstrcmpi(g_pszXmlTypeByte, sType.c_str())) { - tistringstream in(sValue.c_str()); - in.imbue(GetSystemLocale()); - dbs.value.cVal = in.get(); - if (in.good() && in.eof()) { - dbs.value.type = DBVT_BYTE; - if (set_contact_settings(hContact, dbs)) - ++cCreatedRecords; - } - } - else if (0 == mir_wstrcmpi(g_pszXmlTypeWord, sType.c_str())) { - tistringstream in(sValue.c_str()); - in.imbue(GetSystemLocale()); - in >> dbs.value.wVal; - if (in.good() || in.eof()) { - dbs.value.type = DBVT_WORD; - if (set_contact_settings(hContact, dbs)) - ++cCreatedRecords; - } - } - else if (0 == mir_wstrcmpi(g_pszXmlTypeDword, sType.c_str())) { - tistringstream in(sValue.c_str()); - in.imbue(GetSystemLocale()); - in >> dbs.value.dVal; - if (in.good() || in.eof()) { - dbs.value.type = DBVT_DWORD; - if (set_contact_settings(hContact, dbs)) - ++cCreatedRecords; - } - } - else if (0 == mir_wstrcmpi(g_pszXmlTypeAsciiz, sType.c_str())) { - ptrA v(mir_u2a(sValue.c_str())); - dbs.value.pszVal = v; - dbs.value.type = DBVT_ASCIIZ; - if (set_contact_settings(hContact, dbs)) - ++cCreatedRecords; - } - else if (0 == mir_wstrcmpi(g_pszXmlTypeUtf8, sType.c_str())) { - T2Utf szValue(sValue.c_str()); - dbs.value.pszVal = szValue; - dbs.value.type = DBVT_UTF8; - if (set_contact_settings(hContact, dbs)) - ++cCreatedRecords; - } - else if (0 == mir_wstrcmpi(g_pszXmlTypeWchar, sType.c_str())) { - ptrW val(mir_wstrdup(sValue.c_str())); - dbs.value.pwszVal = val; - dbs.value.type = DBVT_WCHAR; - if (set_contact_settings(hContact, dbs)) - ++cCreatedRecords; - } - else if (0 == mir_wstrcmpi(g_pszXmlTypeBlob, sType.c_str())) { - size_t bufLen; - mir_ptr<BYTE> buf((PBYTE)mir_base64_decode(_T2A(sValue.c_str()), &bufLen)); - if (buf) { - dbs.value.pbVal = buf; - dbs.value.cpbVal = (WORD)bufLen; - dbs.value.type = DBVT_BLOB; - - if (set_contact_settings(hContact, dbs)) - ++cCreatedRecords; - } - } - - if ((true == bCListModule) && (0 == mir_wstrcmpi(sName.c_str(), L"Group"))) - Clist_GroupCreate(NULL, sValue.c_str()); - } - } + bool bCListModule = !mir_strcmpi(szModuleName, "CList"); + + DBCONTACTWRITESETTING dbs; + dbs.szModule = szModuleName; + for (auto *pSetting = pXmlModule->FirstChildElement(g_szXmlSetting); pSetting != nullptr; pSetting = pSetting->NextSiblingElement(g_szXmlSetting)) { + auto *pNode = pSetting->FirstChildElement(g_szXmlName); + if (pNode == nullptr) + continue; + const char *sName = pNode->GetText(); + + pNode = pSetting->FirstChildElement(g_szXmlValue); + if (pNode == nullptr) + continue; + const char *sValue = pNode->GetText(); + const char *sType = pNode->Attribute(g_szXmlType); + + if (sName == nullptr || sType == nullptr || sValue == nullptr) + continue; + + dbs.szSetting = sName; + if (!mir_strcmpi(g_szXmlTypeByte, sType)) { + std::istringstream in(sValue); + in.imbue(GetSystemLocale()); + dbs.value.cVal = in.get(); + if (in.good() && in.eof()) { + dbs.value.type = DBVT_BYTE; + if (set_contact_settings(hContact, dbs)) + ++cCreatedRecords; + } + } + else if (!mir_strcmpi(g_szXmlTypeWord, sType)) { + std::istringstream in(sValue); + in.imbue(GetSystemLocale()); + in >> dbs.value.wVal; + if (in.good() || in.eof()) { + dbs.value.type = DBVT_WORD; + if (set_contact_settings(hContact, dbs)) + ++cCreatedRecords; } } + else if (!mir_strcmpi(g_szXmlTypeDword, sType)) { + std::istringstream in(sValue); + in.imbue(GetSystemLocale()); + in >> dbs.value.dVal; + if (in.good() || in.eof()) { + dbs.value.type = DBVT_DWORD; + if (set_contact_settings(hContact, dbs)) + ++cCreatedRecords; + } + } + else if (!mir_strcmpi(g_szXmlTypeAsciiz, sType)) { + dbs.value.pszVal = (char*)sValue; + dbs.value.type = DBVT_ASCIIZ; + if (set_contact_settings(hContact, dbs)) + ++cCreatedRecords; + } + else if (!mir_strcmpi(g_szXmlTypeUtf8, sType)) { + dbs.value.pszVal = (char*)sValue; + dbs.value.type = DBVT_UTF8; + if (set_contact_settings(hContact, dbs)) + ++cCreatedRecords; + } + else if (!mir_strcmpi(g_szXmlTypeWchar, sType)) { + Utf2T val(sValue); + dbs.value.pwszVal = val; + dbs.value.type = DBVT_WCHAR; + if (set_contact_settings(hContact, dbs)) + ++cCreatedRecords; + } + else if (!mir_strcmpi(g_szXmlTypeBlob, sType)) { + size_t bufLen; + mir_ptr<BYTE> buf((PBYTE)mir_base64_decode(sValue, &bufLen)); + if (buf) { + dbs.value.pbVal = buf; + dbs.value.cpbVal = (WORD)bufLen; + dbs.value.type = DBVT_BLOB; + + if (set_contact_settings(hContact, dbs)) + ++cCreatedRecords; + } + } + + if (bCListModule && !mir_strcmpi(sName, "Group")) + Clist_GroupCreate(NULL, Utf2T(sValue)); } return true; } -size_t count_contacts(const IXMLNode::TXMLNodePtr& pXmlRoot, bool bInContactsGroup) +size_t count_contacts(const TiXmlNode *pXmlRoot, bool bInContactsGroup) { size_t cContacts = 0; - size_t cChild = pXmlRoot->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = pXmlRoot->GetChildNode(i); - tstring sName = pNode->GetName(); + + for (auto *pNode = pXmlRoot->FirstChildElement(); pNode != nullptr; pNode = pNode->NextSiblingElement()) { + const char *sName = pNode->Name(); if (false == bInContactsGroup) { - if (0 == mir_wstrcmpi(g_pszXmlContacts, sName.c_str())) + if (!mir_strcmpi(g_szXmlContacts, sName)) cContacts += count_contacts(pNode, true); else cContacts += count_contacts(pNode, false); } else { - if (0 == mir_wstrcmpi(g_pszXmlContact, sName.c_str())) + if (!mir_strcmpi(g_szXmlContact, sName)) ++cContacts; } } @@ -392,208 +376,154 @@ struct CContactState bool m_bNewContact; }; -IXMLNode::TXMLNodePtr find_currencyrates_module(const IXMLNode::TXMLNodePtr& pXmlContact) +const TiXmlNode* find_currencyrates_module(const TiXmlNode *pXmlContact) { - static const tstring g_sCurrencyRates = currencyrates_a2t(CURRENCYRATES_MODULE_NAME); - size_t cChild = pXmlContact->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = pXmlContact->GetChildNode(i); - tstring sName = pNode->GetName(); - if ((0 == mir_wstrcmpi(g_pszXmlModule, sName.c_str())) - && (0 == mir_wstrcmpi(g_sCurrencyRates.c_str(), pNode->GetText().c_str()))) { + for (auto *pNode = pXmlContact->FirstChildElement(); pNode != nullptr; pNode = pNode->NextSiblingElement()) + if ((!mir_strcmpi(g_szXmlModule, pNode->Name())) && (!mir_strcmpi(CURRENCYRATES_MODULE_NAME, pNode->GetText()))) return pNode; - } - } - return IXMLNode::TXMLNodePtr(); + return nullptr; } -typedef std::pair<tstring, tstring> TNameValue;//first is name,second is value -TNameValue parse_setting_node(const IXMLNode::TXMLNodePtr& pXmlSetting) +typedef std::pair<const char*, const char*> TNameValue; // first is name,second is value +TNameValue parse_setting_node(const TiXmlNode *pXmlSetting) { assert(pXmlSetting); - tstring sName, sValue; - size_t cSettingChildItems = pXmlSetting->GetChildCount(); - for (size_t j = 0; j < cSettingChildItems; ++j) { - IXMLNode::TXMLNodePtr pXMLSetChild = pXmlSetting->GetChildNode(j); - if (pXMLSetChild) { - if (0 == mir_wstrcmpi(g_pszXmlName, pXMLSetChild->GetName().c_str())) { - sName = pXMLSetChild->GetText(); - } - else if (0 == mir_wstrcmpi(g_pszXmlValue, pXMLSetChild->GetName().c_str())) { - sValue = pXMLSetChild->GetText(); - } - } + const char *sName, *sValue; + for (auto *pNode = pXmlSetting->FirstChildElement(); pNode != nullptr; pNode = pNode->NextSiblingElement()) { + if (!mir_strcmpi(g_szXmlName, pNode->Name())) + sName = pNode->GetText(); + else if (!mir_strcmpi(g_szXmlValue, pNode->Name())) + sValue = pNode->GetText(); } return std::make_pair(sName, sValue); } -CCurrencyRatesProviders::TCurrencyRatesProviderPtr find_provider(const IXMLNode::TXMLNodePtr& pXmlCurrencyRatesModule) +CCurrencyRatesProviders::TCurrencyRatesProviderPtr find_provider(const TiXmlNode *pXmlCurrencyRatesModule) { - // USES_CONVERSION; - static const tstring g_sCurrencyRatesProvider = currencyrates_a2t(DB_STR_CURRENCYRATE_PROVIDER);//A2CT(DB_STR_CURRENCYRATE_PROVIDER); - size_t cChild = pXmlCurrencyRatesModule->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pXMLSetting = pXmlCurrencyRatesModule->GetChildNode(i); - if (pXMLSetting && (0 == mir_wstrcmpi(g_pszXmlSetting, pXMLSetting->GetName().c_str()))) { - TNameValue Item = parse_setting_node(pXMLSetting); - if ((0 == mir_wstrcmpi(g_sCurrencyRatesProvider.c_str(), Item.first.c_str())) && (false == Item.second.empty())) { - return CModuleInfo::GetInstance().GetCurrencyRateProvidersPtr()->FindProvider(Item.second); - } - } + for (auto *pNode = pXmlCurrencyRatesModule->FirstChildElement(g_szXmlSetting); pNode != nullptr; pNode = pNode->NextSiblingElement(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 CCurrencyRatesProviders::TCurrencyRatesProviderPtr(); } -bool get_contact_state(const IXMLNode::TXMLNodePtr& pXmlContact, CContactState& cst) +bool get_contact_state(const TiXmlNode *pXmlContact, CContactState& cst) { class visitor : public CCurrencyRatesProviderVisitor { public: - visitor(const IXMLNode::TXMLNodePtr& pXmlCurrencyRates) + visitor(const TiXmlNode *pXmlCurrencyRates) : m_hContact(NULL), m_pXmlCurrencyRates(pXmlCurrencyRates) { } - MCONTACT GetContact()const { return m_hContact; } + MCONTACT GetContact() const { return m_hContact; } private: - virtual void Visit(const CCurrencyRatesProviderCurrencyConverter& rProvider)override + void Visit(const CCurrencyRatesProviderCurrencyConverter &rProvider) override { - static const tstring g_sFromID = currencyrates_a2t(DB_STR_FROM_ID);//A2CT(DB_STR_FROM_ID); - static const tstring g_sToID = currencyrates_a2t(DB_STR_TO_ID);//A2CT(DB_STR_TO_ID); - - tstring sFromID; - tstring sToID; - size_t cChild = m_pXmlCurrencyRates->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = m_pXmlCurrencyRates->GetChildNode(i); - if (pNode && (0 == mir_wstrcmpi(g_pszXmlSetting, pNode->GetName().c_str()))) { - TNameValue Item = parse_setting_node(pNode); - if (0 == mir_wstrcmpi(g_sFromID.c_str(), Item.first.c_str())) { - sFromID = Item.second; - } - else if (0 == mir_wstrcmpi(g_sToID.c_str(), Item.first.c_str())) { - sToID = Item.second; - } - } + const char *sFromID = nullptr, *sToID = nullptr; + + for (auto *pNode = m_pXmlCurrencyRates->FirstChildElement(g_szXmlSetting); pNode != nullptr; pNode = pNode->NextSiblingElement(g_szXmlSetting)) { + 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 ((false == sFromID.empty()) && (false == sToID.empty())) { - m_hContact = rProvider.GetContactByID(sFromID, sToID); - } + if (sFromID && sToID) + m_hContact = rProvider.GetContactByID(Utf2T(sFromID).get(), Utf2T(sToID).get()); } - tstring GetXMLNodeValue(const char* pszXMLNodeName)const + tstring GetXMLNodeValue(const char* pszXMLNodeName) const { - tstring sXMLNodeName = currencyrates_a2t(pszXMLNodeName); - - tstring sValue; - size_t cChild = m_pXmlCurrencyRates->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = m_pXmlCurrencyRates->GetChildNode(i); - if (pNode && (0 == mir_wstrcmpi(g_pszXmlSetting, pNode->GetName().c_str()))) { - TNameValue Item = parse_setting_node(pNode); - if (0 == mir_wstrcmpi(Item.first.c_str(), sXMLNodeName.c_str())) { - sValue = Item.second; - break; - } - } + for (auto *pNode = m_pXmlCurrencyRates->FirstChildElement(g_szXmlSetting); pNode != nullptr; pNode = pNode->NextSiblingElement(g_szXmlSetting)) { + TNameValue Item = parse_setting_node(pNode); + if (!mir_strcmpi(Item.first, pszXMLNodeName)) + return Utf2T(Item.second).get(); } - return sValue; + return tstring(); } private: MCONTACT m_hContact; - IXMLNode::TXMLNodePtr m_pXmlCurrencyRates; + const TiXmlNode *m_pXmlCurrencyRates; }; - IXMLNode::TXMLNodePtr pXmlCurrencyRates = find_currencyrates_module(pXmlContact); - if (pXmlCurrencyRates) { - cst.m_pProvider = find_provider(pXmlCurrencyRates); - if (cst.m_pProvider) { - visitor vs(pXmlCurrencyRates); - cst.m_pProvider->Accept(vs); - cst.m_hContact = vs.GetContact(); - return true; - } - } + auto *pXmlCurrencyRates = find_currencyrates_module(pXmlContact); + if (!pXmlCurrencyRates) + return false; + + cst.m_pProvider = find_provider(pXmlCurrencyRates); + if (!cst.m_pProvider) + return false; - return false; + visitor vs(pXmlCurrencyRates); + cst.m_pProvider->Accept(vs); + cst.m_hContact = vs.GetContact(); + return true; } -bool import_contact(const IXMLNode::TXMLNodePtr& pXmlContact, CImportContext& impctx) +bool import_contact(const TiXmlNode *pXmlContact, CImportContext &impctx) { ++impctx.m_cHandledContacts; CContactState cst; - bool bResult = get_contact_state(pXmlContact, cst); - if (bResult) { - if (NULL == cst.m_hContact) { - cst.m_hContact = db_add_contact(); - cst.m_bNewContact = true; - } - else if (impctx.m_nFlags & CURRENCYRATES_IMPORT_SKIP_EXISTING_CONTACTS) - return true; - - if (cst.m_hContact) { - size_t cChild = pXmlContact->GetChildCount(); - for (size_t i = 0; i < cChild && bResult; ++i) { - IXMLNode::TXMLNodePtr pNode = pXmlContact->GetChildNode(i); - tstring sName = pNode->GetName(); - if (0 == mir_wstrcmpi(g_pszXmlModule, sName.c_str())) - bResult &= handle_module(cst.m_hContact, pNode); - } + if (!get_contact_state(pXmlContact, cst)) + return false; - if (cst.m_bNewContact && bResult) { - cst.m_pProvider->AddContact(cst.m_hContact); - cst.m_pProvider->RefreshContact(cst.m_hContact); - } - } - else bResult = false; + if (NULL == cst.m_hContact) { + cst.m_hContact = db_add_contact(); + cst.m_bNewContact = true; } + else if (impctx.m_nFlags & CURRENCYRATES_IMPORT_SKIP_EXISTING_CONTACTS) + return true; - return bResult; + if (!cst.m_hContact) + return false; + + for (auto *pNode = pXmlContact->FirstChildElement(g_szXmlModule); pNode != nullptr; pNode = pNode->NextSiblingElement(g_szXmlModule)) + if (!handle_module(cst.m_hContact, pNode)) + return false; + + if (cst.m_bNewContact) { + cst.m_pProvider->AddContact(cst.m_hContact); + cst.m_pProvider->RefreshContact(cst.m_hContact); + } + return true; } -size_t import_contacts(const IXMLNode::TXMLNodePtr& pXmlContacts, CImportContext& impctx) +size_t import_contacts(const TiXmlNode *pXmlContacts, CImportContext &impctx) { size_t cContacts = 0; - size_t cChild = pXmlContacts->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = pXmlContacts->GetChildNode(i); - tstring sName = pNode->GetName(); - if (0 == mir_wstrcmpi(g_pszXmlContact, sName.c_str())) - if (true == import_contact(pNode, impctx)) - ++cContacts; - } + for (auto *pNode = pXmlContacts->FirstChildElement(g_szXmlContact); pNode != nullptr; pNode = pNode->NextSiblingElement(g_szXmlContact)) + if (import_contact(pNode, impctx)) + ++cContacts; return cContacts; - } -size_t handle_contacts_node(const IXMLNode::TXMLNodePtr& pXmlRoot, CImportContext& impctx) +size_t handle_contacts_node(const TiXmlNode *pXmlRoot, CImportContext& impctx) { size_t cContacts = 0; - size_t cChild = pXmlRoot->GetChildCount(); - for (size_t i = 0; i < cChild; ++i) { - IXMLNode::TXMLNodePtr pNode = pXmlRoot->GetChildNode(i); - tstring sName = pNode->GetName(); - if (0 == mir_wstrcmpi(g_pszXmlContacts, sName.c_str())) + for (auto *pNode = pXmlRoot->FirstChildElement(); pNode != nullptr; pNode = pNode->NextSiblingElement()) { + if (!mir_strcmpi(g_szXmlContacts, pNode->Name())) cContacts += import_contacts(pNode, impctx); else cContacts += handle_contacts_node(pNode, impctx); } return cContacts; - } -bool do_import(const IXMLNode::TXMLNodePtr& pXmlRoot, UINT nFlags) +bool do_import(const TiXmlNode *pXmlRoot, UINT nFlags) { CImportContext imctx(count_contacts(pXmlRoot, false)); imctx.m_cHandledContacts = 0; @@ -604,30 +534,23 @@ bool do_import(const IXMLNode::TXMLNodePtr& pXmlRoot, UINT nFlags) INT_PTR CurrencyRates_Import(WPARAM wp, LPARAM lp) { - // USES_CONVERSION; - tstring sFileName; const char* pszFile = reinterpret_cast<const char*>(lp); if (nullptr == pszFile) { if (false == show_open_file_dialog(true, sFileName)) return -1; } - else sFileName = currencyrates_a2t(pszFile);//A2CT(pszFile); - - CModuleInfo::TXMLEnginePtr pXmlEngine = CModuleInfo::GetInstance().GetXMLEnginePtr(); - IXMLNode::TXMLNodePtr pXmlRoot = pXmlEngine->LoadFile(sFileName); - if (pXmlRoot) - return ((true == do_import(pXmlRoot, wp)) ? 0 : 1); - - return 1; -} - -INT_PTR CurrencyRatesMenu_ImportAll(WPARAM, LPARAM) -{ - return CallService(MS_CURRENCYRATES_IMPORT, 0, 0); -} - -INT_PTR CurrencyRatesMenu_ExportAll(WPARAM, LPARAM) -{ - return CallService(MS_CURRENCYRATES_EXPORT, 0, 0); + else sFileName = currencyrates_a2t(pszFile); + + FILE *in = _wfopen(sFileName.c_str(), L"rb"); + if (in == nullptr) + return 1; + + TiXmlDocument doc; + int res = doc.LoadFile(in); + fclose(in); + if (res) + return 1; + + return (do_import(&doc, wp) ? 0 : 1); } diff --git a/plugins/CurrencyRates/src/ImportExport.h b/plugins/CurrencyRates/src/ImportExport.h index 3c167674ce..4fd3a42470 100644 --- a/plugins/CurrencyRates/src/ImportExport.h +++ b/plugins/CurrencyRates/src/ImportExport.h @@ -4,6 +4,4 @@ INT_PTR CurrencyRates_Export(WPARAM wp, LPARAM lp); INT_PTR CurrencyRates_Import(WPARAM wp, LPARAM lp); -INT_PTR CurrencyRatesMenu_ImportAll(WPARAM wp, LPARAM lp); -INT_PTR CurrencyRatesMenu_ExportAll(WPARAM wp, LPARAM lp); #endif //__F86374E6_713C_4600_85FB_903A5CDF7251_IMPORT_EXPORT_H__ diff --git a/plugins/CurrencyRates/src/ModuleInfo.cpp b/plugins/CurrencyRates/src/ModuleInfo.cpp index a44eda3d78..069f902238 100644 --- a/plugins/CurrencyRates/src/ModuleInfo.cpp +++ b/plugins/CurrencyRates/src/ModuleInfo.cpp @@ -1,34 +1,23 @@ #include "StdAfx.h" -static CModuleInfo::TXMLEnginePtr g_pXMLEngine; +static CModuleInfo mi; static CModuleInfo::THTMLEnginePtr g_pHTMLEngine; static mir_cs g_lmParsers; -CModuleInfo::CModuleInfo() -{ -} - -CModuleInfo::~CModuleInfo() -{ -} - -CModuleInfo& CModuleInfo::GetInstance() -{ - static CModuleInfo mi; - return mi; -} +typedef std::map<std::string, MWindowList> THandles; +static THandles g_ahWindowLists; MWindowList CModuleInfo::GetWindowList(const std::string& rsKey, bool bAllocateIfNonExist /*= true*/) { MWindowList hResult = nullptr; - THandles::const_iterator i = m_ahWindowLists.find(rsKey); - if (i != m_ahWindowLists.end()) { + THandles::const_iterator i = g_ahWindowLists.find(rsKey); + if (i != g_ahWindowLists.end()) { hResult = i->second; } else if (bAllocateIfNonExist) { hResult = WindowList_Create(); if (hResult) - m_ahWindowLists.insert(std::make_pair(rsKey, hResult)); + g_ahWindowLists.insert(std::make_pair(rsKey, hResult)); } return hResult; @@ -36,7 +25,7 @@ MWindowList CModuleInfo::GetWindowList(const std::string& rsKey, bool bAllocateI void CModuleInfo::OnMirandaShutdown() { - BOOST_FOREACH(THandles::value_type p, m_ahWindowLists) + BOOST_FOREACH(THandles::value_type p, g_ahWindowLists) { WindowList_Broadcast(p.second, WM_CLOSE, 0, 0); } @@ -48,17 +37,6 @@ CModuleInfo::TCurrencyRatesProvidersPtr CModuleInfo::GetCurrencyRateProvidersPtr return pProviders; } -CModuleInfo::TXMLEnginePtr CModuleInfo::GetXMLEnginePtr() -{ - if (!g_pXMLEngine) { - mir_cslock lck(g_lmParsers); - if (!g_pXMLEngine) - g_pXMLEngine = TXMLEnginePtr(new CXMLEngineMI); - } - - return g_pXMLEngine; -} - CModuleInfo::THTMLEnginePtr CModuleInfo::GetHTMLEngine() { if (!g_pHTMLEngine) { @@ -83,11 +61,6 @@ bool CModuleInfo::Verify() if (FALSE == ::InitCommonControlsEx(&icc)) return false; - if (!GetXMLEnginePtr()) { - CurrencyRates_MessageBox(nullptr, TranslateT("Miranda could not load CurrencyRates plugin. XML parser is missing."), MB_OK | MB_ICONERROR); - return false; - } - if (!g_pHTMLEngine && (false == CHTMLParserMS::IsInstalled())) { CurrencyRates_MessageBox(nullptr, TranslateT("Miranda could not load CurrencyRates plugin. Microsoft HTML parser is missing."), @@ -96,4 +69,4 @@ bool CModuleInfo::Verify() } return true; -}
\ No newline at end of file +} diff --git a/plugins/CurrencyRates/src/ModuleInfo.h b/plugins/CurrencyRates/src/ModuleInfo.h index b8ee955dfb..8b99a248d6 100644 --- a/plugins/CurrencyRates/src/ModuleInfo.h +++ b/plugins/CurrencyRates/src/ModuleInfo.h @@ -2,40 +2,24 @@ #define __d0f22b66_3135_4bbe_bee5_a31ea631ce58_ModuleInfo__ class CCurrencyRatesProviders; -class IXMLEngine; class IHTMLEngine; -// class IHTMLParser; class CModuleInfo { public: typedef boost::shared_ptr<CCurrencyRatesProviders> TCurrencyRatesProvidersPtr; - typedef boost::shared_ptr<IXMLEngine> TXMLEnginePtr; typedef boost::shared_ptr<IHTMLEngine> THTMLEnginePtr; -private: - CModuleInfo(); - ~CModuleInfo(void); - public: - static CModuleInfo& GetInstance(); - - void OnMirandaShutdown(); - MWindowList GetWindowList(const std::string& rsKey, bool bAllocateIfNonExist = true); + static void OnMirandaShutdown(void); + static MWindowList GetWindowList(const std::string& rsKey, bool bAllocateIfNonExist = true); static bool Verify(); static TCurrencyRatesProvidersPtr GetCurrencyRateProvidersPtr(); - static TXMLEnginePtr GetXMLEnginePtr(); - // static void SetXMLEnginePtr(TXMLEnginePtr pEngine); - static THTMLEnginePtr GetHTMLEngine(); static void SetHTMLEngine(THTMLEnginePtr pEngine); - -private: - typedef std::map<std::string, MWindowList> THandles; - THandles m_ahWindowLists; }; #endif //__d0f22b66_3135_4bbe_bee5_a31ea631ce58_ModuleInfo__ diff --git a/plugins/CurrencyRates/src/SettingsDlg.cpp b/plugins/CurrencyRates/src/SettingsDlg.cpp index fd79d43421..14c7dfb1fb 100644 --- a/plugins/CurrencyRates/src/SettingsDlg.cpp +++ b/plugins/CurrencyRates/src/SettingsDlg.cpp @@ -252,7 +252,7 @@ INT_PTR CALLBACK EditSettingsPerContactDlgProc(HWND hWnd, UINT msg, WPARAM wp, L { MCONTACT hContact = MCONTACT(lp); - MWindowList hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_SETTINGS, false); + MWindowList hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX_SETTINGS, false); assert(hWL); WindowList_Add(hWL, hWnd, hContact); @@ -450,7 +450,7 @@ INT_PTR CALLBACK EditSettingsPerContactDlgProc(HWND hWnd, UINT msg, WPARAM wp, L CSettingWindowParam* pParam = get_param(hWnd); SetWindowLongPtr(hWnd, GWLP_USERDATA, 0); - MWindowList hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_SETTINGS, false); + MWindowList hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX_SETTINGS, false); assert(hWL); WindowList_Remove(hWL, hWnd); Utils_SaveWindowPosition(hWnd, pParam->m_hContact, CURRENCYRATES_MODULE_NAME, WINDOW_PREFIX_SETTINGS); @@ -463,7 +463,7 @@ INT_PTR CALLBACK EditSettingsPerContactDlgProc(HWND hWnd, UINT msg, WPARAM wp, L void ShowSettingsDlg(MCONTACT hContact) { - MWindowList hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_SETTINGS, true); + MWindowList hWL = CModuleInfo::GetWindowList(WINDOW_PREFIX_SETTINGS, true); assert(hWL); HWND hWnd = WindowList_Find(hWL, hContact); if (nullptr != hWnd) { diff --git a/plugins/CurrencyRates/src/XMLEngineMI.cpp b/plugins/CurrencyRates/src/XMLEngineMI.cpp deleted file mode 100644 index 0224b2c7d9..0000000000 --- a/plugins/CurrencyRates/src/XMLEngineMI.cpp +++ /dev/null @@ -1,163 +0,0 @@ -#include "StdAfx.h" - -class CXMLNodeMI : public IXMLNode, private boost::noncopyable -{ -public: - typedef boost::shared_ptr<IXMLNode> TXMLNodePtr; - -public: - explicit CXMLNodeMI(HXML hXMl, bool bDestroy = false) : m_hXML(hXMl), m_bDestroy(bDestroy) - { - assert(m_hXML); - } - - virtual ~CXMLNodeMI() - { - if (m_bDestroy) - xmlDestroyNode(m_hXML); - } - - virtual size_t GetChildCount()const - { - return xmlGetChildCount(m_hXML); - } - - virtual TXMLNodePtr GetChildNode(size_t nIndex)const - { - HXML h = xmlGetChild(m_hXML, (int)nIndex); - if (h) - return TXMLNodePtr(new CXMLNodeMI(h)); - - return TXMLNodePtr(); - } - - virtual tstring GetText()const - { - tstring sResult; - LPCTSTR psz = xmlGetText(m_hXML); - if (psz) - sResult = psz; - - return sResult; - } - - virtual tstring GetName()const - { - tstring sResult; - LPCTSTR psz = xmlGetName(m_hXML); - if (psz) - sResult = psz; - - return sResult; - } - - virtual bool AddChild(const TXMLNodePtr& pNode) - { - CXMLNodeMI* pXML = dynamic_cast<CXMLNodeMI*>(pNode.get()); - if (pXML) { - xmlAddChild2(pXML->m_hXML, m_hXML); - pXML->m_bDestroy = false; - return true; - } - - return false; - } - - virtual bool AddAttribute(const tstring& rsName, const tstring& rsValue) - { - xmlAddAttr(m_hXML, rsName.c_str(), rsValue.c_str()); - return true; - } - - virtual tstring GetAttributeValue(const tstring& rsAttrName) - { - LPCTSTR pszValue = xmlGetAttrValue(m_hXML, rsAttrName.c_str()); - return ((nullptr != pszValue) ? tstring(pszValue) : tstring()); - } - - virtual void Write(tostream& o)const - { - ptrW ss(xmlToString(m_hXML, nullptr)); - if (ss != NULL) - o << (char*)T2Utf(ss); - } - -private: - HXML m_hXML; - bool m_bDestroy; -}; - -CXMLEngineMI::CXMLEngineMI() -{ -} - -CXMLEngineMI::~CXMLEngineMI() -{ -} - -IXMLNode::TXMLNodePtr CXMLEngineMI::LoadFile(const tstring& rsFileName)const -{ - IXMLNode::TXMLNodePtr pResult; - FILE *stream; - if (0 == ::_tfopen_s(&stream, rsFileName.c_str(), L"r")) { - struct _stat st; - if (-1 != ::_fstat(::_fileno(stream), &st)) { - std::vector<char> aBuffer(st.st_size + 1); - char* pBuffer = &*(aBuffer.begin()); - size_t cBytes = ::fread(pBuffer, sizeof(char), st.st_size, stream); - if (cBytes > 0 && cBytes <= static_cast<size_t>(st.st_size)) { - pBuffer[cBytes] = '\0'; - - int nLen = (int)cBytes; - ptrW ss(mir_utf8decodeW(pBuffer)); - if (ss) { - HXML h = xmlParseString(ss, &nLen, nullptr); - if (h) - pResult = IXMLNode::TXMLNodePtr(new CXMLNodeMI(h, true)); - } - } - } - ::fclose(stream); - } - - return pResult; -} - -static IXMLNode::TXMLNodePtr create_node(const tstring& rsName, const tstring& rsText, bool bIsDecl) -{ - IXMLNode::TXMLNodePtr pResult; - HXML h = xmlCreateNode(rsName.c_str(), rsText.c_str(), bIsDecl); - if (h) - pResult = IXMLNode::TXMLNodePtr(new CXMLNodeMI(h, true)); - - return pResult; -} - -bool CXMLEngineMI::SaveFile(const tstring& rsFileName, const IXMLNode::TXMLNodePtr& pNode)const -{ - CXMLNodeMI* pXML = dynamic_cast<CXMLNodeMI*>(pNode.get()); - if (pXML) { - tofstream file(rsFileName.c_str()); - if (file.good()) { - IXMLNode::TXMLNodePtr pRoot(create_node(L"xml", tstring(), true)); - if (pRoot) { - pRoot->AddAttribute(L"version", L"1.0"); - pRoot->AddAttribute(L"encoding", L"UTF-8"); - file << *pRoot; - } - - if (file.good()) { - file << *pNode; - } - } - - return file.good(); - } - - return false; -} - -IXMLNode::TXMLNodePtr CXMLEngineMI::CreateNode(const tstring& rsName, const tstring& rsText)const -{ - return create_node(rsName, rsText, false); -} diff --git a/plugins/CurrencyRates/src/XMLEngineMI.h b/plugins/CurrencyRates/src/XMLEngineMI.h deleted file mode 100644 index d9015613ab..0000000000 --- a/plugins/CurrencyRates/src/XMLEngineMI.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef __0c3d1da4_92b7_431c_83e5_f998cd513f0d_XMLEngineMI_h__ -#define __0c3d1da4_92b7_431c_83e5_f998cd513f0d_XMLEngineMI_h__ - -class CXMLEngineMI : public IXMLEngine -{ -public: - CXMLEngineMI(); - ~CXMLEngineMI(); - - virtual IXMLNode::TXMLNodePtr LoadFile(const tstring& rsFileName)const; - virtual bool SaveFile(const tstring& rsFileName, const IXMLNode::TXMLNodePtr& pNode)const; - virtual IXMLNode::TXMLNodePtr CreateNode(const tstring& rsName, const tstring& rsText)const; -}; - -#endif //__0c3d1da4_92b7_431c_83e5_f998cd513f0d_XMLEngineMI_h__ diff --git a/plugins/CurrencyRates/src/stdafx.h b/plugins/CurrencyRates/src/stdafx.h index 1aa1e5533b..62c7306491 100644 --- a/plugins/CurrencyRates/src/stdafx.h +++ b/plugins/CurrencyRates/src/stdafx.h @@ -109,8 +109,6 @@ inline tstring currencyrates_a2t(const char* s) #include "IHTMLParser.h" #include "IHTMLEngine.h" #include "HTMLParserMS.h" -#include "IXMLEngine.h" -#include "XMLEngineMI.h" struct CMPlugin : public PLUGIN<CMPlugin> { diff --git a/plugins/CurrencyRates/src/version.h b/plugins/CurrencyRates/src/version.h index 3e294ffd83..603fea2945 100644 --- a/plugins/CurrencyRates/src/version.h +++ b/plugins/CurrencyRates/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 -#define __MINOR_VERSION 1 +#define __MINOR_VERSION 2 #define __RELEASE_NUM 0 -#define __BUILD_NUM 101 +#define __BUILD_NUM 1 #include <stdver.h> |