2020-07-24 15:30:35 +10:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
|
|
|
import QtQuick 2.12
|
|
|
|
import QtQuick.Layouts 1.12
|
2020-08-19 14:17:24 +10:00
|
|
|
import "../.."
|
2020-07-24 15:30:35 +10:00
|
|
|
import "../../Base"
|
|
|
|
import "../../Base/Buttons"
|
|
|
|
import "../../PythonBridge"
|
|
|
|
|
|
|
|
HBox {
|
2020-08-19 14:17:24 +10:00
|
|
|
id: box
|
2020-07-24 15:30:35 +10:00
|
|
|
|
|
|
|
property string acceptedUserUrl: ""
|
|
|
|
property string acceptedUrl: ""
|
2020-07-26 13:31:13 +10:00
|
|
|
property var loginFlows: []
|
2020-07-24 15:30:35 +10:00
|
|
|
|
|
|
|
property string saveName: "serverBrowser"
|
|
|
|
property var saveProperties: ["acceptedUserUrl"]
|
2020-08-19 14:17:24 +10:00
|
|
|
property string loadingIconStep: "server-ping-bad"
|
|
|
|
|
2020-07-24 15:30:35 +10:00
|
|
|
property Future connectFuture: null
|
2020-08-19 14:17:24 +10:00
|
|
|
property Future fetchServersFuture: null
|
2020-07-24 15:30:35 +10:00
|
|
|
|
|
|
|
signal accepted()
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
function takeFocus() { serverField.item.field.forceActiveFocus() }
|
|
|
|
|
|
|
|
function fetchServers() {
|
|
|
|
fetchServersFuture = py.callCoro("fetch_homeservers", [], () => {
|
|
|
|
fetchServersFuture = null
|
|
|
|
}, (type, args, error, traceback) => {
|
|
|
|
fetchServersFuture = null
|
|
|
|
// TODO
|
|
|
|
print( traceback)
|
|
|
|
})
|
|
|
|
}
|
2020-07-24 15:30:35 +10:00
|
|
|
|
|
|
|
function connect() {
|
|
|
|
if (connectFuture) connectFuture.cancel()
|
|
|
|
connectTimeout.restart()
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
const args = [serverField.item.field.cleanText]
|
2020-07-24 15:30:35 +10:00
|
|
|
|
|
|
|
connectFuture = py.callCoro("server_info", args, ([url, flows]) => {
|
|
|
|
connectTimeout.stop()
|
2020-08-19 14:17:24 +10:00
|
|
|
serverField.errorLabel.text = ""
|
|
|
|
connectFuture = null
|
2020-07-26 13:31:13 +10:00
|
|
|
|
|
|
|
if (! (
|
|
|
|
flows.includes("m.login.password") ||
|
|
|
|
(
|
|
|
|
flows.includes("m.login.sso") &&
|
|
|
|
flows.includes("m.login.token")
|
|
|
|
)
|
|
|
|
)) {
|
2020-08-19 14:17:24 +10:00
|
|
|
serverField.errorLabel.text =
|
2020-07-26 13:31:13 +10:00
|
|
|
qsTr("No supported sign-in method for this homeserver.")
|
|
|
|
return
|
|
|
|
}
|
2020-07-24 15:30:35 +10:00
|
|
|
|
|
|
|
acceptedUrl = url
|
2020-07-28 06:37:33 +10:00
|
|
|
acceptedUserUrl = String(args[0])
|
2020-07-24 15:30:35 +10:00
|
|
|
loginFlows = flows
|
|
|
|
accepted()
|
|
|
|
|
|
|
|
}, (type, args, error, traceback, uuid) => {
|
2020-08-19 16:14:46 +10:00
|
|
|
console.error(traceback)
|
|
|
|
|
2020-07-24 15:30:35 +10:00
|
|
|
connectTimeout.stop()
|
|
|
|
connectFuture = null
|
|
|
|
|
|
|
|
let text = qsTr("Unexpected error: %1 [%2]").arg(type).arg(args)
|
|
|
|
|
|
|
|
type === "MatrixNotFound" ?
|
|
|
|
text = qsTr("Invalid homeserver address") :
|
|
|
|
|
|
|
|
type.startsWith("Matrix") ?
|
2020-08-19 16:14:46 +10:00
|
|
|
text = qsTr("Connection failed: %1(%2)").arg(type).arg(args) :
|
2020-07-24 15:30:35 +10:00
|
|
|
|
2020-08-03 15:19:08 +10:00
|
|
|
py.showError(type, traceback, uuid)
|
2020-07-24 15:30:35 +10:00
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
serverField.errorLabel.text = text
|
2020-07-24 15:30:35 +10:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
padding: 0
|
|
|
|
implicitWidth: theme.controls.box.defaultWidth * 1.25
|
2020-08-19 18:38:29 +10:00
|
|
|
contentHeight: window.height
|
2020-08-19 14:17:24 +10:00
|
|
|
|
|
|
|
header: HLabel {
|
|
|
|
text: qsTr(
|
|
|
|
"Choose a homeserver to create your account on, or the " +
|
|
|
|
"server on which you made an account to sign in to:"
|
|
|
|
)
|
|
|
|
wrapMode: HLabel.Wrap
|
|
|
|
padding: theme.spacing
|
2020-07-24 15:30:35 +10:00
|
|
|
}
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
footer: HLabeledItem {
|
|
|
|
id: serverField
|
2020-07-24 15:30:35 +10:00
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
readonly property bool knownServerChosen:
|
|
|
|
serverList.model.find(item.cleanText) !== null
|
|
|
|
|
|
|
|
label.text: qsTr("Homeserver address:")
|
|
|
|
label.topPadding: theme.spacing
|
|
|
|
label.leftPadding: label.topPadding
|
|
|
|
label.rightPadding: label.topPadding
|
|
|
|
errorLabel.leftPadding: label.topPadding
|
|
|
|
errorLabel.rightPadding: label.topPadding
|
|
|
|
errorLabel.bottomPadding: label.topPadding
|
|
|
|
|
|
|
|
Layout.fillWidth: true
|
|
|
|
Layout.margins: theme.spacing
|
|
|
|
|
|
|
|
HRowLayout {
|
|
|
|
readonly property alias field: field
|
|
|
|
readonly property alias apply: apply
|
|
|
|
|
|
|
|
width: parent.width
|
|
|
|
|
|
|
|
HTextField {
|
|
|
|
id: field
|
|
|
|
|
|
|
|
readonly property string cleanText:
|
|
|
|
text.toLowerCase().trim().replace(/\/+$/, "")
|
|
|
|
|
2020-08-19 16:14:46 +10:00
|
|
|
inputMethodHints: Qt.ImhUrlCharactersOnly
|
2020-08-19 14:17:24 +10:00
|
|
|
defaultText: window.getState(
|
|
|
|
box, "acceptedUserUrl", "",
|
|
|
|
)
|
2020-08-19 16:14:46 +10:00
|
|
|
placeholderText: "example.org"
|
2020-08-19 14:17:24 +10:00
|
|
|
|
2020-08-19 18:38:29 +10:00
|
|
|
onTextEdited: py.callCoro(
|
|
|
|
"set_substring_filter", ["filtered_homeservers", text],
|
|
|
|
)
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
Layout.fillWidth: true
|
|
|
|
Layout.fillHeight: true
|
2020-08-19 18:38:29 +10:00
|
|
|
|
|
|
|
Keys.onBacktabPressed: ev => Keys.onUpPressed(ev)
|
|
|
|
Keys.onTabPressed: ev => Keys.onDownPressed(ev)
|
|
|
|
Keys.onUpPressed: {
|
|
|
|
serverList.decrementCurrentIndex()
|
|
|
|
serverList.setFieldText(serverList.currentIndex)
|
|
|
|
}
|
|
|
|
Keys.onDownPressed: {
|
|
|
|
serverList.incrementCurrentIndex()
|
|
|
|
serverList.setFieldText(serverList.currentIndex)
|
|
|
|
}
|
2020-08-19 14:17:24 +10:00
|
|
|
}
|
2020-07-24 15:30:35 +10:00
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
HButton {
|
|
|
|
id: apply
|
|
|
|
enabled: field.cleanText && ! field.error
|
|
|
|
icon.name: "apply"
|
|
|
|
icon.color: theme.colors.positiveBackground
|
|
|
|
loading: box.connectFuture !== null
|
|
|
|
disableWhileLoading: false
|
|
|
|
onClicked: box.connect()
|
|
|
|
|
|
|
|
Layout.fillHeight: true
|
|
|
|
}
|
2020-07-24 15:30:35 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
onKeyboardAccept: if (serverField.item.apply.enabled) box.connect()
|
2020-07-24 15:30:35 +10:00
|
|
|
onAccepted: window.saveState(this)
|
|
|
|
|
|
|
|
Timer {
|
|
|
|
id: connectTimeout
|
|
|
|
interval: 30 * 1000
|
|
|
|
onTriggered: {
|
2020-08-19 14:17:24 +10:00
|
|
|
serverField.errorLabel.text =
|
2020-07-24 15:30:35 +10:00
|
|
|
serverField.knownServerChosen ?
|
|
|
|
|
|
|
|
qsTr("This homeserver seems unavailable. Verify your inter" +
|
2020-08-19 14:17:24 +10:00
|
|
|
"net connection or try again later.") :
|
2020-07-24 15:30:35 +10:00
|
|
|
|
|
|
|
qsTr("This homeserver seems unavailable. Verify the " +
|
|
|
|
"entered address, your internet connection or try " +
|
2020-08-19 14:17:24 +10:00
|
|
|
"again later.")
|
2020-07-24 15:30:35 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
Timer {
|
|
|
|
interval: 1000
|
2020-08-19 18:38:29 +10:00
|
|
|
running:
|
|
|
|
fetchServersFuture === null &&
|
|
|
|
ModelStore.get("homeservers").count === 0
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
repeat: true
|
|
|
|
triggeredOnStart: true
|
|
|
|
onTriggered: box.fetchServers()
|
2020-07-24 15:30:35 +10:00
|
|
|
}
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
Timer {
|
|
|
|
interval: theme.animationDuration * 2
|
|
|
|
running: true
|
|
|
|
repeat: true
|
|
|
|
onTriggered:
|
|
|
|
box.loadingIconStep = "server-ping-" + (
|
|
|
|
box.loadingIconStep === "server-ping-bad" ? "medium" :
|
|
|
|
box.loadingIconStep === "server-ping-medium" ? "good" :
|
|
|
|
"bad"
|
|
|
|
)
|
|
|
|
}
|
2020-07-24 15:30:35 +10:00
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
HListView {
|
|
|
|
id: serverList
|
2020-08-19 18:38:29 +10:00
|
|
|
|
|
|
|
function setFieldText(fromItemIndex) {
|
|
|
|
serverField.item.field.text =
|
|
|
|
model.get(fromItemIndex).id.replace(/^https:\/\//, "")
|
|
|
|
}
|
|
|
|
|
2020-08-19 14:17:24 +10:00
|
|
|
clip: true
|
2020-08-19 18:38:29 +10:00
|
|
|
model: ModelStore.get("filtered_homeservers")
|
2020-08-19 14:17:24 +10:00
|
|
|
|
|
|
|
delegate: ServerDelegate {
|
|
|
|
width: serverList.width
|
|
|
|
loadingIconStep: box.loadingIconStep
|
|
|
|
onClicked: {
|
2020-08-19 18:38:29 +10:00
|
|
|
setFieldText(model.index)
|
2020-08-19 14:17:24 +10:00
|
|
|
serverField.item.apply.clicked()
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 15:30:35 +10:00
|
|
|
|
|
|
|
Layout.fillWidth: true
|
2020-08-19 14:17:24 +10:00
|
|
|
Layout.fillHeight: true
|
2020-07-24 15:30:35 +10:00
|
|
|
}
|
|
|
|
}
|