not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / solid / networkmanager-0.7 / manager.cpp
blob3c5434022a310c680cf64333a2e202b9302a97fc
1 /*
2 Copyright 2008 Will Stephenson <wstephenson@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of
6 the License or (at your option) version 3 or any later version
7 accepted by the membership of KDE e.V. (or its successor approved
8 by the membership of KDE e.V.), which shall act as a proxy
9 defined in Section 14 of version 3 of the license.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "manager.h"
21 #include "manager_p.h"
23 #include <KDebug>
24 #include <NetworkManager.h>
26 #include "dbus/nm-deviceinterface.h"
27 #include "networkmanagerdefinitions.h"
28 #include "wirednetworkinterface.h"
29 #include "wirelessnetworkinterface.h"
30 #include "networkgsminterface.h"
31 #include "networkcdmainterface.h"
33 const QString NMNetworkManager::DBUS_SERVICE(QString::fromLatin1("org.freedesktop.NetworkManager"));
34 const QString NMNetworkManager::DBUS_DAEMON_PATH(QString::fromLatin1("/org/freedesktop/NetworkManager"));
35 const QString NMNetworkManager::DBUS_USER_SETTINGS_PATH(QString::fromLatin1("org.freedesktop.NetworkManagerUserSettings"));
36 const QString NMNetworkManager::DBUS_SYSTEM_SETTINGS_PATH(QString::fromLatin1("org.freedesktop.NetworkManagerSystemSettings"));
39 NMNetworkManagerPrivate::NMNetworkManagerPrivate() : iface(NMNetworkManager::DBUS_SERVICE, "/org/freedesktop/NetworkManager", QDBusConnection::systemBus())
41 kDebug(1441) << NMNetworkManager::DBUS_SERVICE;
44 NMNetworkManager::NMNetworkManager(QObject * parent, const QStringList &)
46 qDBusRegisterMetaType<QList<QDBusObjectPath> >();
47 d_ptr = new NMNetworkManagerPrivate;
48 Q_D(NMNetworkManager);
49 d->nmState = d->iface.state();
50 d->isWirelessHardwareEnabled = d->iface.wirelessHardwareEnabled();
51 d->isWirelessEnabled = d->iface.wirelessEnabled();
52 connect( &d->iface, SIGNAL(DeviceAdded(const QDBusObjectPath &)),
53 this, SLOT(deviceAdded(const QDBusObjectPath &)));
54 connect( &d->iface, SIGNAL(DeviceRemoved(const QDBusObjectPath &)),
55 this, SLOT(deviceRemoved(const QDBusObjectPath &)));
56 connect( &d->iface, SIGNAL(PropertiesChanged(const QVariantMap &)),
57 this, SLOT(propertiesChanged(const QVariantMap &)));
58 connect( &d->iface, SIGNAL(StateChanged(uint)),
59 this, SLOT(stateChanged(uint)));
61 d->iface.connection().connect(QLatin1String("org.freedesktop.DBus"),
62 QLatin1String("/org/freedesktop/DBus"), QLatin1String("org.freedesktop.DBus"),
63 QLatin1String("NameOwnerChanged"), QLatin1String("sss"),
64 this, SLOT(nameOwnerChanged(QString,QString,QString)));
66 QDBusReply< QList <QDBusObjectPath> > deviceList = d->iface.GetDevices();
67 if (deviceList.isValid())
69 kDebug(1441) << "Device list";
70 QList <QDBusObjectPath> devices = deviceList.value();
71 foreach (QDBusObjectPath op, devices)
73 d->networkInterfaces.append(op.path());
74 kDebug(1441) << " " << op.path();
77 else
78 kDebug(1441) << "Error getting device list: " << deviceList.error().name() << ": " << deviceList.error().message();
80 kDebug(1441) << "Active connections:";
81 QList <QDBusObjectPath> activeConnections = d->iface.activeConnections();
82 foreach (QDBusObjectPath ac, activeConnections)
84 d->activeConnections.append(ac.path());
85 kDebug(1441) << " " << ac.path();
89 NMNetworkManager::~NMNetworkManager()
91 delete d_ptr;
94 Solid::Networking::Status NMNetworkManager::status() const
96 Q_D(const NMNetworkManager);
97 return convertNMState(d->nmState);
100 QStringList NMNetworkManager::networkInterfaces() const
102 Q_D(const NMNetworkManager);
103 return d->networkInterfaces;
106 QObject *NMNetworkManager::createNetworkInterface(const QString &uni)
108 kDebug(1441);
109 OrgFreedesktopNetworkManagerDeviceInterface devIface(NMNetworkManager::DBUS_SERVICE, uni, QDBusConnection::systemBus());
110 uint deviceType = devIface.deviceType();
111 NMNetworkInterface * createdInterface = 0;
112 switch ( deviceType ) {
113 case DEVICE_TYPE_802_3_ETHERNET:
114 createdInterface = new NMWiredNetworkInterface(uni, this, 0); // these are deleted by the frontend manager
115 break;
116 case DEVICE_TYPE_802_11_WIRELESS:
117 createdInterface = new NMWirelessNetworkInterface(uni, this, 0);
118 break;
119 case DEVICE_TYPE_GSM:
120 createdInterface = new NMGsmNetworkInterface(uni, this, 0);
121 break;
122 case DEVICE_TYPE_CDMA:
123 createdInterface = new NMCdmaNetworkInterface(uni, this, 0);
124 break;
126 case DEVICE_TYPE_SERIAL:
127 createdInterface = new NMSerialNetworkInterface(uni, this, 0);
128 break;
130 default:
131 kDebug(1441) << "Can't create object of type " << deviceType;
132 break;
135 return createdInterface;
138 bool NMNetworkManager::isNetworkingEnabled() const
140 Q_D(const NMNetworkManager);
141 return !(NM_STATE_UNKNOWN == d->nmState || NM_STATE_ASLEEP == d->nmState);
144 bool NMNetworkManager::isWirelessEnabled() const
146 Q_D(const NMNetworkManager);
147 return d->isWirelessEnabled;
150 bool NMNetworkManager::isWirelessHardwareEnabled() const
152 Q_D(const NMNetworkManager);
153 return d->isWirelessHardwareEnabled;
156 void NMNetworkManager::activateConnection(const QString & interfaceUni, const QString & connectionUni, const QVariantMap & connectionParameters)
158 Q_D(NMNetworkManager);
159 QString serviceName = connectionUni.section(' ', 0, 0);
160 QString connectionPath = connectionUni.section(' ', 1, 1);
161 // ### FIXME find a better name for the parameter needed for NM 0.7
162 QString extra_connection_parameter = connectionParameters.value("extra_connection_parameter").toString();
163 if (extra_connection_parameter.isEmpty()) {
164 extra_connection_parameter = QLatin1String("/foo");
166 if ( serviceName.isEmpty() || connectionPath.isEmpty() ) {
167 return;
169 // TODO store error code
170 QDBusObjectPath connPath(connectionPath);
171 QDBusObjectPath interfacePath(interfaceUni);
172 kDebug(1441) << "Activating connection" << connPath.path() << "on service" << serviceName << "on interface" << interfacePath.path() << "with extra" << extra_connection_parameter;
173 d->iface.ActivateConnection(serviceName, connPath, interfacePath, QDBusObjectPath(extra_connection_parameter));
176 void NMNetworkManager::deactivateConnection( const QString & activeConnectionPath )
178 Q_D(NMNetworkManager);
179 d->iface.DeactivateConnection(QDBusObjectPath(activeConnectionPath));
182 void NMNetworkManager::setNetworkingEnabled(bool enabled)
184 Q_D(NMNetworkManager);
185 d->iface.Sleep(!enabled);
188 void NMNetworkManager::setWirelessEnabled(bool enabled)
190 Q_D(NMNetworkManager);
191 d->iface.setWirelessEnabled(enabled);
194 void NMNetworkManager::deviceAdded(const QDBusObjectPath & objpath)
196 kDebug(1441);
197 Q_D(NMNetworkManager);
198 d->networkInterfaces.append(objpath.path());
199 emit networkInterfaceAdded(objpath.path());
202 void NMNetworkManager::deviceRemoved(const QDBusObjectPath & objpath)
204 kDebug(1441);
205 Q_D(NMNetworkManager);
206 d->networkInterfaces.removeAll(objpath.path());
207 emit networkInterfaceRemoved(objpath.path());
210 void NMNetworkManager::stateChanged(uint state)
212 Q_D(NMNetworkManager);
213 if ( d->nmState != state ) {
214 d->nmState = state;
215 emit statusChanged( convertNMState( state ) );
219 void NMNetworkManager::propertiesChanged(const QVariantMap &properties)
221 Q_D(NMNetworkManager);
222 kDebug(1441) << properties.keys();
223 QLatin1String activeConnKey("ActiveConnections");
224 QLatin1String wifiHwKey("WirelessHardwareEnabled");
225 QLatin1String wifiEnabledKey("WirelessEnabled");
226 QVariantMap::const_iterator it = properties.find(activeConnKey);
227 if ( it != properties.end()) {
228 QList<QDBusObjectPath> activePaths = qdbus_cast< QList<QDBusObjectPath> >(*it);
229 d->activeConnections.clear();
230 if ( activePaths.count() ) {
231 kDebug(1441) << activeConnKey;
233 foreach (QDBusObjectPath ac, activePaths)
235 d->activeConnections.append(ac.path());
236 kDebug(1441) << " " << ac.path();
238 emit activeConnectionsChanged();
240 it = properties.find(wifiHwKey);
241 if ( it != properties.end()) {
242 d->isWirelessHardwareEnabled = it->toBool();
243 kDebug(1441) << wifiHwKey << d->isWirelessHardwareEnabled;
245 it = properties.find(wifiEnabledKey);
246 if ( it != properties.end()) {
247 d->isWirelessEnabled = it->toBool();
248 kDebug(1441) << wifiEnabledKey << d->isWirelessEnabled;
249 emit wirelessEnabledChanged(d->isWirelessEnabled);
253 Solid::Networking::Status NMNetworkManager::convertNMState(uint state)
255 Solid::Networking::Status status = Solid::Networking::Unknown;
256 switch (state) {
257 case NM_STATE_UNKNOWN:
258 case NM_STATE_ASLEEP:
259 break;
260 case NM_STATE_CONNECTING:
261 status = Solid::Networking::Connecting;
262 break;
263 case NM_STATE_CONNECTED:
264 status = Solid::Networking::Connected;
265 break;
266 case NM_STATE_DISCONNECTED:
267 status = Solid::Networking::Unconnected;
268 break;
270 return status;
273 void NMNetworkManager::nameOwnerChanged(QString name, QString oldOwner, QString newOwner)
275 if ( name == QLatin1String("org.freedesktop.NetworkManager") ) {
276 kDebug(1441) << "name: " << name << ", old owner: " << oldOwner << ", new owner: " << newOwner;
277 if ( oldOwner.isEmpty() && !newOwner.isEmpty() ) {
278 // NetworkManager started, but we are already listening to StateChanged so we should get
279 // its status that way
282 if ( !oldOwner.isEmpty() && newOwner.isEmpty() ) {
283 // NetworkManager stopped, set status Unknown for safety
284 stateChanged(NM_STATE_UNKNOWN);
289 QStringList NMNetworkManager::activeConnections() const
291 Q_D(const NMNetworkManager);
292 return d->activeConnections;
295 #include "manager.moc"