From 27e56d478fd53d2457068fd06ece531242daa3c4 Mon Sep 17 00:00:00 2001
From: George Hazan <george.hazan@gmail.com>
Date: Thu, 14 Mar 2013 16:36:24 +0000
Subject: - fix for crash on exit - code cleaning

git-svn-id: http://svn.miranda-ng.org/main/trunk@4028 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 plugins/AVS/src/acc.cpp         |   8 +-
 plugins/AVS/src/commonheaders.h |   4 +-
 plugins/AVS/src/image_utils.cpp |  62 ++++-------
 plugins/AVS/src/main.cpp        | 223 ++++++++++++++++++----------------------
 plugins/AVS/src/options.cpp     | 184 ++++++++++++++++-----------------
 plugins/AVS/src/poll.cpp        | 162 ++++++++++++-----------------
 plugins/AVS/src/poll.h          |   1 -
 7 files changed, 282 insertions(+), 362 deletions(-)

(limited to 'plugins/AVS')

diff --git a/plugins/AVS/src/acc.cpp b/plugins/AVS/src/acc.cpp
index 598ccb6ab8..d080f74d30 100644
--- a/plugins/AVS/src/acc.cpp
+++ b/plugins/AVS/src/acc.cpp
@@ -23,8 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "commonheaders.h"
 
-extern FI_INTERFACE *fei;
-
 int GetImageFormat(TCHAR *filename);
 INT_PTR DrawAvatarPicture(WPARAM wParam, LPARAM lParam);
 INT_PTR GetAvatarBitmap(WPARAM wParam, LPARAM lParam);
@@ -155,7 +153,7 @@ void StartFlash(HWND hwnd, ACCData* data)
 	int format;
 	if (data->hContact != NULL)
 	{
-		format = DBGetContactSettingWord(data->hContact, "ContactPhoto", "Format", 0);
+		format = db_get_w(data->hContact, "ContactPhoto", "Format", 0);
 	}
 	else if (data->proto[0] != '\0')
 	{
@@ -381,7 +379,7 @@ void StartAnimatedGif(HWND hwnd, ACCData* data)
 		return;
 
 	FREE_IMAGE_FORMAT fif = fei->FI_GetFileTypeT(ace->szFilename, 0);
-	if(fif == FIF_UNKNOWN)
+	if (fif == FIF_UNKNOWN)
 		fif = fei->FI_GetFIFFromFilenameT(ace->szFilename);
 
 	data->ag.multi = fei->FI_OpenMultiBitmapT(fif, ace->szFilename, FALSE, TRUE, FALSE, GIF_LOAD256);
@@ -763,7 +761,7 @@ static LRESULT CALLBACK ACCWndProc(HWND hwnd, UINT msg,  WPARAM wParam, LPARAM l
 			}
 
 			if (data->hContact == NULL && data->proto[0] == '\0'
-				&& DBGetContactSettingByte(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1))
+				&& db_get_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1))
 			{
 				DrawText(hdc, data->hFont, rc, TranslateT("Protocols have different avatars"));
 			}
diff --git a/plugins/AVS/src/commonheaders.h b/plugins/AVS/src/commonheaders.h
index b1136e7e0f..7697653a86 100644
--- a/plugins/AVS/src/commonheaders.h
+++ b/plugins/AVS/src/commonheaders.h
@@ -82,7 +82,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  all the bad free()'s to good ones, however it's still incorrect code. The reasons for not
  changing them include:
 
-  * DBFreeVariant has a CallService() lookup
+  * db_unset has a CallService() lookup
   * free() is executed in some large loops to do with clist creation of group data
   * easy search and replace
 
@@ -102,12 +102,12 @@ struct protoPicCacheEntry : public avatarCacheEntry, public MZeroedObject
 
 extern OBJLIST<protoPicCacheEntry> g_ProtoPictures, g_MyAvatars;
 
+extern FI_INTERFACE *fei;
 
 int SetAvatarAttribute(HANDLE hContact, DWORD attrib, int mode);
 
 #define MAX_REGS(_A_) ( sizeof(_A_) / sizeof(_A_[0]))
 
-
 #define GAIR_FAILED 1000
 
 #define AVS_IGNORENOTIFY 0x1000
diff --git a/plugins/AVS/src/image_utils.cpp b/plugins/AVS/src/image_utils.cpp
index 4c1d830711..9b926ccbee 100644
--- a/plugins/AVS/src/image_utils.cpp
+++ b/plugins/AVS/src/image_utils.cpp
@@ -1,42 +1,21 @@
 #include "commonheaders.h"
 #include "image_utils.h"
 
-#include <ocidl.h>
-#include <olectl.h>
-
-/*
-Theese are the ones needed
-#include <win2k.h>
-#include <newpluginapi.h>
-#include <m_langpack.h>
-#include <m_utils.h>
-#include <m_png.h>
-#include <m_protocols.h>
-*/
-
 extern int _DebugTrace(const char *fmt, ...);
 extern int _DebugTrace(HANDLE hContact, const char *fmt, ...);
 
-
 #define GET_PIXEL(__P__, __X__, __Y__) ( __P__ + width * 4 * (__Y__) + 4 * (__X__))
 
