not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / solid / networkmanager-0.6 / NetworkManager-networkinterface.cpp
blob86f0068db550ae9c55596dd8b44be378466dff3e
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Will Stephenson <wstephenson@kde.org>
3 Copyright (C) 2008 Pino Toscano <pino@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 "NetworkManager-networkinterface.h"
22 #include "NetworkManager-networkinterface_p.h"
24 #include <QtDBus/QDBusConnection>
25 #include <QtDBus/QDBusReply>
27 #include <kdebug.h>
29 #include <solid/control/networkinterface.h>
31 void dump(const NMDBusDeviceProperties &device)
33 kDebug(1441)
34 << "\n path:" << device.path.path()
35 << "\n interface:" << device.interface
36 << "\n type:" << device.type
37 << "\n udi:" << device.udi
38 << "\n active:" << device.active
39 << "\n activationStage:" << device.activationStage
40 << "\n ipv4Address:" << device.ipv4Address
41 << "\n subnetMask:" << device.subnetMask
42 << "\n broadcast:" << device.broadcast
43 << "\n hardwareAddress:" << device.hardwareAddress
44 << "\n route:" << device.route
45 << "\n primaryDNS:" << device.primaryDNS
46 << "\n secondaryDNS:" << device.secondaryDNS
47 << "\n strength:" << device.strength
48 << "\n linkActive:" << device.linkActive
49 << "\n speed:" << device.speed
50 << "\n capabilities:" << device.capabilities
51 << "\n capabilitiesType:" << device.capabilitiesType
52 << "\n activeNetPath:" << device.activeNetPath
53 << "\n networks:" << device.networks
57 void deserialize(const QDBusMessage &message, NMDBusDeviceProperties & device)
59 //kDebug(1441) << /*"deserialize args: " << message.arguments() << */"signature: " << message.signature();
60 QList<QVariant> args = message.arguments();
61 device.path = ((args.size() != 0) ? args.takeFirst().value<QDBusObjectPath>() : QDBusObjectPath());
62 device.interface = (args.size() != 0) ? args.takeFirst().toString() : QString();
63 device.type = (args.size() != 0) ? args.takeFirst().toUInt() : 0;
64 device.udi = (args.size() != 0) ? args.takeFirst().toString() : QString();
65 device.active = (args.size() != 0) ? args.takeFirst().toBool() : false;
66 device.activationStage = (args.size() != 0) ? args.takeFirst().toUInt() : 0;
67 device.ipv4Address = (args.size() != 0) ? args.takeFirst().toString() : QString();
68 device.subnetMask = (args.size() != 0) ? args.takeFirst().toString() : QString();
69 device.broadcast = (args.size() != 0) ? args.takeFirst().toString() : QString();
70 device.hardwareAddress = (args.size() != 0) ? args.takeFirst().toString() : QString();
71 device.route = (args.size() != 0) ? args.takeFirst().toString() : QString();
72 device.primaryDNS = (args.size() != 0) ? args.takeFirst().toString() : QString();
73 device.secondaryDNS = (args.size() != 0) ? args.takeFirst().toString() : QString();
74 device.mode = (args.size() != 0) ? args.takeFirst().toInt() : 0;
75 device.strength = (args.size() != 0) ? args.takeFirst().toInt() : 0;
76 device.linkActive = (args.size() != 0) ? args.takeFirst().toBool() : false;
77 device.speed = (args.size() != 0) ? args.takeFirst().toInt() : 0;
78 device.capabilities = (args.size() != 0) ? args.takeFirst().toUInt() : 0;
79 device.capabilitiesType = (args.size() != 0) ? args.takeFirst().toUInt() : 0;
80 device.activeNetPath = (args.size() != 0) ? args.takeFirst().toString() : QString();
81 device.networks = (args.size() != 0) ? args.takeFirst().toStringList() : QStringList();
84 quint32 parseIPv4Address(const QString & addressString)
86 const QStringList parts = addressString.split(QChar::fromLatin1('.'), QString::SkipEmptyParts);
87 if (parts.count() != 4)
88 return 0;
90 quint32 address = 0;
91 for (int i = 0; i < 4; ++i)
93 bool ok = false;
94 const short value = parts.at(i).toShort(&ok);
95 if (value < 0 || value > 255)
96 return 0;
97 address |= (value << ((3 - i) * 8));
99 return address;
102 Solid::Control::IPv4Config parseIPv4Config(const NMDBusDeviceProperties & dev)
104 Solid::Control::IPv4Address address(
105 parseIPv4Address(dev.ipv4Address),
106 parseIPv4Address(dev.subnetMask),
107 parseIPv4Address(dev.route));
108 QList<quint32> dnsServers;
109 dnsServers.append(parseIPv4Address(dev.primaryDNS));
110 dnsServers.append(parseIPv4Address(dev.secondaryDNS));
111 return Solid::Control::IPv4Config(
112 QList<Solid::Control::IPv4Address>() << address,
113 dnsServers /*nameservers*/,
114 QStringList() /* domains */,
115 QList<Solid::Control::IPv4Route>() /* routes*/);
119 NMNetworkInterfacePrivate::NMNetworkInterfacePrivate(const QString & objPath)
120 : iface(NM_DBUS_SERVICE, objPath, NM_DBUS_INTERFACE_DEVICES, QDBusConnection::systemBus())
121 , objectPath(objPath)
122 , manager(0)
123 , active(false)
124 , type(Solid::Control::NetworkInterface::UnknownType)
125 , activationStage(NM_ACT_STAGE_UNKNOWN)
126 , designSpeed(0)
130 NMNetworkInterfacePrivate::~NMNetworkInterfacePrivate()
135 NMNetworkInterface::NMNetworkInterface(const QString & objectPath)
136 : NetworkInterface(), QObject(), d_ptr(new NMNetworkInterfacePrivate(objectPath))
138 Q_D(NMNetworkInterface);
139 d->q_ptr = this;
140 d->initGeneric();
143 NMNetworkInterface::NMNetworkInterface(NMNetworkInterfacePrivate &dd)
144 : NetworkInterface(), QObject(), d_ptr(&dd)
146 Q_D(NMNetworkInterface);
147 d->q_ptr = this;
148 d->initGeneric();
152 void NMNetworkInterfacePrivate::initGeneric()
154 QDBusMessage reply = iface.call("getProperties");
155 NMDBusDeviceProperties dev;
156 deserialize(reply, dev);
157 //dump(dev);
158 applyProperties(dev);
159 QDBusReply<QString> dbusdriver = iface.call("getDriver");
160 if (dbusdriver.isValid())
161 driver = dbusdriver.value();
162 ipv4Config = parseIPv4Config(dev);
165 NMNetworkInterface::~NMNetworkInterface()
167 delete d_ptr;
170 QString NMNetworkInterface::uni() const
172 Q_D(const NMNetworkInterface);
173 return d->objectPath;
176 bool NMNetworkInterface::isActive() const
178 Q_D(const NMNetworkInterface);
179 return d->active;
182 Solid::Control::NetworkInterface::Type NMNetworkInterface::type() const
184 Q_D(const NMNetworkInterface);
185 return d->type;
188 Solid::Control::NetworkInterface::ConnectionState NMNetworkInterface::connectionState() const
190 Q_D(const NMNetworkInterface);
191 Solid::Control::NetworkInterface::ConnectionState state = Solid::Control::NetworkInterface::UnknownState;
192 switch (d->activationStage)
194 default:
195 case NM_ACT_STAGE_UNKNOWN:
196 state = Solid::Control::NetworkInterface::UnknownState;
197 break;
198 case NM_ACT_STAGE_DEVICE_PREPARE:
199 state = Solid::Control::NetworkInterface::Preparing;
200 break;
201 case NM_ACT_STAGE_DEVICE_CONFIG:
202 state = Solid::Control::NetworkInterface::Configuring;
203 break;
204 case NM_ACT_STAGE_NEED_USER_KEY:
205 state = Solid::Control::NetworkInterface::NeedAuth;
206 break;
207 case NM_ACT_STAGE_IP_CONFIG_START:
208 state = Solid::Control::NetworkInterface::IPConfig;
209 break;
210 case NM_ACT_STAGE_IP_CONFIG_GET:
211 state = Solid::Control::NetworkInterface::IPConfig;
212 break;
213 case NM_ACT_STAGE_IP_CONFIG_COMMIT:
214 state = Solid::Control::NetworkInterface::IPConfig;
215 break;
216 case NM_ACT_STAGE_ACTIVATED:
217 state = Solid::Control::NetworkInterface::Activated;
218 break;
219 case NM_ACT_STAGE_FAILED:
220 state = Solid::Control::NetworkInterface::Failed;
221 break;
222 case NM_ACT_STAGE_CANCELLED:
223 state = Solid::Control::NetworkInterface::Disconnected;
224 break;
226 return state;
229 int NMNetworkInterface::designSpeed() const
231 Q_D(const NMNetworkInterface);
232 return d->designSpeed;
235 Solid::Control::NetworkInterface::Capabilities NMNetworkInterface::capabilities() const
237 Q_D(const NMNetworkInterface);
238 return d->capabilities;
241 QString NMNetworkInterface::activeNetwork() const
243 Q_D(const NMNetworkInterface);
244 return d->activeNetPath;
247 void NMNetworkInterfacePrivate::applyProperties(const NMDBusDeviceProperties & props)
249 switch (props.type)
251 case DEVICE_TYPE_UNKNOWN:
252 type = Solid::Control::NetworkInterface::UnknownType;
253 break;
254 case DEVICE_TYPE_802_3_ETHERNET:
255 type = Solid::Control::NetworkInterface::Ieee8023;
256 break;
257 case DEVICE_TYPE_802_11_WIRELESS:
258 type = Solid::Control::NetworkInterface::Ieee80211;
259 break;
260 default:
261 type = Solid::Control::NetworkInterface::UnknownType;
262 break;
264 active = props.active;
265 activationStage = static_cast<NMActStage>(props.activationStage);
266 designSpeed = props.speed;
267 if (props.capabilities & NM_DEVICE_CAP_NM_SUPPORTED)
268 capabilities |= Solid::Control::NetworkInterface::IsManageable;
269 if (props.capabilities & NM_DEVICE_CAP_CARRIER_DETECT)
270 capabilities |= Solid::Control::NetworkInterface::SupportsCarrierDetect;
271 #if 0
272 if (props.capabilities & NM_DEVICE_CAP_WIRELESS_SCAN)
273 capabilities |= Solid::Control::NetworkInterface::SupportsWirelessScan;
274 #endif
275 activeNetPath = props.activeNetPath;
276 interface = props.interface;
279 void NMNetworkInterface::setActive(bool active)
281 Q_D(NMNetworkInterface);
282 d->active = active;
283 #if 0
284 emit activeChanged(active);
285 #endif
288 void NMNetworkInterface::setActivationStage(int activationStage)
290 Q_D(NMNetworkInterface);
291 d->activationStage = static_cast<NMActStage>(activationStage);
292 emit connectionStateChanged(connectionState());
295 void NMNetworkInterface::addNetwork(const QDBusObjectPath & netPath)
297 Q_D(NMNetworkInterface);
298 d->notifyNewNetwork(netPath);
301 void NMNetworkInterface::removeNetwork(const QDBusObjectPath & netPath)
303 Q_D(NMNetworkInterface);
304 d->notifyRemoveNetwork(netPath);
307 void NMNetworkInterface::setManagerInterface(QDBusInterface * manager)
309 Q_D(NMNetworkInterface);
310 d->manager = manager;
313 QString NMNetworkInterface::interfaceName() const
315 Q_D(const NMNetworkInterface);
316 return d->interface;
319 QString NMNetworkInterface::driver() const
321 Q_D(const NMNetworkInterface);
322 return d->driver;
325 Solid::Control::IPv4Config NMNetworkInterface::ipV4Config() const
327 Q_D(const NMNetworkInterface);
328 return d->ipv4Config;
331 bool NMNetworkInterface::activateConnection(const QString & connectionUni, const QVariantMap & connectionParameters)
333 Q_UNUSED(connectionUni)
334 Q_UNUSED(connectionParameters)
335 return false;
338 bool NMNetworkInterface::deactivateConnection()
340 return false;
343 #include "NetworkManager-networkinterface.moc"