diff options
author | George Hazan <george.hazan@gmail.com> | 2014-07-07 13:18:55 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2014-07-07 13:18:55 +0000 |
commit | 198f78183833d6f9c7256286c23acda597575c2a (patch) | |
tree | d99d62d2b619b62333fce496a093e1bddf9e7162 /plugins/FavContacts/src/contact_cache.cpp | |
parent | bf7e27ebc315e2663f6f271e9e631c724ad64082 (diff) |
FavContacts:
- MCONTACTs sometimes were drawn as groups;
- code cleaning;
git-svn-id: http://svn.miranda-ng.org/main/trunk@9722 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/FavContacts/src/contact_cache.cpp')
-rw-r--r-- | plugins/FavContacts/src/contact_cache.cpp | 129 |
1 files changed, 63 insertions, 66 deletions
diff --git a/plugins/FavContacts/src/contact_cache.cpp b/plugins/FavContacts/src/contact_cache.cpp index 2ba319b59a..bda23904e2 100644 --- a/plugins/FavContacts/src/contact_cache.cpp +++ b/plugins/FavContacts/src/contact_cache.cpp @@ -1,43 +1,65 @@ #include "headers.h"
+static int CompareItem(const CContactCache::TContactInfo *p1, const CContactCache::TContactInfo *p2)
+{
+ if (p1->rate > p2->rate) return -1;
+ if (p1->rate < p2->rate) return 1;
+ return 0;
+}
+
+CContactCache::CContactCache() :
+ m_cache(50, CompareItem)
+{
+ InitializeCriticalSection(&m_cs);
+
+ int(__cdecl CContactCache::*pfn)(WPARAM, LPARAM);
+ pfn = &CContactCache::OnDbEventAdded;
+ HookEventObj(ME_DB_EVENT_ADDED, *(MIRANDAHOOKOBJ *)&pfn, this);
+
+ Rebuild();
+}
+
+CContactCache::~CContactCache()
+{
+ for (int i = 0; i < m_cache.getCount(); i++)
+ delete m_cache[i];
+
+ DeleteCriticalSection(&m_cs);
+}
+
int __cdecl CContactCache::OnDbEventAdded(WPARAM hContact, LPARAM lParam)
{
HANDLE hEvent = (HANDLE)lParam;
- DBEVENTINFO dbei = {0};
- dbei.cbSize = sizeof(dbei);
+ DBEVENTINFO dbei = { sizeof(dbei) };
db_event_get(hEvent, &dbei);
- if (dbei.eventType != EVENTTYPE_MESSAGE) return 0;
+ if (dbei.eventType != EVENTTYPE_MESSAGE)
+ return 0;
float weight = GetEventWeight(time(NULL) - dbei.timestamp);
float q = GetTimeWeight(time(NULL) - m_lastUpdate);
m_lastUpdate = time(NULL);
- if (!weight) return 0;
-
- Lock();
- bool found = false;
- for (int i = 0; i < m_cache.getCount(); ++i)
- {
- m_cache[i].rate *= q;
- if (m_cache[i].hContact == hContact)
- {
- found = true;
- m_cache[i].rate += weight;
+ if (!weight)
+ return 0;
+
+ TContactInfo *pFound = NULL;
+ mir_cslock lck(m_cs);
+ for (int i = m_cache.getCount()-1; i >= 0; i--) {
+ TContactInfo *p = m_cache[i];
+ p->rate *= q;
+ if (p->hContact == hContact) {
+ p->rate += weight;
+ pFound = p;
+ m_cache.remove(i); // reinsert to maintain the sort order
}
}
- if (!found)
- {
- TContactInfo *info = new TContactInfo;
- info->hContact = hContact;
- info->rate = weight;
- m_cache.insert(info);
- } else
- {
- qsort(m_cache.getArray(), m_cache.getCount(), sizeof(TContactInfo *), TContactInfo::cmp2);
+ if (!pFound) {
+ pFound = new TContactInfo;
+ pFound->hContact = hContact;
+ pFound->rate = weight;
}
-
- Unlock();
+ m_cache.insert(pFound);
return 0;
}
@@ -59,22 +81,6 @@ float CContactCache::GetTimeWeight(unsigned long age) return exp(age * (log(ceil) - log(floor)) / depth);
}
-CContactCache::CContactCache(): m_cache(50, TContactInfo::cmp)
-{
- InitializeCriticalSection(&m_cs);
-
- int (__cdecl CContactCache::*pfn)(WPARAM, LPARAM);
- pfn = &CContactCache::OnDbEventAdded;
- HookEventObj(ME_DB_EVENT_ADDED, *(MIRANDAHOOKOBJ *)&pfn, this);
-
- Rebuild();
-}
-
-CContactCache::~CContactCache()
-{
- DeleteCriticalSection(&m_cs);
-}
-
void CContactCache::Rebuild()
{
unsigned long timestamp = time(NULL);
@@ -88,8 +94,7 @@ void CContactCache::Rebuild() for (HANDLE hEvent = db_event_last(hContact); hEvent; hEvent = db_event_prev(hContact, hEvent)) {
DBEVENTINFO dbei = { sizeof(dbei) };
if (!db_event_get(hEvent, &dbei)) {
- if (float weight = GetEventWeight(timestamp - dbei.timestamp))
- {
+ if (float weight = GetEventWeight(timestamp - dbei.timestamp)) {
if (dbei.eventType == EVENTTYPE_MESSAGE)
info->rate += weight;
}
@@ -104,32 +109,29 @@ void CContactCache::Rebuild() MCONTACT CContactCache::get(int rate)
{
if (rate >= 0 && rate < m_cache.getCount())
- return m_cache[rate].hContact;
+ return m_cache[rate]->hContact;
return NULL;
}
float CContactCache::getWeight(int rate)
{
if (rate >= 0 && rate < m_cache.getCount())
- return m_cache[rate].rate;
+ return m_cache[rate]->rate;
return -1;
}
static bool AppendInfo(TCHAR *buf, int size, MCONTACT hContact, int info)
{
- CONTACTINFO ci = {0};
+ CONTACTINFO ci = { 0 };
ci.cbSize = sizeof(ci);
ci.hContact = hContact;
ci.dwFlag = info;
ci.dwFlag |= CNF_UNICODE;
-
bool ret = false;
- if (!CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)&ci) && (ci.type == CNFT_ASCIIZ) && ci.pszVal)
- {
- if (*ci.pszVal && (lstrlen(ci.pszVal) < size-2))
- {
+ if (!CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)&ci) && (ci.type == CNFT_ASCIIZ) && ci.pszVal) {
+ if (*ci.pszVal && (lstrlen(ci.pszVal) < size - 2)) {
lstrcpy(buf, ci.pszVal);
ret = true;
}
@@ -147,16 +149,15 @@ void CContactCache::TContactInfo::LoadInfo() p[0] = p[1] = 0;
static const int items[] = {
- CNF_FIRSTNAME, CNF_LASTNAME, CNF_NICK , CNF_CUSTOMNICK, CNF_EMAIL, CNF_CITY, CNF_STATE,
+ CNF_FIRSTNAME, CNF_LASTNAME, CNF_NICK, CNF_CUSTOMNICK, CNF_EMAIL, CNF_CITY, CNF_STATE,
CNF_COUNTRY, CNF_PHONE, CNF_HOMEPAGE, CNF_ABOUT, CNF_UNIQUEID, CNF_MYNOTES, CNF_STREET,
CNF_CONAME, CNF_CODEPT, CNF_COCITY, CNF_COSTATE, CNF_COSTREET, CNF_COCOUNTRY
};
for (int i = 0; i < SIZEOF(items); ++i)
- {
if (AppendInfo(p, SIZEOF(info) - (p - info), hContact, items[i]))
p += lstrlen(p) + 1;
- }
+
*p = 0;
infoLoaded = true;
@@ -173,7 +174,7 @@ TCHAR *nb_stristr(TCHAR *str, TCHAR *substr) CharUpperBuff(str_up, lstrlen(str_up));
CharUpperBuff(substr_up, lstrlen(substr_up));
- TCHAR* p = _tcsstr(str_up, substr_up);
+ TCHAR *p = _tcsstr(str_up, substr_up);
return p ? (str + (p - str_up)) : NULL;
}
@@ -181,33 +182,29 @@ bool CContactCache::filter(int rate, TCHAR *str) {
if (!str || !*str)
return true;
- m_cache[rate].LoadInfo();
+ m_cache[rate]->LoadInfo();
HKL kbdLayoutActive = GetKeyboardLayout(GetCurrentThreadId());
HKL kbdLayouts[10];
int nKbdLayouts = GetKeyboardLayoutList(SIZEOF(kbdLayouts), kbdLayouts);
TCHAR buf[256];
- BYTE keyState[256] = {0};
+ BYTE keyState[256] = { 0 };
- for (int iLayout = 0; iLayout < nKbdLayouts; ++iLayout)
- {
+ for (int iLayout = 0; iLayout < nKbdLayouts; ++iLayout) {
if (kbdLayoutActive == kbdLayouts[iLayout])
- {
lstrcpy(buf, str);
- } else
- {
+ else {
int i;
- for (i = 0; str[i]; ++i)
- {
+ for (i = 0; str[i]; ++i) {
UINT vk = VkKeyScanEx(str[i], kbdLayoutActive);
UINT scan = MapVirtualKeyEx(vk, 0, kbdLayoutActive);
- ToUnicodeEx(vk, scan, keyState, buf+i, SIZEOF(buf)-i, 0, kbdLayouts[iLayout]);
+ ToUnicodeEx(vk, scan, keyState, buf + i, SIZEOF(buf) - i, 0, kbdLayouts[iLayout]);
}
buf[i] = 0;
}
- for (TCHAR *p = m_cache[rate].info; p && *p; p = p + lstrlen(p) + 1)
+ for (TCHAR *p = m_cache[rate]->info; p && *p; p = p + lstrlen(p) + 1)
if (nb_stristr(p, buf))
return true;
}
|