thedesk/locker/locker-ui/mainwindow.cpp

103 lines
2.6 KiB
C++
Raw Normal View History

2023-06-01 22:16:26 +10:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCoroProcess>
#include <QProcess>
#include <SystemSlide/systemslide.h>
2023-06-04 18:20:47 +10:00
#include <Wm/desktopwm.h>
#include <lockmanager.h>
#include <terrorflash.h>
2023-06-01 22:16:26 +10:00
struct MainWindowPrivate {
QString lockCheckProcess;
SystemSlide* slide;
};
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);
d = new MainWindowPrivate();
d->slide = new SystemSlide(this);
2023-06-04 19:20:02 +10:00
d->slide->setBackgroundMode(SystemSlide::LockScreenBackground);
2023-06-01 22:16:26 +10:00
d->slide->setAction(tr("Unlock"), tr("Resume your session, continuing where you left off"));
d->slide->setActionIcon(QIcon::fromTheme("go-up"));
d->slide->setDragResultWidget(this->centralWidget());
if (qEnvironmentVariableIsSet("TD_LOCK_CHECKER")) {
d->lockCheckProcess = qEnvironmentVariable("TD_LOCK_CHECKER");
} else {
d->lockCheckProcess = "/usr/lib/td-locker-checker"; // TODO: Don't hardcode
}
2023-06-04 18:20:47 +10:00
ui->usernameLabel->setText(DesktopWm::userDisplayName());
ui->capsLockOn->setVisible(false);
connect(d->slide, &SystemSlide::deactivated, this, [this] {
2023-06-01 22:16:26 +10:00
if (this->checkPassword("")) {
QApplication::exit(0);
2023-06-04 18:20:47 +10:00
return;
2023-06-01 22:16:26 +10:00
}
2023-06-04 18:20:47 +10:00
ui->password->setFocus();
2023-06-01 22:16:26 +10:00
2023-06-04 18:20:47 +10:00
QTimer::singleShot(100, this, [this] {
ui->PasswordUnderline->startAnimation();
});
2023-06-01 22:16:26 +10:00
});
}
MainWindow::~MainWindow() {
delete d;
delete ui;
}
void MainWindow::on_titleLabel_backButtonClicked() {
d->slide->activate();
}
bool MainWindow::checkPassword(QString password) {
QString name = qgetenv("USER");
if (name.isEmpty()) {
name = qgetenv("USERNAME");
}
QProcess checker;
checker.start(d->lockCheckProcess, {name});
2023-06-04 18:20:47 +10:00
checker.write(password.toUtf8());
checker.write("\n");
checker.closeWriteChannel();
2023-06-01 22:16:26 +10:00
checker.waitForFinished();
return checker.exitCode() == 0;
}
2023-06-04 18:20:47 +10:00
void MainWindow::on_unlockButton_clicked() {
this->setEnabled(false);
if (checkPassword(ui->password->text())) {
QApplication::exit(0);
return;
}
this->setEnabled(true);
ui->password->setText("");
ui->password->setFocus();
tErrorFlash::flashError(ui->password);
}
void MainWindow::on_password_returnPressed() {
ui->unlockButton->click();
}
void MainWindow::changeEvent(QEvent* event) {
if (event->type() == QEvent::ActivationChange) {
LockManager::instance()->raiseAll();
}
}
void MainWindow::showEvent(QShowEvent* event) {
DesktopWm::instance()->setSystemWindow(this, DesktopWm::SystemWindowTypeLockScreen);
}