Add LoginPINDialog

This commit is contained in:
Florian Märkl 2019-08-25 16:38:13 +02:00
commit 31fb11fd43
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
13 changed files with 208 additions and 26 deletions

View file

@ -63,7 +63,9 @@ add_executable(chiaki WIN32
src/manualhostdialog.cpp
res/resources.qrc
include/controllermanager.h
src/controllermanager.cpp)
src/controllermanager.cpp
include/loginpindialog.h
src/loginpindialog.cpp)
target_include_directories(chiaki PRIVATE include)
target_link_libraries(chiaki chiaki-lib)

View file

@ -0,0 +1,44 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_LOGINPINDIALOG_H
#define CHIAKI_LOGINPINDIALOG_H
#include <QDialog>
class QLineEdit;
class QDialogButtonBox;
class LoginPINDialog : public QDialog
{
Q_OBJECT
private:
QString pin;
QLineEdit *pin_edit;
QDialogButtonBox *button_box;
void UpdateButtons();
public:
explicit LoginPINDialog(QWidget *parent = nullptr);
QString GetPIN() { return pin; }
};
#endif // CHIAKI_LOGINPINDIALOG_H

View file

@ -92,6 +92,8 @@ class StreamSession : public QObject
void Start();
void Stop();
void SetLoginPIN(const QString &pin);
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
QGamepad *GetGamepad() { return gamepad; }
#endif
@ -103,6 +105,7 @@ class StreamSession : public QObject
signals:
void CurrentImageUpdated();
void SessionQuit(ChiakiQuitReason reason, const QString &reason_str);
void LoginPINRequested();
private slots:
void UpdateGamepads();

View file

@ -47,6 +47,7 @@ class StreamWindow: public QMainWindow
private slots:
void SessionQuit(ChiakiQuitReason reason, const QString &reason_str);
void LoginPINRequested();
void ToggleFullscreen();
};

View file

@ -0,0 +1,57 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#include <loginpindialog.h>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#define PIN_LENGTH 4
static const QRegularExpression pin_re(QString("[0-9]").repeated(PIN_LENGTH));
LoginPINDialog::LoginPINDialog(QWidget *parent) : QDialog(parent)
{
auto layout = new QVBoxLayout(this);
setLayout(layout);
pin_edit = new QLineEdit(this);
pin_edit->setPlaceholderText(tr("Login PIN"));
pin_edit->setValidator(new QRegularExpressionValidator(pin_re, pin_edit));
layout->addWidget(pin_edit);
connect(pin_edit, &QLineEdit::textChanged, this, [this](const QString &text) {
this->pin = text;
UpdateButtons();
});
button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
layout->addWidget(button_box);
connect(button_box, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
UpdateButtons();
}
void LoginPINDialog::UpdateButtons()
{
button_box->button(QDialogButtonBox::Ok)->setEnabled(pin_edit->text().length() == PIN_LENGTH);
}

View file

@ -124,6 +124,12 @@ void StreamSession::Stop()
chiaki_session_stop(&session);
}
void StreamSession::SetLoginPIN(const QString &pin)
{
QByteArray data = pin.toUtf8();
chiaki_session_set_login_pin(&session, (const uint8_t *)data.constData(), data.size());
}
void StreamSession::HandleKeyboardEvent(QKeyEvent *event)
{
uint64_t button_mask;
@ -325,6 +331,9 @@ void StreamSession::Event(ChiakiEvent *event)
case CHIAKI_EVENT_QUIT:
emit SessionQuit(event->quit.reason, event->quit.reason_str ? QString::fromUtf8(event->quit.reason_str) : QString());
break;
case CHIAKI_EVENT_LOGIN_PIN_REQUEST:
emit LoginPINRequested();
break;
}
}

View file

@ -18,6 +18,7 @@
#include <streamwindow.h>
#include <streamsession.h>
#include <avopenglwidget.h>
#include <loginpindialog.h>
#include <QLabel>
#include <QMessageBox>
@ -29,14 +30,16 @@ StreamWindow::StreamWindow(const StreamSessionConnectInfo &connect_info, QWidget
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(qApp->applicationName());
session = nullptr;
av_widget = nullptr;
try
{
Init(connect_info);
}
catch(const Exception &e)
{
session = nullptr;
av_widget = nullptr;
QMessageBox::critical(this, tr("Stream failed"), tr("Failed to initialize Stream Session: %1").arg(e.what()));
close();
}
@ -53,6 +56,7 @@ void StreamWindow::Init(const StreamSessionConnectInfo &connect_info)
session = new StreamSession(connect_info, this);
connect(session, &StreamSession::SessionQuit, this, &StreamWindow::SessionQuit);
connect(session, &StreamSession::LoginPINRequested, this, &StreamWindow::LoginPINRequested);
av_widget = new AVOpenGLWidget(session->GetVideoDecoder(), this);
setCentralWidget(av_widget);
@ -90,15 +94,35 @@ void StreamWindow::closeEvent(QCloseEvent *)
void StreamWindow::SessionQuit(ChiakiQuitReason reason, const QString &reason_str)
{
if(reason == CHIAKI_QUIT_REASON_STOPPED)
return;
QString m = tr("Chiaki Session has quit") + ":\n" + chiaki_quit_reason_string(reason);
if(!reason_str.isEmpty())
m += "\n" + tr("Reason") + ": \"" + reason_str + "\"";
QMessageBox::critical(this, tr("Session has quit"), m);
if(reason != CHIAKI_QUIT_REASON_STOPPED)
{
QString m = tr("Chiaki Session has quit") + ":\n" + chiaki_quit_reason_string(reason);
if(!reason_str.isEmpty())
m += "\n" + tr("Reason") + ": \"" + reason_str + "\"";
QMessageBox::critical(this, tr("Session has quit"), m);
}
close();
}
void StreamWindow::LoginPINRequested()
{
auto dialog = new LoginPINDialog(this);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &QDialog::finished, this, [this, dialog](int result) {
grabKeyboard();
if(!session)
return;
if(result == QDialog::Accepted)
session->SetLoginPIN(dialog->GetPIN());
else
session->Stop();
});
releaseKeyboard();
dialog->show();
}
void StreamWindow::ToggleFullscreen()
{
if(isFullScreen())