-
-extern FI_INTERFACE *fei;
-
 // Make a bitmap all transparent, but only if it is a 32bpp
 void MakeBmpTransparent(HBITMAP hBitmap)
 {
 	BITMAP bmp;
-	DWORD dwLen;
-	BYTE *p;
-
 	GetObject(hBitmap, sizeof(bmp), &bmp);
-
 	if (bmp.bmBitsPixel != 32)
 		return;
 
-	dwLen = bmp.bmWidth * bmp.bmHeight * (bmp.bmBitsPixel / 8);
-	p = (BYTE *)malloc(dwLen);
+	DWORD dwLen = bmp.bmWidth * bmp.bmHeight * (bmp.bmBitsPixel / 8);
+	BYTE *p = (BYTE *)malloc(dwLen);
 	if (p == NULL)
 		return;
 
@@ -48,7 +27,6 @@ void MakeBmpTransparent(HBITMAP hBitmap)
 
 // Resize /////////////////////////////////////////////////////////////////////////////////////////
 
-
 // Returns a copy of the bitmap with the size especified
 // wParam = ResizeBitmap *
 // lParam = NULL
@@ -226,32 +204,32 @@ INT_PTR BmpFilterLoadBitmap32(WPARAM wParam,LPARAM lParam)
 {
 	FIBITMAP *dib32 = NULL;
 
-	if(fei == NULL)
+	if (fei == NULL)
 		return 0;
 
 	FIBITMAP *dib = (FIBITMAP *)CallService(MS_IMG_LOAD, lParam, IMGL_RETURNDIB|IMGL_TCHAR);
 
-	if(dib == NULL)
+	if (dib == NULL)
 		return 0;
 
-	if(fei->FI_GetBPP(dib) != 32) {
+	if (fei->FI_GetBPP(dib) != 32) {
 		dib32 = fei->FI_ConvertTo32Bits(dib);
 		fei->FI_Unload(dib);
 	}
 	else
 		dib32 = dib;
 
-	if(dib32) {
-		if(fei->FI_IsTransparent(dib32)) {
-			if(wParam) {
+	if (dib32) {
+		if (fei->FI_IsTransparent(dib32)) {
+			if (wParam) {
 				DWORD *dwTrans = (DWORD *)wParam;
 				*dwTrans = 1;
 			}
 		}
-		if(fei->FI_GetWidth(dib32) > 128 || fei->FI_GetHeight(dib32) > 128) {
+		if (fei->FI_GetWidth(dib32) > 128 || fei->FI_GetHeight(dib32) > 128) {
 			FIBITMAP *dib_new = fei->FI_MakeThumbnail(dib32, 128, FALSE);
 			fei->FI_Unload(dib32);
-			if(dib_new == NULL)
+			if (dib_new == NULL)
 				return 0;
 			dib32 = dib_new;
 		}
@@ -434,13 +412,13 @@ DWORD GetImgHash(HBITMAP hBitmap)
  */
 HBITMAP MakeGrayscale(HANDLE hContact, HBITMAP hBitmap)
 {
-	if(hBitmap) {
+	if (hBitmap) {
 		FIBITMAP *dib = fei->FI_CreateDIBFromHBITMAP(hBitmap);
 
-		if(dib) {
+		if (dib) {
 			FIBITMAP *dib_new = fei->FI_ConvertToGreyscale(dib);
 			fei->FI_Unload(dib);
-			if(dib_new) {
+			if (dib_new) {
 				DeleteObject(hBitmap);
 				HBITMAP hbm_new = fei->FI_CreateHBITMAPFromDIB(dib_new);
 				fei->FI_Unload(dib_new);
@@ -471,8 +449,8 @@ BOOL MakeTransparentBkg(HANDLE hContact, HBITMAP *hBitmap)
 	GetObject(*hBitmap, sizeof(bmp), &bmp);
 	width = bmp.bmWidth;
 	height = bmp.bmHeight;
-	colorDiff = DBGetContactSettingWord(hContact, "ContactPhoto", "TranspBkgColorDiff",
-					DBGetContactSettingWord(0, AVS_MODULE, "TranspBkgColorDiff", 10));
+	colorDiff = db_get_w(hContact, "ContactPhoto", "TranspBkgColorDiff",
+					db_get_w(0, AVS_MODULE, "TranspBkgColorDiff", 10));
 
 	// Min 5x5 to easy things in loop
 	if (width <= 4 || height <= 4)
@@ -580,8 +558,8 @@ BOOL MakeTransparentBkg(HANDLE hContact, HBITMAP *hBitmap)
 			count++;
 	}
 
-	if (count < DBGetContactSettingWord(hContact, "ContactPhoto", "TranspBkgNumPoints",
-						DBGetContactSettingWord(0, AVS_MODULE, "TranspBkgNumPoints", 5)))
+	if (count < db_get_w(hContact, "ContactPhoto", "TranspBkgNumPoints",
+						db_get_w(0, AVS_MODULE, "TranspBkgNumPoints", 5)))
 	{
 		if (hBmpTmp != *hBitmap) DeleteObject(hBmpTmp);
 		free(p);
@@ -610,8 +588,8 @@ BOOL MakeTransparentBkg(HANDLE hContact, HBITMAP *hBitmap)
 		}
 	}
 
-	if (maxCount < DBGetContactSettingWord(hContact, "ContactPhoto", "TranspBkgNumPoints",
-						DBGetContactSettingWord(0, AVS_MODULE, "TranspBkgNumPoints", 5)))
+	if (maxCount < db_get_w(hContact, "ContactPhoto", "TranspBkgNumPoints",
+						db_get_w(0, AVS_MODULE, "TranspBkgNumPoints", 5)))
 	{
 		// Not enought corners with the same color
 		if (hBmpTmp != *hBitmap) DeleteObject(hBmpTmp);
@@ -662,7 +640,7 @@ BOOL MakeTransparentBkg(HANDLE hContact, HBITMAP *hBitmap)
 		int topPos = 0;
 		int curPos = 0;
 		int *stack = (int *)malloc(width * height * 2 * sizeof(int));
-		bool transpProportional = (DBGetContactSettingByte(NULL, AVS_MODULE, "MakeTransparencyProportionalToColorDiff", 0) != 0);
+		bool transpProportional = (db_get_b(NULL, AVS_MODULE, "MakeTransparencyProportionalToColorDiff", 0) != 0);
 
 		if (stack == NULL)
 		{
diff --git a/plugins/AVS/src/main.cpp b/plugins/AVS/src/main.cpp
index ca1f696a64..6c95ec7026 100644
--- a/plugins/AVS/src/main.cpp
+++ b/plugins/AVS/src/main.cpp
@@ -23,8 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "commonheaders.h"
 
-HINSTANCE g_hInst = 0;
-int hLangpack;
+HINSTANCE      g_hInst = 0;
+int            hLangpack;
 
 static TCHAR   g_szDataPath[MAX_PATH];		// user datae path (read at startup only)
 static BOOL    g_MetaAvail = FALSE;
@@ -34,7 +34,8 @@ static long    hwndSetMyAvatar = 0;
 static HANDLE  hMyAvatarsFolder;
 static HANDLE  hGlobalAvatarFolder;
 static HANDLE  hLoaderEvent;
-HANDLE  hEventChanged, hEventContactAvatarChanged, hMyAvatarChanged;
+static HANDLE  hEventContactAvatarChanged, hMyAvatarChanged;
+HANDLE         hEventChanged;
 
 HICON	g_hIcon = 0;
 
@@ -225,11 +226,11 @@ static void NotifyMetaAware(HANDLE hContact, struct CacheNode *node = NULL, AVAT
 
 	NotifyEventHooks(hEventChanged, (WPARAM)hContact, (LPARAM)ace);
 
-	if (g_MetaAvail && (node->dwFlags & MC_ISSUBCONTACT) && DBGetContactSettingByte(NULL, g_szMetaName, "Enabled", 0)) {
-		HANDLE hMasterContact = (HANDLE)DBGetContactSettingDword(hContact, g_szMetaName, "Handle", 0);
+	if (g_MetaAvail && (node->dwFlags & MC_ISSUBCONTACT) && db_get_b(NULL, g_szMetaName, "Enabled", 0)) {
+		HANDLE hMasterContact = (HANDLE)db_get_dw(hContact, g_szMetaName, "Handle", 0);
 
 		if (hMasterContact && (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hMasterContact, 0) == hContact &&
-			!DBGetContactSettingByte(hMasterContact, "ContactPhoto", "Locked", 0))
+			!db_get_b(hMasterContact, "ContactPhoto", "Locked", 0))
 			NotifyEventHooks(hEventChanged, (WPARAM)hMasterContact, (LPARAM)ace);
 	}
 	if (node->dwFlags & AVH_MUSTNOTIFY) {
@@ -247,7 +248,7 @@ static void NotifyMetaAware(HANDLE hContact, struct CacheNode *node = NULL, AVAT
 			char *szProto = GetContactProto(hContact);
 			if (szProto != NULL) {
 				DBVARIANT dbv = {0};
-				if (!DBGetContactSetting(hContact, szProto, "AvatarHash", &dbv)) {
+				if ( !DBGetContactSettingString(hContact, szProto, "AvatarHash", &dbv)) {
 					if (dbv.type == DBVT_TCHAR) {
 						_tcsncpy(cacn.hash, dbv.ptszVal, SIZEOF(cacn.hash));
 					} else if (dbv.type == DBVT_BLOB) {
@@ -277,7 +278,7 @@ static void NotifyMetaAware(HANDLE hContact, struct CacheNode *node = NULL, AVAT
 							cacn.hash[i*4+3] = '=';
 						}
 					}
-					DBFreeVariant(&dbv);
+					db_free(&dbv);
 				}
 			}
 
@@ -357,9 +358,9 @@ void MakePathRelative(HANDLE hContact, TCHAR *path)
 
 	size_t result = AVS_pathToRelative(path, szFinalPath);
 	if (result && lstrlen(szFinalPath) > 0) {
-		DBWriteContactSettingTString(hContact, "ContactPhoto", "RFile", szFinalPath);
-		if (!DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0))
-			DBWriteContactSettingTString(hContact, "ContactPhoto", "Backup", szFinalPath);
+		db_set_ts(hContact, "ContactPhoto", "RFile", szFinalPath);
+		if (!db_get_b(hContact, "ContactPhoto", "Locked", 0))
+			db_set_ts(hContact, "ContactPhoto", "Backup", szFinalPath);
 	}
 }
 
@@ -370,39 +371,36 @@ void MakePathRelative(HANDLE hContact, TCHAR *path)
 
 static void MakePathRelative(HANDLE hContact)
 {
-	DBVARIANT dbv = {0};
-
-	if (!DBGetContactSetting(hContact, "ContactPhoto", "File", &dbv)) {
-		if (dbv.type == DBVT_TCHAR) {
-			MakePathRelative(hContact, dbv.ptszVal);
-		}
-		DBFreeVariant(&dbv);
+	DBVARIANT dbv;
+	if ( !DBGetContactSettingTStrring(hContact, "ContactPhoto", "File", &dbv)) {
+		MakePathRelative(hContact, dbv.ptszVal);
+		db_free(&dbv);
 	}
 }
 
 static void ResetTranspSettings(HANDLE hContact)
 {
-	DBDeleteContactSetting(hContact, "ContactPhoto", "MakeTransparentBkg");
-	DBDeleteContactSetting(hContact, "ContactPhoto", "TranspBkgNumPoints");
-	DBDeleteContactSetting(hContact, "ContactPhoto", "TranspBkgColorDiff");
+	db_unset(hContact, "ContactPhoto", "MakeTransparentBkg");
+	db_unset(hContact, "ContactPhoto", "TranspBkgNumPoints");
+	db_unset(hContact, "ContactPhoto", "TranspBkgColorDiff");
 }
 
 static TCHAR *getJGMailID(char *szProto)
 {
-	static TCHAR szJID[MAX_PATH+1];
-	DBVARIANT dbva={0}, dbvb={0};
+	static TCHAR szJID[MAX_PATH+1]; szJID[0] = '\0';
 
-	szJID[0] = '\0';
-	if (DBGetContactSettingTString(NULL, szProto, "LoginName", &dbva))
+	DBVARIANT dbva, dbvb;
+	if ( DBGetContactSettingTString(NULL, szProto, "LoginName", &dbva))
 		return szJID;
-	if (DBGetContactSettingTString(NULL, szProto, "LoginServer", &dbvb)) {
-		DBFreeVariant(&dbva);
+
+	if ( DBGetContactSettingTString(NULL, szProto, "LoginServer", &dbvb)) {
+		db_free(&dbva);
 		return szJID;
 	}
 
 	mir_sntprintf(szJID, SIZEOF(szJID), _T("%s@%s"), dbva.ptszVal, dbvb.ptszVal);
-	DBFreeVariant(&dbva);
-	DBFreeVariant(&dbvb);
+	db_free(&dbva);
+	db_free(&dbvb);
 	return szJID;
 }
 
@@ -427,22 +425,22 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 
 	if (szProto == NULL) {
 		char *proto = GetContactProto(hContact);
-		if (proto == NULL || !DBGetContactSettingByte(NULL, AVS_MODULE, proto, 1)) {
+		if (proto == NULL || !db_get_b(NULL, AVS_MODULE, proto, 1)) {
 			return -1;
 		}
 
-		if (DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0)
+		if (db_get_b(hContact, "ContactPhoto", "Locked", 0)
 			&& !DBGetContactSettingTString(hContact, "ContactPhoto", "Backup", &dbv)) {
 				AVS_pathToAbsolute(dbv.ptszVal, tszFilename);
-				DBFreeVariant(&dbv);
+				db_free(&dbv);
 		}
 		else if (!DBGetContactSettingTString(hContact, "ContactPhoto", "RFile", &dbv)) {
 			AVS_pathToAbsolute(dbv.ptszVal, tszFilename);
-			DBFreeVariant(&dbv);
+			db_free(&dbv);
 		}
 		else if (!DBGetContactSettingTString(hContact, "ContactPhoto", "File", &dbv)) {
 			AVS_pathToAbsolute(dbv.ptszVal, tszFilename);
-			DBFreeVariant(&dbv);
+			db_free(&dbv);
 		}
 		else {
 			return -2;
@@ -452,13 +450,13 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 		if (hContact == 0) {				// create a protocol picture in the proto picture cache
 			if (!DBGetContactSettingTString(NULL, PPICT_MODULE, szProto, &dbv)) {
 				AVS_pathToAbsolute(dbv.ptszVal, tszFilename);
-				DBFreeVariant(&dbv);
+				db_free(&dbv);
 			}
 			else {
 				if (lstrcmpA(szProto, AVS_DEFAULT)) {
 					if (!DBGetContactSettingTString(NULL, PPICT_MODULE, AVS_DEFAULT, &dbv)) {
 						AVS_pathToAbsolute(dbv.ptszVal, tszFilename);
-						DBFreeVariant(&dbv);
+						db_free(&dbv);
 					}
 
 					if (!strstr(szProto, "Global avatar for")) {
@@ -469,7 +467,7 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 						mir_snprintf(key, SIZEOF(key), "Global avatar for %s accounts", pdescr->szProtoName);
 						if (!DBGetContactSettingTString(NULL, PPICT_MODULE, key, &dbv)) {
 							AVS_pathToAbsolute(dbv.ptszVal, tszFilename);
-							DBFreeVariant(&dbv);
+							db_free(&dbv);
 						}
 					}
 				}
@@ -483,7 +481,7 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 					return -10;
 
 				AVS_pathToAbsolute(dbv.ptszVal, tszFilename);
-				DBFreeVariant(&dbv);
+				db_free(&dbv);
 			}
 			else if (ProtoServiceExists(szProto, PS_GETMYAVATART)) {
 				if (CallProtoService(szProto, PS_GETMYAVATART, (WPARAM)tszFilename, (LPARAM)MAX_PATH))
@@ -498,7 +496,7 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 			}
 			else if (!DBGetContactSettingTString(NULL, szProto, "AvatarFile", &dbv)) {
 				AVS_pathToAbsolute(dbv.ptszVal, tszFilename);
-				DBFreeVariant(&dbv);
+				db_free(&dbv);
 			}
 			else return -1;
 		}
@@ -531,13 +529,13 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 
 		ace->cbSize = sizeof(avatarCacheEntry);
 		ace->dwFlags = AVS_BITMAP_VALID;
-		if (hContact != NULL && DBGetContactSettingByte(hContact, "ContactPhoto", "Hidden", 0))
+		if (hContact != NULL && db_get_b(hContact, "ContactPhoto", "Hidden", 0))
 			ace->dwFlags |= AVS_HIDEONCLIST;
 		ace->hContact = hContact;
 		ace->bmHeight = bminfo.bmHeight;
 		ace->bmWidth = bminfo.bmWidth;
 
-		BOOL noTransparency = DBGetContactSettingByte(0, AVS_MODULE, "RemoveAllTransparency", 0);
+		BOOL noTransparency = db_get_b(0, AVS_MODULE, "RemoveAllTransparency", 0);
 
 		// Calc image hash
 		if (hContact != 0 && hContact != (HANDLE)-1)
@@ -545,7 +543,7 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 			// Have to reset settings? -> do it if image changed
 			DWORD imgHash = GetImgHash(ace->hbmPic);
 
-			if (imgHash != DBGetContactSettingDword(hContact, "ContactPhoto", "ImageHash", 0))
+			if (imgHash != db_get_dw(hContact, "ContactPhoto", "ImageHash", 0))
 			{
 				ResetTranspSettings(hContact);
 				DBWriteContactSettingDword(hContact, "ContactPhoto", "ImageHash", imgHash);
@@ -553,8 +551,8 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 
 			// Make transparent?
 			if (!noTransparency && !isTransparentImage
-				&& DBGetContactSettingByte(hContact, "ContactPhoto", "MakeTransparentBkg",
-				DBGetContactSettingByte(0, AVS_MODULE, "MakeTransparentBkg", 0)))
+				&& db_get_b(hContact, "ContactPhoto", "MakeTransparentBkg",
+				db_get_b(0, AVS_MODULE, "MakeTransparentBkg", 0)))
 			{
 				if (MakeTransparentBkg(hContact, &ace->hbmPic))
 				{
@@ -567,8 +565,8 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 		else if (hContact == (HANDLE)-1) // My avatars
 		{
 			if (!noTransparency && !isTransparentImage
-				&& DBGetContactSettingByte(0, AVS_MODULE, "MakeTransparentBkg", 0)
-				&& DBGetContactSettingByte(0, AVS_MODULE, "MakeMyAvatarsTransparent", 0))
+				&& db_get_b(0, AVS_MODULE, "MakeTransparentBkg", 0)
+				&& db_get_b(0, AVS_MODULE, "MakeMyAvatarsTransparent", 0))
 			{
 				if (MakeTransparentBkg(0, &ace->hbmPic))
 				{
@@ -579,7 +577,7 @@ int CreateAvatarInCache(HANDLE hContact, avatarCacheEntry *ace, char *szProto)
 			}
 		}
 
-		if (DBGetContactSettingByte(0, AVS_MODULE, "MakeGrayscale", 0))
+		if (db_get_b(0, AVS_MODULE, "MakeGrayscale", 0))
 		{
 			ace->hbmPic = MakeGrayscale(hContact, ace->hbmPic);
 		}
@@ -632,53 +630,43 @@ struct CacheNode *FindAvatarInCache(HANDLE hContact, BOOL add, BOOL findAny = FA
 	struct CacheNode *cacheNode = g_Cache, *foundNode = NULL;
 
 	char *szProto = GetContactProto(hContact);
-	if (szProto == NULL || !DBGetContactSettingByte(NULL, AVS_MODULE, szProto, 1))
+	if (szProto == NULL || !db_get_b(NULL, AVS_MODULE, szProto, 1))
 		return NULL;
 
-	EnterCriticalSection(&cachecs);
+	mir_cslock lck(cachecs);
 
-	while(cacheNode)
-	{
-		if (cacheNode->ace.hContact == hContact)
-		{
+	while(cacheNode) {
+		if (cacheNode->ace.hContact == hContact) {
 			cacheNode->ace.t_lastAccess = time(NULL);
 			foundNode = cacheNode->loaded || findAny ? cacheNode : NULL;
-			LeaveCriticalSection(&cachecs);
 			return foundNode;
 		}
+
+		// found an empty and usable node
 		if (foundNode == NULL && cacheNode->ace.hContact == 0)
-			foundNode = cacheNode;				// found an empty and usable node
+			foundNode = cacheNode;
 
 		cacheNode = cacheNode->pNextNode;
 	}
 
 	// not found
-
-	if (add)
-	{
-		if (foundNode == NULL) {					// no free entry found, create a new and append it to the list
-			EnterCriticalSection(&alloccs);     // protect memory block allocation
-			struct CacheNode *newNode = AllocCacheBlock();
-			AddToList(newNode);
-			foundNode = newNode;
-			LeaveCriticalSection(&alloccs);
-		}
-
-		foundNode->ace.hContact = hContact;
-		if (g_MetaAvail)
-			foundNode->dwFlags |= (DBGetContactSettingByte(hContact, g_szMetaName, "IsSubcontact", 0) ? MC_ISSUBCONTACT : 0);
-		foundNode->loaded = FALSE;
-		foundNode->mustLoad = 1;                                 // pic loader will watch this and load images
-		LeaveCriticalSection(&cachecs);
-		SetEvent(hLoaderEvent);                                     // wake him up
+	if (!add)
 		return NULL;
+
+	if (foundNode == NULL) {					// no free entry found, create a new and append it to the list
+		mir_cslock all(alloccs);     // protect memory block allocation
+		struct CacheNode *newNode = AllocCacheBlock();
+		AddToList(newNode);
+		foundNode = newNode;
 	}
-	else
-	{
-		foundNode = NULL;
-	}
-	LeaveCriticalSection(&cachecs);
-	return foundNode;
+
+	foundNode->ace.hContact = hContact;
+	if (g_MetaAvail)
+		foundNode->dwFlags |= (db_get_b(hContact, g_szMetaName, "IsSubcontact", 0) ? MC_ISSUBCONTACT : 0);
+	foundNode->loaded = FALSE;
+	foundNode->mustLoad = 1;                                 // pic loader will watch this and load images
+	SetEvent(hLoaderEvent);                                     // wake him up
+	return NULL;
 }
 
 #define POLYNOMIAL (0x488781ED) /* This is the CRC Poly */
@@ -761,7 +749,7 @@ static int ProtocolAck(WPARAM wParam, LPARAM lParam)
 INT_PTR ProtectAvatar(WPARAM wParam, LPARAM lParam)
 {
 	HANDLE hContact = (HANDLE)wParam;
-	BYTE was_locked = DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0);
+	BYTE was_locked = db_get_b(hContact, "ContactPhoto", "Locked", 0);
 
 	if (fei == NULL || was_locked == (BYTE)lParam)      // no need for redundant lockings...
 		return 0;
@@ -787,47 +775,40 @@ struct OpenFileSubclassData {
 
 static BOOL CALLBACK OpenFileSubclass(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
+	OpenFileSubclassData *data= (OpenFileSubclassData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
+
 	switch(msg) {
 	case WM_INITDIALOG:
 		{
 			OPENFILENAME *ofn = (OPENFILENAME *)lParam;
 
-			OpenFileSubclassData *data = (OpenFileSubclassData *) malloc(sizeof(OpenFileSubclassData));
+			data = (OpenFileSubclassData *) malloc(sizeof(OpenFileSubclassData));
 			SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data);
 			data->locking_request = (BYTE *)ofn->lCustData;
 			data->setView = TRUE;
 
 			TranslateDialogDefault(hwnd);
 			CheckDlgButton(hwnd, IDC_PROTECTAVATAR, *(data->locking_request));
-			break;
 		}
+		break;
 
 	case WM_COMMAND:
 		if (LOWORD(wParam) == IDC_PROTECTAVATAR)
-		{
-			OpenFileSubclassData *data= (OpenFileSubclassData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
 			*(data->locking_request) = IsDlgButtonChecked(hwnd, IDC_PROTECTAVATAR) ? TRUE : FALSE;
-		}
 		break;
 
 	case WM_NOTIFY:
-		{
-			OpenFileSubclassData *data= (OpenFileSubclassData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
-			if (data->setView)
-			{
-				HWND hwndParent = GetParent(hwnd);
-				HWND hwndLv = FindWindowEx(hwndParent, NULL, _T("SHELLDLL_DefView"), NULL) ;
-				if (hwndLv != NULL)
-				{
-					SendMessage(hwndLv, WM_COMMAND, SHVIEW_THUMBNAIL, 0);
-					data->setView = FALSE;
-				}
+		if (data->setView) {
+			HWND hwndParent = GetParent(hwnd);
+			HWND hwndLv = FindWindowEx(hwndParent, NULL, _T("SHELLDLL_DefView"), NULL) ;
+			if (hwndLv != NULL) {
+				SendMessage(hwndLv, WM_COMMAND, SHVIEW_THUMBNAIL, 0);
+				data->setView = FALSE;
 			}
 		}
 		break;
 
 	case WM_NCDESTROY:
-		OpenFileSubclassData *data= (OpenFileSubclassData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
 		free((OpenFileSubclassData *)data);
 		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)0);
 		break;
@@ -853,7 +834,7 @@ INT_PTR avSetAvatar(HANDLE hContact, TCHAR* tszPath)
 	if (hContact == NULL || fei == NULL)
 		return 0;
 
-	is_locked = DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0);
+	is_locked = db_get_b(hContact, "ContactPhoto", "Locked", 0);
 
 	if ( tszPath == NULL ) {
 		OPENFILENAME ofn = {0};
@@ -902,10 +883,10 @@ INT_PTR avSetAvatar(HANDLE hContact, TCHAR* tszPath)
 	CloseHandle(hFile);
 
 	AVS_pathToRelative(szFinalName, szBackupName);
-	DBWriteContactSettingTString(hContact, "ContactPhoto", "Backup", szBackupName);
+	db_set_ts(hContact, "ContactPhoto", "Backup", szBackupName);
 
 	DBWriteContactSettingByte(hContact, "ContactPhoto", "Locked", is_locked);
-	DBWriteContactSettingTString(hContact, "ContactPhoto", "File", szFinalName);
+	db_set_ts(hContact, "ContactPhoto", "File", szFinalName);
 	MakePathRelative(hContact, szFinalName);
 	// Fix cache
 	ChangeAvatar(hContact, TRUE);
@@ -1119,10 +1100,10 @@ static void DeleteGlobalUserAvatar()
 
 	TCHAR szFilename[MAX_PATH];
 	AVS_pathToAbsolute(dbv.ptszVal, szFilename);
-	DBFreeVariant(&dbv);
+	db_free(&dbv);
 
 	DeleteFile(szFilename);
-	DBDeleteContactSetting(NULL, AVS_MODULE, "GlobalUserAvatarFile");
+	db_unset(NULL, AVS_MODULE, "GlobalUserAvatarFile");
 }
 
 static void SetIgnoreNotify(char *protocol, BOOL ignore)
@@ -1154,9 +1135,8 @@ static int InternalRemoveMyAvatar(char *protocol)
 		{
 			// Has global avatar?
 			DBVARIANT dbv = {0};
-			if (!DBGetContactSettingTString(NULL, AVS_MODULE, "GlobalUserAvatarFile", &dbv))
-			{
-				DBFreeVariant(&dbv);
+			if (!DBGetContactSettingTString(NULL, AVS_MODULE, "GlobalUserAvatarFile", &dbv)) {
+				db_unset(&dbv);
 				DBWriteContactSettingByte(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1);
 				DeleteGlobalUserAvatar();
 			}
@@ -1318,9 +1298,9 @@ static int InternalSetMyAvatar(char *protocol, TCHAR *szFinalName, SetMyAvatarHo
 			{
 				TCHAR relFile[1024];
 				if (AVS_pathToRelative(globalFile, relFile))
-					DBWriteContactSettingTString(NULL, AVS_MODULE, "GlobalUserAvatarFile", relFile);
+					db_set_ts(NULL, AVS_MODULE, "GlobalUserAvatarFile", relFile);
 				else
-					DBWriteContactSettingTString(NULL, AVS_MODULE, "GlobalUserAvatarFile", globalFile);
+					db_set_ts(NULL, AVS_MODULE, "GlobalUserAvatarFile", globalFile);
 
 				DBWriteContactSettingByte(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 0);
 			}
@@ -1388,7 +1368,7 @@ INT_PTR avSetMyAvatar( char* protocol, TCHAR* tszPath )
 			allAcceptSWF = allAcceptSWF && Proto_IsAvatarFormatSupported( accs[i]->szModuleName, PA_FORMAT_SWF);
 		}
 
-		data.square = DBGetContactSettingByte(0, AVS_MODULE, "SetAllwaysMakeSquare", 0);
+		data.square = db_get_b(0, AVS_MODULE, "SetAllwaysMakeSquare", 0);
 	}
 	else
 	{
@@ -1397,7 +1377,7 @@ INT_PTR avSetMyAvatar( char* protocol, TCHAR* tszPath )
 
 		data.protocol = protocol;
 		data.square = (Proto_AvatarImageProportion(protocol) & PIP_SQUARE)
-						|| DBGetContactSettingByte(0, AVS_MODULE, "SetAllwaysMakeSquare", 0);
+						|| db_get_b(0, AVS_MODULE, "SetAllwaysMakeSquare", 0);
 	}
 
 	if (tszPath == NULL) {
@@ -1707,10 +1687,10 @@ static protoPicCacheEntry *GetProtoDefaultAvatar(HANDLE hContact)
 
 HANDLE GetContactThatHaveTheAvatar(HANDLE hContact, int locked = -1)
 {
-	if (g_MetaAvail && DBGetContactSettingByte(NULL, g_szMetaName, "Enabled", 0)) {
-		if (DBGetContactSettingDword(hContact, g_szMetaName, "NumContacts", 0) >= 1) {
+	if (g_MetaAvail && db_get_b(NULL, g_szMetaName, "Enabled", 0)) {
+		if (db_get_dw(hContact, g_szMetaName, "NumContacts", 0) >= 1) {
 			if (locked == -1)
-				locked = DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0);
+				locked = db_get_b(hContact, "ContactPhoto", "Locked", 0);
 
 			if (!locked)
 				hContact = (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hContact, 0);
@@ -1746,7 +1726,7 @@ void DeleteAvatarFromCache(HANDLE hContact, BOOL forever)
 	if (node == NULL) {
 		struct CacheNode temp_node = {0};
 		if (g_MetaAvail)
-			temp_node.dwFlags |= (DBGetContactSettingByte(hContact, g_szMetaName, "IsSubcontact", 0) ? MC_ISSUBCONTACT : 0);
+			temp_node.dwFlags |= (db_get_b(hContact, g_szMetaName, "IsSubcontact", 0) ? MC_ISSUBCONTACT : 0);
 		NotifyMetaAware(hContact, &temp_node, (AVATARCACHEENTRY *)GetProtoDefaultAvatar(hContact));
 		return;
 	}
@@ -1785,7 +1765,7 @@ int ChangeAvatar(HANDLE hContact, BOOL fLoad, BOOL fNotifyHist, int pa_format)
 
 static void PicLoader(LPVOID param)
 {
-	DWORD dwDelay = DBGetContactSettingDword(NULL, AVS_MODULE, "picloader_sleeptime", 80);
+	DWORD dwDelay = db_get_dw(NULL, AVS_MODULE, "picloader_sleeptime", 80);
 
 	if (dwDelay < 30)
 		dwDelay = 30;
@@ -1800,7 +1780,7 @@ static void PicLoader(LPVOID param)
 				node->mustLoad = 0;
 				AVATARCACHEENTRY ace_temp;
 
-				if (DBGetContactSettingByte(node->ace.hContact, "ContactPhoto", "NeedUpdate", 0))
+				if (db_get_b(node->ace.hContact, "ContactPhoto", "NeedUpdate", 0))
 					QueueAdd(node->ace.hContact);
 
 				CopyMemory(&ace_temp, &node->ace, sizeof(AVATARCACHEENTRY));
@@ -1906,7 +1886,7 @@ static void LoadDefaultInfo()
 {
 	protoPicCacheEntry* pce = new protoPicCacheEntry;
 	if (CreateAvatarInCache(0, pce, AVS_DEFAULT) != 1)
-		DBDeleteContactSetting(0, PPICT_MODULE, AVS_DEFAULT);
+		db_unset(0, PPICT_MODULE, AVS_DEFAULT);
 
 	pce->szProtoname = mir_strdup(AVS_DEFAULT);
 	pce->tszAccName = mir_tstrdup(TranslateT(AVS_DEFAULT));
@@ -1924,7 +1904,7 @@ static void LoadProtoInfo(PROTOCOLDESCRIPTOR* proto)
 		mir_sntprintf(protoNameTmp, SIZEOF(protoNameTmp), TranslateT("Global avatar for %s accounts"), tszName);
 		protoPicCacheEntry* pce = new protoPicCacheEntry;
 		if (CreateAvatarInCache(0, pce, protoName) != 1)
-			DBDeleteContactSetting(0, PPICT_MODULE, protoName);
+			db_unset(0, PPICT_MODULE, protoName);
 
 		pce->szProtoname = mir_strdup(protoName);
 		pce->tszAccName = mir_tstrdup(protoNameTmp);
@@ -1937,7 +1917,7 @@ static void LoadAccountInfo( PROTOACCOUNT* acc )
 {
 	protoPicCacheEntry* pce = new protoPicCacheEntry;
 	if ( CreateAvatarInCache(0, pce, acc->szModuleName ) != 1 )
-		DBDeleteContactSetting(0, PPICT_MODULE, acc->szModuleName);
+		db_unset(0, PPICT_MODULE, acc->szModuleName);
 
 	pce->szProtoname = mir_strdup( acc->szModuleName );
 	pce->tszAccName = mir_tstrdup( acc->tszAccountName );
@@ -2095,7 +2075,7 @@ static int ContactSettingChanged(WPARAM wParam, LPARAM lParam)
 			ReportMyAvatarChanged((WPARAM) cws->szModule, 0);
 		return 0;
 	}
-	
+
 	if (g_MetaAvail && !strcmp(cws->szModule, g_szMetaName)) {
 		if (lstrlenA(cws->szSetting) > 6 && !strncmp(cws->szSetting, "Status", 5))
 			MetaChanged(wParam, 0);
@@ -2147,7 +2127,6 @@ static int OkToExitProc(WPARAM wParam, LPARAM lParam)
 	LeaveCriticalSection(&cachecs);
 
 	SetEvent(hLoaderEvent);
-	FreePolls();
 	return 0;
 }
 
@@ -2290,7 +2269,7 @@ INT_PTR DrawAvatarPicture(WPARAM wParam, LPARAM lParam)
 		if (r->szProto == NULL)
 			return 0;
 
-		if (r->szProto[0] == '\0' && DBGetContactSettingByte(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1))
+		if (r->szProto[0] == '\0' && db_get_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1))
 			return -1;
 
 		ace = (AVATARCACHEENTRY *)GetMyAvatar(0, (LPARAM)r->szProto);
@@ -2329,7 +2308,7 @@ static int OnDetailsInit(WPARAM wParam, LPARAM lParam)
 	else
 	{
 		char *szProto = GetContactProto(hContact);
-		if (szProto == NULL || DBGetContactSettingByte(NULL, AVS_MODULE, szProto, 1))
+		if (szProto == NULL || db_get_b(NULL, AVS_MODULE, szProto, 1))
 		{
 			// Contact dialog
 			OPTIONSDIALOGPAGE odp = {0};
diff --git a/plugins/AVS/src/options.cpp b/plugins/AVS/src/options.cpp
index 218a092e85..402684a4b9 100644
--- a/plugins/AVS/src/options.cpp
+++ b/plugins/AVS/src/options.cpp
@@ -2,8 +2,8 @@
 
 Miranda IM: the free IM client for Microsoft* Windows*
 
-Copyright 2000-2004 Miranda ICQ/IM project, 
-all portions of this codebase are copyrighted to the people 
+Copyright 2000-2004 Miranda ICQ/IM project,
+all portions of this codebase are copyrighted to the people
 listed in contributors.txt.
 
 This program is free software; you can redistribute it and/or
@@ -56,7 +56,7 @@ struct WindowData {
 
 static void RemoveProtoPic(const char *szProto)
 {
-	DBDeleteContactSetting(NULL, PPICT_MODULE, szProto);
+	db_unset(NULL, PPICT_MODULE, szProto);
 
 	if ( szProto == NULL )
 		return;
@@ -73,7 +73,7 @@ static void RemoveProtoPic(const char *szProto)
 		}
 		return;
 	}
-	
+
 	if (strstr(szProto, "Global avatar for")) {
 		char szProtoname[MAX_PATH] = {0};
 		lstrcpynA(szProtoname, szProto, lstrlenA(szProto)- lstrlenA("accounts"));
@@ -84,7 +84,7 @@ static void RemoveProtoPic(const char *szProto)
 			PROTOACCOUNT* pdescr = (PROTOACCOUNT*)CallService(MS_PROTO_GETACCOUNT, 0, (LPARAM)p.szProtoname);
 			if (pdescr == NULL && lstrcmpA(p.szProtoname, szProto))
 				continue;
-			
+
 			if (!lstrcmpA(p.szProtoname, szProto) || !lstrcmpA(pdescr->szProtoName, szProtoname)) {
 				if (p.szProtoname == NULL)
 					continue;
@@ -135,7 +135,7 @@ static void SetProtoPic(char *szProto)
 
 		TCHAR szNewPath[MAX_PATH];
 		AVS_pathToRelative(FileName, szNewPath);
-		DBWriteContactSettingTString(NULL, PPICT_MODULE, szProto, szNewPath);
+		db_set_ts(NULL, PPICT_MODULE, szProto, szNewPath);
 
 		if (!lstrcmpA(AVS_DEFAULT, szProto)) {
 			for ( int i = 0; i < g_ProtoPictures.getCount(); i++ ) {
@@ -175,7 +175,7 @@ static void SetProtoPic(char *szProto)
 					break;
 
 				if (!strcmp(p.szProtoname, szProto) && lstrlenA(p.szProtoname) == lstrlenA(szProto)) {
-					if (p.hbmPic != 0) 
+					if (p.hbmPic != 0)
 						DeleteObject(p.hbmPic);
 					ZeroMemory(&p, sizeof(avatarCacheEntry));
 					CreateAvatarInCache(0, &p, ( char* )szProto);
@@ -195,16 +195,16 @@ INT_PTR CALLBACK DlgProcOptionsAvatars(HWND hwndDlg, UINT msg, WPARAM wParam, LP
 	case WM_INITDIALOG:
 		TranslateDialogDefault(hwndDlg);
 
-		CheckDlgButton(hwndDlg, IDC_SHOWWARNINGS, DBGetContactSettingByte(0, AVS_MODULE, "warnings", 0));
-		CheckDlgButton(hwndDlg, IDC_MAKE_GRAYSCALE, DBGetContactSettingByte(0, AVS_MODULE, "MakeGrayscale", 0));
-		CheckDlgButton(hwndDlg, IDC_MAKE_TRANSPARENT_BKG, DBGetContactSettingByte(0, AVS_MODULE, "MakeTransparentBkg", 0));
-		CheckDlgButton(hwndDlg, IDC_MAKE_TRANSP_PROPORTIONAL, DBGetContactSettingByte(0, AVS_MODULE, "MakeTransparencyProportionalToColorDiff", 0));
+		CheckDlgButton(hwndDlg, IDC_SHOWWARNINGS, db_get_b(0, AVS_MODULE, "warnings", 0));
+		CheckDlgButton(hwndDlg, IDC_MAKE_GRAYSCALE, db_get_b(0, AVS_MODULE, "MakeGrayscale", 0));
+		CheckDlgButton(hwndDlg, IDC_MAKE_TRANSPARENT_BKG, db_get_b(0, AVS_MODULE, "MakeTransparentBkg", 0));
+		CheckDlgButton(hwndDlg, IDC_MAKE_TRANSP_PROPORTIONAL, db_get_b(0, AVS_MODULE, "MakeTransparencyProportionalToColorDiff", 0));
 
 		SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_SETRANGE, 0, MAKELONG(8, 2));
-		SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_SETPOS, 0, (LPARAM)DBGetContactSettingWord(0, AVS_MODULE, "TranspBkgNumPoints", 5));
+		SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_SETPOS, 0, (LPARAM)db_get_w(0, AVS_MODULE, "TranspBkgNumPoints", 5));
 
 		SendDlgItemMessage(hwndDlg, IDC_BKG_COLOR_DIFFERENCE_SPIN, UDM_SETRANGE, 0, MAKELONG(100, 0));
-		SendDlgItemMessage(hwndDlg, IDC_BKG_COLOR_DIFFERENCE_SPIN, UDM_SETPOS, 0, (LPARAM)DBGetContactSettingWord(0, AVS_MODULE, "TranspBkgColorDiff", 10));
+		SendDlgItemMessage(hwndDlg, IDC_BKG_COLOR_DIFFERENCE_SPIN, UDM_SETPOS, 0, (LPARAM)db_get_w(0, AVS_MODULE, "TranspBkgColorDiff", 10));
 		{
 			BOOL enabled = IsDlgButtonChecked(hwndDlg, IDC_MAKE_TRANSPARENT_BKG);
 			EnableWindow(GetDlgItem(hwndDlg, IDC_BKG_NUM_POINTS_L), enabled);
@@ -246,8 +246,8 @@ INT_PTR CALLBACK DlgProcOptionsAvatars(HWND hwndDlg, UINT msg, WPARAM wParam, LP
 				DBWriteContactSettingByte(NULL, AVS_MODULE, "MakeGrayscale", IsDlgButtonChecked(hwndDlg, IDC_MAKE_GRAYSCALE) ? 1 : 0);
 				DBWriteContactSettingByte(NULL, AVS_MODULE, "MakeTransparentBkg", IsDlgButtonChecked(hwndDlg, IDC_MAKE_TRANSPARENT_BKG) ? 1 : 0);
 				DBWriteContactSettingByte(NULL, AVS_MODULE, "MakeTransparencyProportionalToColorDiff", IsDlgButtonChecked(hwndDlg, IDC_MAKE_TRANSP_PROPORTIONAL) ? 1 : 0);
-				DBWriteContactSettingWord(NULL, AVS_MODULE, "TranspBkgNumPoints", (WORD) SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_GETPOS, 0, 0));
-				DBWriteContactSettingWord(NULL, AVS_MODULE, "TranspBkgColorDiff", (WORD) SendDlgItemMessage(hwndDlg, IDC_BKG_COLOR_DIFFERENCE_SPIN, UDM_GETPOS, 0, 0));
+				db_set_w(NULL, AVS_MODULE, "TranspBkgNumPoints", (WORD) SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_GETPOS, 0, 0));
+				db_set_w(NULL, AVS_MODULE, "TranspBkgColorDiff", (WORD) SendDlgItemMessage(hwndDlg, IDC_BKG_COLOR_DIFFERENCE_SPIN, UDM_GETPOS, 0, 0));
 		}	}
 		break;
 	}
@@ -260,8 +260,8 @@ INT_PTR CALLBACK DlgProcOptionsOwn(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM
 	case WM_INITDIALOG:
 		TranslateDialogDefault(hwndDlg);
 
-		CheckDlgButton(hwndDlg, IDC_MAKE_MY_AVATARS_TRANSP, DBGetContactSettingByte(0, AVS_MODULE, "MakeMyAvatarsTransparent", 0));
-		CheckDlgButton(hwndDlg, IDC_SET_MAKE_SQUARE, DBGetContactSettingByte(0, AVS_MODULE, "SetAllwaysMakeSquare", 0));
+		CheckDlgButton(hwndDlg, IDC_MAKE_MY_AVATARS_TRANSP, db_get_b(0, AVS_MODULE, "MakeMyAvatarsTransparent", 0));
+		CheckDlgButton(hwndDlg, IDC_SET_MAKE_SQUARE, db_get_b(0, AVS_MODULE, "SetAllwaysMakeSquare", 0));
 
 		return TRUE;
 
@@ -321,8 +321,8 @@ INT_PTR CALLBACK DlgProcOptionsProtos(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 				item.pszText = g_ProtoPictures[i].tszAccName;
 				newItem = ListView_InsertItem(hwndList, &item);
 				if (newItem >= 0)
-					ListView_SetCheckState(hwndList, newItem, 
-						DBGetContactSettingByte(NULL, AVS_MODULE, g_ProtoPictures[i].szProtoname, 1) ? TRUE : FALSE);
+					ListView_SetCheckState(hwndList, newItem,
+						db_get_b(NULL, AVS_MODULE, g_ProtoPictures[i].szProtoname, 1) ? TRUE : FALSE);
 			}
 			ListView_SetColumnWidth(hwndList, 0, LVSCW_AUTOSIZE);
 			ListView_Arrange(hwndList, LVA_ALIGNLEFT | LVA_ALIGNTOP);
@@ -353,7 +353,7 @@ INT_PTR CALLBACK DlgProcOptionsProtos(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 			}
 		}
 		break;
-	
+
 	case WM_DRAWITEM:
 		{
 			LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lParam;
@@ -386,7 +386,7 @@ INT_PTR CALLBACK DlgProcOptionsProtos(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 				break;
 			case LVN_ITEMCHANGED:
 				{
-					NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;						
+					NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;
 					if (IsWindowVisible(GetDlgItem(hwndDlg, IDC_PROTOCOLS)) && ((nmlv->uNewState ^ nmlv->uOldState) & LVIS_STATEIMAGEMASK))
 						SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
 				}
@@ -399,11 +399,9 @@ INT_PTR CALLBACK DlgProcOptionsProtos(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 					int iItem = ListView_GetSelectionMark(hwndList);
 					g_selectedProto = GetProtoFromList(hwndDlg, iItem);
 					if ( g_selectedProto ) {
-						DBVARIANT dbv = {0};
-						if (!DBGetContactSettingTString(NULL, PPICT_MODULE, g_selectedProto, &dbv)) 
-						{
-							if (!AVS_pathIsAbsolute(dbv.ptszVal))
-							{
+						DBVARIANT dbv;
+						if ( !DBGetContactSettingTString(NULL, PPICT_MODULE, g_selectedProto, &dbv)) {
+							if ( !AVS_pathIsAbsolute(dbv.ptszVal)) {
 								TCHAR szFinalPath[MAX_PATH];
 								mir_sntprintf(szFinalPath, SIZEOF(szFinalPath), _T("%%miranda_path%%\\%s"), dbv.ptszVal);
 								SetDlgItemText(hwndDlg, IDC_PROTOAVATARNAME, szFinalPath);
@@ -411,7 +409,7 @@ INT_PTR CALLBACK DlgProcOptionsProtos(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 							else SetDlgItemText(hwndDlg, IDC_PROTOAVATARNAME, dbv.ptszVal);
 
 							InvalidateRect(GetDlgItem(hwndDlg, IDC_PROTOPIC), NULL, TRUE);
-							DBFreeVariant(&dbv);
+							db_free(&dbv);
 						}
 						else {
 							SetWindowText(GetDlgItem(hwndDlg, IDC_PROTOAVATARNAME), _T(""));
@@ -429,16 +427,16 @@ INT_PTR CALLBACK DlgProcOptionsProtos(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 					for (int i = 0; i < ListView_GetItemCount(hwndList); i++) {
 						char *szProto = GetProtoFromList(hwndDlg, i);
 
-						BOOL oldVal = DBGetContactSettingByte(NULL, AVS_MODULE, szProto, 1);
+						BOOL oldVal = db_get_b(NULL, AVS_MODULE, szProto, 1);
 						BOOL newVal = ListView_GetCheckState(hwndList, i);
 
 						if (oldVal && !newVal)
 						{
 							HANDLE hContact = db_find_first();
-							while (hContact != NULL) 
+							while (hContact != NULL)
 							{
 								char* szContactProto = GetContactProto(hContact);
-								if (szContactProto != NULL && !strcmp(szContactProto, szProto)) 
+								if (szContactProto != NULL && !strcmp(szContactProto, szProto))
 									DeleteAvatarFromCache(hContact, TRUE);
 
 								hContact = db_find_next(hContact);
@@ -460,9 +458,9 @@ INT_PTR CALLBACK DlgProcOptionsProtos(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 
 void LoadTransparentData(HWND hwndDlg, HANDLE hContact)
 {
-	CheckDlgButton(hwndDlg, IDC_MAKETRANSPBKG, DBGetContactSettingByte(hContact, "ContactPhoto", "MakeTransparentBkg", DBGetContactSettingByte(0, AVS_MODULE, "MakeTransparentBkg", 0)));
-	SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_SETPOS, 0, (LPARAM)DBGetContactSettingWord(hContact, "ContactPhoto", "TranspBkgNumPoints", DBGetContactSettingWord(0, AVS_MODULE, "TranspBkgNumPoints", 5)));
-	SendDlgItemMessage(hwndDlg, IDC_BKG_COLOR_DIFFERENCE_SPIN, UDM_SETPOS, 0, (LPARAM)DBGetContactSettingWord(hContact, "ContactPhoto", "TranspBkgColorDiff", DBGetContactSettingWord(0, AVS_MODULE, "TranspBkgColorDiff", 10)));
+	CheckDlgButton(hwndDlg, IDC_MAKETRANSPBKG, db_get_b(hContact, "ContactPhoto", "MakeTransparentBkg", db_get_b(0, AVS_MODULE, "MakeTransparentBkg", 0)));
+	SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_SETPOS, 0, (LPARAM)db_get_w(hContact, "ContactPhoto", "TranspBkgNumPoints", db_get_w(0, AVS_MODULE, "TranspBkgNumPoints", 5)));
+	SendDlgItemMessage(hwndDlg, IDC_BKG_COLOR_DIFFERENCE_SPIN, UDM_SETPOS, 0, (LPARAM)db_get_w(hContact, "ContactPhoto", "TranspBkgColorDiff", db_get_w(0, AVS_MODULE, "TranspBkgColorDiff", 10)));
 
 	BOOL transp_enabled = IsDlgButtonChecked(hwndDlg, IDC_MAKETRANSPBKG);
 	EnableWindow(GetDlgItem(hwndDlg, IDC_BKG_NUM_POINTS_L), transp_enabled);
@@ -476,22 +474,22 @@ void LoadTransparentData(HWND hwndDlg, HANDLE hContact)
 void SaveTransparentData(HWND hwndDlg, HANDLE hContact)
 {
 	BOOL transp = IsDlgButtonChecked(hwndDlg, IDC_MAKETRANSPBKG);
-	if (DBGetContactSettingByte(0, AVS_MODULE, "MakeTransparentBkg", 0) == transp)
-		DBDeleteContactSetting(hContact, "ContactPhoto", "MakeTransparentBkg");
+	if (db_get_b(0, AVS_MODULE, "MakeTransparentBkg", 0) == transp)
+		db_unset(hContact, "ContactPhoto", "MakeTransparentBkg");
 	else
 		DBWriteContactSettingByte(hContact, "ContactPhoto", "MakeTransparentBkg", transp);
 
 	WORD tmp = (WORD) SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_GETPOS, 0, 0);
-	if (DBGetContactSettingWord(0, AVS_MODULE, "TranspBkgNumPoints", 5) == tmp)
-		DBDeleteContactSetting(hContact, "ContactPhoto", "TranspBkgNumPoints");
+	if (db_get_w(0, AVS_MODULE, "TranspBkgNumPoints", 5) == tmp)
+		db_unset(hContact, "ContactPhoto", "TranspBkgNumPoints");
 	else
-		DBWriteContactSettingWord(hContact, "ContactPhoto", "TranspBkgNumPoints", tmp);
+		db_set_w(hContact, "ContactPhoto", "TranspBkgNumPoints", tmp);
 
 	tmp = (WORD) SendDlgItemMessage(hwndDlg, IDC_BKG_COLOR_DIFFERENCE_SPIN, UDM_GETPOS, 0, 0);
-	if (DBGetContactSettingWord(0, AVS_MODULE, "TranspBkgColorDiff", 10) == tmp)
-		DBDeleteContactSetting(hContact, "ContactPhoto", "TranspBkgColorDiff");
+	if (db_get_w(0, AVS_MODULE, "TranspBkgColorDiff", 10) == tmp)
+		db_unset(hContact, "ContactPhoto", "TranspBkgColorDiff");
 	else
-		DBWriteContactSettingWord(hContact, "ContactPhoto", "TranspBkgColorDiff", tmp);
+		db_set_w(hContact, "ContactPhoto", "TranspBkgColorDiff", tmp);
 }
 
 void SaveTransparentData(HWND hwndDlg, HANDLE hContact, BOOL locked)
@@ -532,8 +530,8 @@ INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 			SendMessage(hwndDlg, DM_SETAVATARNAME, 0, 0);
 			ShowWindow(hwndDlg, SW_SHOWNORMAL);
 			InvalidateRect(GetDlgItem(hwndDlg, IDC_PROTOPIC), NULL, FALSE);
-			CheckDlgButton(hwndDlg, IDC_PROTECTAVATAR, DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0) ? TRUE : FALSE);
-			CheckDlgButton(hwndDlg, IDC_HIDEAVATAR, DBGetContactSettingByte(hContact, "ContactPhoto", "Hidden", 0) ? TRUE : FALSE);
+			CheckDlgButton(hwndDlg, IDC_PROTECTAVATAR, db_get_b(hContact, "ContactPhoto", "Locked", 0) ? TRUE : FALSE);
+			CheckDlgButton(hwndDlg, IDC_HIDEAVATAR, db_get_b(hContact, "ContactPhoto", "Hidden", 0) ? TRUE : FALSE);
 
 			SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_SETBUDDY, (WPARAM)GetDlgItem(hwndDlg, IDC_BKG_NUM_POINTS), 0);
 			SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_SETRANGE, 0, MAKELONG(8, 2));
@@ -552,9 +550,9 @@ INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 		case ID_USE_DEFAULTS:
 			hContact = GetContactThatHaveTheAvatar(hContact);
 
-			DBDeleteContactSetting(hContact, "ContactPhoto", "MakeTransparentBkg");
-			DBDeleteContactSetting(hContact, "ContactPhoto", "TranspBkgNumPoints");
-			DBDeleteContactSetting(hContact, "ContactPhoto", "TranspBkgColorDiff");
+			db_unset(hContact, "ContactPhoto", "MakeTransparentBkg");
+			db_unset(hContact, "ContactPhoto", "TranspBkgNumPoints");
+			db_unset(hContact, "ContactPhoto", "TranspBkgColorDiff");
 
 			LoadTransparentData(hwndDlg, hContact);
 
@@ -566,10 +564,10 @@ INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 				BOOL locked = IsDlgButtonChecked(hwndDlg, IDC_PROTECTAVATAR);
 				int hidden = IsDlgButtonChecked(hwndDlg, IDC_HIDEAVATAR) ? 1 : 0;
 				SetAvatarAttribute(hContact, AVS_HIDEONCLIST, hidden);
-				if (hidden != DBGetContactSettingByte(hContact, "ContactPhoto", "Hidden", 0))
+				if (hidden != db_get_b(hContact, "ContactPhoto", "Hidden", 0))
 					DBWriteContactSettingByte(hContact, "ContactPhoto", "Hidden", hidden);
 
-				if (!locked && DBGetContactSettingByte(hContact, "ContactPhoto", "NeedUpdate", 0))
+				if (!locked && db_get_b(hContact, "ContactPhoto", "NeedUpdate", 0))
 					QueueAdd(hContact);
 
 				// Continue to the cancel handle
@@ -589,7 +587,7 @@ INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 		case IDC_CHANGE:
 			SetAvatar((WPARAM)hContact, 0);
 			SendMessage(hwndDlg, DM_SETAVATARNAME, 0, 0);
-			CheckDlgButton(hwndDlg, IDC_PROTECTAVATAR, DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0) ? TRUE : FALSE);
+			CheckDlgButton(hwndDlg, IDC_PROTECTAVATAR, db_get_b(hContact, "ContactPhoto", "Locked", 0) ? TRUE : FALSE);
 			break;
 
 		case IDC_BKG_NUM_POINTS:
@@ -620,16 +618,16 @@ INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 				if (MessageBox(0, TranslateT("Delete picture file from disk (may be necessary to force a reload, but will delete local pictures)?"), TranslateT("Reset contact picture"), MB_YESNO) == IDYES) {
 					if (!DBGetContactSettingTString(hContact, "ContactPhoto", "File", &dbv)) {
 						DeleteFile(dbv.ptszVal);
-						DBFreeVariant(&dbv);
+						db_free(&dbv);
 					}
 				}
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Locked");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Backup");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "RFile");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "File");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Format");
-				DBDeleteContactSetting(hContact, szProto, "AvatarHash");
-				DBDeleteContactSetting(hContact, szProto, "AvatarSaved");
+				db_unset(hContact, "ContactPhoto", "Locked");
+				db_unset(hContact, "ContactPhoto", "Backup");
+				db_unset(hContact, "ContactPhoto", "RFile");
+				db_unset(hContact, "ContactPhoto", "File");
+				db_unset(hContact, "ContactPhoto", "Format");
+				db_unset(hContact, szProto, "AvatarHash");
+				db_unset(hContact, szProto, "AvatarSaved");
 				DeleteAvatarFromCache(hContact, FALSE);
 
 				QueueAdd(hContact);
@@ -645,14 +643,14 @@ INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 				if (MessageBox(0, TranslateT("Delete picture file from disk (may be necessary to force a reload, but will delete local pictures)?"), TranslateT("Reset contact picture"), MB_YESNO) == IDYES) {
 					if (!DBGetContactSettingTString(hContact, "ContactPhoto", "File", &dbv)) {
 						DeleteFile(dbv.ptszVal);
-						DBFreeVariant(&dbv);
+						db_free(&dbv);
 					}
 				}
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Locked");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Backup");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "RFile");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "File");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Format");
+				db_unset(hContact, "ContactPhoto", "Locked");
+				db_unset(hContact, "ContactPhoto", "Backup");
+				db_unset(hContact, "ContactPhoto", "RFile");
+				db_unset(hContact, "ContactPhoto", "File");
+				db_unset(hContact, "ContactPhoto", "Format");
 				DeleteAvatarFromCache(hContact, FALSE);
 				SendMessage(hwndDlg, DM_SETAVATARNAME, 0, 0);
 				InvalidateRect(GetDlgItem(hwndDlg, IDC_PROTOPIC), NULL, TRUE);
@@ -677,7 +675,7 @@ INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 				avdrq.dwFlags |= AVDRQ_DRAWBORDER;
 				avdrq.clrBorder = GetSysColor(COLOR_BTNTEXT);
 				avdrq.radius = 6;
-				if (!CallService(MS_AV_DRAWAVATAR, 0, (LPARAM)&avdrq)) 
+				if (!CallService(MS_AV_DRAWAVATAR, 0, (LPARAM)&avdrq))
 				{
 					// Get text rectangle
 					RECT rc = avdrq.rcDraw;
@@ -688,13 +686,13 @@ INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 
 					// Calc text size
 					RECT rc_ret = rc;
-					DrawText(dis->hDC, TranslateT("Contact has no avatar"), -1, &rc_ret, 
+					DrawText(dis->hDC, TranslateT("Contact has no avatar"), -1, &rc_ret,
 						DT_WORDBREAK | DT_NOPREFIX | DT_CENTER | DT_CALCRECT);
 
 					// Calc needed size
 					rc.top += ((rc.bottom - rc.top) - (rc_ret.bottom - rc_ret.top)) / 2;
 					rc.bottom = rc.top + (rc_ret.bottom - rc_ret.top);
-					DrawText(dis->hDC, TranslateT("Contact has no avatar"), -1, &rc, 
+					DrawText(dis->hDC, TranslateT("Contact has no avatar"), -1, &rc,
 						DT_WORDBREAK | DT_NOPREFIX | DT_CENTER);
 				}
 
@@ -706,21 +704,21 @@ INT_PTR CALLBACK DlgProcAvatarOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPA
 		{
 			TCHAR szFinalName[MAX_PATH];
 			DBVARIANT dbv = {0};
-			BYTE is_locked = DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0);
+			BYTE is_locked = db_get_b(hContact, "ContactPhoto", "Locked", 0);
 
 			szFinalName[0] = 0;
 
 			if (is_locked && !DBGetContactSettingTString(hContact, "ContactPhoto", "Backup", &dbv)) {
 				AVS_pathToAbsolute(dbv.ptszVal, szFinalName);
-				DBFreeVariant(&dbv);
+				db_free(&dbv);
 			}
 			else if (!DBGetContactSettingTString(hContact, "ContactPhoto", "RFile", &dbv)) {
 				AVS_pathToAbsolute(dbv.ptszVal, szFinalName);
-				DBFreeVariant(&dbv);
+				db_free(&dbv);
 			}
 			else if (!DBGetContactSettingTString(hContact, "ContactPhoto", "File", &dbv)) {
 				AVS_pathToAbsolute(dbv.ptszVal, szFinalName);
-				DBFreeVariant(&dbv);
+				db_free(&dbv);
 			}
 			szFinalName[MAX_PATH - 1] = 0;
 			SetDlgItemText(hwndDlg, IDC_AVATARNAME, szFinalName);
@@ -775,8 +773,8 @@ INT_PTR CALLBACK DlgProcAvatarUserInfo(HWND hwndDlg, UINT msg, WPARAM wParam, LP
 			hContact = (HANDLE)lParam;
 			TranslateDialogDefault(hwndDlg);
 			SendMessage(hwndDlg, DM_SETAVATARNAME, 0, 0);
-			CheckDlgButton(hwndDlg, IDC_PROTECTAVATAR, DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0) ? TRUE : FALSE);
-			CheckDlgButton(hwndDlg, IDC_HIDEAVATAR, DBGetContactSettingByte(hContact, "ContactPhoto", "Hidden", 0) ? TRUE : FALSE);
+			CheckDlgButton(hwndDlg, IDC_PROTECTAVATAR, db_get_b(hContact, "ContactPhoto", "Locked", 0) ? TRUE : FALSE);
+			CheckDlgButton(hwndDlg, IDC_HIDEAVATAR, db_get_b(hContact, "ContactPhoto", "Hidden", 0) ? TRUE : FALSE);
 
 			SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_SETBUDDY, (WPARAM)GetDlgItem(hwndDlg, IDC_BKG_NUM_POINTS), 0);
 			SendDlgItemMessage(hwndDlg, IDC_BKG_NUM_POINTS_SPIN, UDM_SETRANGE, 0, MAKELONG(8, 2));
@@ -793,9 +791,9 @@ INT_PTR CALLBACK DlgProcAvatarUserInfo(HWND hwndDlg, UINT msg, WPARAM wParam, LP
 		case ID_USE_DEFAULTS:
 			hContact = GetContactThatHaveTheAvatar(hContact);
 
-			DBDeleteContactSetting(hContact, "ContactPhoto", "MakeTransparentBkg");
-			DBDeleteContactSetting(hContact, "ContactPhoto", "TranspBkgNumPoints");
-			DBDeleteContactSetting(hContact, "ContactPhoto", "TranspBkgColorDiff");
+			db_unset(hContact, "ContactPhoto", "MakeTransparentBkg");
+			db_unset(hContact, "ContactPhoto", "TranspBkgNumPoints");
+			db_unset(hContact, "ContactPhoto", "TranspBkgColorDiff");
 
 			LoadTransparentData(hwndDlg, hContact);
 
@@ -805,14 +803,14 @@ INT_PTR CALLBACK DlgProcAvatarUserInfo(HWND hwndDlg, UINT msg, WPARAM wParam, LP
 		case IDC_CHANGE:
 			SetAvatar((WPARAM)hContact, 0);
 			SendMessage(hwndDlg, DM_SETAVATARNAME, 0, 0);
-			CheckDlgButton(hwndDlg, IDC_PROTECTAVATAR, DBGetContactSettingByte(hContact, "ContactPhoto", "Locked", 0) ? TRUE : FALSE);
+			CheckDlgButton(hwndDlg, IDC_PROTECTAVATAR, db_get_b(hContact, "ContactPhoto", "Locked", 0) ? TRUE : FALSE);
 			break;
 
 		case IDC_HIDEAVATAR:
 			{
 				int hidden = IsDlgButtonChecked(hwndDlg, IDC_HIDEAVATAR) ? 1 : 0;
 				SetAvatarAttribute(hContact, AVS_HIDEONCLIST, hidden);
-				if (hidden != DBGetContactSettingByte(hContact, "ContactPhoto", "Hidden", 0))
+				if (hidden != db_get_b(hContact, "ContactPhoto", "Hidden", 0))
 					DBWriteContactSettingByte(hContact, "ContactPhoto", "Hidden", hidden);
 				break;
 			}
@@ -851,16 +849,16 @@ INT_PTR CALLBACK DlgProcAvatarUserInfo(HWND hwndDlg, UINT msg, WPARAM wParam, LP
 				if (MessageBox(0, TranslateT("Delete picture file from disk (may be necessary to force a reload, but will delete local pictures)?"), TranslateT("Reset contact picture"), MB_YESNO) == IDYES) {
 					if (!DBGetContactSettingTString(hContact, "ContactPhoto", "File", &dbv)) {
 						DeleteFile(dbv.ptszVal);
-						DBFreeVariant(&dbv);
+						db_free(&dbv);
 					}
 				}
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Locked");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Backup");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "RFile");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "File");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Format");
-				DBDeleteContactSetting(hContact, szProto, "AvatarHash");
-				DBDeleteContactSetting(hContact, szProto, "AvatarSaved");
+				db_unset(hContact, "ContactPhoto", "Locked");
+				db_unset(hContact, "ContactPhoto", "Backup");
+				db_unset(hContact, "ContactPhoto", "RFile");
+				db_unset(hContact, "ContactPhoto", "File");
+				db_unset(hContact, "ContactPhoto", "Format");
+				db_unset(hContact, szProto, "AvatarHash");
+				db_unset(hContact, szProto, "AvatarSaved");
 				DeleteAvatarFromCache(hContact, FALSE);
 
 				QueueAdd(hContact);
@@ -874,14 +872,14 @@ INT_PTR CALLBACK DlgProcAvatarUserInfo(HWND hwndDlg, UINT msg, WPARAM wParam, LP
 				if (MessageBox(0, TranslateT("Delete picture file from disk (may be necessary to force a reload, but will delete local pictures)?"), TranslateT("Reset contact picture"), MB_YESNO) == IDYES) {
 					if (!DBGetContactSettingTString(hContact, "ContactPhoto", "File", &dbv)) {
 						DeleteFile(dbv.ptszVal);
-						DBFreeVariant(&dbv);
+						db_free(&dbv);
 					}
 				}
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Locked");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Backup");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "RFile");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "File");
-				DBDeleteContactSetting(hContact, "ContactPhoto", "Format");
+				db_unset(hContact, "ContactPhoto", "Locked");
+				db_unset(hContact, "ContactPhoto", "Backup");
+				db_unset(hContact, "ContactPhoto", "RFile");
+				db_unset(hContact, "ContactPhoto", "File");
+				db_unset(hContact, "ContactPhoto", "Format");
 				DeleteAvatarFromCache(hContact, FALSE);
 				SendMessage(hwndDlg, DM_SETAVATARNAME, 0, 0);
 				break;
@@ -950,7 +948,7 @@ static void EnableDisableControls(HWND hwndDlg, char *proto)
 	{
 		EnableWindow(GetDlgItem(hwndDlg, IDC_CHANGE), TRUE);
 
-		if (DBGetContactSettingByte(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1))
+		if (db_get_b(NULL, AVS_MODULE, "GlobalUserAvatarNotConsistent", 1))
 		{
 			EnableWindow(GetDlgItem(hwndDlg, IDC_DELETE), TRUE);
 		}
@@ -1071,7 +1069,7 @@ INT_PTR CALLBACK DlgProcAvatarProtoInfo(HWND hwndDlg, UINT msg, WPARAM wParam, L
 			ListView_Arrange(hwndList, LVA_ALIGNLEFT | LVA_ALIGNTOP);
 
 			// Check if should show per protocol avatars
-			CheckDlgButton(hwndDlg, IDC_PER_PROTO, DBGetContactSettingByte(NULL, AVS_MODULE, "PerProtocolUserAvatars", 1));
+			CheckDlgButton(hwndDlg, IDC_PER_PROTO, db_get_b(NULL, AVS_MODULE, "PerProtocolUserAvatars", 1));
 			EnableDisableProtocols(hwndDlg, TRUE);
 		}
 		break;
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);
 		}
 	}
 
diff --git a/plugins/AVS/src/poll.h b/plugins/AVS/src/poll.h
index 9761e7360a..cfc5417488 100644
--- a/plugins/AVS/src/poll.h
+++ b/plugins/AVS/src/poll.h
@@ -28,7 +28,6 @@ struct QueueItem
 };
 
 void InitPolls();
-void FreePolls();
 
 // Add an contact to a queue
 void QueueAdd(HANDLE hContact);
-- 
cgit v1.2.3