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

Merge branch 'pr686'

This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-11-10 01:17:35 +01:00
commit fbbffc0806
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
4 changed files with 77 additions and 14 deletions

View File

@ -713,18 +713,29 @@ void Core::acceptFriendRequest(const QString& userId)
void Core::requestFriendship(const QString& friendAddress, const QString& message) void Core::requestFriendship(const QString& friendAddress, const QString& message)
{ {
const QString userId = friendAddress.mid(0, TOX_CLIENT_ID_SIZE * 2);
if(hasFriendWithAddress(friendAddress))
{
emit failedToAddFriend(userId, QString(tr("Friend is already added")));
}
else
{
qDebug() << "Core: requesting friendship of "+friendAddress; qDebug() << "Core: requesting friendship of "+friendAddress;
CString cMessage(message); CString cMessage(message);
int friendId = tox_add_friend(tox, CFriendAddress(friendAddress).data(), cMessage.data(), cMessage.size()); 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)
if (friendId < 0) { {
emit failedToAddFriend(userId); emit failedToAddFriend(userId);
} else { }
else
{
// Update our friendAddresses // Update our friendAddresses
Settings::getInstance().updateFriendAdress(friendAddress); Settings::getInstance().updateFriendAdress(friendAddress);
emit friendAdded(friendId, userId); emit friendAdded(friendId, userId);
} }
}
saveConfiguration(); saveConfiguration();
} }
@ -1592,6 +1603,51 @@ void Core::createGroup()
emit emptyGroupCreated(tox_add_groupchat(tox)); 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 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 // 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 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 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 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 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 void quitGroupChat(int groupId) const; ///< Quit a groupchat
@ -154,7 +156,7 @@ signals:
void groupSentResult(int groupId, const QString& message, int result); void groupSentResult(int groupId, const QString& message, int result);
void actionSentResult(int friendId, const QString& action, int success); 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 failedToRemoveFriend(int friendId);
void failedToSetUsername(const QString& username); void failedToSetUsername(const QString& username);
void failedToSetStatusMessage(const QString& message); void failedToSetStatusMessage(const QString& message);

View File

@ -650,9 +650,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(tr("Couldn't request friendship"));
if(!errorInfo.isEmpty()) {
info = info + (QString(": ") + errorInfo);
}
QMessageBox::critical(0,"Error",info);
} }
void Widget::onFriendStatusChanged(int friendId, Status status) void Widget::onFriendStatusChanged(int friendId, Status status)

View File

@ -102,7 +102,7 @@ private slots:
void setUsername(const QString& username); void setUsername(const QString& username);
void setStatusMessage(const QString &statusMessage); void setStatusMessage(const QString &statusMessage);
void addFriend(int friendId, const QString& userId); 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 onFriendStatusChanged(int friendId, Status status);
void onFriendStatusMessageChanged(int friendId, const QString& message); void onFriendStatusMessageChanged(int friendId, const QString& message);
void onFriendUsernameChanged(int friendId, const QString& username); void onFriendUsernameChanged(int friendId, const QString& username);