add more spacing
[personal-kdebase.git] / workspace / solid / networkmanager-0.7 / networkinterface.cpp
blob397fe9dad0ecb26c824b75d887171931344741d7
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
6 published by the Free Software Foundation; either version 2 of
7 the License or (at your option) version 3 or any later version
8 accepted by the membership of KDE e.V. (or its successor approved
9 by the membership of KDE e.V.), which shall act as a proxy
10 defined in Section 14 of version 3 of the license.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "networkinterface.h"
22 #include "networkinterface_p.h"
24 #include <arpa/inet.h>
26 #include <KDebug>
28 #include "dbus/nm-ip4-configinterface.h"
29 #include "manager.h"
30 #include "networkmanagerdefinitions.h"
32 // lifted from libnm-glib/nm-device.h, remove when our NM packages have this version
33 #define NM_DEVICE_UDI "udi"
34 #define NM_DEVICE_INTERFACE "interface"
35 #define NM_DEVICE_DRIVER "driver"
36 #define NM_DEVICE_CAPABILITIES "capabilities"
37 #define NM_DEVICE_MANAGED "managed"
38 #define NM_DEVICE_IP4_CONFIG "ip4-config"
39 #define NM_DEVICE_STATE "state"
40 #define NM_DEVICE_VENDOR "vendor"
41 #define NM_DEVICE_PRODUCT "product"
43 NMNetworkInterfacePrivate::NMNetworkInterfacePrivate( const QString & path, QObject * owner ) : deviceIface(NMNetworkManager::DBUS_SERVICE, path, QDBusConnection::systemBus()), uni(path), designSpeed(0), manager(0)/*, propHelper(owner)*/
45 Q_UNUSED(owner);
46 //isLinkUp = deviceIface.isLinkUp();
47 driver = deviceIface.driver();
48 interfaceName = deviceIface.interface();
49 ipV4Address = deviceIface.ip4Address();
50 managed = deviceIface.managed();
52 //TODO set active connections based on active connection list on the manager; find out if
53 //signal needed
54 //activeConnection = deviceIface.activeConnection();
55 //propHelper.registerProperty(NM_DEVICE_UDI, PropertySignalPair("uni",0));
58 NMNetworkInterface::NMNetworkInterface(const QString & path, NMNetworkManager * manager, QObject * parent) : QObject(parent), d_ptr(new NMNetworkInterfacePrivate(path, this))
60 Q_D(NMNetworkInterface);
61 init();
62 d->manager = manager;
65 NMNetworkInterface::NMNetworkInterface(NMNetworkInterfacePrivate & dd, NMNetworkManager * manager, QObject * parent) : QObject(parent), d_ptr(&dd)
67 qDBusRegisterMetaType<UIntList>();
68 qDBusRegisterMetaType<UIntListList>();
69 Q_D(NMNetworkInterface);
70 init();
71 d->manager = manager;
74 void NMNetworkInterface::init()
76 Q_D(NMNetworkInterface);
77 d->capabilities = convertCapabilities(d->deviceIface.capabilities());
78 d->connectionState = convertState(d->deviceIface.state());
80 connect(&d->deviceIface, SIGNAL(StateChanged(uint,uint,uint)), this, SLOT(stateChanged(uint,uint,uint)));
83 NMNetworkInterface::~NMNetworkInterface()
88 QString NMNetworkInterface::uni() const
90 Q_D(const NMNetworkInterface);
91 return d->uni;
94 void NMNetworkInterface::setUni(const QVariant & uni)
96 Q_D(NMNetworkInterface);
97 d->uni = uni.toString();
100 QString NMNetworkInterface::interfaceName() const
102 Q_D(const NMNetworkInterface);
103 return d->interfaceName;
106 void NMNetworkInterface::setInterfaceName(const QVariant & name)
108 Q_D(NMNetworkInterface);
109 d->interfaceName = name.toString();
112 QString NMNetworkInterface::driver() const
114 Q_D(const NMNetworkInterface);
115 return d->driver;
118 void NMNetworkInterface::setDriver(const QVariant & driver)
120 Q_D(NMNetworkInterface);
121 d->driver = driver.toString();
124 int NMNetworkInterface::ipV4Address() const
126 Q_D(const NMNetworkInterface);
127 return d->ipV4Address;
130 Solid::Control::IPv4Config NMNetworkInterface::ipV4Config() const
132 Q_D(const NMNetworkInterface);
133 if (d->connectionState != Solid::Control::NetworkInterface::Activated) {
134 return Solid::Control::IPv4Config();
135 } else {
136 // ask the daemon for the details
137 QDBusObjectPath ipV4ConfigPath = d->deviceIface.ip4Config();
138 OrgFreedesktopNetworkManagerIP4ConfigInterface iface(NMNetworkManager::DBUS_SERVICE, ipV4ConfigPath.path(), QDBusConnection::systemBus());
139 if (iface.isValid()) {
140 //convert ipaddresses into object
141 UIntListList addresses = iface.addresses();
142 QList<Solid::Control::IPv4Address> addressObjects;
143 foreach (UIntList addressList, addresses) {
144 if ( addressList.count() == 3 ) {
145 Solid::Control::IPv4Address addr(htonl(addressList[0]), htonl(addressList[1]), htonl(addressList[2]));
146 addressObjects.append(addr);
149 //convert routes into objects
150 UIntListList routes = iface.routes();
151 QList<Solid::Control::IPv4Route> routeObjects;
152 foreach (UIntList routeList, routes) {
153 if ( routeList.count() == 4 ) {
154 Solid::Control::IPv4Route addr(routeList[0], routeList[1], routeList[2], routeList[3]);
155 routeObjects.append(addr);
158 return Solid::Control::IPv4Config(addressObjects,
159 iface.nameservers(), iface.domains(),
160 routeObjects);
161 } else {
162 return Solid::Control::IPv4Config();
167 bool NMNetworkInterface::isActive() const
169 Q_D(const NMNetworkInterface);
170 return !(d->connectionState == Solid::Control::NetworkInterface::Unavailable
171 || d->connectionState == Solid::Control::NetworkInterface::Disconnected
172 || d->connectionState == Solid::Control::NetworkInterface::Failed );
175 bool NMNetworkInterface::managed() const
177 Q_D(const NMNetworkInterface);
178 return d->managed;
181 void NMNetworkInterface::setManaged(const QVariant & driver)
183 Q_D(NMNetworkInterface);
184 d->driver = driver.toBool();
187 Solid::Control::NetworkInterface::ConnectionState NMNetworkInterface::connectionState() const
189 Q_D(const NMNetworkInterface);
190 return d->connectionState;
193 void NMNetworkInterface::setConnectionState(const QVariant & state)
195 Q_D(NMNetworkInterface);
196 d->connectionState = convertState(state.toUInt());
199 int NMNetworkInterface::designSpeed() const
201 Q_D(const NMNetworkInterface);
202 return d->designSpeed;
205 bool NMNetworkInterface::isLinkUp() const
207 Q_D(const NMNetworkInterface);
208 return d->isLinkUp;
211 Solid::Control::NetworkInterface::Capabilities NMNetworkInterface::capabilities() const
213 Q_D(const NMNetworkInterface);
214 return d->capabilities;
217 QVariant NMNetworkInterface::capabilitiesV() const
219 Q_D(const NMNetworkInterface);
220 return QVariant(d->capabilities);
223 void NMNetworkInterface::setCapabilitiesV(const QVariant & caps)
225 Q_D(NMNetworkInterface);
226 d->capabilities = convertCapabilities(caps.toUInt());
229 Solid::Control::NetworkInterface::Capabilities NMNetworkInterface::convertCapabilities(uint theirCaps)
231 Solid::Control::NetworkInterface::Capabilities ourCaps
232 = (Solid::Control::NetworkInterface::Capabilities) theirCaps;
233 return ourCaps;
236 Solid::Control::NetworkInterface::ConnectionState NMNetworkInterface::convertState(uint theirState)
238 Solid::Control::NetworkInterface::ConnectionState ourState = (Solid::Control::NetworkInterface::ConnectionState)theirState;
239 return ourState;
242 void NMNetworkInterface::stateChanged(uint new_state, uint old_state, uint reason)
244 Q_D(NMNetworkInterface);
245 d->connectionState = convertState(new_state);
246 emit connectionStateChanged(d->connectionState);
249 #include "networkinterface.moc"