From 25e0e3fa8e7624a50a8e3330d3c489d60adc9a9e Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 30 Jul 2023 15:34:12 +0100 Subject: [PATCH] Remove NeoChatUser and just use Quotient::User this simplifies things and avoids casting from one to the other which address santizer didn't like --- autotests/texthandlertest.cpp | 15 ++++-- src/CMakeLists.txt | 2 - src/controller.cpp | 10 ++-- src/controller.h | 5 +- src/main.cpp | 9 ++-- src/models/locationsmodel.cpp | 2 +- src/models/locationsmodel.h | 3 +- src/models/messageeventmodel.cpp | 19 ++++---- src/models/reactionmodel.cpp | 4 +- src/models/reactionmodel.h | 9 ++-- src/models/searchmodel.cpp | 3 +- src/models/userlistmodel.cpp | 3 +- src/neochatroom.cpp | 34 +++++++++----- src/neochatroom.h | 47 ++++++++++--------- src/neochatuser.cpp | 43 ----------------- src/neochatuser.h | 39 --------------- src/qml/Component/Timeline/ReplyComponent.qml | 4 +- .../Component/Timeline/TimelineContainer.qml | 8 ++-- src/texthandler.cpp | 14 ++++-- 19 files changed, 110 insertions(+), 163 deletions(-) delete mode 100644 src/neochatuser.cpp delete mode 100644 src/neochatuser.h diff --git a/autotests/texthandlertest.cpp b/autotests/texthandlertest.cpp index 7336532d6..a6fe17005 100644 --- a/autotests/texthandlertest.cpp +++ b/autotests/texthandlertest.cpp @@ -71,6 +71,13 @@ private Q_SLOTS: void linkPreviewsReject(); }; +static QColor polishColor(qreal hueF) +{ + const auto lightness = static_cast(QGuiApplication::instance())->palette().color(QPalette::Active, QPalette::Window).lightnessF(); + // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal + return QColor::fromHslF(hueF, 1, -0.7 * lightness + 0.9, 1); +} + void TextHandlerTest::initTestCase() { connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org")); @@ -555,10 +562,10 @@ void TextHandlerTest::receiveRichPlainUrl() void TextHandlerTest::receiveRichEmote() { auto event = room->messageEvents().at(1).get(); - auto author = static_cast(room->user(event->senderId())); + auto author = room->user(event->senderId()); const QString testInputString = QStringLiteral("This is an emote."); - const QString testOutputString = QStringLiteral("* color().name() - + QStringLiteral("\">@example:example.org This is an emote."); + const QString testOutputString = QStringLiteral("* hueF()).name() + QStringLiteral("\">@example:example.org This is an emote."); TextHandler testTextHandler; testTextHandler.setData(testInputString); @@ -644,5 +651,5 @@ void TextHandlerTest::linkPreviewsReject() QCOMPARE(testTextHandler.getLinkPreviews(), testOutputLinks); } -QTEST_GUILESS_MAIN(TextHandlerTest) +QTEST_MAIN(TextHandlerTest) #include "texthandlertest.moc" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3f4a23fd5..c3a229a00 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -44,8 +44,6 @@ add_library(neochat STATIC roommanager.h neochatroom.cpp neochatroom.h - neochatuser.cpp - neochatuser.h models/userlistmodel.cpp models/userlistmodel.h models/userfiltermodel.cpp diff --git a/src/controller.cpp b/src/controller.cpp index b5a8f3dba..61d75a475 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -35,15 +35,15 @@ #include #include #include +#include #include +#include #include #include -#include -#include +#include #include "neochatconfig.h" #include "neochatroom.h" -#include "neochatuser.h" #include "notificationsmanager.h" #include "roommanager.h" #include "windowcontroller.h" @@ -60,7 +60,7 @@ Controller::Controller(QObject *parent) : QObject(parent) { Connection::setRoomType(); - Connection::setUserType(); + Connection::setUserType(); setApplicationProxy(); @@ -532,7 +532,7 @@ void Controller::joinRoom(const QString &alias) RoomManager::instance().joinRoom(m_connection, alias, QStringList{knownServer}); } -void Controller::openOrCreateDirectChat(NeoChatUser *user) +void Controller::openOrCreateDirectChat(User *user) { const auto existing = activeConnection()->directChats(); diff --git a/src/controller.h b/src/controller.h index e28dc6209..98ba7ecaf 100644 --- a/src/controller.h +++ b/src/controller.h @@ -12,9 +12,9 @@ #include #include #include +#include class NeoChatRoom; -class NeoChatUser; class TrayIcon; class QWindow; class QQuickTextDocument; @@ -23,6 +23,7 @@ namespace Quotient { class Connection; class Room; +class User; } namespace QKeychain @@ -183,7 +184,7 @@ public: * * If a direct chat with the user doesn't exist one is created and then joined. */ - Q_INVOKABLE void openOrCreateDirectChat(NeoChatUser *user); + Q_INVOKABLE void openOrCreateDirectChat(Quotient::User *user); [[nodiscard]] bool supportSystemTray() const; diff --git a/src/main.cpp b/src/main.cpp index 4b80e8841..1b02b5e8c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,10 +29,11 @@ #include "neochat-version.h" -#include #include +#include #include #include +#include #include #include "actionshandler.h" @@ -76,7 +77,6 @@ #include "models/webshortcutmodel.h" #include "neochatconfig.h" #include "neochatroom.h" -#include "neochatuser.h" #include "notificationsmanager.h" #include "pollhandler.h" #include "roommanager.h" @@ -89,7 +89,6 @@ #endif #include "models/completionmodel.h" #include "models/statemodel.h" -#include "neochatuser.h" #ifdef HAVE_RUNNER #include "runner.h" @@ -260,7 +259,7 @@ int main(int argc, char *argv[]) qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "PushNotificationState", "ENUM"); qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "PushNotificationAction", "ENUM"); qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "NeoChatRoomType", "ENUM"); - qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "NeoChatUser", {}); + qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "User", {}); qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "NeoChatRoom", {}); qRegisterMetaType("User*"); @@ -270,7 +269,7 @@ int main(int argc, char *argv[]) qRegisterMetaType("Connection*"); qRegisterMetaType("MessageEventType"); qRegisterMetaType("NeoChatRoom*"); - qRegisterMetaType("NeoChatUser*"); + qRegisterMetaType("User*"); qRegisterMetaType("GetRoomEventsJob*"); qRegisterMetaType("QMimeType"); #ifdef Quotient_E2EE_ENABLED diff --git a/src/models/locationsmodel.cpp b/src/models/locationsmodel.cpp index 92b9809e3..5a55a9f37 100644 --- a/src/models/locationsmodel.cpp +++ b/src/models/locationsmodel.cpp @@ -61,7 +61,7 @@ void LocationsModel::addLocation(const RoomMessageEvent *event) .latitude = latitude, .longitude = longitude, .content = event->contentJson(), - .author = dynamic_cast(m_room->user(event->senderId())), + .author = m_room->user(event->senderId()), }; endInsertRows(); } diff --git a/src/models/locationsmodel.h b/src/models/locationsmodel.h index 1faa7fe3f..17991c20d 100644 --- a/src/models/locationsmodel.h +++ b/src/models/locationsmodel.h @@ -10,6 +10,7 @@ #include "neochatroom.h" #include +#include class LocationsModel : public QAbstractListModel { @@ -51,7 +52,7 @@ private: float latitude; float longitude; QJsonObject content; - NeoChatUser *author; + Quotient::User *author; }; QList m_locations; void addLocation(const Quotient::RoomMessageEvent *event); diff --git a/src/models/messageeventmodel.cpp b/src/models/messageeventmodel.cpp index 68d545275..c9c982ff2 100644 --- a/src/models/messageeventmodel.cpp +++ b/src/models/messageeventmodel.cpp @@ -25,7 +25,6 @@ #include #include "models/reactionmodel.h" -#include "neochatuser.h" #include "texthandler.h" using namespace Quotient; @@ -531,7 +530,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } if (role == AuthorRole) { - auto author = static_cast(isPending ? m_currentRoom->localUser() : m_currentRoom->user(evt.senderId())); + auto author = isPending ? m_currentRoom->localUser() : m_currentRoom->user(evt.senderId()); return m_currentRoom->getUser(author); } @@ -679,7 +678,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const auto replyPtr = m_currentRoom->getReplyForEvent(evt); if (replyPtr) { - auto replyUser = static_cast(m_currentRoom->user(replyPtr->senderId())); + auto replyUser = m_currentRoom->user(replyPtr->senderId()); return m_currentRoom->getUser(replyUser); } else { return m_currentRoom->getUser(nullptr); @@ -808,7 +807,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const QVariantList users; users.reserve(userIds.size()); for (const auto &userId : userIds) { - auto user = static_cast(m_currentRoom->user(userId)); + auto user = m_currentRoom->user(userId); users += m_currentRoom->getUser(user); } @@ -836,7 +835,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const */ QString readMarkersString = i18np("1 user: ", "%1 users: ", userIds.size()); for (const auto &userId : userIds) { - auto user = static_cast(m_currentRoom->user(userId)); + auto user = m_currentRoom->user(userId); readMarkersString += user->displayname(m_currentRoom) + i18nc("list separator", ", "); } readMarkersString.chop(2); @@ -877,7 +876,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } if (role == DisplayNameForInitialsRole) { - auto user = static_cast(isPending ? m_currentRoom->localUser() : m_currentRoom->user(evt.senderId())); + auto user = isPending ? m_currentRoom->localUser() : m_currentRoom->user(evt.senderId()); return user->displayname(m_currentRoom).remove(QStringLiteral(" (%1)").arg(user->id())); } @@ -889,7 +888,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const } return previousDisplayName; } else { - auto author = static_cast(isPending ? m_currentRoom->localUser() : m_currentRoom->user(evt.senderId())); + auto author = isPending ? m_currentRoom->localUser() : m_currentRoom->user(evt.senderId()); return m_currentRoom->htmlSafeMemberName(author->id()); } } @@ -1062,13 +1061,13 @@ void MessageEventModel::createReactionModelForEvent(const Quotient::RoomMessageE return; }; - QMap> reactions = {}; + QMap> reactions = {}; for (const auto &a : annotations) { if (a->isRedacted()) { // Just in case? continue; } if (const auto &e = eventCast(a)) { - reactions[e->key()].append(static_cast(m_currentRoom->user(e->senderId()))); + reactions[e->key()].append(m_currentRoom->user(e->senderId())); } } @@ -1095,7 +1094,7 @@ void MessageEventModel::createReactionModelForEvent(const Quotient::RoomMessageE if (m_reactionModels.contains(eventId)) { m_reactionModels[eventId]->setReactions(res); } else if (res.size() > 0) { - m_reactionModels[eventId] = new ReactionModel(this, res, static_cast(m_currentRoom->localUser())); + m_reactionModels[eventId] = new ReactionModel(this, res, m_currentRoom->localUser()); } else { if (m_reactionModels.contains(eventId)) { delete m_reactionModels[eventId]; diff --git a/src/models/reactionmodel.cpp b/src/models/reactionmodel.cpp index 56ecaa535..745f1a014 100644 --- a/src/models/reactionmodel.cpp +++ b/src/models/reactionmodel.cpp @@ -7,9 +7,9 @@ #include -#include "neochatuser.h" +#include -ReactionModel::ReactionModel(QObject *parent, QList reactions, NeoChatUser *localUser) +ReactionModel::ReactionModel(QObject *parent, QList reactions, Quotient::User *localUser) : QAbstractListModel(parent) , m_localUser(localUser) { diff --git a/src/models/reactionmodel.h b/src/models/reactionmodel.h index c866bd46a..d99d2235b 100644 --- a/src/models/reactionmodel.h +++ b/src/models/reactionmodel.h @@ -5,7 +5,10 @@ #include -class NeoChatUser; +namespace Quotient +{ +class User; +} /** * @class ReactionModel @@ -36,7 +39,7 @@ public: HasLocalUser, /**< Whether the local user is in the list of authors. */ }; - explicit ReactionModel(QObject *parent = nullptr, QList reactions = {}, NeoChatUser *localUser = nullptr); + explicit ReactionModel(QObject *parent = nullptr, QList reactions = {}, Quotient::User *localUser = nullptr); /** * @brief Get the given role value at the given index. @@ -67,6 +70,6 @@ public: private: QList m_reactions; - NeoChatUser *m_localUser; + Quotient::User *m_localUser; }; Q_DECLARE_METATYPE(ReactionModel *) diff --git a/src/models/searchmodel.cpp b/src/models/searchmodel.cpp index 54beda9ad..68e1523d1 100644 --- a/src/models/searchmodel.cpp +++ b/src/models/searchmodel.cpp @@ -5,7 +5,6 @@ #include "messageeventmodel.h" #include "neochatroom.h" -#include "neochatuser.h" #include #include @@ -109,7 +108,7 @@ QVariant SearchModel::data(const QModelIndex &index, int role) const return false; case ReplyAuthorRole: if (const auto &replyPtr = m_room->getReplyForEvent(event)) { - return m_room->getUser(static_cast(m_room->user(replyPtr->senderId()))); + return m_room->getUser(m_room->user(replyPtr->senderId())); } else { return m_room->getUser(nullptr); } diff --git a/src/models/userlistmodel.cpp b/src/models/userlistmodel.cpp index a4c41ac60..2398e5aae 100644 --- a/src/models/userlistmodel.cpp +++ b/src/models/userlistmodel.cpp @@ -71,8 +71,7 @@ QVariant UserListModel::data(const QModelIndex &index, int role) const return user->id(); } if (role == AvatarRole) { - auto neoChatUser = static_cast(user); - return m_currentRoom->avatarForMember(neoChatUser); + return m_currentRoom->avatarForMember(user); } if (role == ObjectRole) { return QVariant::fromValue(user); diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 7ecfb0387..caec0f83b 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -4,8 +4,10 @@ #include "neochatroom.h" #include +#include #include #include +#include #include #include @@ -13,6 +15,7 @@ #include #include +#include #include #include @@ -405,6 +408,13 @@ QDateTime NeoChatRoom::lastActiveTime() return messageEvents().rbegin()->get()->originTimestamp(); } +static QColor polishColor(qreal hueF) +{ + const auto lightness = static_cast(QGuiApplication::instance())->palette().color(QPalette::Active, QPalette::Window).lightnessF(); + // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal + return QColor::fromHslF(hueF, 1, -0.7 * lightness + 0.9, 1); +} + QVariantList NeoChatRoom::getUsers(const QString &keyword, int limit) const { const auto userList = users(); @@ -412,11 +422,11 @@ QVariantList NeoChatRoom::getUsers(const QString &keyword, int limit) const int count = 0; for (const auto u : userList) { if (u->displayname(this).contains(keyword, Qt::CaseInsensitive)) { - NeoChatUser user(u->id(), u->connection()); + Quotient::User user(u->id(), u->connection()); QVariantMap userVariant{{QStringLiteral("id"), user.id()}, {QStringLiteral("displayName"), user.displayname(this)}, {QStringLiteral("avatarMediaId"), user.avatarMediaId(this)}, - {QStringLiteral("color"), user.color()}}; + {QStringLiteral("color"), polishColor(user.hueF())}}; matchedList.append(QVariant::fromValue(userVariant)); count++; @@ -442,11 +452,10 @@ static const QVariantMap emptyUser = { QVariantMap NeoChatRoom::getUser(const QString &userID) const { - NeoChatUser *userObject = static_cast(user(userID)); - return getUser(userObject); + return getUser(user(userID)); } -QVariantMap NeoChatRoom::getUser(NeoChatUser *user) const +QVariantMap NeoChatRoom::getUser(User *user) const { if (user == nullptr) { return emptyUser; @@ -458,7 +467,7 @@ QVariantMap NeoChatRoom::getUser(NeoChatUser *user) const {QStringLiteral("displayName"), user->displayname(this)}, {QStringLiteral("avatarSource"), avatarForMember(user)}, {QStringLiteral("avatarMediaId"), user->avatarMediaId(this)}, - {QStringLiteral("color"), user->color()}, + {QStringLiteral("color"), polishColor(user->hueF())}, {QStringLiteral("object"), QVariant::fromValue(user)}, }; } @@ -539,7 +548,7 @@ QString NeoChatRoom::eventToString(const RoomEvent &evt, Qt::TextFormat format, if (prettyPrint) { subjectName = QStringLiteral("%3") - .arg(e.userId(), static_cast(user(e.userId()))->color().name(), subjectName); + .arg(e.userId(), polishColor(user(e.userId())->hueF()).name(), subjectName); } // The below code assumes senderName output in AuthorRole @@ -1682,7 +1691,7 @@ QVariantMap NeoChatRoom::chatBoxReplyUser() const if (m_chatBoxReplyId.isEmpty()) { return emptyUser; } - return getUser(static_cast(user((*findInTimeline(m_chatBoxReplyId))->senderId()))); + return getUser(user((*findInTimeline(m_chatBoxReplyId))->senderId())); } QString NeoChatRoom::chatBoxReplyMessage() const @@ -1698,7 +1707,7 @@ QVariantMap NeoChatRoom::chatBoxEditUser() const if (m_chatBoxEditId.isEmpty()) { return emptyUser; } - return getUser(static_cast(user((*findInTimeline(m_chatBoxEditId))->senderId()))); + return getUser(user((*findInTimeline(m_chatBoxEditId))->senderId())); } QString NeoChatRoom::chatBoxEditMessage() const @@ -1904,9 +1913,10 @@ int NeoChatRoom::maxRoomVersion() const } return maxVersion; } -NeoChatUser *NeoChatRoom::directChatRemoteUser() const + +Quotient::User *NeoChatRoom::directChatRemoteUser() const { - return dynamic_cast(connection()->directChatUsers(this)[0]); + return connection()->directChatUsers(this)[0]; } void NeoChatRoom::sendLocation(float lat, float lon, const QString &description) @@ -1938,7 +1948,7 @@ QByteArray NeoChatRoom::roomAcountDataJson(const QString &eventType) return QJsonDocument(accountData(eventType)->fullJson()).toJson(); } -QUrl NeoChatRoom::avatarForMember(NeoChatUser *user) const +QUrl NeoChatRoom::avatarForMember(Quotient::User *user) const { const auto &url = memberAvatarUrl(user->id()); if (url.isEmpty() || url.scheme() != "mxc"_ls) { diff --git a/src/neochatroom.h b/src/neochatroom.h index 392151065..44481881b 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -10,10 +10,15 @@ #include #include +#include -#include "neochatuser.h" #include "pollhandler.h" +namespace Quotient +{ +class User; +} + class PushNotificationState : public QObject { Q_OBJECT @@ -64,7 +69,7 @@ class NeoChatRoom : public Quotient::Room * * The list does not include the local user. * - * This is different to getting a list of NeoChatUser objects or Quotient::User objects + * This is different to getting a list of Quotient::User objects * as neither of those can provide details like the displayName or avatarMediaId * without the room context as these can vary from room to room. This function * provides the room context and puts the result as a list of QVariantMap objects. @@ -76,7 +81,7 @@ class NeoChatRoom : public Quotient::Room * - displayName - Display name in the context of this room. * - display - Name in the context of this room. * - * @sa Quotient::User, NeoChatUser + * @sa Quotient::User */ Q_PROPERTY(QVariantList usersTyping READ getUsersTyping NOTIFY typingChanged) @@ -119,7 +124,7 @@ class NeoChatRoom : public Quotient::Room /** * @brief Get a user object for the other person in a direct chat. */ - Q_PROPERTY(NeoChatUser *directChatRemoteUser READ directChatRemoteUser CONSTANT) + Q_PROPERTY(Quotient::User *directChatRemoteUser READ directChatRemoteUser CONSTANT) /** * @brief If the room is a space. @@ -316,7 +321,7 @@ class NeoChatRoom : public Quotient::Room /** * @brief Get the user for the message being replied to. * - * This is different to getting a NeoChatUser object or Quotient::User object + * This is different to getting a Quotient::User object * as neither of those can provide details like the displayName or avatarMediaId * without the room context as these can vary from room to room. * @@ -329,9 +334,9 @@ class NeoChatRoom : public Quotient::Room * - avatarSource - The mxc URL for the user's avatar in the current room. * - avatarMediaId - Avatar id in the context of this room. * - color - Color for the user. - * - object - The NeoChatUser object for the user. + * - object - The Quotient::User object for the user. * - * @sa getUser, Quotient::User, NeoChatUser + * @sa getUser, Quotient::User */ Q_PROPERTY(QVariantMap chatBoxReplyUser READ chatBoxReplyUser NOTIFY chatBoxReplyIdChanged) @@ -345,7 +350,7 @@ class NeoChatRoom : public Quotient::Room /** * @brief Get the user for the message being edited. * - * This is different to getting a NeoChatUser object or Quotient::User object + * This is different to getting a Quotient::User object * as neither of those can provide details like the displayName or avatarMediaId * without the room context as these can vary from room to room. * @@ -358,9 +363,9 @@ class NeoChatRoom : public Quotient::Room * - avatarSource - The mxc URL for the user's avatar in the current room. * - avatarMediaId - Avatar id in the context of this room. * - color - Color for the user. - * - object - The NeoChatUser object for the user. + * - object - The Quotient::User object for the user. * - * @sa getUser, Quotient::User, NeoChatUser + * @sa getUser, Quotient::User */ Q_PROPERTY(QVariantMap chatBoxEditUser READ chatBoxEditUser NOTIFY chatBoxEditIdChanged) @@ -392,7 +397,7 @@ public: /** * @brief Get a list of users in the context of this room. * - * This is different to getting a list of NeoChatUser objects or Quotient::User objects + * This is different to getting a list of Quotient::User objects * as neither of those can provide details like the displayName or avatarMediaId * without the room context as these can vary from room to room. This function * provides the room context and returns the result as a list of QVariantMap objects. @@ -407,14 +412,14 @@ public: * - avatarMediaId - Avatar id in the context of this room. * - color - Color for the user. * - * @sa Quotient::User, NeoChatUser + * @sa Quotient::User */ Q_INVOKABLE [[nodiscard]] QVariantList getUsers(const QString &keyword, int limit = -1) const; /** * @brief Get a user in the context of this room. * - * This is different to getting a NeoChatUser object or Quotient::User object + * This is different to getting a Quotient::User object * as neither of those can provide details like the displayName or avatarMediaId * without the room context as these can vary from room to room. This function * provides the room context and outputs the result as QVariantMap. @@ -431,16 +436,16 @@ public: * - avatarSource - The mxc URL for the user's avatar in the current room. * - avatarMediaId - Avatar id in the context of this room. * - color - Color for the user. - * - object - The NeoChatUser object for the user. + * - object - The Quotient::User object for the user. * - * @sa Quotient::User, NeoChatUser + * @sa Quotient::User */ Q_INVOKABLE [[nodiscard]] QVariantMap getUser(const QString &userID) const; /** * @brief Get a user in the context of this room. * - * This is different to getting a NeoChatUser object or Quotient::User object + * This is different to getting a Quotient::User object * as neither of those can provide details like the displayName or avatarMediaId * without the room context as these can vary from room to room. This function * provides the room context and outputs the result as QVariantMap. @@ -457,11 +462,11 @@ public: * - avatarSource - The mxc URL for the user's avatar in the current room. * - avatarMediaId - Avatar id in the context of this room. * - color - Color for the user. - * - object - The NeoChatUser object for the user. + * - object - The Quotient::User object for the user. * - * @sa Quotient::User, NeoChatUser + * @sa Quotient::User */ - Q_INVOKABLE [[nodiscard]] QVariantMap getUser(NeoChatUser *user) const; + Q_INVOKABLE [[nodiscard]] QVariantMap getUser(Quotient::User *user) const; [[nodiscard]] QVariantList getUsersTyping() const; @@ -602,7 +607,7 @@ public: [[nodiscard]] QString avatarMediaId() const; - NeoChatUser *directChatRemoteUser() const; + Quotient::User *directChatRemoteUser() const; [[nodiscard]] bool isSpace(); @@ -818,7 +823,7 @@ public: */ Q_INVOKABLE QByteArray roomAcountDataJson(const QString &eventType); - Q_INVOKABLE [[nodiscard]] QUrl avatarForMember(NeoChatUser *user) const; + Q_INVOKABLE [[nodiscard]] QUrl avatarForMember(Quotient::User *user) const; /** * @brief Returns the event that is being replied to. This includes events that were manually loaded using NeoChatRoom::loadReply. diff --git a/src/neochatuser.cpp b/src/neochatuser.cpp deleted file mode 100644 index 1acb3aa76..000000000 --- a/src/neochatuser.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// SPDX-FileCopyrightText: 2019 Black Hat -// SPDX-License-Identifier: GPL-3.0-only - -#include "neochatuser.h" - -#include -#include -#include - -#include - -using namespace Quotient; - -NeoChatUser::NeoChatUser(QString userId, Connection *connection) - : User(std::move(userId), connection) -{ - connect(static_cast(QGuiApplication::instance()), &QGuiApplication::paletteChanged, this, &NeoChatUser::polishColor); - polishColor(); -} - -QColor NeoChatUser::color() -{ - return m_color; -} - -void NeoChatUser::setColor(const QColor &color) -{ - if (m_color == color) { - return; - } - - m_color = color; - Q_EMIT colorChanged(m_color); -} - -void NeoChatUser::polishColor() -{ - const auto lightness = static_cast(QGuiApplication::instance())->palette().color(QPalette::Active, QPalette::Window).lightnessF(); - // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal - setColor(QColor::fromHslF(hueF(), 1, -0.7 * lightness + 0.9, 1)); -} - -#include "moc_neochatuser.cpp" diff --git a/src/neochatuser.h b/src/neochatuser.h deleted file mode 100644 index c9e96d1d9..000000000 --- a/src/neochatuser.h +++ /dev/null @@ -1,39 +0,0 @@ -// SPDX-FileCopyrightText: 2018-2019 Black Hat -// SPDX-License-Identifier: GPL-3.0-only - -#pragma once - -#include - -#include - -/** - * @class NeoChatUser - * - * A class inherited from Quotient::User to add a user color function. - * - * @sa Quotient::User - */ -class NeoChatUser : public Quotient::User -{ - Q_OBJECT - - /** - * @brief The color to use for the user. - */ - Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) -public: - explicit NeoChatUser(QString userId, Quotient::Connection *connection); - -public Q_SLOTS: - QColor color(); - void setColor(const QColor &color); - -Q_SIGNALS: - void colorChanged(QColor _t1); - -private: - QColor m_color; - - void polishColor(); -}; diff --git a/src/qml/Component/Timeline/ReplyComponent.qml b/src/qml/Component/Timeline/ReplyComponent.qml index 708098024..43d0e8abd 100644 --- a/src/qml/Component/Timeline/ReplyComponent.qml +++ b/src/qml/Component/Timeline/ReplyComponent.qml @@ -34,9 +34,9 @@ Item { * - displayName - The display name of the reply author. * - display - The name of the reply author. * - color - The color for the reply author. - * - object - The NeoChatUser object for the reply author. + * - object - The Quotient::User object for the reply author. * - * @sa NeoChatUser + * @sa Quotient::User */ required property var author diff --git a/src/qml/Component/Timeline/TimelineContainer.qml b/src/qml/Component/Timeline/TimelineContainer.qml index d61e55d19..5b58b13af 100644 --- a/src/qml/Component/Timeline/TimelineContainer.qml +++ b/src/qml/Component/Timeline/TimelineContainer.qml @@ -54,9 +54,9 @@ ColumnLayout { * - displayName - The display name of the author. * - display - The name of the author. * - color - The color for the author. - * - object - The NeoChatUser object for the author. + * - object - The Quotient::User object for the author. * - * @sa NeoChatUser + * @sa Quotient::User */ required property var author @@ -137,9 +137,9 @@ ColumnLayout { * - displayName - The display name of the reply author. * - display - The name of the reply author. * - color - The color for the reply author. - * - object - The NeoChatUser object for the reply author. + * - object - The Quotient::User object for the reply author. * - * @sa NeoChatUser + * @sa Quotient::User */ required property var replyAuthor diff --git a/src/texthandler.cpp b/src/texthandler.cpp index a7bec3399..9355fd1a5 100644 --- a/src/texthandler.cpp +++ b/src/texthandler.cpp @@ -4,8 +4,9 @@ #include "texthandler.h" #include -#include +#include #include +#include #include #include @@ -76,6 +77,13 @@ QString TextHandler::handleSendText() return outputString; } +static QColor polishColor(qreal hueF) +{ + const auto lightness = static_cast(QGuiApplication::instance())->palette().color(QPalette::Active, QPalette::Window).lightnessF(); + // https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal + return QColor::fromHslF(hueF, 1, -0.7 * lightness + 0.9, 1); +} + QString TextHandler::handleRecieveRichText(Qt::TextFormat inputFormat, const NeoChatRoom *room, const Quotient::RoomEvent *event, bool stripNewlines) { m_pos = 0; @@ -135,9 +143,9 @@ QString TextHandler::handleRecieveRichText(Qt::TextFormat inputFormat, const Neo if (event != nullptr) { auto e = eventCast(event); if (e->msgtype() == Quotient::MessageEventType::Emote) { - auto author = static_cast(room->user(e->senderId())); + auto author = room->user(e->senderId()); QString emoteString = QStringLiteral("* senderId() + QStringLiteral("\" style=\"color:") - + author->color().name() + QStringLiteral("\">") + author->displayname(room) + QStringLiteral(" "); + + polishColor(author->hueF()).name() + QStringLiteral("\">") + author->displayname(room) + QStringLiteral(" "); if (outputString.startsWith(QStringLiteral("

"))) { outputString.insert(3, emoteString); } else {