summaryrefslogtreecommitdiff
path: root/plugins/AVS/src/poll.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/AVS/src/poll.cpp')
-rw-r--r--plugins/AVS/src/poll.cpp162
1 files changed, 65 insertions, 97 deletions
diff --git a/plugins/AVS/src/poll.cpp b/plugins/AVS/src/poll.cpp
index 3fb4263497..dd59cdd46f 100644
--- a/plugins/AVS/src/poll.cpp
+++ b/plugins/AVS/src/poll.cpp
@@ -54,9 +54,7 @@ BOOL Proto_IsFetchingAlwaysAllowed(const char *proto);
struct CacheNode *FindAvatarInCache(HANDLE hContact, BOOL add, BOOL findAny = FALSE);
-extern HANDLE hEventContactAvatarChanged;
extern BOOL g_AvatarHistoryAvail;
-extern FI_INTERFACE *fei;
#ifdef _DEBUG
int _DebugTrace(const char *fmt, ...);
@@ -78,16 +76,12 @@ static int waitTime;
void InitPolls()
{
waitTime = REQUEST_WAIT_TIME;
- InitializeCriticalSection( &cs );
+ InitializeCriticalSection(&cs);
// Init request queue
mir_forkthread(RequestThread, NULL);
}
-void FreePolls()
-{
-}
-
// Return true if this protocol can have avatar requested
static BOOL PollProtocolCanHaveAvatar(const char *szProto)
{
@@ -101,60 +95,52 @@ static BOOL PollProtocolCanHaveAvatar(const char *szProto)
// Return true if this protocol has to be checked
static BOOL PollCheckProtocol(const char *szProto)
{
- return DBGetContactSettingByte(NULL, AVS_MODULE, szProto, 1);
+ return db_get_b(NULL, AVS_MODULE, szProto, 1);
}
// Return true if this contact can have avatar requested
static BOOL PollContactCanHaveAvatar(HANDLE hContact, const char *szProto)
{
- int status = DBGetContactSettingWord(hContact, szProto, "Status", ID_STATUS_OFFLINE);
+ int status = db_get_w(hContact, szProto, "Status", ID_STATUS_OFFLINE);
return (Proto_IsFetchingAlwaysAllowed(szProto) || status != ID_STATUS_OFFLINE)
- && !DBGetContactSettingByte(hContact, "CList", "NotOnList", 0)
- && DBGetContactSettingByte(hContact, "CList", "ApparentMode", 0) != ID_STATUS_OFFLINE;
+ && !db_get_b(hContact, "CList", "NotOnList", 0) && db_get_b(hContact, "CList", "ApparentMode", 0) != ID_STATUS_OFFLINE;
}
// Return true if this contact has to be checked
static BOOL PollCheckContact(HANDLE hContact, const char *szProto)
{
- return !DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0)
- && FindAvatarInCache(hContact, FALSE, TRUE) != NULL;
+ return !db_get_b(hContact, "ContactPhoto", "Locked", 0) && FindAvatarInCache(hContact, FALSE, TRUE) != NULL;
}
static void QueueRemove(HANDLE hContact)
{
- EnterCriticalSection(&cs);
+ mir_cslock lck(cs);
for (int i = queue.getCount()-1 ; i >= 0 ; i-- ) {
QueueItem& item = queue[i];
if (item.hContact == hContact)
queue.remove(i);
}
-
- LeaveCriticalSection(&cs);
}
static void QueueAdd(HANDLE hContact, int waitTime)
{
- if(fei == NULL)
+ if (fei == NULL || g_shutDown)
return;
- EnterCriticalSection(&cs);
+ mir_cslock lck(cs);
// Only add if not exists yet
- int i;
- for (i = queue.getCount()-1; i >= 0; i--)
- if ( queue[i].hContact == hContact)
- break;
-
- if (i < 0) {
- QueueItem *item = new QueueItem;
- if (item != NULL) {
- item->hContact = hContact;
- item->check_time = GetTickCount() + waitTime;
- queue.insert(item);
- } }
-
- LeaveCriticalSection(&cs);
+ for (int i = queue.getCount()-1; i >= 0; i--)
+ if (queue[i].hContact == hContact)
+ return;
+
+ QueueItem *item = new QueueItem;
+ if (item != NULL) {
+ item->hContact = hContact;
+ item->check_time = GetTickCount() + waitTime;
+ queue.insert(item);
+ }
}
// Add an contact to a queue
@@ -167,18 +153,17 @@ void ProcessAvatarInfo(HANDLE hContact, int type, PROTO_AVATAR_INFORMATIONT *pai
{
QueueRemove(hContact);
- if (type == GAIR_SUCCESS)
- {
+ if (type == GAIR_SUCCESS) {
if (pai == NULL)
return;
// Fix settings in DB
- DBDeleteContactSetting(hContact, "ContactPhoto", "NeedUpdate");
- DBDeleteContactSetting(hContact, "ContactPhoto", "RFile");
- if (!DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0))
- DBDeleteContactSetting(hContact, "ContactPhoto", "Backup");
- DBWriteContactSettingTString(hContact, "ContactPhoto", "File", pai->filename);
- DBWriteContactSettingWord(hContact, "ContactPhoto", "Format", pai->format);
+ db_unset(hContact, "ContactPhoto", "NeedUpdate");
+ db_unset(hContact, "ContactPhoto", "RFile");
+ if (!db_get_b(hContact, "ContactPhoto", "Locked", 0))
+ db_unset(hContact, "ContactPhoto", "Backup");
+ db_set_ts(hContact, "ContactPhoto", "File", pai->filename);
+ db_set_w(hContact, "ContactPhoto", "Format", pai->format);
if (pai->format == PA_FORMAT_PNG || pai->format == PA_FORMAT_JPEG
|| pai->format == PA_FORMAT_ICON || pai->format == PA_FORMAT_BMP
@@ -196,16 +181,16 @@ void ProcessAvatarInfo(HANDLE hContact, int type, PROTO_AVATAR_INFORMATIONT *pai
}
else if (type == GAIR_NOAVATAR)
{
- DBDeleteContactSetting(hContact, "ContactPhoto", "NeedUpdate");
+ db_unset(hContact, "ContactPhoto", "NeedUpdate");
- if (DBGetContactSettingByte(NULL, AVS_MODULE, "RemoveAvatarWhenContactRemoves", 1))
+ if (db_get_b(NULL, AVS_MODULE, "RemoveAvatarWhenContactRemoves", 1))
{
// Delete settings
- DBDeleteContactSetting(hContact, "ContactPhoto", "RFile");
- if (!DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0))
- DBDeleteContactSetting(hContact, "ContactPhoto", "Backup");
- DBDeleteContactSetting(hContact, "ContactPhoto", "File");
- DBDeleteContactSetting(hContact, "ContactPhoto", "Format");
+ db_unset(hContact, "ContactPhoto", "RFile");
+ if (!db_get_b(hContact, "ContactPhoto", "Locked", 0))
+ db_unset(hContact, "ContactPhoto", "Backup");
+ db_unset(hContact, "ContactPhoto", "File");
+ db_unset(hContact, "ContactPhoto", "Format");
// Fix cache
ChangeAvatar(hContact, FALSE, TRUE, 0);
@@ -214,8 +199,7 @@ void ProcessAvatarInfo(HANDLE hContact, int type, PROTO_AVATAR_INFORMATIONT *pai
else if (type == GAIR_FAILED)
{
int wait = Proto_GetDelayAfterFail(szProto);
- if (wait > 0)
- {
+ if (wait > 0) {
// Reschedule to request after needed time (and avoid requests before that)
EnterCriticalSection(&cs);
QueueRemove(hContact);
@@ -232,23 +216,17 @@ int FetchAvatarFor(HANDLE hContact, char *szProto = NULL)
if (szProto == NULL)
szProto = GetContactProto(hContact);
- if (szProto != NULL && PollProtocolCanHaveAvatar(szProto) && PollContactCanHaveAvatar(hContact, szProto))
- {
+ if (szProto != NULL && PollProtocolCanHaveAvatar(szProto) && PollContactCanHaveAvatar(hContact, szProto)) {
// Can have avatar, but must request it?
- if (
- (g_AvatarHistoryAvail && CallService(MS_AVATARHISTORY_ENABLED, (WPARAM) hContact, 0))
- || (PollCheckProtocol(szProto) && PollCheckContact(hContact, szProto))
- )
+ if ((g_AvatarHistoryAvail && CallService(MS_AVATARHISTORY_ENABLED, (WPARAM) hContact, 0))
+ || (PollCheckProtocol(szProto) && PollCheckContact(hContact, szProto)))
{
// Request it
PROTO_AVATAR_INFORMATIONT pai_s = {0};
pai_s.cbSize = sizeof(pai_s);
pai_s.hContact = hContact;
- //_DebugTrace(hContact, "schedule request");
INT_PTR res = CallProtoService(szProto, PS_GETAVATARINFOT, GAIF_FORCE, (LPARAM)&pai_s);
-
- if (res == CALLSERVICE_NOTFOUND)
- {
+ if (res == CALLSERVICE_NOTFOUND) {
PROTO_AVATAR_INFORMATION pai = {0};
pai.cbSize = sizeof(pai);
pai.hContact = hContact;
@@ -267,50 +245,40 @@ int FetchAvatarFor(HANDLE hContact, char *szProto = NULL)
static void RequestThread(void *vParam)
{
- while (!g_shutDown)
- {
+ while (!g_shutDown) {
EnterCriticalSection(&cs);
- if ( queue.getCount() == 0 )
- {
- // No items, so supend thread
+ if ( queue.getCount() == 0 ) {
+ // No items, so suspend thread
LeaveCriticalSection(&cs);
+ mir_sleep(POOL_DELAY);
+ continue;
+ }
+ // Take a look at first item
+ QueueItem& qi = queue[ queue.getCount()-1 ];
+ if (qi.check_time > GetTickCount()) {
+ // Not time to request yet, wait...
+ LeaveCriticalSection(&cs);
mir_sleep(POOL_DELAY);
+ continue;
}
- else
- {
- // Take a look at first item
- QueueItem& qi = queue[ queue.getCount()-1 ];
-
- if (qi.check_time > GetTickCount())
- {
- // Not time to request yet, wait...
- LeaveCriticalSection(&cs);
- mir_sleep(POOL_DELAY);
- }
- else
- {
- // Will request this item
- HANDLE hContact = qi.hContact;
- queue.remove( queue.getCount()-1 );
-
- QueueRemove(hContact);
-
- LeaveCriticalSection(&cs);
-
- if (FetchAvatarFor(hContact) == GAIR_WAITFOR)
- {
- // Mark to not request this contact avatar for more 30 min
- EnterCriticalSection(&cs);
- QueueRemove(hContact);
- QueueAdd(hContact, REQUEST_WAITFOR_WAIT_TIME);
- LeaveCriticalSection(&cs);
-
- // Wait a little until requesting again
- mir_sleep(REQUEST_DELAY);
- }
- }
+
+ // Will request this item
+ HANDLE hContact = qi.hContact;
+ queue.remove( queue.getCount()-1 );
+ QueueRemove(hContact);
+ LeaveCriticalSection(&cs);
+
+ if (FetchAvatarFor(hContact) == GAIR_WAITFOR) {
+ // Mark to not request this contact avatar for more 30 min
+ EnterCriticalSection(&cs);
+ QueueRemove(hContact);
+ QueueAdd(hContact, REQUEST_WAITFOR_WAIT_TIME);
+ LeaveCriticalSection(&cs);
+
+ // Wait a little until requesting again
+ mir_sleep(REQUEST_DELAY);
}
}