File indexing completed on 2024-04-14 14:20:35

0001 /*
0002     Copyright 2006-2007 Will Stephenson <wstephenson@kde.org>
0003     Copyright 2006-2007 Kevin Ottens <ervin@kde.org>
0004 
0005     This library is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU Lesser General Public
0007     License as published by the Free Software Foundation; either
0008     version 2.1 of the License, or (at your option) version 3, or any
0009     later version accepted by the membership of KDE e.V. (or its
0010     successor approved by the membership of KDE e.V.), which shall
0011     act as a proxy defined in Section 6 of version 3 of the license.
0012 
0013     This library is distributed in the hope that it will be useful,
0014     but WITHOUT ANY WARRANTY; without even the implied warranty of
0015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016     Lesser General Public License for more details.
0017 
0018     You should have received a copy of the GNU Lesser General Public
0019     License along with this library. If not, see <http://www.gnu.org/licenses/>.
0020 */
0021 
0022 //#include <KDebug>
0023 
0024 #include "networking.h"
0025 #include "networking_p.h"
0026 
0027 #include "org_kde_solid_networking_client.h"
0028 
0029 Solid::NetworkingPrivate::NetworkingPrivate()
0030     : netStatus(Solid::Networking::Unknown),
0031       connectPolicy(Solid::Networking::Managed),
0032       disconnectPolicy(Solid::Networking::Managed),
0033       iface(nullptr)
0034 {
0035     QDBusServiceWatcher *watcher = new QDBusServiceWatcher("org.kde.kded5", QDBusConnection::sessionBus(),
0036             QDBusServiceWatcher::WatchForOwnerChange, this);
0037     connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
0038             this, SLOT(serviceOwnerChanged(QString,QString,QString)));
0039 
0040     initialize();
0041 }
0042 
0043 Solid::NetworkingPrivate::~NetworkingPrivate()
0044 {
0045     delete iface;
0046 }
0047 
0048 void Solid::NetworkingPrivate::initialize()
0049 {
0050     delete iface;
0051     iface  = new OrgKdeSolidNetworkingClientInterface("org.kde.kded5",
0052             "/modules/networkstatus",
0053             QDBusConnection::sessionBus(),
0054             this);
0055 
0056     //connect( iface, SIGNAL(statusChanged(uint)), globalNetworkManager, SIGNAL(statusChanged(Networking::Status)) );
0057     connect(iface, SIGNAL(statusChanged(uint)), this, SLOT(serviceStatusChanged(uint)));
0058 
0059     QDBusReply<uint> reply = iface->status();
0060     if (reply.isValid()) {
0061         netStatus = (Solid::Networking::Status)reply.value();
0062     } else {
0063         netStatus = Solid::Networking::Unknown;
0064     }
0065 }
0066 
0067 void Solid::NetworkingPrivate::serviceStatusChanged(uint status)
0068 {
0069 //    qDebug() ;
0070     netStatus = (Solid::Networking::Status)status;
0071     switch (netStatus) {
0072     case Solid::Networking::Unknown:
0073         break;
0074     case Solid::Networking::Unconnected:
0075     case Solid::Networking::Disconnecting:
0076     case Solid::Networking::Connecting:
0077         if (disconnectPolicy == Solid::Networking::Managed) {
0078             emit Solid::Networking::notifier()->shouldDisconnect();
0079         } else if (disconnectPolicy == Solid::Networking::OnNextStatusChange) {
0080             setDisconnectPolicy(Solid::Networking::Manual);
0081             emit Solid::Networking::notifier()->shouldDisconnect();
0082         }
0083         break;
0084     case Solid::Networking::Connected:
0085         if (disconnectPolicy == Solid::Networking::Managed) {
0086             emit Solid::Networking::notifier()->shouldConnect();
0087         } else if (disconnectPolicy == Solid::Networking::OnNextStatusChange) {
0088             setConnectPolicy(Solid::Networking::Manual);
0089             emit Solid::Networking::notifier()->shouldConnect();
0090         }
0091         break;
0092 //      default:
0093 //        qDebug() <<  "Unrecognised status code!";
0094     }
0095     emit Solid::Networking::notifier()->statusChanged(netStatus);
0096 }
0097 
0098 void Solid::NetworkingPrivate::serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner)
0099 {
0100     Q_UNUSED(name)
0101     Q_UNUSED(oldOwner)
0102     if (newOwner.isEmpty()) {
0103         // kded quit on us
0104         netStatus = Solid::Networking::Unknown;
0105         emit Solid::Networking::notifier()->statusChanged(netStatus);
0106 
0107     } else {
0108         // kded was replaced or started
0109         initialize();
0110         emit Solid::Networking::notifier()->statusChanged(netStatus);
0111         serviceStatusChanged(netStatus);
0112     }
0113 }