From 6d89a0373c7c028552239f173382c46ef784f014 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Mon, 26 Feb 2018 20:41:20 +0800 Subject: [PATCH] Init login/logout support. --- main.cpp | 13 ++++---- qml/Login.qml | 24 ++++++++++++-- qml/main.qml | 88 +++++++++++++++++++++++++++++++++++---------------- 3 files changed, 90 insertions(+), 35 deletions(-) diff --git a/main.cpp b/main.cpp index cc2fd4a..65b650f 100644 --- a/main.cpp +++ b/main.cpp @@ -20,13 +20,14 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); - qmlRegisterType(); qRegisterMetaType ("SyncJob*"); - qmlRegisterType(); qRegisterMetaType ("Room*"); - qmlRegisterType(); qRegisterMetaType ("User*"); + qmlRegisterType(); + qRegisterMetaType ("SyncJob*"); + qmlRegisterType(); + qRegisterMetaType("Room*"); + qmlRegisterType(); + qRegisterMetaType("User*"); - qmlRegisterType ("Matrique", 0, 1, "Connection"); -// qmlRegisterType ("Matrique", 0, 1, "MessageEventModel"); -// qmlRegisterType ("Matrique", 0, 1, "RoomListModel"); + qmlRegisterType("Matrique", 0, 1, "Connection"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); diff --git a/qml/Login.qml b/qml/Login.qml index 2810a67..c48f348 100644 --- a/qml/Login.qml +++ b/qml/Login.qml @@ -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() + } } } } diff --git a/qml/main.qml b/qml/main.qml index a72bf98..2266ba6 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -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() - settings.token = connection.accessToken + console.info("Matrix connected.") - connection.connectionError.connect(connection.reconnect) + connection.syncError.connect(reconnect) + connection.resolveError.connect(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]) + 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 { @@ -165,4 +195,8 @@ ApplicationWindow { } } + + Component.onCompleted: { + init() + } }