diff --git a/src/friend.cpp b/src/friend.cpp index 61931078d..e30b9845f 100644 --- a/src/friend.cpp +++ b/src/friend.cpp @@ -19,15 +19,22 @@ #include "widget/friendwidget.h" #include "widget/form/chatform.h" #include "widget/widget.h" +#include "src/core.h" +#include "src/misc/settings.h" -Friend::Friend(int FriendId, QString UserId) +Friend::Friend(int FriendId, const ToxID &UserId) : friendId(FriendId) { hasNewEvents = 0; friendStatus = Status::Offline; - userID = ToxID::fromString(UserId); - userName = UserId; - widget = new FriendWidget(friendId, UserId); + userID = UserId; + userName = Core::getInstance()->getPeerName(UserId); + if (userName.size() == 0) + userName = UserId.publicKey; + + userAlias = Settings::getInstance().getFriendAlias(UserId); + + widget = new FriendWidget(friendId, getDisplayedName()); chatForm = new ChatForm(this); } diff --git a/src/friend.h b/src/friend.h index 899b9c9cb..7117acc1c 100644 --- a/src/friend.h +++ b/src/friend.h @@ -26,7 +26,7 @@ class ChatForm; struct Friend { public: - Friend(int FriendId, QString UserId); + Friend(int FriendId, const ToxID &UserId); ~Friend(); void setName(QString name); diff --git a/src/friendlist.cpp b/src/friendlist.cpp index 87e8f0c01..d75da5796 100644 --- a/src/friendlist.cpp +++ b/src/friendlist.cpp @@ -24,7 +24,7 @@ QHash FriendList::friendList; QHash FriendList::tox2id; -Friend* FriendList::addFriend(int friendId, const QString& userId) +Friend* FriendList::addFriend(int friendId, const ToxID& userId) { auto friendChecker = friendList.find(friendId); if (friendChecker != friendList.end()) @@ -32,7 +32,7 @@ Friend* FriendList::addFriend(int friendId, const QString& userId) Friend* newfriend = new Friend(friendId, userId); friendList[friendId] = newfriend; - tox2id[userId] = friendId; + tox2id[userId.publicKey] = friendId; return newfriend; } @@ -63,15 +63,15 @@ void FriendList::clear() delete friendptr; } -Friend* FriendList::findFriend(QString userId) +Friend* FriendList::findFriend(const ToxID& userId) { - auto id = tox2id.find(userId); + auto id = tox2id.find(userId.publicKey); if (id != tox2id.end()) { Friend *f = findFriend(*id); if (!f) return nullptr; - if (f->getToxID() == ToxID::fromString(userId)) + if (f->getToxID() == userId) return f; } diff --git a/src/friendlist.h b/src/friendlist.h index 67e7292a2..6d41d44ce 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -21,14 +21,15 @@ template class QList; template class QHash; struct Friend; class QString; +struct ToxID; class FriendList { public: FriendList(); - static Friend* addFriend(int friendId, const QString& userId); + static Friend* addFriend(int friendId, const ToxID &userId); static Friend* findFriend(int friendId); - static Friend* findFriend(QString userId); + static Friend* findFriend(const ToxID &userId); static QList getAllFriends(); static void removeFriend(int friendId, bool fake = false); static void clear(); diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index ac46e2340..d221fdc78 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -78,9 +78,6 @@ ChatForm::ChatForm(Friend* chatFriend) connect(this, SIGNAL(chatAreaCleared()), this, SLOT(clearReciepts())); setAcceptDrops(true); - - if (Settings::getInstance().getEnableLogging()) - loadHistory(QDateTime::currentDateTime().addDays(-7), true); } ChatForm::~ChatForm() diff --git a/src/widget/form/chatform.h b/src/widget/form/chatform.h index 4ec1735d7..b8920b86f 100644 --- a/src/widget/form/chatform.h +++ b/src/widget/form/chatform.h @@ -36,6 +36,7 @@ public: ChatForm(Friend* chatFriend); ~ChatForm(); void setStatusMessage(QString newMessage); + void loadHistory(QDateTime since, bool processUndelivered = false); void dischargeReceipt(int receipt); @@ -85,7 +86,6 @@ private slots: void updateTime(); protected: - void loadHistory(QDateTime since, bool processUndelivered = false); // drag & drop void dragEnterEvent(QDragEnterEvent* ev); void dropEvent(QDropEvent* ev); diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index dc4d00ea6..6b9d2bdd5 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -364,7 +364,7 @@ MessageActionPtr GenericChatForm::genMessageActionAction(const ToxID& author, QS if (isMe) authorStr = core->getUsername(); else { - Friend *f = FriendList::findFriend(author.publicKey); + Friend *f = FriendList::findFriend(author); if (f) authorStr = f->getDisplayedName(); else diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 3410845d4..81e359370 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -611,12 +611,13 @@ void Widget::setStatusMessage(const QString &statusMessage) void Widget::addFriend(int friendId, const QString &userId) { //qDebug() << "Widget: Adding friend with id" << userId; - Friend* newfriend = FriendList::addFriend(friendId, userId); + ToxID userToxId = ToxID::fromString(userId); + Friend* newfriend = FriendList::addFriend(friendId, userToxId); QLayout* layout = contactListWidget->getFriendLayout(Status::Offline); layout->addWidget(newfriend->getFriendWidget()); - QString alias = Settings::getInstance().getFriendAlias(ToxID::fromString(userId)); - newfriend->setAlias(alias); + if (Settings::getInstance().getEnableLogging()) + newfriend->getChatForm()->loadHistory(QDateTime::currentDateTime().addDays(-7), true); connect(newfriend->getFriendWidget(), SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), this, SLOT(onChatroomWidgetClicked(GenericChatroomWidget*))); connect(newfriend->getFriendWidget(), SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int)));