File indexing completed on 2025-04-20 13:28:31
0001 /* 0002 SPDX-FileCopyrightText: 2013 Jan Grulich <jgrulich@redhat.com> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "wiredconnectionwidget.h" 0008 #include "ui_wiredconnectionwidget.h" 0009 0010 #include <NetworkManagerQt/Utils> 0011 #include <NetworkManagerQt/WiredSetting> 0012 0013 #include <QRandomGenerator> 0014 0015 WiredConnectionWidget::WiredConnectionWidget(const NetworkManager::Setting::Ptr &setting, QWidget *parent, Qt::WindowFlags f) 0016 : SettingWidget(setting, parent, f) 0017 , m_widget(new Ui::WiredConnectionWidget) 0018 { 0019 m_widget->setupUi(this); 0020 0021 connect(m_widget->btnRandomMacAddr, &QPushButton::clicked, this, &WiredConnectionWidget::generateRandomClonedMac); 0022 0023 // Connect for setting check 0024 watchChangedSetting(); 0025 0026 // Connect for validity check 0027 connect(m_widget->clonedMacAddress, &KLineEdit::textChanged, this, &WiredConnectionWidget::slotWidgetChanged); 0028 connect(m_widget->macAddress, &HwAddrComboBox::hwAddressChanged, this, &WiredConnectionWidget::slotWidgetChanged); 0029 connect(m_widget->linkNegotiation, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) { 0030 m_widget->duplex->setEnabled(index == LinkNegotiation::Manual); 0031 m_widget->speed->setEnabled(index == LinkNegotiation::Manual); 0032 }); 0033 0034 KAcceleratorManager::manage(this); 0035 0036 if (setting) { 0037 loadConfig(setting); 0038 } 0039 } 0040 0041 WiredConnectionWidget::~WiredConnectionWidget() 0042 { 0043 delete m_widget; 0044 } 0045 0046 void WiredConnectionWidget::loadConfig(const NetworkManager::Setting::Ptr &setting) 0047 { 0048 NetworkManager::WiredSetting::Ptr wiredSetting = setting.staticCast<NetworkManager::WiredSetting>(); 0049 0050 m_widget->macAddress->init(NetworkManager::Device::Ethernet, NetworkManager::macAddressAsString(wiredSetting->macAddress())); 0051 0052 if (!wiredSetting->clonedMacAddress().isEmpty()) { 0053 m_widget->clonedMacAddress->setText(NetworkManager::macAddressAsString(wiredSetting->clonedMacAddress())); 0054 } 0055 0056 if (wiredSetting->mtu()) { 0057 m_widget->mtu->setValue(wiredSetting->mtu()); 0058 } 0059 0060 if (wiredSetting->autoNegotiate()) { 0061 m_widget->linkNegotiation->setCurrentIndex(LinkNegotiation::Automatic); 0062 } else if (wiredSetting->speed() && wiredSetting->duplexType() != NetworkManager::WiredSetting::UnknownDuplexType) { 0063 m_widget->linkNegotiation->setCurrentIndex(LinkNegotiation::Manual); 0064 } 0065 0066 if (wiredSetting->speed()) { 0067 switch (wiredSetting->speed()) { 0068 case 10: 0069 m_widget->speed->setCurrentIndex(0); 0070 break; 0071 case 100: 0072 m_widget->speed->setCurrentIndex(1); 0073 break; 0074 case 1000: 0075 m_widget->speed->setCurrentIndex(2); 0076 break; 0077 case 2500: 0078 m_widget->speed->setCurrentIndex(3); 0079 break; 0080 case 5000: 0081 m_widget->speed->setCurrentIndex(4); 0082 break; 0083 case 10000: 0084 m_widget->speed->setCurrentIndex(5); 0085 break; 0086 case 40000: 0087 m_widget->speed->setCurrentIndex(6); 0088 break; 0089 case 100000: 0090 m_widget->speed->setCurrentIndex(7); 0091 break; 0092 } 0093 } 0094 0095 if (wiredSetting->duplexType() != NetworkManager::WiredSetting::Half) { 0096 m_widget->duplex->setCurrentIndex(Duplex::Full); 0097 } else { 0098 m_widget->duplex->setCurrentIndex(Duplex::Half); 0099 } 0100 } 0101 0102 QVariantMap WiredConnectionWidget::setting() const 0103 { 0104 NetworkManager::WiredSetting wiredSetting; 0105 0106 wiredSetting.setMacAddress(NetworkManager::macAddressFromString(m_widget->macAddress->hwAddress())); 0107 0108 if (!m_widget->clonedMacAddress->text().isEmpty() && m_widget->clonedMacAddress->text() != QLatin1String(":::::")) { 0109 wiredSetting.setClonedMacAddress(NetworkManager::macAddressFromString(m_widget->clonedMacAddress->text())); 0110 } 0111 0112 if (m_widget->mtu->value()) { 0113 wiredSetting.setMtu(m_widget->mtu->value()); 0114 } 0115 0116 if (m_widget->linkNegotiation->currentIndex() == LinkNegotiation::Automatic || m_widget->linkNegotiation->currentIndex() == LinkNegotiation::Ignore) { 0117 wiredSetting.setDuplexType(NetworkManager::WiredSetting::UnknownDuplexType); 0118 wiredSetting.setSpeed(0); 0119 } else { 0120 switch (m_widget->speed->currentIndex()) { 0121 case 0: 0122 wiredSetting.setSpeed(10); 0123 break; 0124 case 1: 0125 wiredSetting.setSpeed(100); 0126 break; 0127 case 2: 0128 wiredSetting.setSpeed(1000); 0129 break; 0130 case 3: 0131 wiredSetting.setSpeed(2500); 0132 break; 0133 case 4: 0134 wiredSetting.setSpeed(5000); 0135 break; 0136 case 5: 0137 wiredSetting.setSpeed(10000); 0138 break; 0139 case 6: 0140 wiredSetting.setSpeed(40000); 0141 break; 0142 case 7: 0143 wiredSetting.setSpeed(100000); 0144 break; 0145 } 0146 0147 if (m_widget->duplex->currentIndex() == Duplex::Full) { 0148 wiredSetting.setDuplexType(NetworkManager::WiredSetting::Full); 0149 } else { 0150 wiredSetting.setDuplexType(NetworkManager::WiredSetting::Half); 0151 } 0152 } 0153 0154 wiredSetting.setAutoNegotiate(m_widget->linkNegotiation->currentIndex() == LinkNegotiation::Automatic); 0155 0156 return wiredSetting.toMap(); 0157 } 0158 0159 void WiredConnectionWidget::generateRandomClonedMac() 0160 { 0161 auto generator = QRandomGenerator::global(); 0162 QByteArray mac; 0163 mac.resize(6); 0164 for (int i = 0; i < 6; i++) { 0165 const int random = generator->bounded(255); 0166 mac[i] = random; 0167 } 0168 0169 // Disable the multicast bit and enable the locally administered bit. 0170 mac[0] = mac[0] & ~0x1; 0171 mac[0] = mac[0] | 0x2; 0172 0173 m_widget->clonedMacAddress->setText(NetworkManager::macAddressAsString(mac)); 0174 } 0175 0176 bool WiredConnectionWidget::isValid() const 0177 { 0178 if (!m_widget->macAddress->isValid()) { 0179 return false; 0180 } 0181 0182 if (m_widget->clonedMacAddress->text() != QLatin1String(":::::")) { 0183 if (!NetworkManager::macAddressIsValid(m_widget->clonedMacAddress->text())) { 0184 return false; 0185 } 0186 } 0187 0188 return true; 0189 }