Clean up code.

This commit is contained in:
Black Hat 2018-03-06 19:11:39 +08:00
parent b1d7840882
commit 788b17e06f
6 changed files with 104 additions and 103 deletions

View File

@ -19,61 +19,61 @@ class Controller : public QObject
Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged) Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged)
Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged) Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged)
public: public:
explicit Controller(QObject *parent = nullptr); explicit Controller(QObject *parent = nullptr);
~Controller(); ~Controller();
// All the Q_INVOKABLEs. // All the Q_INVOKABLEs.
Q_INVOKABLE void login(QString, QString, QString); Q_INVOKABLE void login(QString, QString, QString);
Q_INVOKABLE void logout(); Q_INVOKABLE void logout();
// All the non-Q_INVOKABLE functions. // All the non-Q_INVOKABLE functions.
// All the Q_PROPERTYs. // All the Q_PROPERTYs.
QMatrixClient::Connection* m_connection; QMatrixClient::Connection* m_connection;
QMatrixClient::Connection* getConnection() { return m_connection; } QMatrixClient::Connection* getConnection() { return m_connection; }
void setConnection(QMatrixClient::Connection* conn); void setConnection(QMatrixClient::Connection* conn);
bool isLogin = false; bool isLogin = false;
bool getIsLogin() { return isLogin; } bool getIsLogin() { return isLogin; }
void setIsLogin(bool n) { void setIsLogin(bool n) {
if(n != isLogin) { if(n != isLogin) {
isLogin = n; isLogin = n;
emit isLoginChanged(); emit isLoginChanged();
}
} }
}
QString userID; QString userID;
QString getUserID() { return userID; } QString getUserID() { return userID; }
void setUserID(QString n) { void setUserID(QString n) {
if(n != userID) { if(n != userID) {
userID = n; userID = n;
emit userIDChanged(); emit userIDChanged();
}
} }
}
QByteArray token; QByteArray token;
QByteArray getToken() { return token; } QByteArray getToken() { return token; }
void setToken(QByteArray n) { void setToken(QByteArray n) {
if(n != token) { if(n != token) {
token = n; token = n;
emit tokenChanged(); emit tokenChanged();
}
} }
}
private: private:
void connected(); void connected();
void resync(); void resync();
void reconnect(); void reconnect();
signals: signals:
void connectionChanged(); void connectionChanged();
void isLoginChanged(); void isLoginChanged();
void userIDChanged(); void userIDChanged();
void tokenChanged(); void tokenChanged();
void homeServerChanged(); void homeServerChanged();
public slots: public slots:
}; };
#endif // CONTROLLER_H #endif // CONTROLLER_H

View File

@ -57,7 +57,7 @@ QImage ImageProvider::requestImage(const QString& id,
condition.wait(&m_lock); condition.wait(&m_lock);
} }
if( pSize != nullptr ) if(pSize != nullptr)
*pSize = result.size(); *pSize = result.size();
return result; return result;

View File

