From 1a32f34505fd7960304e15e5ca539be877d06c24 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 11 Aug 2017 17:53:29 +0200 Subject: Strip unneeded stuff, simplify pam dialog handling, add safety timeout of 10 seconds --- src/loginform.cpp | 173 +++++++++++++++++++++++++++-------------------------- src/loginform.h | 12 +++- src/loginform.ui | 99 ++++++++++++++---------------- src/mainwindow.cpp | 5 +- 4 files changed, 146 insertions(+), 143 deletions(-) diff --git a/src/loginform.cpp b/src/loginform.cpp index 3f40e81..07d4094 100644 --- a/src/loginform.cpp +++ b/src/loginform.cpp @@ -23,27 +23,21 @@ #include "ui_loginform.h" #include "settings.h" -const int KeyRole = QLightDM::SessionsModel::KeyRole; - int rows(QAbstractItemModel& model) { return model.rowCount(QModelIndex()); } -QString displayData(QAbstractItemModel& model, int row, int role) -{ - QModelIndex modelIndex = model.index(row, 0); - return model.data(modelIndex, role).toString(); -} - LoginForm::LoginForm(QWidget *parent) : QWidget(parent), ui(new Ui::LoginForm), m_Greeter(), power(this), - sessionsModel() + sessionsModel(), + clearMsg(false) { if (!m_Greeter.connectSync()) { - close(); + exit(0); + return; } ui->setupUi(this); @@ -71,57 +65,55 @@ void LoginForm::initialize() ui->iconLabel->setPixmap(icon.scaled(ui->iconLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); ui->hostnameLabel->setText(m_Greeter.hostname()); - ui->sessionCombo->setModel(&sessionsModel); - addLeaveEntry(power.canShutdown(), "system-shutdown", tr("Shutdown"), "shutdown"); addLeaveEntry(power.canRestart(), "system-reboot", tr("Restart"), "restart"); - addLeaveEntry(power.canHibernate(), "system-suspend-hibernate", tr("Hibernate"), "hibernate"); - addLeaveEntry(power.canSuspend(), "system-suspend", tr("Suspend"), "suspend"); + //addLeaveEntry(power.canHibernate(), "system-suspend-hibernate", tr("Hibernate"), "hibernate"); + //addLeaveEntry(power.canSuspend(), "system-suspend", tr("Suspend"), "suspend"); ui->leaveComboBox->setDisabled(ui->leaveComboBox->count() <= 1); - ui->sessionCombo->setCurrentIndex(0); - setCurrentSession(m_Greeter.defaultSessionHint()); + cancelLoginTimer.setInterval(10000); + cancelLoginTimer.setSingleShot(true); + connect(&cancelLoginTimer, SIGNAL(timeout()), this, SLOT(cancelLogin())); - connect(ui->userInput, SIGNAL(editingFinished()), this, SLOT(userChanged())); + //connect(ui->userInput, SIGNAL(editingFinished()), this, SLOT(userChanged())); connect(ui->leaveComboBox, SIGNAL(activated(int)), this, SLOT(leaveDropDownActivated(int))); connect(&m_Greeter, SIGNAL(showPrompt(QString, QLightDM::Greeter::PromptType)), this, SLOT(onPrompt(QString, QLightDM::Greeter::PromptType))); - connect(&m_Greeter, SIGNAL(authenticationComplete()), this, SLOT(authenticationComplete())); + connect(&m_Greeter, SIGNAL(showMessage(QString, QLightDM::Greeter::MessageType)), this, SLOT(onMessage(QString, QLightDM::Greeter::MessageType))); + connect(&m_Greeter, SIGNAL(authenticationComplete()), this, SLOT(onAuthenticationComplete())); - ui->passwordInput->setEnabled(false); ui->passwordInput->clear(); - - if (! m_Greeter.hideUsersHint()) { - QStringList knownUsers; - QLightDM::UsersModel usersModel; - for (int i = 0; i < usersModel.rowCount(QModelIndex()); i++) { - knownUsers << usersModel.data(usersModel.index(i, 0), QLightDM::UsersModel::NameRole).toString(); - } - ui->userInput->setCompleter(new QCompleter(knownUsers)); - ui->userInput->completer()->setCompletionMode(QCompleter::InlineCompletion); - } - - QString user = Cache().getLastUser(); - if (user.isEmpty()) { - user = m_Greeter.selectUserHint(); - } - ui->userInput->setText(user); - userChanged(); } -void LoginForm::userChanged() +void LoginForm::startAuthentication() { - setCurrentSession(Cache().getLastSession(ui->userInput->text())); + qDebug() << "Start auth"; + //setCurrentSession(Cache().getLastSession(ui->userInput->text())); - if (m_Greeter.inAuthentication()) { - m_Greeter.cancelAuthentication(); - } - if (! ui->userInput->text().isEmpty()) { - m_Greeter.authenticate(ui->userInput->text()); - ui->passwordInput->setFocus(); + if (m_Greeter.inAuthentication()) { + m_Greeter.cancelAuthentication(); + } + if (ui->userInput->text().isEmpty()) { + ui->userInput->setFocus(); + return; } - else { - ui->userInput->setFocus(); + if (ui->passwordInput->text().isEmpty()) { + ui->passwordInput->setFocus(); + return; } + + ui->messageLabel->setText(tr("Logging in...")); + clearMsg = false; + ui->userInput->setEnabled(false); + ui->passwordInput->setEnabled(false); + cancelLoginTimer.start(); + m_Greeter.authenticate(ui->userInput->text()); +} + +void LoginForm::onPrompt(QString prompt, QLightDM::Greeter::PromptType promptType) +{ + qDebug() << "Prompt: " << prompt; + m_Greeter.respond(ui->passwordInput->text()); + ui->passwordInput->clear(); } void LoginForm::leaveDropDownActivated(int index) @@ -133,20 +125,13 @@ void LoginForm::leaveDropDownActivated(int index) else if (actionName == "suspend") power.suspend(); } -void LoginForm::respond() +void LoginForm::onMessage(QString message, QLightDM::Greeter::MessageType type) { - m_Greeter.respond(ui->passwordInput->text().trimmed()); - ui->passwordInput->clear(); - ui->passwordInput->setEnabled(false); -} - -void LoginForm::onPrompt(QString prompt, QLightDM::Greeter::PromptType promptType) -{ - ui->passwordInput->setEnabled(true); - ui->passwordInput->setFocus(); + qDebug() << "Message: " << message; + ui->messageLabel->setText(message); + clearMsg = true; } - void LoginForm::addLeaveEntry(bool canDo, QString iconName, QString text, QString actionName) { if (canDo) { @@ -154,44 +139,64 @@ void LoginForm::addLeaveEntry(bool canDo, QString iconName, QString text, QStrin } } -QString LoginForm::currentSession() -{ - QModelIndex index = sessionsModel.index(ui->sessionCombo->currentIndex(), 0, QModelIndex()); - return sessionsModel.data(index, QLightDM::SessionsModel::KeyRole).toString(); -} - -void LoginForm::setCurrentSession(QString session) +void LoginForm::onAuthenticationComplete() { - for (int i = 0; i < ui->sessionCombo->count(); i++) { - if (session == sessionsModel.data(sessionsModel.index(i, 0), KeyRole).toString()) { - ui->sessionCombo->setCurrentIndex(i); - return; + if (m_Greeter.isAuthenticated()) { + qDebug() << "Auth complete, start session"; + ui->messageLabel->setText(tr("Starting session...")); + QModelIndex i = sessionsModel.index(0, 0); + if (m_Greeter.startSessionSync(sessionsModel.data(i, QLightDM::SessionsModel::KeyRole).toString())) { + cancelLoginTimer.stop(); + } else { + ui->messageLabel->setText(tr("Cannot open session")); + clearMsg = true; } + } else { + qDebug() << "Auth failed, cancelling..."; + cancelLogin(); } } - -void LoginForm::authenticationComplete() +void LoginForm::cancelLogin() { - if (m_Greeter.isAuthenticated()) { - Cache().setLastUser(ui->userInput->text()); - Cache().setLastSession(ui->userInput->text(), currentSession()); - Cache().sync(); - m_Greeter.startSessionSync(currentSession()); - } - else { - ui->passwordInput->clear(); - userChanged(); - } + qDebug() << "Cancel login"; + if (m_Greeter.inAuthentication()) { + qDebug() << "Was in authentication"; + m_Greeter.cancelAuthentication(); + } + cancelLoginTimer.stop(); + ui->passwordInput->clear(); + ui->userInput->setEnabled(true); + ui->passwordInput->setEnabled(true); + if (!clearMsg) { + ui->messageLabel->setText(tr("Login failed")); + } + ui->userInput->setFocus(); + ui->userInput->selectAll(); + clearMsg = true; } void LoginForm::keyPressEvent(QKeyEvent *event) { + if (clearMsg) { + clearMsg = false; + ui->messageLabel->clear(); + } if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { - respond(); - } - else { - QWidget::keyPressEvent(event); + if (!ui->userInput->isEnabled()) { + // Ignore if auth in progress + return; + } + if (ui->userInput->hasFocus()) { + ui->passwordInput->setFocus(); + return; + } + if (ui->passwordInput->hasFocus()) { + startAuthentication(); + return; + } } + // Fallback: Passthrough + QWidget::keyPressEvent(event); } diff --git a/src/loginform.h b/src/loginform.h index 8346a58..6dfc390 100644 --- a/src/loginform.h +++ b/src/loginform.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -37,11 +38,12 @@ public: virtual void setFocus(Qt::FocusReason reason); public slots: - void userChanged(); + void startAuthentication(); void leaveDropDownActivated(int index); - void respond(); void onPrompt(QString prompt, QLightDM::Greeter::PromptType promptType); - void authenticationComplete(); + void onMessage(QString prompt, QLightDM::Greeter::MessageType messageType); + void onAuthenticationComplete(); + void cancelLogin(); protected: virtual void keyPressEvent(QKeyEvent *event); @@ -59,6 +61,10 @@ private: QLightDM::SessionsModel sessionsModel; QMap powerSlots; + + QTimer cancelLoginTimer; + + bool clearMsg; }; #endif // LOGINFORM_H diff --git a/src/loginform.ui b/src/loginform.ui index 3525a9d..b4c4cd5 100644 --- a/src/loginform.ui +++ b/src/loginform.ui @@ -7,7 +7,7 @@ 0 0 350 - 325 + 354 @@ -34,7 +34,8 @@ } QComboBox, -QPushButton { +QPushButton, +#messageLabel { border: 1px solid silver; background-color: rgb(200, 200, 200); } @@ -103,32 +104,29 @@ QComboBox::down-arrow { - - + + - 50 - 50 + 80 + 60 - 50 - 50 + 80 + 60 - - - - - :/resources/rqt-2.png - - - true - - - Qt::AlignCenter - + + + + + + + :/resources/leaveIcon.svg:/resources/leaveIcon.svg + + @@ -160,7 +158,7 @@ QComboBox::down-arrow { - + @@ -201,58 +199,48 @@ QComboBox::down-arrow { - - + + - 200 - 60 + 50 + 50 - 10000 - 60 + 50 + 50 - - - Bitstream Vera Sans - 50 - false - + + - - QComboBox::AdjustToContents + + :/resources/rqt-2.png - + true + + Qt::AlignCenter + - - + + - 80 - 60 + 200 + 30 - - - 80 - 60 - + + false + + + - - - - - - - :/resources/leaveIcon.svg:/resources/leaveIcon.svg - - @@ -261,7 +249,8 @@ QComboBox::down-arrow { - + + diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a8dbd21..56f927f 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -117,7 +117,10 @@ void MainWindow::setBackground() palette.setColor(QPalette::Background, Qt::black); } else { - QBrush brush(backgroundImage.scaled(rect.width(), rect.height())); + backgroundImage = backgroundImage.scaled(rect.width(), rect.height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); + int xoff = (backgroundImage.width() - rect.width()) / 2; + int yoff = (backgroundImage.height() - rect.height()) / 2; + QBrush brush(backgroundImage.copy(xoff, yoff, backgroundImage.width(), backgroundImage.height())); palette.setBrush(this->backgroundRole(), brush); } this->setPalette(palette); -- cgit v1.2.3-55-g7522