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

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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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