From 0c02c88b221e727d65b88ac71ba448b4a317a32c Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Tue, 4 Jun 2024 02:48:39 +0300 Subject: [PATCH] plasma: new network applet Signed-off-by: Ivailo Monev --- plasma/applets/CMakeLists.txt | 3 +- plasma/applets/network/CMakeLists.txt | 21 +++ plasma/applets/network/network.cpp | 155 ++++++++++++++++++ plasma/applets/network/network.h | 45 +++++ .../network/plasma-applet-network.desktop | 23 +++ 5 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 plasma/applets/network/CMakeLists.txt create mode 100644 plasma/applets/network/network.cpp create mode 100644 plasma/applets/network/network.h create mode 100644 plasma/applets/network/plasma-applet-network.desktop diff --git a/plasma/applets/CMakeLists.txt b/plasma/applets/CMakeLists.txt index 6a511b73..a90cf3b7 100644 --- a/plasma/applets/CMakeLists.txt +++ b/plasma/applets/CMakeLists.txt @@ -1,3 +1,4 @@ project(plasma-applets) -add_subdirectory(lsof) \ No newline at end of file +add_subdirectory(lsof) +add_subdirectory(network) \ No newline at end of file diff --git a/plasma/applets/network/CMakeLists.txt b/plasma/applets/network/CMakeLists.txt new file mode 100644 index 00000000..a316e85f --- /dev/null +++ b/plasma/applets/network/CMakeLists.txt @@ -0,0 +1,21 @@ +project(plasma-applet-network) + +set(network_SRCS + network.cpp +) + +kde4_add_plugin(plasma_applet_network ${network_SRCS}) +target_link_libraries(plasma_applet_network + KDE4::plasma + KDE4::knetworkmanager +) + +install( + TARGETS plasma_applet_network + DESTINATION ${KDE4_PLUGIN_INSTALL_DIR} +) + +install( + FILES plasma-applet-network.desktop + DESTINATION ${KDE4_SERVICES_INSTALL_DIR} +) diff --git a/plasma/applets/network/network.cpp b/plasma/applets/network/network.cpp new file mode 100644 index 00000000..04b1e82a --- /dev/null +++ b/plasma/applets/network/network.cpp @@ -0,0 +1,155 @@ +/* This file is part of the KDE project + Copyright (C) 2024 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "network.h" + +#include +#include +#include +#include + +// TODO: KNetworkManager::isEnabled() for initial status +// TODO: KNetworkManager::connections() for connections status (which is the one being used) + +static const int s_svgiconsize = 256; +static const QString s_defaultpopupicon = QString::fromLatin1("network-workgroup"); +static const QSizeF s_minimumsize = QSizeF(150, 50); + +static QIcon kNetworkIcon(QObject *parent, const bool active, bool wireless, const int strength) +{ + QIcon result; + Plasma::Svg plasmasvg(parent); + plasmasvg.setImagePath("icons/network"); + plasmasvg.setContainsMultipleImages(true); + if (plasmasvg.isValid()) { + QPixmap iconpixmap(s_svgiconsize, s_svgiconsize); + iconpixmap.fill(Qt::transparent); + QPainter iconpainter(&iconpixmap); + if (wireless) { + if (strength > 75) { + plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-100"); + } else if (strength > 50) { + plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-75"); + } else if (strength > 25) { + plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-50"); + } else if (strength > 0) { + plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-25"); + } else { + plasmasvg.paint(&iconpainter, iconpixmap.rect(), "network-wireless-0"); + } + } else { + plasmasvg.paint(&iconpainter, iconpixmap.rect(), active ? "network-wired-activated" : "network-wired"); + } + result = QIcon(iconpixmap); + } else { + result = KIcon(s_defaultpopupicon); + } + return result; +} + +class NetworkAppletWidget : public QGraphicsWidget +{ + Q_OBJECT +public: + NetworkAppletWidget(NetworkApplet* network); + ~NetworkAppletWidget(); + +private Q_SLOTS: + void slotDisableNetwork(const bool disable); + void slotStatusChanged(const KNetworkManager::KNetworkStatus status); + +private: + NetworkApplet* m_network; + QGraphicsLinearLayout* m_layout; + Plasma::CheckBox* m_disablenetworkbox; + KNetworkManager* m_networkmanager; +}; + +NetworkAppletWidget::NetworkAppletWidget(NetworkApplet* network) + : QGraphicsWidget(network), + m_network(network), + m_layout(nullptr), + m_disablenetworkbox(nullptr) +{ + setMinimumSize(s_minimumsize); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + + m_layout = new QGraphicsLinearLayout(Qt::Vertical, this); + m_disablenetworkbox = new Plasma::CheckBox(this); + m_disablenetworkbox->setText(i18n("Disable network")); + m_disablenetworkbox->setEnabled(KNetworkManager::isSupported()); + connect(m_disablenetworkbox, SIGNAL(toggled(bool)), this, SLOT(slotDisableNetwork(bool))); + m_layout->addItem(m_disablenetworkbox); + setLayout(m_layout); + + m_networkmanager = new KNetworkManager(this); + slotStatusChanged(m_networkmanager->status()); + connect( + m_networkmanager, SIGNAL(statusChanged(KNetworkManager::KNetworkStatus)), + this, SLOT(slotStatusChanged(KNetworkManager::KNetworkStatus)) + ); +} + +NetworkAppletWidget::~NetworkAppletWidget() +{ +} + +void NetworkAppletWidget::slotDisableNetwork(const bool disable) +{ + if (disable) { + m_networkmanager->enable(false); + } else { + m_networkmanager->enable(true); + } +} + +void NetworkAppletWidget::slotStatusChanged(const KNetworkManager::KNetworkStatus status) +{ + m_network->setPopupIcon( + kNetworkIcon(this, status == KNetworkManager::ConnectedStatus, false, 0) + ); +} + + +NetworkApplet::NetworkApplet(QObject *parent, const QVariantList &args) + : Plasma::PopupApplet(parent, args), + m_networkwidget(nullptr) +{ + setAspectRatioMode(Plasma::AspectRatioMode::IgnoreAspectRatio); + setPopupIcon(s_defaultpopupicon); +} + +NetworkApplet::~NetworkApplet() +{ + delete m_networkwidget; +} + +void NetworkApplet::init() +{ + Plasma::PopupApplet::init(); + + m_networkwidget = new NetworkAppletWidget(this); +} + +QGraphicsWidget* NetworkApplet::graphicsWidget() +{ + return m_networkwidget; +} + +#include "moc_network.cpp" +#include "network.moc" diff --git a/plasma/applets/network/network.h b/plasma/applets/network/network.h new file mode 100644 index 00000000..1af31c5c --- /dev/null +++ b/plasma/applets/network/network.h @@ -0,0 +1,45 @@ +/* This file is part of the KDE project + Copyright (C) 2024 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef NETWORK_H +#define NETWORK_H + +#include + +class NetworkAppletWidget; + +class NetworkApplet : public Plasma::PopupApplet +{ + Q_OBJECT +public: + NetworkApplet(QObject *parent, const QVariantList &args); + ~NetworkApplet(); + + // Plasma::Applet reimplementation + void init() final; + // Plasma::PopupApplet reimplementation + QGraphicsWidget* graphicsWidget() final; + +private: + friend NetworkAppletWidget; + NetworkAppletWidget *m_networkwidget; +}; + +K_EXPORT_PLASMA_APPLET(network, NetworkApplet) + +#endif // NETWORK_H diff --git a/plasma/applets/network/plasma-applet-network.desktop b/plasma/applets/network/plasma-applet-network.desktop new file mode 100644 index 00000000..086cea6c --- /dev/null +++ b/plasma/applets/network/plasma-applet-network.desktop @@ -0,0 +1,23 @@ +[Desktop Entry] +Name=Network Monitor and Manager +Comment=See the network status and manage it +Encoding=UTF-8 +Keywords=Network;System; + +Icon=network-workgroup +Type=Service +X-KDE-ServiceTypes=Plasma/Applet,Plasma/PopupApplet +X-KDE-Library=plasma_applet_network + +X-Plasma-DefaultSize=450,300 +X-Plasma-NotificationArea=true + +X-KDE-PluginInfo-Author=Ivailo Monev +X-KDE-PluginInfo-Category=System Information +X-KDE-PluginInfo-Email=xakepa10@gmail.com +X-KDE-PluginInfo-License=GPL +X-KDE-PluginInfo-Name=network +X-KDE-PluginInfo-Version=1.0 +X-KDE-PluginInfo-Website= +X-KDE-PluginInfo-Depends= +X-KDE-PluginInfo-EnabledByDefault=true