Init RoomListModel and test sync.

This commit is contained in:
Black Hat 2018-02-28 17:10:42 +08:00
parent 535e4bb3a0
commit a4b00f823e
12 changed files with 136 additions and 28 deletions

View File

@ -1,7 +1,9 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QNetworkProxy>
#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<Controller>("Matrique", 0, 1, "Controller");
qmlRegisterType<RoomListModel>("Matrique", 0, 1, "RoomListModel");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

View File

@ -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

View File

@ -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, "");
}

View File

@ -4,13 +4,17 @@
#include <QObject>
#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();

44
matrix/roomlistmodel.cpp Normal file
View File

@ -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)
{
}

39
matrix/roomlistmodel.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef ROOMLISTMODEL_H
#define ROOMLISTMODEL_H
#include <QObject>
#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<QMatrixClient::Room*> m_rooms;
};
#endif // ROOMLISTMODEL_H

View File

@ -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
}
}

View File

@ -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

View File

@ -130,8 +130,8 @@ ApplicationWindow {
controller: controller
}
Contact {
Room {
roomListModel: controller.roomListModel
}
Setting {

View File

@ -4,7 +4,6 @@
<file>asset/img/avatar.png</file>
<file>asset/img/background.jpg</file>
<file>asset/font/material.ttf</file>
<file>qml/Contact.qml</file>
<file>qml/Home.qml</file>
<file>qml/Login.qml</file>
<file>qml/main.qml</file>
@ -12,8 +11,9 @@
<file>qml/component/ButtonDelegate.qml</file>
<file>qml/component/ImageStatus.qml</file>
<file>qml/component/SideNav.qml</file>
<file>qml/form/ChatForm.qml</file>
<file>qml/form/ContactDetailForm.qml</file>
<file>qml/form/ContactListForm.qml</file>
<file>qml/form/RoomForm.qml</file>
<file>qml/form/RoomListForm.qml</file>
<file>qml/Room.qml</file>
</qresource>
</RCC>