summaryrefslogblamecommitdiffstats
path: root/src/loginform.cpp
blob: 885a56747ad0f8860c37d3d6f69bc02e4d5778c1 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                            
                         
 

                                                   


                                                             





                         
                     





                    

                             
                  
                

                      
                    
                       
                   

                   

                              
                                       

                          

                    
 

                      



                                                                                                                                   

                                                        




                                                



                       
 














                                                






                                                      
                                                                                                         

                                          
     






                                                                                            
 
                                                                                                   
 

                                         
                                                                                
 

                                         




                                                                 
 
                                                    

                                                             






                                                                              
           



































                                                                                   
     
 

                                                                  


                                                                               


                                                                                   
 

                                                                                                     
 



                                                                                                                           

                                              





                                                                                  
         

     


                                            
                           
                                                                    
                               
                                                             

















                                                                              

 
                                     
 
                                                          


                                                                             
                             
                                                         

               

                                                  



                                          
     


                                              
     
 
                                            



                                         
                                                  

 
                                                                                        
 
                                                                     
                                                              
                                   




                                                                       



                                                                     

 
                                                                               
 


                                                                   
                                                                       
                                    
                        

 






                                                                                             
                                          
 
                                               
                                                                 
                                                      
                                 
                                     

                                        
                                                             
                                
         
            
                                                               
                      


     
                             
 
                                                 
                                                
                                                                  
                                                          





                                            


                                                             
         
                                      
                        

 

















                                                             

                                               

                                 
                              
         
                                                                          











                                              
     
                                                                            
                    
     

                                  

 
                           
 

                                                            
                               

                              

 






                                                           
/*
* Copyright (c) 2012-2015 Christian Surlykke
* 2017 bwLehrpool project
*
* Based on qt-lightdm-greeter, stripped down to fit
* our specific needs.
* It is distributed under the LGPL 2.1 or later license.
* Please refer to the LICENSE file for a copy of the license.
*/

#include "loginform.h"
#include "ui_loginform.h"
#include "settings.h"
#include "global.h"
#include "namereplace.h"
#include "loginrpc.h"
#include "x11util.h"
#undef KeyPress
#undef KeyRelease
#undef FocusIn
#undef FocusOut

#include <QAbstractListModel>
#include <QModelIndex>
#include <QPixmap>
#include <QIcon>
#include <QMessageBox>
#include <QMenu>
#include <QListView>
#include <QSvgRenderer>
#include <QX11Info>
#include <iostream>

void createSimpleBackground();

LoginForm::LoginForm(QWidget *parent) :
    QWidget(parent), 
    ui(new Ui::LoginForm),
    clearMsg(false),
    capsOn(-1)
{
    ui->setupUi(this);
    initialize();
    int port = Settings::rpcPort();
    if (port != 0) {
        auto *car = new LoginRpc(port, this);
        connect(car, &LoginRpc::loginRequest, [this](const QString &username, const QString &password, const QString &resolution) {
           if (username.isEmpty() || password.isEmpty())
               return;
           ui->userInput->setText(username);
           ui->passwordInput->setText(password);
           this->startAuthentication();
        });
    }
}

LoginForm::~LoginForm()
{

    delete ui;
}

void LoginForm::setFocus(Qt::FocusReason reason)
{
    if (ui->userInput->text().isEmpty()) {
        ui->userInput->setFocus(reason);
    } else {
        ui->passwordInput->setFocus(reason);
    }
}


