not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / solid / control / networkmanager.cpp
blob3869bf8a3e74a36a77118090ed3d8501413d70bc
1 /* This file is part of the KDE project
2 Copyright (C) 2006 Will Stephenson <wstephenson@kde.org>
3 Copyright (C) 2006-2007 Kevin Ottens <ervin@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include "ifaces/networkmanager.h"
22 #include "ifaces/networkinterface.h"
23 #include "ifaces/wirednetworkinterface.h"
24 #include "ifaces/wirelessnetworkinterface.h"
25 #include "ifaces/networkgsminterface.h"
26 #include "ifaces/networkcdmainterface.h"
28 #include "soliddefs_p.h"
29 #include "networkmanager_p.h"
30 #include "networkinterface.h"
31 #include "wirednetworkinterface.h"
32 #include "wirelessnetworkinterface.h"
34 #include "networkmanager.h"
36 #include <kglobal.h>
38 #include <kdebug.h>
40 K_GLOBAL_STATIC(Solid::Control::NetworkManagerPrivate, globalNetworkManager)
42 Solid::Control::NetworkManagerPrivate::NetworkManagerPrivate() : m_invalidDevice(0)
44 loadBackend("Network Management",
45 "SolidNetworkManager",
46 "Solid::Control::Ifaces::NetworkManager");
48 if (managerBackend()!=0) {
49 connect(managerBackend(), SIGNAL(networkInterfaceAdded(const QString &)),
50 this, SLOT(_k_networkInterfaceAdded(const QString &)));
51 connect(managerBackend(), SIGNAL(networkInterfaceRemoved(const QString &)),
52 this, SLOT(_k_networkInterfaceRemoved(const QString &)));
53 connect(managerBackend(), SIGNAL(statusChanged(Solid::Networking::Status)),
54 this, SIGNAL(statusChanged(Solid::Networking::Status)));
55 connect(managerBackend(), SIGNAL(wirelessEnabledChanged(bool)),
56 this, SIGNAL(wirelessEnabledChanged(bool)));
57 connect(managerBackend(), SIGNAL(wirelessHardwareEnabledChanged(bool)),
58 this, SIGNAL(wirelessHardwareEnabledChanged(bool)));
59 connect(managerBackend(), SIGNAL(activeConnectionsChanged()),
60 this, SIGNAL(activeConnectionsChanged()));
64 Solid::Control::NetworkManagerPrivate::~NetworkManagerPrivate()
66 // Delete all the devices, they are now outdated
67 typedef QPair<NetworkInterface *, QObject *> NetworkInterfaceIfacePair;
69 foreach (const NetworkInterfaceIfacePair &pair, m_networkInterfaceMap) {
70 delete pair.first;
71 delete pair.second;
74 m_networkInterfaceMap.clear();
77 Solid::Control::NetworkInterfaceList Solid::Control::NetworkManagerPrivate::buildDeviceList(const QStringList &uniList)
79 NetworkInterfaceList list;
80 Ifaces::NetworkManager *backend = qobject_cast<Ifaces::NetworkManager *>(managerBackend());
82 if (backend == 0) return list;
84 foreach (const QString &uni, uniList)
86 QPair<NetworkInterface *, QObject *> pair = findRegisteredNetworkInterface(uni);
88 if (pair.first!= 0)
90 list.append(pair.first);
94 return list;
97 Solid::Control::NetworkInterfaceList Solid::Control::NetworkManagerPrivate::networkInterfaces()
99 Ifaces::NetworkManager *backend = qobject_cast<Ifaces::NetworkManager *>(managerBackend());
101 if (backend!= 0)
103 return buildDeviceList(backend->networkInterfaces());
105 else
107 return NetworkInterfaceList();
111 Solid::Control::NetworkInterfaceList Solid::Control::NetworkManager::networkInterfaces()
113 return globalNetworkManager->networkInterfaces();
116 bool Solid::Control::NetworkManager::isNetworkingEnabled()
118 return_SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), false, isNetworkingEnabled());
121 bool Solid::Control::NetworkManager::isWirelessEnabled()
123 return_SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), false, isWirelessEnabled());
126 bool Solid::Control::NetworkManager::isWirelessHardwareEnabled()
128 return_SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), false, isWirelessHardwareEnabled());
131 void Solid::Control::NetworkManager::setNetworkingEnabled(bool enabled)
133 SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), setNetworkingEnabled(enabled));
136 void Solid::Control::NetworkManager::setWirelessEnabled(bool enabled)
138 SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), setWirelessEnabled(enabled));
141 Solid::Networking::Status Solid::Control::NetworkManager::status()
143 return_SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), Solid::Networking::Unknown, status());
146 Solid::Control::NetworkInterface * Solid::Control::NetworkManagerPrivate::findNetworkInterface(const QString &uni)
148 Ifaces::NetworkManager *backend = qobject_cast<Ifaces::NetworkManager *>(managerBackend());
150 if (backend == 0) return 0;
152 QPair<NetworkInterface *, QObject *> pair = findRegisteredNetworkInterface(uni);
154 if (pair.first != 0)
156 return pair.first;
158 else
160 return 0;
164 Solid::Control::NetworkInterface * Solid::Control::NetworkManager::findNetworkInterface(const QString &uni)
166 return globalNetworkManager->findNetworkInterface(uni);
169 Solid::Control::NetworkManager::Notifier * Solid::Control::NetworkManager::notifier()
171 return globalNetworkManager;
174 void Solid::Control::NetworkManagerPrivate::_k_networkInterfaceAdded(const QString &uni)
176 QPair<NetworkInterface *, QObject*> pair = m_networkInterfaceMap.take(uni);
178 if (pair.first!= 0)
180 // Oops, I'm not sure it should happen...
181 // But well in this case we'd better kill the old device we got, it's probably outdated
183 delete pair.first;
184 delete pair.second;
187 emit networkInterfaceAdded(uni);
190 void Solid::Control::NetworkManagerPrivate::_k_networkInterfaceRemoved(const QString &uni)
192 QPair<NetworkInterface *, QObject *> pair = m_networkInterfaceMap.take(uni);
194 if (pair.first!= 0)
196 delete pair.first;
197 delete pair.second;
200 emit networkInterfaceRemoved(uni);
203 void Solid::Control::NetworkManagerPrivate::_k_destroyed(QObject *object)
205 Ifaces::NetworkInterface *device = qobject_cast<Ifaces::NetworkInterface *>(object);
207 if (device!=0)
209 QString uni = device->uni();
210 QPair<NetworkInterface *, QObject *> pair = m_networkInterfaceMap.take(uni);
211 delete pair.first;
215 /***************************************************************************/
217 QPair<Solid::Control::NetworkInterface *, QObject *>
218 Solid::Control::NetworkManagerPrivate::findRegisteredNetworkInterface(const QString &uni)
220 if (m_networkInterfaceMap.contains(uni)) {
221 return m_networkInterfaceMap[uni];
222 } else {
223 Ifaces::NetworkManager *backend = qobject_cast<Ifaces::NetworkManager *>(managerBackend());
225 if (backend!=0)
227 QObject * iface = backend->createNetworkInterface(uni);
228 NetworkInterface *device = 0;
229 if (qobject_cast<Ifaces::WirelessNetworkInterface *>(iface) != 0) {
230 device = new WirelessNetworkInterface(iface);
231 } else if (qobject_cast<Ifaces::WiredNetworkInterface *>(iface) != 0) {
232 device = new WiredNetworkInterface(iface);
233 } else if (qobject_cast<Ifaces::GsmNetworkInterface *>(iface) != 0) {
234 device = new GsmNetworkInterface(iface);
235 } else if (qobject_cast<Ifaces::CdmaNetworkInterface *>(iface) != 0) {
236 device = new CdmaNetworkInterface(iface);
237 } else {
238 kDebug() << "Unhandled network interface: " << uni;
240 if (device != 0) {
241 QPair<NetworkInterface *, QObject *> pair(device, iface);
242 connect(iface, SIGNAL(destroyed(QObject *)),
243 this, SLOT(_k_destroyed(QObject *)));
244 m_networkInterfaceMap[uni] = pair;
245 return pair;
247 else
249 return QPair<NetworkInterface *, QObject *>(0, 0);
252 else
254 return QPair<NetworkInterface *, QObject *>(0, 0);
259 void Solid::Control::NetworkManager::activateConnection(const QString & interfaceUni, const QString & connectionUni,
260 const QVariantMap & connectionParameters )
262 SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), activateConnection(interfaceUni, connectionUni, connectionParameters));
265 void Solid::Control::NetworkManager::deactivateConnection(const QString & activeConnectionUni)
267 SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), deactivateConnection(activeConnectionUni));
270 QStringList Solid::Control::NetworkManager::activeConnections()
272 return_SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), QStringList(), activeConnections());
275 #include "networkmanager_p.moc"
276 #include "networkmanager.moc"