@ -9,8 +9,8 @@ MatriqueRoom::MatriqueRoom(Connection* connection, QString roomId,
JoinState joinState) JoinState joinState)
: Room(connection, roomId, joinState) : Room(connection, roomId, joinState)
{ {
connect( this, &MatriqueRoom::notificationCountChanged, this, &MatriqueRoom::countChanged ); connect(this, &MatriqueRoom::notificationCountChanged, this, &MatriqueRoom::countChanged);
connect( this, &MatriqueRoom::highlightCountChanged, this, &MatriqueRoom::countChanged ); connect(this, &MatriqueRoom::highlightCountChanged, this, &MatriqueRoom::countChanged);
} }
const QString& MatriqueRoom::cachedInput() const const QString& MatriqueRoom::cachedInput() const
@ -58,7 +58,7 @@ void MatriqueRoom::saveViewport(int topIndex, int bottomIndex)
void MatriqueRoom::countChanged() void MatriqueRoom::countChanged()
{ {
if( displayed() && !hasUnreadMessages() ) if(displayed() && !hasUnreadMessages())
{ {
resetNotificationCount(); resetNotificationCount();
resetHighlightCount(); resetHighlightCount();

View File

@ -43,14 +43,14 @@ void MessageEventModel::changeRoom(MatriqueRoom* room)
return; return;
beginResetModel(); beginResetModel();
if( m_currentRoom ) if(m_currentRoom)
{ {
m_currentRoom->disconnect( this ); m_currentRoom->disconnect(this);
qDebug() << "Disconnected from" << m_currentRoom->id(); qDebug() << "Disconnected from" << m_currentRoom->id();
} }
m_currentRoom = room; m_currentRoom = room;
if( room ) if(room)
{ {
using namespace QMatrixClient; using namespace QMatrixClient;
connect(m_currentRoom, &Room::aboutToAddNewMessages, this, connect(m_currentRoom, &Room::aboutToAddNewMessages, this,
@ -156,14 +156,14 @@ QString MessageEventModel::makeDateString(MatriqueRoom::rev_iter_t baseIt) const
int MessageEventModel::rowCount(const QModelIndex& parent) const int MessageEventModel::rowCount(const QModelIndex& parent) const
{ {
if( !m_currentRoom || parent.isValid() ) if(!m_currentRoom || parent.isValid())
return 0; return 0;
return m_currentRoom->timelineSize(); return m_currentRoom->timelineSize();
} }
QVariant MessageEventModel::data(const QModelIndex& index, int role) const QVariant MessageEventModel::data(const QModelIndex& index, int role) const
{ {
if( !m_currentRoom || if(!m_currentRoom ||
index.row() < 0 || index.row() >= m_currentRoom->timelineSize()) index.row() < 0 || index.row() >= m_currentRoom->timelineSize())
return QVariant(); return QVariant();
@ -173,7 +173,7 @@ QVariant MessageEventModel::data(const QModelIndex& index, int role) const
QString senderName = m_currentRoom->roomMembername(event->senderId()); QString senderName = m_currentRoom->roomMembername(event->senderId());
using namespace QMatrixClient; using namespace QMatrixClient;
if( role == Qt::DisplayRole ) if(role == Qt::DisplayRole)
{ {
if (event->isRedacted()) if (event->isRedacted())
{ {
@ -185,7 +185,7 @@ QVariant MessageEventModel::data(const QModelIndex& index, int role) const
.arg(event->redactedBecause()->reason()); .arg(event->redactedBecause()->reason());
} }
if( event->type() == EventType::RoomMessage ) if(event->type() == EventType::RoomMessage)
{ {
using namespace MessageEventContent; using namespace MessageEventContent;
@ -202,13 +202,13 @@ QVariant MessageEventModel::data(const QModelIndex& index, int role) const
} }
return m_currentRoom->prettyPrint(e->plainBody()); return m_currentRoom->prettyPrint(e->plainBody());
} }
if( event->type() == EventType::RoomMember ) if(event->type() == EventType::RoomMember)
{ {
auto* e = static_cast<const RoomMemberEvent*>(event); auto* e = static_cast<const RoomMemberEvent*>(event);
// FIXME: Rewind to the name that was at the time of this event // FIXME: Rewind to the name that was at the time of this event
QString subjectName = m_currentRoom->roomMembername(e->userId()); QString subjectName = m_currentRoom->roomMembername(e->userId());
// The below code assumes senderName output in AuthorRole // The below code assumes senderName output in AuthorRole
switch( e->membership() ) switch(e->membership())
{ {
case MembershipType::Invite: case MembershipType::Invite:
if (e->repeatsState()) if (e->repeatsState())
@ -269,43 +269,43 @@ QVariant MessageEventModel::data(const QModelIndex& index, int role) const
// return tr("made something unknown"); // return tr("made something unknown");
} }
} }
if( event->type() == EventType::RoomAliases ) if(event->type() == EventType::RoomAliases)
{ {
auto* e = static_cast<const RoomAliasesEvent*>(event); auto* e = static_cast<const RoomAliasesEvent*>(event);
return tr("set aliases to: %1").arg(e->aliases().join(", ")); return tr("set aliases to: %1").arg(e->aliases().join(", "));
} }
if( event->type() == EventType::RoomCanonicalAlias ) if(event->type() == EventType::RoomCanonicalAlias)
{ {
auto* e = static_cast<const RoomCanonicalAliasEvent*>(event); auto* e = static_cast<const RoomCanonicalAliasEvent*>(event);
return tr("set the room main alias to: %1").arg(e->alias()); return tr("set the room main alias to: %1").arg(e->alias());
} }
if( event->type() == EventType::RoomName ) if(event->type() == EventType::RoomName)
{ {
auto* e = static_cast<const RoomNameEvent*>(event); auto* e = static_cast<const RoomNameEvent*>(event);
return tr("set the room name to: %1").arg(e->name()); return tr("set the room name to: %1").arg(e->name());
} }
if( event->type() == EventType::RoomTopic ) if(event->type() == EventType::RoomTopic)
{ {
auto* e = static_cast<const RoomTopicEvent*>(event); auto* e = static_cast<const RoomTopicEvent*>(event);
return tr("set the topic to: %1").arg(e->topic()); return tr("set the topic to: %1").arg(e->topic());
} }
if( event->type() == EventType::RoomAvatar ) if(event->type() == EventType::RoomAvatar)
{ {
return tr("changed the room avatar"); return tr("changed the room avatar");
} }
if( event->type() == EventType::RoomEncryption ) if(event->type() == EventType::RoomEncryption)
{ {
return tr("activated End-to-End Encryption"); return tr("activated End-to-End Encryption");
} }
return tr("Unknown Event"); return tr("Unknown Event");
} }
if( role == Qt::ToolTipRole ) if(role == Qt::ToolTipRole)
{ {
return event->originalJson(); return event->originalJson();
} }
if( role == EventTypeRole ) if(role == EventTypeRole)
{ {
if (event->isStateEvent()) if (event->isStateEvent())
return "state"; return "state";
@ -332,20 +332,20 @@ QVariant MessageEventModel::data(const QModelIndex& index, int role) const
return "other"; return "other";
} }
if( role == TimeRole ) if(role == TimeRole)
return makeMessageTimestamp(eventIt); return makeMessageTimestamp(eventIt);
if( role == SectionRole ) if(role == SectionRole)
return makeDateString(eventIt); // FIXME: move date rendering to QML return makeDateString(eventIt); // FIXME: move date rendering to QML
if( role == AboveSectionRole ) // FIXME: shouldn't be here, because #312 if(role == AboveSectionRole) // FIXME: shouldn't be here, because #312
{ {
auto aboveEventIt = eventIt + 1; auto aboveEventIt = eventIt + 1;
if (aboveEventIt != m_currentRoom->timelineEdge()) if (aboveEventIt != m_currentRoom->timelineEdge())
return makeDateString(aboveEventIt); return makeDateString(aboveEventIt);
} }
if( role == AuthorRole ) if(role == AuthorRole)
{ {
auto userId = event->senderId(); auto userId = event->senderId();
// FIXME: This will go away after senderName is generated correctly // FIXME: This will go away after senderName is generated correctly
@ -383,7 +383,7 @@ QVariant MessageEventModel::data(const QModelIndex& index, int role) const
.arg(event->redactedBecause()->reason()); .arg(event->redactedBecause()->reason());
} }
if( event->type() == EventType::RoomMessage ) if(event->type() == EventType::RoomMessage)
{ {
using namespace MessageEventContent; using namespace MessageEventContent;
@ -401,13 +401,13 @@ QVariant MessageEventModel::data(const QModelIndex& index, int role) const
} }
} }
if( role == HighlightRole ) if(role == HighlightRole)
return m_currentRoom->isEventHighlighted(event); return m_currentRoom->isEventHighlighted(event);
if( role == ReadMarkerRole ) if(role == ReadMarkerRole)
return event->id() == lastReadEventId; return event->id() == lastReadEventId;
if( role == SpecialMarksRole ) if(role == SpecialMarksRole)
{ {
if (event->isStateEvent() && if (event->isStateEvent() &&
static_cast<const StateEventBase*>(event)->repeatsState()) static_cast<const StateEventBase*>(event)->repeatsState())
@ -415,10 +415,10 @@ QVariant MessageEventModel::data(const QModelIndex& index, int role) const
return event->isRedacted() ? "redacted" : ""; return event->isRedacted() ? "redacted" : "";
} }
if( role == EventIdRole ) if(role == EventIdRole)
return event->id(); return event->id();
if( role == LongOperationRole ) if(role == LongOperationRole)
{ {
if (event->type() == EventType::RoomMessage && if (event->type() == EventType::RoomMessage &&
static_cast<const RoomMessageEvent*>(event)->hasFileContent()) static_cast<const RoomMessageEvent*>(event)->hasFileContent())

View File

@ -7,13 +7,14 @@
class MessageEventModel: public QAbstractListModel class MessageEventModel: public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
// The below property is marked constant because it only changes // The below property is marked constant because it only changes
// when the whole model is reset (so anything that depends on the model // when the whole model is reset (so anything that depends on the model
// has to be re-calculated anyway). // has to be re-calculated anyway).
// XXX: A better way would be to make [Room::]Timeline a list model // XXX: A better way would be to make [Room::]Timeline a list model
// itself, leaving only representation of the model to a client. // itself, leaving only representation of the model to a client.
Q_PROPERTY(MatriqueRoom* room MEMBER m_currentRoom CONSTANT) Q_PROPERTY(MatriqueRoom* room MEMBER m_currentRoom CONSTANT)
public: public:
enum EventRoles { enum EventRoles {
EventTypeRole = Qt::UserRole + 1, EventTypeRole = Qt::UserRole + 1,

View File

@ -18,37 +18,37 @@ class RoomListModel : public QAbstractListModel
Q_PROPERTY(QMatrixClient::Connection *connection READ getConnection WRITE setConnection NOTIFY connectionChanged) Q_PROPERTY(QMatrixClient::Connection *connection READ getConnection WRITE setConnection NOTIFY connectionChanged)
public: public:
explicit RoomListModel(); explicit RoomListModel();
~RoomListModel(); ~RoomListModel();
enum RoomModelRoles { enum RoomModelRoles {
NameRole, ValueRole, AvatarRole NameRole, ValueRole, AvatarRole
}; };
QMatrixClient::Connection* m_connection; QMatrixClient::Connection* m_connection;
QMatrixClient::Connection* getConnection() { return m_connection; } QMatrixClient::Connection* getConnection() { return m_connection; }
void setConnection(QMatrixClient::Connection* conn); void setConnection(QMatrixClient::Connection* conn);
QHash<int, QByteArray> roleNames() const; QHash<int, QByteArray> roleNames() const;
Q_INVOKABLE QMatrixClient::Room* roomAt(int row); Q_INVOKABLE QMatrixClient::Room* roomAt(int row);
QVariant data(const QModelIndex& index, int role) const override; QVariant data(const QModelIndex& index, int role) const override;
Q_INVOKABLE int rowCount(const QModelIndex& parent=QModelIndex()) const override; Q_INVOKABLE int rowCount(const QModelIndex& parent=QModelIndex()) const override;
signals: signals:
void connectionChanged(); void connectionChanged();
public slots: public slots:
private slots: private slots:
void namesChanged(QMatrixClient::Room* room); void namesChanged(QMatrixClient::Room* room);
void unreadMessagesChanged(QMatrixClient::Room* room); void unreadMessagesChanged(QMatrixClient::Room* room);
void addRoom(QMatrixClient::Room* room); void addRoom(QMatrixClient::Room* room);
private: private:
QList<QMatrixClient::Room*> m_rooms; QList<QMatrixClient::Room*> m_rooms;
}; };
#endif // ROOMLISTMODEL_H #endif // ROOMLISTMODEL_H