File indexing completed on 2024-04-14 15:49:47

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 - 2019 Martin Koller, kollix@aon.at
0004 
0005   This file is part of liquidshell.
0006 
0007   liquidshell is free software: you can redistribute it and/or modify
0008   it under the terms of the GNU General Public License as published by
0009   the Free Software Foundation, either version 3 of the License, or
0010   (at your option) any later version.
0011 
0012   liquidshell is distributed in the hope that it will be useful,
0013   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with liquidshell.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <Network.hxx>
0022 
0023 #include <NetworkManagerQt/Manager>
0024 #include <NetworkManagerQt/WirelessDevice>
0025 #include <NetworkManagerQt/VpnConnection>
0026 
0027 #include <QIcon>
0028 #include <QMouseEvent>
0029 #include <QDBusConnection>
0030 #include <QDBusMessage>
0031 #include <QDebug>
0032 
0033 #include <KLocalizedString>
0034 #include <KIconLoader>
0035 
0036 //--------------------------------------------------------------------------------
0037 
0038 Network::Network(QWidget *parent)
0039   : SysTrayItem(parent)
0040 {
0041   blinkTimer.setInterval(500);
0042   connect(&blinkTimer, &QTimer::timeout, [this]() { blinkState = !blinkState; setPixmap(blinkState ? origPixmap : QPixmap()); });
0043 
0044   checkState();
0045 
0046   connect(NetworkManager::notifier(), &NetworkManager::Notifier::statusChanged, this, &Network::checkState);
0047   connect(NetworkManager::notifier(), &NetworkManager::Notifier::connectivityChanged, this, &Network::checkState);
0048   connect(NetworkManager::notifier(), &NetworkManager::Notifier::primaryConnectionChanged, this, &Network::checkState);
0049   connect(NetworkManager::notifier(), &NetworkManager::Notifier::activeConnectionRemoved, this, &Network::checkState);
0050   connect(NetworkManager::notifier(), &NetworkManager::Notifier::activeConnectionsChanged, this, &Network::checkState);
0051 
0052   connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged, this, &Network::checkState);
0053 
0054   QDBusConnection::sessionBus().send(
0055       QDBusMessage::createMethodCall("org.kde.kded5", "/modules/networkmanagement",
0056                                      "org.kde.plasmanetworkmanagement", "init"));
0057 }
0058 
0059 //--------------------------------------------------------------------------------
0060 
0061 void Network::checkState()
0062 {
0063   if ( NetworkManager::status() == NetworkManager::Unknown )
0064   {
0065     // if the system does not have NM running, hide the icon
0066     hide();
0067     return;
0068   }
0069 
0070   show();
0071 
0072   if ( NetworkManager::status() == NetworkManager::Connecting )
0073     blinkTimer.start();
0074   else
0075     blinkTimer.stop();
0076 
0077   if ( !NetworkManager::primaryConnection() || !NetworkManager::primaryConnection()->connection() )
0078   {
0079     if ( QIcon::hasThemeIcon("network-offline") )
0080       setPixmap(origPixmap = QIcon::fromTheme("network-offline").pixmap(size()));
0081     else
0082       setPixmap(origPixmap = QIcon::fromTheme("network-disconnect").pixmap(size()));
0083 
0084     setToolTip(i18n("No Network Connection"));
0085     return;
0086   }
0087 
0088   NetworkManager::ActiveConnection::Ptr conn(NetworkManager::primaryConnection());
0089   //connect(conn.data(), &NetworkManager::ActiveConnection::vpnChanged, this, &Network::checkState);
0090 
0091   QString tip;
0092 
0093   if ( NetworkManager::connectivity() == NetworkManager::Full )
0094     tip = i18n("Full Network Connectivity (%1)", conn->connection()->name());
0095   else
0096     tip = i18n("Limited Network Connectivity (%1)", conn->connection()->name());
0097 
0098   NetworkManager::Device::Ptr device;
0099 
0100   if ( conn->devices().count() )
0101   {
0102     device = NetworkManager::findNetworkInterface(conn->devices()[0]);
0103 
0104     if ( device && device->ipV4Config().addresses().count() )
0105       tip += "\n" + i18n("IPv4 Address: %1", device->ipV4Config().addresses()[0].ip().toString());
0106   }
0107 
0108   QPixmap pixmap;
0109 
0110   if ( conn->type() == NetworkManager::ConnectionSettings::Wireless )
0111   {
0112     NetworkManager::WirelessDevice::Ptr dev = qobject_cast<NetworkManager::WirelessDevice::Ptr>(device);
0113 
0114     if ( dev )
0115     {
0116       NetworkManager::AccessPoint::Ptr accessPoint = dev->activeAccessPoint();
0117       int signalStrength = accessPoint.isNull() ? 0 : accessPoint->signalStrength();
0118       int x = qRound(signalStrength / 25.0) * 25;
0119       pixmap = QIcon::fromTheme(QString("network-wireless-connected-%1").arg(x)).pixmap(size());
0120 
0121       if ( !accessPoint.isNull() )
0122         tip += "\n" + i18n("SSID: %1", accessPoint->ssid());
0123 
0124       tip += "\n" + i18n("Signal Strength: %1", signalStrength);
0125     }
0126   }
0127   else
0128   {
0129     pixmap = QIcon::fromTheme("network-connect").pixmap(size());
0130   }
0131 
0132   //qDebug() << conn << "type" << conn->type() << "vpn" << conn->vpn();
0133 
0134   bool vpnActive = false;
0135   for (const NetworkManager::ActiveConnection::Ptr &ac : NetworkManager::activeConnections())
0136   {
0137     //qDebug() << ac << ac->id() << ac->type();
0138 
0139     if ( ac->vpn() )
0140     {
0141       vpnActive = true;
0142       break;
0143     }
0144 
0145     // search for tunnel device
0146     if ( ac->type() == NetworkManager::ConnectionSettings::Tun )
0147     {
0148       vpnActive = true;
0149       break;
0150     }
0151   }
0152 
0153   if ( vpnActive )
0154   {
0155     pixmap = QIcon::fromTheme("security-high").pixmap(size());
0156     tip += "\n" + i18n("VPN active");
0157   }
0158 
0159   setPixmap(origPixmap = pixmap);
0160   setToolTip(tip);
0161 }
0162 
0163 //--------------------------------------------------------------------------------
0164 
0165 QWidget *Network::getDetailsList()
0166 {
0167   if ( !networkList )
0168   {
0169     networkList = new NetworkList(this);
0170     networkList->setAttribute(Qt::WA_DeleteOnClose);
0171     connect(networkList.data(), &NetworkList::changed, this, &Network::showDetailsList);  // reposition
0172   }
0173   return networkList.data();
0174 }
0175 
0176 //--------------------------------------------------------------------------------