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

Additional error info for failedToAddFriend, checks if friend is added already

This commit is contained in:
saneki 2014-11-09 13:23:43 -06:00
parent 1339298db5
commit 55bb6ac367
4 changed files with 67 additions and 14 deletions

View File

@ -713,17 +713,23 @@ void Core::acceptFriendRequest(const QString& userId)
void Core::requestFriendship(const QString& friendAddress, const QString& message)
{
qDebug() << "Core: requesting friendship of "+friendAddress;
CString cMessage(message);
int friendId = tox_add_friend(tox, CFriendAddress(friendAddress).data(), cMessage.data(), cMessage.size());
const QString userId = friendAddress.mid(0, TOX_CLIENT_ID_SIZE * 2);
if (friendId < 0) {
emit failedToAddFriend(userId);
} else {
// Update our friendAddresses
Settings::getInstance().updateFriendAdress(friendAddress);
emit friendAdded(friendId, userId);
if(hasFriendWithAddress(friendAddress)) {
emit failedToAddFriend(userId, "Friend is already added");
}
else {
qDebug() << "Core: requesting friendship of "+friendAddress;
CString cMessage(message);
int friendId = tox_add_friend(tox, CFriendAddress(friendAddress).data(), cMessage.data(), cMessage.size());
if (friendId < 0) {
emit failedToAddFriend(userId);
} else {
// Update our friendAddresses
Settings::getInstance().updateFriendAdress(friendAddress);
emit friendAdded(friendId, userId);
}
}
saveConfiguration();
}
@ -1592,6 +1598,46 @@ void Core::createGroup()
emit emptyGroupCreated(tox_add_groupchat(tox));
}
bool Core::hasFriendWithAddress(const QString &addr) const
{
// Valid length check
if(addr.length() != (TOX_FRIEND_ADDRESS_SIZE * 2)) {
return false;
}
QString pubkey = addr.left(TOX_CLIENT_ID_SIZE * 2);
return hasFriendWithPublicKey(pubkey);
}
bool Core::hasFriendWithPublicKey(const QString &pubkey) const
{
// Valid length check
if(pubkey.length() != (TOX_CLIENT_ID_SIZE * 2)) {
return false;
}
bool found = false;
const uint32_t friendCount = tox_count_friendlist(tox);
if (friendCount > 0) {
int32_t *ids = new int32_t[friendCount];
tox_get_friendlist(tox, ids, friendCount);
for (int32_t i = 0; i < static_cast<int32_t>(friendCount); ++i) {
// getFriendAddress may return either id (public key) or address
QString addrOrId = getFriendAddress(ids[i]);
// Set true if found
if(addrOrId.toUpper().startsWith(pubkey.toUpper())) {
found = true;
break;
}
}
delete[] ids;
}
return found;
}
QString Core::getFriendAddress(int friendNumber) const
{
// If we don't know the full address of the client, return just the id, otherwise get the full address

View File

@ -53,6 +53,8 @@ public:
QList<QString> getGroupPeerNames(int groupId) const; ///< Get the names of the peers of a group
QString getFriendAddress(int friendNumber) const; ///< Get the full address if known, or Tox ID of a friend
QString getFriendUsername(int friendNumber) const; ///< Get the username of a friend
bool hasFriendWithAddress(const QString &addr) const; ///< Check if we have a friend by address
bool hasFriendWithPublicKey(const QString &pubkey) const; ///< Check if we have a friend by public key
int joinGroupchat(int32_t friendNumber, const uint8_t* pubkey,uint16_t length) const; ///< Accept a groupchat invite
void quitGroupChat(int groupId) const; ///< Quit a groupchat
@ -154,7 +156,7 @@ signals:
void groupSentResult(int groupId, const QString& message, int result);
void actionSentResult(int friendId, const QString& action, int success);
void failedToAddFriend(const QString& userId);
void failedToAddFriend(const QString& userId, const QString& errorInfo = QString());
void failedToRemoveFriend(int friendId);
void failedToSetUsername(const QString& username);
void failedToSetStatusMessage(const QString& message);

View File

@ -644,9 +644,14 @@ void Widget::addFriend(int friendId, const QString &userId)
}
}
void Widget::addFriendFailed(const QString&)
void Widget::addFriendFailed(const QString&, const QString& errorInfo)
{
QMessageBox::critical(0,"Error","Couldn't request friendship");
QString info = QString("Couldn't request friendship");
if(!errorInfo.isEmpty()) {
info = info + (QString(": ") + errorInfo);
}
QMessageBox::critical(0,"Error",info);
}
void Widget::onFriendStatusChanged(int friendId, Status status)

View File

@ -101,7 +101,7 @@ private slots:
void setUsername(const QString& username);
void setStatusMessage(const QString &statusMessage);
void addFriend(int friendId, const QString& userId);
void addFriendFailed(const QString& userId);
void addFriendFailed(const QString& userId, const QString& errorInfo = QString());
void onFriendStatusChanged(int friendId, Status status);
void onFriendStatusMessageChanged(int friendId, const QString& message);
void onFriendUsernameChanged(int friendId, const QString& username);