diff --git a/Network.cxx b/Network.cxx
index abc40db..35e6c02 100644
--- a/Network.cxx
+++ b/Network.cxx
@@ -1,163 +1,163 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
Copyright 2017 - 2019 Martin Koller, kollix@aon.at
This file is part of liquidshell.
liquidshell is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
liquidshell 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with liquidshell. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//--------------------------------------------------------------------------------
Network::Network(QWidget *parent)
: SysTrayItem(parent)
{
blinkTimer.setInterval(500);
connect(&blinkTimer, &QTimer::timeout, [this]() { blinkState = !blinkState; setPixmap(blinkState ? origPixmap : QPixmap()); });
checkState();
connect(NetworkManager::notifier(), &NetworkManager::Notifier::statusChanged, this, &Network::checkState);
connect(NetworkManager::notifier(), &NetworkManager::Notifier::connectivityChanged, this, &Network::checkState);
connect(NetworkManager::notifier(), &NetworkManager::Notifier::primaryConnectionChanged, this, &Network::checkState);
connect(NetworkManager::notifier(), &NetworkManager::Notifier::activeConnectionRemoved, this, &Network::checkState);
connect(NetworkManager::notifier(), &NetworkManager::Notifier::activeConnectionsChanged, this, &Network::checkState);
connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged, this, &Network::checkState);
QDBusConnection::sessionBus().send(
QDBusMessage::createMethodCall("org.kde.kded5", "/modules/networkmanagement",
"org.kde.plasmanetworkmanagement", "init"));
}
//--------------------------------------------------------------------------------
void Network::checkState()
{
if ( NetworkManager::status() == NetworkManager::Unknown )
{
// if the system does not have NM running, hide the icon
hide();
return;
}
show();
if ( NetworkManager::status() == NetworkManager::Connecting )
blinkTimer.start();
else
blinkTimer.stop();
if ( !NetworkManager::primaryConnection() || !NetworkManager::primaryConnection()->connection() )
{
setPixmap(origPixmap = QIcon::fromTheme("network-disconnect").pixmap(size()));
setToolTip(i18n("No Network Connection"));
return;
}
NetworkManager::ActiveConnection::Ptr conn(NetworkManager::primaryConnection());
//connect(conn.data(), &NetworkManager::ActiveConnection::vpnChanged, this, &Network::checkState);
QString tip;
if ( NetworkManager::connectivity() == NetworkManager::Full )
tip = i18n("Full Network Connectivity (%1)", conn->connection()->name());
else
tip = i18n("Limited Network Connectivity (%1)", conn->connection()->name());
NetworkManager::Device::Ptr device;
if ( conn->devices().count() )
{
device = NetworkManager::findNetworkInterface(conn->devices()[0]);
if ( device && device->ipV4Config().addresses().count() )
tip += "\n" + i18n("IPv4 Address: %1", device->ipV4Config().addresses()[0].ip().toString());
}
QPixmap pixmap;
if ( conn->type() == NetworkManager::ConnectionSettings::Wireless )
{
NetworkManager::WirelessDevice::Ptr dev = qobject_cast(device);
if ( dev )
{
NetworkManager::AccessPoint::Ptr accessPoint = dev->activeAccessPoint();
int signalStrength = accessPoint.isNull() ? 0 : accessPoint->signalStrength();
- int x = std::round(signalStrength / 25.0) * 25;
+ int x = qRound(signalStrength / 25.0) * 25;
pixmap = QIcon::fromTheme(QString("network-wireless-connected-%1").arg(x)).pixmap(size());
if ( !accessPoint.isNull() )
tip += "\n" + i18n("SSID: %1", accessPoint->ssid());
tip += "\n" + i18n("Signal Strength: %1", signalStrength);
}
}
else
{
pixmap = QIcon::fromTheme("network-connect").pixmap(size());
}
//qDebug() << conn << "type" << conn->type() << "vpn" << conn->vpn();
bool vpnActive = false;
for (const NetworkManager::ActiveConnection::Ptr &ac : NetworkManager::activeConnections())
{
if ( ac->vpn() )
{
vpnActive = true;
break;
}
}
if ( vpnActive )
{
pixmap = QIcon::fromTheme("security-high").pixmap(size());
tip += "\n" + i18n("VPN active");
}
setPixmap(origPixmap = pixmap);
setToolTip(tip);
}
//--------------------------------------------------------------------------------
QWidget *Network::getDetailsList()
{
if ( !networkList )
{
networkList = new NetworkList(this);
networkList->setAttribute(Qt::WA_DeleteOnClose);
connect(networkList.data(), &NetworkList::changed, this, &Network::showDetailsList); // reposition
}
return networkList.data();
}
//--------------------------------------------------------------------------------
diff --git a/Network.hxx b/Network.hxx
index 8ca9af5..fea929d 100644
--- a/Network.hxx
+++ b/Network.hxx
@@ -1,51 +1,52 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
Copyright 2017 Martin Koller, kollix@aon.at
This file is part of liquidshell.
liquidshell is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
liquidshell 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with liquidshell. If not, see .
*/
#ifndef _Network_H_
#define _Network_H_
// network management (status display, activate, switch, ...)
#include
#include
+#include
#include
#include
class Network : public SysTrayItem
{
Q_OBJECT
public:
Network(QWidget *parent);
protected:
QWidget *getDetailsList() override;
private Q_SLOTS:
void checkState();
private:
QTimer blinkTimer;
bool blinkState = false;
QPixmap origPixmap;
QPointer networkList;
};
#endif
diff --git a/NetworkList.cxx b/NetworkList.cxx
index f5d4ccd..1acbc53 100644
--- a/NetworkList.cxx
+++ b/NetworkList.cxx
@@ -1,353 +1,354 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
Copyright 2017 - 2019 Martin Koller, kollix@aon.at
This file is part of liquidshell.
liquidshell is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
liquidshell 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with liquidshell. If not, see .
*/
#include
#include
#include
#include
#include
+#include
#include
#include
#include
#include
#include
#include
#include
#include
//--------------------------------------------------------------------------------
NetworkButton::NetworkButton(NetworkManager::Connection::Ptr c, NetworkManager::Device::Ptr dev)
: connection(c), device(dev)
{
setCheckable(true);
if ( connection )
{
for (const NetworkManager::ActiveConnection::Ptr &ac : NetworkManager::activeConnections())
{
if ( ac->uuid() == c->uuid() )
{
setChecked(true);
break;
}
}
connect(this, &NetworkButton::toggled, this, &NetworkButton::toggleNetworkStatus);
}
else
setEnabled(false);
}
//--------------------------------------------------------------------------------
void NetworkButton::toggleNetworkStatus(bool on)
{
if ( on )
{
switch ( connection->settings()->connectionType() )
{
case NetworkManager::ConnectionSettings::Wired:
{
NetworkManager::activateConnection(connection->path(), connection->settings()->interfaceName(), QString());
break;
}
case NetworkManager::ConnectionSettings::Wireless:
{
NetworkManager::activateConnection(connection->path(), device->uni(), QString());
break;
}
case NetworkManager::ConnectionSettings::Vpn:
{
NetworkManager::ActiveConnection::Ptr conn(NetworkManager::primaryConnection());
if ( conn && !conn->devices().isEmpty() )
NetworkManager::activateConnection(connection->path(), conn->devices()[0], QString());
break;
}
default: ; // TODO
}
}
else
{
for (const NetworkManager::ActiveConnection::Ptr &ac : NetworkManager::activeConnections())
{
if ( ac->uuid() == connection->uuid() )
{
NetworkManager::deactivateConnection(ac->path());
break;
}
}
}
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
NetworkList::NetworkList(QWidget *parent)
: QFrame(parent)
{
setWindowFlags(windowFlags() | Qt::Popup);
setFrameShape(QFrame::StyledPanel);
QVBoxLayout *vbox = new QVBoxLayout(this);
hbox = new QHBoxLayout;
vbox->addLayout(hbox);
network = new QToolButton;
network->setIcon(QIcon::fromTheme("network-wired"));
network->setCheckable(true);
connect(network, &QToolButton::toggled, [](bool on) { NetworkManager::setNetworkingEnabled(on); });
connect(NetworkManager::notifier(), &NetworkManager::Notifier::networkingEnabledChanged, this, &NetworkList::statusUpdate);
hbox->addWidget(network);
wireless = new QToolButton;
wireless->setIcon(QIcon::fromTheme("network-wireless"));
wireless->setCheckable(true);
connect(wireless, &QToolButton::toggled, [](bool on) { NetworkManager::setWirelessEnabled(on); });
connect(NetworkManager::notifier(), &NetworkManager::Notifier::wirelessEnabledChanged, this, &NetworkList::statusUpdate);
hbox->addWidget(wireless);
statusUpdate();
hbox->addStretch();
QToolButton *configure = new QToolButton;
configure->setIcon(QIcon::fromTheme("configure"));
connect(configure, &QToolButton::clicked, this, &NetworkList::openConfigureDialog);
hbox->addWidget(configure);
// show connections
QWidget *widget = new QWidget;
connectionsVbox = new QVBoxLayout(widget);
connectionsVbox->setContentsMargins(QMargins());
connectionsVbox->setSizeConstraint(QLayout::SetMinAndMaxSize);
scroll = new QScrollArea;
scroll->setWidgetResizable(true);
scroll->setWidget(widget);
vbox->addWidget(scroll);
fillConnections();
QTimer *checkConnectionsTimer = new QTimer(this);
checkConnectionsTimer->setInterval(1000);
connect(checkConnectionsTimer, &QTimer::timeout, this, &NetworkList::fillConnections);
checkConnectionsTimer->start();
}
//--------------------------------------------------------------------------------
void NetworkList::openConfigureDialog()
{
// newer plasma has already a KCM
KService::Ptr service = KService::serviceByDesktopName("kcm_networkmanagement");
if ( service )
KRun::runApplication(*service, QList(), this);
else
KRun::run("kde5-nm-connection-editor", QList(), this);
close();
}
//--------------------------------------------------------------------------------
void NetworkList::statusUpdate()
{
network->setChecked(NetworkManager::isNetworkingEnabled());
wireless->setChecked(NetworkManager::isWirelessEnabled());
}
//--------------------------------------------------------------------------------
void NetworkList::fillConnections()
{
if ( underMouse() ) // avoid to delete the widget we are possibly hovering over
return;
QLayoutItem *child;
while ( (child = connectionsVbox->takeAt(0)) )
{
delete child->widget();
delete child;
}
NetworkManager::Connection::List allConnections = NetworkManager::listConnections();
for (const NetworkManager::Connection::Ptr c : allConnections)
{
if ( !c->isValid() )
continue;
if ( (c->settings()->connectionType() == NetworkManager::ConnectionSettings::Wired) &&
!c->uuid().isEmpty() )
{
NetworkButton *net = new NetworkButton(c);
net->setText(c->name());
net->setIcon(QIcon::fromTheme("network-wired"));
connectionsVbox->addWidget(net);
net->show();
}
else if ( c->settings()->connectionType() == NetworkManager::ConnectionSettings::Vpn )
{
NetworkButton *vpn = new NetworkButton(c);
vpn->setText(c->name());
vpn->setIcon(QIcon::fromTheme("security-high"));
connectionsVbox->addWidget(vpn);
vpn->show();
}
}
// show available wifi networks
if ( NetworkManager::isWirelessEnabled() )
{
for (const NetworkManager::Device::Ptr &device : NetworkManager::networkInterfaces())
{
if ( device->type() != NetworkManager::Device::Wifi )
continue;
NetworkManager::WirelessDevice::Ptr wifiDevice = device.objectCast();
for (const NetworkManager::WirelessNetwork::Ptr &network : wifiDevice->networks())
{
NetworkManager::AccessPoint::Ptr accessPoint = network->referenceAccessPoint();
if ( !accessPoint )
continue;
// check if we have a connection for this SSID
bool haveConnection = false;
NetworkManager::Connection::Ptr conn;
for (const NetworkManager::Connection::Ptr c : allConnections)
{
if ( c->isValid() && (c->settings()->connectionType() == NetworkManager::ConnectionSettings::Wireless) )
{
NetworkManager::Setting::Ptr setting = c->settings()->setting(NetworkManager::Setting::Wireless);
NetworkManager::WirelessSetting::Ptr s = setting.staticCast();
if ( s->ssid() == network->ssid() )
{
haveConnection = true;
conn = c;
break;
}
}
}
if ( haveConnection )
{
NetworkButton *net = new NetworkButton(conn, device);
net->setText(QString("%1 (%2%)").arg(network->ssid()).arg(network->signalStrength()));
net->setIcon(QIcon::fromTheme("network-wireless"));
connectionsVbox->addWidget(net);
net->show();
}
else
{
NetworkButton *net = new NetworkButton;
net->setText(QString("%1 (%2%)").arg(network->ssid()).arg(network->signalStrength()));
net->setIcon(QIcon::fromTheme("network-wireless"));
net->setEnabled(false); // TODO: allow to add a new connection. See NetworkManager::addAndActivateConnection
connectionsVbox->addWidget(net);
net->show();
}
/*
NetworkManager::WirelessSecurityType security =
NetworkManager::findBestWirelessSecurity(wifiDevice->wirelessCapabilities(), true,
wifiDevice->mode() == NetworkManager::WirelessDevice::Adhoc,
accessPoint->capabilities(), accessPoint->wpaFlags(), accessPoint->rsnFlags());
if ( (security != NetworkManager::UnknownSecurity) && (security != NetworkManager::NoneSecurity) )
net->setIcon(QIcon::fromTheme("object-locked"));
else
net->setIcon(QIcon::fromTheme("object-unlocked"));
*/
}
}
}
#if 0
// TEST
static int count = 15;
for (int i = 0; i < count; i++)
{
NetworkButton *net = new NetworkButton();
net->setText(QString("dummy %1").arg(i));
net->setIcon(QIcon::fromTheme("network-wired"));
connectionsVbox->addWidget(net);
net->show();
}
count -= 3;
if ( count <= 0 ) count = 15;
#endif
connectionsVbox->addStretch();
adjustSize();
emit changed();
}
//--------------------------------------------------------------------------------
QSize NetworkList::sizeHint() const
{
QWidget *w = scroll->widget();
QSize s;
s.setHeight(frameWidth() +
contentsMargins().top() +
layout()->contentsMargins().top() +
hbox->sizeHint().height() +
((layout()->spacing() == -1) ? style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing) : layout()->spacing()) +
scroll->frameWidth() +
scroll->contentsMargins().top() +
w->sizeHint().height() +
scroll->contentsMargins().bottom() +
scroll->frameWidth() +
layout()->contentsMargins().bottom() +
contentsMargins().bottom() +
frameWidth()
);
s.setWidth(frameWidth() +
contentsMargins().left() +
layout()->contentsMargins().left() +
scroll->frameWidth() +
scroll->contentsMargins().left() +
w->sizeHint().width() +
scroll->verticalScrollBar()->sizeHint().width() +
scroll->contentsMargins().right() +
scroll->frameWidth() +
layout()->contentsMargins().right() +
contentsMargins().right() +
frameWidth()
);
return s;
}
//--------------------------------------------------------------------------------