1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(toxid): improve code clarity and documentation

This commit is contained in:
sudden6 2016-12-26 01:39:22 +01:00
parent 152c134a4b
commit 3d0938a3c6
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
2 changed files with 42 additions and 37 deletions

View File

@ -446,7 +446,7 @@ void Core::bootstrapDht()
void Core::onFriendRequest(Tox*/* tox*/, const uint8_t* cFriendPk, void Core::onFriendRequest(Tox*/* tox*/, const uint8_t* cFriendPk,
const uint8_t* cMessage, size_t cMessageSize, void* core) const uint8_t* cMessage, size_t cMessageSize, void* core)
{ {
QString friendPk = ToxId(QByteArray(reinterpret_cast<const char*> (cFriendPk), TOX_PUBLIC_KEY_SIZE)).getPublicKeyString(); QString friendPk = ToxId(cFriendPk, TOX_PUBLIC_KEY_SIZE).getPublicKeyString();
emit static_cast<Core*>(core)->friendRequestReceived(friendPk, CString::toString(cMessage, cMessageSize)); emit static_cast<Core*>(core)->friendRequestReceived(friendPk, CString::toString(cMessage, cMessageSize));
} }
@ -558,7 +558,7 @@ void Core::onReadReceiptCallback(Tox*, uint32_t friendId, uint32_t receipt, void
void Core::acceptFriendRequest(const QString& userId) void Core::acceptFriendRequest(const QString& userId)
{ {
uint32_t friendId = tox_friend_add_norequest(tox, reinterpret_cast<const uint8_t*> (ToxId(userId).getPublicKeyBytes()), nullptr); uint32_t friendId = tox_friend_add_norequest(tox, ToxId(userId).getPublicKeyBytes(), nullptr);
if (friendId == std::numeric_limits<uint32_t>::max()) if (friendId == std::numeric_limits<uint32_t>::max())
{ {
emit failedToAddFriend(userId); emit failedToAddFriend(userId);
@ -858,8 +858,8 @@ void Core::setAvatar(const QByteArray& data)
*/ */
ToxId Core::getSelfId() const ToxId Core::getSelfId() const
{ {
QByteArray friendAddress(TOX_ADDRESS_SIZE, 0x00); uint8_t friendAddress[TOX_ADDRESS_SIZE] = {0x00};
tox_self_get_address(tox, reinterpret_cast<uint8_t*>(friendAddress.data())); tox_self_get_address(tox, friendAddress.data());
return ToxId(friendAddress); return ToxId(friendAddress);
} }
@ -875,8 +875,8 @@ QPair<QByteArray, QByteArray> Core::getKeypair() const
QByteArray pk(TOX_PUBLIC_KEY_SIZE, 0x00); QByteArray pk(TOX_PUBLIC_KEY_SIZE, 0x00);
QByteArray sk(TOX_SECRET_KEY_SIZE, 0x00); QByteArray sk(TOX_SECRET_KEY_SIZE, 0x00);
tox_self_get_public_key(tox, (uint8_t*) pk.data()); tox_self_get_public_key(tox, reinterpret_cast<uint8_t*>(pk.data()));
tox_self_get_secret_key(tox, (uint8_t*) sk.data()); tox_self_get_secret_key(tox, reinterpret_cast<uint8_t*>(sk.data()));
keypair.first = pk; keypair.first = pk;
keypair.second = sk; keypair.second = sk;
@ -986,17 +986,17 @@ void Core::loadFriends()
// assuming there are not that many friends to fill up the whole stack // assuming there are not that many friends to fill up the whole stack
uint32_t *ids = new uint32_t[friendCount]; uint32_t *ids = new uint32_t[friendCount];
tox_self_get_friend_list(tox, ids); tox_self_get_friend_list(tox, ids);
QByteArray friendPk(TOX_PUBLIC_KEY_SIZE, 0x00); uint8_t friendPk[TOX_PUBLIC_KEY_SIZE] = {0x00};
for (int32_t i = 0; i < static_cast<int32_t>(friendCount); ++i) for (int32_t i = 0; i < static_cast<int32_t>(friendCount); ++i)
{ {
if (tox_friend_get_public_key(tox, ids[i], reinterpret_cast<uint8_t*>(friendPk.data()), nullptr)) if (tox_friend_get_public_key(tox, ids[i], friendPk, nullptr))
{ {
emit friendAdded(ids[i], ToxId(friendPk).getPublicKeyString()); emit friendAdded(ids[i], ToxId(friendPk, TOX_PUBLIC_KEY_SIZE).getPublicKeyString());
const size_t nameSize = tox_friend_get_name_size(tox, ids[i], nullptr); const size_t nameSize = tox_friend_get_name_size(tox, ids[i], nullptr);
if (nameSize && nameSize != SIZE_MAX) if (nameSize && nameSize != SIZE_MAX)
{ {
uint8_t *name = new uint8_t[nameSize]; uint8_t* name = new uint8_t[nameSize];
if (tox_friend_get_name(tox, ids[i], name, nullptr)) if (tox_friend_get_name(tox, ids[i], name, nullptr))
emit friendUsernameChanged(ids[i], CString::toString(name, nameSize)); emit friendUsernameChanged(ids[i], CString::toString(name, nameSize));
delete[] name; delete[] name;
@ -1005,7 +1005,7 @@ void Core::loadFriends()
const size_t statusMessageSize = tox_friend_get_status_message_size(tox, ids[i], nullptr); const size_t statusMessageSize = tox_friend_get_status_message_size(tox, ids[i], nullptr);
if (statusMessageSize != SIZE_MAX) if (statusMessageSize != SIZE_MAX)
{ {
uint8_t *statusMessage = new uint8_t[statusMessageSize]; uint8_t* statusMessage = new uint8_t[statusMessageSize];
if (tox_friend_get_status_message(tox, ids[i], statusMessage, nullptr)) if (tox_friend_get_status_message(tox, ids[i], statusMessage, nullptr))
{ {
emit friendStatusMessageChanged(ids[i], CString::toString(statusMessage, statusMessageSize)); emit friendStatusMessageChanged(ids[i], CString::toString(statusMessage, statusMessageSize));
@ -1104,17 +1104,16 @@ QString Core::getGroupPeerName(int groupId, int peerId) const
*/ */
ToxId Core::getGroupPeerToxId(int groupId, int peerId) const ToxId Core::getGroupPeerToxId(int groupId, int peerId) const
{ {
QByteArray friendPk(TOX_PUBLIC_KEY_SIZE, 0x00); uint8_t friendPk[TOX_PUBLIC_KEY_SIZE] = {0x00};
TOX_ERR_CONFERENCE_PEER_QUERY error; TOX_ERR_CONFERENCE_PEER_QUERY error;
bool success = tox_conference_peer_get_public_key(tox, groupId, peerId, bool success = tox_conference_peer_get_public_key(tox, groupId, peerId, friendPk, &error);
reinterpret_cast<uint8_t*> (friendPk.data()), &error);
if (!parsePeerQueryError(error) || !success) if (!parsePeerQueryError(error) || !success)
{ {
qWarning() << "getGroupPeerToxId: Unknown error"; qWarning() << "getGroupPeerToxId: Unknown error";
return ToxId(); return ToxId();
} }
return ToxId(friendPk); return ToxId(friendPk, TOX_PUBLIC_KEY_SIZE);
} }
/** /**

View File

@ -19,11 +19,12 @@
#include "toxid.h" #include "toxid.h"
#include "core.h" #include "core.h"
#include <tox/tox.h> #include <tox/tox.h>
#include <qregularexpression.h>
#include <QRegularExpression>
#include <cstdint>
// Tox doesn't publicly define these // Tox doesn't publicly define these
#define NOSPAM_BYTES 4 #define NOSPAM_BYTES 4
@ -69,8 +70,9 @@ ToxId::ToxId(const ToxId& other)
/** /**
* @brief Create a Tox ID from a QString. * @brief Create a Tox ID from a QString.
* *
* If the given id is not a valid Tox ID, then: * If the given rawId is not a valid Tox ID, but can be a Public Key then:
* publicKey == id and noSpam == "" == checkSum. * publicKey == rawId and noSpam == 0 == checkSum.
* If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created.
* *
* @param id Tox ID string to convert to ToxId object * @param id Tox ID string to convert to ToxId object
*/ */
@ -93,10 +95,11 @@ ToxId::ToxId(const QString& id)
/** /**
* @brief Create a Tox ID from a QByteArray. * @brief Create a Tox ID from a QByteArray.
* *
* If the given id is not a valid Tox ID, then: * If the given rawId is not a valid Tox ID, but can be a Public Key then:
* publicKey == id and noSpam == "" == checkSum. * publicKey == rawId and noSpam == 0 == checkSum.
* If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created.
* *
* @param id Tox ID string to convert to ToxId object * @param rawId Tox ID bytes to convert to ToxId object
*/ */
ToxId::ToxId(const QByteArray& rawId) ToxId::ToxId(const QByteArray& rawId)
{ {
@ -104,12 +107,15 @@ ToxId::ToxId(const QByteArray& rawId)
} }
/** /**
* @brief Create a Tox ID from a uint8_t* and length. * @brief Create a Tox ID from uint8_t bytes and lenght, convenience function for toxcore interface.
* *
* If the given id is not a valid Tox ID, then: * If the given rawId is not a valid Tox ID, but can be a Public Key then:
* publicKey == id and noSpam == "" == checkSum. * publicKey == rawId and noSpam == 0 == checkSum.
* If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created.
* *
* @param id Tox ID string to convert to ToxId object * @param rawId Pointer to bytes to convert to ToxId object
* @param len Number of bytes to read. Must be TOX_SECRET_KEY_SIZE for a Public Key or
* TOX_ADDRESS_SIZE for a Tox ID.
*/ */
ToxId::ToxId(const uint8_t& rawId, int len) ToxId::ToxId(const uint8_t& rawId, int len)
{ {
@ -126,7 +132,6 @@ void ToxId::checkToxId(const QByteArray& rawId)
} }
else if (rawId.length() == TOX_ADDRESS_SIZE else if (rawId.length() == TOX_ADDRESS_SIZE
&& isToxId(rawId.toHex().toUpper())) && isToxId(rawId.toHex().toUpper()))
{ {
toxId = QByteArray(rawId); // construct from full toxid toxId = QByteArray(rawId); // construct from full toxid
} }
@ -137,9 +142,9 @@ void ToxId::checkToxId(const QByteArray& rawId)
} }
/** /**
* @brief Compares, that public key equals. * @brief Compares the equality of the Public Key.
* @param other Tox ID to compare. * @param other Tox ID to compare.
* @return True if both Tox ID have same public keys, false otherwise. * @return True if both Tox IDs have the same public keys, false otherwise.
*/ */
bool ToxId::operator==(const ToxId& other) const bool ToxId::operator==(const ToxId& other) const
{ {
@ -147,9 +152,9 @@ bool ToxId::operator==(const ToxId& other) const
} }
/** /**
* @brief Compares, that only public key not equals. * @brief Compares the inequality of the Public Key.
* @param other Tox ID to compare. * @param other Tox ID to compare.
* @return True if both Tox ID have different public keys, false otherwise. * @return True if both Tox IDs have different public keys, false otherwise.
*/ */
bool ToxId::operator!=(const ToxId& other) const bool ToxId::operator!=(const ToxId& other) const
{ {
@ -157,7 +162,8 @@ bool ToxId::operator!=(const ToxId& other) const
} }
/** /**
* @brief Returns Tox ID converted to QString. * @brief Returns the Tox ID converted to QString.
* Is equal to getPublicKey() if the Tox ID was constructed from only a Public Key.
* @return The Tox ID as QString. * @return The Tox ID as QString.
*/ */
QString ToxId::toString() const QString ToxId::toString() const
@ -187,11 +193,11 @@ bool ToxId::isValidToxId(const QString& id)
/** /**
* @brief Gets the ToxID as bytes, convenience function for toxcore interface. * @brief Gets the ToxID as bytes, convenience function for toxcore interface.
* @return The ToxID * @return The ToxID as uint8_t*
*/ */
const uint8_t* ToxId::getBytes() const const uint8_t* ToxId::getBytes() const
{ {
return reinterpret_cast<const uint8_t*> (toxId.constData()); return reinterpret_cast<const uint8_t*>(toxId.constData());
} }
/** /**
@ -204,12 +210,12 @@ QByteArray ToxId::getPublicKey() const
} }
/** /**
* @brief Gets the Public Key part of the ToxID, convenience fuction for toxcore interface. * @brief Gets the Public Key part of the ToxID, convenience function for toxcore interface.
* @return Public Key of the ToxID * @return Public Key of the ToxID as uint8_t*
*/ */
const uint8_t* ToxId::getPublicKeyBytes() const const uint8_t* ToxId::getPublicKeyBytes() const
{ {
return reinterpret_cast<const uint8_t*> (toxId.mid(0, TOX_PUBLIC_KEY_SIZE).constData()); return reinterpret_cast<const uint8_t*>(toxId.mid(0, TOX_PUBLIC_KEY_SIZE).constData());
} }
/** /**