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);
qmlRegisterType<SyncJob>(); qRegisterMetaType<SyncJob*> ("SyncJob*");
qmlRegisterType<Room>(); qRegisterMetaType<Room*> ("Room*");
qmlRegisterType<User>(); qRegisterMetaType<User*> ("User*");
qmlRegisterType<SyncJob>();
qRegisterMetaType<SyncJob*> ("SyncJob*");
qmlRegisterType<Room>();
qRegisterMetaType<Room*>("Room*");
qmlRegisterType<User>();
qRegisterMetaType<User*>("User*");
qmlRegisterType<Connection>("Matrique", 0, 1, "Connection");
// qmlRegisterType<MessageEventModel> ("Matrique", 0, 1, "MessageEventModel");
// qmlRegisterType<RoomListModel> ("Matrique", 0, 1, "RoomListModel");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

View File

@ -3,12 +3,23 @@ import QtQuick.Layouts 1.3
import QtGraphicalEffects 1.0
import QtQuick.Controls 2.3
import QtQuick.Controls.Material 2.3
import Qt.labs.settings 1.0
import "qrc:/qml/component"
Page {
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 {
anchors.fill: parent
@ -124,6 +135,15 @@ Page {
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
title: qsTr("Matrique")
Connection { id: connection }
Connection {
id: connection
homeserver: settings.homeserver
}
Settings {
id: settings
property alias user: loginPage.username
property alias pass: loginPage.password
property var token
property string homeserver
property string userID
property string token
property string deviceID
}
FontLoader { id: materialFont; source: "qrc:/asset/font/material.ttf" }
function login() {
console.info("Login is invoked.")
var connect = connection.connectToServer
function init() {
connection.connected.connect(function() {
settings.user = connection.userId()
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.connectionError.connect(connection.reconnect)
connection.syncDone.connect(resync)
connection.reconnected.connect(resync)
connection.sync()
})
var userParts = settings.user.split(':')
if(userParts.length === 1 || userParts[1] === "matrix.org") { // If this user uses default server.
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.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 {
@ -165,4 +195,8 @@ ApplicationWindow {
}
}
Component.onCompleted: {
init()
}
}