diff options
author | Simon Rettberg | 2017-08-11 17:53:29 +0200 |
---|---|---|
committer | Simon Rettberg | 2017-08-11 17:53:29 +0200 |
commit | 1a32f34505fd7960304e15e5ca539be877d06c24 (patch) | |
tree | 2e825bb8e77033193777207343c6ffa7a52075b7 /src/loginform.cpp | |
parent | Move code to src/, tweak CMakeLists.txt (diff) | |
download | slxgreeter-1a32f34505fd7960304e15e5ca539be877d06c24.tar.gz slxgreeter-1a32f34505fd7960304e15e5ca539be877d06c24.tar.xz slxgreeter-1a32f34505fd7960304e15e5ca539be877d06c24.zip |
Strip unneeded stuff, simplify pam dialog handling, add safety timeout of 10 seconds
Diffstat (limited to 'src/loginform.cpp')
-rw-r--r-- | src/loginform.cpp | 173 |
1 files changed, 89 insertions, 84 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); } |