2
Copyright 2013 Lukas Tinkl <ltinkl@redhat.com>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Lesser General Public
6
License as published by the Free Software Foundation; either
7
version 2.1 of the License, or (at your option) version 3, or any
8
later version accepted by the membership of KDE e.V. (or its
9
successor approved by the membership of KDE e.V.), which shall
10
act as a proxy defined in Section 6 of version 3 of the license.
12
This library is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
Lesser General Public License for more details.
17
You should have received a copy of the GNU Lesser General Public
18
License along with this library. If not, see <http://www.gnu.org/licenses/>.
21
#include "hwaddrcombobox.h"
23
#include <NetworkManagerQt/Manager>
24
#include <NetworkManagerQt/WiredDevice>
25
#include <NetworkManagerQt/WirelessDevice>
26
#include <NetworkManagerQt/BluetoothDevice>
27
#include <NetworkManagerQt/OlpcMeshDevice>
28
#include <NetworkManagerQt/WimaxDevice>
29
#include <NetworkManagerQt/InfinibandDevice>
30
#include <NetworkManagerQt/BondDevice>
31
#include <NetworkManagerQt/BridgeDevice>
32
#include <NetworkManagerQt/VlanDevice>
33
#include <NetworkManagerQt/Utils>
35
HwAddrComboBox::HwAddrComboBox(QWidget *parent) :
36
KComboBox(parent), m_dirty(false)
39
setInsertPolicy(QComboBox::NoInsert);
41
connect(this, SIGNAL(editTextChanged(QString)), SLOT(editTextChanged(QString)));
42
connect(this, SIGNAL(currentIndexChanged(int)), SLOT(currentIndexChanged(int)));
45
bool HwAddrComboBox::isValid() const
47
if (hwAddress().isEmpty()) {
51
return NetworkManager::Utils::macAddressIsValid(hwAddress());
54
QString HwAddrComboBox::hwAddress() const
58
result = itemData(currentIndex()).toString();
60
result = currentText();
62
//qDebug() << "Result:" << currentIndex() << result;
67
void HwAddrComboBox::editTextChanged(const QString &)
70
emit hwAddressChanged();
73
void HwAddrComboBox::currentIndexChanged(int)
76
emit hwAddressChanged();
79
void HwAddrComboBox::init(const NetworkManager::Device::Type &deviceType, const QString &address)
81
m_initialAddress = address;
83
//qDebug() << "Initial address:" << m_initialAddress;
86
foreach(const NetworkManager::Device::Ptr & device, NetworkManager::networkInterfaces()) {
87
const NetworkManager::Device::Type type = device->type();
88
if (type == deviceType) {
89
if (address == hwAddressFromDevice(device).toString()) {
90
if (device->state() == NetworkManager::Device::Activated) {
91
deviceName = device->ipInterfaceName();
93
deviceName = device->interfaceName();
96
addAddressToCombo(device);
100
const int index = findData(m_initialAddress);
102
if (!m_initialAddress.isEmpty()) {
103
QString text = QString("%1 (%2)").arg(deviceName).arg(m_initialAddress);
104
insertItem(0, text, m_initialAddress);
106
insertItem(0, m_initialAddress, m_initialAddress);
110
setCurrentIndex(index);
114
void HwAddrComboBox::addAddressToCombo(const NetworkManager::Device::Ptr &device)
116
QVariant data = hwAddressFromDevice(device);
117
//qDebug() << "Data:" << data;
120
if (device->state() == NetworkManager::Device::Activated)
121
name = device->ipInterfaceName();
123
name = device->interfaceName();
125
//qDebug() << "Name:" << name;
127
if (!data.isNull()) {
128
if (name == data.toString()) {
129
addItem(data.toString(), data);
132
addItem(QString("%1 (%2)").arg(name).arg(data.toString()), data);
138
QVariant HwAddrComboBox::hwAddressFromDevice(const NetworkManager::Device::Ptr& device)
140
const NetworkManager::Device::Type type = device->type();
143
if (type == NetworkManager::Device::Ethernet) {
144
data = device->as<NetworkManager::WiredDevice>()->hardwareAddress();
145
} else if (type == NetworkManager::Device::Wifi) {
146
data = device->as<NetworkManager::WirelessDevice>()->hardwareAddress();
147
} else if (type == NetworkManager::Device::Bluetooth) {
148
data = device->as<NetworkManager::BluetoothDevice>()->hardwareAddress();
149
} else if (type == NetworkManager::Device::OlpcMesh) {
150
data = device->as<NetworkManager::OlpcMeshDevice>()->hardwareAddress();
151
} else if (type == NetworkManager::Device::Wimax) {
152
data = device->as<NetworkManager::WimaxDevice>()->hardwareAddress();
153
} else if (type == NetworkManager::Device::InfiniBand) {
154
data = device->as<NetworkManager::InfinibandDevice>()->hwAddress();
155
} else if (type == NetworkManager::Device::Bond) {
156
data = device->as<NetworkManager::BondDevice>()->hwAddress();
157
} else if (type == NetworkManager::Device::Bridge) {
158
data = device->as<NetworkManager::BridgeDevice>()->hwAddress();
159
} else if (type == NetworkManager::Device::Vlan) {
160
data = device->as<NetworkManager::VlanDevice>()->hwAddress();