From 2d737d50469b965c2787823a94757f4c9f0a7107 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 9 Mar 2017 17:28:47 +0300 Subject: chat API: - SESSION_INFO now addresses an instance of CChatRoomDlg instead of HWND; - linked list of sessions replaced with the usual LIST<>; - saveCI removed everywhere --- src/core/stdmsg/src/chat_manager.cpp | 92 +++++++++------------- src/core/stdmsg/src/chat_util.cpp | 11 ++- src/core/stdmsg/src/chat_window.cpp | 14 ++-- src/core/stdmsg/src/globals.cpp | 7 +- src/core/stdmsg/src/stdafx.h | 2 - src/core/stdmsg/src/tabs.cpp | 30 +++---- src/mir_app/src/chat.h | 8 +- src/mir_app/src/chat_clist.cpp | 4 +- src/mir_app/src/chat_manager.cpp | 147 +++++++++++++++++++---------------- src/mir_app/src/chat_svc.cpp | 45 ++++++----- src/mir_app/src/chat_tools.cpp | 15 ++-- src/mir_app/src/netlibsock.cpp | 2 +- 12 files changed, 188 insertions(+), 189 deletions(-) (limited to 'src') diff --git a/src/core/stdmsg/src/chat_manager.cpp b/src/core/stdmsg/src/chat_manager.cpp index 0041d3ea6f..c0e881954d 100644 --- a/src/core/stdmsg/src/chat_manager.cpp +++ b/src/core/stdmsg/src/chat_manager.cpp @@ -22,56 +22,42 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "stdafx.h" +pfnDoTrayIcon oldDoTrayIcon; +pfnDoPopup oldDoPopup; + SESSION_INFO* SM_GetPrevWindow(SESSION_INFO *si) { - if (!si) - return NULL; - - BOOL bFound = FALSE; - SESSION_INFO *pTemp = pci->wndList; - while (pTemp != NULL) { - if (si == pTemp) { - if (bFound) - return NULL; - else - bFound = TRUE; - } - else if (bFound == TRUE && pTemp->hWnd) - return pTemp; - pTemp = pTemp->next; - if (pTemp == NULL && bFound) - pTemp = pci->wndList; + int i = pci->arSessions.indexOf(si); + if (i == -1) + return nullptr; + + for (i--; i >= 0; i--) { + SESSION_INFO *p = pci->arSessions[i]; + if (p->pDlg) + return p; } - return NULL; + + return nullptr; } SESSION_INFO* SM_GetNextWindow(SESSION_INFO *si) { - if (!si) - return NULL; - - SESSION_INFO *pTemp = pci->wndList, *pLast = NULL; - while (pTemp != NULL) { - if (si == pTemp) { - if (pLast) { - if (pLast != pTemp) - return pLast; - else - return NULL; - } - } - if (pTemp->hWnd) - pLast = pTemp; - pTemp = pTemp->next; - if (pTemp == NULL) - pTemp = pci->wndList; + int i = pci->arSessions.indexOf(si); + if (i == -1) + return nullptr; + + for (i++; i < pci->arSessions.getCount(); i++) { + SESSION_INFO *p = pci->arSessions[i]; + if (p->pDlg) + return p; } - return NULL; + + return nullptr; } ///////////////////////////////////////////////////////////////////////////////////////// -CHAT_MANAGER *pci, saveCI; +CHAT_MANAGER *pci; HMENU g_hMenu = NULL; @@ -95,14 +81,14 @@ static void OnCreateModule(MODULEINFO *mi) static void OnReplaceSession(SESSION_INFO *si) { - if (si->hWnd) - RedrawWindow(GetDlgItem(si->hWnd, IDC_LIST), NULL, NULL, RDW_INVALIDATE); + if (si->pDlg) + RedrawWindow(GetDlgItem(si->pDlg->GetHwnd(), IDC_LIST), NULL, NULL, RDW_INVALIDATE); } static void OnNewUser(SESSION_INFO *si, USERINFO*) { - if (si->hWnd) - SendMessage(si->hWnd, GC_UPDATENICKLIST, 0, 0); + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_UPDATENICKLIST, 0, 0); } static void OnFlashHighlight(SESSION_INFO *si, int bInactive) @@ -110,10 +96,10 @@ static void OnFlashHighlight(SESSION_INFO *si, int bInactive) if (!bInactive) return; - if (!g_Settings.bTabsEnable && si->hWnd && g_Settings.bFlashWindowHighlight) - SetTimer(si->hWnd, TIMERID_FLASHWND, 900, NULL); + if (!g_Settings.bTabsEnable && si->pDlg && g_Settings.bFlashWindowHighlight) + SetTimer(si->pDlg->GetHwnd(), TIMERID_FLASHWND, 900, NULL); if (g_Settings.bTabsEnable && si->pDlg) - SendMessage(si->hWnd, GC_SETMESSAGEHIGHLIGHT, 0, (LPARAM)si->pDlg); + SendMessage(si->pDlg->GetHwnd(), GC_SETMESSAGEHIGHLIGHT, 0, (LPARAM)si->pDlg); } static void OnFlashWindow(SESSION_INFO *si, int bInactive) @@ -121,23 +107,23 @@ static void OnFlashWindow(SESSION_INFO *si, int bInactive) if (!bInactive) return; - if (!g_Settings.bTabsEnable && si->hWnd && g_Settings.bFlashWindow) - SetTimer(si->hWnd, TIMERID_FLASHWND, 900, NULL); + if (!g_Settings.bTabsEnable && si->pDlg && g_Settings.bFlashWindow) + SetTimer(si->pDlg->GetHwnd(), TIMERID_FLASHWND, 900, NULL); if (g_Settings.bTabsEnable && si->pDlg) - SendMessage(si->hWnd, GC_SETTABHIGHLIGHT, 0, (LPARAM)si->pDlg); + SendMessage(si->pDlg->GetHwnd(), GC_SETTABHIGHLIGHT, 0, (LPARAM)si->pDlg); } static BOOL DoTrayIcon(SESSION_INFO *si, GCEVENT *gce) { if (gce->pDest->iType & g_Settings.dwTrayIconFlags) - return saveCI.DoTrayIcon(si, gce); + return oldDoTrayIcon(si, gce); return TRUE; } static BOOL DoPopup(SESSION_INFO *si, GCEVENT *gce) { if (gce->pDest->iType & g_Settings.dwPopupFlags) - return saveCI.DoPopup(si, gce); + return oldDoPopup(si, gce); return TRUE; } @@ -212,10 +198,8 @@ void Load_ChatModule() CHAT_MANAGER_INITDATA data = { &g_Settings, sizeof(MODULEINFO), sizeof(SESSION_INFO), LPGENW("Chat module"), FONTMODE_SKIP }; pci = Chat_GetInterface(&data); - saveCI = *pci; pci->OnNewUser = OnNewUser; - pci->OnCreateModule = OnCreateModule; pci->OnReplaceSession = OnReplaceSession; @@ -224,8 +208,8 @@ void Load_ChatModule() pci->OnFlashHighlight = OnFlashHighlight; pci->ShowRoom = ShowRoom; - pci->DoPopup = DoPopup; - pci->DoTrayIcon = DoTrayIcon; + oldDoPopup = pci->DoPopup; pci->DoPopup = DoPopup; + oldDoTrayIcon = pci->DoTrayIcon; pci->DoTrayIcon = DoTrayIcon; pci->ReloadSettings(); g_hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU)); diff --git a/src/core/stdmsg/src/chat_util.cpp b/src/core/stdmsg/src/chat_util.cpp index 26d8f37f12..b9a44e45f6 100644 --- a/src/core/stdmsg/src/chat_util.cpp +++ b/src/core/stdmsg/src/chat_util.cpp @@ -171,13 +171,12 @@ bool LoadMessageFont(LOGFONT *lf, COLORREF *colour) lf->lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE; mir_snprintf(str, "SRMFont%d", i); - DBVARIANT dbv; - if (db_get_ws(NULL, "SRMM", str, &dbv)) + ptrW wszFontFace(db_get_wsa(NULL, "SRMM", str)); + if (wszFontFace == nullptr) mir_wstrcpy(lf->lfFaceName, L"Arial"); - else { - mir_wstrncpy(lf->lfFaceName, dbv.ptszVal, _countof(lf->lfFaceName)); - db_free(&dbv); - } + else + mir_wstrncpy(lf->lfFaceName, wszFontFace, _countof(lf->lfFaceName)); + mir_snprintf(str, "SRMFont%dSet", i); lf->lfCharSet = db_get_b(NULL, "SRMM", str, DEFAULT_CHARSET); } diff --git a/src/core/stdmsg/src/chat_window.cpp b/src/core/stdmsg/src/chat_window.cpp index b66912c87d..cf7cf376ac 100644 --- a/src/core/stdmsg/src/chat_window.cpp +++ b/src/core/stdmsg/src/chat_window.cpp @@ -819,8 +819,8 @@ static void __cdecl phase2(void * lParam) { SESSION_INFO *si = (SESSION_INFO*)lParam; Sleep(30); - if (si && si->hWnd) - PostMessage(si->hWnd, GC_REDRAWLOG2, 0, 0); + if (si && si->pDlg) + PostMessage(si->pDlg->GetHwnd(), GC_REDRAWLOG2, 0, 0); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -881,7 +881,6 @@ CChatRoomDlg::CChatRoomDlg(SESSION_INFO *si) : void CChatRoomDlg::OnInitDialog() { - m_si->hWnd = m_hwnd; m_si->pDlg = this; if (g_Settings.bTabsEnable) @@ -937,8 +936,7 @@ void CChatRoomDlg::OnDestroy() WindowList_Remove(pci->hWindowList, m_hwnd); - m_si->pDlg = NULL; - m_si->hWnd = NULL; + m_si->pDlg = nullptr; m_si->wState &= ~STATE_TALK; DestroyWindow(m_hwndStatus); m_hwndStatus = NULL; @@ -1168,14 +1166,14 @@ void CChatRoomDlg::SetWindowPosition() else SetWindowPos(m_hwnd, 0, (screen.right - screen.left) / 2 - (550) / 2, (screen.bottom - screen.top) / 2 - (400) / 2, (550), (400), SWP_NOZORDER | SWP_HIDEWINDOW | SWP_NOACTIVATE); SESSION_INFO *pActive = pci->GetActiveSession(); - if (pActive && pActive->hWnd && db_get_b(NULL, CHAT_MODULE, "CascadeWindows", 1)) { + if (pActive && pActive->pDlg && db_get_b(NULL, CHAT_MODULE, "CascadeWindows", 1)) { RECT rcThis, rcNew; int dwFlag = SWP_NOZORDER | SWP_NOACTIVATE; if (!IsWindowVisible(m_hwnd)) dwFlag |= SWP_HIDEWINDOW; GetWindowRect(m_hwnd, &rcThis); - GetWindowRect(pActive->hWnd, &rcNew); + GetWindowRect(pActive->pDlg->GetHwnd(), &rcNew); int offset = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME); SetWindowPos(m_hwnd, 0, rcNew.left + offset, rcNew.top + offset, rcNew.right - rcNew.left, rcNew.bottom - rcNew.top, dwFlag); @@ -1491,7 +1489,7 @@ INT_PTR CChatRoomDlg::DlgProc(UINT uMsg, WPARAM wParam, LPARAM lParam) switch (wParam) { case SESSION_OFFLINE: SendMessage(m_hwnd, GC_UPDATESTATUSBAR, 0, 0); - SendMessage(m_si->hWnd, GC_UPDATENICKLIST, 0, 0); + SendMessage(m_si->pDlg->GetHwnd(), GC_UPDATENICKLIST, 0, 0); return TRUE; case SESSION_ONLINE: diff --git a/src/core/stdmsg/src/globals.cpp b/src/core/stdmsg/src/globals.cpp index 4a42858cbb..5b2ec570f5 100644 --- a/src/core/stdmsg/src/globals.cpp +++ b/src/core/stdmsg/src/globals.cpp @@ -44,8 +44,11 @@ static int IconsChanged(WPARAM, LPARAM) static int OnShutdown(WPARAM, LPARAM) { - for (SESSION_INFO *si = pci->wndList; si; si = si->next) - SendMessage(si->hWnd, WM_CLOSE, 0, 0); + for (int i = 0; i < pci->arSessions.getCount(); i++) { + SESSION_INFO *si = pci->arSessions[i]; + if (si->pDlg) + si->pDlg->Close(); + } UninitTabs(); ImageList_Destroy(hIconsList); diff --git a/src/core/stdmsg/src/stdafx.h b/src/core/stdmsg/src/stdafx.h index 076d743dad..f15624007b 100644 --- a/src/core/stdmsg/src/stdafx.h +++ b/src/core/stdmsg/src/stdafx.h @@ -92,7 +92,6 @@ struct MODULEINFO : public GCModuleInfoBase struct SESSION_INFO : public GCSessionInfoBase { int iX, iY; - class CChatRoomDlg *pDlg; }; struct LOGSTREAMDATA : public GCLogStreamDataBase {}; @@ -110,7 +109,6 @@ struct GlobalLogSettings : public GlobalLogSettingsBase ///////////////////////////////////////////////////////////////////////////////////////// extern GlobalLogSettings g_Settings; -extern CHAT_MANAGER saveCI; extern HMENU g_hMenu; extern HIMAGELIST hIconsList; diff --git a/src/core/stdmsg/src/tabs.cpp b/src/core/stdmsg/src/tabs.cpp index 11bc1d1e39..5a4a904096 100644 --- a/src/core/stdmsg/src/tabs.cpp +++ b/src/core/stdmsg/src/tabs.cpp @@ -243,7 +243,7 @@ void CTabbedWindow::TabClicked() } SendMessage(m_hwnd, GC_FIXTABICONS, 0, (LPARAM)pDlg); - if (!s->hWnd) { + if (!s->pDlg) { pci->ShowRoom(s); SendMessage(m_hwnd, WM_MOUSEACTIVATE, 0, 0); } @@ -527,9 +527,9 @@ void ShowRoom(SESSION_INFO *si) return; // Do we need to create a window? - if (si->hWnd == NULL) { + if (si->pDlg == nullptr) { if (g_Settings.bTabsEnable) { - if (pDialog == NULL) { + if (pDialog == nullptr) { pDialog = new CTabbedWindow(); pDialog->Show(); } @@ -541,22 +541,22 @@ void ShowRoom(SESSION_INFO *si) pRoom->Show(); } - PostMessage(si->hWnd, WM_SIZE, 0, 0); + PostMessage(si->pDlg->GetHwnd(), WM_SIZE, 0, 0); if (si->iType != GCW_SERVER) - SendMessage(si->hWnd, GC_UPDATENICKLIST, 0, 0); + SendMessage(si->pDlg->GetHwnd(), GC_UPDATENICKLIST, 0, 0); else - SendMessage(si->hWnd, GC_UPDATETITLE, 0, 0); - SendMessage(si->hWnd, GC_REDRAWLOG, 0, 0); - SendMessage(si->hWnd, GC_UPDATESTATUSBAR, 0, 0); + SendMessage(si->pDlg->GetHwnd(), GC_UPDATETITLE, 0, 0); + SendMessage(si->pDlg->GetHwnd(), GC_REDRAWLOG, 0, 0); + SendMessage(si->pDlg->GetHwnd(), GC_UPDATESTATUSBAR, 0, 0); } - SetWindowLongPtr(si->hWnd, GWL_EXSTYLE, GetWindowLongPtr(si->hWnd, GWL_EXSTYLE) | WS_EX_APPWINDOW); + SetWindowLongPtr(si->pDlg->GetHwnd(), GWL_EXSTYLE, GetWindowLongPtr(si->pDlg->GetHwnd(), GWL_EXSTYLE) | WS_EX_APPWINDOW); - if (IsIconic(si->hWnd)) - ShowWindow(si->hWnd, SW_NORMAL); - ShowWindow(si->hWnd, SW_SHOW); - SetForegroundWindow(si->hWnd); + if (IsIconic(si->pDlg->GetHwnd())) + ShowWindow(si->pDlg->GetHwnd(), SW_NORMAL); + ShowWindow(si->pDlg->GetHwnd(), SW_SHOW); + SetForegroundWindow(si->pDlg->GetHwnd()); - SendMessage(si->hWnd, WM_MOUSEACTIVATE, 0, 0); - SetFocus(GetDlgItem(si->hWnd, IDC_MESSAGE)); + SendMessage(si->pDlg->GetHwnd(), WM_MOUSEACTIVATE, 0, 0); + SetFocus(GetDlgItem(si->pDlg->GetHwnd(), IDC_MESSAGE)); } diff --git a/src/mir_app/src/chat.h b/src/mir_app/src/chat.h index 66c39fa753..fbfc8872f6 100644 --- a/src/mir_app/src/chat.h +++ b/src/mir_app/src/chat.h @@ -27,6 +27,11 @@ struct MODULEINFO : public GCModuleInfoBase {}; struct SESSION_INFO : public GCSessionInfoBase {}; struct LOGSTREAMDATA : public GCLogStreamDataBase {}; +class CChatRoomDlg : public CSrmmBaseDialog +{ + CChatRoomDlg(); // just to suppress compiler's warnings, never implemented +}; + extern HGENMENU hJoinMenuItem, hLeaveMenuItem; extern GlobalLogSettingsBase *g_Settings; extern int g_cbSession, g_cbModuleInfo, g_iFontMode, g_iChatLang; @@ -34,6 +39,7 @@ extern wchar_t *g_szFontGroup; extern mir_cs csChat; extern char* pLogIconBmpBits[14]; +extern LIST g_arSessions; // log.c void LoadMsgLogBitmaps(void); @@ -104,7 +110,7 @@ bool IsHighlighted(SESSION_INFO *si, GCEVENT *pszText); UINT CreateGCMenu(HWND hwndDlg, HMENU *hMenu, int iIndex, POINT pt, SESSION_INFO *si, wchar_t* pszUID, wchar_t* pszWordText); void DestroyGCMenu(HMENU *hMenu, int iIndex); BOOL DoEventHookAsync(HWND hwnd, const wchar_t *pszID, const char *pszModule, int iType, const USERINFO *pUser, const wchar_t* pszText, INT_PTR dwItem); -BOOL DoEventHook(const wchar_t *pszID, const char *pszModule, int iType, const USERINFO *pUser, const wchar_t* pszText, INT_PTR dwItem); +BOOL DoEventHook(SESSION_INFO *si, int iType, const USERINFO *pUser, const wchar_t* pszText, INT_PTR dwItem); BOOL IsEventSupported(int eventType); BOOL LogToFile(SESSION_INFO *si, GCEVENT *gce); BOOL DoTrayIcon(SESSION_INFO *si, GCEVENT *gce); diff --git a/src/mir_app/src/chat_clist.cpp b/src/mir_app/src/chat_clist.cpp index 93346dd334..70a126dbe8 100644 --- a/src/mir_app/src/chat_clist.cpp +++ b/src/mir_app/src/chat_clist.cpp @@ -117,8 +117,8 @@ int RoomDoubleclicked(WPARAM hContact, LPARAM) SESSION_INFO *si = SM_FindSession(roomid, szProto); if (si) { // is the "toggle visibility option set, so we need to close the window? - if (si->hWnd != NULL && db_get_b(NULL, CHAT_MODULE, "ToggleVisibility", 0) == 1 && !cli.pfnGetEvent(hContact, 0) && IsWindowVisible(si->hWnd) && !IsIconic(si->hWnd)) { - PostMessage(si->hWnd, GC_CLOSEWINDOW, 0, 0); + if (si->pDlg != NULL && db_get_b(NULL, CHAT_MODULE, "ToggleVisibility", 0) == 1 && !cli.pfnGetEvent(hContact, 0) && IsWindowVisible(si->pDlg->GetHwnd()) && !IsIconic(si->pDlg->GetHwnd())) { + PostMessage(si->pDlg->GetHwnd(), GC_CLOSEWINDOW, 0, 0); return 1; } chatApi.ShowRoom(si); diff --git a/src/mir_app/src/chat_manager.cpp b/src/mir_app/src/chat_manager.cpp index 9c373c376a..1ff2b061e2 100644 --- a/src/mir_app/src/chat_manager.cpp +++ b/src/mir_app/src/chat_manager.cpp @@ -26,6 +26,18 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define WINDOWS_COMMANDS_MAX 30 +static int compareSessions(const SESSION_INFO *p1, const SESSION_INFO *p2) +{ + int res = mir_strcmp(p1->pszModule, p2->pszModule); + return (res != 0) ? res : mir_wstrcmp(p1->ptszID, p2->ptszID); +} + +LIST g_arSessions(10, compareSessions); + +CHAT_MANAGER::CHAT_MANAGER() : + arSessions(g_arSessions) +{} + CHAT_MANAGER chatApi; MODULEINFO *m_ModList = 0; @@ -51,7 +63,7 @@ static SESSION_INFO* GetActiveSession(void) if (si) return si; - return chatApi.wndList; + return g_arSessions[0]; } //--------------------------------------------------- @@ -99,18 +111,13 @@ int SM_RemoveSession(const wchar_t *pszID, const char *pszModule, BOOL removeCon if (!pszModule) return FALSE; - SESSION_INFO *si = chatApi.wndList, *pLast = NULL; - while (si != NULL) { + for (int i = g_arSessions.getCount() - 1; i >= 0; i--) { + SESSION_INFO *si = g_arSessions[i]; // match if ((!pszID && si->iType != GCW_SERVER || !mir_wstrcmpi(si->ptszID, pszID)) && !mir_strcmpi(si->pszModule, pszModule)) { - if (si->hWnd) - SendMessage(si->hWnd, GC_CONTROL_MSG, SESSION_TERMINATE, 0); - DoEventHook(si->ptszID, si->pszModule, GC_SESSION_TERMINATE, NULL, NULL, (INT_PTR)si->pItemData); - - if (pLast == NULL) - chatApi.wndList = si->next; - else - pLast->next = si->next; + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_CONTROL_MSG, SESSION_TERMINATE, 0); + DoEventHook(si, GC_SESSION_TERMINATE, NULL, NULL, (INT_PTR)si->pItemData); // contact may have been deleted here already, since function may be called after deleting // contact so the handle may be invalid, therefore db_get_b shall return 0 @@ -118,18 +125,10 @@ int SM_RemoveSession(const wchar_t *pszID, const char *pszModule, BOOL removeCon db_delete_contact(si->hContact); SM_FreeSession(si); + g_arSessions.remove(i); if (pszID) return 1; - - if (pLast) - si = pLast->next; - else - si = chatApi.wndList; - } - else { - pLast = si; - si = si->next; } } return FALSE; @@ -138,14 +137,14 @@ int SM_RemoveSession(const wchar_t *pszID, const char *pszModule, BOOL removeCon SESSION_INFO* SM_FindSession(const wchar_t *pszID, const char *pszModule) { if (!pszID || !pszModule) - return NULL; + return nullptr; - mir_cslock lck(csChat); - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) - if (!mir_wstrcmpi(si->ptszID, pszID) && !mir_strcmpi(si->pszModule, pszModule)) - return si; + SESSION_INFO tmp; + tmp.pszModule = (char*)pszModule; + tmp.ptszID = (wchar_t*)pszID; - return NULL; + mir_cslock lck(csChat); + return g_arSessions.find(&tmp); } BOOL SM_SetOffline(const wchar_t *pszID, const char *pszModule) @@ -153,7 +152,8 @@ BOOL SM_SetOffline(const wchar_t *pszID, const char *pszModule) if (!pszModule) return FALSE; - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if ((pszID && mir_wstrcmpi(si->ptszID, pszID)) || mir_strcmpi(si->pszModule, pszModule)) continue; @@ -231,7 +231,8 @@ BOOL SM_RemoveUser(const wchar_t *pszID, const char *pszModule, const wchar_t *p if (!pszModule || !pszUID) return FALSE; - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if ((pszID && mir_wstrcmpi(si->ptszID, pszID)) || mir_strcmpi(si->pszModule, pszModule)) continue; @@ -245,8 +246,8 @@ BOOL SM_RemoveUser(const wchar_t *pszID, const char *pszModule, const wchar_t *p si->pMe = NULL; chatApi.UM_RemoveUser(&si->pUsers, pszUID); - if (si->hWnd) - SendMessage(si->hWnd, GC_UPDATENICKLIST, 0, 0); + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_UPDATENICKLIST, 0, 0); if (pszID) return TRUE; @@ -271,8 +272,8 @@ BOOL SM_GiveStatus(const wchar_t *pszID, const char *pszModule, const wchar_t *p USERINFO *ui = chatApi.UM_GiveStatus(si->pUsers, pszUID, chatApi.TM_StringToWord(si->pStatuses, pszStatus)); if (ui) { SM_MoveUser(si->ptszID, si->pszModule, ui->pszUID); - if (si->hWnd) - SendMessage(si->hWnd, GC_UPDATENICKLIST, 0, 0); + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_UPDATENICKLIST, 0, 0); } return TRUE; } @@ -286,8 +287,8 @@ BOOL SM_SetContactStatus(const wchar_t *pszID, const char *pszModule, const wcha USERINFO *ui = chatApi.UM_SetContactStatus(si->pUsers, pszUID, wStatus); if (ui) { SM_MoveUser(si->ptszID, si->pszModule, ui->pszUID); - if (si->hWnd) - SendMessage(si->hWnd, GC_UPDATENICKLIST, 0, 0); + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_UPDATENICKLIST, 0, 0); } return TRUE; } @@ -301,8 +302,8 @@ BOOL SM_TakeStatus(const wchar_t *pszID, const char *pszModule, const wchar_t *p USERINFO *ui = chatApi.UM_TakeStatus(si->pUsers, pszUID, chatApi.TM_StringToWord(si->pStatuses, pszStatus)); if (ui) { SM_MoveUser(si->ptszID, si->pszModule, ui->pszUID); - if (si->hWnd) - SendMessage(si->hWnd, GC_UPDATENICKLIST, 0, 0); + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_UPDATENICKLIST, 0, 0); } return TRUE; } @@ -312,12 +313,13 @@ LRESULT SM_SendMessage(const wchar_t *pszID, const char *pszModule, UINT msg, WP if (pszModule == NULL) return 0; - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if ((pszID && mir_wstrcmpi(si->ptszID, pszID)) || mir_strcmpi(si->pszModule, pszModule)) continue; - if (si->hWnd) { - LRESULT i = SendMessage(si->hWnd, msg, wParam, lParam); + if (si->pDlg) { + LRESULT i = SendMessage(si->pDlg->GetHwnd(), msg, wParam, lParam); if (pszID) return i; } @@ -329,15 +331,16 @@ LRESULT SM_SendMessage(const wchar_t *pszID, const char *pszModule, UINT msg, WP static BOOL SM_BroadcastMessage(const char *pszModule, UINT msg, WPARAM wParam, LPARAM lParam, BOOL bAsync) { - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if (pszModule && _strcmpi(si->pszModule, pszModule)) continue; - if (si->hWnd) { + if (si->pDlg) { if (bAsync) - PostMessage(si->hWnd, msg, wParam, lParam); + PostMessage(si->pDlg->GetHwnd(), msg, wParam, lParam); else - SendMessage(si->hWnd, msg, wParam, lParam); + SendMessage(si->pDlg->GetHwnd(), msg, wParam, lParam); } } return TRUE; @@ -348,7 +351,8 @@ BOOL SM_SetStatus(const wchar_t *pszID, const char *pszModule, int wStatus) if (!pszModule) return FALSE; - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if ((pszID && mir_wstrcmpi(si->ptszID, pszID)) || mir_strcmpi(si->pszModule, pszModule)) continue; @@ -374,14 +378,15 @@ BOOL SM_ChangeNick(const wchar_t *pszID, const char *pszModule, GCEVENT *gce) if (!pszModule) return FALSE; - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if ((!pszID || !mir_wstrcmpi(si->ptszID, pszID)) && !mir_strcmpi(si->pszModule, pszModule)) { USERINFO *ui = chatApi.UM_FindUser(si->pUsers, gce->ptszUID); if (ui) { replaceStrW(ui->pszNick, gce->ptszText); SM_MoveUser(si->ptszID, si->pszModule, ui->pszUID); - if (si->hWnd) - SendMessage(si->hWnd, GC_UPDATENICKLIST, 0, 0); + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_UPDATENICKLIST, 0, 0); if (chatApi.OnChangeNick) chatApi.OnChangeNick(si); } @@ -395,17 +400,16 @@ BOOL SM_ChangeNick(const wchar_t *pszID, const char *pszModule, GCEVENT *gce) void SM_RemoveAll(void) { - while (chatApi.wndList) { - SESSION_INFO *pLast = chatApi.wndList->next; + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; - if (chatApi.wndList->hWnd) - SendMessage(chatApi.wndList->hWnd, GC_CONTROL_MSG, SESSION_TERMINATE, 0); - DoEventHook(chatApi.wndList->ptszID, chatApi.wndList->pszModule, GC_SESSION_TERMINATE, NULL, NULL, (INT_PTR)chatApi.wndList->pItemData); + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_CONTROL_MSG, SESSION_TERMINATE, 0); + DoEventHook(si, GC_SESSION_TERMINATE, NULL, NULL, (INT_PTR)si->pItemData); - SM_FreeSession(chatApi.wndList); - chatApi.wndList = pLast; + SM_FreeSession(si); } - chatApi.wndList = NULL; + g_arSessions.destroy(); } static void SM_AddCommand(const wchar_t *pszID, const char *pszModule, const char* lpNewCommand) @@ -480,9 +484,11 @@ static int SM_GetCount(const char *pszModule) { int count = 0; - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if (!mir_strcmpi(pszModule, si->pszModule)) count++; + } return count; } @@ -490,7 +496,8 @@ static int SM_GetCount(const char *pszModule) static SESSION_INFO* SM_FindSessionByIndex(const char *pszModule, int iItem) { int count = 0; - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if (!mir_strcmpi(pszModule, si->pszModule)) { if (iItem == count) return si; @@ -508,7 +515,8 @@ char* SM_GetUsers(SESSION_INFO *si) return NULL; USERINFO *utemp = NULL; - for (SESSION_INFO *p = chatApi.wndList; p != NULL; p = p->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *p = g_arSessions[i]; if (si == p) { if ((utemp = p->pUsers) == NULL) return NULL; @@ -536,8 +544,10 @@ char* SM_GetUsers(SESSION_INFO *si) static void SM_InvalidateLogDirectories() { - for (SESSION_INFO *si = chatApi.wndList; si; si = si->next) + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; si->pszLogFileName[0] = si->pszLogFileName[1] = 0; + } } //--------------------------------------------------- @@ -1031,6 +1041,12 @@ static BOOL LM_RemoveAll(LOGINFO **ppLogListStart, LOGINFO **ppLogListEnd) return TRUE; } +static BOOL DoEventHook(const wchar_t *pszID, const char *pszModule, int iType, const USERINFO *pUser, const wchar_t* pszText, INT_PTR dwItem) +{ + SESSION_INFO *si = chatApi.SM_FindSession(pszID, pszModule); + return (si) ? DoEventHook(si, iType, pUser, pszText, dwItem) : FALSE; +} + MIR_APP_DLL(CHAT_MANAGER*) Chat_GetInterface(CHAT_MANAGER_INITDATA *pInit, int _hLangpack) { if (pInit == NULL) @@ -1041,18 +1057,15 @@ MIR_APP_DLL(CHAT_MANAGER*) Chat_GetInterface(CHAT_MANAGER_INITDATA *pInit, int _ if (g_cbSession) { // reallocate old sessions mir_cslock lck(csChat); - SESSION_INFO *pPrev = NULL; - for (SESSION_INFO *p = chatApi.wndList; p; p = p->next) { + + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *p = g_arSessions[i]; SESSION_INFO *p1 = (SESSION_INFO*)mir_realloc(p, pInit->cbSession); memset(PBYTE(p1) + sizeof(GCSessionInfoBase), 0, pInit->cbSession - sizeof(GCSessionInfoBase)); if (p1 != p) { // realloc could change a pointer, reinsert a structure - if (chatApi.wndList == p) - chatApi.wndList = p1; - if (pPrev != NULL) - pPrev->next = p1; - p = p1; + g_arSessions.remove(i); + g_arSessions.insert(p1); } - pPrev = p; } } if (g_cbModuleInfo) { // reallocate old modules diff --git a/src/mir_app/src/chat_svc.cpp b/src/mir_app/src/chat_svc.cpp index 2d8dc7cb24..a3f8f492d0 100644 --- a/src/mir_app/src/chat_svc.cpp +++ b/src/mir_app/src/chat_svc.cpp @@ -220,12 +220,7 @@ EXTERN_C MIR_APP_DLL(GCSessionInfoBase*) Chat_NewSession( si->ptszID = mir_wstrdup(ptszID); si->pszModule = mir_strdup(pszModule); - if (chatApi.wndList == NULL) // list is empty - chatApi.wndList = si; - else { - si->next = chatApi.wndList; - chatApi.wndList = si; - } + chatApi.arSessions.insert(si); lck.unlock(); // set the defaults @@ -413,7 +408,8 @@ static BOOL AddEventToAllMatchingUID(GCEVENT *gce) { int bManyFix = 0; - for (SESSION_INFO *p = chatApi.wndList; p != NULL; p = p->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *p = g_arSessions[i]; if (!p->bInitDone || mir_strcmpi(p->pszModule, gce->pDest->pszModule)) continue; @@ -423,11 +419,11 @@ static BOOL AddEventToAllMatchingUID(GCEVENT *gce) if (chatApi.OnEventBroadcast) chatApi.OnEventBroadcast(p, gce); - if (p->hWnd && p->bInitDone) { + if (p->pDlg && p->bInitDone) { if (SM_AddEvent(p->ptszID, p->pszModule, gce, FALSE)) - SendMessage(p->hWnd, GC_ADDLOG, 0, 0); + SendMessage(p->pDlg->GetHwnd(), GC_ADDLOG, 0, 0); else - SendMessage(p->hWnd, GC_REDRAWLOG2, 0, 0); + SendMessage(p->pDlg->GetHwnd(), GC_REDRAWLOG2, 0, 0); } if (!(gce->dwFlags & GCEF_NOTNOTIFY)) { @@ -569,11 +565,11 @@ EXTERN_C MIR_APP_DLL(int) Chat_Event(GCEVENT *gce) } int isOk = SM_AddEvent(pWnd, pMod, gce, bIsHighlighted); - if (si->hWnd) { + if (si->pDlg) { if (isOk) - SendMessage(si->hWnd, GC_ADDLOG, 0, 0); + SendMessage(si->pDlg->GetHwnd(), GC_ADDLOG, 0, 0); else - SendMessage(si->hWnd, GC_REDRAWLOG2, 0, 0); + SendMessage(si->pDlg->GetHwnd(), GC_REDRAWLOG2, 0, 0); } if (!(gce->dwFlags & GCEF_NOTNOTIFY)) { @@ -637,8 +633,8 @@ MIR_APP_DLL(int) Chat_ChangeSessionName(const char *szModule, const wchar_t *wsz replaceStrW(si->ptszName, wszNewName); db_set_ws(si->hContact, szModule, "Nick", wszNewName); - if (si->hWnd) - SendMessage(si->hWnd, GC_UPDATETITLE, 0, 0); + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_UPDATETITLE, 0, 0); } return 0; } @@ -649,7 +645,8 @@ MIR_APP_DLL(int) Chat_ChangeUserId(const char *szModule, const wchar_t *wszId, c return GC_EVENT_ERROR; mir_cslock lck(csChat); - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if ((wszId && mir_wstrcmpi(si->ptszID, wszId)) || mir_strcmpi(si->pszModule, szModule)) continue; @@ -676,12 +673,13 @@ MIR_APP_DLL(int) Chat_SendUserMessage(const char *szModule, const wchar_t *wszId return GC_EVENT_ERROR; mir_cslock lck(csChat); - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if ((wszId && mir_wstrcmpi(si->ptszID, wszId)) || mir_strcmpi(si->pszModule, szModule)) continue; if (si->iType == GCW_CHATROOM || si->iType == GCW_PRIVMESS) - DoEventHook(si->ptszID, si->pszModule, GC_USER_MESSAGE, NULL, wszText, 0); + DoEventHook(si, GC_USER_MESSAGE, NULL, wszText, 0); if (wszId) break; } @@ -702,8 +700,8 @@ MIR_APP_DLL(int) Chat_SetStatusbarText(const char *szModule, const wchar_t *wszI else db_set_s(si->hContact, si->pszModule, "StatusBar", ""); - if (si->hWnd) - SendMessage(si->hWnd, GC_UPDATESTATUSBAR, 0, 0); + if (si->pDlg) + SendMessage(si->pDlg->GetHwnd(), GC_UPDATESTATUSBAR, 0, 0); } return 0; } @@ -714,13 +712,14 @@ MIR_APP_DLL(int) Chat_SetStatusEx(const char *szModule, const wchar_t *wszId, in return GC_EVENT_ERROR; mir_cslock lck(csChat); - for (SESSION_INFO *si = chatApi.wndList; si != NULL; si = si->next) { + for (int i = 0; i < g_arSessions.getCount(); i++) { + SESSION_INFO *si = g_arSessions[i]; if ((wszId && mir_wstrcmpi(si->ptszID, wszId)) || mir_strcmpi(si->pszModule, szModule)) continue; chatApi.UM_SetStatusEx(si->pUsers, wszText, flags); - if (si->hWnd) - RedrawWindow(GetDlgItem(si->hWnd, IDC_LIST), NULL, NULL, RDW_INVALIDATE); + if (si->pDlg) + RedrawWindow(GetDlgItem(si->pDlg->GetHwnd(), IDC_LIST), NULL, NULL, RDW_INVALIDATE); if (wszId) break; } diff --git a/src/mir_app/src/chat_tools.cpp b/src/mir_app/src/chat_tools.cpp index 39ad6f98a4..13f573a2c4 100644 --- a/src/mir_app/src/chat_tools.cpp +++ b/src/mir_app/src/chat_tools.cpp @@ -154,8 +154,8 @@ static LRESULT CALLBACK PopupDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPA if (cli.pfnGetEvent(si->hContact, 0)) cli.pfnRemoveEvent(si->hContact, GC_FAKE_EVENT); - if (si->hWnd && KillTimer(si->hWnd, TIMERID_FLASHWND)) - FlashWindow(si->hWnd, FALSE); + if (si->pDlg && KillTimer(si->pDlg->GetHwnd(), TIMERID_FLASHWND)) + FlashWindow(si->pDlg->GetHwnd(), FALSE); PUDeletePopup(hWnd); break; @@ -273,7 +273,7 @@ BOOL DoSoundsFlashPopupTrayStuff(SESSION_INFO *si, GCEVENT *gce, BOOL bHighlight if (!gce || !si || gce->bIsMe || si->iType == GCW_SERVER) return FALSE; - BOOL bInactive = si->hWnd == NULL || GetForegroundWindow() != si->hWnd; + BOOL bInactive = si->pDlg == NULL || GetForegroundWindow() != si->pDlg->GetHwnd(); int iEvent = gce->pDest->iType; @@ -631,15 +631,14 @@ BOOL DoEventHookAsync(HWND hwnd, const wchar_t *pszID, const char *pszModule, in return TRUE; } -BOOL DoEventHook(const wchar_t *pszID, const char *pszModule, int iType, const USERINFO *pUser, const wchar_t* pszText, INT_PTR dwItem) +BOOL DoEventHook(SESSION_INFO *si, int iType, const USERINFO *pUser, const wchar_t* pszText, INT_PTR dwItem) { - SESSION_INFO *si = chatApi.SM_FindSession(pszID, pszModule); - if (si == NULL) + if (si == nullptr) return FALSE; - GCDEST gcd = { (char*)pszModule, pszID, iType }; + GCDEST gcd = { si->pszModule, si->ptszID, iType }; GCHOOK gch = { 0 }; - if (pUser != NULL) { + if (pUser != nullptr) { gch.ptszUID = pUser->pszUID; gch.ptszNick = pUser->pszNick; } diff --git a/src/mir_app/src/netlibsock.cpp b/src/mir_app/src/netlibsock.cpp index 57cb023b4b..7bfbd3c7df 100644 --- a/src/mir_app/src/netlibsock.cpp +++ b/src/mir_app/src/netlibsock.cpp @@ -69,7 +69,7 @@ MIR_APP_DLL(int) Netlib_Recv(HNETLIBCONN nlc, char *buf, int len, int flags) recvResult = NetlibHttpGatewayRecv(nlc, buf, len, flags); else { if (!nlc->foreBuf.isEmpty()) { - recvResult = min(len, nlc->foreBuf.length()); + recvResult = min(len, (int)nlc->foreBuf.length()); memcpy(buf, nlc->foreBuf.data(), recvResult); nlc->foreBuf.remove(recvResult); } -- cgit v1.2.3