~ubuntu-branches/ubuntu/saucy/plasma-nm/saucy-proposed

« back to all changes in this revision

Viewing changes to lib/hwaddrcombobox.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-08-16 19:07:09 UTC
  • Revision ID: package-import@ubuntu.com-20130816190709-ef9ydm9skigmg15l
Tags: upstream-0.0~git20130816
ImportĀ upstreamĀ versionĀ 0.0~git20130816

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2013 Lukas Tinkl <ltinkl@redhat.com>
 
3
 
 
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.
 
11
 
 
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.
 
16
 
 
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/>.
 
19
*/
 
20
 
 
21
#include "hwaddrcombobox.h"
 
22
 
 
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>
 
34
 
 
35
HwAddrComboBox::HwAddrComboBox(QWidget *parent) :
 
36
    KComboBox(parent), m_dirty(false)
 
37
{
 
38
    setEditable(true);
 
39
    setInsertPolicy(QComboBox::NoInsert);
 
40
 
 
41
    connect(this, SIGNAL(editTextChanged(QString)), SLOT(editTextChanged(QString)));
 
42
    connect(this, SIGNAL(currentIndexChanged(int)), SLOT(currentIndexChanged(int)));
 
43
}
 
44
 
 
45
bool HwAddrComboBox::isValid() const
 
46
{
 
47
    if (hwAddress().isEmpty()) {
 
48
        return true;
 
49
    }
 
50
 
 
51
    return NetworkManager::Utils::macAddressIsValid(hwAddress());
 
52
}
 
53
 
 
54
QString HwAddrComboBox::hwAddress() const
 
55
{
 
56
    QString result;
 
57
    if (!m_dirty)
 
58
        result = itemData(currentIndex()).toString();
 
59
    else
 
60
        result = currentText();
 
61
 
 
62
    //qDebug() << "Result:" << currentIndex() << result;
 
63
 
 
64
    return result;
 
65
}
 
66
 
 
67
void HwAddrComboBox::editTextChanged(const QString &)
 
68
{
 
69
    m_dirty = true;
 
70
    emit hwAddressChanged();
 
71
}
 
72
 
 
73
void HwAddrComboBox::currentIndexChanged(int)
 
74
{
 
75
    m_dirty = false;
 
76
    emit hwAddressChanged();
 
77
}
 
78
 
 
79
void HwAddrComboBox::init(const NetworkManager::Device::Type &deviceType, const QString &address)
 
80
{
 
81
    m_initialAddress = address;
 
82
 
 
83
    //qDebug() << "Initial address:" << m_initialAddress;
 
84
 
 
85
    QString deviceName;
 
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();
 
92
                } else {
 
93
                    deviceName = device->interfaceName();
 
94
                }
 
95
            }
 
96
            addAddressToCombo(device);
 
97
        }
 
98
    }
 
99
 
 
100
    const int index = findData(m_initialAddress);
 
101
    if (index == -1) {
 
102
        if (!m_initialAddress.isEmpty()) {
 
103
            QString text = QString("%1 (%2)").arg(deviceName).arg(m_initialAddress);
 
104
            insertItem(0, text, m_initialAddress);
 
105
        } else {
 
106
            insertItem(0, m_initialAddress, m_initialAddress);
 
107
        }
 
108
        setCurrentIndex(0);
 
109
    } else {
 
110
        setCurrentIndex(index);
 
111
    }
 
112
}
 
113
 
 
114
void HwAddrComboBox::addAddressToCombo(const NetworkManager::Device::Ptr &device)
 
115
{
 
116
    QVariant data = hwAddressFromDevice(device);
 
117
    //qDebug() << "Data:" << data;
 
118
 
 
119
    QString name;
 
120
    if (device->state() == NetworkManager::Device::Activated)
 
121
        name = device->ipInterfaceName();
 
122
    else
 
123
        name = device->interfaceName();
 
124
 
 
125
    //qDebug() << "Name:" << name;
 
126
 
 
127
    if (!data.isNull()) {
 
128
        if (name == data.toString()) {
 
129
            addItem(data.toString(), data);
 
130
        }
 
131
        else {
 
132
            addItem(QString("%1 (%2)").arg(name).arg(data.toString()), data);
 
133
        }
 
134
    }
 
135
}
 
136
 
 
137
 
 
138
QVariant HwAddrComboBox::hwAddressFromDevice(const NetworkManager::Device::Ptr& device)
 
139
{
 
140
    const NetworkManager::Device::Type type = device->type();
 
141
 
 
142
    QVariant data;
 
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();
 
161
    }
 
162
 
 
163
    return data;
 
164
}