File indexing completed on 2024-12-01 11:13:10

0001 /*
0002     SPDX-FileCopyrightText: 2013 Lukas Tinkl <ltinkl@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 "hwaddrcombobox.h"
0008 
0009 #include <NetworkManagerQt/BluetoothDevice>
0010 #include <NetworkManagerQt/BondDevice>
0011 #include <NetworkManagerQt/BridgeDevice>
0012 #include <NetworkManagerQt/InfinibandDevice>
0013 #include <NetworkManagerQt/Manager>
0014 #include <NetworkManagerQt/OlpcMeshDevice>
0015 #include <NetworkManagerQt/Utils>
0016 #include <NetworkManagerQt/VlanDevice>
0017 #include <NetworkManagerQt/WiredDevice>
0018 #include <NetworkManagerQt/WirelessDevice>
0019 
0020 HwAddrComboBox::HwAddrComboBox(QWidget *parent)
0021     : QComboBox(parent)
0022 {
0023     setEditable(true);
0024     setInsertPolicy(QComboBox::NoInsert);
0025 
0026     connect(this, &HwAddrComboBox::editTextChanged, this, &HwAddrComboBox::slotEditTextChanged);
0027     connect(this, QOverload<int>::of(&HwAddrComboBox::currentIndexChanged), this, &HwAddrComboBox::slotCurrentIndexChanged);
0028 }
0029 
0030 bool HwAddrComboBox::isValid() const
0031 {
0032     if (hwAddress().isEmpty()) {
0033         return true;
0034     }
0035 
0036     return NetworkManager::macAddressIsValid(hwAddress());
0037 }
0038 
0039 QString HwAddrComboBox::hwAddress() const
0040 {
0041     QString result;
0042     if (!m_dirty)
0043         result = itemData(currentIndex()).toString();
0044     else
0045         result = currentText();
0046 
0047     // qCDebug(PLASMA_NM_EDITOR_LOG)() << "Result:" << currentIndex() << result;
0048 
0049     return result;
0050 }
0051 
0052 void HwAddrComboBox::slotEditTextChanged(const QString &)
0053 {
0054     m_dirty = true;
0055     Q_EMIT hwAddressChanged();
0056 }
0057 
0058 void HwAddrComboBox::slotCurrentIndexChanged(int)
0059 {
0060     m_dirty = false;
0061     Q_EMIT hwAddressChanged();
0062 }
0063 
0064 void HwAddrComboBox::init(NetworkManager::Device::Type deviceType, const QString &address)
0065 {
0066     m_initialAddress = address;
0067 
0068     // qCDebug(PLASMA_NM_EDITOR_LOG) << "Initial address:" << m_initialAddress;
0069 
0070     QString deviceName;
0071     for (const NetworkManager::Device::Ptr &device : NetworkManager::networkInterfaces()) {
0072         const NetworkManager::Device::Type type = device->type();
0073         if (type == deviceType) {
0074             if (address == hwAddressFromDevice(device).toString()) {
0075                 if (device->state() == NetworkManager::Device::Activated) {
0076                     deviceName = device->ipInterfaceName();
0077                 } else {
0078                     deviceName = device->interfaceName();
0079                 }
0080             }
0081             addAddressToCombo(device);
0082         }
0083     }
0084 
0085     const int index = findData(m_initialAddress);
0086     if (index == -1) {
0087         if (!m_initialAddress.isEmpty()) {
0088             const QString text = QStringLiteral("%1 (%2)").arg(deviceName, m_initialAddress);
0089             insertItem(0, text, m_initialAddress);
0090         } else {
0091             insertItem(0, m_initialAddress, m_initialAddress);
0092         }
0093         setCurrentIndex(0);
0094     } else {
0095         setCurrentIndex(index);
0096     }
0097 }
0098 
0099 void HwAddrComboBox::addAddressToCombo(const NetworkManager::Device::Ptr &device)
0100 {
0101     const QVariant data = hwAddressFromDevice(device);
0102     // qCDebug(PLASMA_NM_EDITOR_LOG) << "Data:" << data;
0103 
0104     QString name;
0105     if (device->state() == NetworkManager::Device::Activated)
0106         name = device->ipInterfaceName();
0107     else
0108         name = device->interfaceName();
0109 
0110     // qCDebug(PLASMA_NM_EDITOR_LOG) << "Name:" << name;
0111 
0112     if (!data.isNull()) {
0113         if (name == data.toString()) {
0114             addItem(data.toString(), data);
0115         } else {
0116             addItem(QStringLiteral("%1 (%2)").arg(name, data.toString()), data);
0117         }
0118     }
0119 }
0120 
0121 QVariant HwAddrComboBox::hwAddressFromDevice(const NetworkManager::Device::Ptr &device)
0122 {
0123     const NetworkManager::Device::Type type = device->type();
0124 
0125     QVariant data;
0126     if (type == NetworkManager::Device::Ethernet) {
0127         data = device->as<NetworkManager::WiredDevice>()->permanentHardwareAddress();
0128     } else if (type == NetworkManager::Device::Wifi) {
0129         data = device->as<NetworkManager::WirelessDevice>()->permanentHardwareAddress();
0130     } else if (type == NetworkManager::Device::Bluetooth) {
0131         data = device->as<NetworkManager::BluetoothDevice>()->hardwareAddress();
0132     } else if (type == NetworkManager::Device::OlpcMesh) {
0133         data = device->as<NetworkManager::OlpcMeshDevice>()->hardwareAddress();
0134     } else if (type == NetworkManager::Device::InfiniBand) {
0135         data = device->as<NetworkManager::InfinibandDevice>()->hwAddress();
0136     } else if (type == NetworkManager::Device::Bond) {
0137         data = device->as<NetworkManager::BondDevice>()->hwAddress();
0138     } else if (type == NetworkManager::Device::Bridge) {
0139         data = device->as<NetworkManager::BridgeDevice>()->hwAddress();
0140     } else if (type == NetworkManager::Device::Vlan) {
0141         data = device->as<NetworkManager::VlanDevice>()->hwAddress();
0142     }
0143 
0144     return data;
0145 }