From 416460b8da337bca3f12fb8bdc00371b2c56481d Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 1 Nov 2016 14:03:54 +0900 Subject: Moved MemberNameSorter from Quaternion to lib This code is useful for any client that uses the Room class and needs to display the list of room members. Also removed an unused #include. --- room.cpp | 17 ++++++++++++++++- room.h | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/room.cpp b/room.cpp index 6fad463b..85ab1720 100644 --- a/room.cpp +++ b/room.cpp @@ -28,7 +28,6 @@ #include "connection.h" #include "state.h" #include "user.h" -#include "events/event.h" #include "events/roommessageevent.h" #include "events/roomnameevent.h" #include "events/roomaliasesevent.h" @@ -677,3 +676,19 @@ void Room::Private::updateDisplayname() if (old_name != displayname) emit q->displaynameChanged(q); } + +MemberSorter Room::memberSorter() const +{ + return MemberSorter(this); +} + +bool MemberSorter::operator()(User *u1, User *u2) const +{ + auto n1 = room->roomMembername(u1); + auto n2 = room->roomMembername(u2); + if (n1[0] == '@') + n1.remove(0, 1); + if (n2[0] == '@') + n2.remove(0, 1); + return n1 < n2; +} diff --git a/room.h b/room.h index 38435e95..79c765ba 100644 --- a/room.h +++ b/room.h @@ -32,6 +32,7 @@ namespace QMatrixClient class Event; class Connection; class User; + class MemberSorter; class Room: public QObject { @@ -92,6 +93,8 @@ namespace QMatrixClient Q_INVOKABLE int highlightCount() const; Q_INVOKABLE void resetHighlightCount(); + MemberSorter memberSorter() const; + public slots: void getPreviousContent(); void userRenamed(User* user, QString oldName); @@ -138,6 +141,24 @@ namespace QMatrixClient void setLastReadEvent(User* user, QString eventId); }; + + class MemberSorter + { + public: + MemberSorter(const Room* r) : room(r) { } + + bool operator()(User* u1, User* u2) const; + + template + typename ContT::size_type lowerBoundIndex(const ContT& c, + typename ContT::value_type v) const + { + return std::lower_bound(c.begin(), c.end(), v, *this) - c.begin(); + } + + private: + const Room* room; + }; } #endif // QMATRIXCLIENT_ROOM_H -- cgit v1.2.3