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

refactor(toxid): drop the cData class in favour of ToxId

This commit is contained in:
sudden6 2016-12-25 23:16:00 +01:00
parent 94ec5614f2
commit 152c134a4b
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
7 changed files with 72 additions and 184 deletions

View File

@ -526,7 +526,6 @@ SOURCES += \
src/chatlog/customtextdocument.cpp\
src/chatlog/documentcache.cpp \
src/chatlog/pixmapcache.cpp \
src/core/cdata.cpp \
src/core/core.cpp \
src/core/coreav.cpp \
src/core/coreencryption.cpp \

View File

@ -1,83 +0,0 @@
/*
Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
Copyright © 2014-2015 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cdata.h"
#include <QString>
#include <tox/tox.h>
CData::CData(const QString &data, uint16_t byteSize)
{
cData = new uint8_t[byteSize+1];
cDataSize = fromString(data, cData);
}
CData::~CData()
{
delete[] cData;
}
uint8_t* CData::data()
{
return cData;
}
uint16_t CData::size() const
{
return cDataSize;
}
QString CData::toString(const uint8_t *cData, const uint16_t cDataSize)
{
return QString(QByteArray(reinterpret_cast<const char*>(cData), cDataSize).toHex()).toUpper();
}
uint16_t CData::fromString(const QString& data, uint8_t* cData)
{
QByteArray arr = QByteArray::fromHex(data.toLower().toLatin1());
memcpy(cData, reinterpret_cast<uint8_t*>(arr.data()), arr.size());
return arr.size();
}
const uint16_t CUserId::SIZE{TOX_PUBLIC_KEY_SIZE};
CUserId::CUserId(const QString &userId) :
CData(userId, SIZE < userId.size() ? userId.size() : SIZE)
{
// intentionally left empty
}
QString CUserId::toString(const uint8_t* cUserId)
{
return CData::toString(cUserId, SIZE);
}
const uint16_t CFriendAddress::SIZE{TOX_ADDRESS_SIZE};
CFriendAddress::CFriendAddress(const QString &friendAddress) :
CData(friendAddress, SIZE)
{
// intentionally left empty
}
QString CFriendAddress::toString(const uint8_t *cFriendAddress)
{
return CData::toString(cFriendAddress, SIZE);
}

View File

@ -1,72 +0,0 @@
/*
Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
Copyright © 2014-2015 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CDATA_H
#define CDATA_H
#include <cstdint>
class QString;
class CData
{
public:
uint8_t* data();
uint16_t size() const;
protected:
explicit CData(const QString& data, uint16_t byteSize);
CData(const CData& other)=delete;
virtual ~CData();
CData& operator=(const CData& other)=delete;
static QString toString(const uint8_t* cData, const uint16_t cDataSize);
private:
uint8_t* cData;
uint16_t cDataSize;
static uint16_t fromString(const QString& userId, uint8_t* cData);
};
class CUserId : public CData
{
public:
explicit CUserId(const QString& userId);
static QString toString(const uint8_t *cUserId);
private:
static const uint16_t SIZE;
};
class CFriendAddress : public CData
{
public:
explicit CFriendAddress(const QString& friendAddress);
static QString toString(const uint8_t* cFriendAddress);
private:
static const uint16_t SIZE;
};
#endif // CDATA_H

View File

@ -20,7 +20,6 @@
#include "core.h"
#include "nexus.h"
#include "core/cdata.h"
#include "core/cstring.h"
#include "core/coreav.h"
#include "persistence/settings.h"
@ -424,14 +423,17 @@ void Core::bootstrapDht()
qDebug() << "Connecting to "+QString(dhtServer.address.toLatin1().data())
+':'+QString().setNum(dhtServer.port)+" ("+dhtServer.name+')';
if (!tox_bootstrap(tox, dhtServer.address.toLatin1().data(),
dhtServer.port, CUserId(dhtServer.userId).data(), nullptr))
QByteArray address = dhtServer.address.toLatin1();
QByteArray pk = ToxId{dhtServer.userId}.getPublicKey();
const uint8_t* pkPtr = reinterpret_cast<const uint8_t*>(pk.constData());
if (!tox_bootstrap(tox, address.constData(), dhtServer.port, pkPtr, nullptr))
{
qDebug() << "Error bootstrapping from "+dhtServer.name;
}
if (!tox_add_tcp_relay(tox, dhtServer.address.toLatin1().data(),
dhtServer.port, CUserId(dhtServer.userId).data(), nullptr))
if (!tox_add_tcp_relay(tox, address.constData(), dhtServer.port, pkPtr, nullptr))
{
qDebug() << "Error adding TCP relay from "+dhtServer.name;
}
@ -441,11 +443,11 @@ void Core::bootstrapDht()
}
}
void Core::onFriendRequest(Tox*/* tox*/, const uint8_t* cUserId,
void Core::onFriendRequest(Tox*/* tox*/, const uint8_t* cFriendPk,
const uint8_t* cMessage, size_t cMessageSize, void* core)
{
emit static_cast<Core*>(core)->friendRequestReceived(CUserId::toString(cUserId),
CString::toString(cMessage, cMessageSize));
QString friendPk = ToxId(QByteArray(reinterpret_cast<const char*> (cFriendPk), TOX_PUBLIC_KEY_SIZE)).getPublicKeyString();
emit static_cast<Core*>(core)->friendRequestReceived(friendPk, CString::toString(cMessage, cMessageSize));
}
void Core::onFriendMessage(Tox*/* tox*/, uint32_t friendId, TOX_MESSAGE_TYPE type,
@ -556,7 +558,7 @@ void Core::onReadReceiptCallback(Tox*, uint32_t friendId, uint32_t receipt, void
void Core::acceptFriendRequest(const QString& userId)
{
uint32_t friendId = tox_friend_add_norequest(tox, CUserId(userId).data(), nullptr);
uint32_t friendId = tox_friend_add_norequest(tox, reinterpret_cast<const uint8_t*> (ToxId(userId).getPublicKeyBytes()), nullptr);
if (friendId == std::numeric_limits<uint32_t>::max())
{
emit failedToAddFriend(userId);
@ -589,7 +591,7 @@ void Core::requestFriendship(const QString& friendAddress, const QString& messag
{
CString cMessage(message);
uint32_t friendId = tox_friend_add(tox, CFriendAddress(friendAddress).data(),
uint32_t friendId = tox_friend_add(tox, ToxId(friendAddress).getBytes(),
cMessage.data(), cMessage.size(), nullptr);
if (friendId == std::numeric_limits<uint32_t>::max())
{
@ -856,9 +858,9 @@ void Core::setAvatar(const QByteArray& data)
*/
ToxId Core::getSelfId() const
{
uint8_t friendAddress[TOX_ADDRESS_SIZE] = {0};
tox_self_get_address(tox, friendAddress);
return ToxId(CFriendAddress::toString(friendAddress));
QByteArray friendAddress(TOX_ADDRESS_SIZE, 0x00);
tox_self_get_address(tox, reinterpret_cast<uint8_t*>(friendAddress.data()));
return ToxId(friendAddress);
}
/**
@ -984,12 +986,12 @@ void Core::loadFriends()
// assuming there are not that many friends to fill up the whole stack
uint32_t *ids = new uint32_t[friendCount];
tox_self_get_friend_list(tox, ids);
uint8_t clientId[TOX_PUBLIC_KEY_SIZE];
QByteArray friendPk(TOX_PUBLIC_KEY_SIZE, 0x00);
for (int32_t i = 0; i < static_cast<int32_t>(friendCount); ++i)
{
if (tox_friend_get_public_key(tox, ids[i], clientId, nullptr))
if (tox_friend_get_public_key(tox, ids[i], reinterpret_cast<uint8_t*>(friendPk.data()), nullptr))
{
emit friendAdded(ids[i], CUserId::toString(clientId));
emit friendAdded(ids[i], ToxId(friendPk).getPublicKeyString());
const size_t nameSize = tox_friend_get_name_size(tox, ids[i], nullptr);
if (nameSize && nameSize != SIZE_MAX)
@ -1102,16 +1104,17 @@ QString Core::getGroupPeerName(int groupId, int peerId) const
*/
ToxId Core::getGroupPeerToxId(int groupId, int peerId) const
{
uint8_t rawID[TOX_PUBLIC_KEY_SIZE];
QByteArray friendPk(TOX_PUBLIC_KEY_SIZE, 0x00);
TOX_ERR_CONFERENCE_PEER_QUERY error;
bool success = tox_conference_peer_get_public_key(tox, groupId, peerId, rawID, &error);
bool success = tox_conference_peer_get_public_key(tox, groupId, peerId,
reinterpret_cast<uint8_t*> (friendPk.data()), &error);
if (!parsePeerQueryError(error) || !success)
{
qWarning() << "getGroupPeerToxId: Unknown error";
return ToxId();
}
return ToxId(CUserId::toString(rawID));
return ToxId(friendPk);
}
/**
@ -1445,9 +1448,7 @@ QList<CString> Core::splitMessage(const QString &message, int maxLen)
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);
uint32_t friendId = tox_friend_by_public_key(tox, id.getBytes(), nullptr);
if (friendId == std::numeric_limits<uint32_t>::max())
{
qWarning() << "getPeerName: No such peer";

View File

@ -62,7 +62,7 @@ ToxId::ToxId()
* @brief The copy constructor.
* @param other ToxId to copy
*/
ToxId::ToxId(const ToxId &other)
ToxId::ToxId(const ToxId& other)
: toxId(other.toxId)
{}
@ -74,7 +74,7 @@ ToxId::ToxId(const ToxId &other)
*
* @param id Tox ID string to convert to ToxId object
*/
ToxId::ToxId(const QString &id)
ToxId::ToxId(const QString& id)
{
if (isToxId(id))
{
@ -98,7 +98,27 @@ ToxId::ToxId(const QString &id)
*
* @param id Tox ID string to convert to ToxId object
*/
ToxId::ToxId(const QByteArray &rawId)
ToxId::ToxId(const QByteArray& rawId)
{
checkToxId(rawId);
}
/**
* @brief Create a Tox ID from a uint8_t* and length.
*
* If the given id is not a valid Tox ID, then:
* publicKey == id and noSpam == "" == checkSum.
*
* @param id Tox ID string to convert to ToxId object
*/
ToxId::ToxId(const uint8_t& rawId, int len)
{
QByteArray tmpId(reinterpret_cast<const char *>(rawId), len);
checkToxId(tmpId);
}
void ToxId::checkToxId(const QByteArray& rawId)
{
if(rawId.length() == TOX_SECRET_KEY_SIZE)
{
@ -106,8 +126,8 @@ ToxId::ToxId(const QByteArray &rawId)
}
else if (rawId.length() == TOX_ADDRESS_SIZE
&& isToxId(rawId.toHex().toUpper()))
{
{
toxId = QByteArray(rawId); // construct from full toxid
}
else
@ -131,7 +151,7 @@ bool ToxId::operator==(const ToxId& other) const
* @param other Tox ID to compare.
* @return True if both Tox ID have different public keys, false otherwise.
*/
bool ToxId::operator!=(const ToxId &other) const
bool ToxId::operator!=(const ToxId& other) const
{
return getPublicKey() != other.getPublicKey();
}
@ -165,6 +185,15 @@ bool ToxId::isValidToxId(const QString& id)
return id.length() == TOXID_HEX_CHARS && id.contains(hexRegExp);
}
/**
* @brief Gets the ToxID as bytes, convenience function for toxcore interface.
* @return The ToxID
*/
const uint8_t* ToxId::getBytes() const
{
return reinterpret_cast<const uint8_t*> (toxId.constData());
}
/**
* @brief Gets the Public Key part of the ToxID
* @return Public Key of the ToxID
@ -174,6 +203,15 @@ QByteArray ToxId::getPublicKey() const
return toxId.mid(0, TOX_PUBLIC_KEY_SIZE);
}
/**
* @brief Gets the Public Key part of the ToxID, convenience fuction for toxcore interface.
* @return Public Key of the ToxID
*/
const uint8_t* ToxId::getPublicKeyBytes() const
{
return reinterpret_cast<const uint8_t*> (toxId.mid(0, TOX_PUBLIC_KEY_SIZE).constData());
}
/**
* @brief Returns the Public Key converted to QString.
* @return The Public Key as QString.

View File

@ -30,6 +30,7 @@ public:
ToxId(const ToxId& other);
explicit ToxId(const QString& id);
explicit ToxId(const QByteArray& rawId);
explicit ToxId(const uint8_t& rawId, int len);
bool operator==(const ToxId& other) const;
bool operator!=(const ToxId& other) const;
@ -38,11 +39,16 @@ public:
static bool isToxId(const QString& id);
static bool isValidToxId(const QString &id);
const uint8_t* getBytes() const;
QByteArray getToxId() const;
QByteArray getPublicKey() const;
const uint8_t* getPublicKeyBytes() const;
QString getPublicKeyString() const;
QString getNoSpamString() const;
private:
void checkToxId(const QByteArray& rawId);
private:
QByteArray toxId;
};

View File

@ -29,7 +29,6 @@
#include <tox/tox.h>
#include "nexus.h"
#include "core/core.h"
#include "core/cdata.h"
#include "net/toxme.h"
#include "persistence/settings.h"
#include "widget/gui.h"