From df1139ff3c50f0cffc7dc7a8a4e7310eb7e55e2d Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 23 Aug 2014 18:49:59 +0000 Subject: Tox: - fixed own status changing - refactored contacts adding git-svn-id: http://svn.miranda-ng.org/main/trunk@10315 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_contacts.cpp | 71 ++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 37 deletions(-) (limited to 'protocols/Tox/src/tox_contacts.cpp') diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index c5bc9890b2..0806bc7b24 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -34,7 +34,7 @@ MCONTACT CToxProto::GetContactFromAuthEvent(HANDLE hEvent) dbei.cbBlob = sizeof(DWORD)* 2; dbei.pBlob = (PBYTE)&body; - if (::db_event_get(hEvent, &dbei)) + if (db_event_get(hEvent, &dbei)) return INVALID_CONTACT_ID; if (dbei.eventType != EVENTTYPE_AUTHREQUEST) @@ -52,7 +52,7 @@ bool CToxProto::IsProtoContact(MCONTACT hContact) return lstrcmpiA(GetContactProto(hContact), m_szModuleName) == 0; } -MCONTACT CToxProto::FindContact(const char *clientId) +MCONTACT CToxProto::FindContact(const std::string &id) { MCONTACT hContact = NULL; @@ -60,9 +60,11 @@ MCONTACT CToxProto::FindContact(const char *clientId) for (hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) { - ptrA contactId(getStringA(hContact, TOX_SETTINGS_ID)); - if (lstrcmpiA(contactId, clientId) == 0) + std::string contactId = ToxAddressToId(getStringA(hContact, TOX_SETTINGS_ID)); + if (id.compare(contactId)) + { break; + } } //LeaveCriticalSection(&contact_search_lock); @@ -70,26 +72,25 @@ MCONTACT CToxProto::FindContact(const char *clientId) return hContact; } -MCONTACT CToxProto::AddContact(const char *clientId, bool isTemporary) +MCONTACT CToxProto::FindContact(const int friendNumber) { - std::string toxId = clientId; - if (toxId.length() > TOX_CLIENT_ID_SIZE * 2) - { - toxId.erase(toxId.begin() + TOX_CLIENT_ID_SIZE * 2, toxId.end()); - } - MCONTACT hContact = FindContact(toxId.c_str()); + std::vector clientId(TOX_CLIENT_ID_SIZE); + tox_get_client_id(tox, friendNumber, &clientId[0]); + std::string id = DataToHexString(clientId); + + return FindContact(id); +} + +MCONTACT CToxProto::AddContact(const std::string &id, bool isTemporary) +{ + MCONTACT hContact = FindContact(id); if (!hContact) { hContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0); CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)m_szModuleName); - if (isTemporary) - { - db_set_b(hContact, "CList", "NotOnList", 1); - db_set_b(hContact, "CList", "Auth", 1); - } - - setString(hContact, TOX_SETTINGS_ID, toxId.c_str()); + setString(hContact, TOX_SETTINGS_ID, id.c_str()); + setByte(hContact, "Auth", 1); DBVARIANT dbv; if (!getTString(TOX_SETTINGS_GROUP, &dbv)) @@ -97,8 +98,12 @@ MCONTACT CToxProto::AddContact(const char *clientId, bool isTemporary) db_set_ts(hContact, "CList", "Group", dbv.ptszVal); db_free(&dbv); } - } + if (isTemporary) + { + db_set_b(hContact, "CList", "NotOnList", 1); + } + } return hContact; } @@ -109,13 +114,14 @@ void CToxProto::LoadContactList() { int32_t *friends = (int32_t*)mir_alloc(count * sizeof(int32_t)); tox_get_friendlist(tox, friends, count); + std::vector clientId(TOX_CLIENT_ID_SIZE); for (uint32_t i = 0; i < count; ++i) { tox_get_client_id(tox, friends[i], &clientId[0]); - std::string toxId = DataToHexString(clientId); + std::string id = DataToHexString(clientId); - MCONTACT hContact = AddContact(toxId.c_str()); + MCONTACT hContact = AddContact(id.c_str()); if (hContact) { int size = tox_get_name_size(tox, friends[i]); @@ -132,24 +138,15 @@ void CToxProto::LoadContactList() } } } -} - -void CToxProto::SearchByIdAsync(void* arg) -{ - std::string toxId = (char*)arg; - toxId.erase(toxId.begin() + TOX_CLIENT_ID_SIZE * 2, toxId.end()); - - MCONTACT hContact = FindContact(toxId.c_str()); - if (hContact) + else { - ShowNotification(TranslateT("Contact already in your contact list"), 0, hContact); - ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HWND)1, 0); - mir_free(arg); - return; + debugLogA("CToxProto::LoadContactList: your friend list is empty"); } +} +void CToxProto::SearchByIdAsync(void*) +{ ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HWND)1, 0); - mir_free(arg); } void CToxProto::SearchByNameAsync(void* arg) @@ -158,8 +155,8 @@ void CToxProto::SearchByNameAsync(void* arg) request.requestType = REQUEST_POST; request.szUrl = "https://toxme.se/api"; request.flags = NLHRF_HTTP11 | NLHRF_SSL | NLHRF_NODUMP; - - request.headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)*2); + + request.headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)* 2); request.headers[0].szName = "Content-Type"; request.headers[0].szValue = "text/plain; charset=utf-8"; request.headersCount = 1; -- cgit v1.2.3