/* Module: buddylist.c Purpose: Manages your list of buddies in memory Author: leecher Date: 30.08.2009 */ #include #include #include "memlist.h" #include "buddylist.h" static void SetEntry(NICKENTRY *pEntry, cJSON *pNick); // ----------------------------------------------------------------------------- // Interface // ----------------------------------------------------------------------------- TYP_LIST *BuddyList_Init(void) { TYP_LIST *hList = List_Init(16); return hList; } // ----------------------------------------------------------------------------- void BuddyList_Exit(TYP_LIST *hList) { NICKENTRY *pEntry; while (pEntry=(NICKENTRY*)List_Pop(hList)) { BuddyList_FreeEntry(pEntry); free (pEntry); } List_Exit(hList); } // ----------------------------------------------------------------------------- BOOL BuddyList_Insert(TYP_LIST *hList, cJSON *pNick) { NICKENTRY *pEntry; if (pEntry=BuddyList_Find (hList, cJSON_GetObjectItem(pNick, "buid")->valuestring)) BuddyList_FreeEntry (pEntry); else { if (!(pEntry = calloc (1, sizeof(NICKENTRY)))) return FALSE; if (!List_Push(hList, pEntry)) return FALSE; } SetEntry(pEntry, pNick); pEntry->iBuddyStatus = 3; return TRUE; } // ----------------------------------------------------------------------------- BOOL BuddyList_AddTemporaryUser(TYP_LIST *hList, char *pszUser) { NICKENTRY *pEntry; if (BuddyList_Find (hList, pszUser)) return TRUE; if (!(pEntry = calloc (1, sizeof(NICKENTRY)))) return FALSE; pEntry->pszUser = strdup(pszUser); pEntry->pszAlias = strdup(pszUser); strcpy (pEntry->szStatus, "OFFLINE"); pEntry->iBuddyStatus = 2; return List_Push(hList, pEntry); } // ----------------------------------------------------------------------------- BOOL BuddyList_Remove(TYP_LIST *hList, NICKENTRY *pEntry) { NICKENTRY *pListEntry; int i, nCount; for (i=0, nCount=List_Count(hList); ipszUser, pszUser) == 0) return pEntry; } return NULL; } // ----------------------------------------------------------------------------- BOOL BuddyList_SetStatus(TYP_LIST *hList, cJSON *pNick) { NICKENTRY *pEntry; if ((pEntry = BuddyList_Find(hList, cJSON_GetObjectItem(pNick, "buid")->valuestring))) { BuddyList_FreeEntry(pEntry); SetEntry(pEntry, pNick); return TRUE; } return FALSE; } // ----------------------------------------------------------------------------- void BuddyList_FreeEntry(NICKENTRY *pEntry) { if (pEntry->pszAlias) free (pEntry->pszAlias); if (pEntry->pszUser) free (pEntry->pszUser); if (pEntry->pszStatusText) free(pEntry->pszStatusText); } // ----------------------------------------------------------------------------- // Static // ----------------------------------------------------------------------------- static void SetEntry(NICKENTRY *pEntry, cJSON *pNick) { pEntry->pszAlias = strdup(cJSON_GetObjectItem(pNick, "alias")->valuestring); pEntry->pszUser = strdup(cJSON_GetObjectItem(pNick, "buid")->valuestring); pEntry->pszStatusText = cJSON_GetObjectItem(pNick, "status")->valuestring; if (pEntry->pszStatusText) pEntry->pszStatusText = strdup(pEntry->pszStatusText); strcpy (pEntry->szStatus, cJSON_GetObjectItem(pNick, "primitive")->valuestring); }