Implement login/logout in controller.

This commit is contained in:
Black Hat
2018-02-27 19:07:50 +08:00
parent 6d34f1042f
commit 5b1047ed98
5 changed files with 145 additions and 110 deletions

View File

@@ -8,6 +8,7 @@ import "qrc:/qml/component"
Page {
property var window
property var controller
property alias homeserver: settings.server
property alias username: settings.user
@@ -79,35 +80,53 @@ Page {
anchors.horizontalCenter: parent.horizontalCenter
}
TextField {
id: serverField
RowLayout {
width: parent.width
height: 48
placeholderText: "Server"
leftPadding: 16
topPadding: 0
bottomPadding: 0
spacing: 0
background: Rectangle {
color: "#eaeaea"
border.color: parent.activeFocus ? Material.accent : "transparent"
border.width: 2
Text {
text: "@"
horizontalAlignment: Text.AlignHCenter
Layout.preferredWidth: parent.width * 0.05
}
}
TextField {
id: usernameField
width: parent.width
height: 48
placeholderText: "Username"
leftPadding: 16
topPadding: 0
bottomPadding: 0
TextField {
id: usernameField
Layout.preferredWidth: parent.width * 0.45
Layout.fillHeight: true
placeholderText: "Username"
leftPadding: 16
topPadding: 0
bottomPadding: 0
background: Rectangle {
color: "#eaeaea"
border.color: parent.activeFocus ? Material.accent : "transparent"
border.width: 2
background: Rectangle {
color: "#eaeaea"
border.color: parent.activeFocus ? Material.accent : "transparent"
border.width: 2
}
}
Text {
text: ":"
horizontalAlignment: Text.AlignHCenter
Layout.preferredWidth: parent.width * 0.05
}
TextField {
id: serverField
Layout.preferredWidth: parent.width * 0.45
Layout.fillHeight: true
placeholderText: "Server"
leftPadding: 16
topPadding: 0
bottomPadding: 0
background: Rectangle {
color: "#eaeaea"
border.color: parent.activeFocus ? Material.accent : "transparent"
border.width: 2
}
}
}
@@ -133,7 +152,7 @@ Page {
highlighted: true
width: parent.width
onClicked: window.login()
onClicked: controller.login(homeserver, username, password)
}
Button {
@@ -142,7 +161,7 @@ Page {
flat: true
width: parent.width
onClicked: window.logout()
onClicked: controller.logout()
}
}
}

View File

@@ -17,79 +17,19 @@ ApplicationWindow {
height: 640
title: qsTr("Matrique")
Connection {
id: connection
homeserver: settings.homeserver
Controller {
id: controller
}
Settings {
id: settings
property string homeserver
property string userID
property string token
property string deviceID
property alias userID: controller.userID
property alias token: controller.token
}
FontLoader { id: materialFont; source: "qrc:/asset/font/material.ttf" }
function init() {
connection.connected.connect(function() {
console.info("Matrix connected.")
connection.syncError.connect(reconnect)
connection.resolveError.connect(reconnect)
connection.syncDone.connect(resync)
})
}
function resync() {
if(!initialised) {
}
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 {
id: sideNav
width: 80
@@ -185,6 +125,7 @@ ApplicationWindow {
Login {
id: loginPage
window: window
controller: controller
}
Contact {
@@ -197,6 +138,6 @@ ApplicationWindow {
}
Component.onCompleted: {
init()
controller.init()
}
}