More functionality, get cloned MAC address management working

main
Aaron Rainbolt 1 year ago
parent 117df45214
commit 77320c140f

@ -1,4 +1,5 @@
#include "connectionsettingsengine.h"
#include <QDebug>
/*
* The configuration map:
@ -9,8 +10,8 @@
* vpnAutoconnectEnabled: bool
* autoconnectVpn: QString
* meteredConnection: ConnectionSettingsEngine::Metered
* !device: QString
* !clonedMacAddress: QString
* device: QString
* clonedMacAddress: QString
* !mtu: int
*
* For Ethernet devices only:
@ -28,6 +29,8 @@ QVariantMap ConnectionSettingsEngine::readConnectionSettings(QString connUuidStr
// Contains adapted code from plasma-nm
NetworkManager::Connection::Ptr conn = NetworkManager::findConnectionByUuid(connUuidStr);
NetworkManager::ConnectionSettings::Ptr connSettings = conn->settings();
NetworkManager::WiredSetting::Ptr connWiredSetting = connSettings->setting(NetworkManager::Setting::Wired).dynamicCast<NetworkManager::WiredSetting>();
NetworkManager::WirelessSetting::Ptr connWirelessSetting = connSettings->setting(NetworkManager::Setting::Wireless).dynamicCast<NetworkManager::WirelessSetting>();
QVariantMap result;
result.insert("autoconnectEnabled", connSettings->autoconnect());
result.insert("autoconnectPriority", connSettings->autoconnectPriority());
@ -73,6 +76,13 @@ QVariantMap ConnectionSettingsEngine::readConnectionSettings(QString connUuidStr
break;
}
result.insert("device", connSettings->interfaceName());
if (!connWiredSetting.isNull()) {
result.insert("clonedMacAddress", NetworkManager::macAddressAsString(connWiredSetting->clonedMacAddress()));
} else if (!connWirelessSetting.isNull()) {
result.insert("clonedMacAddress", NetworkManager::macAddressAsString(connWirelessSetting->clonedMacAddress()));
}
// TODO: pick up here
return result;
@ -81,8 +91,12 @@ QVariantMap ConnectionSettingsEngine::readConnectionSettings(QString connUuidStr
void ConnectionSettingsEngine::modifyConnectionSettings(QString connUuidStr, QVariantMap settings)
{
// Contains adapted code from plasma-nm
bool wipeClonedMacAddress = false;
NetworkManager::Connection::Ptr conn = NetworkManager::findConnectionByUuid(connUuidStr);
NetworkManager::ConnectionSettings::Ptr connSettings = conn->settings();
NetworkManager::WiredSetting::Ptr connWiredSetting = connSettings->setting(NetworkManager::Setting::Wired).dynamicCast<NetworkManager::WiredSetting>();
NetworkManager::WirelessSetting::Ptr connWirelessSetting = connSettings->setting(NetworkManager::Setting::Wireless).dynamicCast<NetworkManager::WirelessSetting>();
if (settings["autoconnectEnabled"].isValid()) {
connSettings->setAutoconnect(settings["autoconnectEnabled"].toBool());
@ -95,7 +109,7 @@ void ConnectionSettingsEngine::modifyConnectionSettings(QString connUuidStr, QVa
connSettings->setPermissions(QHash<QString, QString>());
} else {
if (connSettings->permissions().isEmpty()) {
connSettings->addToPermissions(ConnectionSettingsEngine::userName(), QString());
connSettings->addToPermissions(userName(), QString());
} // Otherwise we just leave the permissions as-is
}
}
@ -120,7 +134,41 @@ void ConnectionSettingsEngine::modifyConnectionSettings(QString connUuidStr, QVa
}
}
if (settings["device"].isValid()) {
connSettings->setInterfaceName(settings["device"].toString());
}
if (settings["clonedMacAddress"].isValid()) {
QByteArray macBin = NetworkManager::macAddressFromString(settings["clonedMacAddress"].toString());
if (settings["clonedMacAddress"].toString().length() != 17) {
wipeClonedMacAddress = true;
} else {
if (!connWiredSetting.isNull()) {
connWiredSetting->setClonedMacAddress(macBin);
} else if (!connWirelessSetting.isNull()) {
connWirelessSetting->setClonedMacAddress(macBin);
}
}
}
// TODO: pick up here
conn->update(connSettings->toMap());
if (wipeClonedMacAddress) {
// Wiping a MAC address with NetworkManagerQt is surprisingly difficult, so we do it with nmcli instead.
QProcess clonedMacWiper;
clonedMacWiper.setProgram("bash");
if (!connWiredSetting.isNull()) {
clonedMacWiper.setArguments(QStringList() << QString("-c") << QString("nmcli connection modify %1 ethernet.cloned-mac-address \"\"").arg(conn->uuid()));
clonedMacWiper.start();
clonedMacWiper.waitForFinished();
} else if (!connWirelessSetting.isNull()) {
clonedMacWiper.setArguments(QStringList() << QString("-c") << QString("nmcli connection modify %1 wifi.cloned-mac-address \"\"").arg(conn->uuid()));
clonedMacWiper.start();
clonedMacWiper.waitForFinished();
}
}
}
QString ConnectionSettingsEngine::userName()

@ -9,6 +9,10 @@
#include <NetworkManagerQt/ConnectionSettings>
#include <NetworkManagerQt/Settings>
#include <NetworkManagerQt/Manager>
#include <NetworkManagerQt/Setting>
#include <NetworkManagerQt/WiredSetting>
#include <NetworkManagerQt/WirelessSetting>
#include <NetworkManagerQt/Utils>
class ConnectionSettingsEngine
{

@ -15,6 +15,7 @@ EthernetSettings::EthernetSettings(QString title, QString connUuidStr, QWidget *
ui->tabWidget->addTab(ethernetSettingsTab, "Ethernet");
QVariantMap connSettings = ConnectionSettingsEngine::readConnectionSettings(connUuidStr);
generalSettingsTab->loadSettings(connSettings);
ethernetSettingsTab->loadSettings(connSettings);
connect(ui->cancelButton, &QPushButton::clicked, this, &EthernetSettings::onCancelButtonClicked);
connect(ui->saveButton, &QPushButton::clicked, this, &EthernetSettings::onSaveButtonClicked);
}
@ -34,6 +35,7 @@ void EthernetSettings::onCancelButtonClicked()
void EthernetSettings::onSaveButtonClicked()
{
QVariantMap settings = generalSettingsTab->readSettings();
settings.insert(ethernetSettingsTab->readSettings());
ConnectionSettingsEngine::modifyConnectionSettings(targetConnUuidStr, settings);
this->done(0);
}

@ -6,6 +6,7 @@ EthernetSettingsTab::EthernetSettingsTab(QWidget *parent) :
ui(new Ui::EthernetSettingsTab)
{
ui->setupUi(this);
ui->clonedMacAddressLineEdit->setInputMask("HH:HH:HH:HH:HH:HH;_");
ui->speedComboBox->addItems(QStringList() << tr("10 Mb/s") << tr("100 Mb/s") << tr("1 Gb/s") << tr("2.5 Gb/s") << tr("10 Gb/s") << tr("40 Gb/s") << tr("100 Gb/s"));
ui->linkNegotiationComboBox->addItems(QStringList() << tr("Ignore") << tr("Automatic") << tr("Manual"));
}
@ -21,7 +22,7 @@ QVariantMap EthernetSettingsTab::readSettings()
{
QVariantMap output;
output.insert("device", QVariant(ui->deviceComboBox->currentText()));
output.insert("clonedMacAddress", QVariant(ui->clonedMacAddressComboBox->currentText()));
output.insert("clonedMacAddress", QVariant(ui->clonedMacAddressLineEdit->text()));
output.insert("mtu", QVariant(ui->mtuSpinBox->value()));
output.insert("linkNegotiation", QVariant(ui->linkNegotiationComboBox->currentText()));
switch(ui->speedComboBox->currentIndex()) {
@ -61,7 +62,7 @@ void EthernetSettingsTab::loadSettings(QVariantMap settings)
ui->deviceComboBox->setCurrentText(settings["device"].toString());
}
if (settings["clonedMacAddress"].isValid()) {
ui->clonedMacAddressComboBox->setCurrentText(settings["clonedMacAddress"].toString());
ui->clonedMacAddressLineEdit->setText(settings["clonedMacAddress"].toString());
}
if (settings["mtu"].isValid()) {
ui->mtuSpinBox->setValue(settings["mtu"].toInt());

@ -14,18 +14,24 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QComboBox" name="deviceComboBox">
<item row="3" column="0">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Link negotiation</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="linkNegotiationComboBox">
<item row="5" column="2">
<widget class="QComboBox" name="duplexComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
@ -34,49 +40,36 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Cloned MAC address</string>
<string>MTU</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="duplexComboBox">
<item row="0" column="2">
<widget class="QComboBox" name="deviceComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Link negotiation</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="4" column="2">
<widget class="QComboBox" name="speedComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@ -86,8 +79,8 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
@ -95,15 +88,15 @@
</sizepolicy>
</property>
<property name="text">
<string>Device</string>
<string>Cloned MAC address</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_7">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
@ -111,56 +104,69 @@
</sizepolicy>
</property>
<property name="text">
<string>Duplex</string>
<string>Device</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="clonedMacAddressComboBox">
<item row="4" column="0">
<widget class="QLabel" name="label_6">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Speed</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<item row="5" column="0">
<widget class="QLabel" name="label_7">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>MTU</string>
<string>Duplex</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_6">
<item row="6" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="2">
<widget class="QComboBox" name="linkNegotiationComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Speed</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="2" column="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QSpinBox" name="mtuSpinBox">
@ -187,18 +193,8 @@
</item>
</layout>
</item>
<item row="6" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
<item row="1" column="2">
<widget class="QLineEdit" name="clonedMacAddressLineEdit"/>
</item>
</layout>
</widget>

@ -7,6 +7,7 @@ NetworkSelector::NetworkSelector(QWidget *parent)
, ui(new Ui::NetworkSelector)
{
ui->setupUi(this);
this->setWindowTitle(tr("Connection Editor"));
regenConnTree();
connect(ui->connTree, &QTreeView::clicked, this, &NetworkSelector::onTreeSingleClicked);
connect(ui->connTree, &QTreeView::doubleClicked, this, &NetworkSelector::onTreeDoubleClicked);

Loading…
Cancel
Save