Init login/logout support.

This commit is contained in:
Black Hat 2018-02-26 20:41:20 +08:00 committed by Black Hat
parent c2f01ec1c4
commit 6d89a0373c
3 changed files with 90 additions and 35 deletions

View File

@ -20,13 +20,14 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
qmlRegisterType<SyncJob>(); qRegisterMetaType<SyncJob*> ("SyncJob*"); qmlRegisterType<SyncJob>();
qmlRegisterType<Room>(); qRegisterMetaType<Room*> ("Room*"); qRegisterMetaType<SyncJob*> ("SyncJob*");
qmlRegisterType<User>(); qRegisterMetaType<User*> ("User*"); qmlRegisterType<Room>();
qRegisterMetaType<Room*>("Room*");
qmlRegisterType<User>();
qRegisterMetaType<User*>("User*");
qmlRegisterType<Connection> ("Matrique", 0, 1, "Connection"); qmlRegisterType<Connection>("Matrique", 0, 1, "Connection");
// qmlRegisterType<MessageEventModel> ("Matrique", 0, 1, "MessageEventModel");
// qmlRegisterType<RoomListModel> ("Matrique", 0, 1, "RoomListModel");
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

View File

@ -3,12 +3,23 @@ import QtQuick.Layouts 1.3
import QtGraphicalEffects 1.0 import QtGraphicalEffects 1.0
import QtQuick.Controls 2.3 import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.3 import QtQuick.Controls.Material 2.3
import Qt.labs.settings 1.0
import "qrc:/qml/component" import "qrc:/qml/component"
Page { Page {
property var window property var window
property alias username: usernameField.text
property alias password: passwordField.text property alias homeserver: settings.server
property alias username: settings.user
property alias password: settings.pass
Settings {
id: settings
property alias server: serverField.text
property alias user: usernameField.text
property alias pass: passwordField.text
}
Row { Row {
anchors.fill: parent anchors.fill: parent
@ -124,6 +135,15 @@ Page {
onClicked: window.login() onClicked: window.login()
} }
Button {
id: logoutButton
text: "LOGOUT"
flat: true
width: parent.width
onClicked: window.logout()
}
} }
} }
} }

View File

@ -17,47 +17,77 @@ ApplicationWindow {
height: 640 height: 640
title: qsTr("Matrique") title: qsTr("Matrique")
Connection { id: connection } Connection {
id: connection
homeserver: settings.homeserver
}
Settings { Settings {
id: settings id: settings
property alias user: loginPage.username property string homeserver
property alias pass: loginPage.password
property var token property string userID
property string token
property string deviceID
} }
FontLoader { id: materialFont; source: "qrc:/asset/font/material.ttf" } FontLoader { id: materialFont; source: "qrc:/asset/font/material.ttf" }
function login() { function init() {
console.info("Login is invoked.")
var connect = connection.connectToServer
connection.connected.connect(function() { connection.connected.connect(function() {
settings.user = connection.userId() console.info("Matrix connected.")
settings.token = connection.accessToken
connection.connectionError.connect(connection.reconnect) connection.syncError.connect(reconnect)
connection.resolveError.connect(reconnect)
connection.syncDone.connect(resync) connection.syncDone.connect(resync)
connection.reconnected.connect(resync)
connection.sync()
}) })
}
var userParts = settings.user.split(':') function resync() {
if(userParts.length === 1 || userParts[1] === "matrix.org") { // If this user uses default server. if(!initialised) {
console.info("Matrix server is used.")
connect(settings.user, settings.pass, "Device")
} else {
connection.resolved.connect(function() {
connect(settings.user, settings.pass, "Device")
})
connection.resolveError.connect(function() {
console.info("Couldn't resolve server!")
})
connection.resolveServer(userParts[1])
} }
connection.sync(30000)
}
function reconnect() {
connection.connectWithToken(connection.localUserId,
connection.accessToken,
connection.deviceId)
}
function login() {
if(!settings.homeserver) settings.homeserver = "https://matrix.org"
console.info("Homeserver:", connection.homeserver)
console.info("UserID:", settings.userID)
console.info("Token:", settings.token)
console.info("DeviceID:", settings.deviceID)
if(!settings.token || !settings.userID) {
console.info("Using server address.")
settings.homeserver = loginPage.homeserver
function saveCredentials() {
settings.userID = connection.localUserId
settings.token = connection.accessToken
connection.connected.disconnect(saveCredentials)
}
connection.connected.connect(saveCredentials)
connection.connectToServer(loginPage.username, loginPage.password, connection.deviceId)
} else {
console.info("Using token")
connection.connectWithToken(settings.userID, settings.token, connection.deviceId)
}
}
function logout() {
settings.homeserver = null;
settings.userID = null;
settings.token = null;
} }
SideNav { SideNav {
@ -165,4 +195,8 @@ ApplicationWindow {
} }
} }
Component.onCompleted: {
init()
}
} }