QString Core::getPeerName(const ToxId& id) const { QString name; CUserId cid(id.toString()); uint32_t friendId = tox_friend_by_public_key(tox, (uint8_t*)cid.data(), nullptr); if (friendId == std::numeric_limits<uint32_t>::max()) { qWarning() << "getPeerName: No such peer"; return name; } const size_t nameSize = tox_friend_get_name_size(tox, friendId, nullptr); if (nameSize == SIZE_MAX) return name; uint8_t* cname = new uint8_t[nameSize<TOX_MAX_NAME_LENGTH ? TOX_MAX_NAME_LENGTH : nameSize]; if (!tox_friend_get_name(tox, friendId, cname, nullptr)) { qWarning() << "getPeerName: Can't get name of friend "+QString().setNum(friendId); delete[] cname; return name; } name = CString::toString(cname, nameSize); delete[] cname; return name; }
QString Core::getPeerName(const ToxID& id) const { QString name; CUserId cid(id.toString()); uint32_t friendId = tox_friend_by_public_key(tox, (uint8_t*)cid.data(), nullptr); if (friendId == std::numeric_limits<uint32_t>::max()) { qWarning() << "Core::getPeerName: No such peer "+id.toString(); return name; } const size_t nameSize = tox_friend_get_name_size(tox, friendId, nullptr); if (nameSize == SIZE_MAX) { //qDebug() << "Core::getPeerName: Can't get name of friend "+QString().setNum(friendId)+" ("+id.toString()+")"; return name; } uint8_t* cname = new uint8_t[nameSize<TOX_MAX_NAME_LENGTH ? TOX_MAX_NAME_LENGTH : nameSize]; if (tox_friend_get_name(tox, friendId, cname, nullptr) == false) { qWarning() << "Core::getPeerName: Can't get name of friend "+QString().setNum(friendId)+" ("+id.toString()+")"; delete[] cname; return name; } name = name.fromLocal8Bit((char*)cname, nameSize); delete[] cname; return name; }
/** * @brief Checks if we have a friend by public key */ bool Core::hasFriendWithPublicKey(const ToxPk& publicKey) const { if (publicKey.isEmpty()) { return false; } // TODO: error handling uint32_t friendId = tox_friend_by_public_key(tox, publicKey.getBytes(), nullptr); return friendId != std::numeric_limits<uint32_t>::max(); }