fix some refactor commits that weren't tested (#20140)

Co-authored-by: Comma Device <device@comma.ai>
old-commit-hash: 9e29d5621c
commatwo_master
grekiki 4 years ago committed by GitHub
parent 545c046b99
commit 4af3c6f533
  1. 114
      selfdrive/ui/qt/widgets/ssh_keys.cc
  2. 21
      selfdrive/ui/qt/widgets/ssh_keys.hpp

@ -1,12 +1,20 @@
#include <QDebug>
#include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QPushButton> #include <QPushButton>
#include <QState> #include <QState>
#include <QStateMachine> #include <QStateMachine>
#include <QNetworkReply> #include <QNetworkReply>
#include "common/params.h"
#include "widgets/ssh_keys.hpp" #include "widgets/ssh_keys.hpp"
#include "widgets/input_field.hpp" #include "widgets/input_field.hpp"
#include "common/params.h"
QWidget* layout_to_widget(QLayout* l){
QWidget* q = new QWidget;
q->setLayout(l);
return q;
}
SSH::SSH(QWidget* parent) : QWidget(parent){ SSH::SSH(QWidget* parent) : QWidget(parent){
// init variables // init variables
@ -16,24 +24,37 @@ SSH::SSH(QWidget* parent) : QWidget(parent){
networkTimer->setInterval(5000); networkTimer->setInterval(5000);
connect(networkTimer, SIGNAL(timeout()), this, SLOT(timeout())); connect(networkTimer, SIGNAL(timeout()), this, SLOT(timeout()));
// Layout on entering
QVBoxLayout* main_layout = new QVBoxLayout;
main_layout->setMargin(50);
// Construct the layouts to display
slayout = new QStackedLayout(this); // Initial screen, input, waiting for response
//Layout on entering
QVBoxLayout* initialLayout = new QVBoxLayout;
initialLayout->setContentsMargins(80, 80, 80, 80);
QHBoxLayout* header = new QHBoxLayout;
QPushButton* exitButton = new QPushButton("BACK", this); QPushButton* exitButton = new QPushButton("BACK", this);
exitButton->setFixedSize(500, 100); exitButton->setFixedSize(500, 100);
main_layout->addWidget(exitButton, 0, Qt::AlignLeft | Qt::AlignTop); header->addWidget(exitButton, 0, Qt::AlignLeft | Qt::AlignTop);
connect(exitButton, SIGNAL(released()), this, SIGNAL(closeSSHSettings())); initialLayout->addWidget(layout_to_widget(header));
QLabel* wallOfText = new QLabel("Warning: This grants SSH access to all public keys in your GitHub settings. Never enter a GitHub username other than your own."); QLabel* title = new QLabel("Authorize SSH keys");
wallOfText->setAlignment(Qt::AlignHCenter); title->setStyleSheet(R"(font-size: 75px;)");
header->addWidget(title, 0, Qt::AlignRight | Qt::AlignTop);
QLabel* wallOfText = new QLabel("Warning: This grants SSH access to all public keys in your GitHub settings. Never enter a GitHub username other than your own. A Comma employee will NEVER ask you to add their GitHub username.");
wallOfText->setWordWrap(true); wallOfText->setWordWrap(true);
wallOfText->setStyleSheet(R"(font-size: 60px;)"); wallOfText->setStyleSheet(R"(font-size: 60px;)");
main_layout->addWidget(wallOfText, 0); initialLayout->addWidget(wallOfText, 0);
QPushButton* actionButton = new QPushButton; QPushButton* actionButton = new QPushButton;
actionButton->setFixedHeight(100); actionButton->setFixedHeight(100);
main_layout->addWidget(actionButton, 0, Qt::AlignBottom); initialLayout->addWidget(actionButton, 0, Qt::AlignBottom);
slayout->addWidget(layout_to_widget(initialLayout));
QLabel* loading = new QLabel("Loading SSH keys from GitHub.");
slayout->addWidget(loading);
setStyleSheet(R"( setStyleSheet(R"(
QPushButton { QPushButton {
@ -45,61 +66,66 @@ SSH::SSH(QWidget* parent) : QWidget(parent){
background-color: #444444; background-color: #444444;
} }
)"); )");
setLayout(main_layout); setLayout(slayout);
// Initialize the state machine and states
//Initialize the state machine and states
QStateMachine* state = new QStateMachine(this); QStateMachine* state = new QStateMachine(this);
QState* initialState = new QState(); //State when entering the widget QState* initialState = new QState(); //State when entering the widget
QState* initialStateNoGithub = new QState(); //Starting state, key not connected QState* initialStateNoGithub = new QState(); //Starting state, key not connected
QState* initialStateConnected = new QState(); //Starting state, ssh connected QState* initialStateConnected = new QState(); //Starting state, ssh connected
QState* quitState = new QState(); // State when exiting the widget
QState* removeSSH_State = new QState(); // State when user wants to remove the SSH keys QState* removeSSH_State = new QState(); // State when user wants to remove the SSH keys
QState* defaultInputFieldState = new QState(); // State when we want the user to give us the username
QState* loadingState = new QState(); // State while waiting for the network response QState* loadingState = new QState(); // State while waiting for the network response
// Adding states to the state machine and adding the transitions // Adding states to the state machine and adding the transitions
state->addState(initialState); state->addState(initialState);
connect(initialState, &QState::entered, [=](){ connect(initialState, &QState::entered, [=](){checkForSSHKey(); slayout->setCurrentIndex(0);});
checkForSSHKey();
});
initialState->addTransition(this, &SSH::NoSSHAdded, initialStateNoGithub); initialState->addTransition(this, &SSH::NoSSHAdded, initialStateNoGithub);
initialState->addTransition(this, &SSH::SSHAdded, initialStateConnected); initialState->addTransition(this, &SSH::SSHAdded, initialStateConnected);
state->addState(quitState);
connect(quitState, &QState::entered, [=](){emit closeSSHSettings();});
quitState->addTransition(quitState, &QState::entered, initialState);
state->addState(initialStateConnected); state->addState(initialStateConnected);
connect(initialStateConnected, &QState::entered, [=](){ connect(initialStateConnected, &QState::entered, [=](){actionButton->setText("Remove GitHub SSH keys"); actionButton->setStyleSheet(R"(background-color: #750c0c;)");});
actionButton->setText("Clear SSH keys"); initialStateConnected->addTransition(exitButton, &QPushButton::released, quitState);
actionButton->setStyleSheet(R"(background-color: #750c0c;)");
});
initialStateConnected->addTransition(actionButton, &QPushButton::released, removeSSH_State); initialStateConnected->addTransition(actionButton, &QPushButton::released, removeSSH_State);
state->addState(removeSSH_State); state->addState(removeSSH_State);
connect(removeSSH_State, &QState::entered, [=](){ connect(removeSSH_State, &QState::entered, [=](){Params().delete_db_value("GithubSshKeys");});
Params().delete_db_value("GithubSshKeys");
});
removeSSH_State->addTransition(removeSSH_State, &QState::entered, initialState); removeSSH_State->addTransition(removeSSH_State, &QState::entered, initialState);
state->addState(initialStateNoGithub); state->addState(initialStateNoGithub);
connect(initialStateNoGithub, &QState::entered, [=](){ connect(initialStateNoGithub, &QState::entered, [=](){actionButton->setText("Link GitHub SSH keys"); actionButton->setStyleSheet(R"(background-color: #444444;)");});
actionButton->setText("Link GitHub SSH keys"); initialStateNoGithub->addTransition(exitButton, &QPushButton::released, quitState);
actionButton->setStyleSheet(R"(background-color: #444444;)"); initialStateNoGithub->addTransition(actionButton, &QPushButton::released, defaultInputFieldState);
connect(actionButton, &QPushButton::released, [=](){inputFieldMessage = "Enter your GitHub username";});
state->addState(defaultInputFieldState);
connect(defaultInputFieldState, &QState::entered, [=](){
QString user = InputDialog::getText(inputFieldMessage);
if (user.length()) {
emit inputFieldEmitText(user);
} else {
emit inputFieldCancelled();
}
}); });
initialStateNoGithub->addTransition(actionButton, &QPushButton::released, loadingState); connect(this, &SSH::inputFieldEmitText, [=](QString a){usernameGitHub = a;}); // Store the string the user provided
defaultInputFieldState->addTransition(this, &SSH::inputFieldCancelled, initialState);
defaultInputFieldState->addTransition(this, &SSH::inputFieldEmitText, loadingState);
state->addState(loadingState); state->addState(loadingState);
connect(loadingState, &QState::entered, [=](){ connect(loadingState, &QState::entered, [=](){slayout->setCurrentIndex(1); getSSHKeys();});
QString user = InputDialog::getText("Enter your GitHub username"); connect(this, &SSH::failedResponse, [=](QString message){inputFieldMessage = message;});
if (user.size()) { loadingState->addTransition(this, &SSH::failedResponse, defaultInputFieldState);
getSSHKeys(user);
}
});
connect(this, &SSH::failedResponse, [=](QString message){
QString user = InputDialog::getText(message);
if (user.size()) {
getSSHKeys(user);
}
});
loadingState->addTransition(loadingState, &QState::entered, initialState);
loadingState->addTransition(this, &SSH::failedResponse, initialState);
loadingState->addTransition(this, &SSH::gotSSHKeys, initialState); loadingState->addTransition(this, &SSH::gotSSHKeys, initialState);
state->setInitialState(initialState); state->setInitialState(initialState);
state->start(); state->start();
} }
@ -113,8 +139,8 @@ void SSH::checkForSSHKey(){
} }
} }
void SSH::getSSHKeys(QString username){ void SSH::getSSHKeys(){
QString url = "https://github.com/" + username + ".keys"; QString url = "https://github.com/" + usernameGitHub + ".keys";
aborted = false; aborted = false;
reply = manager->get(QNetworkRequest(QUrl(url))); reply = manager->get(QNetworkRequest(QUrl(url)));
connect(reply, SIGNAL(finished()), this, SLOT(parseResponse())); connect(reply, SIGNAL(finished()), this, SLOT(parseResponse()));
@ -134,9 +160,9 @@ void SSH::parseResponse(){
Params().write_db_value("GithubSshKeys", response.toStdString()); Params().write_db_value("GithubSshKeys", response.toStdString());
emit gotSSHKeys(); emit gotSSHKeys();
} else { } else {
emit failedResponse("Username doesn't exist"); emit failedResponse("Username "+usernameGitHub+" doesn't exist");
} }
} else { }else{
emit failedResponse("Request timed out"); emit failedResponse("Request timed out");
} }
reply->deleteLater(); reply->deleteLater();

