From a4b00f823ef474ea3ce4c69895163612baca0fab Mon Sep 17 00:00:00 2001 From: Black Hat Date: Wed, 28 Feb 2018 17:10:42 +0800 Subject: [PATCH] Init RoomListModel and test sync. --- main.cpp | 10 +++++ matrique.pro | 12 ++--- matrix/controller.cpp | 22 +++++----- matrix/controller.h | 13 +++++- matrix/roomlistmodel.cpp | 44 +++++++++++++++++++ matrix/roomlistmodel.h | 39 ++++++++++++++++ qml/Home.qml | 10 ++--- qml/{Contact.qml => Room.qml} | 4 +- qml/form/{ChatForm.qml => RoomForm.qml} | 0 .../{ContactListForm.qml => RoomListForm.qml} | 0 qml/main.qml | 4 +- res.qrc | 6 +-- 12 files changed, 136 insertions(+), 28 deletions(-) create mode 100644 matrix/roomlistmodel.cpp create mode 100644 matrix/roomlistmodel.h rename qml/{Contact.qml => Room.qml} (85%) rename qml/form/{ChatForm.qml => RoomForm.qml} (100%) rename qml/form/{ContactListForm.qml => RoomListForm.qml} (100%) diff --git a/main.cpp b/main.cpp index 2b0e68eee..4c96d3aed 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,9 @@ #include #include +#include #include "matrix/controller.h" +#include "matrix/roomlistmodel.h" using namespace QMatrixClient; int main(int argc, char *argv[]) @@ -12,7 +14,15 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); +// Enable this if you need proxy. +// QNetworkProxy proxy; +// proxy.setType(QNetworkProxy::HttpProxy); +// proxy.setHostName("localhost"); +// proxy.setPort(1082); +// QNetworkProxy::setApplicationProxy(proxy); + qmlRegisterType("Matrique", 0, 1, "Controller"); + qmlRegisterType("Matrique", 0, 1, "RoomListModel"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); diff --git a/matrique.pro b/matrique.pro index 1e2cb653f..332d5186a 100644 --- a/matrique.pro +++ b/matrique.pro @@ -15,7 +15,8 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += main.cpp \ - matrix/controller.cpp + matrix/controller.cpp \ + matrix/roomlistmodel.cpp RESOURCES += \ res.qrc @@ -40,10 +41,11 @@ DISTFILES += \ ImageStatus.qml \ ButtonDelegate.qml \ SideNav.qml \ - ContactListForm.qml \ - ContactDetailForm.qml \ - Contact.qml \ + RoomListForm.qml \ + RoomDetailForm.qml \ + Room.qml \ Setting.qml HEADERS += \ - matrix/controller.h + matrix/controller.h \ + matrix/roomlistmodel.h diff --git a/matrix/controller.cpp b/matrix/controller.cpp index 382d90227..a43d2f326 100644 --- a/matrix/controller.cpp +++ b/matrix/controller.cpp @@ -3,10 +3,10 @@ #include "libqmatrixclient/connection.h" Controller::Controller(QObject *parent) : QObject(parent) { - connect(connection, &Connection::connected, this, &Controller::connected); - connect(connection, &Connection::resolveError, this, &Controller::reconnect); - connect(connection, &Connection::syncError, this, &Controller::reconnect); - connect(connection, &Connection::syncDone, this, &Controller::resync); + connect(m_connection, &QMatrixClient::Connection::connected, this, &Controller::connected); + connect(m_connection, &QMatrixClient::Connection::resolveError, this, &Controller::reconnect); + connect(m_connection, &QMatrixClient::Connection::syncError, this, &Controller::reconnect); + connect(m_connection, &QMatrixClient::Connection::syncDone, this, &Controller::resync); } Controller::~Controller() { @@ -24,10 +24,10 @@ void Controller::login(QString home, QString user, QString pass) { if(!userID.isEmpty() && !token.isEmpty()) { qDebug() << "Using token."; - connection->connectWithToken(userID, token, ""); + m_connection->connectWithToken(userID, token, ""); } else if(!user.isEmpty() && !pass.isEmpty()) { qDebug() << "Using given credential."; - connection->connectToServer("@"+user+":"+home, pass, ""); + m_connection->connectToServer("@"+user+":"+home, pass, ""); } } @@ -38,15 +38,17 @@ void Controller::logout() { } void Controller::connected() { - setUserID(connection->userId()); - setToken(connection->accessToken()); + setUserID(m_connection->userId()); + setToken(m_connection->accessToken()); + roomListModel->init(m_connection); + resync(); setIsLogin(true); } void Controller::resync() { - connection->sync(30000); + m_connection->sync(30000); } void Controller::reconnect() { - Controller::connection->connectWithToken(userID, token, ""); + m_connection->connectWithToken(userID, token, ""); } diff --git a/matrix/controller.h b/matrix/controller.h index a0e831494..f4c971a20 100644 --- a/matrix/controller.h +++ b/matrix/controller.h @@ -4,13 +4,17 @@ #include #include "libqmatrixclient/connection.h" +#include "roomlistmodel.h" -using namespace QMatrixClient; +namespace QMatrixClient { + class Connection; +} class Controller : public QObject { Q_OBJECT + Q_PROPERTY(RoomListModel *roomListModel READ getRoomListModel NOTIFY roomListModelChanged) Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged) Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged) Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged) @@ -25,6 +29,9 @@ public: // All the non-Q_INVOKABLE functions. // All the Q_PROPERTYs. + RoomListModel *roomListModel = new RoomListModel(this); + RoomListModel* getRoomListModel() { return roomListModel; } + bool isLogin = false; bool getIsLogin() { return isLogin; } void setIsLogin(bool n) { @@ -53,12 +60,14 @@ public: } private: - QMatrixClient::Connection *connection = new QMatrixClient::Connection(); + QMatrixClient::Connection *m_connection = new QMatrixClient::Connection(); + void connected(); void resync(); void reconnect(); signals: + void roomListModelChanged(); void isLoginChanged(); void userIDChanged(); void tokenChanged(); diff --git a/matrix/roomlistmodel.cpp b/matrix/roomlistmodel.cpp new file mode 100644 index 000000000..ed270113c --- /dev/null +++ b/matrix/roomlistmodel.cpp @@ -0,0 +1,44 @@ +#include "roomlistmodel.h" + +#include "controller.h" + +RoomListModel::RoomListModel(QObject *parent) : QObject(parent) +{ + +} + +void RoomListModel::init(QMatrixClient::Connection *conn) { + qDebug() << "Registering connection."; + m_connection = conn; + connect(m_connection, &QMatrixClient::Connection::newRoom, this, &RoomListModel::addRoom); + for(QMatrixClient::Room* room: m_connection->roomMap().values()) { + connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged); + m_rooms.append(room); + } +} + +RoomListModel::~RoomListModel() { + +} + +QMatrixClient::Room* RoomListModel::roomAt(int row) +{ + return m_rooms.at(row); +} + +void RoomListModel::addRoom(QMatrixClient::Room* room) +{ + qDebug() << "Adding room."; + connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged ); + m_rooms.append(room); +} + +void RoomListModel::namesChanged(QMatrixClient::Room* room) +{ + +} + +void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room) +{ + +} diff --git a/matrix/roomlistmodel.h b/matrix/roomlistmodel.h new file mode 100644 index 000000000..d49033814 --- /dev/null +++ b/matrix/roomlistmodel.h @@ -0,0 +1,39 @@ +#ifndef ROOMLISTMODEL_H +#define ROOMLISTMODEL_H + +#include + +#include "libqmatrixclient/connection.h" +#include "libqmatrixclient/room.h" + +namespace QMatrixClient { + class Connection; + class Room; +} + +class RoomListModel : public QObject +{ + Q_OBJECT +public: + explicit RoomListModel(QObject *parent = nullptr); + ~RoomListModel(); + + void init(QMatrixClient::Connection*); + + Q_INVOKABLE QMatrixClient::Room* roomAt(int row); + +signals: + +public slots: + +private slots: + void namesChanged(QMatrixClient::Room* room); + void unreadMessagesChanged(QMatrixClient::Room* room); + void addRoom(QMatrixClient::Room* room); + +private: + QMatrixClient::Connection *m_connection; + QList m_rooms; +}; + +#endif // ROOMLISTMODEL_H diff --git a/qml/Home.qml b/qml/Home.qml index d22562d88..c61556c16 100644 --- a/qml/Home.qml +++ b/qml/Home.qml @@ -3,15 +3,15 @@ import QtQuick.Controls 2.3 import "qrc:/qml/form" Page { - ContactListForm { - id: contactListForm + RoomListForm { + id: roomListForm height: parent.height width: 320 } - ChatForm { - id: chatForm + RoomForm { + id: roomForm anchors.fill: parent - anchors.leftMargin: contactListForm.width + anchors.leftMargin: roomListForm.width } } diff --git a/qml/Contact.qml b/qml/Room.qml similarity index 85% rename from qml/Contact.qml rename to qml/Room.qml index efcbe0104..a3a893fb1 100644 --- a/qml/Contact.qml +++ b/qml/Room.qml @@ -3,7 +3,9 @@ import QtQuick.Controls 2.3 import "qrc:/qml/form" Page { - ContactListForm { + property var roomListModel + + RoomListForm { id: contactListForm height: parent.height width: 320 diff --git a/qml/form/ChatForm.qml b/qml/form/RoomForm.qml similarity index 100% rename from qml/form/ChatForm.qml rename to qml/form/RoomForm.qml diff --git a/qml/form/ContactListForm.qml b/qml/form/RoomListForm.qml similarity index 100% rename from qml/form/ContactListForm.qml rename to qml/form/RoomListForm.qml diff --git a/qml/main.qml b/qml/main.qml index 2ac6f6baa..693872094 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -130,8 +130,8 @@ ApplicationWindow { controller: controller } - Contact { - + Room { + roomListModel: controller.roomListModel } Setting { diff --git a/res.qrc b/res.qrc index c3b15669b..7a9cc442b 100644 --- a/res.qrc +++ b/res.qrc @@ -4,7 +4,6 @@ asset/img/avatar.png asset/img/background.jpg asset/font/material.ttf - qml/Contact.qml qml/Home.qml qml/Login.qml qml/main.qml @@ -12,8 +11,9 @@ qml/component/ButtonDelegate.qml qml/component/ImageStatus.qml qml/component/SideNav.qml - qml/form/ChatForm.qml qml/form/ContactDetailForm.qml - qml/form/ContactListForm.qml + qml/form/RoomForm.qml + qml/form/RoomListForm.qml + qml/Room.qml