• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

networkengine.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Percy Leonhardt <percy@eris23.de>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License version 2 as
00006  *   published by the Free Software Foundation
00007  *
00008  *   This program is distributed in the hope that it will be useful,
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *   GNU General Public License for more details
00012  *
00013  *   You should have received a copy of the GNU Library General Public
00014  *   License along with this program; if not, write to the
00015  *   Free Software Foundation, Inc.,
00016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017  */
00018 
00019 #include "networkengine.h"
00020 
00021 #include <stdio.h>
00022 #include <string.h>
00023 
00024 #include <QMap>
00025 #include <QDir>
00026 #include <QTimer>
00027 #include <QRegExp>
00028 #include <QDateTime>
00029 #include <QStringList>
00030 #include <QNetworkAddressEntry>
00031 
00032 #include <KDebug>
00033 #include <KLocale>
00034 #include <kio/global.h>
00035 #include <Plasma/DataContainer>
00036 #include <solid/control/networkmanager.h>
00037 #include <solid/control/wirelessnetworkinterface.h>
00038 #include <solid/control/wirelessaccesspoint.h>
00039 
00040 //#define SYSPATH "/sys/class/net/"
00041 
00042 NetworkEngine::NetworkEngine(QObject* parent, const QVariantList& args)
00043     : Plasma::DataEngine(parent, args),
00044       m_secondsSinceLastUpdate(0)
00045 {
00046     Q_UNUSED(args)
00047     setMinimumPollingInterval(1000);
00048 }
00049 
00050 NetworkEngine::~NetworkEngine()
00051 {
00052 }
00053 
00054 QStringList NetworkEngine::sources() const
00055 {
00056     Solid::Control::NetworkInterfaceList iflist = Solid::Control::NetworkManager::networkInterfaces();
00057 
00058     QStringList availableInterfaces;
00059     foreach (Solid::Control::NetworkInterface *iface, iflist) {
00060         availableInterfaces.append(iface->uni().section('/', -1));
00061     }
00062     return availableInterfaces;
00063 }
00064 
00065 bool NetworkEngine::sourceRequestEvent(const QString &name)
00066 {
00067     Solid::Control::NetworkInterfaceList iflist = Solid::Control::NetworkManager::networkInterfaces();
00068 
00069     foreach(Solid::Control::NetworkInterface *iface, iflist) {
00070         if (name == iface->uni().section('/', -1)) {
00071             setData(name, DataEngine::Data());
00072             setData(name, I18N_NOOP("UNI"), iface->uni());
00073             updateSourceEvent(name);
00074             return true;
00075         }
00076     }
00077     return false;
00078 }
00079 
00080 bool NetworkEngine::updateSourceEvent(const QString &source)
00081 {
00082     QString uni = query(source)[I18N_NOOP("UNI")].toString();
00083     Solid::Control::NetworkInterface *iface = Solid::Control::NetworkManager::findNetworkInterface(uni);
00084     if (!iface) {
00085         // remove the source as the interface is no longer available
00086         removeSource(source);
00087         return false;
00088     }
00089 
00090     // Check if it is a wireless interface.
00091     bool isWireless = iface->type() == Solid::Control::NetworkInterface::Ieee80211;
00092     setData(source, I18N_NOOP("Wireless"), isWireless);
00093     Solid::Control::WirelessNetworkInterface *wlan = 0;
00094 
00095     if (isWireless) {
00096         wlan = static_cast<Solid::Control::WirelessNetworkInterface*>(iface);
00097 
00098         QStringList availableNetworks;
00099         Solid::Control::AccessPointList apList = wlan->accessPoints();
00100         foreach (const QString &apId, apList) {
00101             Solid::Control::AccessPoint *ap = wlan->findAccessPoint(apId);
00102             availableNetworks += ap->ssid();
00103         }
00104         setData(source, I18N_NOOP("Available networks"), availableNetworks);
00105     }
00106 
00107     Solid::Control::NetworkInterface::ConnectionState connectionState = iface->connectionState();
00108     if (connectionState == Solid::Control::NetworkInterface::Activated) {
00109         // update the interface
00110         updateInterfaceData(source, iface);
00111         if (isWireless) {
00112             updateWirelessData(source, wlan);
00113         }
00114     } else if (connectionState != Solid::Control::NetworkInterface::Activated &&
00115                query(source)[I18N_NOOP("ConnectionState")].toString() == I18N_NOOP("Activated")) {
00116         // the interface was disconnected
00117         removeAllData(source);
00118         setData(source, I18N_NOOP("UNI"), uni);
00119     }
00120 
00121     switch (connectionState) {
00122     case Solid::Control::NetworkInterface::Preparing:
00123         setData(source, I18N_NOOP("ConnectionState"), "Preparing");
00124         break;
00125     case Solid::Control::NetworkInterface::Configuring:
00126         setData(source, I18N_NOOP("ConnectionState"), "Configuring");
00127         break;
00128     case Solid::Control::NetworkInterface::NeedAuth:
00129         setData(source, I18N_NOOP("ConnectionState"), "NeedAuth");
00130         break;
00131     case Solid::Control::NetworkInterface::IPConfig:
00132         setData(source, I18N_NOOP("ConnectionState"), "IPConfig");
00133         break;
00134     case Solid::Control::NetworkInterface::Activated:
00135         setData(source, I18N_NOOP("ConnectionState"), "Activated");
00136         break;
00137     case Solid::Control::NetworkInterface::Failed:
00138         setData(source, I18N_NOOP("ConnectionState"), "Failed");
00139         break;
00140     case Solid::Control::NetworkInterface::Unmanaged:
00141         setData(source, I18N_NOOP("ConnectionState"), "Unmanaged");
00142         break;
00143     case Solid::Control::NetworkInterface::Unavailable:
00144         setData(source, I18N_NOOP("ConnectionState"), "Unavailable");
00145         break;
00146     default:
00147         setData(source, I18N_NOOP("ConnectionState"), "UnknownState");
00148         break;
00149     }
00150     return true;
00151 }
00152 
00153 void NetworkEngine::updateInterfaceData(const QString &source, const Solid::Control::NetworkInterface *iface)
00154 {
00155     Q_ASSERT(iface);
00156     Solid::Control::IPv4Config network = iface->ipV4Config();
00157 
00158 #if 0
00159     if (network.isValid()) {
00160         setData(source, I18N_NOOP("Broadcast"), network.broadcast());
00161     } else {
00162         removeData(source, I18N_NOOP("Broadcast"));
00163     }
00164 #endif
00165 
00166     QList<Solid::Control::IPv4Address> addresses = network.addresses();
00167     if (addresses.isEmpty()) {
00168         removeData(source, I18N_NOOP("Gateway"));
00169         removeData(source, I18N_NOOP("IP"));
00170         removeData(source, I18N_NOOP("Subnet mask"));
00171     } else {
00172         // FIXME: assumes there is only one network for ethernet
00173         Solid::Control::IPv4Address address = addresses[0];
00174         setData(source, I18N_NOOP("Gateway"), address.gateway());
00175         setData(source, I18N_NOOP("IP"), address.address());
00176         setData(source, I18N_NOOP("Subnet mask"), address.netMask());
00177     }
00178 }
00179 
00180 void NetworkEngine::updateWirelessData(const QString &source, const Solid::Control::WirelessNetworkInterface *iface)
00181 {
00182     Q_ASSERT(iface);
00183     QString currentAP = iface->activeAccessPoint();
00184 
00185     using namespace Solid::Control;
00186     AccessPoint *ap = iface->findAccessPoint(currentAP);
00187     WirelessNetworkInterface::OperationMode mode(WirelessNetworkInterface::Unassociated);
00188     WirelessNetworkInterface::Capabilities capabilities(WirelessNetworkInterface::NoCapability);
00189 
00190     if (ap) {
00191         setData(source, I18N_NOOP("Link quality"), ap->signalStrength());
00192         setData(source, I18N_NOOP("Frequency"), ap->frequency());
00193         setData(source, I18N_NOOP("ESSID"), ap->ssid());
00194         setData(source, I18N_NOOP("Bitrate"), ap->maxBitRate());
00195         setData(source, I18N_NOOP("Accesspoint"), ap->hardwareAddress());
00196         mode = ap->mode();
00197     } else {
00198         setData(source, I18N_NOOP("Accesspoint"), i18n("None"));
00199         removeData(source, I18N_NOOP("Link quality"));
00200         removeData(source, I18N_NOOP("Frequency"));
00201         removeData(source, I18N_NOOP("ESSID"));
00202         removeData(source, I18N_NOOP("Bitrate"));
00203     }
00204 
00205     switch (mode) {
00206     case WirelessNetworkInterface::Unassociated:
00207         setData(source, I18N_NOOP("Mode"), "Unassociated");
00208         break;
00209     case WirelessNetworkInterface::Adhoc:
00210         setData(source, I18N_NOOP("Mode"), "Adhoc");
00211         break;
00212     case WirelessNetworkInterface::Managed:
00213         setData(source, I18N_NOOP("Mode"), "Managed");
00214         break;
00215     case WirelessNetworkInterface::Master:
00216         setData(source, I18N_NOOP("Mode"), "Master");
00217         break;
00218     case WirelessNetworkInterface::Repeater:
00219         setData(source, I18N_NOOP("Mode"), "Repeater");
00220         break;
00221     default:
00222         setData(source, I18N_NOOP("Mode"), i18n("Unknown"));
00223         break;
00224     }
00225 
00226     setData(source, I18N_NOOP("Encryption"),
00227             (capabilities & Solid::Control::AccessPoint::Privacy) != 0);
00228 }
00229 
00230 #include "networkengine.moc"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal