From e02a45507e91d072b86f26f36dbebf655ad9d79f Mon Sep 17 00:00:00 2001
From: Alexander Lantsev <aunsane@gmail.com>
Date: Sat, 27 Sep 2014 09:06:55 +0000
Subject: Tox: avatars support

git-svn-id: http://svn.miranda-ng.org/main/trunk@10611 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 protocols/Tox/src/tox_account.cpp |  4 ++--
 protocols/Tox/src/tox_avatars.cpp | 34 +++++++++++++++++++++++-----------
 protocols/Tox/src/tox_proto.h     |  2 +-
 3 files changed, 26 insertions(+), 14 deletions(-)

(limited to 'protocols')

diff --git a/protocols/Tox/src/tox_account.cpp b/protocols/Tox/src/tox_account.cpp
index b7c7276c12..ebb246c6f4 100644
--- a/protocols/Tox/src/tox_account.cpp
+++ b/protocols/Tox/src/tox_account.cpp
@@ -135,14 +135,14 @@ void CToxProto::PollingThread(void*)
 				DoBootstrap();
 			}
 		}
-		else
+		/*else
 		{
 			if (!tox_isconnected(tox))
 			{
 				debugLogA("CToxProto::PollingThread: disconnected from DHT");
 				SetStatus(ID_STATUS_OFFLINE);
 			}
-		}
+		}*/
 	}
 
 	isConnected = false;
diff --git a/protocols/Tox/src/tox_avatars.cpp b/protocols/Tox/src/tox_avatars.cpp
index 820ad0cc2f..ef4810e520 100644
--- a/protocols/Tox/src/tox_avatars.cpp
+++ b/protocols/Tox/src/tox_avatars.cpp
@@ -1,6 +1,6 @@
 #include "common.h"
 
-TCHAR* CToxProto::GetContactAvatarFilePath(MCONTACT hContact)
+std::tstring CToxProto::GetContactAvatarFilePath(MCONTACT hContact)
 {
 	TCHAR path[MAX_PATH];
 	mir_sntprintf(path, SIZEOF(path), _T("%s\\%S"), VARST(_T("%miranda_avatarcache%")), m_szModuleName);
@@ -11,13 +11,13 @@ TCHAR* CToxProto::GetContactAvatarFilePath(MCONTACT hContact)
 
 	ptrT id(getTStringA(hContact, TOX_SETTINGS_ID));
 	if (hContact != NULL)
-		mir_sntprintf(path, MAX_PATH, _T("%s\\%s.jpg"), path, id);
+		mir_sntprintf(path, MAX_PATH, _T("%s\\%s.png"), path, id);
 	else if (id != NULL)
-		mir_sntprintf(path, MAX_PATH, _T("%s\\%s avatar.jpg"), path, id);
+		mir_sntprintf(path, MAX_PATH, _T("%s\\%s avatar.png"), path, id);
 	else
 		return NULL;
 
-	return mir_tstrdup(path);
+	return path;
 }
 
 bool CToxProto::SetToxAvatar(std::tstring path, bool checkHash)
@@ -73,7 +73,7 @@ bool CToxProto::SetToxAvatar(std::tstring path, bool checkHash)
 		return false;
 	}
 	mir_free(data);
-	
+
 	if (checkHash)
 	{
 		db_set_blob(NULL, m_szModuleName, TOX_SETTINGS_AVATAR_HASH, (void*)hash, TOX_HASH_LENGTH);
@@ -188,13 +188,14 @@ void CToxProto::OnGotFriendAvatarInfo(Tox *tox, int32_t number, uint8_t format,
 	MCONTACT hContact = proto->FindContact(number);
 	if (hContact)
 	{
-		TCHAR *path = proto->GetContactAvatarFilePath(hContact);
+		std::tstring path = proto->GetContactAvatarFilePath(hContact);
 		if (format == TOX_AVATAR_FORMAT_NONE)
 		{
 			proto->delSetting(hContact, TOX_SETTINGS_AVATAR_HASH);
+			proto->ProtoBroadcastAck(hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, 0, 0);
 			if (IsFileExists(path))
 			{
-				DeleteFile(path);
+				DeleteFile(path.c_str());
 			}
 		}
 		else
@@ -208,6 +209,10 @@ void CToxProto::OnGotFriendAvatarInfo(Tox *tox, int32_t number, uint8_t format,
 				}
 				db_free(&dbv);
 			}
+			else
+			{
+				tox_request_avatar_data(proto->tox, number);
+			}
 		}
 	}
 }
@@ -221,13 +226,20 @@ void CToxProto::OnGotFriendAvatarData(Tox *tox, int32_t number, uint8_t format,
 	{
 		db_set_blob(hContact, proto->m_szModuleName, TOX_SETTINGS_AVATAR_HASH, hash, TOX_HASH_LENGTH);
 
-		TCHAR *path = proto->GetContactAvatarFilePath(hContact);
-		FILE *hFile = _tfopen(path, L"wb");
+		std::tstring path = proto->GetContactAvatarFilePath(hContact);
+		FILE *hFile = _tfopen(path.c_str(), L"wb");
 		if (hFile)
 		{
-			fwrite(data, sizeof(uint8_t), length, hFile);
+			if (fwrite(data, sizeof(uint8_t), length, hFile) == length)
+			{
+				PROTO_AVATAR_INFORMATIONW pai = { sizeof(pai) };
+				pai.format = PA_FORMAT_PNG;
+				pai.hContact = hContact;
+				_tcscpy(pai.filename, path.c_str());
+
+				proto->ProtoBroadcastAck(hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, (HANDLE)&pai, 0);
+			}
 			fclose(hFile);
 		}
-		mir_free(path);
 	}
 }
\ No newline at end of file
diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h
index 06b15d3197..182bc4bb3f 100644
--- a/protocols/Tox/src/tox_proto.h
+++ b/protocols/Tox/src/tox_proto.h
@@ -198,7 +198,7 @@ private:
 	static void OnFileData(Tox *tox, int32_t number, uint8_t fileNumber, const uint8_t *data, uint16_t length, void *arg);
 
 	// avatars
-	TCHAR* GetContactAvatarFilePath(MCONTACT hContact);
+	std::tstring GetContactAvatarFilePath(MCONTACT hContact);
 	bool SetToxAvatar(std::tstring path, bool checkHash = false);
 
 	INT_PTR __cdecl GetAvatarCaps(WPARAM wParam, LPARAM lParam);
-- 
cgit v1.2.3