plasma: new network applet

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-06-04 02:48:39 +03:00
parent 31e7fef0a1
commit 0c02c88b22
5 changed files with 246 additions and 1 deletions

View file

@ -1,3 +1,4 @@
project(plasma-applets)
add_subdirectory(lsof)
add_subdirectory(network)

View file

@ -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}
)

View file

@ -0,0 +1,155 @@
/* This file is part of the KDE project
Copyright (C) 2024 Ivailo Monev <xakepa10@gmail.com>
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 <KIcon>
#include <KNetworkManager>
#include <Plasma/CheckBox>
#include <KDebug>
// 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"

View file

@ -0,0 +1,45 @@
/* This file is part of the KDE project
Copyright (C) 2024 Ivailo Monev <xakepa10@gmail.com>
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 <Plasma/PopupApplet>
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

View file

@ -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