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.

155 lines
6.4 KiB

#include "ipv4settingstab.h"
#include "ui_ipv4settingstab.h"
Ipv4SettingsTab::Ipv4SettingsTab(QWidget *parent) :
QWidget(parent),
ui(new Ui::Ipv4SettingsTab)
{
ui->setupUi(this);
ui->methodComboBox->addItems(QStringList() << tr("Automatic") << tr("Automatic (address-only)") << tr("Link-Local") << tr("Manual") << tr("Shared to other computers") << tr("Disabled"));
connect(ui->methodComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &Ipv4SettingsTab::onMethodComboBoxCurrentIndexChanged);
connect(ui->editDnsServersButton, &QPushButton::clicked, this, &Ipv4SettingsTab::onEditDnsServersButtonClicked);
connect(ui->editSearchDomainsButton, &QPushButton::clicked, this, &Ipv4SettingsTab::onEditSearchDomainsButtonClicked);
connect(ui->manualIpv4ConfigurationAddButton, &QPushButton::clicked, this, &Ipv4SettingsTab::onManualIpv4ConfigurationAddButtonClicked);
connect(ui->manualIpv4ConfigurationRemoveButton, &QPushButton::clicked, this, &Ipv4SettingsTab::onManualIpv4ConfigurationRemoveButtonClicked);
}
Ipv4SettingsTab::~Ipv4SettingsTab()
{
delete ui;
}
QVariantMap Ipv4SettingsTab::readSettings()
{
QVariantMap output;
switch (ui->methodComboBox->currentIndex()) {
case 0: // Automatic
output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Automatic);
break;
case 1: // Automatic (address-only)
output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4AutomaticAddressOnly);
break;
case 2: // Link-local
output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4LinkLocal);
break;
case 3: // Manual
output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Manual);
break;
case 4: // Shared
output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Shared);
break;
case 5: // Disabled
output.insert("ipv4Method", ConnectionSettingsEngine::Ipv4Disabled);
break;
}
output.insert("ipv4DnsServers", ui->dnsServersLineEdit->text());
output.insert("ipv4SearchDomains", ui->searchDomainsLineEdit->text());
output.insert("ipv4DhcpClientId", ui->dhcpClientIdLineEdit->text());
QList<NetworkManager::IpAddress> ipAddrList;
if (ui->methodComboBox->currentIndex() == 3) { // Manual
for (int i = 0, rowCount = ui->manualIpv4ConfigurationTable->rowCount(); i < rowCount; i++) {
NetworkManager::IpAddress ipAddr;
ipAddr.setIp(QHostAddress(ui->manualIpv4ConfigurationTable->item(i, 0)->text()));
ipAddr.setNetmask(QHostAddress(ui->manualIpv4ConfigurationTable->item(i, 1)->text()));
ipAddr.setGateway(QHostAddress(ui->manualIpv4ConfigurationTable->item(i, 2)->text()));
ipAddrList.append(ipAddr);
}
}
output.insert("ipv4AddressList", QVariant::fromValue(ipAddrList));
output.insert("ipv4Required", ui->ipv4RequiredCheckBox->isChecked());
return output;
}
void Ipv4SettingsTab::loadSettings(QVariantMap settings)
{
if (settings["ipv4Method"].isValid()) {
switch (settings["ipv4Method"].toInt()) {
case ConnectionSettingsEngine::Ipv4Automatic:
ui->methodComboBox->setCurrentIndex(0);
break;
case ConnectionSettingsEngine::Ipv4AutomaticAddressOnly:
ui->methodComboBox->setCurrentIndex(1);
break;
case ConnectionSettingsEngine::Ipv4LinkLocal:
ui->methodComboBox->setCurrentIndex(2);
break;
case ConnectionSettingsEngine::Ipv4Manual:
ui->methodComboBox->setCurrentIndex(3);
break;
case ConnectionSettingsEngine::Ipv4Shared:
ui->methodComboBox->setCurrentIndex(4);
break;
case ConnectionSettingsEngine::Ipv4Disabled:
ui->methodComboBox->setCurrentIndex(5);
break;
}
}
onMethodComboBoxCurrentIndexChanged(ui->methodComboBox->currentIndex());
if (settings["ipv4DnsServers"].isValid()) {
ui->dnsServersLineEdit->setText(settings["ipv4DnsServers"].toString());
}
if (settings["ipv4SearchDomains"].isValid()) {
ui->searchDomainsLineEdit->setText(settings["ipv4SearchDomains"].toString());
}
if (settings["ipv4DhcpClientId"].isValid()) {
ui->dhcpClientIdLineEdit->setText(settings["ipv4DhcpClientId"].toString());
}
if (settings["ipv4AddressList"].isValid()) {
QList<NetworkManager::IpAddress> ipAddrList = settings["ipv4AddressList"].value<QList<NetworkManager::IpAddress>>();
for (int i = 0; i < ipAddrList.length(); i++) {
int newRow = ui->manualIpv4ConfigurationTable->rowCount();
ui->manualIpv4ConfigurationTable->insertRow(newRow);
ui->manualIpv4ConfigurationTable->setItem(newRow, 0, new QTableWidgetItem(ipAddrList[i].ip().toString()));
ui->manualIpv4ConfigurationTable->setItem(newRow, 1, new QTableWidgetItem(ipAddrList[i].netmask().toString()));
ui->manualIpv4ConfigurationTable->setItem(newRow, 2, new QTableWidgetItem(ipAddrList[i].gateway().toString()));
}
}
if (settings["ipv4Required"].isValid()) {
ui->ipv4RequiredCheckBox->setChecked(settings["ipv4Required"].toBool());
}
}
void Ipv4SettingsTab::onMethodComboBoxCurrentIndexChanged(int index)
{
if (index == 3) { // manual
ui->manualIpv4ConfigurationTable->setEnabled(true);
ui->manualIpv4ConfigurationAddButton->setEnabled(true);
ui->manualIpv4ConfigurationRemoveButton->setEnabled(true);
} else {
ui->manualIpv4ConfigurationTable->setEnabled(false);
ui->manualIpv4ConfigurationAddButton->setEnabled(false);
ui->manualIpv4ConfigurationRemoveButton->setEnabled(false);
}
}
void Ipv4SettingsTab::onEditDnsServersButtonClicked()
{
ListEditorDialog led(this, ui->dnsServersLineEdit->text(), ',');
led.exec();
ui->dnsServersLineEdit->setText(led.list());
}
void Ipv4SettingsTab::onEditSearchDomainsButtonClicked()
{
ListEditorDialog led(this, ui->searchDomainsLineEdit->text(), ',');
led.exec();
ui->searchDomainsLineEdit->setText(led.list());
}
void Ipv4SettingsTab::onManualIpv4ConfigurationAddButtonClicked()
{
int newRow = ui->manualIpv4ConfigurationTable->rowCount();
ui->manualIpv4ConfigurationTable->insertRow(newRow);
}
void Ipv4SettingsTab::onManualIpv4ConfigurationRemoveButtonClicked()
{
ui->manualIpv4ConfigurationTable->removeRow(ui->manualIpv4ConfigurationTable->currentRow());
}