diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-05-02 22:26:06 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-05-02 23:05:59 +0900 |
commit | 9a415111fefd0d21f8b4abbbe9af9066d78c971b (patch) | |
tree | 737052a0675b5ecce7c3d2902f2278400f13885e /room.cpp | |
parent | b36a9f32b1fcdce404e9a0a8cb780ba2ab89c24b (diff) | |
download | libquotient-9a415111fefd0d21f8b4abbbe9af9066d78c971b.tar.gz libquotient-9a415111fefd0d21f8b4abbbe9af9066d78c971b.zip |
Actually connect User::nameChanged to Room; make a new Room::memberRenamed signal, which clients should use in the room context.
Processing changes of user displaynames is tricky: we have to not only deal with the currently renamed user but also with its past and new namesakes which might change representation due to that renaming. So in the worst case a single User::nameChanged signal may lead to three Room::memberRenamed references (and 3 user displaynames updated in the UI, respectively). And the newly added users should be taken care of in a similar manner, of course.
Diffstat (limited to 'room.cpp')
-rw-r--r-- | room.cpp | 54 |
1 files changed, 45 insertions, 9 deletions
@@ -89,6 +89,9 @@ class Room::Private: public QObject private: QString calculateDisplayname() const; QString roomNameFromMemberNames(const QList<User*>& userlist) const; + + void insertMemberIntoMap(User* u); + void removeMemberFromMap(QString username, User* u); }; Room::Room(Connection* connection, QString id) @@ -209,12 +212,37 @@ QList< User* > Room::users() const return d->membersMap.values(); } +void Room::Private::insertMemberIntoMap(User *u) +{ + QList<User*> namesakes = membersMap.values(u->name()); + membersMap.insert(u->name(), u); + // If there is exactly one namesake of the added user, signal member renaming + // for that other one because the two should be disambiguated now. + if (namesakes.size() == 1) + emit q->memberRenamed(namesakes[0]); + + updateDisplayname(); +} + +void Room::Private::removeMemberFromMap(QString username, User* u) +{ + membersMap.remove(username, u); + // If there was one namesake besides the removed user, signal member renaming + // for it because it doesn't need to be disambiguated anymore. + // TODO: Think about left users. + QList<User*> formerNamesakes = membersMap.values(username); + if (formerNamesakes.size() == 1) + emit q->memberRenamed(formerNamesakes[0]); + + updateDisplayname(); +} + void Room::Private::addMember(User *u) { - if (!membersMap.values(u->name()).contains(u)) + if (!hasMember(u)) { - membersMap.insert(u->name(), u); - updateDisplayname(); + insertMemberIntoMap(u); + connect(u, &User::nameChanged, q, &Room::userRenamed); emit q->userAdded(u); } } @@ -232,11 +260,20 @@ User* Room::Private::member(QString id) const void Room::Private::renameMember(User* u, QString oldName) { - // We can't use hasUser because we need to search by oldName + if (hasMember(u)) + { + qWarning() << "Room::Private::renameMember(): the user " + << u->name() + << "is already known in the room under a new name."; + return; + } + if (membersMap.values(oldName).contains(u)) { - membersMap.remove(oldName, u); - membersMap.insert(u->name(), u); + removeMemberFromMap(oldName, u); + insertMemberIntoMap(u); + emit q->memberRenamed(u); + updateDisplayname(); } } @@ -247,13 +284,12 @@ void Room::Private::removeMember(User* u) { if ( !membersLeft.contains(u) ) membersLeft.append(u); - membersMap.remove(u->name(), u); - updateDisplayname(); + removeMemberFromMap(u->name(), u); emit q->userRemoved(u); } } -void Room::memberRenamed(User* user, QString oldName) +void Room::userRenamed(User* user, QString oldName) { d->renameMember(user, oldName); } |