diff --git a/src/LoginPage.cpp b/src/LoginPage.cpp index 9a920d1d..ac625db1 100644 --- a/src/LoginPage.cpp +++ b/src/LoginPage.cpp @@ -16,9 +16,11 @@ */ #include +#include #include #include #include +#include #include #include @@ -95,8 +97,6 @@ LoginPage::LoginPage(QWidget *parent) "address there, if your server doesn't support .well-known lookup.\nExample: " "@user:server.my\nIf Nheko fails to discover your homeserver, it will show you a " "field to enter the server manually.")); - matrixid_input_->setValidator( - new QRegularExpressionValidator(QRegularExpression("@.+?:.{3,}"), this)); spinner_ = new LoadingIndicator(this); spinner_->setFixedHeight(40); @@ -110,6 +110,12 @@ LoginPage::LoginPage(QWidget *parent) matrixidLayout_ = new QHBoxLayout(); matrixidLayout_->addWidget(matrixid_input_, 0, Qt::AlignVCenter); + QFont font; + + error_matrixid_label_ = new QLabel(this); + error_matrixid_label_->setFont(font); + error_matrixid_label_->setWordWrap(true); + password_input_ = new TextField(this); password_input_->setLabel(tr("Password")); password_input_->setEchoMode(QLineEdit::Password); @@ -132,10 +138,13 @@ LoginPage::LoginPage(QWidget *parent) serverLayout_->addWidget(serverInput_, 0, Qt::AlignVCenter); form_layout_->addLayout(matrixidLayout_); + form_layout_->addWidget(error_matrixid_label_, 0, Qt::AlignHCenter); form_layout_->addWidget(password_input_); form_layout_->addWidget(deviceName_, Qt::AlignHCenter); form_layout_->addLayout(serverLayout_); + error_matrixid_label_->hide(); + button_layout_ = new QHBoxLayout(); button_layout_->setSpacing(0); button_layout_->setContentsMargins(0, 0, 0, 30); @@ -149,8 +158,6 @@ LoginPage::LoginPage(QWidget *parent) button_layout_->addWidget(login_button_); button_layout_->addStretch(1); - QFont font; - error_label_ = new QLabel(this); error_label_->setFont(font); error_label_->setWordWrap(true); @@ -183,9 +190,30 @@ LoginPage::LoginPage(QWidget *parent) void LoginPage::loginError(const QString &msg) { + auto rect = QFontMetrics(font()).boundingRect(msg); + int width = rect.width(); + int height = rect.height(); + error_label_->setFixedHeight(qCeil(width / 200) * height); error_label_->setText(msg); } +void +LoginPage::matrixIdError(const QString &msg) +{ + error_matrixid_label_->show(); + error_matrixid_label_->setText(msg); + matrixid_input_->setValid(false); +} + +bool +LoginPage::isMatrixIdValid() +{ + QRegularExpressionValidator v(QRegularExpression("@.+?:.{3,}"), this); + QString s = matrixid_input_->text(); + int pos = 0; + return v.validate(s, pos) == QValidator::Acceptable; +} + void LoginPage::onMatrixIdEntered() { @@ -193,10 +221,20 @@ LoginPage::onMatrixIdEntered() User user; + if (!isMatrixIdValid()) { + matrixIdError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); + return; + } else { + error_matrixid_label_->setText(""); + error_matrixid_label_->hide(); + matrixid_input_->setValid(true); + } + try { user = parse(matrixid_input_->text().toStdString()); } catch (const std::exception &e) { - return loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); + matrixIdError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); + return; } QString homeServer = QString::fromStdString(user.hostname()); @@ -307,7 +345,7 @@ LoginPage::onServerAddressEntered() void LoginPage::versionError(const QString &error) { - error_label_->setText(error); + loginError(error); serverInput_->show(); spinner_->stop(); @@ -345,10 +383,20 @@ LoginPage::onLoginButtonClicked() User user; + if (!isMatrixIdValid()) { + matrixIdError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); + return; + } else { + error_matrixid_label_->setText(""); + error_matrixid_label_->hide(); + matrixid_input_->setValid(true); + } + try { user = parse(matrixid_input_->text().toStdString()); } catch (const std::exception &e) { - return loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); + matrixIdError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); + return; } if (loginMethod == LoginMethod::Password) { diff --git a/src/LoginPage.h b/src/LoginPage.h index c9220297..92b60afe 100644 --- a/src/LoginPage.h +++ b/src/LoginPage.h @@ -67,6 +67,7 @@ protected: public slots: // Displays errors produced during the login. void loginError(const QString &msg); + void matrixIdError(const QString &msg); private slots: // Callback for the back button. @@ -112,6 +113,7 @@ private: QLabel *logo_; QLabel *error_label_; + QLabel *error_matrixid_label_; QHBoxLayout *serverLayout_; QHBoxLayout *matrixidLayout_; diff --git a/src/ui/TextField.cpp b/src/ui/TextField.cpp index 27584693..f717bcba 100644 --- a/src/ui/TextField.cpp +++ b/src/ui/TextField.cpp @@ -69,6 +69,18 @@ TextField::hasLabel() const return show_label_; } +bool +TextField::isValid() const +{ + return is_valid_; +} + +void +TextField::setValid(bool valid) +{ + is_valid_ = valid; +} + void TextField::setLabelFontSize(qreal size) { @@ -147,7 +159,7 @@ QColor TextField::underlineColor() const { if (!underline_color_.isValid()) { - if (hasAcceptableInput() || !isModified()) + if ((hasAcceptableInput() && isValid()) || !isModified()) return QPalette().color(QPalette::Highlight); else return Qt::red; diff --git a/src/ui/TextField.h b/src/ui/TextField.h index 85d5036d..966155f4 100644 --- a/src/ui/TextField.h +++ b/src/ui/TextField.h @@ -30,6 +30,7 @@ public: void setLabelFontSize(qreal size); void setShowLabel(bool value); void setUnderlineColor(const QColor &color); + void setValid(bool valid); QColor inkColor() const; QColor labelColor() const; @@ -37,6 +38,7 @@ public: QColor backgroundColor() const; QString label() const; bool hasLabel() const; + bool isValid() const; qreal labelFontSize() const; protected: @@ -54,6 +56,7 @@ private: TextFieldLabel *label_; TextFieldStateMachine *state_machine_; bool show_label_; + bool is_valid_; qreal label_font_size_; };