Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a colour code for the user #298

Merged
merged 1 commit into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include <QtCore/QStringBuilder>
#include <QtCore/QElapsedTimer>

#include <QtCore/QCryptographicHash>
#include <QtCore/QtEndian>

#include <functional>

using namespace QMatrixClient;
Expand All @@ -47,8 +50,21 @@ class User::Private
return Avatar(move(url));
}

qreal makeHueF(QString userId)
{
QByteArray hash = QCryptographicHash::hash(userId.toUtf8(),
QCryptographicHash::Sha1);
QDataStream dataStream(qToLittleEndian(hash).left(2));
dataStream.setByteOrder(QDataStream::LittleEndian);
quint16 hashValue;
dataStream >> hashValue;
qreal hueF = static_cast<qreal>(hashValue)/std::numeric_limits<quint16>::max();
Q_ASSERT((0 <= hueF) && (hueF <= 1));
return hueF;
}

Private(QString userId, Connection* connection)
: userId(move(userId)), connection(connection)
: userId(move(userId)), connection(connection), hueF(makeHueF(userId))
{ }

QString userId;
Expand All @@ -57,6 +73,7 @@ class User::Private
QString bridged;
QString mostUsedName;
QMultiHash<QString, const Room*> otherNames;
qreal hueF;
Avatar mostUsedAvatar { makeAvatar({}) };
std::vector<Avatar> otherAvatars;
auto otherAvatar(QUrl url)
Expand Down Expand Up @@ -219,6 +236,11 @@ bool User::isGuest() const
return *it == ':';
}

int User::hue() const
{
return int(hueF()*359);
}

QString User::name(const Room* room) const
{
return d->nameForRoom(room);
Expand Down Expand Up @@ -424,3 +446,7 @@ void User::processEvent(const RoomMemberEvent& event, const Room* room)
updateAvatarUrl(event.avatarUrl(), d->avatarUrlForRoom(room), room);
}
}

qreal User::hueF() const {
return d->hueF;
}
11 changes: 11 additions & 0 deletions lib/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace QMatrixClient
Q_OBJECT
Q_PROPERTY(QString id READ id CONSTANT)
Q_PROPERTY(bool isGuest READ isGuest CONSTANT)
Q_PROPERTY(int hue READ hue CONSTANT)
Q_PROPERTY(qreal hueF READ hueF CONSTANT)
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
Q_PROPERTY(QString displayName READ displayname NOTIFY nameChanged STORED false)
Q_PROPERTY(QString fullName READ fullName NOTIFY nameChanged STORED false)
Expand Down Expand Up @@ -95,6 +97,15 @@ namespace QMatrixClient
*/
bool isGuest() const;

/** Hue color component of this user based on id.
* The implementation is based on XEP-0392:
* https://xmpp.org/extensions/xep-0392.html
* Naming and ranges are the same as QColor's hue methods:
* https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
*/
int hue() const;
qreal hueF() const;

const Avatar& avatarObject(const Room* room = nullptr) const;
Q_INVOKABLE QImage avatar(int dimension, const Room* room = nullptr);
Q_INVOKABLE QImage avatar(int requestedWidth, int requestedHeight,
Expand Down