2
Copyright 2013 Lukas Tinkl <ltinkl@redhat.com>
3
Copyright 2013 Jan Grulich <jgrulich@redhat.com>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) version 3, or any
9
later version accepted by the membership of KDE e.V. (or its
10
successor approved by the membership of KDE e.V.), which shall
11
act as a proxy defined in Section 6 of version 3 of the license.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public
19
License along with this library. If not, see <http://www.gnu.org/licenses/>.
22
#include "ssidcombobox.h"
25
#include <NetworkManagerQt/Manager>
26
#include <NetworkManagerQt/WirelessDevice>
28
#include <QtAlgorithms>
31
#include <KLocalizedString>
33
bool signalCompare(const NetworkManager::WirelessNetwork::Ptr & one, const NetworkManager::WirelessNetwork::Ptr & two)
35
return one->signalStrength() > two->signalStrength();
38
SsidComboBox::SsidComboBox(QWidget *parent) :
39
KComboBox(parent), m_dirty(false)
42
setInsertPolicy(QComboBox::NoInsert);
44
connect(this, SIGNAL(editTextChanged(QString)), SLOT(editTextChanged(QString)));
45
connect(this, SIGNAL(activated(int)), SLOT(currentIndexChanged(int)));
48
QString SsidComboBox::ssid() const
52
result = itemData(currentIndex()).toString();
54
result = currentText();
56
//qDebug() << "Result:" << currentIndex() << result;
61
void SsidComboBox::editTextChanged(const QString &)
67
void SsidComboBox::currentIndexChanged(int)
74
void SsidComboBox::init(const QString &ssid)
78
//qDebug() << "Initial ssid:" << m_initialSsid;
80
QList<NetworkManager::WirelessNetwork::Ptr> networks;
82
foreach(const NetworkManager::Device::Ptr & device, NetworkManager::networkInterfaces()) {
83
if (device->type() == NetworkManager::Device::Wifi) {
84
NetworkManager::WirelessDevice::Ptr wifiDevice = device.objectCast<NetworkManager::WirelessDevice>();
86
foreach (const NetworkManager::WirelessNetwork::Ptr & newNetwork, wifiDevice->networks()) {
88
foreach (const NetworkManager::WirelessNetwork::Ptr & existingNetwork, networks) {
89
if (newNetwork->ssid() == existingNetwork->ssid()) {
90
if (newNetwork->signalStrength() > existingNetwork->signalStrength()) {
91
networks.removeOne(existingNetwork);
100
networks << newNetwork;
106
qSort(networks.begin(), networks.end(), signalCompare);
107
addSsidsToCombo(networks);
109
int index = findData(m_initialSsid);
111
insertItem(0, m_initialSsid, m_initialSsid);
114
setCurrentIndex(index);
116
setEditText(m_initialSsid);
119
void SsidComboBox::addSsidsToCombo(const QList<NetworkManager::WirelessNetwork::Ptr> &networks)
121
QList<NetworkManager::WirelessDevice::Ptr> wifiDevices;
123
foreach (const NetworkManager::Device::Ptr & dev, NetworkManager::networkInterfaces()) {
124
if (dev->type() == NetworkManager::Device::Wifi) {
125
wifiDevices << dev.objectCast<NetworkManager::WirelessDevice>();
132
foreach (const NetworkManager::WirelessNetwork::Ptr & network, networks) {
133
NetworkManager::AccessPoint::Ptr accessPoint = network->referenceAccessPoint();
139
foreach (const NetworkManager::WirelessDevice::Ptr & wifiDev, wifiDevices) {
140
if (wifiDev->findNetwork(network->ssid()) == network) {
142
insertSeparator(count());
146
if (network->ssid().length() > longestSsid.length()) {
147
longestSsid = network->ssid();
150
NetworkManager::Utils::WirelessSecurityType security = NetworkManager::Utils::findBestWirelessSecurity(wifiDev->wirelessCapabilities(), true, (wifiDev->mode() == NetworkManager::WirelessDevice::Adhoc), accessPoint->capabilities(), accessPoint->wpaFlags(), accessPoint->rsnFlags());
151
if (security != NetworkManager::Utils::Unknown && security != NetworkManager::Utils::None) {
152
QString text = i18n("%1 (%2%)\nSecurity: %3\nFrequency: %4 Mhz", accessPoint->ssid(), network->signalStrength(), UiUtils::labelFromWirelessSecurity(security), accessPoint->frequency());
153
addItem(KIcon("object-locked"), text, accessPoint->ssid());
155
QString text = i18n("%1 (%2%)\nSecurity: Insecure\nFrequency: %3 Mhz", accessPoint->ssid(), network->signalStrength(), accessPoint->frequency());
156
addItem(KIcon("object-unlocked"), text, accessPoint->ssid());
162
QFontMetrics metrics(font());
163
setMinimumWidth(metrics.width(longestSsid));