3333#include < QtCore/QStringBuilder>
3434#include < QtCore/QElapsedTimer>
3535
36+ #include < QtCore/QCryptographicHash>
37+ #include < QtCore/QtEndian>
38+
3639#include < functional>
3740
3841using namespace QMatrixClient ;
@@ -47,8 +50,21 @@ class User::Private
4750 return Avatar (move (url));
4851 }
4952
53+ qreal makeHueF (QString userId)
54+ {
55+ QByteArray hash = QCryptographicHash::hash (userId.toUtf8 (),
56+ QCryptographicHash::Sha1);
57+ QDataStream dataStream (qToLittleEndian (hash).left (2 ));
58+ dataStream.setByteOrder (QDataStream::LittleEndian);
59+ quint16 hashValue;
60+ dataStream >> hashValue;
61+ qreal hueF = static_cast <qreal>(hashValue)/std::numeric_limits<quint16>::max ();
62+ Q_ASSERT ((0 <= hueF) && (hueF <= 1 ));
63+ return hueF;
64+ }
65+
5066 Private (QString userId, Connection* connection)
51- : userId(move(userId)), connection(connection)
67+ : userId(move(userId)), connection(connection), hueF(makeHueF(userId))
5268 { }
5369
5470 QString userId;
@@ -57,6 +73,7 @@ class User::Private
5773 QString bridged;
5874 QString mostUsedName;
5975 QMultiHash<QString, const Room*> otherNames;
76+ qreal hueF;
6077 Avatar mostUsedAvatar { makeAvatar ({}) };
6178 std::vector<Avatar> otherAvatars;
6279 auto otherAvatar (QUrl url)
@@ -219,6 +236,11 @@ bool User::isGuest() const
219236 return *it == ' :' ;
220237}
221238
239+ int User::hue () const
240+ {
241+ return int (hueF ()*359 );
242+ }
243+
222244QString User::name (const Room* room) const
223245{
224246 return d->nameForRoom (room);
@@ -424,3 +446,7 @@ void User::processEvent(const RoomMemberEvent& event, const Room* room)
424446 updateAvatarUrl (event.avatarUrl (), d->avatarUrlForRoom (room), room);
425447 }
426448}
449+
450+ qreal User::hueF () const {
451+ return d->hueF ;
452+ }
0 commit comments