diff --git a/src/model/group.cpp b/src/model/group.cpp index 5506efbc3..944b8b818 100644 --- a/src/model/group.cpp +++ b/src/model/group.cpp @@ -52,7 +52,8 @@ void Group::updatePeer(int peerId, QString name) toxids[peerPk] = name; Friend* f = FriendList::findFriend(peerKey); - if (f != nullptr && f->hasAlias()) { + if (f != nullptr) { + // use the displayed name from the friends list peers[peerId] = f->getDisplayedName(); toxids[peerPk] = f->getDisplayedName(); } else { diff --git a/src/widget/contentdialog.cpp b/src/widget/contentdialog.cpp index b3bfaa9ba..57311728a 100644 --- a/src/widget/contentdialog.cpp +++ b/src/widget/contentdialog.cpp @@ -167,6 +167,7 @@ FriendWidget* ContentDialog::addFriend(const Friend* frnd, GenericChatForm* form friendLayout->addFriendWidget(friendWidget, frnd->getStatus()); friendChatForms[friendId] = form; + // TODO(sudden6): move this connection to the Friend::displayedNameChanged signal connect(frnd, &Friend::aliasChanged, this, &ContentDialog::updateFriendWidget); connect(friendWidget, &FriendWidget::chatroomWidgetClicked, this, &ContentDialog::activate); connect(friendWidget, &FriendWidget::newWindowOpened, this, &ContentDialog::openNewDialog); @@ -745,7 +746,6 @@ void ContentDialog::updateFriendWidget(uint32_t friendId, QString alias) Friend* f = FriendList::findFriend(friendId); GenericChatroomWidget* widget = std::get<1>(friendList.find(friendId).value()); FriendWidget* friendWidget = static_cast(widget); - friendWidget->setName(alias); Status status = f->getStatus(); friendLayout->addFriendWidget(friendWidget, status); diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 9a172358f..a26ef2efd 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -195,6 +195,9 @@ ChatForm::ChatForm(Friend* chatFriend, History* history) isTyping = false; }); + // reflect name changes in the header + // TODO(sudden6): check if this creates a cycle when the alias is changed + connect(f, &Friend::displayedNameChanged, this, &ChatForm::setName); connect(headWidget, &ChatFormHeader::nameChanged, this, [=](const QString& newName) { f->setAlias(newName); }); diff --git a/src/widget/friendwidget.cpp b/src/widget/friendwidget.cpp index c4cf4e473..18d381661 100644 --- a/src/widget/friendwidget.cpp +++ b/src/widget/friendwidget.cpp @@ -70,9 +70,12 @@ FriendWidget::FriendWidget(const Friend* f, bool compact) avatar->setPixmap(QPixmap(":/img/contact.svg")); statusPic.setPixmap(QPixmap(":/img/status/offline.svg")); statusPic.setMargin(3); - nameLabel->setText(f->getDisplayedName()); + setName(f->getDisplayedName()); nameLabel->setTextFormat(Qt::PlainText); - connect(nameLabel, &CroppingLabel::editFinished, this, &FriendWidget::setAlias); + // update on changes of the displayed name + connect(f, &Friend::displayedNameChanged, this, &FriendWidget::setName); + // update alias when edited + connect(nameLabel, &CroppingLabel::editFinished, f, &Friend::setAlias); statusMessageLabel->setTextFormat(Qt::PlainText); } @@ -466,11 +469,3 @@ void FriendWidget::mouseMoveEvent(QMouseEvent* ev) drag->exec(Qt::CopyAction | Qt::MoveAction); } } - -void FriendWidget::setAlias(const QString& _alias) -{ - QString alias = _alias.left(tox_max_name_length()); - // Hack to avoid edit const Friend. TODO: Repalce on emit - Friend* f = FriendList::findFriend(frnd->getId()); - f->setAlias(alias); -} diff --git a/src/widget/friendwidget.h b/src/widget/friendwidget.h index 13a1125d1..fd82687b5 100644 --- a/src/widget/friendwidget.h +++ b/src/widget/friendwidget.h @@ -47,7 +47,6 @@ signals: public slots: void onAvatarChange(uint32_t friendId, const QPixmap& pic); void onAvatarRemoved(uint32_t friendId); - void setAlias(const QString& alias); void onContextMenuCalled(QContextMenuEvent* event); protected: diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 3593d6ddf..187211b96 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -998,7 +998,7 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk) contactListWidget->addFriendWidget(widget, Status::Offline, s.getFriendCircleID(friendPk)); connect(newfriend, &Friend::aliasChanged, this, &Widget::onFriendAliasChanged); - connect(newfriend, &Friend::nameChanged, this, &Widget::onFriendAliasChanged); + connect(newfriend, &Friend::displayedNameChanged, this, &Widget::onFriendDisplayedNameChanged); connect(friendForm, &ChatForm::incomingNotification, this, &Widget::incomingNotification); connect(friendForm, &ChatForm::outgoingNotification, this, &Widget::outgoingNotification); @@ -1083,6 +1083,16 @@ void Widget::onFriendStatusMessageChanged(int friendId, const QString& message) ContentDialog::updateFriendStatusMessage(friendId, message); } +void Widget::onFriendDisplayedNameChanged(const QString& displayed) +{ + Friend* f = qobject_cast(sender()); + FriendWidget* friendWidget = friendWidgets[f->getId()]; + + if (friendWidget->isActive()) { + GUI::setWindowTitle(displayed); + } +} + void Widget::onFriendUsernameChanged(int friendId, const QString& username) { Friend* f = FriendList::findFriend(friendId); @@ -1097,27 +1107,16 @@ void Widget::onFriendUsernameChanged(int friendId, const QString& username) void Widget::onFriendAliasChanged(uint32_t friendId, const QString& alias) { - Friend* f = FriendList::findFriend(friendId); - FriendWidget* friendWidget = friendWidgets[friendId]; - - friendWidget->setName(alias); - - if (friendWidget->isActive()) { - GUI::setWindowTitle(alias); - } + Friend* f = qobject_cast(sender()); + // TODO(sudden6): don't update the contact list here, make it update itself + FriendWidget* friendWidget = friendWidgets[f->getId()]; Status status = f->getStatus(); contactListWidget->moveWidget(friendWidget, status); FilterCriteria criteria = getFilterCriteria(); bool filter = status == Status::Offline ? filterOffline(criteria) : filterOnline(criteria); friendWidget->searchName(ui->searchContactText->text(), filter); - ChatForm* friendForm = chatForms[friendId]; - friendForm->setName(alias); - for (Group* g : GroupList::getAllGroups()) { - g->regeneratePeerList(); - } - const ToxPk& pk = f->getPublicKey(); Settings& s = Settings::getInstance(); s.setFriendAlias(pk, alias); diff --git a/src/widget/widget.h b/src/widget/widget.h index 81d8e99e8..6ddf78843 100644 --- a/src/widget/widget.h +++ b/src/widget/widget.h @@ -157,6 +157,7 @@ public slots: void addFriendFailed(const ToxPk& userId, const QString& errorInfo = QString()); void onFriendStatusChanged(int friendId, Status status); void onFriendStatusMessageChanged(int friendId, const QString& message); + void onFriendDisplayedNameChanged(const QString& displayed); void onFriendUsernameChanged(int friendId, const QString& username); void onFriendAliasChanged(uint32_t friendId, const QString& alias); void onFriendMessageReceived(int friendId, const QString& message, bool isAction);