Port registration to Qml

This commit is contained in:
Nicolas Werner 2022-01-28 15:24:56 +01:00
parent 573624a490
commit f28013dc18
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
12 changed files with 444 additions and 443 deletions

View file

@ -21,6 +21,14 @@ ColumnLayout {
property alias echoMode: input.echoMode property alias echoMode: input.echoMode
property alias selectByMouse: input.selectByMouse property alias selectByMouse: input.selectByMouse
Timer {
id: timer
interval: 350
onTriggered: editingFinished()
}
onTextChanged: timer.restart()
signal textEdited signal textEdited
signal accepted signal accepted
signal editingFinished signal editingFinished

View file

@ -58,7 +58,7 @@ Popup {
id: completerPopup id: completerPopup
x: roomTextInput.x x: roomTextInput.x
y: roomTextInput.y + roomTextInput.height y: roomTextInput.y + quickSwitcher.textHeight
visible: roomTextInput.length > 0 visible: roomTextInput.length > 0
width: parent.width width: parent.width
completerName: "room" completerName: "room"

View file

@ -395,6 +395,13 @@ Pane {
} }
} }
Component {
id: registerPage
RegistrationPage {
}
}
Connections { Connections {
function onSwitchToChatPage() { function onSwitchToChatPage() {
mainWindow.replace(null, chatPage); mainWindow.replace(null, chatPage);

View file

@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: 2022 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick 2.15 import QtQuick 2.15
import QtQuick.Controls 2.15 import QtQuick.Controls 2.15
import QtQuick.Layouts 1.2 import QtQuick.Layouts 1.2
@ -9,7 +13,7 @@ import "../"
Item { Item {
id: loginPage id: loginPage
property int maxExpansion: 800 property int maxExpansion: 400
property string error: login.error property string error: login.error

View file

@ -0,0 +1,215 @@
// SPDX-FileCopyrightText: 2022 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.2
import QtQuick.Window 2.15
import im.nheko 1.0
import "../components/"
import "../ui/"
import "../"
Item {
id: registrationPage
property int maxExpansion: 400
property string error: regis.error
Registration {
id: regis
}
ScrollView {
id: scroll
clip: false
palette: Nheko.colors
ScrollBar.horizontal.visible: false
anchors.left: parent.left
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
height: Math.min(registrationPage.height, col.implicitHeight)
anchors.margins: Nheko.paddingLarge
contentWidth: availableWidth
ColumnLayout {
id: col
spacing: Nheko.paddingMedium
anchors.horizontalCenter: parent.horizontalCenter
width: Math.min(registrationPage.maxExpansion, scroll.width- Nheko.paddingLarge*2)
Image {
Layout.alignment: Qt.AlignHCenter
source: "qrc:/logos/login.png"
height: 128
width: 128
}
RowLayout {
spacing: Nheko.paddingLarge
Layout.fillWidth: true
MatrixTextField {
id: hsLabel
label: qsTr("Homeserver")
placeholderText: qsTr("your.server")
onEditingFinished: regis.setServer(text)
ToolTip.text: qsTr("A server that allows registration. Since matrix is decentralized, you need to first find a server you can register on or host your own.")
}
Spinner {
height: hsLabel.height/2
Layout.alignment: Qt.AlignBottom
visible: running
running: regis.lookingUpHs
foreground: Nheko.colors.mid
}
}
MatrixText {
textFormat: Text.PlainText
color: Nheko.theme.error
text: regis.hsError
visible: text
}
RowLayout {
spacing: Nheko.paddingLarge
visible: regis.supported
Layout.fillWidth: true
MatrixTextField {
id: usernameLabel
Layout.fillWidth: true
label: qsTr("Username")
ToolTip.text: qsTr("The username must not be empty, and must contain only the characters a-z, 0-9, ., _, =, -, and /.")
onEditingFinished: regis.checkUsername(text)
}
Spinner {
height: usernameLabel.height/2
Layout.alignment: Qt.AlignBottom
visible: running
running: regis.lookingUpUsername
foreground: Nheko.colors.mid
}
Image {
width: usernameLabel.height/2
height: width
Layout.preferredHeight: usernameLabel.height/2
Layout.preferredWidth: usernameLabel.height/2
Layout.alignment: Qt.AlignBottom
source: regis.usernameAvailable ? ("image://colorimage/:/icons/icons/ui/checkmark.svg?green") : ("image://colorimage/:/icons/icons/ui/dismiss.svg?"+Nheko.theme.error)
visible: regis.usernameAvailable || regis.usernameUnavailable
ToolTip.visible: ma.hovered
ToolTip.text: qsTr("Back")
sourceSize.height: height * Screen.devicePixelRatio
sourceSize.width: width * Screen.devicePixelRatio
HoverHandler {
id: ma
}
}
}
MatrixText {
textFormat: Text.PlainText
color: Nheko.theme.error
text: regis.usernameError
visible: text
}
MatrixTextField {
visible: regis.supported
id: passwordLabel
Layout.fillWidth: true
label: qsTr("Password")
echoMode: TextInput.Password
ToolTip.text: qsTr("Please choose a secure password. The exact requirements for password strength may depend on your server.")
}
MatrixTextField {
visible: regis.supported
id: passwordConfirmationLabel
Layout.fillWidth: true
label: qsTr("Password confirmation")
echoMode: TextInput.Password
}
MatrixText {
visible: regis.supported
textFormat: Text.PlainText
color: Nheko.theme.error
text: passwordLabel.text != passwordConfirmationLabel.text ? qsTr("Your passwords do not match!") : ""
}
MatrixTextField {
visible: regis.supported
id: deviceNameLabel
Layout.fillWidth: true
label: qsTr("Device name")
placeholderText: regis.initialDeviceName()
ToolTip.text: qsTr("A name for this device, which will be shown to others, when verifying your devices. If none is provided a default is used.")
}
Item {
height: Nheko.avatarSize
Layout.fillWidth: true
Spinner {
height: parent.height
anchors.centerIn: parent
visible: running
running: regis.registering
foreground: Nheko.colors.mid
}
}
MatrixText {
textFormat: Text.PlainText
color: Nheko.theme.error
text: registrationPage.error
visible: text
}
FlatButton {
id: regisBtn
visible: regis.supported
enabled: usernameLabel.text && passwordLabel.text && passwordLabel.text == passwordConfirmationLabel.text
Layout.alignment: Qt.AlignHCenter
text: qsTr("REGISTER")
function register() {
regis.startRegistration(usernameLabel.text, passwordLabel.text, deviceNameLabel.text)
}
onClicked: regisBtn.register()
Keys.onEnterPressed: regisBtn.register()
Keys.onReturnPressed: regisBtn.register()
Keys.enabled: regisBtn.enabled && regis.supported
}
}
}
ImageButton {
anchors.top: parent.top
anchors.left: parent.left
anchors.margins: Nheko.paddingMedium
width: Nheko.avatarSize
height: Nheko.avatarSize
image: ":/icons/icons/ui/angle-arrow-left.svg"
ToolTip.visible: hovered
ToolTip.text: qsTr("Back")
onClicked: mainWindow.pop()
}
}

View file

@ -48,6 +48,7 @@ ColumnLayout {
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
text: qsTr("REGISTER") text: qsTr("REGISTER")
onClicked: { onClicked: {
mainWindow.push(registerPage);
} }
} }
FlatButton { FlatButton {

View file

@ -112,6 +112,7 @@
<file>qml/pages/UserSettingsPage.qml</file> <file>qml/pages/UserSettingsPage.qml</file>
<file>qml/pages/WelcomePage.qml</file> <file>qml/pages/WelcomePage.qml</file>
<file>qml/pages/LoginPage.qml</file> <file>qml/pages/LoginPage.qml</file>
<file>qml/pages/RegistrationPage.qml</file>
<file>qml/components/AdaptiveLayout.qml</file> <file>qml/components/AdaptiveLayout.qml</file>
<file>qml/components/AdaptiveLayoutElement.qml</file> <file>qml/components/AdaptiveLayoutElement.qml</file>
<file>qml/components/AvatarListTile.qml</file> <file>qml/components/AvatarListTile.qml</file>

View file

@ -67,6 +67,22 @@ public:
onMatrixIdEntered(); onMatrixIdEntered();
} }
} }
static std::string initialDeviceName_()
{
#if defined(Q_OS_MAC)
return "Nheko on macOS";
#elif defined(Q_OS_LINUX)
return "Nheko on Linux";
#elif defined(Q_OS_WIN)
return "Nheko on Windows";
#elif defined(Q_OS_FREEBSD)
return "Nheko on FreeBSD";
#else
return "Nheko";
#endif
}
signals: signals:
void loggingInChanged(); void loggingInChanged();
void errorOccurred(); void errorOccurred();
@ -105,20 +121,6 @@ public slots:
private: private:
void checkHomeserverVersion(); void checkHomeserverVersion();
void onMatrixIdEntered(); void onMatrixIdEntered();
std::string initialDeviceName_() const
{
#if defined(Q_OS_MAC)
return "Nheko on macOS";
#elif defined(Q_OS_LINUX)
return "Nheko on Linux";
#elif defined(Q_OS_WIN)
return "Nheko on Windows";
#elif defined(Q_OS_FREEBSD)
return "Nheko on FreeBSD";
#else
return "Nheko";
#endif
}
void clearErrors() void clearErrors()
{ {
error_.clear(); error_.clear();

View file

@ -85,24 +85,10 @@ MainWindow::MainWindow(QWindow *parent)
setColor(Theme::paletteFromTheme(userSettings_->theme()).window().color()); setColor(Theme::paletteFromTheme(userSettings_->theme()).window().color());
setSource(QUrl(QStringLiteral("qrc:///qml/Root.qml"))); setSource(QUrl(QStringLiteral("qrc:///qml/Root.qml")));
// modal_ = new OverlayModal(this);
// QFont font;
// font.setStyleStrategy(QFont::PreferAntialias);
// setFont(font);
trayIcon_ = new TrayIcon(QStringLiteral(":/logos/nheko.svg"), this); trayIcon_ = new TrayIcon(QStringLiteral(":/logos/nheko.svg"), this);
// welcome_page_ = new WelcomePage(this);
// register_page_ = new RegisterPage(this);
//// Initialize sliding widget manager.
// connect(welcome_page_, SIGNAL(userRegister()), this, SLOT(showRegisterPage()));
connect(chat_page_, &ChatPage::closing, this, [this] { switchToLoginPage(""); }); connect(chat_page_, &ChatPage::closing, this, [this] { switchToLoginPage(""); });
// connect(
// chat_page_, &ChatPage::showOverlayProgressBar, this, &MainWindow::showOverlayProgressBar);
connect(chat_page_, &ChatPage::unreadMessages, this, &MainWindow::setWindowTitle); connect(chat_page_, &ChatPage::unreadMessages, this, &MainWindow::setWindowTitle);
connect(chat_page_, SIGNAL(unreadMessages(int)), trayIcon_, SLOT(setUnreadCount(int))); connect(chat_page_, SIGNAL(unreadMessages(int)), trayIcon_, SLOT(setUnreadCount(int)));
connect(chat_page_, &ChatPage::showLoginPage, this, [this](const QString &msg) { connect(chat_page_, &ChatPage::showLoginPage, this, [this](const QString &msg) {
@ -117,13 +103,6 @@ MainWindow::MainWindow(QWindow *parent)
connect(chat_page_, SIGNAL(contentLoaded()), this, SLOT(removeOverlayProgressBar())); connect(chat_page_, SIGNAL(contentLoaded()), this, SLOT(removeOverlayProgressBar()));
// connect(login_page_, &LoginPage::loginOk, this, [this](const mtx::responses::Login &res) {
// http::client()->set_user(res.user_id);
// showChatPage();
// });
// connect(register_page_, &RegisterPage::registerOk, this, &MainWindow::showChatPage);
trayIcon_->setVisible(userSettings_->tray()); trayIcon_->setVisible(userSettings_->tray());
// load cache on event loop // load cache on event loop
@ -198,6 +177,7 @@ MainWindow::registerQmlTypes()
qmlRegisterType<MxcMediaProxy>("im.nheko", 1, 0, "MxcMedia"); qmlRegisterType<MxcMediaProxy>("im.nheko", 1, 0, "MxcMedia");
qmlRegisterType<RoomDirectoryModel>("im.nheko", 1, 0, "RoomDirectoryModel"); qmlRegisterType<RoomDirectoryModel>("im.nheko", 1, 0, "RoomDirectoryModel");
qmlRegisterType<LoginPage>("im.nheko", 1, 0, "Login"); qmlRegisterType<LoginPage>("im.nheko", 1, 0, "Login");
qmlRegisterType<RegisterPage>("im.nheko", 1, 0, "Registration");
qmlRegisterUncreatableType<DeviceVerificationFlow>( qmlRegisterUncreatableType<DeviceVerificationFlow>(
"im.nheko", "im.nheko",
1, 1,
@ -460,7 +440,3 @@ MainWindow::showDialog(QWidget *dialog)
dialog->raise(); dialog->raise();
dialog->show(); dialog->show();
} }
void
MainWindow::showRegisterPage()
{}

View file

@ -69,9 +69,6 @@ private slots:
//! Handle interaction with the tray icon. //! Handle interaction with the tray icon.
void iconActivated(QSystemTrayIcon::ActivationReason reason); void iconActivated(QSystemTrayIcon::ActivationReason reason);
//! Show the register page in the main window.
void showRegisterPage();
virtual void setWindowTitle(int notificationCount); virtual void setWindowTitle(int notificationCount);
signals: signals:

View file

@ -4,312 +4,81 @@
// //
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
#include <QInputDialog> #include <mtx/responses/common.hpp>
#include <QLabel>
#include <QMetaType>
#include <QPainter>
#include <QStyleOption>
#include <QTimer>
#include <QtMath>
#include <mtx/responses/register.hpp> #include <mtx/responses/register.hpp>
#include <mtx/responses/well-known.hpp> #include <mtx/responses/well-known.hpp>
#include <mtxclient/http/client.hpp> #include <mtxclient/http/client.hpp>
#include "Config.h" #include "Config.h"
#include "Logging.h" #include "Logging.h"
#include "LoginPage.h"
#include "MainWindow.h" #include "MainWindow.h"
#include "MatrixClient.h" #include "MatrixClient.h"
#include "RegisterPage.h" #include "RegisterPage.h"
#include "ui/FlatButton.h"
#include "ui/RaisedButton.h"
#include "ui/TextField.h"
#include "ui/UIA.h" #include "ui/UIA.h"
#include "dialogs/FallbackAuth.h" RegisterPage::RegisterPage(QObject *parent)
#include "dialogs/ReCaptcha.h" : QObject(parent)
{}
Q_DECLARE_METATYPE(mtx::user_interactive::Unauthorized) void
Q_DECLARE_METATYPE(mtx::user_interactive::Auth) RegisterPage::setError(QString err)
RegisterPage::RegisterPage(QWidget *parent)
: QWidget(parent)
{ {
qRegisterMetaType<mtx::user_interactive::Unauthorized>(); registrationError_ = err;
qRegisterMetaType<mtx::user_interactive::Auth>(); emit errorChanged();
top_layout_ = new QVBoxLayout(); registering_ = false;
emit registeringChanged();
}
void
RegisterPage::setHsError(QString err)
{
hsError_ = err;
emit hsErrorChanged();
lookingUpHs_ = false;
emit lookingUpHsChanged();
}
back_layout_ = new QHBoxLayout(); QString
back_layout_->setSpacing(0); RegisterPage::initialDeviceName() const
back_layout_->setContentsMargins(5, 5, -1, -1); {
return QString::fromStdString(LoginPage::initialDeviceName_());
back_button_ = new FlatButton(this);
back_button_->setMinimumSize(QSize(30, 30));
QIcon icon;
icon.addFile(QStringLiteral(":/icons/icons/ui/angle-arrow-left.svg"));
back_button_->setIcon(icon);
back_button_->setIconSize(QSize(32, 32));
back_layout_->addWidget(back_button_, 0, Qt::AlignLeft | Qt::AlignVCenter);
back_layout_->addStretch(1);
QIcon logo;
logo.addFile(QStringLiteral(":/logos/register.png"));
logo_ = new QLabel(this);
logo_->setPixmap(logo.pixmap(128));
logo_layout_ = new QHBoxLayout();
logo_layout_->setContentsMargins(0, 0, 0, 0);
logo_layout_->addWidget(logo_, 0, Qt::AlignHCenter);
form_wrapper_ = new QHBoxLayout();
form_widget_ = new QWidget();
form_widget_->setMinimumSize(QSize(350, 300));
form_layout_ = new QVBoxLayout();
form_layout_->setSpacing(20);
form_layout_->setContentsMargins(0, 0, 0, 40);
form_widget_->setLayout(form_layout_);
form_wrapper_->addStretch(1);
form_wrapper_->addWidget(form_widget_);
form_wrapper_->addStretch(1);
username_input_ = new TextField();
username_input_->setLabel(tr("Username"));
username_input_->setRegexp(QRegularExpression(QStringLiteral("[a-z0-9._=/-]+")));
username_input_->setToolTip(tr("The username must not be empty, and must contain only the "
"characters a-z, 0-9, ., _, =, -, and /."));
password_input_ = new TextField();
password_input_->setLabel(tr("Password"));
password_input_->setRegexp(QRegularExpression(QStringLiteral("^.{8,}$")));
password_input_->setEchoMode(QLineEdit::Password);
password_input_->setToolTip(tr("Please choose a secure password. The exact requirements "
"for password strength may depend on your server."));
password_confirmation_ = new TextField();
password_confirmation_->setLabel(tr("Password confirmation"));
password_confirmation_->setEchoMode(QLineEdit::Password);
server_input_ = new TextField();
server_input_->setLabel(tr("Homeserver"));
server_input_->setRegexp(QRegularExpression(QStringLiteral(".+")));
server_input_->setToolTip(
tr("A server that allows registration. Since matrix is decentralized, you need to first "
"find a server you can register on or host your own."));
error_username_label_ = new QLabel(this);
error_username_label_->setWordWrap(true);
error_username_label_->hide();
error_password_label_ = new QLabel(this);
error_password_label_->setWordWrap(true);
error_password_label_->hide();
error_password_confirmation_label_ = new QLabel(this);
error_password_confirmation_label_->setWordWrap(true);
error_password_confirmation_label_->hide();
error_server_label_ = new QLabel(this);
error_server_label_->setWordWrap(true);
error_server_label_->hide();
form_layout_->addWidget(username_input_, Qt::AlignHCenter);
form_layout_->addWidget(error_username_label_, Qt::AlignHCenter);
form_layout_->addWidget(password_input_, Qt::AlignHCenter);
form_layout_->addWidget(error_password_label_, Qt::AlignHCenter);
form_layout_->addWidget(password_confirmation_, Qt::AlignHCenter);
form_layout_->addWidget(error_password_confirmation_label_, Qt::AlignHCenter);
form_layout_->addWidget(server_input_, Qt::AlignHCenter);
form_layout_->addWidget(error_server_label_, Qt::AlignHCenter);
button_layout_ = new QHBoxLayout();
button_layout_->setSpacing(0);
button_layout_->setContentsMargins(0, 0, 0, 0);
error_label_ = new QLabel(this);
error_label_->setWordWrap(true);
register_button_ = new RaisedButton(tr("REGISTER"), this);
register_button_->setMinimumSize(350, 65);
register_button_->setFontSize(conf::btn::fontSize);
register_button_->setCornerRadius(conf::btn::cornerRadius);
button_layout_->addStretch(1);
button_layout_->addWidget(register_button_);
button_layout_->addStretch(1);
top_layout_->addLayout(back_layout_);
top_layout_->addLayout(logo_layout_);
top_layout_->addLayout(form_wrapper_);
top_layout_->addStretch(1);
top_layout_->addLayout(button_layout_);
top_layout_->addWidget(error_label_, 0, Qt::AlignHCenter);
top_layout_->addStretch(1);
setLayout(top_layout_);
connect(back_button_, SIGNAL(clicked()), this, SLOT(onBackButtonClicked()));
connect(register_button_, SIGNAL(clicked()), this, SLOT(onRegisterButtonClicked()));
connect(username_input_, SIGNAL(returnPressed()), register_button_, SLOT(click()));
connect(username_input_, &TextField::editingFinished, this, &RegisterPage::checkUsername);
connect(password_input_, SIGNAL(returnPressed()), register_button_, SLOT(click()));
connect(password_input_, &TextField::editingFinished, this, &RegisterPage::checkPassword);
connect(password_confirmation_, SIGNAL(returnPressed()), register_button_, SLOT(click()));
connect(password_confirmation_,
&TextField::editingFinished,
this,
&RegisterPage::checkPasswordConfirmation);
connect(server_input_, SIGNAL(returnPressed()), register_button_, SLOT(click()));
connect(server_input_, &TextField::editingFinished, this, &RegisterPage::checkServer);
connect(
this,
&RegisterPage::serverError,
this,
[this](const QString &msg) {
server_input_->setValid(false);
showError(error_server_label_, msg);
},
Qt::QueuedConnection);
connect(this, &RegisterPage::wellKnownLookup, this, &RegisterPage::doWellKnownLookup);
connect(this, &RegisterPage::versionsCheck, this, &RegisterPage::doVersionsCheck);
connect(this, &RegisterPage::registration, this, &RegisterPage::doRegistration);
} }
void void
RegisterPage::onBackButtonClicked() RegisterPage::setServer(QString server)
{ {
emit backButtonClicked(); if (server == lastServer)
} return;
void lastServer = server;
RegisterPage::showError(const QString &msg)
{
emit errorOccurred();
auto rect = QFontMetrics(font()).boundingRect(msg);
int width = rect.width();
int height = rect.height();
error_label_->setFixedHeight(qCeil(width / 200.0) * height);
error_label_->setText(msg);
}
void http::client()->set_server(server.toStdString());
RegisterPage::showError(QLabel *label, const QString &msg) http::client()->verify_certificates(!UserSettings::instance()->disableCertificateValidation());
{
emit errorOccurred();
auto rect = QFontMetrics(font()).boundingRect(msg);
int width = rect.width();
int height = rect.height();
label->setFixedHeight((int)qCeil(width / 200.0) * height);
label->setText(msg);
label->show();
}
bool hsError_.clear();
RegisterPage::checkOneField(QLabel *label, const TextField *t_field, const QString &msg) emit hsErrorChanged();
{ supported_ = false;
if (t_field->isValid()) { lookingUpHs_ = true;
label->hide(); emit lookingUpHsChanged();
return true;
} else {
showError(label, msg);
return false;
}
}
bool
RegisterPage::checkUsername()
{
return checkOneField(error_username_label_,
username_input_,
tr("The username must not be empty, and must contain only the "
"characters a-z, 0-9, ., _, =, -, and /."));
}
bool
RegisterPage::checkPassword()
{
return checkOneField(
error_password_label_, password_input_, tr("Password is not long enough (min 8 chars)"));
}
bool
RegisterPage::checkPasswordConfirmation()
{
if (password_input_->text() == password_confirmation_->text()) {
error_password_confirmation_label_->hide();
password_confirmation_->setValid(true);
return true;
} else {
showError(error_password_confirmation_label_, tr("Passwords don't match"));
password_confirmation_->setValid(false);
return false;
}
}
bool
RegisterPage::checkServer()
{
// This doesn't check that the server is reachable,
// just that the input is not obviously wrong.
return checkOneField(error_server_label_, server_input_, tr("Invalid server name"));
}
void
RegisterPage::onRegisterButtonClicked()
{
if (checkUsername() && checkPassword() && checkPasswordConfirmation() && checkServer()) {
auto server = server_input_->text().toStdString();
http::client()->set_server(server);
http::client()->verify_certificates(
!UserSettings::instance()->disableCertificateValidation());
// This starts a chain of `emit`s which ends up doing the
// registration. Signals are used rather than normal function
// calls so that the dialogs used in UIA work correctly.
//
// The sequence of events looks something like this:
//
// doKnownLookup
// v
// doVersionsCheck
// v
// doRegistration -> loops the UIAHandler until complete
emit wellKnownLookup();
emit registering();
}
}
void
RegisterPage::doWellKnownLookup()
{
http::client()->well_known( http::client()->well_known(
[this](const mtx::responses::WellKnown &res, mtx::http::RequestErr err) { [this](const mtx::responses::WellKnown &res, mtx::http::RequestErr err) {
if (err) { if (err) {
if (err->status_code == 404) { if (err->status_code == 404) {
nhlog::net()->info("Autodiscovery: No .well-known."); nhlog::net()->info("Autodiscovery: No .well-known.");
// Check that the homeserver can be reached // Check that the homeserver can be reached
emit versionsCheck(); versionsCheck();
return; return;
} }
if (!err->parse_error.empty()) { if (!err->parse_error.empty()) {
emit serverError(tr("Autodiscovery failed. Received malformed response.")); setHsError(tr("Autodiscovery failed. Received malformed response."));
nhlog::net()->error("Autodiscovery failed. Received malformed response."); nhlog::net()->error("Autodiscovery failed. Received malformed response.");
emit hsErrorChanged();
return; return;
} }
emit serverError(tr("Autodiscovery failed. Unknown error when " setHsError(tr("Autodiscovery failed. Unknown error when requesting .well-known."));
"requesting .well-known."));
nhlog::net()->error("Autodiscovery failed. Unknown error when " nhlog::net()->error("Autodiscovery failed. Unknown error when "
"requesting .well-known. {} {}", "requesting .well-known. {} {}",
err->status_code, err->status_code,
@ -319,98 +88,140 @@ RegisterPage::doWellKnownLookup()
nhlog::net()->info("Autodiscovery: Discovered '" + res.homeserver.base_url + "'"); nhlog::net()->info("Autodiscovery: Discovered '" + res.homeserver.base_url + "'");
http::client()->set_server(res.homeserver.base_url); http::client()->set_server(res.homeserver.base_url);
emit hsErrorChanged();
// Check that the homeserver can be reached // Check that the homeserver can be reached
emit versionsCheck(); versionsCheck();
}); });
} }
void void
RegisterPage::doVersionsCheck() RegisterPage::versionsCheck()
{ {
// Make a request to /_matrix/client/versions to check the address // Make a request to /_matrix/client/versions to check the address
// given is a Matrix homeserver. // given is a Matrix homeserver.
http::client()->versions([this](const mtx::responses::Versions &, mtx::http::RequestErr err) { http::client()->versions([this](const mtx::responses::Versions &, mtx::http::RequestErr err) {
if (err) { if (err) {
if (err->status_code == 404) { if (err->status_code == 404) {
emit serverError(tr("The required endpoints were not found. Possibly " setHsError(
"not a Matrix server.")); tr("The required endpoints were not found. Possibly not a Matrix server."));
emit hsErrorChanged();
return; return;
} }
if (!err->parse_error.empty()) { if (!err->parse_error.empty()) {
emit serverError(tr("Received malformed response. Make sure the homeserver " setHsError(
"domain is valid.")); tr("Received malformed response. Make sure the homeserver domain is valid."));
emit hsErrorChanged();
return; return;
} }
emit serverError(tr("An unknown error occured. Make sure the " setHsError(tr("An unknown error occured. Make sure the homeserver domain is valid."));
"homeserver domain is valid.")); emit hsErrorChanged();
return; return;
} }
// Attempt registration without an `auth` dict http::client()->registration(
emit registration(); [this](const mtx::responses::Register &, mtx::http::RequestErr e) {
nhlog::net()->debug("Registration check: {}", e);
if (!e) {
setHsError(tr("Server does not support querying registration flows!"));
emit hsErrorChanged();
return;
}
if (e->status_code != 401) {
setHsError(tr("Server does not support registration."));
emit hsErrorChanged();
return;
}
supported_ = true;
lookingUpHs_ = false;
emit lookingUpHsChanged();
});
}); });
} }
void void
RegisterPage::doRegistration() RegisterPage::checkUsername(QString name)
{ {
// These inputs should still be alright, but check just in case usernameAvailable_ = usernameUnavailable_ = false;
if (checkUsername() && checkPassword() && checkPasswordConfirmation()) { usernameError_.clear();
auto username = username_input_->text().toStdString(); lookingUpUsername_ = true;
auto password = password_input_->text().toStdString(); emit lookingUpUsernameChanged();
connect(UIA::instance(), &UIA::error, this, [this](QString msg) {
showError(msg);
disconnect(UIA::instance(), &UIA::error, this, nullptr);
});
http::client()->registration(
username,
password,
::UIA::instance()->genericHandler(QStringLiteral("Registration")),
registrationCb());
}
}
mtx::http::Callback<mtx::responses::Register> http::client()->register_username_available(
RegisterPage::registrationCb() name.toStdString(),
{ [this](const mtx::responses::Available &available, mtx::http::RequestErr e) {
// Return a function to be used as the callback when an attempt at if (e) {
// registration is made. if (e->matrix_error.errcode == mtx::errors::ErrorCode::M_INVALID_USERNAME) {
return [this](const mtx::responses::Register &res, mtx::http::RequestErr err) { usernameError_ = tr("Invalid username.");
if (!err) { } else if (e->matrix_error.errcode == mtx::errors::ErrorCode::M_USER_IN_USE) {
http::client()->set_user(res.user_id); usernameError_ = tr("Name already in use.");
http::client()->set_access_token(res.access_token); } else if (e->matrix_error.errcode == mtx::errors::ErrorCode::M_EXCLUSIVE) {
emit registerOk(); usernameError_ = tr("Part of the reserved namespace.");
disconnect(UIA::instance(), &UIA::error, this, nullptr); } else {
return; }
}
// The server requires registration flows. usernameAvailable_ = false;
if (err->status_code == 401) { usernameUnavailable_ = true;
if (err->matrix_error.unauthorized.flows.empty()) { } else {
nhlog::net()->warn("failed to retrieve registration flows: " usernameAvailable_ = available.available;
"status_code({}), matrix_error({}) ", usernameUnavailable_ = !available.available;
static_cast<int>(err->status_code), }
err->matrix_error.error); lookingUpUsername_ = false;
showError(QString::fromStdString(err->matrix_error.error)); emit lookingUpUsernameChanged();
} });
return;
}
nhlog::net()->error("failed to register: status_code ({}), matrix_error({})",
static_cast<int>(err->status_code),
err->matrix_error.error);
showError(QString::fromStdString(err->matrix_error.error));
};
} }
void void
RegisterPage::paintEvent(QPaintEvent *) RegisterPage::startRegistration(QString username, QString password, QString devicename)
{ {
QStyleOption opt; // These inputs should still be alright, but check just in case
opt.initFrom(this); if (!username.isEmpty() && !password.isEmpty() && usernameAvailable_ && supported_) {
QPainter p(this); registrationError_.clear();
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); emit errorChanged();
registering_ = true;
emit registeringChanged();
connect(UIA::instance(), &UIA::error, this, [this](QString msg) {
setError(msg);
disconnect(UIA::instance(), &UIA::error, this, nullptr);
});
http::client()->registration(
username.toStdString(),
password.toStdString(),
::UIA::instance()->genericHandler(QStringLiteral("Registration")),
[this](const mtx::responses::Register &res, mtx::http::RequestErr err) {
registering_ = false;
emit registeringChanged();
if (!err) {
http::client()->set_user(res.user_id);
http::client()->set_access_token(res.access_token);
MainWindow::instance()->showChatPage();
disconnect(UIA::instance(), &UIA::error, this, nullptr);
return;
}
// The server requires registration flows.
if (err->status_code == 401 && err->matrix_error.unauthorized.flows.empty()) {
nhlog::net()->warn("failed to retrieve registration flows: "
"status_code({}), matrix_error({}) ",
static_cast<int>(err->status_code),
err->matrix_error.error);
setError(QString::fromStdString(err->matrix_error.error));
disconnect(UIA::instance(), &UIA::error, this, nullptr);
return;
}
nhlog::net()->error("failed to register: status_code ({}), matrix_error({})",
static_cast<int>(err->status_code),
err->matrix_error.error);
setError(QString::fromStdString(err->matrix_error.error));
disconnect(UIA::instance(), &UIA::error, this, nullptr);
},
devicename.isEmpty() ? LoginPage::initialDeviceName_() : devicename.toStdString());
}
} }

View file

@ -6,88 +6,67 @@
#pragma once #pragma once
#include <QWidget> #include <QObject>
#include <QString>
#include <memory>
#include <mtx/user_interactive.hpp> #include <mtx/user_interactive.hpp>
#include <mtxclient/http/client.hpp> #include <mtxclient/http/client.hpp>
class FlatButton; class RegisterPage : public QObject
class RaisedButton;
class TextField;
class QLabel;
class QVBoxLayout;
class QHBoxLayout;
class RegisterPage : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: Q_PROPERTY(QString error READ error NOTIFY errorChanged)
RegisterPage(QWidget *parent = nullptr); Q_PROPERTY(QString hsError READ hsError NOTIFY hsErrorChanged)
Q_PROPERTY(QString usernameError READ usernameError NOTIFY lookingUpUsernameChanged)
Q_PROPERTY(bool registering READ registering NOTIFY registeringChanged)
Q_PROPERTY(bool supported READ supported NOTIFY lookingUpHsChanged)
Q_PROPERTY(bool lookingUpHs READ lookingUpHs NOTIFY lookingUpHsChanged)
Q_PROPERTY(bool lookingUpUsername READ lookingUpUsername NOTIFY lookingUpUsernameChanged)
Q_PROPERTY(bool usernameAvailable READ usernameAvailable NOTIFY lookingUpUsernameChanged)
Q_PROPERTY(bool usernameUnavailable READ usernameUnavailable NOTIFY lookingUpUsernameChanged)
protected: public:
void paintEvent(QPaintEvent *event) override; RegisterPage(QObject *parent = nullptr);
Q_INVOKABLE void setServer(QString server);
Q_INVOKABLE void checkUsername(QString name);
Q_INVOKABLE void startRegistration(QString username, QString password, QString deviceName);
Q_INVOKABLE QString initialDeviceName() const;
bool registering() const { return registering_; }
bool supported() const { return supported_; }
bool lookingUpHs() const { return lookingUpHs_; }
bool lookingUpUsername() const { return lookingUpUsername_; }
bool usernameAvailable() const { return usernameAvailable_; }
bool usernameUnavailable() const { return usernameUnavailable_; }
QString error() const { return registrationError_; }
QString usernameError() const { return usernameError_; }
QString hsError() const { return hsError_; }
signals: signals:
void backButtonClicked(); void errorChanged();
void errorOccurred(); void hsErrorChanged();
//! Used to trigger the corresponding slot outside of the main thread. void registeringChanged();
void serverError(const QString &err); void lookingUpHsChanged();
void lookingUpUsernameChanged();
void wellKnownLookup();
void versionsCheck();
void registration();
void registering();
void registerOk();
private slots:
void onBackButtonClicked();
void onRegisterButtonClicked();
// function for showing different errors
void showError(const QString &msg);
void showError(QLabel *label, const QString &msg);
bool checkOneField(QLabel *label, const TextField *t_field, const QString &msg);
bool checkUsername();
bool checkPassword();
bool checkPasswordConfirmation();
bool checkServer();
void doWellKnownLookup();
void doVersionsCheck();
void doRegistration();
mtx::http::Callback<mtx::responses::Register> registrationCb();
private: private:
QVBoxLayout *top_layout_; void versionsCheck();
QHBoxLayout *back_layout_; void setHsError(QString err);
QHBoxLayout *logo_layout_; void setError(QString err);
QHBoxLayout *button_layout_;
QLabel *logo_; QString registrationError_, hsError_, usernameError_;
QLabel *error_label_;
QLabel *error_username_label_;
QLabel *error_password_label_;
QLabel *error_password_confirmation_label_;
QLabel *error_server_label_;
QLabel *error_registration_token_label_;
FlatButton *back_button_; bool registering_;
RaisedButton *register_button_; bool supported_;
bool lookingUpHs_;
bool lookingUpUsername_;
bool usernameAvailable_;
bool usernameUnavailable_;
QWidget *form_widget_; QString lastServer;
QHBoxLayout *form_wrapper_;
QVBoxLayout *form_layout_;
TextField *username_input_;
TextField *password_input_;
TextField *password_confirmation_;
TextField *server_input_;
TextField *registration_token_input_;
}; };