Make the WiFi password prompt pop up again if a wrong password is input

pull/2/head
Aaron Rainbolt 1 year ago
parent 22819a4443
commit f13034ecc2

@ -55,7 +55,7 @@ InstallerPrompt::InstallerPrompt(QWidget *parent)
foreach (const NetworkManager::Device::Ptr &device, NetworkManager::networkInterfaces()) { foreach (const NetworkManager::Device::Ptr &device, NetworkManager::networkInterfaces()) {
if (device->type() == NetworkManager::Device::Wifi) { if (device->type() == NetworkManager::Device::Wifi) {
wifiDevice = device.objectCast<NetworkManager::WirelessDevice>(); wifiDevice = device.objectCast<NetworkManager::WirelessDevice>();
connect(wifiDevice.data(), &NetworkManager::Device::stateChanged, this, [this]{updateConnectionStatus();}); connect(wifiDevice.data(), &NetworkManager::Device::stateChanged, this, &InstallerPrompt::handleWiFiConnectionChange);
break; break;
} }
} }
@ -118,16 +118,37 @@ void InstallerPrompt::updateConnectionStatus() {
ui->WiFiSpacer->changeSize(connectable ? 40 : 0, connectable ? 20 : 0, QSizePolicy::Fixed, QSizePolicy::Fixed); ui->WiFiSpacer->changeSize(connectable ? 40 : 0, connectable ? 20 : 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
} }
void InstallerPrompt::handleWifiConnection(const QString &ssid) { void InstallerPrompt::handleWiFiConnectionChange(NetworkManager::Device::State newstate, NetworkManager::Device::State oldstate, NetworkManager::Device::StateChangeReason reason)
{
if (reason == NetworkManager::Device::NoSecretsReason) {
foreach (const NetworkManager::Connection::Ptr &connection, NetworkManager::listConnections()) {
if (connection->settings()->connectionType() == NetworkManager::ConnectionSettings::Wireless) {
auto wirelessSetting = connection->settings()->setting(NetworkManager::Setting::Wireless).dynamicCast<NetworkManager::WirelessSetting>();
if (wirelessSetting && wirelessSetting->ssid() == ui->networkComboBox->currentText()) {
qDebug() << "Wiping connection with wrong password: " << ui->networkComboBox->currentText();
QDBusPendingReply removeReply = connection->remove();
removeReply.waitForFinished();
handleWifiConnection(ui->networkComboBox->currentText(), true);
}
}
}
}
}
void InstallerPrompt::handleWifiConnection(const QString &ssid, bool recoverFromWrongPassword) {
ui->networkComboBox->setEnabled(false);
qDebug() << "Attempting to find connection for SSID:" << ssid; qDebug() << "Attempting to find connection for SSID:" << ssid;
foreach (const NetworkManager::Connection::Ptr &connection, NetworkManager::listConnections()) { if (!recoverFromWrongPassword) {
if (connection->settings()->connectionType() == NetworkManager::ConnectionSettings::Wireless) { foreach (const NetworkManager::Connection::Ptr &connection, NetworkManager::listConnections()) {
auto wirelessSetting = connection->settings()->setting(NetworkManager::Setting::Wireless).dynamicCast<NetworkManager::WirelessSetting>(); if (connection->settings()->connectionType() == NetworkManager::ConnectionSettings::Wireless) {
if (wirelessSetting && wirelessSetting->ssid() == ssid) { auto wirelessSetting = connection->settings()->setting(NetworkManager::Setting::Wireless).dynamicCast<NetworkManager::WirelessSetting>();
qDebug() << "Attempting to use existing connection:" << ssid; if (wirelessSetting && wirelessSetting->ssid() == ssid) {
NetworkManager::activateConnection(connection->path(), wifiDevice->uni(), QString()); qDebug() << "Attempting to use existing connection:" << ssid;
qDebug() << "Successfully connected:" << ssid; NetworkManager::activateConnection(connection->path(), wifiDevice->uni(), QString());
return; qDebug() << "Successfully connected:" << ssid;
ui->networkComboBox->setEnabled(true);
return;
}
} }
} }
} }
@ -153,7 +174,14 @@ void InstallerPrompt::handleWifiConnection(const QString &ssid) {
// If the network is secured, display the password dialog // If the network is secured, display the password dialog
QDialog passwordDialog(this); QDialog passwordDialog(this);
QVBoxLayout layout(&passwordDialog); QVBoxLayout layout(&passwordDialog);
QLabel label(tr("Enter Wi-Fi Password for %1:").arg(ssid), &passwordDialog); QLabel label(&passwordDialog);
if (!recoverFromWrongPassword) {
label.setText(tr("Enter Wi-Fi Password for %1:").arg(ssid));
label.setStyleSheet("color: black");
} else {
label.setText(tr("Wrong Wi-Fi password for %1. Try again:").arg(ssid));
label.setStyleSheet("color: red");
}
QLineEdit lineEdit(&passwordDialog); QLineEdit lineEdit(&passwordDialog);
QPushButton button(tr("Connect"), &passwordDialog); QPushButton button(tr("Connect"), &passwordDialog);
@ -165,7 +193,10 @@ void InstallerPrompt::handleWifiConnection(const QString &ssid) {
connect(&button, &QPushButton::clicked, &passwordDialog, &QDialog::accept); connect(&button, &QPushButton::clicked, &passwordDialog, &QDialog::accept);
for (int attempts = 0; attempts < 3; ++attempts) { for (int attempts = 0; attempts < 3; ++attempts) {
if (passwordDialog.exec() == QDialog::Rejected) return; if (passwordDialog.exec() == QDialog::Rejected) {
ui->networkComboBox->setEnabled(true);
return;
}
QString password = lineEdit.text(); QString password = lineEdit.text();
if (wifiDevice && wifiDevice->isValid()) { if (wifiDevice && wifiDevice->isValid()) {
// Update the wireless security settings in the map // Update the wireless security settings in the map
@ -203,18 +234,17 @@ void InstallerPrompt::handleWifiConnection(const QString &ssid) {
reply.waitForFinished(); reply.waitForFinished();
if (reply.isError()) { if (reply.isError()) {
qDebug() << "Unable to activate the connection:" << addreply.error().message(); qDebug() << "Unable to activate the connection:" << addreply.error().message();
QMessageBox::warning(this, tr("Connection Failed"), tr("Unable to connect to the network."));
ui->networkComboBox->setEnabled(true);
return;
} else { } else {
NetworkManager::reloadConnections(); NetworkManager::reloadConnections();
qDebug() << "Successfully connected:" << ssid; qDebug() << "Successfully connected:" << ssid;
// ui->networkComboBox->setEnabled(true); !!! We don't run this here since we actually *want* the box to remain locked right now
return; return;
} }
} }
label.setStyleSheet("color: red;");
label.setText(tr("Incorrect password. Please try again:"));
} }
QMessageBox::warning(this, tr("Connection Failed"), tr("Unable to connect to the network."));
} }
QMap<QString, QVariant> InstallerPrompt::createSettingsBySSID(const QString &ssid) { QMap<QString, QVariant> InstallerPrompt::createSettingsBySSID(const QString &ssid) {

@ -31,6 +31,7 @@ private slots:
void onLanguageChanged(int index); void onLanguageChanged(int index);
void onConnectWifiClicked(); void onConnectWifiClicked();
void updateConnectionStatus(); void updateConnectionStatus();
void handleWiFiConnectionChange(NetworkManager::Device::State newstate, NetworkManager::Device::State oldstate, NetworkManager::Device::StateChangeReason reason);
void tryLubuntu(); void tryLubuntu();
void installLubuntu(); void installLubuntu();
@ -40,7 +41,7 @@ private:
NetworkManager::WirelessDevice::Ptr wifiDevice; NetworkManager::WirelessDevice::Ptr wifiDevice;
QMap<QString, NetworkManager::WirelessNetwork::Ptr> wifiNetworkMap; QMap<QString, NetworkManager::WirelessNetwork::Ptr> wifiNetworkMap;
void handleWifiConnection(const QString &ssid); void handleWifiConnection(const QString &ssid, bool recoverFromWrongPassword = false);
void initLanguageComboBox(); void initLanguageComboBox();
QStringList getAvailableLanguages() const; QStringList getAvailableLanguages() const;
void showWifiOptions(); void showWifiOptions();

Loading…
Cancel
Save