From a910d57824b3d9ca79a814a40dadefc415bb3cbb Mon Sep 17 00:00:00 2001 From: sudden6 Date: Sat, 24 Feb 2018 02:41:29 +0100 Subject: [PATCH] refactor(friend): add an additional signal to Friend --- src/model/friend.cpp | 37 +++++++++++++++++++++++++++++++++---- src/model/friend.h | 1 + 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/model/friend.cpp b/src/model/friend.cpp index 2da20ebf2..28d30a43c 100644 --- a/src/model/friend.cpp +++ b/src/model/friend.cpp @@ -39,6 +39,10 @@ Friend::Friend(uint32_t friendId, const ToxPk& friendPk, const QString& userAlia } } +/** + * @brief Friend::setName sets a new username for the friend + * @param _name new username, sets the public key if _name is empty + */ void Friend::setName(const QString& _name) { QString name = _name; @@ -46,17 +50,36 @@ void Friend::setName(const QString& _name) name = friendPk.toString(); } + // save old displayed name to be able to compare for changes + const auto oldDisplayed = getDisplayedName(); if (userName != name) { userName = name; emit nameChanged(friendId, name); } -} + const auto newDisplayed = getDisplayedName(); + if (oldDisplayed != newDisplayed) { + emit displayedNameChanged(newDisplayed); + } +} +/** + * @brief Friend::setAlias sets the alias for the friend + * @param alias new alias, removes it if set to an empty string + */ void Friend::setAlias(const QString& alias) { - if (userAlias != alias) { - userAlias = alias; - emit aliasChanged(friendId, alias); + if (userAlias == alias) { + return; + } + emit aliasChanged(friendId, alias); + + // save old displayed name to be able to compare for changes + const auto oldDisplayed = getDisplayedName(); + userAlias = alias; + + const auto newDisplayed = getDisplayedName(); + if (oldDisplayed != newDisplayed) { + emit displayedNameChanged(newDisplayed); } } @@ -73,6 +96,12 @@ QString Friend::getStatusMessage() const return statusMessage; } +/** + * @brief Friend::getDisplayedName Gets the name that should be displayed for a user + * @return a QString containing either alias, username or public key + * @note This function and corresponding signal should be preferred over getting + * the name or alias directly. + */ QString Friend::getDisplayedName() const { if (userAlias.isEmpty()) { diff --git a/src/model/friend.h b/src/model/friend.h index eed54e51c..79a354d05 100644 --- a/src/model/friend.h +++ b/src/model/friend.h @@ -52,6 +52,7 @@ public: Status getStatus() const; signals: + void displayedNameChanged(const QString& newName); void nameChanged(uint32_t friendId, const QString& name); void aliasChanged(uint32_t friendId, QString alias); void statusChanged(uint32_t friendId, Status status);