Qt-UI Cleanup SSH and Tethering password (#20169)

Co-authored-by: Comma Device <device@comma.ai>
albatross
grekiki 2021-02-26 14:08:26 +01:00 committed by GitHub
parent 2b9a8e18fd
commit c60cc0310c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 53 deletions

View File

@ -5,7 +5,6 @@
#include <QPushButton>
#include <QLineEdit>
#include <QRandomGenerator>
#include <QtConcurrent>
#include <algorithm>
#include "common/params.h"
@ -30,20 +29,6 @@ QWidget* layoutToWidget(QLayout* l, QWidget* parent){
return q;
}
// https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
// Networking functions
Networking::Networking(QWidget* parent, bool show_advanced) : QWidget(parent), show_advanced(show_advanced){
@ -131,23 +116,19 @@ void Networking::connectToNetwork(Network n) {
if (n.security_type == SecurityType::OPEN) {
wifi->connect(n);
} else if (n.security_type == SecurityType::WPA) {
QString pass = InputDialog::getText("Enter password for \"" + n.ssid + "\"");
QString pass = InputDialog::getText("Enter password for \"" + n.ssid + "\"", 8);
wifi->connect(n, pass);
}
}
void Networking::wrongPassword(QString ssid) {
return; // TODO: add this back
/*
for (Network n : wifi->seen_networks) {
if (n.ssid == ssid) {
inputField->setPromptText("Wrong password for \"" + n.ssid +"\"");
s->setCurrentIndex(0);
emit openKeyboard();
QString pass = InputDialog::getText("Wrong password for \"" + n.ssid +"\"", 8);
wifi->connect(n, pass);
return;
}
}
*/
}
QFrame* hline(QWidget* parent = 0){
@ -193,7 +174,7 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid
editPasswordButton = new QPushButton("EDIT");
editPasswordButton->setFixedWidth(500);
connect(editPasswordButton, &QPushButton::released, [=](){
QString pass = InputDialog::getText("Enter new tethering password");
QString pass = InputDialog::getText("Enter new tethering password", 8);
if (pass.size()) {
wifi->changeTetheringPassword(pass);
}
@ -255,16 +236,11 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid
}
bool AdvancedNetworking::isSSHEnabled(){
QString response = QString::fromStdString(exec("systemctl is-active ssh"));
return response.startsWith("active");
return Params().get("SshEnabled") == "1";
}
void AdvancedNetworking::refresh(){
ipLabel->setText(wifi->ipv4_address);
// Don't refresh while changing SSH state
if(!toggle_switch_SSH->getEnabled()){
return;
}
if (toggle_switch_SSH->on != isSSHEnabled()) {
toggle_switch_SSH->togglePosition();
}
@ -281,23 +257,8 @@ void AdvancedNetworking::toggleTethering(int enable) {
editPasswordButton->setEnabled(!enable);
}
void enableSSH(Toggle* toggle_switch_SSH){
Params().write_db_value("SshEnabled", "1");
toggle_switch_SSH->setEnabled(true);
}
void disableSSH(Toggle* toggle_switch_SSH){
Params().write_db_value("SshEnabled", "0");
toggle_switch_SSH->setEnabled(true);
}
void AdvancedNetworking::toggleSSH(int enable) {
toggle_switch_SSH->setEnabled(false);
if (enable) {
QtConcurrent::run(enableSSH, toggle_switch_SSH);
} else {
QtConcurrent::run(disableSSH, toggle_switch_SSH);
}
Params().write_db_value("SshEnabled", QString::number(enable).toStdString());
}

View File

@ -3,7 +3,7 @@
#include "input_field.hpp"
#include "qt_window.hpp"
InputDialog::InputDialog(QString prompt_text, QWidget *parent): QDialog(parent) {
InputDialog::InputDialog(QString prompt_text, QWidget *parent):QDialog(parent) {
layout = new QVBoxLayout();
layout->setContentsMargins(50, 50, 50, 50);
layout->setSpacing(20);
@ -56,8 +56,9 @@ InputDialog::InputDialog(QString prompt_text, QWidget *parent): QDialog(parent)
setLayout(layout);
}
QString InputDialog::getText(const QString prompt) {
QString InputDialog::getText(const QString prompt, int minLength) {
InputDialog d = InputDialog(prompt);
d.setMinLength(minLength);
const int ret = d.exec();
if (ret) {
return d.text();
@ -81,8 +82,12 @@ void InputDialog::handleInput(QString s) {
}
if (!QString::compare(s,"")) {
done(QDialog::Accepted);
emitText(line->text());
if (line->text().length() > minLength){
done(QDialog::Accepted);
emitText(line->text());
} else {
setMessage("Need at least "+QString::number(minLength)+" characters!", false);
}
}
QVector<QString> control_buttons {"", "", "ABC", "", "#+=", "", "123"};
@ -105,3 +110,7 @@ void InputDialog::setMessage(QString message, bool clearInputField){
line->setText("");
}
}
void InputDialog::setMinLength(int length){
minLength = length;
}

View File

@ -14,12 +14,14 @@ class InputDialog : public QDialog {
public:
explicit InputDialog(QString prompt_text, QWidget* parent = 0);
static QString getText(QString prompt);
static QString getText(QString prompt, int minLength = -1);
QString text();
void show();
void setMessage(QString message, bool clearInputField=true);
void setMinLength(int length);
private:
int minLength;
QLineEdit *line;
Keyboard *k;
QLabel *label;

View File

@ -13,14 +13,14 @@ const int SPACEBAR_STRETCH = 3;
KeyboardLayout::KeyboardLayout(QWidget *parent, std::vector<QVector<QString>> layout) : QWidget(parent) {
QVBoxLayout* vlayout = new QVBoxLayout;
vlayout->setMargin(0);
vlayout->setSpacing(15);
vlayout->setSpacing(35);
QButtonGroup* btn_group = new QButtonGroup(this);
QObject::connect(btn_group, SIGNAL(buttonClicked(QAbstractButton*)), parent, SLOT(handleButton(QAbstractButton*)));
for (const auto &s : layout) {
QHBoxLayout *hlayout = new QHBoxLayout;
hlayout->setSpacing(30);
hlayout->setSpacing(25);
if (vlayout->count() == 1) {
hlayout->addSpacing(90);
@ -28,7 +28,7 @@ KeyboardLayout::KeyboardLayout(QWidget *parent, std::vector<QVector<QString>> la
for (const QString &p : s) {
QPushButton* btn = new QPushButton(p);
btn->setFixedHeight(120);
btn->setFixedHeight(135);
btn_group->addButton(btn);
hlayout->addWidget(btn, p == QString(" ") ? SPACEBAR_STRETCH : DEFAULT_STRETCH);
}