mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(core): use automatic memory for toxcore string buffers
Avoid reinterpret_casts in Core, offload reinterpretation to model classes
This commit is contained in:
parent
f77396a09e
commit
11ef88a8ac
|
@ -1156,11 +1156,12 @@ QString Core::getUsername() const
|
||||||
}
|
}
|
||||||
|
|
||||||
int size = tox_self_get_name_size(tox.get());
|
int size = tox_self_get_name_size(tox.get());
|
||||||
uint8_t* name = new uint8_t[size];
|
if (!size) {
|
||||||
tox_self_get_name(tox.get(), name);
|
return {};
|
||||||
sname = ToxString(name, size).getQString();
|
}
|
||||||
delete[] name;
|
std::vector<uint8_t> nameBuf(size);
|
||||||
return sname;
|
tox_self_get_name(tox.get(), nameBuf.data());
|
||||||
|
return ToxString(nameBuf.data(), size).getQString();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Core::setUsername(const QString& username)
|
void Core::setUsername(const QString& username)
|
||||||
|
@ -1237,14 +1238,12 @@ QString Core::getStatusMessage() const
|
||||||
assert(tox != nullptr);
|
assert(tox != nullptr);
|
||||||
|
|
||||||
size_t size = tox_self_get_status_message_size(tox.get());
|
size_t size = tox_self_get_status_message_size(tox.get());
|
||||||
if (size == 0) {
|
if (!size) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
uint8_t* name = new uint8_t[size];
|
std::vector<uint8_t> nameBuf(size);
|
||||||
tox_self_get_status_message(tox.get(), name);
|
tox_self_get_status_message(tox.get(), nameBuf.data());
|
||||||
QString sname = ToxString(name, size).getQString();
|
return ToxString(nameBuf.data(), size).getQString();
|
||||||
delete[] name;
|
|
||||||
return sname;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1328,8 +1327,8 @@ void Core::loadFriends()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t* ids = new uint32_t[friendCount];
|
std::vector<uint32_t> ids(friendCount);
|
||||||
tox_self_get_friend_list(tox.get(), ids);
|
tox_self_get_friend_list(tox.get(), ids.data());
|
||||||
uint8_t friendPk[TOX_PUBLIC_KEY_SIZE] = {0x00};
|
uint8_t friendPk[TOX_PUBLIC_KEY_SIZE] = {0x00};
|
||||||
for (size_t i = 0; i < friendCount; ++i) {
|
for (size_t i = 0; i < friendCount; ++i) {
|
||||||
Tox_Err_Friend_Get_Public_Key keyError;
|
Tox_Err_Friend_Get_Public_Key keyError;
|
||||||
|
@ -1341,7 +1340,7 @@ void Core::loadFriends()
|
||||||
emit friendUsernameChanged(ids[i], getFriendUsername(ids[i]));
|
emit friendUsernameChanged(ids[i], getFriendUsername(ids[i]));
|
||||||
Tox_Err_Friend_Query queryError;
|
Tox_Err_Friend_Query queryError;
|
||||||
size_t statusMessageSize = tox_friend_get_status_message_size(tox.get(), ids[i], &queryError);
|
size_t statusMessageSize = tox_friend_get_status_message_size(tox.get(), ids[i], &queryError);
|
||||||
if (PARSE_ERR(ToxErrFriendQuery, queryError)) {
|
if (PARSE_ERR(ToxErrFriendQuery, queryError) && statusMessageSize) {
|
||||||
std::vector<uint8_t> messageData(statusMessageSize);
|
std::vector<uint8_t> messageData(statusMessageSize);
|
||||||
tox_friend_get_status_message(tox.get(), ids[i], messageData.data(), &queryError);
|
tox_friend_get_status_message(tox.get(), ids[i], messageData.data(), &queryError);
|
||||||
QString friendStatusMessage = ToxString(messageData.data(), statusMessageSize).getQString();
|
QString friendStatusMessage = ToxString(messageData.data(), statusMessageSize).getQString();
|
||||||
|
@ -1349,7 +1348,6 @@ void Core::loadFriends()
|
||||||
}
|
}
|
||||||
checkLastOnline(ids[i]);
|
checkLastOnline(ids[i]);
|
||||||
}
|
}
|
||||||
delete[] ids;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Core::loadGroups()
|
void Core::loadGroups()
|
||||||
|
@ -1361,8 +1359,8 @@ void Core::loadGroups()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto groupNumbers = new uint32_t[groupCount];
|
std::vector<uint32_t> groupNumbers(groupCount);
|
||||||
tox_conference_get_chatlist(tox.get(), groupNumbers);
|
tox_conference_get_chatlist(tox.get(), groupNumbers.data());
|
||||||
|
|
||||||
for (size_t i = 0; i < groupCount; ++i) {
|
for (size_t i = 0; i < groupCount; ++i) {
|
||||||
Tox_Err_Conference_Title error;
|
Tox_Err_Conference_Title error;
|
||||||
|
@ -1371,12 +1369,11 @@ void Core::loadGroups()
|
||||||
size_t titleSize = tox_conference_get_title_size(tox.get(), groupNumber, &error);
|
size_t titleSize = tox_conference_get_title_size(tox.get(), groupNumber, &error);
|
||||||
const GroupId persistentId = getGroupPersistentId(groupNumber);
|
const GroupId persistentId = getGroupPersistentId(groupNumber);
|
||||||
const QString defaultName = tr("Groupchat %1").arg(persistentId.toString().left(8));
|
const QString defaultName = tr("Groupchat %1").arg(persistentId.toString().left(8));
|
||||||
if (PARSE_ERR(ToxErrConferenceTitle, error)) {
|
if (PARSE_ERR(ToxErrConferenceTitle, error) || !titleSize) {
|
||||||
QByteArray nameByteArray = QByteArray(static_cast<int>(titleSize), Qt::Uninitialized);
|
std::vector<uint8_t> nameBuf(titleSize);
|
||||||
tox_conference_get_title(tox.get(), groupNumber,
|
tox_conference_get_title(tox.get(), groupNumber, nameBuf.data(), &error);
|
||||||
reinterpret_cast<uint8_t*>(nameByteArray.data()), &error);
|
|
||||||
if (PARSE_ERR(ToxErrConferenceTitle, error)) {
|
if (PARSE_ERR(ToxErrConferenceTitle, error)) {
|
||||||
name = ToxString(nameByteArray).getQString();
|
name = ToxString(nameBuf.data(), titleSize).getQString();
|
||||||
} else {
|
} else {
|
||||||
name = defaultName;
|
name = defaultName;
|
||||||
}
|
}
|
||||||
|
@ -1390,8 +1387,6 @@ void Core::loadGroups()
|
||||||
}
|
}
|
||||||
emit emptyGroupCreated(groupNumber, persistentId, name);
|
emit emptyGroupCreated(groupNumber, persistentId, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete[] groupNumbers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Core::checkLastOnline(uint32_t friendId)
|
void Core::checkLastOnline(uint32_t friendId)
|
||||||
|
@ -1422,11 +1417,10 @@ GroupId Core::getGroupPersistentId(uint32_t groupNumber) const
|
||||||
{
|
{
|
||||||
QMutexLocker ml{&coreLoopLock};
|
QMutexLocker ml{&coreLoopLock};
|
||||||
|
|
||||||
size_t conferenceIdSize = TOX_CONFERENCE_UID_SIZE;
|
std::vector<uint8_t> idBuff(TOX_CONFERENCE_UID_SIZE);
|
||||||
QByteArray groupPersistentId(conferenceIdSize, Qt::Uninitialized);
|
|
||||||
if (tox_conference_get_id(tox.get(), groupNumber,
|
if (tox_conference_get_id(tox.get(), groupNumber,
|
||||||
reinterpret_cast<uint8_t*>(groupPersistentId.data()))) {
|
idBuff.data())) {
|
||||||
return GroupId{groupPersistentId};
|
return GroupId{idBuff.data()};
|
||||||
} else {
|
} else {
|
||||||
qCritical() << "Failed to get conference ID of group" << groupNumber;
|
qCritical() << "Failed to get conference ID of group" << groupNumber;
|
||||||
return {};
|
return {};
|
||||||
|
@ -1459,19 +1453,17 @@ QString Core::getGroupPeerName(int groupId, int peerId) const
|
||||||
|
|
||||||
Tox_Err_Conference_Peer_Query error;
|
Tox_Err_Conference_Peer_Query error;
|
||||||
size_t length = tox_conference_peer_get_name_size(tox.get(), groupId, peerId, &error);
|
size_t length = tox_conference_peer_get_name_size(tox.get(), groupId, peerId, &error);
|
||||||
|
if (!PARSE_ERR(ToxErrConferencePeerQuery, error) || !length) {
|
||||||
|
return QString{};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> nameBuf(length);
|
||||||
|
tox_conference_peer_get_name(tox.get(), groupId, peerId, nameBuf.data(), &error);
|
||||||
if (!PARSE_ERR(ToxErrConferencePeerQuery, error)) {
|
if (!PARSE_ERR(ToxErrConferencePeerQuery, error)) {
|
||||||
return QString{};
|
return QString{};
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray name(length, Qt::Uninitialized);
|
return ToxString(nameBuf.data(), length).getQString();
|
||||||
uint8_t* namePtr = reinterpret_cast<uint8_t*>(name.data());
|
|
||||||
bool success = tox_conference_peer_get_name(tox.get(), groupId, peerId, namePtr, &error);
|
|
||||||
if (!PARSE_ERR(ToxErrConferencePeerQuery, error)) {
|
|
||||||
return QString{};
|
|
||||||
}
|
|
||||||
assert(success);
|
|
||||||
|
|
||||||
return ToxString(name).getQString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1483,11 +1475,10 @@ ToxPk Core::getGroupPeerPk(int groupId, int peerId) const
|
||||||
|
|
||||||
uint8_t 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.get(), groupId, peerId, friendPk, &error);
|
tox_conference_peer_get_public_key(tox.get(), groupId, peerId, friendPk, &error);
|
||||||
if (!PARSE_ERR(ToxErrConferencePeerQuery, error)) {
|
if (!PARSE_ERR(ToxErrConferencePeerQuery, error)) {
|
||||||
return ToxPk{};
|
return ToxPk{};
|
||||||
}
|
}
|
||||||
assert(success);
|
|
||||||
|
|
||||||
return ToxPk(friendPk);
|
return ToxPk(friendPk);
|
||||||
}
|
}
|
||||||
|
@ -1511,15 +1502,14 @@ QStringList Core::getGroupPeerNames(int groupId) const
|
||||||
for (uint32_t i = 0; i < nPeers; ++i) {
|
for (uint32_t i = 0; i < nPeers; ++i) {
|
||||||
Tox_Err_Conference_Peer_Query error;
|
Tox_Err_Conference_Peer_Query error;
|
||||||
size_t length = tox_conference_peer_get_name_size(tox.get(), groupId, i, &error);
|
size_t length = tox_conference_peer_get_name_size(tox.get(), groupId, i, &error);
|
||||||
if (!PARSE_ERR(ToxErrConferencePeerQuery, error)) {
|
if (!PARSE_ERR(ToxErrConferencePeerQuery, error) || !length) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray name(length, Qt::Uninitialized);
|
std::vector<uint8_t> nameBuf(length);
|
||||||
uint8_t* namePtr = reinterpret_cast<uint8_t*>(name.data());
|
tox_conference_peer_get_name(tox.get(), groupId, i, nameBuf.data(), &error);
|
||||||
bool ok = tox_conference_peer_get_name(tox.get(), groupId, i, namePtr, &error);
|
if (PARSE_ERR(ToxErrConferencePeerQuery, error)) {
|
||||||
if (ok && PARSE_ERR(ToxErrConferencePeerQuery, error)) {
|
names.append(ToxString(nameBuf.data(), length).getQString());
|
||||||
names.append(ToxString(name).getQString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1678,22 +1668,17 @@ QString Core::getFriendUsername(uint32_t friendnumber) const
|
||||||
QMutexLocker ml{&coreLoopLock};
|
QMutexLocker ml{&coreLoopLock};
|
||||||
|
|
||||||
Tox_Err_Friend_Query error;
|
Tox_Err_Friend_Query error;
|
||||||
size_t namesize = tox_friend_get_name_size(tox.get(), friendnumber, &error);
|
size_t nameSize = tox_friend_get_name_size(tox.get(), friendnumber, &error);
|
||||||
if (!PARSE_ERR(ToxErrFriendQuery, error)) {
|
if (!PARSE_ERR(ToxErrFriendQuery, error) || !nameSize) {
|
||||||
qWarning() << "getFriendUsername: Failed to get name size for friend " << friendnumber;
|
|
||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* name = new uint8_t[namesize];
|
std::vector<uint8_t> nameBuf(nameSize);
|
||||||
tox_friend_get_name(tox.get(), friendnumber, name, &error);
|
tox_friend_get_name(tox.get(), friendnumber, nameBuf.data(), &error);
|
||||||
if (!PARSE_ERR(ToxErrFriendQuery, error)) {
|
if (!PARSE_ERR(ToxErrFriendQuery, error)) {
|
||||||
qWarning() << "getFriendUsername: Failed to get name of friend " << friendnumber;
|
|
||||||
delete[] name;
|
|
||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
ToxString sname(name, namesize);
|
return ToxString(nameBuf.data(), nameSize).getQString();
|
||||||
delete[] name;
|
|
||||||
return sname.getQString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList Core::splitMessage(const QString& message)
|
QStringList Core::splitMessage(const QString& message)
|
||||||
|
@ -1744,31 +1729,27 @@ QString Core::getPeerName(const ToxPk& id) const
|
||||||
{
|
{
|
||||||
QMutexLocker ml{&coreLoopLock};
|
QMutexLocker ml{&coreLoopLock};
|
||||||
|
|
||||||
QString name;
|
|
||||||
Tox_Err_Friend_By_Public_Key keyError;
|
Tox_Err_Friend_By_Public_Key keyError;
|
||||||
uint32_t friendId = tox_friend_by_public_key(tox.get(), id.getData(), &keyError);
|
uint32_t friendId = tox_friend_by_public_key(tox.get(), id.getData(), &keyError);
|
||||||
if (!PARSE_ERR(ToxErrFriendByPublicKey, keyError)) {
|
if (!PARSE_ERR(ToxErrFriendByPublicKey, keyError)) {
|
||||||
qWarning() << "getPeerName: No such peer";
|
qWarning() << "getPeerName: No such peer";
|
||||||
return name;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Tox_Err_Friend_Query queryError;
|
Tox_Err_Friend_Query queryError;
|
||||||
const size_t nameSize = tox_friend_get_name_size(tox.get(), friendId, &queryError);
|
const size_t nameSize = tox_friend_get_name_size(tox.get(), friendId, &queryError);
|
||||||
if (!PARSE_ERR(ToxErrFriendQuery, queryError)) {
|
if (!PARSE_ERR(ToxErrFriendQuery, queryError) || !nameSize) {
|
||||||
return name;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* cname = new uint8_t[nameSize < tox_max_name_length() ? tox_max_name_length() : nameSize];
|
std::vector<uint8_t> nameBuf(nameSize);
|
||||||
tox_friend_get_name(tox.get(), friendId, cname, &queryError);
|
tox_friend_get_name(tox.get(), friendId, nameBuf.data(), &queryError);
|
||||||
if (!PARSE_ERR(ToxErrFriendQuery, queryError)) {
|
if (!PARSE_ERR(ToxErrFriendQuery, queryError)) {
|
||||||
qWarning() << "getPeerName: Can't get name of friend " + QString().setNum(friendId);
|
qWarning() << "getPeerName: Can't get name of friend " + QString().setNum(friendId);
|
||||||
delete[] cname;
|
return {};
|
||||||
return name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
name = ToxString(cname, nameSize).getQString();
|
return ToxString(nameBuf.data(), nameSize).getQString();
|
||||||
delete[] cname;
|
|
||||||
return name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user