From 123fa87f68c42b4f121ad315623a9fb5a39ab30c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 14 Apr 2023 23:33:11 +0300 Subject: all LogStreamInEvents implementations unified, old memory allocation schema dropped --- plugins/TabSRMM/src/globals.cpp | 7 +- plugins/TabSRMM/src/msgdlgother.cpp | 4 +- plugins/TabSRMM/src/msglog.cpp | 231 ++++++++++++------------------------ plugins/TabSRMM/src/msgoptions.cpp | 10 +- plugins/TabSRMM/src/msgs.h | 7 +- plugins/TabSRMM/src/sendqueue.cpp | 11 +- 6 files changed, 93 insertions(+), 177 deletions(-) (limited to 'plugins/TabSRMM') diff --git a/plugins/TabSRMM/src/globals.cpp b/plugins/TabSRMM/src/globals.cpp index 7c7a649412..997bd3780c 100644 --- a/plugins/TabSRMM/src/globals.cpp +++ b/plugins/TabSRMM/src/globals.cpp @@ -494,10 +494,9 @@ void CGlobals::logStatusChange(WPARAM wParam, const CContactCache *c) else text.Format(TranslateT("changed status from %s to %s."), szOldStatus, szNewStatus); - T2Utf szMsg(text); - DBEVENTINFO dbei = {}; - dbei.pBlob = (uint8_t*)(char*)szMsg; - dbei.cbBlob = (int)mir_strlen(szMsg) + 1; + DB::EventInfo dbei; + dbei.pBlob = (uint8_t*)T2Utf(text).detach(); + dbei.cbBlob = (int)mir_strlen((char*)dbei.pBlob); dbei.flags = DBEF_UTF | DBEF_READ; dbei.eventType = EVENTTYPE_STATUSCHANGE; dbei.timestamp = time(0); diff --git a/plugins/TabSRMM/src/msgdlgother.cpp b/plugins/TabSRMM/src/msgdlgother.cpp index 2360894d31..68b97ba962 100644 --- a/plugins/TabSRMM/src/msgdlgother.cpp +++ b/plugins/TabSRMM/src/msgdlgother.cpp @@ -1413,7 +1413,7 @@ void CMsgDialog::LoadSplitter() ///////////////////////////////////////////////////////////////////////////////////////// -void CMsgDialog::LogEvent(DBEVENTINFO &dbei) +void CMsgDialog::LogEvent(DB::EventInfo &dbei) { if (m_iLogMode != WANT_BUILTIN_LOG) { dbei.flags |= DBEF_TEMPORARY; @@ -1424,7 +1424,7 @@ void CMsgDialog::LogEvent(DBEVENTINFO &dbei) db_event_delete(hDbEvent); } } - else LOG()->LogEvents(0, 1, true, &dbei); + else LOG()->LogEvents(0, 0, true, &dbei); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/plugins/TabSRMM/src/msglog.cpp b/plugins/TabSRMM/src/msglog.cpp index 59b68b9bc5..c48d5d1d4d 100644 --- a/plugins/TabSRMM/src/msglog.cpp +++ b/plugins/TabSRMM/src/msglog.cpp @@ -80,22 +80,9 @@ COLORREF fontcolors[MSGDLGFONTCOUNT + 2]; static HICON Logicons[NR_LOGICONS]; -#define STREAMSTAGE_HEADER 0 -#define STREAMSTAGE_EVENTS 1 -#define STREAMSTAGE_TAIL 2 -#define STREAMSTAGE_STOP 3 -struct LogStreamData +struct RtfLogStreamData : public RtfLogStreamBase { - int stage; - MCONTACT hContact; - MEVENT hDbEvent, hDbEventLast; - char *buffer; - int bufferOffset, bufferLen; - int eventsToInsert; - int isEmpty; int isAppend; - class CLogWindow *pLog; - DBEVENTINFO *dbei; }; __forceinline char* GetRTFFont(uint32_t dwIndex) @@ -303,68 +290,6 @@ static void AppendUnicodeToBuffer(CMStringA &str, const wchar_t *line, int mode) str.AppendChar('}'); } -///////////////////////////////////////////////////////////////////////////////////////// -// mir_free() the return value - -static char* CreateRTFHeader(CLogWindow *pLog) -{ - int i; - CMStringA str; - auto &dat = pLog->GetDialog(); - TLogTheme *theme = &dat.m_pContainer->m_theme; - LOGFONTW *logFonts = theme->logFonts; - COLORREF *fontColors = theme->fontColors; - - str.Append("{\\rtf1\\ansi\\deff0{\\fonttbl"); - - for (i = 0; i < MSGDLGFONTCOUNT; i++) - str.AppendFormat("{\\f%u\\fnil\\fcharset%u %S;}", i, logFonts[i].lfCharSet, logFonts[i].lfFaceName); - str.AppendFormat("{\\f%u\\fnil\\fcharset%u %s;}", MSGDLGFONTCOUNT, logFonts[i].lfCharSet, "Arial"); - - str.Append("}{\\colortbl "); - for (i = 0; i < MSGDLGFONTCOUNT; i++) - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(fontColors[i]), GetGValue(fontColors[i]), GetBValue(fontColors[i])); - - COLORREF colour = (GetSysColorBrush(COLOR_HOTLIGHT) == nullptr) ? RGB(0, 0, 255) : GetSysColor(COLOR_HOTLIGHT); - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); - - // OnO: Create incoming and outcoming colours - colour = theme->inbg; - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); - colour = theme->outbg; - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); - colour = theme->bg; - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); - colour = theme->hgrid; - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); - colour = theme->oldinbg; - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); - colour = theme->oldoutbg; - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); - colour = theme->statbg; - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); - - // custom template colors... - for (i = 1; i <= 5; i++) { - colour = theme->custom_colors[i - 1]; - if (colour == 0) - colour = RGB(1, 1, 1); - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); - } - - // bbcode colors... - for (auto &p : Utils::rtf_clrs) - str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(p->clr), GetGValue(p->clr), GetBValue(p->clr)); - - // paragraph header - str.AppendFormat("}"); - - // indent - if (!(dat.m_dwFlags & MWF_LOG_INDENT)) - str.AppendFormat("\\li%u\\ri%u\\fi%u\\tx%u", 2 * 15, 2 * 15, 0, 70 * 15); - return str.Detach(); -} - static void AppendTimeStamp(wchar_t *szFinalTimestamp, int isSent, CMStringA &str, int skipFont, CMsgDialog *dat, int iFontIDOffset) { if (!skipFont) { @@ -399,12 +324,6 @@ static wchar_t* Template_MakeRelativeDate(HANDLE hTimeZone, time_t check, wchar_ return szResult; } -// mir_free() the return value -static char *CreateRTFTail() -{ - return mir_strdup("}"); -} - bool DbEventIsShown(const DB::EventInfo &dbei) { if (!IsCustomEvent(dbei.eventType) || dbei.isSrmm()) @@ -413,57 +332,6 @@ bool DbEventIsShown(const DB::EventInfo &dbei) return IsStatusEvent(dbei.eventType); } -static DWORD CALLBACK LogStreamInEvents(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) -{ - LogStreamData *dat = (LogStreamData*)dwCookie; - - if (dat->buffer == nullptr) { - dat->bufferOffset = 0; - switch (dat->stage) { - case STREAMSTAGE_HEADER: - mir_free(dat->buffer); - dat->buffer = CreateRTFHeader(dat->pLog); - dat->stage = STREAMSTAGE_EVENTS; - break; - - case STREAMSTAGE_EVENTS: - if (dat->eventsToInsert) { - do { - mir_free(dat->buffer); - dat->buffer = dat->pLog->CreateRTFFromDbEvent(dat); - if (dat->buffer) - dat->hDbEventLast = dat->hDbEvent; - dat->hDbEvent = db_event_next(dat->hContact, dat->hDbEvent); - if (--dat->eventsToInsert == 0) - break; - } while (dat->buffer == nullptr && dat->hDbEvent); - - if (dat->buffer) - break; - } - dat->stage = STREAMSTAGE_TAIL; - __fallthrough; - - case STREAMSTAGE_TAIL: - mir_free(dat->buffer); - dat->buffer = CreateRTFTail(); - dat->stage = STREAMSTAGE_STOP; - break; - - case STREAMSTAGE_STOP: - *pcb = 0; - return 0; - } - dat->bufferLen = (int)mir_strlen(dat->buffer); - } - *pcb = min(cb, dat->bufferLen - dat->bufferOffset); - memcpy(pbBuff, dat->buffer + dat->bufferOffset, *pcb); - dat->bufferOffset += *pcb; - if (dat->bufferOffset == dat->bufferLen) - replaceStr(dat->buffer, nullptr); - return 0; -} - ///////////////////////////////////////////////////////////////////////////////////////// INT_PTR CLogWindow::WndProc(UINT msg, WPARAM wParam, LPARAM lParam) @@ -618,23 +486,79 @@ void CLogWindow::Attach() ///////////////////////////////////////////////////////////////////////////////////////// -char* CLogWindow::CreateRTFFromDbEvent(LogStreamData *streamData) +void CLogWindow::CreateRtfHeader(RtfLogStreamData *streamData) { + int i; + auto &str = streamData->buf; + auto &dat = m_pDlg; + TLogTheme *theme = &dat.m_pContainer->m_theme; + LOGFONTW *logFonts = theme->logFonts; + COLORREF *fontColors = theme->fontColors; + + str.Append("{\\rtf1\\ansi\\deff0{\\fonttbl"); + + for (i = 0; i < MSGDLGFONTCOUNT; i++) + str.AppendFormat("{\\f%u\\fnil\\fcharset%u %S;}", i, logFonts[i].lfCharSet, logFonts[i].lfFaceName); + str.AppendFormat("{\\f%u\\fnil\\fcharset%u %s;}", MSGDLGFONTCOUNT, logFonts[i].lfCharSet, "Arial"); + + str.Append("}{\\colortbl "); + for (i = 0; i < MSGDLGFONTCOUNT; i++) + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(fontColors[i]), GetGValue(fontColors[i]), GetBValue(fontColors[i])); + + COLORREF colour = (GetSysColorBrush(COLOR_HOTLIGHT) == nullptr) ? RGB(0, 0, 255) : GetSysColor(COLOR_HOTLIGHT); + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); + + // OnO: Create incoming and outcoming colours + colour = theme->inbg; + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); + colour = theme->outbg; + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); + colour = theme->bg; + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); + colour = theme->hgrid; + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); + colour = theme->oldinbg; + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); + colour = theme->oldoutbg; + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); + colour = theme->statbg; + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); + + // custom template colors... + for (i = 1; i <= 5; i++) { + colour = theme->custom_colors[i - 1]; + if (colour == 0) + colour = RGB(1, 1, 1); + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(colour), GetGValue(colour), GetBValue(colour)); + } + + // bbcode colors... + for (auto &p : Utils::rtf_clrs) + str.AppendFormat("\\red%u\\green%u\\blue%u;", GetRValue(p->clr), GetGValue(p->clr), GetBValue(p->clr)); + + // paragraph header + str.AppendFormat("}"); + + // indent + if (!(dat.m_dwFlags & MWF_LOG_INDENT)) + str.AppendFormat("\\li%u\\ri%u\\fi%u\\tx%u", 2 * 15, 2 * 15, 0, 70 * 15); +} + +///////////////////////////////////////////////////////////////////////////////////////// + +bool CLogWindow::CreateRtfEvent(RtfLogStreamData *streamData, DB::EventInfo &dbei) +{ + if (!DbEventIsShown(dbei)) + return false; + + if (streamData->dbei && streamData->eventsToInsert > 0) + return false; + HANDLE hTimeZone = nullptr; struct tm event_time = { 0 }; bool skipToNext = false, skipFont = false; bool isBold = false, isItalic = false, isUnderline = false; - DB::EventInfo dbei; - if (streamData->dbei != nullptr) - memcpy(&dbei, streamData->dbei, sizeof(DBEVENTINFO)); - else { - dbei.cbBlob = -1; - db_event_get(streamData->hDbEvent, &dbei); - if (!DbEventIsShown(dbei)) - return nullptr; - } - auto *dat = &m_pDlg; if (dbei.eventType == EVENTTYPE_MESSAGE && !dbei.markedRead()) dat->m_cache->updateStats(TSessionStats::SET_LAST_RCV, mir_strlen((char *)dbei.pBlob)); @@ -653,7 +577,7 @@ char* CLogWindow::CreateRTFFromDbEvent(LogStreamData *streamData) msg.TrimRight(); dat->FormatRaw(msg, 1, FALSE); - CMStringA str; + auto &str = streamData->buf; // means: last \\par was deleted to avoid new line at end of log if (dat->m_bLastParaDeleted) { @@ -1184,9 +1108,6 @@ skip: str.Append("\\par"); - if (streamData->dbei != nullptr) - dbei.pBlob = 0; - dat->m_iLastEventType = MAKELONG((dbei.flags & (DBEF_SENT | DBEF_READ | DBEF_RTL)), dbei.eventType); dat->m_lastEventTime = dbei.timestamp; return str.Detach(); @@ -1199,7 +1120,7 @@ void CLogWindow::LogEvents(MEVENT hDbEventFirst, int count, bool fAppend) LogEvents(hDbEventFirst, count, fAppend, nullptr); } -void CLogWindow::LogEvents(MEVENT hDbEventFirst, int count, bool fAppend, DBEVENTINFO *dbei_s) +void CLogWindow::LogEvents(MEVENT hDbEventFirst, int count, bool fAppend, DB::EventInfo *dbei_s) { CHARRANGE oldSel, sel; @@ -1228,7 +1149,7 @@ void CLogWindow::LogEvents(MEVENT hDbEventFirst, int count, bool fAppend, DBEVEN m_rtf.SendMsg(EM_HIDESELECTION, TRUE, 0); m_rtf.SendMsg(EM_EXGETSEL, 0, (LPARAM)&oldSel); - LogStreamData streamData = { 0 }; + RtfLogStreamData streamData = { 0 }; streamData.hContact = m_pDlg.m_hContact; streamData.hDbEvent = hDbEventFirst; streamData.pLog = this; @@ -1237,10 +1158,6 @@ void CLogWindow::LogEvents(MEVENT hDbEventFirst, int count, bool fAppend, DBEVEN streamData.dbei = dbei_s; streamData.isAppend = fAppend; - EDITSTREAM stream = { 0 }; - stream.pfnCallback = LogStreamInEvents; - stream.dwCookie = (DWORD_PTR)&streamData; - LONG startAt; if (fAppend) { GETTEXTLENGTHEX gtxl = { 0 }; @@ -1262,7 +1179,7 @@ void CLogWindow::LogEvents(MEVENT hDbEventFirst, int count, bool fAppend, DBEVEN // begin to draw m_rtf.SetDraw(false); - m_rtf.SendMsg(EM_STREAMIN, fAppend ? SFF_SELECTION | SF_RTF : SFF_SELECTION | SF_RTF, (LPARAM)&stream); + StreamRtfEvents(&streamData, true); m_pDlg.m_hDbEventLast = streamData.hDbEventLast; @@ -1287,8 +1204,7 @@ void CLogWindow::LogEvents(MEVENT hDbEventFirst, int count, bool fAppend, DBEVEN if (streamData.dbei != nullptr) isSent = (streamData.dbei->flags & DBEF_SENT) != 0; else { - DBEVENTINFO dbei = {}; - db_event_get(hDbEventFirst, &dbei); + DB::EventInfo dbei(hDbEventFirst, false); isSent = (dbei.flags & DBEF_SENT) != 0; } @@ -1306,7 +1222,6 @@ void CLogWindow::LogEvents(MEVENT hDbEventFirst, int count, bool fAppend, DBEVEN m_rtf.SetDraw(true); InvalidateRect(m_rtf.GetHwnd(), nullptr, FALSE); EnableWindow(GetDlgItem(m_pDlg.m_hwnd, IDC_QUOTE), m_pDlg.m_hDbEventLast != 0); - mir_free(streamData.buffer); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/plugins/TabSRMM/src/msgoptions.cpp b/plugins/TabSRMM/src/msgoptions.cpp index db48cbabdf..d94ecf0eb8 100644 --- a/plugins/TabSRMM/src/msgoptions.cpp +++ b/plugins/TabSRMM/src/msgoptions.cpp @@ -622,15 +622,15 @@ public: T2Utf szText((iIndex == 6) ? TranslateT("is now offline (was online)") : TranslateT("The quick brown fox jumps over the lazy dog.")); - DBEVENTINFO dbei = {}; + DB::EventInfo dbei; dbei.szModule = m_szProto; dbei.timestamp = time(0); dbei.eventType = (iIndex == 6) ? EVENTTYPE_STATUSCHANGE : EVENTTYPE_MESSAGE; dbei.eventType = (iIndex == 7) ? EVENTTYPE_ERRMSG : dbei.eventType; if (dbei.eventType == EVENTTYPE_ERRMSG) dbei.szModule = (char *)TranslateT("Sample error message"); - dbei.pBlob = (uint8_t *)szText.get(); - dbei.cbBlob = (int)mir_strlen((char *)dbei.pBlob) + 1; + dbei.pBlob = (uint8_t *)szText.detach(); + dbei.cbBlob = (int)mir_strlen((char *)dbei.pBlob); dbei.flags = (iIndex == 1 || iIndex == 3 || iIndex == 5) ? DBEF_SENT : 0; dbei.flags |= (m_bRtl ? DBEF_RTL : 0); m_lastEventTime = (iIndex == 4 || iIndex == 5) ? time(0) - 1 : 0; @@ -639,10 +639,10 @@ public: m_dwFlags = (m_bRtl ? m_dwFlags | MWF_LOG_RTL : m_dwFlags & ~MWF_LOG_RTL); m_dwFlags = (iIndex == 0 || iIndex == 1) ? m_dwFlags & ~MWF_LOG_GROUPMODE : m_dwFlags | MWF_LOG_GROUPMODE; mir_snwprintf(m_wszMyNickname, TranslateT("My Nickname")); - LOG()->LogEvents(0, 1, false, &dbei); + LOG()->LogEvents(0, 0, false, &dbei); if (m_bFirstUse) { if (m_bRtl) - LOG()->LogEvents(0, 1, false, &dbei); + LOG()->LogEvents(0, 0, false, &dbei); m_bFirstUse = false; } if (m_bChanged) diff --git a/plugins/TabSRMM/src/msgs.h b/plugins/TabSRMM/src/msgs.h index ba0af19cfd..e92ab942e1 100644 --- a/plugins/TabSRMM/src/msgs.h +++ b/plugins/TabSRMM/src/msgs.h @@ -631,7 +631,7 @@ public: return ((CLogWindow *)m_pLog); } - void LogEvent(DBEVENTINFO &dbei); + void LogEvent(DB::EventInfo &dbei); void DM_OptionsApplied(bool bRemakeLog = true); void DM_RecalcPictureSize(void); @@ -687,7 +687,8 @@ public: CSuper(pDlg) {} - char *CreateRTFFromDbEvent(struct LogStreamData *streamData); + void CreateRtfHeader(RtfLogStreamData *streamData) override; + bool CreateRtfEvent(RtfLogStreamData *streamData, DB::EventInfo &dbei) override; void AppendUnicodeString(CMStringA &str, const wchar_t *pwszBuf) override; void Attach() override; @@ -706,7 +707,7 @@ public: return m_rtf.GetRichTextRtf(bText, bSelection); } - void LogEvents(MEVENT hDbEventFirst, int count, bool bAppend, DBEVENTINFO *dbei); + void LogEvents(MEVENT hDbEventFirst, int count, bool bAppend, DB::EventInfo *dbei); void ReplaceIcons(LONG startAt, int fAppend, BOOL isSent); void ScrollToBottom(bool, bool); diff --git a/plugins/TabSRMM/src/sendqueue.cpp b/plugins/TabSRMM/src/sendqueue.cpp index 9b4e11b583..4f6c35a3cd 100644 --- a/plugins/TabSRMM/src/sendqueue.cpp +++ b/plugins/TabSRMM/src/sendqueue.cpp @@ -340,7 +340,7 @@ void SendQueue::logError(CMsgDialog *dat, int iSendJobIndex, const wchar_t *szEr return; size_t iMsgLen; - DBEVENTINFO dbei = {}; + DB::EventInfo dbei; dbei.eventType = EVENTTYPE_ERRMSG; if (iSendJobIndex >= 0) { dbei.pBlob = (uint8_t *)m_jobs[iSendJobIndex].szSendBuffer; @@ -356,6 +356,8 @@ void SendQueue::logError(CMsgDialog *dat, int iSendJobIndex, const wchar_t *szEr dbei.timestamp = time(0); dbei.szModule = (char *)szErrMsg; dat->LogEvent(dbei); + + dbei.pBlob = nullptr; } ///////////////////////////////////////////////////////////////////////////////////////// @@ -539,14 +541,13 @@ int SendQueue::doSendLater(int iJobIndex, CMsgDialog *dat, MCONTACT hContact, bo else szNote = TranslateT("The send later feature is not available on this protocol."); - T2Utf utfText(szNote); - DBEVENTINFO dbei = {}; + DB::EventInfo dbei; dbei.eventType = EVENTTYPE_MESSAGE; dbei.flags = DBEF_SENT | DBEF_UTF; dbei.szModule = Proto_GetBaseAccountName(dat->m_hContact); dbei.timestamp = time(0); - dbei.cbBlob = (int)mir_strlen(utfText) + 1; - dbei.pBlob = (uint8_t*)(char*)utfText; + dbei.pBlob = (uint8_t*)mir_utf8encodeW(szNote); + dbei.cbBlob = (int)mir_strlen((char*)dbei.pBlob); dat->LogEvent(dbei); if (dat->m_hDbEventFirst == 0) -- cgit v1.2.3