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

refactor(friend): add an additional signal to Friend

This commit is contained in:
sudden6 2018-02-24 02:41:29 +01:00
parent e564b85e3c
commit a910d57824
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
2 changed files with 34 additions and 4 deletions

View File

@ -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()) {

View File

@ -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);