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

Commit

Permalink
feat(core): add send message error handling
Browse files Browse the repository at this point in the history
remove unused sendMessageResult signal
  • Loading branch information
anthonybilinski committed Jan 27, 2019
1 parent 5b83667 commit 5289c99
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 52 deletions.
113 changes: 75 additions & 38 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,62 @@ namespace {
}
return error;
}

bool parseFriendSendMessageError(Tox_Err_Friend_Send_Message error)
{
switch (error) {
case TOX_ERR_FRIEND_SEND_MESSAGE_OK:
return true;
case TOX_ERR_FRIEND_SEND_MESSAGE_NULL:
qCritical() << "Send friend message passed an unexpected null argument";
return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_FOUND:
qCritical() << "Send friend message could not find friend";
return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_CONNECTED:
qCritical() << "Send friend message: friend is offline";
return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_SENDQ:
qCritical() << "Failed to allocate more message queue";
return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG:
qCritical() << "Attemped to send message that's too long";
return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_EMPTY:
qCritical() << "Attempted to send an empty message";
return false;
default:
qCritical() << "Unknown friend send message error:" << static_cast<int>(error);
return false;
}
}

bool parseConferenceSendMessageError(Tox_Err_Conference_Send_Message error)
{
switch (error) {
case TOX_ERR_CONFERENCE_SEND_MESSAGE_OK:
return true;

case TOX_ERR_CONFERENCE_SEND_MESSAGE_CONFERENCE_NOT_FOUND:
qCritical() << "Conference not found";
return false;

case TOX_ERR_CONFERENCE_SEND_MESSAGE_FAIL_SEND:
qCritical() << "Conference message failed to send";
return false;

case TOX_ERR_CONFERENCE_SEND_MESSAGE_NO_CONNECTION:
qCritical() << "No connection";
return false;

case TOX_ERR_CONFERENCE_SEND_MESSAGE_TOO_LONG:
qCritical() << "Message too long";
return false;
default:
qCritical() << "Unknown Tox_Err_Conference_Send_Message error:" << static_cast<int>(error);
return false;
}
}
} // namespace

Core::Core(QThread* coreThread)
Expand Down Expand Up @@ -600,24 +656,33 @@ void Core::requestFriendship(const ToxId& friendId, const QString& message)
emit saveRequest();
}

int Core::sendMessage(uint32_t friendId, const QString& message)
int Core::sendMessageWithType(uint32_t friendId, const QString& message, Tox_Message_Type type)
{
QMutexLocker ml(coreLoopLock.get());
int size = message.toUtf8().size();
auto maxSize = tox_max_message_length();
if (size > maxSize) {
qCritical() << "Core::sendMessageWithType called with message of size:" << size << "when max is:" << maxSize <<". Ignoring.";
return 0;
}

ToxString cMessage(message);
int receipt = tox_friend_send_message(tox.get(), friendId, TOX_MESSAGE_TYPE_NORMAL,
cMessage.data(), cMessage.size(), nullptr);
emit messageSentResult(friendId, message, receipt);
Tox_Err_Friend_Send_Message error;
int receipt = tox_friend_send_message(tox.get(), friendId, type,
cMessage.data(), cMessage.size(), &error);
parseFriendSendMessageError(error);
return receipt;
}

int Core::sendMessage(uint32_t friendId, const QString& message)
{
QMutexLocker ml(coreLoopLock.get());
return sendMessageWithType(friendId, message, TOX_MESSAGE_TYPE_NORMAL);
}

int Core::sendAction(uint32_t friendId, const QString& action)
{
QMutexLocker ml(coreLoopLock.get());
ToxString cMessage(action);
int receipt = tox_friend_send_message(tox.get(), friendId, TOX_MESSAGE_TYPE_ACTION,
cMessage.data(), cMessage.size(), nullptr);
emit messageSentResult(friendId, action, receipt);
return receipt;
return sendMessageWithType(friendId, action, TOX_MESSAGE_TYPE_ACTION);
}

