2 * Copyright (C) 2007 Percy Leonhardt <percy@eris23.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "networkengine.h"
29 #include <QStringList>
30 #include <QNetworkAddressEntry>
34 #include <kio/global.h>
35 #include <Plasma/DataContainer>
36 #include <solid/control/networkmanager.h>
37 #include <solid/control/wirelessnetworkinterface.h>
38 #include <solid/control/wirelessaccesspoint.h>
40 //#define SYSPATH "/sys/class/net/"
42 NetworkEngine::NetworkEngine(QObject
* parent
, const QVariantList
& args
)
43 : Plasma::DataEngine(parent
, args
),
44 m_secondsSinceLastUpdate(0)
47 setMinimumPollingInterval(1000);
50 NetworkEngine::~NetworkEngine()
54 QStringList
NetworkEngine::sources() const
56 Solid::Control::NetworkInterfaceList iflist
= Solid::Control::NetworkManager::networkInterfaces();
58 QStringList availableInterfaces
;
59 foreach (Solid::Control::NetworkInterface
*iface
, iflist
) {
60 availableInterfaces
.append(iface
->uni().section('/', -1));
62 return availableInterfaces
;
65 bool NetworkEngine::sourceRequestEvent(const QString
&name
)
67 Solid::Control::NetworkInterfaceList iflist
= Solid::Control::NetworkManager::networkInterfaces();
69 foreach(Solid::Control::NetworkInterface
*iface
, iflist
) {
70 if (name
== iface
->uni().section('/', -1)) {
71 setData(name
, DataEngine::Data());
72 setData(name
, I18N_NOOP("UNI"), iface
->uni());
73 updateSourceEvent(name
);
80 bool NetworkEngine::updateSourceEvent(const QString
&source
)
82 QString uni
= query(source
)[I18N_NOOP("UNI")].toString();
83 Solid::Control::NetworkInterface
*iface
= Solid::Control::NetworkManager::findNetworkInterface(uni
);
85 // remove the source as the interface is no longer available
90 // Check if it is a wireless interface.
91 bool isWireless
= iface
->type() == Solid::Control::NetworkInterface::Ieee80211
;
92 setData(source
, I18N_NOOP("Wireless"), isWireless
);
93 Solid::Control::WirelessNetworkInterface
*wlan
= 0;
96 wlan
= static_cast<Solid::Control::WirelessNetworkInterface
*>(iface
);
98 QStringList availableNetworks
;
99 Solid::Control::AccessPointList apList
= wlan
->accessPoints();
100 foreach (const QString
&apId
, apList
) {
101 Solid::Control::AccessPoint
*ap
= wlan
->findAccessPoint(apId
);
102 availableNetworks
+= ap
->ssid();
104 setData(source
, I18N_NOOP("Available networks"), availableNetworks
);
107 Solid::Control::NetworkInterface::ConnectionState connectionState
= iface
->connectionState();
108 if (connectionState
== Solid::Control::NetworkInterface::Activated
) {
109 // update the interface
110 updateInterfaceData(source
, iface
);
112 updateWirelessData(source
, wlan
);
114 } else if (connectionState
!= Solid::Control::NetworkInterface::Activated
&&
115 query(source
)[I18N_NOOP("ConnectionState")].toString() == I18N_NOOP("Activated")) {
116 // the interface was disconnected
117 removeAllData(source
);
118 setData(source
, I18N_NOOP("UNI"), uni
);
121 switch (connectionState
) {
122 case Solid::Control::NetworkInterface::Preparing
:
123 setData(source
, I18N_NOOP("ConnectionState"), "Preparing");
125 case Solid::Control::NetworkInterface::Configuring
:
126 setData(source
, I18N_NOOP("ConnectionState"), "Configuring");
128 case Solid::Control::NetworkInterface::NeedAuth
:
129 setData(source
, I18N_NOOP("ConnectionState"), "NeedAuth");
131 case Solid::Control::NetworkInterface::IPConfig
:
132 setData(source
, I18N_NOOP("ConnectionState"), "IPConfig");
134 case Solid::Control::NetworkInterface::Activated
:
135 setData(source
, I18N_NOOP("ConnectionState"), "Activated");
137 case Solid::Control::NetworkInterface::Failed
:
138 setData(source
, I18N_NOOP("ConnectionState"), "Failed");
140 case Solid::Control::NetworkInterface::Unmanaged
:
141 setData(source
, I18N_NOOP("ConnectionState"), "Unmanaged");
143 case Solid::Control::NetworkInterface::Unavailable
:
144 setData(source
, I18N_NOOP("ConnectionState"), "Unavailable");
147 setData(source
, I18N_NOOP("ConnectionState"), "UnknownState");
153 void NetworkEngine::updateInterfaceData(const QString
&source
, const Solid::Control::NetworkInterface
*iface
)
156 Solid::Control::IPv4Config network
= iface
->ipV4Config();
159 if (network
.isValid()) {
160 setData(source
, I18N_NOOP("Broadcast"), network
.broadcast());
162 removeData(source
, I18N_NOOP("Broadcast"));
166 QList
<Solid::Control::IPv4Address
> addresses
= network
.addresses();
167 if (addresses
.isEmpty()) {
168 removeData(source
, I18N_NOOP("Gateway"));
169 removeData(source
, I18N_NOOP("IP"));
170 removeData(source
, I18N_NOOP("Subnet mask"));
172 // FIXME: assumes there is only one network for ethernet
173 Solid::Control::IPv4Address address
= addresses
[0];
174 setData(source
, I18N_NOOP("Gateway"), address
.gateway());
175 setData(source
, I18N_NOOP("IP"), address
.address());
176 setData(source
, I18N_NOOP("Subnet mask"), address
.netMask());
180 void NetworkEngine::updateWirelessData(const QString
&source
, const Solid::Control::WirelessNetworkInterface
*iface
)
183 QString currentAP
= iface
->activeAccessPoint();
185 using namespace Solid::Control
;
186 AccessPoint
*ap
= iface
->findAccessPoint(currentAP
);
187 WirelessNetworkInterface::OperationMode
mode(WirelessNetworkInterface::Unassociated
);
188 WirelessNetworkInterface::Capabilities
capabilities(WirelessNetworkInterface::NoCapability
);
191 setData(source
, I18N_NOOP("Link quality"), ap
->signalStrength());
192 setData(source
, I18N_NOOP("Frequency"), ap
->frequency());
193 setData(source
, I18N_NOOP("ESSID"), ap
->ssid());
194 setData(source
, I18N_NOOP("Bitrate"), ap
->maxBitRate());
195 setData(source
, I18N_NOOP("Accesspoint"), ap
->hardwareAddress());
198 setData(source
, I18N_NOOP("Accesspoint"), i18n("None"));
199 removeData(source
, I18N_NOOP("Link quality"));
200 removeData(source
, I18N_NOOP("Frequency"));
201 removeData(source
, I18N_NOOP("ESSID"));
202 removeData(source
, I18N_NOOP("Bitrate"));
206 case WirelessNetworkInterface::Unassociated
:
207 setData(source
, I18N_NOOP("Mode"), "Unassociated");
209 case WirelessNetworkInterface::Adhoc
:
210 setData(source
, I18N_NOOP("Mode"), "Adhoc");
212 case WirelessNetworkInterface::Managed
:
213 setData(source
, I18N_NOOP("Mode"), "Managed");
215 case WirelessNetworkInterface::Master
:
216 setData(source
, I18N_NOOP("Mode"), "Master");
218 case WirelessNetworkInterface::Repeater
:
219 setData(source
, I18N_NOOP("Mode"), "Repeater");
222 setData(source
, I18N_NOOP("Mode"), i18n("Unknown"));
226 setData(source
, I18N_NOOP("Encryption"),
227 (capabilities
& Solid::Control::AccessPoint::Privacy
) != 0);
230 #include "networkengine.moc"