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 "connectionsettingsengine.h"
#include <QDebug>
/* /*
* The configuration map: * The configuration map:
@ -9,8 +10,8 @@
* vpnAutoconnectEnabled: bool * vpnAutoconnectEnabled: bool
* autoconnectVpn: QString * autoconnectVpn: QString
* meteredConnection: ConnectionSettingsEngine::Metered * meteredConnection: ConnectionSettingsEngine::Metered
* !device: QString * device: QString
* !clonedMacAddress: QString * clonedMacAddress: QString
* !mtu: int * !mtu: int
* *
* For Ethernet devices only: * For Ethernet devices only:
@ -28,6 +29,8 @@ QVariantMap ConnectionSettingsEngine::readConnectionSettings(QString connUuidStr
// Contains adapted code from plasma-nm // Contains adapted code from plasma-nm
NetworkManager::Connection::Ptr conn = NetworkManager::findConnectionByUuid(connUuidStr); NetworkManager::Connection::Ptr conn = NetworkManager::findConnectionByUuid(connUuidStr);
NetworkManager::ConnectionSettings::Ptr connSettings = conn->settings(); 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; QVariantMap result;
result.insert("autoconnectEnabled", connSettings->autoconnect()); result.insert("autoconnectEnabled", connSettings->autoconnect());
result.insert("autoconnectPriority", connSettings->autoconnectPriority()); result.insert("autoconnectPriority", connSettings->autoconnectPriority());
@ -73,6 +76,13 @@ QVariantMap ConnectionSettingsEngine::readConnectionSettings(QString connUuidStr
break; 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 // TODO: pick up here
return result; return result;
@ -81,8 +91,12 @@ QVariantMap ConnectionSettingsEngine::readConnectionSettings(QString connUuidStr
void ConnectionSettingsEngine::modifyConnectionSettings(QString connUuidStr, QVariantMap settings) void ConnectionSettingsEngine::modifyConnectionSettings(QString connUuidStr, QVariantMap settings)
{ {
// Contains adapted code from plasma-nm // Contains adapted code from plasma-nm
bool wipeClonedMacAddress = false;
NetworkManager::Connection::Ptr conn = NetworkManager::findConnectionByUuid(connUuidStr); NetworkManager::Connection::Ptr conn = NetworkManager::findConnectionByUuid(connUuidStr);
NetworkManager::ConnectionSettings::Ptr connSettings = conn->settings(); 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()) { if (settings["autoconnectEnabled"].isValid()) {
connSettings->setAutoconnect(settings["autoconnectEnabled"].toBool()); connSettings->setAutoconnect(settings["autoconnectEnabled"].toBool());
@ -95,7 +109,7 @@ void ConnectionSettingsEngine::modifyConnectionSettings(QString connUuidStr, QVa
connSettings->setPermissions(QHash<QString, QString>()); connSettings->setPermissions(QHash<QString, QString>());
} else { } else {
if (connSettings->permissions().isEmpty()) { if (connSettings->permissions().isEmpty()) {
connSettings->addToPermissions(ConnectionSettingsEngine::userName(), QString()); connSettings->addToPermissions(userName(), QString());
} // Otherwise we just leave the permissions as-is } // 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()); 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() QString ConnectionSettingsEngine::userName()

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

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

@ -6,6 +6,7 @@ EthernetSettingsTab::EthernetSettingsTab(QWidget *parent) :
ui(new Ui::EthernetSettingsTab) ui(new Ui::EthernetSettingsTab)
{ {
ui->setupUi(this); 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->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")); ui->linkNegotiationComboBox->addItems(QStringList() << tr("Ignore") << tr("Automatic") << tr("Manual"));
} }
@ -21,7 +22,7 @@ QVariantMap EthernetSettingsTab::readSettings()
{ {
QVariantMap output; QVariantMap output;
output.insert("device", QVariant(ui->deviceComboBox->currentText())); 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("mtu", QVariant(ui->mtuSpinBox->value()));
output.insert("linkNegotiation", QVariant(ui->linkNegotiationComboBox->currentText())); output.insert("linkNegotiation", QVariant(ui->linkNegotiationComboBox->currentText()));
switch(ui->speedComboBox->currentIndex()) { switch(ui->speedComboBox->currentIndex()) {
@ -61,7 +62,7 @@ void EthernetSettingsTab::loadSettings(QVariantMap settings)
ui->deviceComboBox->setCurrentText(settings["device"].toString()); ui->deviceComboBox->setCurrentText(settings["device"].toString());
} }
if (settings["clonedMacAddress"].isValid()) { if (settings["clonedMacAddress"].isValid()) {
ui->clonedMacAddressComboBox->setCurrentText(settings["clonedMacAddress"].toString()); ui->clonedMacAddressLineEdit->setText(settings["clonedMacAddress"].toString());
} }
if (settings["mtu"].isValid()) { if (settings["mtu"].isValid()) {
ui->mtuSpinBox->setValue(settings["mtu"].toInt()); ui->mtuSpinBox->setValue(settings["mtu"].toInt());

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

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

Loading…
Cancel
Save