void Core::sendTyping(uint32_t friendId, bool typing)
Expand All @@ -629,34 +694,6 @@ void Core::sendTyping(uint32_t friendId, bool typing)
}
}

bool parseConferenceSendMessageError(Tox_Err_Conference_Send_Message error)
{
switch (error) {
case TOX_ERR_CONFERENCE_SEND_MESSAGE_OK:
return true;

case TOX_ERR_CONFERENCE_SEND_MESSAGE_CONFERENCE_NOT_FOUND:
qCritical() << "Conference not found";
return false;

case TOX_ERR_CONFERENCE_SEND_MESSAGE_FAIL_SEND:
qCritical() << "Conference message failed to send";
return false;

case TOX_ERR_CONFERENCE_SEND_MESSAGE_NO_CONNECTION:
qCritical() << "No connection";
return false;

case TOX_ERR_CONFERENCE_SEND_MESSAGE_TOO_LONG:
qCritical() << "Message too long";
return false;

default:
qCritical() << "Unknown Tox_Err_Conference_Send_Message error";
return false;
}
}

void Core::sendGroupMessageWithType(int groupId, const QString& message, Tox_Message_Type type)
{
QMutexLocker ml{coreLoopLock.get()};
Expand Down
3 changes: 1 addition & 2 deletions src/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ public slots:
void groupPeerNameChanged(int groupnumber, int peernumber, const QString& newName);
void groupTitleChanged(int groupnumber, const QString& author, const QString& title);
void groupPeerAudioPlaying(int groupnumber, int peernumber);

void messageSentResult(uint32_t friendId, const QString& message, int messageId);
void groupSentFailed(int groupId);
void actionSentResult(uint32_t friendId, const QString& action, int success);

Expand Down Expand Up @@ -236,6 +234,7 @@ public slots:
static void onReadReceiptCallback(Tox* tox, uint32_t friendId, uint32_t receipt, void* core);

void sendGroupMessageWithType(int groupId, const QString& message, Tox_Message_Type type);
int sendMessageWithType(uint32_t friendId, const QString& message, Tox_Message_Type type);
bool parsePeerQueryError(Tox_Err_Conference_Peer_Query error) const;
bool parseConferenceJoinError(Tox_Err_Conference_Join error) const;
bool checkConnection();
Expand Down
1 change: 0 additions & 1 deletion src/nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ void Nexus::showMainGUI()
connect(core, &Core::groupPeerAudioPlaying, widget, &Widget::onGroupPeerAudioPlaying);
connect(core, &Core::emptyGroupCreated, widget, &Widget::onEmptyGroupCreated);
connect(core, &Core::friendTypingChanged, widget, &Widget::onFriendTypingChanged);
connect(core, &Core::messageSentResult, widget, &Widget::onMessageSendResult);
connect(core, &Core::groupSentFailed, widget, &Widget::onGroupSendFailed);
connect(core, &Core::usernameSet, widget, &Widget::refreshPeerListsLocal);

Expand Down
10 changes: 0 additions & 10 deletions src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2075,16 +2075,6 @@ void Widget::setStatusBusy()
Nexus::getCore()->setStatus(Status::Busy);
}

void Widget::onMessageSendResult(uint32_t friendId, const QString& message, int messageId)
{
Q_UNUSED(message)
Q_UNUSED(messageId)
Friend* f = FriendList::findFriend(friendId);
if (!f) {
return;
}
}

void Widget::onGroupSendFailed(int groupId)
{
Group* g = GroupList::findGroup(groupId);
Expand Down
1 change: 0 additions & 1 deletion src/widget/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ public slots:
void onFriendMessageReceived(int friendId, const QString& message, bool isAction);
void onFriendRequestReceived(const ToxPk& friendPk, const QString& message);
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, const QString& title);
void onGroupInviteReceived(const GroupInvite& inviteInfo);
Expand Down

0 comments on commit 5289c99

Please sign in to comment.