mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(core): Added error handling
This commit is contained in:
parent
98fa64f841
commit
3c304c50bc
|
@ -653,10 +653,30 @@ void Core::sendGroupMessageWithType(int groupId, const QString &message, TOX_MES
|
|||
bool success = tox_conference_send_message(tox, groupId, type,
|
||||
cMsg.data(), cMsg.size(), &error);
|
||||
|
||||
if (!success)
|
||||
if (success && error == TOX_ERR_CONFERENCE_SEND_MESSAGE_OK)
|
||||
{
|
||||
emit groupSentResult(groupId, message, -1);
|
||||
return;
|
||||
}
|
||||
|
||||
qCritical() << "Fail of tox_conference_send_message";
|
||||
switch (error)
|
||||
{
|
||||
case TOX_ERR_CONFERENCE_SEND_MESSAGE_CONFERENCE_NOT_FOUND:
|
||||
qCritical() << "Conference not found";
|
||||
return;
|
||||
case TOX_ERR_CONFERENCE_SEND_MESSAGE_FAIL_SEND:
|
||||
qCritical() << "Fail send";
|
||||
return;
|
||||
case TOX_ERR_CONFERENCE_SEND_MESSAGE_NO_CONNECTION:
|
||||
qCritical() << "No connection";
|
||||
return;
|
||||
case TOX_ERR_CONFERENCE_SEND_MESSAGE_TOO_LONG:
|
||||
qCritical() << "Meesage too long";
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
emit groupSentResult(groupId, message, -1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -674,10 +694,30 @@ void Core::changeGroupTitle(int groupId, const QString& title)
|
|||
{
|
||||
CString cTitle(title);
|
||||
TOX_ERR_CONFERENCE_TITLE error;
|
||||
bool ret = tox_conference_set_title(tox, groupId, cTitle.data(),
|
||||
bool success = tox_conference_set_title(tox, groupId, cTitle.data(),
|
||||
cTitle.size(), &error);
|
||||
if (ret)
|
||||
|
||||
if (success && error == TOX_ERR_CONFERENCE_TITLE_OK)
|
||||
{
|
||||
emit groupTitleChanged(groupId, getUsername(), title);
|
||||
return;
|
||||
}
|
||||
|
||||
qCritical() << "Fail of tox_conference_set_title";
|
||||
switch (error)
|
||||
{
|
||||
case TOX_ERR_CONFERENCE_TITLE_CONFERENCE_NOT_FOUND:
|
||||
qCritical() << "Conference not found";
|
||||
break;
|
||||
case TOX_ERR_CONFERENCE_TITLE_FAIL_SEND:
|
||||
qCritical() << "Fail send";
|
||||
break;
|
||||
case TOX_ERR_CONFERENCE_TITLE_INVALID_LENGTH:
|
||||
qCritical() << "Invalid length";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::sendFile(uint32_t friendId, QString filename, QString filePath, long long filesize)
|
||||
|
@ -741,10 +781,23 @@ void Core::removeGroup(int groupId, bool fake)
|
|||
return;
|
||||
|
||||
TOX_ERR_CONFERENCE_DELETE error;
|
||||
bool ret = tox_conference_delete(tox, groupId, &error);
|
||||
bool success = tox_conference_delete(tox, groupId, &error);
|
||||
|
||||
if (ret)
|
||||
if (success && error == TOX_ERR_CONFERENCE_DELETE_OK)
|
||||
{
|
||||
av->leaveGroupCall(groupId);
|
||||
return;
|
||||
}
|
||||
|
||||
qCritical() << "Fail of tox_conference_delete";
|
||||
switch (error)
|
||||
{
|
||||
case TOX_ERR_CONFERENCE_DELETE_CONFERENCE_NOT_FOUND:
|
||||
qCritical() << "Conference not found";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -981,13 +1034,43 @@ QVector<uint32_t> Core::getFriendList() const
|
|||
return friends;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print in console text of error.
|
||||
* @param error Error to handle.
|
||||
* @return True if no error, false otherwise.
|
||||
*/
|
||||
bool Core::parsePeerQueryError(TOX_ERR_CONFERENCE_PEER_QUERY error) const
|
||||
{
|
||||
switch (error)
|
||||
{
|
||||
case TOX_ERR_CONFERENCE_PEER_QUERY_OK:
|
||||
return true;
|
||||
case TOX_ERR_CONFERENCE_PEER_QUERY_CONFERENCE_NOT_FOUND:
|
||||
qCritical() << "Conference not found";
|
||||
return false;
|
||||
case TOX_ERR_CONFERENCE_PEER_QUERY_NO_CONNECTION:
|
||||
qCritical() << "No connection";
|
||||
return false;
|
||||
case TOX_ERR_CONFERENCE_PEER_QUERY_PEER_NOT_FOUND:
|
||||
qCritical() << "Peer not found";
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the number of peers in the group chat on success, or -1 on failure
|
||||
*/
|
||||
int Core::getGroupNumberPeers(int groupId) const
|
||||
uint32_t Core::getGroupNumberPeers(int groupId) const
|
||||
{
|
||||
TOX_ERR_CONFERENCE_PEER_QUERY error;
|
||||
return tox_conference_peer_count(tox, groupId, &error);
|
||||
uint32_t count = tox_conference_peer_count(tox, groupId, &error);
|
||||
|
||||
if (!parsePeerQueryError(error))
|
||||
return std::numeric_limits<uint32_t>::max();
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -998,14 +1081,11 @@ QString Core::getGroupPeerName(int groupId, int peerId) const
|
|||
uint8_t nameArray[TOX_MAX_NAME_LENGTH];
|
||||
TOX_ERR_CONFERENCE_PEER_QUERY error;
|
||||
size_t length = tox_conference_peer_get_name_size(tox, groupId, peerId, &error);
|
||||
if (error != TOX_ERR_CONFERENCE_PEER_QUERY_OK)
|
||||
{
|
||||
qWarning() << "getGroupPeerName: Unknown error";
|
||||
if (!parsePeerQueryError(error))
|
||||
return QString();
|
||||
}
|
||||
|
||||
bool ret = tox_conference_peer_get_name(tox, groupId, peerId, nameArray, &error);
|
||||
if (!ret)
|
||||
bool success = tox_conference_peer_get_name(tox, groupId, peerId, nameArray, &error);
|
||||
if (!parsePeerQueryError(error) || !success)
|
||||
{
|
||||
qWarning() << "getGroupPeerName: Unknown error";
|
||||
return QString();
|
||||
|
@ -1022,7 +1102,7 @@ ToxId Core::getGroupPeerToxId(int groupId, int peerId) const
|
|||
uint8_t rawID[TOX_PUBLIC_KEY_SIZE];
|
||||
TOX_ERR_CONFERENCE_PEER_QUERY error;
|
||||
bool success = tox_conference_peer_get_public_key(tox, groupId, peerId, rawID, &error);
|
||||
if (!success)
|
||||
if (!parsePeerQueryError(error) || !success)
|
||||
{
|
||||
qWarning() << "getGroupPeerToxId: Unknown error";
|
||||
return ToxId();
|
||||
|
@ -1036,53 +1116,96 @@ ToxId Core::getGroupPeerToxId(int groupId, int peerId) const
|
|||
*/
|
||||
QList<QString> Core::getGroupPeerNames(int groupId) const
|
||||
{
|
||||
QList<QString> names;
|
||||
if (!tox)
|
||||
{
|
||||
qWarning() << "Can't get group peer names, tox is null";
|
||||
return names;
|
||||
return QList<QString>();
|
||||
}
|
||||
|
||||
int result = getGroupNumberPeers(groupId);
|
||||
if (result < 0)
|
||||
{
|
||||
qWarning() << "getGroupPeerNames: Unable to get number of peers";
|
||||
return names;
|
||||
return QList<QString>();
|
||||
}
|
||||
uint16_t nPeers = static_cast<uint16_t>(result);
|
||||
|
||||
uint16_t nPeers = static_cast<uint16_t>(result);
|
||||
std::unique_ptr<uint8_t[][TOX_MAX_NAME_LENGTH]> namesArray{new uint8_t[nPeers][TOX_MAX_NAME_LENGTH]};
|
||||
std::unique_ptr<uint16_t[]> lengths{new uint16_t[nPeers]};
|
||||
TOX_ERR_CONFERENCE_PEER_QUERY error;
|
||||
|
||||
uint32_t count = tox_conference_peer_count(tox, groupId, &error);
|
||||
if (!parsePeerQueryError(error))
|
||||
return QList<QString>();
|
||||
|
||||
if (count != nPeers)
|
||||
{
|
||||
qWarning() << "getGroupPeerNames: Unexpected peer count";
|
||||
return names;
|
||||
return QList<QString>();
|
||||
}
|
||||
|
||||
QList<QString> names;
|
||||
for (uint16_t i = 0; i < nPeers; ++i)
|
||||
{
|
||||
lengths[i] = tox_conference_peer_get_name_size(tox, groupId, i, &error);
|
||||
bool ok = tox_conference_peer_get_name(tox, groupId, i, namesArray[i], &error);
|
||||
if (ok)
|
||||
if (parsePeerQueryError(error) && ok)
|
||||
{
|
||||
names.push_back(CString::toString(namesArray[i], lengths[i]));
|
||||
}
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print in console text of error.
|
||||
* @param error Error to handle.
|
||||
* @return True if no error, false otherwise.
|
||||
*/
|
||||
bool Core::parseConferenceJoinError(TOX_ERR_CONFERENCE_JOIN error) const
|
||||
{
|
||||
switch (error)
|
||||
{
|
||||
case TOX_ERR_CONFERENCE_JOIN_OK:
|
||||
return true;
|
||||
case TOX_ERR_CONFERENCE_JOIN_DUPLICATE:
|
||||
qCritical() << "Conference duplicate";
|
||||
return false;
|
||||
case TOX_ERR_CONFERENCE_JOIN_FAIL_SEND:
|
||||
qCritical() << "Fail send";
|
||||
return false;
|
||||
case TOX_ERR_CONFERENCE_JOIN_FRIEND_NOT_FOUND:
|
||||
qCritical() << "Friend not found";
|
||||
return false;
|
||||
case TOX_ERR_CONFERENCE_JOIN_INIT_FAIL:
|
||||
qCritical() << "Init fail";
|
||||
return false;
|
||||
case TOX_ERR_CONFERENCE_JOIN_INVALID_LENGTH:
|
||||
qCritical() << "Invalid length";
|
||||
return false;
|
||||
case TOX_ERR_CONFERENCE_JOIN_WRONG_TYPE:
|
||||
qCritical() << "Wrong conference type";
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Accept a groupchat invite
|
||||
*/
|
||||
int Core::joinGroupchat(int32_t friendnumber, uint8_t type, const uint8_t* friend_group_public_key,uint16_t length) const
|
||||
uint32_t Core::joinGroupchat(int32_t friendnumber, uint8_t type, const uint8_t* friend_group_public_key,uint16_t length) const
|
||||
{
|
||||
if (type == TOX_CONFERENCE_TYPE_TEXT)
|
||||
{
|
||||
qDebug() << QString("Trying to join text groupchat invite sent by friend %1").arg(friendnumber);
|
||||
TOX_ERR_CONFERENCE_JOIN error;
|
||||
return tox_conference_join(tox, friendnumber, friend_group_public_key, length, &error);
|
||||
uint32_t groupId = tox_conference_join(tox, friendnumber, friend_group_public_key, length, &error);
|
||||
if (parseConferenceJoinError(error))
|
||||
return groupId;
|
||||
else
|
||||
return std::numeric_limits<uint32_t>::max();
|
||||
}
|
||||
else if (type == TOX_CONFERENCE_TYPE_AV)
|
||||
{
|
||||
|
@ -1094,7 +1217,7 @@ int Core::joinGroupchat(int32_t friendnumber, uint8_t type, const uint8_t* frien
|
|||
else
|
||||
{
|
||||
qWarning() << "joinGroupchat: Unknown groupchat type "<<type;
|
||||
return -1;
|
||||
return std::numeric_limits<uint32_t>::max();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1105,12 +1228,37 @@ void Core::quitGroupChat(int groupId) const
|
|||
{
|
||||
TOX_ERR_CONFERENCE_DELETE error;
|
||||
tox_conference_delete(tox, groupId, &error);
|
||||
|
||||
switch (error)
|
||||
{
|
||||
case TOX_ERR_CONFERENCE_DELETE_OK:
|
||||
return;
|
||||
case TOX_ERR_CONFERENCE_DELETE_CONFERENCE_NOT_FOUND:
|
||||
qCritical() << "Conferenct not found";
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Core::groupInviteFriend(uint32_t friendId, int groupId)
|
||||
{
|
||||
TOX_ERR_CONFERENCE_INVITE error;
|
||||
tox_conference_invite(tox, friendId, groupId, &error);
|
||||
|
||||
switch (error)
|
||||
{
|
||||
case TOX_ERR_CONFERENCE_INVITE_OK:
|
||||
break;
|
||||
case TOX_ERR_CONFERENCE_INVITE_CONFERENCE_NOT_FOUND:
|
||||
qCritical() << "Conference not found";
|
||||
break;
|
||||
case TOX_ERR_CONFERENCE_INVITE_FAIL_SEND:
|
||||
qCritical() << "Fail send";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int Core::createGroup(uint8_t type)
|
||||
|
@ -1118,16 +1266,25 @@ int Core::createGroup(uint8_t type)
|
|||
if (type == TOX_CONFERENCE_TYPE_TEXT)
|
||||
{
|
||||
TOX_ERR_CONFERENCE_NEW error;
|
||||
uint32_t group = tox_conference_new(tox, &error);
|
||||
emit emptyGroupCreated(group);
|
||||
return group;
|
||||
uint32_t groupId = tox_conference_new(tox, &error);
|
||||
|
||||
switch (error)
|
||||
{
|
||||
case TOX_ERR_CONFERENCE_NEW_OK:
|
||||
emit emptyGroupCreated(groupId);
|
||||
return groupId;
|
||||
case TOX_ERR_CONFERENCE_NEW_INIT:
|
||||
qCritical() << "The conference instance failed to initialize";
|
||||
return std::numeric_limits<uint32_t>::max();
|
||||
default:
|
||||
return std::numeric_limits<uint32_t>::max();
|
||||
}
|
||||
}
|
||||
else if (type == TOX_CONFERENCE_TYPE_AV)
|
||||
{
|
||||
int group = toxav_add_av_groupchat(tox, CoreAV::groupCallCallback,
|
||||
this);
|
||||
emit emptyGroupCreated(group);
|
||||
return group;
|
||||
uint32_t groupId = toxav_add_av_groupchat(tox, CoreAV::groupCallCallback, this);
|
||||
emit emptyGroupCreated(groupId);
|
||||
return groupId;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1141,7 +1298,8 @@ int Core::createGroup(uint8_t type)
|
|||
*/
|
||||
bool Core::isFriendOnline(uint32_t friendId) const
|
||||
{
|
||||
return tox_friend_get_connection_status(tox, friendId, nullptr) != TOX_CONNECTION_NONE;
|
||||
TOX_CONNECTION connetion = tox_friend_get_connection_status(tox, friendId, nullptr);
|
||||
return connetion != TOX_CONNECTION_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1151,9 +1309,7 @@ bool Core::hasFriendWithAddress(const QString &addr) const
|
|||
{
|
||||
// Valid length check
|
||||
if (addr.length() != (TOX_ADDRESS_SIZE * 2))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
QString pubkey = addr.left(TOX_PUBLIC_KEY_SIZE * 2);
|
||||
return hasFriendWithPublicKey(pubkey);
|
||||
|
|
|
@ -61,7 +61,7 @@ public:
|
|||
QString getPeerName(const ToxId& id) const;
|
||||
|
||||
QVector<uint32_t> getFriendList() const;
|
||||
int getGroupNumberPeers(int groupId) const;
|
||||
uint32_t getGroupNumberPeers(int groupId) const;
|
||||
QString getGroupPeerName(int groupId, int peerId) const;
|
||||
ToxId getGroupPeerToxId(int groupId, int peerId) const;
|
||||
QList<QString> getGroupPeerNames(int groupId) const;
|
||||
|
@ -72,7 +72,7 @@ public:
|
|||
bool isFriendOnline(uint32_t friendId) const;
|
||||
bool hasFriendWithAddress(const QString &addr) const;
|
||||
bool hasFriendWithPublicKey(const QString &pubkey) const;
|
||||
int joinGroupchat(int32_t friendId, uint8_t type, const uint8_t* pubkey,uint16_t length) const;
|
||||
uint32_t joinGroupchat(int32_t friendId, uint8_t type, const uint8_t* pubkey,uint16_t length) const;
|
||||
void quitGroupChat(int groupId) const;
|
||||
|
||||
QString getUsername() const;
|
||||
|
@ -225,6 +225,8 @@ private:
|
|||
uint32_t receipt, void *core);
|
||||
|
||||
void sendGroupMessageWithType(int groupId, 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();
|
||||
|
||||
void checkEncryptedHistory();
|
||||
|
|
|
@ -597,6 +597,17 @@ bool CoreAV::isGroupAvEnabled(int groupId) const
|
|||
Tox* tox = Core::getInstance()->tox;
|
||||
TOX_ERR_CONFERENCE_GET_TYPE error;
|
||||
TOX_CONFERENCE_TYPE type = tox_conference_get_type(tox, groupId, &error);
|
||||
switch (error)
|
||||
{
|
||||
case TOX_ERR_CONFERENCE_GET_TYPE_OK:
|
||||
break;
|
||||
case TOX_ERR_CONFERENCE_GET_TYPE_CONFERENCE_NOT_FOUND:
|
||||
qCritical() << "Conference not found";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return type == TOX_CONFERENCE_TYPE_AV;
|
||||
}
|
||||
|
||||
|
|
|
@ -81,13 +81,31 @@ void CoreFile::sendAvatarFile(Core* core, uint32_t friendId, const QByteArray& d
|
|||
uint8_t avatarHash[TOX_HASH_LENGTH];
|
||||
tox_hash(avatarHash, (uint8_t*)data.data(), data.size());
|
||||
uint64_t filesize = data.size();
|
||||
TOX_ERR_FILE_SEND err;
|
||||
uint32_t fileNum = tox_file_send(core->tox, friendId, TOX_FILE_KIND_AVATAR, filesize,
|
||||
avatarHash, avatarHash, TOX_HASH_LENGTH, &err);
|
||||
|
||||
if (fileNum == std::numeric_limits<uint32_t>::max())
|
||||
TOX_ERR_FILE_SEND error;
|
||||
uint32_t fileNum = tox_file_send(core->tox, friendId, TOX_FILE_KIND_AVATAR, filesize,
|
||||
avatarHash, avatarHash, TOX_HASH_LENGTH, &error);
|
||||
|
||||
switch (error)
|
||||
{
|
||||
qWarning() << "sendAvatarFile: Can't create the Tox file sender, error"<<err;
|
||||
case TOX_ERR_FILE_SEND_OK:
|
||||
break;
|
||||
case TOX_ERR_FILE_SEND_FRIEND_NOT_CONNECTED:
|
||||
qCritical() << "Friend not connected";
|
||||
return;
|
||||
case TOX_ERR_FILE_SEND_FRIEND_NOT_FOUND:
|
||||
qCritical() << "Friend not found";
|
||||
return;
|
||||
case TOX_ERR_FILE_SEND_NAME_TOO_LONG:
|
||||
qCritical() << "Name too long";
|
||||
return;
|
||||
case TOX_ERR_FILE_SEND_NULL:
|
||||
qCritical() << "Send null";
|
||||
return;
|
||||
case TOX_ERR_FILE_SEND_TOO_MANY:
|
||||
qCritical() << "To many ougoing transfer";
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -260,7 +260,7 @@ void GroupNetCamView::friendAvatarChanged(int FriendId, const QPixmap &pixmap)
|
|||
{
|
||||
Friend* f = FriendList::findFriend(FriendId);
|
||||
|
||||
for (int i = 0; i < Core::getInstance()->getGroupNumberPeers(group); ++i)
|
||||
for (uint32_t i = 0; i < Core::getInstance()->getGroupNumberPeers(group); ++i)
|
||||
{
|
||||
if (Core::getInstance()->getGroupPeerToxId(group, i) == f->getToxId())
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user