From 9f7023fdefc4bbdc888c8bb208bc79bbaa8df225 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 11 Aug 2014 18:47:10 +0000 Subject: Tox: added friend list loading git-svn-id: http://svn.miranda-ng.org/main/trunk@10157 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/common.h | 7 ++++++ protocols/Tox/src/tox_account.cpp | 4 +++- protocols/Tox/src/tox_contacts.cpp | 43 ++++++++++++++++++++++++++-------- protocols/Tox/src/tox_options.cpp | 2 +- protocols/Tox/src/tox_proto.cpp | 6 ++--- protocols/Tox/src/tox_proto.h | 11 +++++---- protocols/Tox/src/tox_utils.cpp | 47 +++++++++++++------------------------- 7 files changed, 70 insertions(+), 50 deletions(-) (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/common.h b/protocols/Tox/src/common.h index 7aaddd04b4..d9bc42d4ce 100644 --- a/protocols/Tox/src/common.h +++ b/protocols/Tox/src/common.h @@ -5,6 +5,11 @@ #include #include +#include +#include +#include +#include + #include #include @@ -24,4 +29,6 @@ extern HINSTANCE g_hInstance; +#define TOX_SETTING_ID "ToxID" + #endif //_COMMON_H_ \ No newline at end of file diff --git a/protocols/Tox/src/tox_account.cpp b/protocols/Tox/src/tox_account.cpp index 33b1f4a8ed..59edd4ec52 100644 --- a/protocols/Tox/src/tox_account.cpp +++ b/protocols/Tox/src/tox_account.cpp @@ -48,7 +48,9 @@ void CToxProto::ConnectionThread(void*) if (tox_isconnected(tox)) { isConnected = true; - + + LoadContactList(); + m_iStatus = m_iDesiredStatus = ID_STATUS_ONLINE; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_CONNECTING, m_iStatus); diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index 263e922595..929ca1fbf3 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -5,7 +5,7 @@ bool CToxProto::IsProtoContact(MCONTACT hContact) return ::lstrcmpiA(::GetContactProto(hContact), m_szModuleName) == 0; } -MCONTACT CToxProto::GetContactByUserId(const wchar_t *userId) +MCONTACT CToxProto::GetContactByUserId(const char *clientId) { MCONTACT hContact = NULL; @@ -13,8 +13,8 @@ MCONTACT CToxProto::GetContactByUserId(const wchar_t *userId) for (hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) { - ptrW cUserId(::db_get_wsa(hContact, m_szModuleName, "UserID")); - if (lstrcmpi(cUserId, userId) == 0) + ptrA contactId(getStringA(hContact, TOX_SETTING_ID)); + if (lstrcmpiA(contactId, clientId) == 0) break; } @@ -23,26 +23,49 @@ MCONTACT CToxProto::GetContactByUserId(const wchar_t *userId) return hContact; } -MCONTACT CToxProto::AddContact(const wchar_t *userId, const wchar_t *nick, bool isHidden) +MCONTACT CToxProto::AddContact(const char *clientId, const char *nick, bool isHidden) { - MCONTACT hContact = GetContactByUserId(userId); - if ( !hContact) + MCONTACT hContact = GetContactByUserId(clientId); + if (!hContact) { hContact = (MCONTACT)::CallService(MS_DB_CONTACT_ADD, 0, 0); - ::CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)m_szModuleName); + CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)m_szModuleName); - db_set_b(hContact, "CList", "NotOnList", 1); + /*db_set_b(hContact, "CList", "NotOnList", 1); if (isHidden) + { db_set_b(hContact, "CList", "Hidden", 1); + }*/ - setWString(hContact, "UserId", userId); - setWString(hContact, "Nick", nick); + setString(hContact, TOX_SETTING_ID, clientId); + setString(hContact, "Nick", nick); } return hContact; } +void CToxProto::LoadContactList() +{ + uint32_t count = tox_count_friendlist(tox); + if (count > 0) + { + int32_t *friends = (int32_t*)mir_alloc(count * sizeof(int32_t)); + tox_get_friendlist(tox, friends, count); + std::vector clientId(TOX_CLIENT_ID_SIZE); + std::vector username(TOX_MAX_NAME_LENGTH); + for (uint32_t i = 0; i < count; ++i) + { + tox_get_client_id(tox, friends[i], &clientId[0]); + std::string toxId = DataToHexString(clientId); + tox_get_name(tox, friends[i], &username[0]); + std::string nick(username.begin(), username.end()); + + AddContact(toxId.c_str(), nick.c_str()); + } + } +} + void __cdecl CToxProto::SearchByUidAsync(void* arg) { ptrW userId((wchar_t*)arg); diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index d9779c6dc4..7f5fd47824 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -19,7 +19,7 @@ INT_PTR CALLBACK CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, if (!dataPath) { char defaultPath[MAX_PATH]; - mir_snprintf(defaultPath, MAX_PATH, "%s\\Tox\\%s.tox", VARS("%miranda_userdata%"), _T2A(proto->m_tszUserName)); + mir_snprintf(defaultPath, MAX_PATH, "%s\\%s.tox", VARS("%miranda_userdata%"), _T2A(proto->m_tszUserName)); dataPath = mir_strdup(defaultPath); } SetDlgItemTextA(hwnd, IDC_DATAPATH, dataPath); diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index c20d1aa7e4..9413a56e40 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -35,9 +35,9 @@ CToxProto::~CToxProto() MCONTACT __cdecl CToxProto::AddToList(int flags, PROTOSEARCHRESULT* psr) { - MCONTACT hContact = AddContact(psr->id, psr->nick); +// MCONTACT hContact = AddContact(psr->id, psr->nick); - return hContact; + return NULL; } MCONTACT __cdecl CToxProto::AddToListByEvent(int flags, int iContact, HANDLE hDbEvent) { return 0; } @@ -67,7 +67,7 @@ DWORD_PTR __cdecl CToxProto::GetCaps(int type, MCONTACT hContact) case PFLAG_UNIQUEIDTEXT: return (INT_PTR)"Tox ID"; case PFLAG_UNIQUEIDSETTING: - return (DWORD_PTR)"ToxID"; + return (DWORD_PTR)TOX_SETTING_ID; case PFLAG_MAXLENOFMESSAGE: return TOX_MAX_MESSAGE_LENGTH; } diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 502928419b..220e07d0c0 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -104,8 +104,10 @@ private: // contacts bool IsProtoContact(MCONTACT hContact); - MCONTACT GetContactByUserId(const wchar_t *userId); - MCONTACT AddContact(const wchar_t*userId, const wchar_t *nick, bool isHidden = false); + MCONTACT GetContactByUserId(const char *clientId); + MCONTACT AddContact(const char *clientId, const char *nick, bool isHidden = false); + + void LoadContactList(); void __cdecl SearchByUidAsync(void* arg); @@ -115,8 +117,9 @@ private: // utils TOX_USERSTATUS MirandaToToxStatus(int status); - uint8_t *HexStringToData(const char *hex_string); - char *DataToHexString(const uint8_t *bin_string); + + std::vector HexStringToData(const std::string hex); + std::string DataToHexString(const std::vector); int LoadToxData(const char *path); int SaveToxData(const char *path); diff --git a/protocols/Tox/src/tox_utils.cpp b/protocols/Tox/src/tox_utils.cpp index e6f1a037f8..456b1dd75a 100644 --- a/protocols/Tox/src/tox_utils.cpp +++ b/protocols/Tox/src/tox_utils.cpp @@ -21,47 +21,32 @@ TOX_USERSTATUS CToxProto::MirandaToToxStatus(int status) return userstatus; } -uint8_t *HexStringToData(const char *hex_string) +std::vector HexStringToData(const std::string hex) { - size_t legth = strlen(hex_string) / 2; - uint8_t *data = (uint8_t*)mir_alloc(legth); + std::stringstream ss; + std::vector data; - for (size_t i = 0; i < legth; i++) + size_t count = hex.length() / 2; + for (size_t i = 0; i < count; i++) { - unsigned int val; - sscanf(&hex_string[i * 2], "%2hhx", &val); - data[i] = val; + uint8_t temp; + ss << std::hex << hex.substr(i * 2, 2); + ss >> temp; + data.push_back(temp); } return data; } -char *CToxProto::DataToHexString(const uint8_t *bin_string) +std::string CToxProto::DataToHexString(const std::vector data) { - uint32_t delta = 0, pos_extra, sum_extra = 0; - char *ret = (char*)mir_alloc(TOX_FRIEND_ADDRESS_SIZE + 1); - - for (uint32_t i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) { - sprintf(&ret[2 * i + delta], "%02X", bin_string[i]); - - if ((i + 1) == TOX_CLIENT_ID_SIZE) - pos_extra = 2 * (i + 1) + delta; - - if (i >= TOX_CLIENT_ID_SIZE) - sum_extra |= bin_string[i]; - - /*if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) { - id_str[2 * (i + 1) + delta] = ' '; - delta++; - }*/ + std::stringstream ss; + ss << std::hex << std::uppercase; + for (uint32_t i = 0; i < data.size(); i++) + { + ss << (int)data[i]; } - - //ret[2 * i + delta] = 0; - - if (!sum_extra) - ret[pos_extra] = 0; - - return ret; + return ss.str(); } int CToxProto::LoadToxData(const char *path) -- cgit v1.2.3