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

feat(core): set group title for loaded groups

Replace double static_cast of pointer with reinterpret_cast.
reinterpret_cast in unsafe because the underlying data may be
meaningless in the new type, static cast to void then to a new pointer
type has exactly the same problem. This is our intention here.
This commit is contained in:
Anthony Bilinski 2018-12-02 14:19:49 -08:00
parent cbf2a1801f
commit 8db744a505
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
4 changed files with 46 additions and 13 deletions

View File

@ -44,6 +44,26 @@ const QString Core::TOX_EXT = ".tox";
#define ASSERT_CORE_THREAD assert(QThread::currentThread() == coreThread.get())
namespace {
bool LogConferenceTitleError(TOX_ERR_CONFERENCE_TITLE error)
{
switch(error)
{
case TOX_ERR_CONFERENCE_TITLE_OK:
break;
case TOX_ERR_CONFERENCE_TITLE_CONFERENCE_NOT_FOUND:
qWarning() << "Conference title not found";
break;
case TOX_ERR_CONFERENCE_TITLE_INVALID_LENGTH:
qWarning() << "Invalid conference title length";
break;
case TOX_ERR_CONFERENCE_TITLE_FAIL_SEND:
qWarning() << "Failed to send title packet";
}
return error;
}
} // namespace
Core::Core(QThread* coreThread)
: tox(nullptr)
, av(nullptr)
@ -451,8 +471,7 @@ void Core::onGroupInvite(Tox* tox, uint32_t friendId, Tox_Conference_Type type,
const uint8_t* cookie, size_t length, void* vCore)
{
Core* core = static_cast<Core*>(vCore);
// static_cast is used twice to replace using unsafe reinterpret_cast
const QByteArray data(static_cast<const char*>(static_cast<const void*>(cookie)), length);
const QByteArray data(reinterpret_cast<const char*>(cookie), length);
const GroupInvite inviteInfo(friendId, type, data);
switch (type) {
case TOX_CONFERENCE_TYPE_TEXT:
@ -1025,7 +1044,18 @@ void Core::loadGroups()
tox_conference_get_chatlist(tox.get(), groupIds);
for(size_t i = 0; i < groupCount; ++i) {
emit emptyGroupCreated(static_cast<int>(groupIds[i]));
TOX_ERR_CONFERENCE_TITLE error;
size_t titleSize = tox_conference_get_title_size(tox.get(), groupIds[i], &error);
if (LogConferenceTitleError(error)) {
continue;
}
QByteArray name(titleSize, Qt::Uninitialized);
if (!tox_conference_get_title(tox.get(), groupIds[i], reinterpret_cast<uint8_t*>(name.data()), &error))
if (LogConferenceTitleError(error)) {
continue;
}
emit emptyGroupCreated(static_cast<int>(groupIds[i]), ToxString(name).getQString());
}
delete[] groupIds;
@ -1114,7 +1144,7 @@ QString Core::getGroupPeerName(int groupId, int peerId) const
}
QByteArray name(length, Qt::Uninitialized);
uint8_t* namePtr = static_cast<uint8_t*>(static_cast<void*>(name.data()));
uint8_t* namePtr = reinterpret_cast<uint8_t*>(name.data());
bool success = tox_conference_peer_get_name(tox.get(), groupId, peerId, namePtr, &error);
if (!parsePeerQueryError(error) || !success) {
qWarning() << "getGroupPeerName: Unknown error";
@ -1179,7 +1209,7 @@ QStringList Core::getGroupPeerNames(int groupId) const
}
QByteArray name(length, Qt::Uninitialized);
uint8_t* namePtr = static_cast<uint8_t*>(static_cast<void*>(name.data()));
uint8_t* namePtr = reinterpret_cast<uint8_t*>(name.data());
bool ok = tox_conference_peer_get_name(tox.get(), groupId, i, namePtr, &error);
if (ok && parsePeerQueryError(error)) {
names.append(ToxString(name).getQString());
@ -1267,7 +1297,7 @@ uint32_t Core::joinGroupchat(const GroupInvite& inviteInfo) const
const uint32_t friendId = inviteInfo.getFriendId();
const uint8_t confType = inviteInfo.getType();
const QByteArray invite = inviteInfo.getInvite();
const uint8_t* const cookie = static_cast<const uint8_t*>(static_cast<const void*>(invite.data()));
const uint8_t* const cookie = reinterpret_cast<const uint8_t*>(invite.data());
const size_t cookieLength = invite.length();
switch (confType) {
case TOX_CONFERENCE_TYPE_TEXT: {

View File

@ -190,7 +190,7 @@ signals:
void friendRemoved(uint32_t friendId);
void friendLastSeenChanged(uint32_t friendId, const QDateTime& dateTime);
void emptyGroupCreated(int groupnumber);
void emptyGroupCreated(int groupnumber, const QString& title = QString());
void groupInviteReceived(const GroupInvite& inviteInfo);
void groupMessageReceived(int groupnumber, int peernumber, const QString& message, bool isAction);
void groupNamelistChanged(int groupnumber, int peernumber, uint8_t change);

View File

@ -1907,16 +1907,19 @@ Group* Widget::createGroup(int groupId)
return newgroup;
}
void Widget::onEmptyGroupCreated(int groupId)
void Widget::onEmptyGroupCreated(int groupId, const QString& title)
{
Group* group = createGroup(groupId);
if (!group) {
return;
}
// Only rename group if groups are visible.
if (Widget::getInstance()->groupsVisible()) {
groupWidgets[groupId]->editName();
if (title.isEmpty()) {
// Only rename group if groups are visible.
if (Widget::getInstance()->groupsVisible()) {
groupWidgets[groupId]->editName();
}
} else {
group->setTitle(QString(), title);
}
}

View File

@ -167,7 +167,7 @@ public slots:
void updateFriendActivity(const Friend* frnd);
void onMessageSendResult(uint32_t friendId, const QString& message, int messageId);
void onReceiptRecieved(int friendId, int receipt);
void onEmptyGroupCreated(int groupId);
void onEmptyGroupCreated(int groupId, const QString& title);
void onGroupInviteReceived(const GroupInvite& inviteInfo);
void onGroupInviteAccepted(const GroupInvite& inviteInfo);
void onGroupMessageReceived(int groupnumber, int peernumber, const QString& message, bool isAction);