void LoginForm::initialize()
{
    QString path = Settings::miniIconFile();
    QPixmap pixmap;
    if (!path.isEmpty()) {
        QSize size = QSvgRenderer(path).defaultSize();
        if (!size.isValid()) {
            size = ui->iconLabel->maximumSize();
        } else {
            size = size.boundedTo(ui->iconLabel->maximumSize()).expandedTo(ui->iconLabel->minimumSize());
        }
        pixmap = QIcon(path).pixmap(size);
    }
    if (pixmap.isNull() || pixmap.width() < 10) {
        // fallback to built-in bwlp logo
        pixmap = QIcon(QLatin1String(":/resources/bwlp.svg")).pixmap(ui->iconLabel->size());
    }

    ui->iconLabel->setPixmap(pixmap);
    ui->iconLabel->setFixedSize(pixmap.size());

    ui->leaveComboBox->setView(new QListView()); // This is required to get the stylesheet to apply

    cancelLoginTimer.setInterval(10000);
    cancelLoginTimer.setSingleShot(true);
    connect(&cancelLoginTimer, &QTimer::timeout, this, &LoginForm::cancelLogin);

    hideMessageTimer.setInterval(10000);
    hideMessageTimer.setSingleShot(true);
    connect(&hideMessageTimer, &QTimer::timeout, this, [this]() {
        this->hideMessage();
        this->capsOn = -1;
        this->checkCaps();
    });

    // timer to reset the form to its original state
    if (Settings::resetForm() > 0) {
        connect(&resetFormTimer, &QTimer::timeout, [this]() {
            int idleTime = static_cast<int>(getIdleTime(QX11Info::display()));
            int remaining = Settings::resetForm() * 1000 - idleTime;
            if (remaining <= 0) {
                resetForm();
                remaining = Settings::resetForm() * 1000;
            }
            resetFormTimer.start(remaining + 100);
        });
        resetFormTimer.start(Settings::resetForm() * 1000);
    }

    if (Settings::guestSessionEnabled()) {
        if (!Settings::guestSessionButtonText().isEmpty()) {
            ui->guestButton->setText(Settings::guestSessionButtonText());
        }
        if (!Settings::userSessionButtonText().isEmpty()) {
            ui->loginButton->setText(Settings::userSessionButtonText());
        }
        if (!Settings::guestSessionStartText().isEmpty()) {
            ui->guestStartText->setText(Settings::guestSessionStartText());
        }
        if (!Settings::guestSessionStartButtonText().isEmpty()) {
            ui->guestStartButton->setText(Settings::guestSessionStartButtonText());
        }
        connect(ui->loginButton, &QAbstractButton::released, this, [this]() {
            ui->loginChooser->setCurrentWidget(ui->loginPage);
            ui->userInput->setFocus();
        });
        connect(ui->guestButton, &QAbstractButton::released, this, [this]() {
            ui->loginChooser->setCurrentWidget(ui->guestPage);
        });
        connect(ui->backButton, &QAbstractButton::released, this, [this]() {
            resetForm();
        });
        connect(ui->loginChooser, &QStackedWidget::currentChanged, this, [this]() {
            if (ui->loginChooser->currentWidget() == ui->welcomePage) {
                ui->backButton->hide();
            } else {
                ui->backButton->show();
            }
        });
        ui->loginChooser->setCurrentWidget(ui->welcomePage);
    } else {
        ui->loginChooser->setCurrentWidget(ui->loginPage);
    }

    if (!Global::testMode()) {
        ui->hostnameLabel->setText(Global::greeter()->hostname());
        if(!Settings::usernamePlaceholder().isEmpty()) {
            ui->userInput->setPlaceholderText(Settings::usernamePlaceholder());
        }
        if(!Settings::passwordPlaceholder().isEmpty()) {
            ui->passwordInput->setPlaceholderText(Settings::passwordPlaceholder());
        }

        addLeaveEntry(Global::power()->canShutdown(), "system-shutdown", tr("Shutdown"), "shutdown");
        addLeaveEntry(Global::power()->canRestart(), "system-reboot", tr("Restart"), "restart");

        connect(ui->leaveComboBox, QOverload<int>::of(&QComboBox::activated), this, &LoginForm::leaveDropDownActivated);
        connect(Global::greeter(), &QLightDM::Greeter::showPrompt, this, &LoginForm::onPrompt);
        connect(Global::greeter(), &QLightDM::Greeter::showMessage, this, &LoginForm::onMessage);
        connect(Global::greeter(), &QLightDM::Greeter::authenticationComplete, this, &LoginForm::onAuthenticationComplete);

        if (Settings::guestSessionEnabled()) {
            connect(ui->guestStartButton, &QAbstractButton::released, this, []() {
                if (!Global::autoLoginGuest()) {
                    std::cerr << "Guest login failed..." << std::endl;
                    // TODO warn user about it
                }
            });
        }
    }

	// Load regexp for name substitution
	NameReplace::loadSubs();

    ui->backButton->hide();
    ui->leaveComboBox->setDisabled(ui->leaveComboBox->count() <= 1);
    ui->passwordInput->clear();
    this->layout()->setSizeConstraint(QLayout::SetFixedSize);
    this->installEventFilter(this);
    checkCaps();
}

void LoginForm::checkCaps()
{
    unsigned int mask = getKeyMask(QX11Info::display());
    int caps = (mask & 1) == 1;
    if (caps != capsOn) {
        capsOn = caps;
        QString message(tr("!! CAPS LOCK ACTIVE !!"));
        if (caps) {
            ui->messageLabel->setProperty("caps", message);
            showMessage(message, false);
        } else if (ui->messageLabel->property("caps").toString() == message) {
            hideMessage();
        }
    }
}

