add more spacing
[personal-kdebase.git] / workspace / solid / networking / kded / networkstatus.cpp
blobcc563fa388381f1ae47a3cec4335fc0a4233fd9a
1 /* This file is part of kdebase/workspace/solid
2 Copyright (C) 2005,2007 Will Stephenson <wstephenson@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library. If not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
18 As a special exception, permission is given to link this library
19 with any edition of Qt, and distribute the resulting executable,
20 without including the source code for Qt in the source distribution.
23 #include "networkstatus.h"
25 #include <QMap>
27 #include <KDebug>
28 #include <solid/control/networkmanager.h>
30 #include "network.h"
31 #include "clientadaptor.h"
32 #include "serviceadaptor.h"
34 #include <kpluginfactory.h>
36 K_PLUGIN_FACTORY(NetworkStatusFactory,
37 registerPlugin<NetworkStatusModule>();
39 K_EXPORT_PLUGIN(NetworkStatusFactory("networkstatus"))
41 // INTERNALLY USED STRUCTS AND TYPEDEFS
43 typedef QMap< QString, Network * > NetworkMap;
45 class NetworkStatusModule::Private
47 public:
48 Private() : status( Solid::Networking::Unknown )
52 ~Private()
56 NetworkMap networks;
57 Solid::Networking::Status status;
58 Solid::Control::NetworkManager::Notifier * notifier;
61 // CTORS/DTORS
63 NetworkStatusModule::NetworkStatusModule(QObject* parent, const QList<QVariant>&)
64 : KDEDModule(parent), d( new Private )
66 new ClientAdaptor( this );
67 new ServiceAdaptor( this );
69 QDBusConnection dbus = QDBusConnection::sessionBus();
70 QDBusConnectionInterface * sessionBus = dbus.interface();
72 connect( sessionBus, SIGNAL(serviceOwnerChanged(const QString&,const QString&,const QString&)), this, SLOT(serviceOwnerChanged(const QString&,const QString&,const QString&)) );
73 init();
76 NetworkStatusModule::~NetworkStatusModule()
78 Q_FOREACH ( Network * net, d->networks ) {
79 delete net;
82 delete d;
85 // CLIENT INTERFACE
87 int NetworkStatusModule::status()
89 kDebug( 1222 ) << " status: " << (int)d->status;
90 return (int)d->status;
93 //protected:
95 void NetworkStatusModule::updateStatus()
97 Solid::Networking::Status bestStatus = Solid::Networking::Unknown;
98 const Solid::Networking::Status oldStatus = d->status;
100 Q_FOREACH ( Network * net, d->networks ) {
101 if ( net->status() > bestStatus )
102 bestStatus = net->status();
104 d->status = bestStatus;
106 if ( oldStatus != d->status ) {
107 emit statusChanged( (uint)d->status );
111 void NetworkStatusModule::serviceOwnerChanged( const QString & name ,const QString & oldOwner, const QString & newOwner )
113 if ( !oldOwner.isEmpty() && newOwner.isEmpty( ) ) {
114 // unregister and delete any networks owned by a service that has just unregistered
115 QMutableMapIterator<QString,Network*> it( d->networks );
116 while ( it.hasNext() ) {
117 it.next();
118 if ( it.value()->service() == name )
120 kDebug( 1222 ) << "Departing service " << name << " owned network " << it.value()->name() << ", removing it";
121 Network * removedNet = it.value();
122 it.remove();
123 updateStatus();
124 delete removedNet;
130 // SERVICE INTERFACE //
132 QStringList NetworkStatusModule::networks()
134 if ( d->networks.count() ) {
135 kDebug() << "Network status module is aware of " << d->networks.count() << " networks";
136 } else {
137 kDebug( 1222 ) << "Network status module is not aware of any networks";
139 return d->networks.keys();
142 void NetworkStatusModule::setNetworkStatus( const QString & networkName, int st )
144 kDebug( 1222 ) << networkName << ", " << st;
145 Solid::Networking::Status changedStatus = (Solid::Networking::Status)st;
146 if ( d->networks.contains( networkName ) ) {
147 Network * net = d->networks[ networkName ];
148 net->setStatus( changedStatus );
149 updateStatus();
150 } else {
151 kDebug( 1222 ) << " No network named '" << networkName << "' known.";
155 void NetworkStatusModule::registerNetwork( const QString & networkName, int status, const QString & serviceName )
157 QDBusConnection dbus = QDBusConnection::sessionBus();
158 QDBusConnectionInterface * sessionBus = dbus.interface();
159 QString uniqueOwner = sessionBus->serviceOwner( serviceName ).value();
161 kDebug( 1222 ) << networkName << ", with status " << status << " is owned by " << uniqueOwner;
163 d->networks.insert( networkName, new Network( networkName, status, uniqueOwner ) );
164 updateStatus();
167 void NetworkStatusModule::unregisterNetwork( const QString & networkName )
169 if ( networkName != QLatin1String("SolidNetwork") ) {
170 kDebug( 1222 ) << networkName << " unregistered.";
172 d->networks.remove( networkName );
173 updateStatus();
177 void NetworkStatusModule::init()
179 d->notifier = Solid::Control::NetworkManager::notifier();
180 connect( d->notifier, SIGNAL(statusChanged(Solid::Networking::Status)),
181 this, SLOT(solidNetworkingStatusChanged(Solid::Networking::Status)));
182 Solid::Networking::Status status = Solid::Control::NetworkManager::status();
183 registerNetwork( QLatin1String("SolidNetwork"), status, QLatin1String("org.kde.kded") );
186 void NetworkStatusModule::solidNetworkingStatusChanged( Solid::Networking::Status status )
188 kDebug( 1222 ) << "SolidNetwork changed status: " << status;
189 setNetworkStatus( QLatin1String("SolidNetwork"), status );
192 #include "networkstatus.moc"
193 // vim: set noet sw=4 ts=4: