You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
9.5 KiB

#include "ipv6settingstab.h"
#include "ui_ipv6settingstab.h"
Ipv6SettingsTab::Ipv6SettingsTab(QWidget *parent) :
QWidget(parent),
ui(new Ui::Ipv6SettingsTab)
{
ui->setupUi(this);
ui->methodComboBox->addItems(QStringList() << tr("Automatic") << tr("Automatic (only addresses)") << tr("Automatic (only DHCP)") << tr("Link-Local") << tr("Manual") << tr("Ignored") << tr("Disabled"));
ui->privacyComboBox->addItems(QStringList() << tr("Default") << tr("Disabled") << tr("Enabled (prefer public address)") << tr("Enabled (prefer temporary address)"));
connect(ui->methodComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &Ipv6SettingsTab::onMethodComboBoxCurrentIndexChanged);
connect(ui->editDnsServersButton, &QPushButton::clicked, this, &Ipv6SettingsTab::onEditDnsServersButtonClicked);
connect(ui->editSearchDomainsButton, &QPushButton::clicked, this, &Ipv6SettingsTab::onEditSearchDomainsButtonClicked);
connect(ui->manualIpv6ConfigurationAddButton, &QPushButton::clicked, this, &Ipv6SettingsTab::onManualIpv6ConfigurationAddButtonClicked);
connect(ui->manualIpv6ConfigurationRemoveButton, &QPushButton::clicked, this, &Ipv6SettingsTab::onManualIpv6ConfigurationRemoveButtonClicked);
}
Ipv6SettingsTab::~Ipv6SettingsTab()
{
delete ui;
}
QVariantMap Ipv6SettingsTab::readSettings()
{
// If the user has invalid data in here, just return the original settings data
if (!checkValidity()) {
return origSettings;
}
QVariantMap output;
switch (ui->methodComboBox->currentIndex()) {
case 0: // Automatic
output.insert("ipv6Method", ConnectionSettingsEngine::Ipv6Automatic);
break;
case 1: // Automatic (only addresses)
output.insert("ipv6Method", ConnectionSettingsEngine::Ipv6AutomaticAddressOnly);
break;
case 2: // Automatic (only DHCP)
output.insert("ipv6Method", ConnectionSettingsEngine::Ipv6AutomaticDhcpOnly);
break;
case 3: // Link-Local
output.insert("ipv6Method", ConnectionSettingsEngine::Ipv6LinkLocal);
break;
case 4: // Manual
output.insert("ipv6Method", ConnectionSettingsEngine::Ipv6Manual);
break;
case 5: // Ignored
output.insert("ipv6Method", ConnectionSettingsEngine::Ipv6Ignored);
break;
case 6: // Disabled
output.insert("ipv6Method", ConnectionSettingsEngine::Ipv6Disabled);
break;
}
output.insert("ipv6DnsServers", ui->dnsServersLineEdit->text());
output.insert("ipv6SearchDomains", ui->searchDomainsLineEdit->text());
switch (ui->privacyComboBox->currentIndex()) {
case 0: // Default
output.insert("ipv6Privacy", ConnectionSettingsEngine::Ipv6PrivacyDefault);
break;
case 1: // Disabled
output.insert("ipv6Privacy", ConnectionSettingsEngine::Ipv6PrivacyDisabled);
break;
case 2: // Enabled (prefer public address)
output.insert("ipv6Privacy", ConnectionSettingsEngine::Ipv6PrivacyEnabledPublicAddress);
break;
case 3: // Enabled (prefer temporary address)
output.insert("ipv6Privacy", ConnectionSettingsEngine::Ipv6PrivacyEnabledTempAddress);
break;
}
QList<NetworkManager::IpAddress> ipAddrList;
if (ui->methodComboBox->currentIndex() == 4) { // Manual
for (int i = 0, rowCount = ui->manualIpv6ConfigurationTable->rowCount(); i < rowCount; i++) {
NetworkManager::IpAddress ipAddr;
ipAddr.setIp(QHostAddress(ui->manualIpv6ConfigurationTable->item(i, 0)->text()));
ipAddr.setPrefixLength(ui->manualIpv6ConfigurationTable->item(i, 1)->text().toInt());
ipAddr.setGateway(QHostAddress(ui->manualIpv6ConfigurationTable->item(i, 2)->text()));
ipAddrList.append(ipAddr);
}
}
output.insert("ipv6AddressList", QVariant::fromValue(ipAddrList));
output.insert("ipv6Required", ui->ipv6RequiredCheckBox->isChecked());
return output;
}
void Ipv6SettingsTab::loadSettings(QVariantMap settings)
{
origSettings = settings; // We may need to restore these if something about the settings the user puts in is invalid.
if (settings["ipv6Method"].isValid()) {
switch (settings["ipv6Method"].toInt()) {
case ConnectionSettingsEngine::Ipv6Automatic:
ui->methodComboBox->setCurrentIndex(0);
break;
case ConnectionSettingsEngine::Ipv6AutomaticAddressOnly:
ui->methodComboBox->setCurrentIndex(1);
break;
case ConnectionSettingsEngine::Ipv6AutomaticDhcpOnly:
ui->methodComboBox->setCurrentIndex(2);
break;
case ConnectionSettingsEngine::Ipv6LinkLocal:
ui->methodComboBox->setCurrentIndex(3);
break;
case ConnectionSettingsEngine::Ipv6Manual:
ui->methodComboBox->setCurrentIndex(4);
break;
case ConnectionSettingsEngine::Ipv6Ignored:
ui->methodComboBox->setCurrentIndex(5);
break;
case ConnectionSettingsEngine::Ipv6Disabled:
ui->methodComboBox->setCurrentIndex(6);
break;
}
}
onMethodComboBoxCurrentIndexChanged(ui->methodComboBox->currentIndex());
if (settings["ipv6DnsServers"].isValid()) {
ui->dnsServersLineEdit->setText(settings["ipv6DnsServers"].toString());
}
if (settings["ipv6SearchDomains"].isValid()) {
ui->searchDomainsLineEdit->setText(settings["ipv6SearchDomais"].toString());
}
if (settings["ipv6Privacy"].isValid()) {
switch (settings["ipv6Privacy"].toInt()) {
case ConnectionSettingsEngine::Ipv6PrivacyDefault:
ui->privacyComboBox->setCurrentIndex(0);
break;
case ConnectionSettingsEngine::Ipv6PrivacyDisabled:
ui->privacyComboBox->setCurrentIndex(1);
break;
case ConnectionSettingsEngine::Ipv6PrivacyEnabledPublicAddress:
ui->privacyComboBox->setCurrentIndex(2);
break;
case ConnectionSettingsEngine::Ipv6PrivacyEnabledTempAddress:
ui->privacyComboBox->setCurrentIndex(3);
break;
}
}
if (settings["ipv6AddressList"].isValid()) {
QList<NetworkManager::IpAddress> ipAddrList = settings["ipv6AddressList"].value<QList<NetworkManager::IpAddress>>();
for (int i = 0; i < ipAddrList.length(); i++) {
int newRow = ui->manualIpv6ConfigurationTable->rowCount();
ui->manualIpv6ConfigurationTable->insertRow(newRow);
ui->manualIpv6ConfigurationTable->setItem(newRow, 0, new QTableWidgetItem(ipAddrList[i].ip().toString()));
ui->manualIpv6ConfigurationTable->setItem(newRow, 1, new QTableWidgetItem(QString(ipAddrList[i].prefixLength())));
ui->manualIpv6ConfigurationTable->setItem(newRow, 2, new QTableWidgetItem(ipAddrList[i].gateway().toString()));
}
}
if (settings["ipv6Required"].isValid()) {
ui->ipv6RequiredCheckBox->setChecked(settings["ipv6Required"].toBool());
}
}
void Ipv6SettingsTab::onMethodComboBoxCurrentIndexChanged(int index)
{
if (index == 4) { // manual
ui->manualIpv6ConfigurationTable->setEnabled(true);
ui->manualIpv6ConfigurationAddButton->setEnabled(true);
ui->manualIpv6ConfigurationRemoveButton->setEnabled(true);
} else {
ui->manualIpv6ConfigurationTable->setEnabled(false);
ui->manualIpv6ConfigurationAddButton->setEnabled(false);
ui->manualIpv6ConfigurationRemoveButton->setEnabled(false);
}
}
void Ipv6SettingsTab::onEditDnsServersButtonClicked()
{
ListEditorDialog led(this, ui->dnsServersLineEdit->text(), ',');
led.exec();
ui->dnsServersLineEdit->setText(led.list());
}
void Ipv6SettingsTab::onEditSearchDomainsButtonClicked()
{
ListEditorDialog led(this, ui->searchDomainsLineEdit->text(), ',');
led.exec();
ui->searchDomainsLineEdit->setText(led.list());
}
void Ipv6SettingsTab::onManualIpv6ConfigurationAddButtonClicked()
{
int newRow = ui->manualIpv6ConfigurationTable->rowCount();
ui->manualIpv6ConfigurationTable->insertRow(newRow);
}
void Ipv6SettingsTab::onManualIpv6ConfigurationRemoveButtonClicked()
{
ui->manualIpv6ConfigurationTable->removeRow(ui->manualIpv6ConfigurationTable->currentRow());
}
bool Ipv6SettingsTab::checkValidity()
{
// almost copied verbatim from plasma-nm
if (ui->methodComboBox->currentIndex() == 4) { // manual
if (!ui->manualIpv6ConfigurationTable->rowCount()) {
return false;
}
for (int i = 0, rowCount = ui->manualIpv6ConfigurationTable->rowCount(); i < rowCount; i++) {
QHostAddress ip = QHostAddress(ui->manualIpv6ConfigurationTable->item(i, 0)->text());
const int prefix = ui->manualIpv6ConfigurationTable->item(i, 1)->text().toInt();
QHostAddress gateway = QHostAddress(ui->manualIpv6ConfigurationTable->item(i, 2)->text());
if (ip.isNull() || !(prefix >= 1 && prefix <= 128) || (gateway.isNull() && !ui->manualIpv6ConfigurationTable->item(i, 2)->text().isEmpty())) {
return false;
}
}
}
if (!ui->dnsServersLineEdit->text().isEmpty() && (ui->methodComboBox->currentIndex() == 0 || ui->methodComboBox->currentIndex() == 1 || ui->methodComboBox->currentIndex() == 4)) { // Automatic, AutomaticIPOnly, or Manual
const QStringList tmp = ui->dnsServersLineEdit->text().split(QLatin1Char(','));
for (const QString &str : tmp) {
QHostAddress addr(str);
if (addr.isNull()) {
return false;
}
}
}
return true;
}