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

SolidModules

manager.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2008 Will Stephenson <wstephenson@kde.org>
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of
00006 the License or (at your option) version 3 or any later version
00007 accepted by the membership of KDE e.V. (or its successor approved
00008 by the membership of KDE e.V.), which shall act as a proxy 
00009 defined in Section 14 of version 3 of the license.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #include "manager.h"
00021 #include "manager_p.h"
00022 
00023 #include <KDebug>
00024 #include <NetworkManager.h>
00025 
00026 #include "dbus/nm-deviceinterface.h"
00027 #include "networkmanagerdefinitions.h"
00028 #include "wirednetworkinterface.h"
00029 #include "wirelessnetworkinterface.h"
00030 #include "networkgsminterface.h"
00031 #include "networkcdmainterface.h"
00032 
00033 const QString NMNetworkManager::DBUS_SERVICE(QString::fromLatin1("org.freedesktop.NetworkManager"));
00034 const QString NMNetworkManager::DBUS_DAEMON_PATH(QString::fromLatin1("/org/freedesktop/NetworkManager"));
00035 const QString NMNetworkManager::DBUS_USER_SETTINGS_PATH(QString::fromLatin1("org.freedesktop.NetworkManagerUserSettings"));
00036 const QString NMNetworkManager::DBUS_SYSTEM_SETTINGS_PATH(QString::fromLatin1("org.freedesktop.NetworkManagerSystemSettings"));
00037 
00038 
00039 NMNetworkManagerPrivate::NMNetworkManagerPrivate() : iface(NMNetworkManager::DBUS_SERVICE, "/org/freedesktop/NetworkManager", QDBusConnection::systemBus())
00040 {
00041     kDebug(1441) << NMNetworkManager::DBUS_SERVICE;
00042 }
00043 
00044 NMNetworkManager::NMNetworkManager(QObject * parent, const QStringList &) 
00045 {
00046     qDBusRegisterMetaType<QList<QDBusObjectPath> >();
00047     d_ptr = new NMNetworkManagerPrivate;
00048     Q_D(NMNetworkManager);
00049     d->nmState = d->iface.state();
00050     d->isWirelessHardwareEnabled = d->iface.wirelessHardwareEnabled();
00051     d->isWirelessEnabled = d->iface.wirelessEnabled();
00052     connect( &d->iface, SIGNAL(DeviceAdded(const QDBusObjectPath &)),
00053                 this, SLOT(deviceAdded(const QDBusObjectPath &)));
00054     connect( &d->iface, SIGNAL(DeviceRemoved(const QDBusObjectPath &)),
00055                 this, SLOT(deviceRemoved(const QDBusObjectPath &)));
00056     connect( &d->iface, SIGNAL(PropertiesChanged(const QVariantMap &)),
00057                 this, SLOT(propertiesChanged(const QVariantMap &)));
00058     connect( &d->iface, SIGNAL(StateChanged(uint)),
00059                 this, SLOT(stateChanged(uint)));
00060 
00061     d->iface.connection().connect(QLatin1String("org.freedesktop.DBus"),
00062             QLatin1String("/org/freedesktop/DBus"), QLatin1String("org.freedesktop.DBus"),
00063             QLatin1String("NameOwnerChanged"), QLatin1String("sss"),
00064             this, SLOT(nameOwnerChanged(QString,QString,QString)));
00065 
00066     QDBusReply< QList <QDBusObjectPath> > deviceList = d->iface.GetDevices();
00067     if (deviceList.isValid())
00068     {
00069         kDebug(1441) << "Device list";
00070         QList <QDBusObjectPath> devices = deviceList.value();
00071         foreach (QDBusObjectPath op, devices)
00072         {
00073             d->networkInterfaces.append(op.path());
00074             kDebug(1441) << "  " << op.path();
00075         }
00076     }
00077     else
00078         kDebug(1441) << "Error getting device list: " << deviceList.error().name() << ": " << deviceList.error().message();
00079 
00080     kDebug(1441) << "Active connections:";
00081     QList <QDBusObjectPath> activeConnections = d->iface.activeConnections();
00082     foreach (QDBusObjectPath ac, activeConnections)
00083     {
00084         d->activeConnections.append(ac.path());
00085         kDebug(1441) << "  " << ac.path();
00086     }
00087 }
00088 
00089 NMNetworkManager::~NMNetworkManager()
00090 {
00091     delete d_ptr;
00092 }
00093 
00094 Solid::Networking::Status NMNetworkManager::status() const
00095 {
00096     Q_D(const NMNetworkManager);
00097     return convertNMState(d->nmState);
00098 }
00099 
00100 QStringList NMNetworkManager::networkInterfaces() const
00101 {
00102     Q_D(const NMNetworkManager);
00103     return d->networkInterfaces;
00104 }
00105 
00106 QObject *NMNetworkManager::createNetworkInterface(const QString &uni)
00107 {
00108     kDebug(1441);
00109     OrgFreedesktopNetworkManagerDeviceInterface devIface(NMNetworkManager::DBUS_SERVICE, uni, QDBusConnection::systemBus());
00110     uint deviceType = devIface.deviceType();
00111     NMNetworkInterface * createdInterface = 0;
00112     switch ( deviceType ) {
00113         case DEVICE_TYPE_802_3_ETHERNET:
00114             createdInterface = new NMWiredNetworkInterface(uni, this, 0); // these are deleted by the frontend manager
00115             break;
00116         case DEVICE_TYPE_802_11_WIRELESS:
00117             createdInterface = new NMWirelessNetworkInterface(uni, this, 0);
00118             break;
00119         case DEVICE_TYPE_GSM:
00120             createdInterface = new NMGsmNetworkInterface(uni, this, 0);
00121             break;
00122         case DEVICE_TYPE_CDMA:
00123             createdInterface = new NMCdmaNetworkInterface(uni, this, 0);
00124             break;
00125             /*
00126         case DEVICE_TYPE_SERIAL:
00127             createdInterface = new NMSerialNetworkInterface(uni, this, 0);
00128             break;
00129             */
00130         default:
00131             kDebug(1441) << "Can't create object of type " << deviceType;
00132             break;
00133     }
00134 
00135     return createdInterface;
00136 }
00137 
00138 bool NMNetworkManager::isNetworkingEnabled() const
00139 {
00140     Q_D(const NMNetworkManager);
00141     return !(NM_STATE_UNKNOWN == d->nmState || NM_STATE_ASLEEP == d->nmState);
00142 }
00143 
00144 bool NMNetworkManager::isWirelessEnabled() const
00145 {
00146     Q_D(const NMNetworkManager);
00147     return d->isWirelessEnabled;
00148 }
00149 
00150 bool NMNetworkManager::isWirelessHardwareEnabled() const
00151 {
00152     Q_D(const NMNetworkManager);
00153     return d->isWirelessHardwareEnabled;
00154 }
00155 
00156 void NMNetworkManager::activateConnection(const QString & interfaceUni, const QString & connectionUni, const QVariantMap & connectionParameters)
00157 {
00158     Q_D(NMNetworkManager);
00159     QString serviceName = connectionUni.section(' ', 0, 0);
00160     QString connectionPath = connectionUni.section(' ', 1, 1);
00161     // ### FIXME find a better name for the parameter needed for NM 0.7
00162     QString extra_connection_parameter = connectionParameters.value("extra_connection_parameter").toString();
00163     if (extra_connection_parameter.isEmpty()) {
00164         extra_connection_parameter = QLatin1String("/foo");
00165     }
00166     if ( serviceName.isEmpty() || connectionPath.isEmpty() ) {
00167         return;
00168     }
00169     // TODO store error code
00170     QDBusObjectPath connPath(connectionPath);
00171     QDBusObjectPath interfacePath(interfaceUni);
00172     kDebug(1441) << "Activating connection" << connPath.path() << "on service" << serviceName << "on interface" << interfacePath.path() << "with extra" << extra_connection_parameter;
00173     d->iface.ActivateConnection(serviceName, connPath, interfacePath, QDBusObjectPath(extra_connection_parameter));
00174 }
00175 
00176 void NMNetworkManager::deactivateConnection( const QString & activeConnectionPath )
00177 {
00178     Q_D(NMNetworkManager);
00179     d->iface.DeactivateConnection(QDBusObjectPath(activeConnectionPath));
00180 }
00181 
00182 void NMNetworkManager::setNetworkingEnabled(bool enabled)
00183 {
00184     Q_D(NMNetworkManager);
00185     d->iface.Sleep(!enabled);
00186 }
00187 
00188 void NMNetworkManager::setWirelessEnabled(bool enabled)
00189 {
00190     Q_D(NMNetworkManager);
00191     d->iface.setWirelessEnabled(enabled);
00192 }
00193 
00194 void NMNetworkManager::deviceAdded(const QDBusObjectPath & objpath)
00195 {
00196     kDebug(1441);
00197     Q_D(NMNetworkManager);
00198     d->networkInterfaces.append(objpath.path());
00199     emit networkInterfaceAdded(objpath.path());
00200 }
00201 
00202 void NMNetworkManager::deviceRemoved(const QDBusObjectPath & objpath)
00203 {
00204     kDebug(1441);
00205     Q_D(NMNetworkManager);
00206     d->networkInterfaces.removeAll(objpath.path());
00207     emit networkInterfaceRemoved(objpath.path());
00208 }
00209 
00210 void NMNetworkManager::stateChanged(uint state)
00211 {
00212     Q_D(NMNetworkManager);
00213     if ( d->nmState != state ) {
00214         d->nmState = state;
00215         emit statusChanged( convertNMState( state ) );
00216     }
00217 }
00218 
00219 void NMNetworkManager::propertiesChanged(const QVariantMap &properties)
00220 {
00221     Q_D(NMNetworkManager);
00222     kDebug(1441) << properties.keys();
00223     QLatin1String activeConnKey("ActiveConnections");
00224     QLatin1String wifiHwKey("WirelessHardwareEnabled");
00225     QLatin1String wifiEnabledKey("WirelessEnabled");
00226     QVariantMap::const_iterator it = properties.find(activeConnKey);
00227     if ( it != properties.end()) {
00228         QList<QDBusObjectPath> activePaths = qdbus_cast< QList<QDBusObjectPath> >(*it);
00229         d->activeConnections.clear();
00230         if ( activePaths.count() ) {
00231             kDebug(1441) << activeConnKey;
00232         }
00233         foreach (QDBusObjectPath ac, activePaths)
00234         {
00235             d->activeConnections.append(ac.path());
00236             kDebug(1441) << "  " << ac.path();
00237         }
00238         emit activeConnectionsChanged();
00239     }
00240     it = properties.find(wifiHwKey);
00241     if ( it != properties.end()) {
00242         d->isWirelessHardwareEnabled = it->toBool();
00243         kDebug(1441) << wifiHwKey << d->isWirelessHardwareEnabled;
00244     }
00245     it = properties.find(wifiEnabledKey);
00246     if ( it != properties.end()) {
00247         d->isWirelessEnabled = it->toBool();
00248         kDebug(1441) << wifiEnabledKey << d->isWirelessEnabled;
00249         emit wirelessEnabledChanged(d->isWirelessEnabled);
00250     }
00251 }
00252 
00253 Solid::Networking::Status NMNetworkManager::convertNMState(uint state)
00254 {
00255     Solid::Networking::Status status = Solid::Networking::Unknown;
00256     switch (state) {
00257         case NM_STATE_UNKNOWN:
00258         case NM_STATE_ASLEEP:
00259             break;
00260         case NM_STATE_CONNECTING:
00261             status = Solid::Networking::Connecting;
00262             break;
00263         case NM_STATE_CONNECTED:
00264             status = Solid::Networking::Connected;
00265             break;
00266         case NM_STATE_DISCONNECTED:
00267             status = Solid::Networking::Unconnected;
00268             break;
00269     }
00270     return status;
00271 }
00272 
00273 void NMNetworkManager::nameOwnerChanged(QString name, QString oldOwner, QString newOwner)
00274 {
00275     if ( name == QLatin1String("org.freedesktop.NetworkManager") ) {
00276         kDebug(1441) << "name: " << name << ", old owner: " << oldOwner << ", new owner: " << newOwner;
00277         if ( oldOwner.isEmpty() && !newOwner.isEmpty() ) {
00278             // NetworkManager started, but we are already listening to StateChanged so we should get
00279             // its status that way
00280             ;
00281         }
00282         if ( !oldOwner.isEmpty() && newOwner.isEmpty() ) {
00283             // NetworkManager stopped, set status Unknown for safety
00284             stateChanged(NM_STATE_UNKNOWN);
00285         }
00286     }
00287 }
00288 
00289 QStringList NMNetworkManager::activeConnections() const
00290 {
00291     Q_D(const NMNetworkManager);
00292     return d->activeConnections;
00293 }
00294 
00295 #include "manager.moc"
00296 

SolidModules

Skip menu "SolidModules"
  • 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