From 5289c99962b6be374e92163313bc620eef2cbd58 Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Sat, 26 Jan 2019 00:40:09 -0800 Subject: [PATCH] feat(core): add send message error handling remove unused sendMessageResult signal --- src/core/core.cpp | 113 ++++++++++++++++++++++++++++-------------- src/core/core.h | 3 +- src/nexus.cpp | 1 - src/widget/widget.cpp | 10 ---- src/widget/widget.h | 1 - 5 files changed, 76 insertions(+), 52 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index fe3d70e8e..4a7377477 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -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(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(error); + return false; + } + } } // namespace Core::Core(QThread* coreThread) @@ -600,24 +656,33 @@ void Core::requestFriendship(const ToxId& friendId, const QString& message) emit saveRequest(); } +int Core::sendMessageWithType(uint32_t friendId, const QString& message, Tox_Message_Type type) +{ + 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); + 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()); - 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); - return receipt; + 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) @@ -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()}; diff --git a/src/core/core.h b/src/core/core.h index 3738e8c3b..ecb84b94c 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -197,8 +197,6 @@ signals: 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); @@ -236,6 +234,7 @@ private: 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(); diff --git a/src/nexus.cpp b/src/nexus.cpp index a94cf3249..1f287b236 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -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); diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index cb15c27a3..3080dbe26 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -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); diff --git a/src/widget/widget.h b/src/widget/widget.h index 993216f8b..78d8ec7ae 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -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);