From 0dfd93b286dd2ab1caf7fcaacbce7273c492433b Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 20 Feb 2019 13:29:24 +0300 Subject: Jabber: - fixes #1855 (Jabber: chinese in services info); - fixes #1854 (Jabber: crash on receiving channels list); - strange obsolete class UserInfoStringBuf removed; - code optimization --- protocols/JabberG/src/jabber_chat.cpp | 10 ++--- protocols/JabberG/src/jabber_disco.cpp | 60 ++++++++++++++---------------- protocols/JabberG/src/jabber_disco.h | 35 +++++++++-------- protocols/JabberG/src/jabber_proto.h | 4 +- protocols/JabberG/src/jabber_userinfo.cpp | 62 ++++++------------------------- protocols/JabberG/src/jabber_util.cpp | 17 +++++++++ protocols/JabberG/src/jabber_xml.h | 4 ++ protocols/JabberG/src/stdafx.h | 4 +- 8 files changed, 87 insertions(+), 109 deletions(-) (limited to 'protocols/JabberG/src') diff --git a/protocols/JabberG/src/jabber_chat.cpp b/protocols/JabberG/src/jabber_chat.cpp index 73942a6398..309eff37af 100644 --- a/protocols/JabberG/src/jabber_chat.cpp +++ b/protocols/JabberG/src/jabber_chat.cpp @@ -1175,16 +1175,16 @@ static void sttNickListHook(CJabberProto *ppro, JABBER_LIST_ITEM *item, GCHOOK* break; case IDM_CPY_NICK: - JabberCopyText(g_clistApi.hwndContactList, Utf2T(him->m_szResourceName)); + JabberCopyText(g_clistApi.hwndContactList, him->m_szResourceName); break; case IDM_RJID_COPY: case IDM_CPY_RJID: - JabberCopyText(g_clistApi.hwndContactList, Utf2T(him->m_szRealJid)); + JabberCopyText(g_clistApi.hwndContactList, him->m_szRealJid); break; case IDM_CPY_INROOMJID: - JabberCopyText(g_clistApi.hwndContactList, CMStringW(FORMAT, L"%s/%s", Utf2T(item->jid).get(), Utf2T(him->m_szResourceName).get())); + JabberCopyText(g_clistApi.hwndContactList, CMStringA(FORMAT, "%s/%s", item->jid, him->m_szResourceName)); break; case IDM_RJID_VCARD: @@ -1331,11 +1331,11 @@ static void sttLogListHook(CJabberProto *ppro, JABBER_LIST_ITEM *item, GCHOOK* g break; case IDM_CPY_RJID: - JabberCopyText(g_clistApi.hwndContactList, Utf2T(item->jid)); + JabberCopyText(g_clistApi.hwndContactList, item->jid); break; case IDM_CPY_TOPIC: - JabberCopyText(g_clistApi.hwndContactList, Utf2T(item->getTemp()->m_szStatusMessage)); + JabberCopyText(g_clistApi.hwndContactList, item->getTemp()->m_szStatusMessage); break; } } diff --git a/protocols/JabberG/src/jabber_disco.cpp b/protocols/JabberG/src/jabber_disco.cpp index f87fc6697e..9f68975dc0 100644 --- a/protocols/JabberG/src/jabber_disco.cpp +++ b/protocols/JabberG/src/jabber_disco.cpp @@ -189,7 +189,7 @@ void CJabberProto::OnIqResultServiceDiscoveryRootInfo(const TiXmlElement *iqNode for (auto *feature : TiXmlFilter(iqNode->FirstChildElement("query"), "feature")) { if (!mir_strcmp(feature->Attribute("var"), (char*)pInfo->m_pUserData)) { CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(pInfo->GetReceiver(), iqNode->Attribute("node"), nullptr); - SendBothRequests(pNode, nullptr); + SendBothRequests(pNode); break; } } @@ -225,7 +225,7 @@ void CJabberProto::OnIqResultServiceDiscoveryRootItems(const TiXmlElement *iqNod m_ThreadInfo->send(packet.ToElement()); } -BOOL CJabberProto::SendInfoRequest(CJabberSDNode *pNode, TiXmlElement *parent) +BOOL CJabberProto::SendInfoRequest(CJabberSDNode *pNode, TiXmlNode *parent) { if (!pNode || !m_bJabberOnline) return FALSE; @@ -242,7 +242,7 @@ BOOL CJabberProto::SendInfoRequest(CJabberSDNode *pNode, TiXmlElement *parent) query->SetAttribute("node", pNode->GetNode()); if (parent) - parent->InsertEndChild(iq); + parent->InsertEndChild(iq.node()->DeepClone(parent->GetDocument())); else m_ThreadInfo->send(iq); } @@ -255,7 +255,7 @@ BOOL CJabberProto::SendInfoRequest(CJabberSDNode *pNode, TiXmlElement *parent) return TRUE; } -BOOL CJabberProto::SendBothRequests(CJabberSDNode *pNode, TiXmlElement *parent) +BOOL CJabberProto::SendBothRequests(CJabberSDNode *pNode, TiXmlNode *parent) { if (!pNode || !m_bJabberOnline) return FALSE; @@ -272,7 +272,7 @@ BOOL CJabberProto::SendBothRequests(CJabberSDNode *pNode, TiXmlElement *parent) query->SetAttribute("node", pNode->GetNode()); if (parent) - parent->InsertEndChild(iq); + parent->InsertEndChild(iq.node()->DeepClone(parent->GetDocument())); else m_ThreadInfo->send(iq); } @@ -289,7 +289,7 @@ BOOL CJabberProto::SendBothRequests(CJabberSDNode *pNode, TiXmlElement *parent) query->SetAttribute("node", pNode->GetNode()); if (parent) - parent->InsertEndChild(iq); + parent->InsertEndChild(iq.node()->DeepClone(parent->GetDocument())); else m_ThreadInfo->send(iq); } @@ -337,7 +337,7 @@ void CJabberProto::PerformBrowse(HWND hwndDlg) m_lstTransports.insert(mir_strdup(item->jid)); CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(item->jid, nullptr, nullptr); - SendBothRequests(pNode, nullptr); + SendBothRequests(pNode); } } } @@ -375,13 +375,13 @@ void CJabberProto::PerformBrowse(HWND hwndDlg) mir_snprintf(setting, "discoWnd_favNode_%d", i); ptrA dbvNode(getUStringA(setting)); CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(dbvJid, dbvNode, tszName); - SendBothRequests(pNode, nullptr); + SendBothRequests(pNode); } } else { sttBrowseMode = SD_BROWSE_NORMAL; CJabberSDNode *pNode = m_SDManager.AddPrimaryNode(T2Utf(szJid), T2Utf(szNode), nullptr); - SendBothRequests(pNode, nullptr); + SendBothRequests(pNode); } lck.unlock(); @@ -810,7 +810,7 @@ public: if (pNode) { TreeList_ResetItem(GetDlgItem(m_hwnd, IDC_TREE_DISCO), hItem); pNode->ResetInfo(); - m_proto->SendBothRequests(pNode, packet.ToElement()); + m_proto->SendBothRequests(pNode, &packet); TreeList_MakeFakeParent(hItem, FALSE); } lck.unlock(); @@ -921,11 +921,11 @@ public: if (!pNode || pNode->GetInfoRequestId()) continue; - m_proto->SendInfoRequest(pNode, packet.ToElement()); + m_proto->SendInfoRequest(pNode, &packet); } } - if (packet.FirstChildElement()) - m_proto->m_ThreadInfo->send(packet.ToElement()); + if (!packet.NoChildren()) + m_proto->m_ThreadInfo->send(packet.RootElement()); KillTimer(m_hwnd, AUTODISCO_TIMER); m_proto->m_dwSDLastRefresh = GetTickCount(); @@ -973,22 +973,22 @@ public: mir_cslock lck(m_proto->m_SDManager.cs()); CJabberSDNode *pNode = (CJabberSDNode*)TreeList_GetData(hItem); if (pNode) - pNode->GetTooltipText(pInfoTip->pszText, pInfoTip->cchTextMax); + wcsncpy_s(pInfoTip->pszText, pInfoTip->cchTextMax, Utf2T(pNode->GetTooltipText()), _TRUNCATE); } else if (pHeader->code == TVN_ITEMEXPANDED) { NMTREEVIEW *pNmTreeView = (NMTREEVIEW *)lParam; HTREELISTITEM hItem = (HTREELISTITEM)pNmTreeView->itemNew.hItem; - XmlNode packet(0); + TiXmlDocument packet; { mir_cslock lck(m_proto->m_SDManager.cs()); CJabberSDNode *pNode = (CJabberSDNode*)TreeList_GetData(hItem); if (pNode) { - m_proto->SendBothRequests(pNode, packet); + m_proto->SendBothRequests(pNode, &packet); TreeList_MakeFakeParent(hItem, FALSE); } } if (packet.FirstChildElement()) - m_proto->m_ThreadInfo->send(packet); + m_proto->m_ThreadInfo->send(packet.RootElement()); } else if (pHeader->code == NM_CUSTOMDRAW) { LPNMLVCUSTOMDRAW lpnmlvcd = (LPNMLVCUSTOMDRAW)lParam; @@ -1178,24 +1178,24 @@ void CJabberProto::ServiceDiscoveryShowMenu(CJabberSDNode *pNode, HTREELISTITEM switch (res) { case SD_ACT_REFRESH: { - XmlNode packet(nullptr); + TiXmlDocument packet; { mir_cslock lck(m_SDManager.cs()); if (pNode) { TreeList_ResetItem(GetDlgItem(m_pDlgServiceDiscovery->GetHwnd(), IDC_TREE_DISCO), hItem); pNode->ResetInfo(); - SendBothRequests(pNode, packet); + SendBothRequests(pNode, &packet); TreeList_MakeFakeParent(hItem, FALSE); } } if (!packet.NoChildren()) - m_ThreadInfo->send(packet); + m_ThreadInfo->send(packet.RootElement()); } break; case SD_ACT_REFRESHCHILDREN: { - XmlNode packet(nullptr); + TiXmlDocument packet; { mir_cslock lck(m_SDManager.cs()); for (int iChild = TreeList_GetChildrenCount(hItem); iChild--;) { @@ -1204,36 +1204,32 @@ void CJabberProto::ServiceDiscoveryShowMenu(CJabberSDNode *pNode, HTREELISTITEM if (n) { TreeList_ResetItem(GetDlgItem(m_pDlgServiceDiscovery->GetHwnd(), IDC_TREE_DISCO), hNode); n->ResetInfo(); - SendBothRequests(n, packet); + SendBothRequests(n, &packet); TreeList_MakeFakeParent(hNode, FALSE); } - if (XmlGetChildCount(packet) > 50) { - m_ThreadInfo->send(packet); + if (XmlGetChildCount(packet.RootElement()) > 50) { + m_ThreadInfo->send(packet.RootElement()); packet.Clear(); } } } if (!packet.NoChildren()) - m_ThreadInfo->send(packet.ToElement()); + m_ThreadInfo->send(packet.RootElement()); } break; case SD_ACT_COPYJID: - JabberCopyText(m_pDlgServiceDiscovery->GetHwnd(), Utf2T(pNode->GetJid())); + JabberCopyText(m_pDlgServiceDiscovery->GetHwnd(), pNode->GetJid()); break; case SD_ACT_COPYNODE: - JabberCopyText(m_pDlgServiceDiscovery->GetHwnd(), Utf2T(pNode->GetNode())); + JabberCopyText(m_pDlgServiceDiscovery->GetHwnd(), pNode->GetNode()); break; case SD_ACT_COPYINFO: - { - wchar_t buf[8192]; - pNode->GetTooltipText(buf, _countof(buf)); - JabberCopyText(m_pDlgServiceDiscovery->GetHwnd(), buf); - } + JabberCopyText(m_pDlgServiceDiscovery->GetHwnd(), pNode->GetTooltipText()); break; case SD_ACT_FAVORITE: diff --git a/protocols/JabberG/src/jabber_disco.h b/protocols/JabberG/src/jabber_disco.h index 418c7ed178..4edb47c5ae 100644 --- a/protocols/JabberG/src/jabber_disco.h +++ b/protocols/JabberG/src/jabber_disco.h @@ -347,51 +347,50 @@ public: return TRUE; } - BOOL GetTooltipText(wchar_t *szText, int nMaxLength) + CMStringA GetTooltipText() { - CMStringW tszTmp; + CMStringA ret; - tszTmp.AppendFormat(L"Jid: %s\r\n", m_szJid); + ret.AppendFormat("Jid: %s\r\n", m_szJid); if (m_szNode) - tszTmp.AppendFormat(L"%s: %s\r\n", TranslateT("Node"), m_szNode); + ret.AppendFormat("%s: %s\r\n", TranslateU("Node"), m_szNode); if (m_pIdentities) { - tszTmp.AppendFormat(L"\r\n%s:\r\n", TranslateT("Identities")); + ret.AppendFormat("\r\n%s:\r\n", TranslateU("Identities")); CJabberSDIdentity *pIdentity = m_pIdentities; while (pIdentity) { if (pIdentity->GetName()) - tszTmp.AppendFormat(L" %c %s (%s: %s, %s: %s)\r\n", + ret.AppendFormat(" %c %s (%s: %s, %s: %s)\r\n", CHR_BULLET, pIdentity->GetName(), - TranslateT("category"), pIdentity->GetCategory(), - TranslateT("type"), pIdentity->GetType()); + TranslateU("category"), pIdentity->GetCategory(), + TranslateU("type"), pIdentity->GetType()); else - tszTmp.AppendFormat(L" %c %s: %s, %s: %s\r\n", + ret.AppendFormat(" %c %s: %s, %s: %s\r\n", CHR_BULLET, - TranslateT("Category"), pIdentity->GetCategory(), - TranslateT("Type"), pIdentity->GetType()); + TranslateU("Category"), pIdentity->GetCategory(), + TranslateU("Type"), pIdentity->GetType()); pIdentity = pIdentity->GetNext(); } } if (m_pFeatures) { - tszTmp.AppendFormat(L"\r\n%s:\r\n", TranslateT("Supported features")); + ret.AppendFormat("\r\n%s:\r\n", TranslateU("Supported features")); for (CJabberSDFeature *pFeature = m_pFeatures; pFeature; pFeature = pFeature->GetNext()) - tszTmp.AppendFormat(L" %c %s\r\n", CHR_BULLET, pFeature->GetVar()); + ret.AppendFormat(" %c %s\r\n", CHR_BULLET, pFeature->GetVar()); } if (m_szInfoError) - tszTmp.AppendFormat(L"\r\n%s: %s\r\n", TranslateT("Info request error"), m_szInfoError); + ret.AppendFormat("\r\n%s: %s\r\n", TranslateU("Info request error"), T2Utf(m_szInfoError).get()); if (m_szItemsError) - tszTmp.AppendFormat(L"\r\n%s: %s\r\n", TranslateT("Items request error"), m_szItemsError); + ret.AppendFormat("\r\n%s: %s\r\n", TranslateU("Items request error"), T2Utf(m_szItemsError).get()); - tszTmp.TrimRight(); - wcsncpy_s(szText, nMaxLength, tszTmp, _TRUNCATE); - return TRUE; + ret.TrimRight(); + return ret; } }; diff --git a/protocols/JabberG/src/jabber_proto.h b/protocols/JabberG/src/jabber_proto.h index d21b7a0078..d0f42a7731 100755 --- a/protocols/JabberG/src/jabber_proto.h +++ b/protocols/JabberG/src/jabber_proto.h @@ -434,8 +434,8 @@ struct CJabberProto : public PROTO, public IJabberInterface void OnIqResultServiceDiscoveryItems(const TiXmlElement *iqNode, CJabberIqInfo *pInfo); void OnIqResultServiceDiscoveryRootInfo(const TiXmlElement *iqNode, CJabberIqInfo *pInfo); void OnIqResultServiceDiscoveryRootItems(const TiXmlElement *iqNode, CJabberIqInfo *pInfo); - BOOL SendInfoRequest(CJabberSDNode *pNode, TiXmlElement *parent); - BOOL SendBothRequests(CJabberSDNode *pNode, TiXmlElement *parent); + BOOL SendInfoRequest(CJabberSDNode *pNode, TiXmlNode *parent); + BOOL SendBothRequests(CJabberSDNode *pNode, TiXmlNode *parent = nullptr); void PerformBrowse(HWND hwndDlg); BOOL IsNodeRegistered(CJabberSDNode *pNode); void ApplyNodeIcon(HTREELISTITEM hItem, CJabberSDNode *pNode); diff --git a/protocols/JabberG/src/jabber_userinfo.cpp b/protocols/JabberG/src/jabber_userinfo.cpp index 13ef901a18..1dd2bcc246 100755 --- a/protocols/JabberG/src/jabber_userinfo.cpp +++ b/protocols/JabberG/src/jabber_userinfo.cpp @@ -33,44 +33,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static MWindowList hUserInfoList = nullptr; -struct UserInfoStringBuf -{ - enum { STRINGBUF_INCREMENT = 1024 }; - - wchar_t *buf; - size_t size, offset; - - UserInfoStringBuf() { buf = nullptr; size = 0; offset = 0; } - ~UserInfoStringBuf() { mir_free(buf); } - - void append(wchar_t *str) - { - if (!str) return; - - size_t length = mir_wstrlen(str); - if (size - offset < length + 1) { - size += (length + STRINGBUF_INCREMENT); - buf = (wchar_t *)mir_realloc(buf, size * sizeof(wchar_t)); - } - mir_wstrcpy(buf + offset, str); - offset += length; - } - - wchar_t* allocate(int length) - { - if (size - offset < length) { - size += (length + STRINGBUF_INCREMENT); - buf = (wchar_t *)mir_realloc(buf, size * sizeof(wchar_t)); - } - return buf + offset; - } - - void actualize() - { - if (buf) offset = mir_wstrlen(buf); - } -}; - ///////////////////////////////////////////////////////////////////////////////////////// // JabberUserInfoDlgProc - main user info dialog @@ -406,24 +368,22 @@ static void sttFillUserInfo(CJabberProto *ppro, HWND hwndTree, JABBER_LIST_ITEM RedrawWindow(hwndTree, nullptr, nullptr, RDW_INVALIDATE); } -static void sttGetNodeText(HWND hwndTree, HTREEITEM hti, UserInfoStringBuf *buf, int indent = 0) +static void sttGetNodeText(HWND hwndTree, HTREEITEM hti, CMStringW &buf, int indent = 0) { for (int i = 0; i < indent; i++) - buf->append(L"\t"); + buf.AppendChar('\t'); + wchar_t wszText[256]; TVITEMEX tvi = { 0 }; tvi.mask = TVIF_HANDLE | TVIF_TEXT | TVIF_STATE; tvi.hItem = hti; - tvi.cchTextMax = 256; - tvi.pszText = buf->allocate(tvi.cchTextMax); - if (!TreeView_GetItem(hwndTree, &tvi)) { // failure, maybe item was removed... - buf->buf[buf->offset] = 0; - buf->actualize(); + tvi.cchTextMax = _countof(wszText); + tvi.pszText = wszText; + if (!TreeView_GetItem(hwndTree, &tvi)) // failure, maybe item was removed... return; - } - buf->actualize(); - buf->append(L"\r\n"); + buf.Append(wszText); + buf.Append(L"\r\n"); if (tvi.state & TVIS_EXPANDED) for (hti = TreeView_GetChild(hwndTree, hti); hti; hti = TreeView_GetNextSibling(hwndTree, hti)) @@ -527,9 +487,9 @@ static INT_PTR CALLBACK JabberUserInfoDlgProc(HWND hwndDlg, UINT msg, WPARAM wPa AppendMenu(hMenu, MF_STRING, (UINT_PTR)0, TranslateT("Cancel")); int nReturnCmd = TrackPopupMenu(hMenu, TPM_RETURNCMD, pt.x, pt.y, 0, hwndDlg, nullptr); if (nReturnCmd == 1) { - UserInfoStringBuf buf; - sttGetNodeText(hwndTree, hItem, &buf); - JabberCopyText(hwndDlg, buf.buf); + CMStringW buf; + sttGetNodeText(hwndTree, hItem, buf); + JabberCopyText(hwndDlg, buf); } else if (nReturnCmd == 2) { wchar_t szBuffer[1024]; diff --git a/protocols/JabberG/src/jabber_util.cpp b/protocols/JabberG/src/jabber_util.cpp index dceac9a0db..659d3b391f 100755 --- a/protocols/JabberG/src/jabber_util.cpp +++ b/protocols/JabberG/src/jabber_util.cpp @@ -769,6 +769,23 @@ const wchar_t *JabberStrIStr(const wchar_t *str, const wchar_t *substr) //////////////////////////////////////////////////////////////////////// // clipboard processing +void JabberCopyText(HWND hwnd, const char *pszText) +{ + if (!hwnd || !pszText) return; + + if (OpenClipboard(hwnd)) { + Utf2T text(pszText); + + EmptyClipboard(); + HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(wchar_t)*(mir_wstrlen(text) + 1)); + wchar_t *s = (wchar_t *)GlobalLock(hMem); + mir_wstrcpy(s, text); + GlobalUnlock(hMem); + SetClipboardData(CF_UNICODETEXT, hMem); + CloseClipboard(); + } +} + void JabberCopyText(HWND hwnd, const wchar_t *text) { if (!hwnd || !text) return; diff --git a/protocols/JabberG/src/jabber_xml.h b/protocols/JabberG/src/jabber_xml.h index b21738e870..69bff6c7e1 100644 --- a/protocols/JabberG/src/jabber_xml.h +++ b/protocols/JabberG/src/jabber_xml.h @@ -47,6 +47,10 @@ public: XmlNode(const char *name); XmlNode(const char *pszName, const char *ptszText); + __forceinline TiXmlElement* node() const + { return m_hXml; + } + __forceinline operator TiXmlElement*() { return m_hXml; } diff --git a/protocols/JabberG/src/stdafx.h b/protocols/JabberG/src/stdafx.h index ef24d224ae..79dfd30f72 100755 --- a/protocols/JabberG/src/stdafx.h +++ b/protocols/JabberG/src/stdafx.h @@ -741,8 +741,10 @@ wchar_t* JabberStrFixLines(const wchar_t *str); wchar_t* JabberErrorStr(int errorCode); CMStringW JabberErrorMsg(const TiXmlElement *errorNode, int *errorCode = nullptr); +void JabberCopyText(HWND hwnd, const char *text); +void JabberCopyText(HWND hwnd, const wchar_t *text); + const wchar_t *JabberStrIStr(const wchar_t *str, const wchar_t *substr); -void JabberCopyText(HWND hwnd, const wchar_t *text); CJabberProto* JabberChooseInstance(bool bIsLink=false); bool JabberReadXep203delay(const TiXmlElement *node, time_t &msgTime); -- cgit v1.2.3