Code cleanup.

This commit is contained in:
Black Hat 2018-07-30 00:04:58 +08:00
parent 93a303799a
commit 4ce12331d1
2 changed files with 44 additions and 50 deletions

View File

@ -10,7 +10,7 @@ RoomListModel::RoomListModel(QObject* parent) : QAbstractListModel(parent) {}
RoomListModel::~RoomListModel() {} RoomListModel::~RoomListModel() {}
void RoomListModel::setConnection(QMatrixClient::Connection* connection) { void RoomListModel::setConnection(Connection* connection) {
Q_ASSERT(connection); Q_ASSERT(connection);
using QMatrixClient::Room; using QMatrixClient::Room;
@ -18,15 +18,14 @@ void RoomListModel::setConnection(QMatrixClient::Connection* connection) {
if (!connection->accessToken().isEmpty()) doResetModel(); if (!connection->accessToken().isEmpty()) doResetModel();
connect(connection, &QMatrixClient::Connection::connected, this, connect(connection, &Connection::connected, this,
&RoomListModel::doResetModel); &RoomListModel::doResetModel);
connect(connection, &QMatrixClient::Connection::invitedRoom, this, connect(connection, &Connection::invitedRoom, this,
&RoomListModel::updateRoom); &RoomListModel::updateRoom);
connect(connection, &QMatrixClient::Connection::joinedRoom, this, connect(connection, &Connection::joinedRoom, this,
&RoomListModel::updateRoom); &RoomListModel::updateRoom);
connect(connection, &QMatrixClient::Connection::leftRoom, this, connect(connection, &Connection::leftRoom, this, &RoomListModel::updateRoom);
&RoomListModel::updateRoom); connect(connection, &Connection::aboutToDeleteRoom, this,
connect(connection, &QMatrixClient::Connection::aboutToDeleteRoom, this,
&RoomListModel::deleteRoom); &RoomListModel::deleteRoom);
} }
@ -37,9 +36,9 @@ void RoomListModel::doResetModel() {
endResetModel(); endResetModel();
} }
QMatrixClient::Room* RoomListModel::roomAt(int row) { return m_rooms.at(row); } Room* RoomListModel::roomAt(int row) { return m_rooms.at(row); }
void RoomListModel::doAddRoom(QMatrixClient::Room* r) { void RoomListModel::doAddRoom(Room* r) {
if (auto* room = r) { if (auto* room = r) {
m_rooms.append(room); m_rooms.append(room);
connectRoomSignals(room); connectRoomSignals(room);
@ -50,24 +49,20 @@ void RoomListModel::doAddRoom(QMatrixClient::Room* r) {
} }
} }
void RoomListModel::connectRoomSignals(QMatrixClient::Room* room) { void RoomListModel::connectRoomSignals(Room* room) {
connect(room, &QMatrixClient::Room::displaynameChanged, this, connect(room, &Room::displaynameChanged, this, [=] { namesChanged(room); });
[=] { namesChanged(room); }); connect(room, &Room::unreadMessagesChanged, this,
connect(room, &QMatrixClient::Room::unreadMessagesChanged, this,
[=] { unreadMessagesChanged(room); }); [=] { unreadMessagesChanged(room); });
connect(room, &QMatrixClient::Room::notificationCountChanged, this, connect(room, &Room::notificationCountChanged, this,
[=] { unreadMessagesChanged(room); }); [=] { unreadMessagesChanged(room); });
connect(room, &QMatrixClient::Room::tagsChanged, this, connect(room, &Room::tagsChanged, this, [=] { refresh(room); });
[=] { refresh(room); }); connect(room, &Room::joinStateChanged, this, [=] { refresh(room); });
connect(room, &QMatrixClient::Room::joinStateChanged, this, connect(room, &Room::avatarChanged, this,
[=] { refresh(room); });
connect(room, &QMatrixClient::Room::avatarChanged, this,
[=] { refresh(room, {AvatarRole}); }); [=] { refresh(room, {AvatarRole}); });
connect(room, &QMatrixClient::Room::unreadMessagesChanged, this, connect(room, &Room::unreadMessagesChanged, this, [=](Room* r) {
[=](QMatrixClient::Room* r) { if (r->hasUnreadMessages()) emit newMessage(r);
if (r->hasUnreadMessages()) emit newMessage(r); });
});
// connect( // connect(
// room, &QMatrixClient::Room::aboutToAddNewMessages, this, // room, &QMatrixClient::Room::aboutToAddNewMessages, this,
// [=](QMatrixClient::RoomEventsRange eventsRange) { // [=](QMatrixClient::RoomEventsRange eventsRange) {
@ -80,15 +75,14 @@ void RoomListModel::connectRoomSignals(QMatrixClient::Room* room) {
// }); // });
} }
void RoomListModel::updateRoom(QMatrixClient::Room* room, void RoomListModel::updateRoom(Room* room, Room* prev) {
QMatrixClient::Room* prev) {
// There are two cases when this method is called: // There are two cases when this method is called:
// 1. (prev == nullptr) adding a new room to the room list // 1. (prev == nullptr) adding a new room to the room list
// 2. (prev != nullptr) accepting/rejecting an invitation or inviting to // 2. (prev != nullptr) accepting/rejecting an invitation or inviting to
// the previously left room (in both cases prev has the previous state). // the previously left room (in both cases prev has the previous state).
if (prev == room) { if (prev == room) {
qCritical() << "RoomListModel::updateRoom: room tried to replace itself"; qCritical() << "RoomListModel::updateRoom: room tried to replace itself";
refresh(static_cast<QMatrixClient::Room*>(room)); refresh(static_cast<Room*>(room));
return; return;
} }
if (prev && room->id() != prev->id()) { if (prev && room->id() != prev->id()) {
@ -98,9 +92,9 @@ void RoomListModel::updateRoom(QMatrixClient::Room* room,
} }
// Ok, we're through with pre-checks, now for the real thing. // Ok, we're through with pre-checks, now for the real thing.
auto* newRoom = room; auto* newRoom = room;
const auto it = std::find_if( const auto it =
m_rooms.begin(), m_rooms.end(), std::find_if(m_rooms.begin(), m_rooms.end(),
[=](const QMatrixClient::Room* r) { return r == prev || r == newRoom; }); [=](const Room* r) { return r == prev || r == newRoom; });
if (it != m_rooms.end()) { if (it != m_rooms.end()) {
const int row = it - m_rooms.begin(); const int row = it - m_rooms.begin();
// There's no guarantee that prev != newRoom // There's no guarantee that prev != newRoom
@ -117,7 +111,7 @@ void RoomListModel::updateRoom(QMatrixClient::Room* room,
} }
} }
void RoomListModel::deleteRoom(QMatrixClient::Room* room) { void RoomListModel::deleteRoom(Room* room) {
qDebug() << "Deleting room" << room->id(); qDebug() << "Deleting room" << room->id();
const auto it = std::find(m_rooms.begin(), m_rooms.end(), room); const auto it = std::find(m_rooms.begin(), m_rooms.end(), room);
if (it == m_rooms.end()) return; // Already deleted, nothing to do if (it == m_rooms.end()) return; // Already deleted, nothing to do
@ -140,7 +134,7 @@ QVariant RoomListModel::data(const QModelIndex& index, int role) const {
qDebug() << "UserListModel: something wrong here..."; qDebug() << "UserListModel: something wrong here...";
return QVariant(); return QVariant();
} }
QMatrixClient::Room* room = m_rooms.at(index.row()); Room* room = m_rooms.at(index.row());
if (role == NameRole) { if (role == NameRole) {
return room->displayName(); return room->displayName();
} }
@ -168,13 +162,12 @@ QVariant RoomListModel::data(const QModelIndex& index, int role) const {
return QVariant(); return QVariant();
} }
void RoomListModel::namesChanged(QMatrixClient::Room* room) { void RoomListModel::namesChanged(Room* room) {
int row = m_rooms.indexOf(room); int row = m_rooms.indexOf(room);
emit dataChanged(index(row), index(row)); emit dataChanged(index(row), index(row));
} }
void RoomListModel::refresh(QMatrixClient::Room* room, void RoomListModel::refresh(Room* room, const QVector<int>& roles) {
const QVector<int>& roles) {
const auto it = std::find(m_rooms.begin(), m_rooms.end(), room); const auto it = std::find(m_rooms.begin(), m_rooms.end(), room);
if (it == m_rooms.end()) { if (it == m_rooms.end()) {
qCritical() << "Room" << room->id() << "not found in the room list"; qCritical() << "Room" << room->id() << "not found in the room list";
@ -184,7 +177,7 @@ void RoomListModel::refresh(QMatrixClient::Room* room,
emit dataChanged(idx, idx, roles); emit dataChanged(idx, idx, roles);
} }
void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room) { void RoomListModel::unreadMessagesChanged(Room* room) {
int row = m_rooms.indexOf(room); int row = m_rooms.indexOf(room);
emit dataChanged(index(row), index(row)); emit dataChanged(index(row), index(row));
} }

View File

@ -5,10 +5,11 @@
#include "connection.h" #include "connection.h"
#include "room.h" #include "room.h"
using namespace QMatrixClient;
class RoomListModel : public QAbstractListModel { class RoomListModel : public QAbstractListModel {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QMatrixClient::Connection* connection READ getConnection WRITE Q_PROPERTY(Connection* connection READ getConnection WRITE setConnection)
setConnection)
public: public:
enum EventRoles { enum EventRoles {
@ -22,11 +23,11 @@ class RoomListModel : public QAbstractListModel {
RoomListModel(QObject* parent = 0); RoomListModel(QObject* parent = 0);
virtual ~RoomListModel(); virtual ~RoomListModel();
QMatrixClient::Connection* getConnection() { return m_connection; } Connection* getConnection() { return m_connection; }
void setConnection(QMatrixClient::Connection* connection); void setConnection(Connection* connection);
void doResetModel(); void doResetModel();
Q_INVOKABLE QMatrixClient::Room* roomAt(int row); Q_INVOKABLE Room* roomAt(int row);
QVariant data(const QModelIndex& index, QVariant data(const QModelIndex& index,
int role = Qt::DisplayRole) const override; int role = Qt::DisplayRole) const override;
@ -36,22 +37,22 @@ class RoomListModel : public QAbstractListModel {
QHash<int, QByteArray> roleNames() const; QHash<int, QByteArray> roleNames() const;
private slots: private slots:
void namesChanged(QMatrixClient::Room* room); void namesChanged(Room* room);
void unreadMessagesChanged(QMatrixClient::Room* room); void unreadMessagesChanged(Room* room);
void doAddRoom(QMatrixClient::Room* room); void doAddRoom(Room* room);
void updateRoom(QMatrixClient::Room* room, QMatrixClient::Room* prev); void updateRoom(Room* room, Room* prev);
void deleteRoom(QMatrixClient::Room* room); void deleteRoom(Room* room);
void refresh(QMatrixClient::Room* room, const QVector<int>& roles = {}); void refresh(Room* room, const QVector<int>& roles = {});
private: private:
QMatrixClient::Connection* m_connection = nullptr; Connection* m_connection = nullptr;
QList<QMatrixClient::Room*> m_rooms; QList<Room*> m_rooms;
void connectRoomSignals(QMatrixClient::Room* room); void connectRoomSignals(Room* room);
signals: signals:
void connectionChanged(); void connectionChanged();
void newMessage(QMatrixClient::Room* room); void newMessage(Room* room);
}; };
#endif // ROOMLISTMODEL_H #endif // ROOMLISTMODEL_H