delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / workspace / plasma / dataengines / network / networkengine.cpp
blob2d4b2547b13202f6cd7babe0cc42e110cef7a4db
1 /*
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"
21 #include <stdio.h>
22 #include <string.h>
24 #include <QMap>
25 #include <QDir>
26 #include <QTimer>
27 #include <QRegExp>
28 #include <QDateTime>
29 #include <QStringList>
30 #include <QNetworkAddressEntry>
32 #include <KDebug>
33 #include <KLocale>
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)
46 Q_UNUSED(args)
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);
74 return true;
77 return false;
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);
84 if (!iface) {
85 // remove the source as the interface is no longer available
86 removeSource(source);
87 return false;
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;
95 if (isWireless) {
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);
111 if (isWireless) {
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");
124 break;
125 case Solid::Control::NetworkInterface::Configuring:
126 setData(source, I18N_NOOP("ConnectionState"), "Configuring");
127 break;
128 case Solid::Control::NetworkInterface::NeedAuth:
129 setData(source, I18N_NOOP("ConnectionState"), "NeedAuth");
130 break;
131 case Solid::Control::NetworkInterface::IPConfig:
132 setData(source, I18N_NOOP("ConnectionState"), "IPConfig");
133 break;
134 case Solid::Control::NetworkInterface::Activated:
135 setData(source, I18N_NOOP("ConnectionState"), "Activated");
136 break;
137 case Solid::Control::NetworkInterface::Failed:
138 setData(source, I18N_NOOP("ConnectionState"), "Failed");
139 break;
140 case Solid::Control::NetworkInterface::Unmanaged:
141 setData(source, I18N_NOOP("ConnectionState"), "Unmanaged");
142 break;
143 case Solid::Control::NetworkInterface::Unavailable:
144 setData(source, I18N_NOOP("ConnectionState"), "Unavailable");
145 break;
146 default:
147 setData(source, I18N_NOOP("ConnectionState"), "UnknownState");
148 break;
150 return true;
153 void NetworkEngine::updateInterfaceData(const QString &source, const Solid::Control::NetworkInterface *iface)
155 Q_ASSERT(iface);
156 Solid::Control::IPv4Config network = iface->ipV4Config();
158 #if 0
159 if (network.isValid()) {
160 setData(source, I18N_NOOP("Broadcast"), network.broadcast());
161 } else {
162 removeData(source, I18N_NOOP("Broadcast"));
164 #endif
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"));
171 } else {
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)
182 Q_ASSERT(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);
190 if (ap) {
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());
196 mode = ap->mode();
197 } else {
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"));
205 switch (mode) {
206 case WirelessNetworkInterface::Unassociated:
207 setData(source, I18N_NOOP("Mode"), "Unassociated");
208 break;
209 case WirelessNetworkInterface::Adhoc:
210 setData(source, I18N_NOOP("Mode"), "Adhoc");
211 break;
212 case WirelessNetworkInterface::Managed:
213 setData(source, I18N_NOOP("Mode"), "Managed");
214 break;
215 case WirelessNetworkInterface::Master:
216 setData(source, I18N_NOOP("Mode"), "Master");
217 break;
218 case WirelessNetworkInterface::Repeater:
219 setData(source, I18N_NOOP("Mode"), "Repeater");
220 break;
221 default:
222 setData(source, I18N_NOOP("Mode"), i18n("Unknown"));
223 break;
226 setData(source, I18N_NOOP("Encryption"),
227 (capabilities & Solid::Control::AccessPoint::Privacy) != 0);
230 #include "networkengine.moc"