void LoginForm::startAuthentication()
{
	QString username(ui->userInput->text().trimmed());
	NameReplace::replace(username);
	std::cerr << "Logging in as " << username.toStdString() << std::endl;

    if (Global::testMode()) {
        showMessage(QLatin1String("Test mode..."), true);
        return;
    }
    if (Global::greeter()->inAuthentication()) {
    	Global::greeter()->cancelAuthentication();
	}
    if (ui->userInput->text().isEmpty()) {
    	ui->userInput->setFocus();
    	return;
    }
    if (ui->passwordInput->text().isEmpty()) {
    	ui->passwordInput->setFocus();
    	return;
    }

    showMessage(tr("Logging in..."), false);
    clearMsg = false;
    ui->userInput->setEnabled(false);
    ui->passwordInput->setEnabled(false);
    cancelLoginTimer.start();
	Global::greeter()->authenticate(username);
}

void LoginForm::onPrompt(QString prompt, QLightDM::Greeter::PromptType /* promptType */)
{
	std::cerr << "Prompt: " << prompt.toStdString() << std::endl;
	Global::greeter()->respond(ui->passwordInput->text());
	ui->passwordInput->clear();
}

void LoginForm::leaveDropDownActivated(int index)
{
    QString actionName = ui->leaveComboBox->itemData(index).toString();
    if      (actionName == "shutdown") Global::power()->shutdown();
    else if (actionName == "restart") Global::power()->restart();
    else if (actionName == "hibernate") Global::power()->hibernate();
    else if (actionName == "suspend") Global::power()->suspend();
}

void LoginForm::onMessage(QString message, QLightDM::Greeter::MessageType type)
{
    if (type == QLightDM::Greeter::MessageType::MessageTypeError) {
        ui->passwordInput->clear();
    }
	std::cerr << "Message: " << message.toStdString() << std::endl;
	showMessage(message, false);
	clearMsg = true;
}

void LoginForm::addLeaveEntry(bool canDo, QString iconName, QString text, QString actionName)
{
    if (canDo) {
        ui->leaveComboBox->addItem(QIcon::fromTheme(iconName), text, actionName);
    }
}

void LoginForm::onAuthenticationComplete()
{
    if (Global::greeter()->isAuthenticated()) {
    	std::cerr << "Auth complete, start session" << std::endl;
    	showMessage(tr("Starting session..."), false);
        createSimpleBackground();
        if (Global::startSession()) {
        	cancelLoginTimer.stop();
        } else {
        	showMessage(tr("Cannot open session"), true);
        	clearMsg = true;
        }
    } else {
    	std::cerr << "Auth failed, cancelling..." << std::endl;
    	cancelLogin();
    }
}

void LoginForm::cancelLogin()
{
	std::cerr << "Cancel login" << std::endl;
    if (Global::greeter()->inAuthentication()) {
		std::cerr << "Was in authentication" << std::endl;
		Global::greeter()->cancelAuthentication();
	}
	cancelLoginTimer.stop();
	ui->passwordInput->clear();
	ui->userInput->setEnabled(true);
	ui->passwordInput->setEnabled(true);
	if (!clearMsg) {
		showMessage(tr("Login failed"), true);
	} else {
		ui->messageLabel->setStyleSheet("color:red");
	}
	ui->passwordInput->setFocus();
	clearMsg = true;
}

void LoginForm::showMessage(QString message, bool error)
{
	hideMessageTimer.stop();
	ui->messageLabel->setText(message);
	if (error) {
		ui->messageLabel->setStyleSheet("color:red");
		hideMessageTimer.start();
	} else {
		ui->messageLabel->setStyleSheet("");
	}
}

void LoginForm::hideMessage()
{
	hideMessageTimer.stop();
	ui->messageLabel->clear();
}

void LoginForm::keyPressEvent(QKeyEvent *event)
{
	if (clearMsg) {
		clearMsg = false;
		hideMessage();
	}
    if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
    	if (!ui->userInput->isEnabled()) {
    		// Ignore if auth in progress
    		return;
    	}
        if (ui->userInput->hasFocus()) {
        	ui->passwordInput->setFocus();
        	return;
        }
        if (ui->passwordInput->hasFocus()) {
        	startAuthentication();
        	return;
        }
    }
    if (Settings::guestSessionEnabled() && event->key() == Qt::Key_Escape) {
        resetForm();
    }
    // Fallback: Passthrough
    QWidget::keyPressEvent(event);
}

void LoginForm::resetForm()
{
    if (Settings::guestSessionEnabled())
        ui->loginChooser->setCurrentWidget(ui->welcomePage);
    ui->passwordInput->clear();
    ui->userInput->clear();
    ui->userInput->setFocus();
}

bool LoginForm::eventFilter(QObject *object, QEvent *event)
{
    if (event->type() == QEvent::KeyRelease) {
        checkCaps();
    }
    return false;
}