@ -1,8 +1,14 @@
#pragma once #pragma once
#include <QTimer>
#include <QWidget> #include <QWidget>
#include <QButtonGroup>
#include <QVBoxLayout>
#include <QStackedWidget>
#include <QPushButton>
#include <QTimer>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QStackedLayout>
class SSH : public QWidget { class SSH : public QWidget {
Q_OBJECT Q_OBJECT
@ -11,22 +17,29 @@ public:
explicit SSH(QWidget* parent = 0); explicit SSH(QWidget* parent = 0);
private: private:
QStackedLayout* slayout;
QString usernameGitHub;
QString inputFieldMessage;
QNetworkAccessManager* manager; QNetworkAccessManager* manager;
QNetworkReply* reply; QNetworkReply* reply;
QTimer* networkTimer; QTimer* networkTimer;
bool aborted; bool aborted;
void getSSHKeys(QString user);
signals: signals:
void closeSSHSettings();
void openKeyboard();
void closeKeyboard();
void NoSSHAdded(); void NoSSHAdded();
void SSHAdded(); void SSHAdded();
void inputFieldCancelled();
void inputFieldEmitText(QString GitHubUsername);
void failedResponse(QString errorString); void failedResponse(QString errorString);
void gotSSHKeys(); void gotSSHKeys();
void closeSSHSettings();
private slots: private slots:
void checkForSSHKey(); void checkForSSHKey();
void getSSHKeys();
void timeout(); void timeout();
void parseResponse(); void parseResponse();
}; };

Loading…
Cancel
Save