diff --git a/src/core.cpp b/src/core.cpp index 48d5c7401..36a623973 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -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(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 diff --git a/src/core.h b/src/core.h index 2ad0f7ad7..1c439025a 100644 --- a/src/core.h +++ b/src/core.h @@ -53,6 +53,8 @@ public: QList 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); diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 6494adad3..369c01b61 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -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) diff --git a/src/widget/widget.h b/src/widget/widget.h index 2193fee55..d5a8b27ad 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -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);