Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / libs / solid / control / networkmanager.cpp
blob5f0a49d6fd1f57c1ed685c9126097be099e96bc4
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"
24 #include "soliddefs_p.h"
25 #include "networkmanager_p.h"
26 #include "networkinterface.h"
28 #include "networkmanager.h"
30 #include <kglobal.h>
32 K_GLOBAL_STATIC(Solid::Control::NetworkManagerPrivate, globalNetworkManager)
34 Solid::Control::NetworkManagerPrivate::NetworkManagerPrivate()
36 loadBackend("Network Management",
37 "SolidNetworkManager",
38 "Solid::Control::Ifaces::NetworkManager");
40 if (managerBackend()!=0) {
41 connect(managerBackend(), SIGNAL(networkInterfaceAdded(const QString &)),
42 this, SLOT(_k_networkInterfaceAdded(const QString &)));
43 connect(managerBackend(), SIGNAL(networkInterfaceRemoved(const QString &)),
44 this, SLOT(_k_networkInterfaceRemoved(const QString &)));
45 connect(managerBackend(), SIGNAL(statusChanged(Solid::Networking::Status)),
46 this, SIGNAL(statusChanged(Solid::Networking::Status)));
50 Solid::Control::NetworkManagerPrivate::~NetworkManagerPrivate()
52 // Delete all the devices, they are now outdated
53 typedef QPair<NetworkInterface *, Ifaces::NetworkInterface *> NetworkInterfaceIfacePair;
55 // Delete all the devices, they are now outdated
56 foreach (const NetworkInterfaceIfacePair &pair, m_networkInterfaceMap.values()) {
57 delete pair.first;
58 delete pair.second;
61 m_networkInterfaceMap.clear();
64 Solid::Control::NetworkInterfaceList Solid::Control::NetworkManagerPrivate::buildDeviceList(const QStringList &uniList)
66 NetworkInterfaceList list;
67 Ifaces::NetworkManager *backend = qobject_cast<Ifaces::NetworkManager *>(managerBackend());
69 if (backend == 0) return list;
71 foreach (const QString &uni, uniList)
73 QPair<NetworkInterface *, Ifaces::NetworkInterface *> pair = findRegisteredNetworkInterface(uni);
75 if (pair.first!= 0)
77 list.append(*pair.first);
81 return list;
84 Solid::Control::NetworkInterfaceList Solid::Control::NetworkManagerPrivate::networkInterfaces()
86 Ifaces::NetworkManager *backend = qobject_cast<Ifaces::NetworkManager *>(managerBackend());
88 if (backend!= 0)
90 return buildDeviceList(backend->networkInterfaces());
92 else
94 return NetworkInterfaceList();
98 Solid::Control::NetworkInterfaceList Solid::Control::NetworkManager::networkInterfaces()
100 return globalNetworkManager->networkInterfaces();
103 bool Solid::Control::NetworkManager::isNetworkingEnabled()
105 return_SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), false, isNetworkingEnabled());
108 bool Solid::Control::NetworkManager::isWirelessEnabled()
110 return_SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), false, isWirelessEnabled());
113 void Solid::Control::NetworkManager::setNetworkingEnabled(bool enabled)
115 SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), setNetworkingEnabled(enabled));
118 void Solid::Control::NetworkManager::setWirelessEnabled(bool enabled)
120 SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), setWirelessEnabled(enabled));
123 void Solid::Control::NetworkManager::notifyHiddenNetwork(const QString &networkName)
125 SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), notifyHiddenNetwork(networkName));
128 Solid::Networking::Status Solid::Control::NetworkManager::status()
130 return_SOLID_CALL(Ifaces::NetworkManager *, globalNetworkManager->managerBackend(), Solid::Networking::Unknown, status());
133 const Solid::Control::NetworkInterface &Solid::Control::NetworkManagerPrivate::findNetworkInterface(const QString &uni)
135 Ifaces::NetworkManager *backend = qobject_cast<Ifaces::NetworkManager *>(managerBackend());
137 if (backend == 0) return m_invalidDevice;
139 QPair<NetworkInterface *, Ifaces::NetworkInterface *> pair = findRegisteredNetworkInterface(uni);
141 if (pair.first != 0)
143 return *pair.first;
145 else
147 return m_invalidDevice;
151 const Solid::Control::NetworkInterface &Solid::Control::NetworkManager::findNetworkInterface(const QString &uni)
153 return globalNetworkManager->findNetworkInterface(uni);
156 Solid::Control::NetworkManager::Notifier * Solid::Control::NetworkManager::notifier()
158 return globalNetworkManager;
161 void Solid::Control::NetworkManagerPrivate::_k_networkInterfaceAdded(const QString &uni)
163 QPair<NetworkInterface *, Ifaces::NetworkInterface *> pair = m_networkInterfaceMap.take(uni);
165 if (pair.first!= 0)
167 // Oops, I'm not sure it should happen...
168 // But well in this case we'd better kill the old device we got, it's probably outdated
170 delete pair.first;
171 delete pair.second;
174 emit networkInterfaceAdded(uni);
177 void Solid::Control::NetworkManagerPrivate::_k_networkInterfaceRemoved(const QString &uni)
179 QPair<NetworkInterface *, Ifaces::NetworkInterface *> pair = m_networkInterfaceMap.take(uni);
181 if (pair.first!= 0)
183 delete pair.first;
184 delete pair.second;
187 emit networkInterfaceRemoved(uni);
190 void Solid::Control::NetworkManagerPrivate::_k_destroyed(QObject *object)
192 Ifaces::NetworkInterface *device = qobject_cast<Ifaces::NetworkInterface *>(object);
194 if (device!=0)
196 QString uni = device->uni();
197 QPair<NetworkInterface *, Ifaces::NetworkInterface *> pair = m_networkInterfaceMap.take(uni);
198 delete pair.first;
202 /***************************************************************************/
204 QPair<Solid::Control::NetworkInterface *, Solid::Control::Ifaces::NetworkInterface *>
205 Solid::Control::NetworkManagerPrivate::findRegisteredNetworkInterface(const QString &uni)
207 if (m_networkInterfaceMap.contains(uni)) {
208 return m_networkInterfaceMap[uni];
209 } else {
210 Ifaces::NetworkManager *backend = qobject_cast<Ifaces::NetworkManager *>(managerBackend());
211 Ifaces::NetworkInterface *iface = 0;
213 if (backend!=0)
215 iface = qobject_cast<Ifaces::NetworkInterface *>(backend->createNetworkInterface(uni));
218 if (iface!=0)
220 NetworkInterface *device = new NetworkInterface(iface);
221 QPair<NetworkInterface *, Ifaces::NetworkInterface *> pair(device, iface);
222 connect(iface, SIGNAL(destroyed(QObject *)),
223 this, SLOT(_k_destroyed(QObject *)));
224 m_networkInterfaceMap[uni] = pair;
225 return pair;
227 else
229 return QPair<NetworkInterface *, Ifaces::NetworkInterface *>(0, 0);
234 #include "networkmanager_p.moc"
235 #include "networkmanager.moc"