diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-06-29 19:59:54 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-06-29 20:05:33 +0900 |
commit | f58e3a24161db2cb2e400b5a686f140eb054f541 (patch) | |
tree | 23a8e907fd1d1c1d2306330b30db0c8f3d9e5562 | |
parent | 5722ceaf4bd10c29f1091e3dc5a87f5650ea8c71 (diff) | |
parent | d3ddd394e855cfe217bf0f0d368822c9b99316bb (diff) | |
download | libquotient-f58e3a24161db2cb2e400b5a686f140eb054f541.tar.gz libquotient-f58e3a24161db2cb2e400b5a686f140eb054f541.zip |
Merge remote-tracking branch 'remotes/origin/master' into use-clang-format
-rw-r--r-- | lib/user.cpp | 18 | ||||
-rw-r--r-- | lib/util.cpp | 17 | ||||
-rw-r--r-- | lib/util.h | 8 |
3 files changed, 26 insertions, 17 deletions
diff --git a/lib/user.cpp b/lib/user.cpp index 8adb1dae..c463b42e 100644 --- a/lib/user.cpp +++ b/lib/user.cpp @@ -29,13 +29,11 @@ #include "events/event.h" #include "events/roommemberevent.h" -#include <QtCore/QCryptographicHash> #include <QtCore/QElapsedTimer> #include <QtCore/QPointer> #include <QtCore/QRegularExpression> #include <QtCore/QStringBuilder> #include <QtCore/QTimer> -#include <QtCore/QtEndian> #include <functional> @@ -48,24 +46,10 @@ class User::Private public: static Avatar makeAvatar(QUrl url) { return Avatar(move(url)); } - qreal makeHueF() - { - Q_ASSERT(!userId.isEmpty()); - QByteArray hash = QCryptographicHash::hash(userId.toUtf8(), - QCryptographicHash::Sha1); - QDataStream dataStream(qToLittleEndian(hash).left(2)); - dataStream.setByteOrder(QDataStream::LittleEndian); - quint16 hashValue; - dataStream >> hashValue; - const auto hueF = 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) - , hueF(makeHueF()) + , hueF(stringToHueF(this->userId)) {} QString userId; diff --git a/lib/util.cpp b/lib/util.cpp index be2665c5..4a7e0f61 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -18,10 +18,13 @@ #include "util.h" +#include <QtCore/QCryptographicHash> +#include <QtCore/QDataStream> #include <QtCore/QDir> #include <QtCore/QRegularExpression> #include <QtCore/QStandardPaths> #include <QtCore/QStringBuilder> +#include <QtCore/QtEndian> static const auto RegExpOptions = QRegularExpression::CaseInsensitiveOption @@ -96,6 +99,20 @@ QString QMatrixClient::cacheLocation(const QString& dirName) return cachePath; } +qreal QMatrixClient::stringToHueF(const QString& string) +{ + Q_ASSERT(!string.isEmpty()); + QByteArray hash = QCryptographicHash::hash(string.toUtf8(), + QCryptographicHash::Sha1); + QDataStream dataStream(qToLittleEndian(hash).left(2)); + dataStream.setByteOrder(QDataStream::LittleEndian); + quint16 hashValue; + dataStream >> hashValue; + const auto hueF = qreal(hashValue) / std::numeric_limits<quint16>::max(); + Q_ASSERT((0 <= hueF) && (hueF <= 1)); + return hueF; +} + // Tests for function_traits<> #ifdef Q_CC_CLANG @@ -341,4 +341,12 @@ QString prettyPrint(const QString& plainText); * \param dir path to cache directory relative to the standard cache path */ QString cacheLocation(const QString& dirName); + +/** Hue color component of based of the hash of the string. + * The implementation is based on XEP-0392: + * https://xmpp.org/extensions/xep-0392.html + * Naming and range are the same as QColor's hueF method: + * https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision + */ +qreal stringToHueF(const QString& string); } // namespace QMatrixClient |