Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat(core): set group title for loaded groups
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
anthonybilinski committed Dec 14, 2018
1 parent cbf2a18 commit 8db744a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
42 changes: 36 additions & 6 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public slots:
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);
Expand Down
13 changes: 8 additions & 5 deletions src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/widget/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8db744a

Please sign in to comment.