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

« back to all changes in this revision

Viewing changes to lib/ssidcombobox.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
    Copyright 2013 Jan Grulich <jgrulich@redhat.com>
 
4
 
 
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.
 
12
 
 
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.
 
17
 
 
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/>.
 
20
*/
 
21
 
 
22
#include "ssidcombobox.h"
 
23
#include "uiutils.h"
 
24
 
 
25
#include <NetworkManagerQt/Manager>
 
26
#include <NetworkManagerQt/WirelessDevice>
 
27
 
 
28
#include <QtAlgorithms>
 
29
 
 
30
#include <KIcon>
 
31
#include <KLocalizedString>
 
32
 
 
33
bool signalCompare(const NetworkManager::WirelessNetwork::Ptr & one, const NetworkManager::WirelessNetwork::Ptr & two)
 
34
{
 
35
    return one->signalStrength() > two->signalStrength();
 
36
}
 
37
 
 
38
SsidComboBox::SsidComboBox(QWidget *parent) :
 
39
    KComboBox(parent), m_dirty(false)
 
40
{
 
41
    setEditable(true);
 
42
    setInsertPolicy(QComboBox::NoInsert);
 
43
 
 
44
    connect(this, SIGNAL(editTextChanged(QString)), SLOT(editTextChanged(QString)));
 
45
    connect(this, SIGNAL(activated(int)), SLOT(currentIndexChanged(int)));
 
46
}
 
47
 
 
48
QString SsidComboBox::ssid() const
 
49
{
 
50
    QString result;
 
51
    if (!m_dirty)
 
52
        result = itemData(currentIndex()).toString();
 
53
    else
 
54
        result = currentText();
 
55
 
 
56
    //qDebug() << "Result:" << currentIndex() << result;
 
57
 
 
58
    return result;
 
59
}
 
60
 
 
61
void SsidComboBox::editTextChanged(const QString &)
 
62
{
 
63
    m_dirty = true;
 
64
    emit ssidChanged();
 
65
}
 
66
 
 
67
void SsidComboBox::currentIndexChanged(int)
 
68
{
 
69
    m_dirty = false;
 
70
    setEditText(ssid());
 
71
    emit ssidChanged();
 
72
}
 
73
 
 
74
void SsidComboBox::init(const QString &ssid)
 
75
{
 
76
    m_initialSsid = ssid;
 
77
 
 
78
    //qDebug() << "Initial ssid:" << m_initialSsid;
 
79
 
 
80
    QList<NetworkManager::WirelessNetwork::Ptr> networks;
 
81
 
 
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>();
 
85
 
 
86
            foreach (const NetworkManager::WirelessNetwork::Ptr & newNetwork, wifiDevice->networks()) {
 
87
                bool found = false;
 
88
                foreach (const NetworkManager::WirelessNetwork::Ptr & existingNetwork, networks) {
 
89
                    if (newNetwork->ssid() == existingNetwork->ssid()) {
 
90
                        if (newNetwork->signalStrength() > existingNetwork->signalStrength()) {
 
91
                            networks.removeOne(existingNetwork);
 
92
                            break;
 
93
                        } else {
 
94
                            found = true;
 
95
                            break;
 
96
                        }
 
97
                    }
 
98
                }
 
99
                if (!found) {
 
100
                    networks << newNetwork;
 
101
                }
 
102
            }
 
103
        }
 
104
    }
 
105
 
 
106
    qSort(networks.begin(), networks.end(), signalCompare);
 
107
    addSsidsToCombo(networks);
 
108
 
 
109
    int index = findData(m_initialSsid);
 
110
    if (index == -1) {
 
111
        insertItem(0, m_initialSsid, m_initialSsid);
 
112
        setCurrentIndex(0);
 
113
    } else {
 
114
        setCurrentIndex(index);
 
115
    }
 
116
    setEditText(m_initialSsid);
 
117
}
 
118
 
 
119
void SsidComboBox::addSsidsToCombo(const QList<NetworkManager::WirelessNetwork::Ptr> &networks)
 
120
{
 
121
    QList<NetworkManager::WirelessDevice::Ptr> wifiDevices;
 
122
 
 
123
    foreach (const NetworkManager::Device::Ptr & dev, NetworkManager::networkInterfaces()) {
 
124
        if (dev->type() == NetworkManager::Device::Wifi) {
 
125
            wifiDevices << dev.objectCast<NetworkManager::WirelessDevice>();
 
126
        }
 
127
    }
 
128
 
 
129
    QString longestSsid;
 
130
    bool empty = true;
 
131
 
 
132
    foreach (const NetworkManager::WirelessNetwork::Ptr & network, networks) {
 
133
        NetworkManager::AccessPoint::Ptr accessPoint = network->referenceAccessPoint();
 
134
 
 
135
        if (!accessPoint) {
 
136
            continue;
 
137
        }
 
138
 
 
139
        foreach (const NetworkManager::WirelessDevice::Ptr & wifiDev, wifiDevices) {
 
140
            if (wifiDev->findNetwork(network->ssid()) == network) {
 
141
                if (!empty) {
 
142
                    insertSeparator(count());
 
143
                }
 
144
                empty = false;
 
145
 
 
146
                if (network->ssid().length() > longestSsid.length()) {
 
147
                    longestSsid = network->ssid();
 
148
                }
 
149
 
 
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());
 
154
                } else {
 
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());
 
157
                }
 
158
            }
 
159
        }
 
160
    }
 
161
 
 
162
    QFontMetrics metrics(font());
 
163
    setMinimumWidth(metrics.width(longestSsid));
 
164
}