From 2d76859d808f2f3576d0a1784c8350752f31da37 Mon Sep 17 00:00:00 2001
From: George Hazan <ghazan@miranda.im>
Date: Wed, 17 Jun 2020 19:26:25 +0300
Subject: fix for a last message time storing

---
 plugins/Clist_modern/src/modern_clc.cpp            |  3 ++-
 plugins/Clist_modern/src/modern_clistsettings.cpp  |  7 ++-----
 plugins/Clist_modern/src/modern_contact.cpp        | 11 +++++++----
 plugins/Clist_modern/src/modern_global_structure.h | 13 +++++++++++--
 4 files changed, 22 insertions(+), 12 deletions(-)

(limited to 'plugins')

diff --git a/plugins/Clist_modern/src/modern_clc.cpp b/plugins/Clist_modern/src/modern_clc.cpp
index 828ee36779..622deee541 100644
--- a/plugins/Clist_modern/src/modern_clc.cpp
+++ b/plugins/Clist_modern/src/modern_clc.cpp
@@ -164,7 +164,8 @@ static int clcHookDbEventAdded(WPARAM hContact, LPARAM hDbEvent)
 			ClcCacheEntry *pdnce = Clist_GetCacheEntry(hContact);
 			if (pdnce) {
 				pdnce->dwLastMsgTime = dbei.timestamp;
-				Clist_Broadcast(CLM_AUTOREBUILD, hContact, 0);
+				if (g_CluiData.hasSort(SORTBY_LASTMSG))
+					Clist_Broadcast(CLM_AUTOREBUILD, hContact, 0);
 			}
 		}
 	}
diff --git a/plugins/Clist_modern/src/modern_clistsettings.cpp b/plugins/Clist_modern/src/modern_clistsettings.cpp
index 89fdb63d6c..2a91f698a9 100644
--- a/plugins/Clist_modern/src/modern_clistsettings.cpp
+++ b/plugins/Clist_modern/src/modern_clistsettings.cpp
@@ -63,11 +63,8 @@ void cliCheckCacheItem(ClcCacheEntry *pdnce)
 		pdnce->m_iStatus = GetStatusForContact(pdnce->hContact, pdnce->szProto);
 
 	// this variable isn't filled inside cliCreateCacheItem() because the filter could be changed dynamically
-	if (pdnce->dwLastMsgTime == -1 && g_CluiData.bFilterEffective & (CLVM_FILTER_LASTMSG | CLVM_FILTER_LASTMSG_NEWERTHAN | CLVM_FILTER_LASTMSG_OLDERTHAN)) {
-		pdnce->dwLastMsgTime = g_plugin.getDword(pdnce->hContact, "mf_lastmsg");
-		if (pdnce->dwLastMsgTime == 0)
-			pdnce->dwLastMsgTime = CompareContacts2_getLMTime(pdnce->hContact);
-	}
+	if (pdnce->dwLastMsgTime == -1 && g_CluiData.bFilterEffective & (CLVM_FILTER_LASTMSG | CLVM_FILTER_LASTMSG_NEWERTHAN | CLVM_FILTER_LASTMSG_OLDERTHAN))
+		pdnce->dwLastMsgTime = CompareContacts2_getLMTime(pdnce->hContact);
 
 	corecli.pfnCheckCacheItem(pdnce);
 }
diff --git a/plugins/Clist_modern/src/modern_contact.cpp b/plugins/Clist_modern/src/modern_contact.cpp
index eb4285f2eb..2654b00063 100644
--- a/plugins/Clist_modern/src/modern_contact.cpp
+++ b/plugins/Clist_modern/src/modern_contact.cpp
@@ -58,13 +58,16 @@ static int GetStatusModeOrdering(int statusMode)
 
 DWORD CompareContacts2_getLMTime(MCONTACT hContact)
 {
-	MEVENT hDbEvent = db_event_last(hContact);
-	while (hDbEvent) {
+	DWORD ret = g_plugin.getDword(hContact, "mf_lastmsg");
+	if (ret != 0)
+		return ret;
+
+	DB::ECPTR pCursor(DB::EventsRev(hContact));
+	while (MEVENT hDbEvent = pCursor.FetchNext()) {
 		DBEVENTINFO dbei = {};
 		db_event_get(hDbEvent, &dbei);
-		if (dbei.eventType == EVENTTYPE_MESSAGE && !(dbei.flags & DBEF_SENT))
+		if ((dbei.eventType == EVENTTYPE_MESSAGE || dbei.eventType == EVENTTYPE_FILE) && !(dbei.flags & DBEF_SENT))
 			return dbei.timestamp;
-		hDbEvent = db_event_prev(hContact, hDbEvent);
 	}
 	return 0;
 }
diff --git a/plugins/Clist_modern/src/modern_global_structure.h b/plugins/Clist_modern/src/modern_global_structure.h
index 03d45eef0c..ba134cdaae 100644
--- a/plugins/Clist_modern/src/modern_global_structure.h
+++ b/plugins/Clist_modern/src/modern_global_structure.h
@@ -3,7 +3,7 @@
 #ifndef modern_global_structure_h__
 #define modern_global_structure_h__
 
-typedef struct tagCLUIDATA
+struct CLUIDATA
 {
 	/************************************
 	**         Global variables       **
@@ -75,7 +75,16 @@ typedef struct tagCLUIDATA
 	HANDLE  hEventSkinServicesCreated;
 
 	int     nGapBetweenTitlebar;
-} CLUIDATA;
+
+	__inline bool hasSort(int order) const
+	{
+		for (auto &it : bSortByOrder)
+			if (it == order)
+				return true;
+
+		return false;
+	}
+};
 
 EXTERN_C CLUIDATA g_CluiData;
 
-- 
cgit v1.2.3