From 355f69d42075967db7f51081f9520fed3ea9a801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20P=C3=B6sel?= Date: Thu, 4 Dec 2014 06:53:31 +0000 Subject: Steam: Reworked avatars support; version bump This result in downloading avatars only when changed and not at every login git-svn-id: http://svn.miranda-ng.org/main/trunk@11236 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Steam/src/steam_avatars.cpp | 122 ++++++++++++++++++++++++--------- protocols/Steam/src/steam_contacts.cpp | 31 ++++----- protocols/Steam/src/steam_proto.h | 4 +- protocols/Steam/src/version.h | 4 +- 4 files changed, 109 insertions(+), 52 deletions(-) (limited to 'protocols') diff --git a/protocols/Steam/src/steam_avatars.cpp b/protocols/Steam/src/steam_avatars.cpp index b009e1c32d..22b80f1f26 100644 --- a/protocols/Steam/src/steam_avatars.cpp +++ b/protocols/Steam/src/steam_avatars.cpp @@ -1,6 +1,6 @@ #include "common.h" -wchar_t * CSteamProto::GetAvatarFilePath(MCONTACT hContact) +TCHAR* CSteamProto::GetAvatarFilePath(MCONTACT hContact) { TCHAR path[MAX_PATH]; mir_sntprintf(path, SIZEOF(path), _T("%s\\%S"), VARST(_T("%miranda_avatarcache%")), m_szModuleName); @@ -9,36 +9,86 @@ wchar_t * CSteamProto::GetAvatarFilePath(MCONTACT hContact) if (dwAttributes == 0xffffffff || (dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) CallService(MS_UTILS_CREATEDIRTREET, 0, (LPARAM)path); - ptrW steamId(db_get_wsa(hContact, m_szModuleName, "SteamID")); - if (hContact != NULL) - mir_sntprintf(path, MAX_PATH, _T("%s\\%s.jpg"), path, steamId); - else if (steamId != NULL) - mir_sntprintf(path, MAX_PATH, _T("%s\\%s avatar.jpg"), path, steamId); + ptrA steamId(getStringA(hContact, "SteamID")); + if (steamId != NULL) + mir_sntprintf(path, MAX_PATH, _T("%s\\%s.jpg"), path, _A2T(steamId)); else return NULL; - return mir_wstrdup(path); + return mir_tstrdup(path); } -INT_PTR CSteamProto::GetAvatarInfo(WPARAM, LPARAM lParam) +bool CSteamProto::GetDbAvatarInfo(PROTO_AVATAR_INFORMATIONT &pai) { - PROTO_AVATAR_INFORMATIONW *pai = (PROTO_AVATAR_INFORMATIONW *)lParam; + ptrT path(GetAvatarFilePath(pai.hContact)); + if (!path) + return false; - if (ptrA(getStringA(pai->hContact, "AvatarUrl"))) + _tcsncpy_s(pai.filename, path, _TRUNCATE); + pai.format = PA_FORMAT_JPEG; + + return true; +} + +void CSteamProto::CheckAvatarChange(MCONTACT hContact, std::string avatarUrl) +{ + if (avatarUrl.empty()) + return; + + // Check for avatar change + ptrA oldAvatarUrl(getStringA(hContact, "AvatarUrl")); + bool update_required = (!oldAvatarUrl || avatarUrl.compare(oldAvatarUrl)); + + if (update_required) + setString(hContact, "AvatarUrl", avatarUrl.c_str()); + + if (!hContact) + { + PROTO_AVATAR_INFORMATIONT pai = { sizeof(pai) }; + if (GetAvatarInfo(update_required ? GAIF_FORCE : 0, (LPARAM)&pai) != GAIR_WAITFOR) + CallService(MS_AV_REPORTMYAVATARCHANGED, (WPARAM)m_szModuleName, 0); + } + else if (update_required) + { + db_set_b(hContact, "ContactPhoto", "NeedUpdate", 1); + ProtoBroadcastAck(hContact, ACKTYPE_AVATAR, ACKRESULT_STATUS, NULL, 0); + } +} + +INT_PTR CSteamProto::GetAvatarInfo(WPARAM wParam, LPARAM lParam) +{ + if (!lParam) return GAIR_NOAVATAR; - ptrA steamId(getStringA(pai->hContact, "SteamID")); - if (steamId) + PROTO_AVATAR_INFORMATIONT* pai = (PROTO_AVATAR_INFORMATIONT*)lParam; + + ptrA avatarUrl(getStringA(pai->hContact, "AvatarUrl")); + if (!avatarUrl) + return GAIR_NOAVATAR; + + if (GetDbAvatarInfo(*pai)) { - ptrW path(GetAvatarFilePath(pai->hContact)); - if (path && !_waccess(path, 0)) + bool fileExist = _taccess(pai->filename, 0) == 0; + + bool needLoad; + if (pai->hContact) + needLoad = (wParam & GAIF_FORCE) && (!fileExist || db_get_b(pai->hContact, "ContactPhoto", "NeedUpdate", 0)); + else + needLoad = (wParam & GAIF_FORCE) || !fileExist; + + if (needLoad) { - wcsncpy(pai->filename, path, SIZEOF(pai->filename)); - pai->format = PA_FORMAT_JPEG; - return GAIR_SUCCESS; + PushRequest( + new SteamWebApi::GetAvatarRequest(avatarUrl), + &CSteamProto::OnGotAvatar, + (void*)pai->hContact); + + return GAIR_WAITFOR; } - } + else if (fileExist) + return GAIR_SUCCESS; + } return GAIR_NOAVATAR; } @@ -73,14 +123,14 @@ INT_PTR CSteamProto::GetAvatarCaps(WPARAM wParam, LPARAM lParam) // server accepts images of 32000 bytees, not bigger return 32000;*/ - /*case AF_DELAYAFTERFAIL: - // do not request avatar again if server gave an error - return 1;// * 60 * 60 * 1000; // one hour*/ - - /*case AF_FETCHIFPROTONOTVISIBLE: + case AF_DELAYAFTERFAIL: + // request avatar again in one hour if server gave an error + return 60 * 60 * 1000; + + case AF_FETCHIFPROTONOTVISIBLE: case AF_FETCHIFCONTACTOFFLINE: // avatars can be fetched all the time (server only operation) - return 1;*/ + return 1; } return 0; @@ -88,15 +138,23 @@ INT_PTR CSteamProto::GetAvatarCaps(WPARAM wParam, LPARAM lParam) INT_PTR CSteamProto::GetMyAvatar(WPARAM wParam, LPARAM lParam) { - if (!wParam) - return -2; + if (!wParam || !lParam) + return -3; - ptrW path(GetAvatarFilePath(NULL)); - if (path && !_waccess(path, 0)) - { - wcsncpy((wchar_t *)wParam, path, (int)lParam); + TCHAR* buf = (TCHAR*)wParam; + int size = (int)lParam; + + PROTO_AVATAR_INFORMATIONT ai = { sizeof(ai) }; + switch (GetAvatarInfo(0, (LPARAM)&ai)) { + case GAIR_SUCCESS: + _tcsncpy(buf, ai.filename, size); + buf[size - 1] = 0; return 0; - } - return -1; + case GAIR_WAITFOR: + return -1; + + default: + return -2; + } } \ No newline at end of file diff --git a/protocols/Steam/src/steam_contacts.cpp b/protocols/Steam/src/steam_contacts.cpp index 7354fa3395..8116588f0a 100644 --- a/protocols/Steam/src/steam_contacts.cpp +++ b/protocols/Steam/src/steam_contacts.cpp @@ -94,13 +94,8 @@ void CSteamProto::UpdateContact(MCONTACT hContact, JSONNODE *data) // avatar node = json_get(data, "avatarfull"); - ptrA avatarUrl(mir_u2a(json_as_string(node))); - setString(hContact, "AvatarUrl", avatarUrl); - - PushRequest( - new SteamWebApi::GetAvatarRequest(avatarUrl), - &CSteamProto::OnGotAvatar, - (void*)hContact); + std::string avatarUrl = _T2A(json_as_string(node)); + CheckAvatarChange(hContact, avatarUrl); // set country node = json_get(data, "loccountrycode"); @@ -317,28 +312,30 @@ void CSteamProto::OnGotUserSummaries(const NETLIBHTTPREQUEST *response, void *ar void CSteamProto::OnGotAvatar(const NETLIBHTTPREQUEST *response, void *arg) { - MCONTACT hContact = (MCONTACT)arg; + PROTO_AVATAR_INFORMATIONW pai = { sizeof(pai) }; + pai.hContact = (MCONTACT)arg; + GetDbAvatarInfo(pai); if (response == NULL || response->resultCode != HTTP_STATUS_OK) { - ptrA steamId(getStringA(hContact, "SteamID")); + ptrA steamId(getStringA(pai.hContact, "SteamID")); debugLogA("CSteamProto::OnGotAvatar: failed to get avatar %s", steamId); + + if (pai.hContact) + ProtoBroadcastAck(pai.hContact, ACKTYPE_AVATAR, ACKRESULT_FAILED, (HANDLE)&pai, 0); return; } - ptrW avatarPath(GetAvatarFilePath(hContact)); - FILE *fp = _wfopen(avatarPath, L"wb"); + FILE *fp = _tfopen(pai.filename, _T("wb")); if (fp) { fwrite(response->pData, sizeof(char), response->dataLength, fp); fclose(fp); - PROTO_AVATAR_INFORMATIONW pai = { sizeof(pai) }; - pai.format = PA_FORMAT_JPEG; - pai.hContact = hContact; - wcscpy(pai.filename, avatarPath); - - ProtoBroadcastAck(hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, (HANDLE)&pai, 0); + if (pai.hContact) + ProtoBroadcastAck(pai.hContact, ACKTYPE_AVATAR, ACKRESULT_SUCCESS, (HANDLE)&pai, 0); + else + CallService(MS_AV_REPORTMYAVATARCHANGED, (WPARAM)m_szModuleName, 0); } } diff --git a/protocols/Steam/src/steam_proto.h b/protocols/Steam/src/steam_proto.h index 192254fb46..639cefca18 100644 --- a/protocols/Steam/src/steam_proto.h +++ b/protocols/Steam/src/steam_proto.h @@ -240,7 +240,9 @@ protected: void OnInitStatusMenu(); // avatars - wchar_t * GetAvatarFilePath(MCONTACT hContact); + TCHAR* GetAvatarFilePath(MCONTACT hContact); + bool GetDbAvatarInfo(PROTO_AVATAR_INFORMATIONT &pai); + void CheckAvatarChange(MCONTACT hContact, std::string avatarUrl); INT_PTR __cdecl GetAvatarInfo(WPARAM, LPARAM); INT_PTR __cdecl GetAvatarCaps(WPARAM, LPARAM); diff --git a/protocols/Steam/src/version.h b/protocols/Steam/src/version.h index 3fd4e763d4..b3a82c9ee3 100644 --- a/protocols/Steam/src/version.h +++ b/protocols/Steam/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 11 -#define __RELEASE_NUM 0 -#define __BUILD_NUM 4 +#define __RELEASE_NUM 1 +#define __BUILD_NUM 0 #include -- cgit v1.2.3