summaryrefslogtreecommitdiffstats
path: root/src/loginform.cpp
blob: 07d4094ef98903fc4a70c776dbcecc46018609ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2012-2015 Christian Surlykke
*
* This file is part of qt-lightdm-greeter 
* It is distributed under the LGPL 2.1 or later license.
* Please refer to the LICENSE file for a copy of the license.
*/
#include <QDebug>
#include <QCompleter>
#include <QAbstractListModel>
#include <QModelIndex>
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QPixmap>
#include <QMessageBox>
#include <QMenu>
#include <QProcess>
#include <QLightDM/UsersModel>
#include <QMetaMethod>

#include "loginform.h"
#include "ui_loginform.h"
#include "settings.h"

int rows(QAbstractItemModel& model) {
    return model.rowCount(QModelIndex());
}

LoginForm::LoginForm(QWidget *parent) :
    QWidget(parent), 
    ui(new Ui::LoginForm),
    m_Greeter(),
    power(this),
    sessionsModel(),
    clearMsg(false)
{
    if (!m_Greeter.connectSync()) {
        exit(0);
        return;
    }

    ui->setupUi(this);
    initialize();
}

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()
{
    QPixmap icon(":/resources/rqt-2.png"); // This project came from Razor-qt
    ui->iconLabel->setPixmap(icon.scaled(ui->iconLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    ui->hostnameLabel->setText(m_Greeter.hostname());

    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");
    ui->leaveComboBox->setDisabled(ui->leaveComboBox->count() <= 1);

    cancelLoginTimer.setInterval(10000);
    cancelLoginTimer.setSingleShot(true);
    connect(&cancelLoginTimer, SIGNAL(timeout()), this, SLOT(cancelLogin()));

    //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(showMessage(QString, QLightDM::Greeter::MessageType)), this, SLOT(onMessage(QString, QLightDM::Greeter::MessageType)));
    connect(&m_Greeter, SIGNAL(authenticationComplete()), this, SLOT(onAuthenticationComplete()));

    ui->passwordInput->clear();
}

void LoginForm::startAuthentication()
{
	qDebug() << "Start auth";
    //setCurrentSession(Cache().getLastSession(ui->userInput->text()));

	if (m_Greeter.inAuthentication()) {
		m_Greeter.cancelAuthentication();
	}
    if (ui->userInput->text().isEmpty()) {
    	ui->userInput->setFocus();
    	return;
    }
    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)
{
    QString actionName = ui->leaveComboBox->itemData(index).toString();
    if      (actionName == "shutdown") power.shutdown();
    else if (actionName == "restart") power.restart();
    else if (actionName == "hibernate") power.hibernate();
    else if (actionName == "suspend") power.suspend();
}

void LoginForm::onMessage(QString message, QLightDM::Greeter::MessageType type)
{
	qDebug() << "Message: " << message;
	ui->messageLabel->setText(message);
	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 (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::cancelLogin()
{
	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) {
    	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);
}