Commit 36dd019c authored by Alexander Volkov's avatar Alexander Volkov Committed by Alexander Volkov

Use NetworkManager::DeviceStatistics instead of Plasma data engine

Summary:
It's more correct to get information about devices from a single
source, i.e. from NetworkManager. Besides it allows to show the
total number of received/transmitted bytes instead of the current
speed, which is more useful for the user. The current speed can
still be seen on the graph.

This change requires NetworkManagerQt 5.50, because of the fixed
DevicesStatistics::setRefreshRateMs().

Reviewers: #plasma, jgrulich, sitter, jriddell

Reviewed By: jgrulich

Subscribers: broulik, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D14835
parent 888058ad
......@@ -6,7 +6,7 @@ set(PROJECT_VERSION "5.13.80")
set(PROJECT_VERSION_MAJOR 5)
set(QT_MIN_VERSION "5.9.0")
set(KF5_MIN_VERSION "5.42.0")
set(KF5_MIN_VERSION "5.50.0")
################# set KDE specific information #################
......
......@@ -34,7 +34,8 @@ PlasmaComponents.ListItem {
property bool predictableWirelessPassword: !Uuid && Type == PlasmaNM.Enums.Wireless &&
(SecurityType == PlasmaNM.Enums.StaticWep || SecurityType == PlasmaNM.Enums.WpaPsk ||
SecurityType == PlasmaNM.Enums.Wpa2Psk)
property bool showSpeed: ConnectionState == PlasmaNM.Enums.Activated &&
property bool showSpeed: plasmoid.expanded &&
ConnectionState == PlasmaNM.Enums.Activated &&
(Type == PlasmaNM.Enums.Wired ||
Type == PlasmaNM.Enums.Wireless ||
Type == PlasmaNM.Enums.Gsm ||
......@@ -46,17 +47,6 @@ PlasmaComponents.ListItem {
enabled: true
height: expanded ? baseHeight + separator.height + expandableComponentLoader.height + (2 * Math.round(units.gridUnit / 3)) : baseHeight
PlasmaCore.DataSource {
id: dataSource
property string downloadSource: "network/interfaces/" + DeviceName + "/receiver/data"
property string uploadSource: "network/interfaces/" + DeviceName + "/transmitter/data"
connectedSources: showSpeed && plasmoid.expanded ? [downloadSource, uploadSource] : []
engine: "systemmonitor"
interval: 2000
}
ColumnLayout {
anchors.fill: parent
......@@ -159,7 +149,7 @@ PlasmaComponents.ListItem {
top: parent.top
}
height: visible ? implicitHeight : 0
visible: showSpeed && dataSource.data && dataSource.data[dataSource.downloadSource] && dataSource.data[dataSource.uploadSource]
visible: showSpeed
PlasmaComponents.TabButton {
id: speedTabButton
......@@ -197,8 +187,9 @@ PlasmaComponents.ListItem {
top: detailsTabBar.visible ? detailsTabBar.bottom : parent.top
topMargin: Math.round(units.gridUnit / 3)
}
dataEngine: dataSource
deviceName: DeviceName
rxBytes: RxBytes
txBytes: TxBytes
interval: 2000
visible: detailsTabBar.currentTab == speedTabButton
}
}
......@@ -313,22 +304,26 @@ PlasmaComponents.ListItem {
result += ", " + SecurityTypeString
return result
} else if (ConnectionState == PlasmaNM.Enums.Activated) {
if (showSpeed && dataSource.data && dataSource.data[dataSource.downloadSource] && dataSource.data[dataSource.uploadSource]) {
if (showSpeed) {
var downloadColor = theme.highlightColor
// cycle upload color by 180 degrees
var uploadColor = Qt.hsva((downloadColor.hsvHue + 0.5) % 1, downloadColor.hsvSaturation, downloadColor.hsvValue, downloadColor.a)
return i18n("Connected, <font color='%1'>⬇</font> %2/s, <font color='%3'>⬆</font> %4/s",
return i18n("Connected, <font color='%1'>⬇</font> %2, <font color='%3'>⬆</font> %4",
downloadColor,
KCoreAddons.Format.formatByteSize(dataSource.data[dataSource.downloadSource].value * 1024 || 0),
KCoreAddons.Format.formatByteSize(RxBytes),
uploadColor,
KCoreAddons.Format.formatByteSize(dataSource.data[dataSource.uploadSource].value * 1024 || 0))
KCoreAddons.Format.formatByteSize(TxBytes))
} else {
return i18n("Connected")
}
}
}
onShowSpeedChanged: {
connectionModel.setDeviceStatisticsRefreshRateMs(DevicePath, showSpeed ? 2000 : 0)
}
onActivatingChanged: {
if (ConnectionState == PlasmaNM.Enums.Activating) {
ListView.view.positionViewAtBeginning()
......
......@@ -24,8 +24,9 @@ import org.kde.kquickcontrolsaddons 2.0 as KQuickControlsAddons
import org.kde.plasma.components 2.0 as PlasmaComponents
Item {
property QtObject dataEngine: null
property string deviceName
property real rxBytes: 0
property real txBytes: 0
property alias interval: timer.interval
height: visible ? plotter.height + units.gridUnit : 0
......@@ -41,7 +42,7 @@ Item {
height: paintedHeight
font.pointSize: theme.smallestFont.pointSize
lineHeight: 1.75
text: KCoreAddons.Format.formatByteSize((plotter.maxValue * 1024) * (1 - index / 5))
text: KCoreAddons.Format.formatByteSize(plotter.maxValue * (1 - index / 5)) + i18n("/s")
}
}
......@@ -75,20 +76,22 @@ Item {
}
]
Connections {
target: dataEngine;
onNewData: {
if (sourceName.indexOf("network/interfaces/" + deviceName) != 0) {
return;
}
var rx = dataEngine.data[dataEngine.downloadSource];
var tx = dataEngine.data[dataEngine.uploadSource];
if (rx === undefined || rx.value === undefined ||
tx === undefined || tx.value === undefined) {
return;
}
plotter.addSample([rx.value, tx.value]);
Timer {
id: timer
repeat: true
running: parent.visible
property real prevRxBytes
property real prevTxBytes
Component.onCompleted: {
prevRxBytes = rxBytes
prevTxBytes = txBytes
}
onTriggered: {
var rxSpeed = (rxBytes - prevRxBytes) * 1000 / interval
var txSpeed = (txBytes - prevTxBytes) * 1000 / interval
prevRxBytes = rxBytes
prevTxBytes = txBytes
plotter.addSample([rxSpeed, txSpeed]);
}
}
}
......
......@@ -106,6 +106,10 @@ QVariant NetworkModel::data(const QModelIndex &index, int role) const
return item->vpnState();
case VpnType:
return item->vpnType();
case RxBytesRole:
return item->rxBytes();
case TxBytesRole:
return item->txBytes();
default:
break;
}
......@@ -149,6 +153,8 @@ QHash<int, QByteArray> NetworkModel::roleNames() const
roles[UuidRole] = "Uuid";
roles[VpnState] = "VpnState";
roles[VpnType] = "VpnType";
roles[RxBytesRole] = "RxBytes";
roles[TxBytesRole] = "TxBytes";
return roles;
}
......@@ -210,6 +216,20 @@ void NetworkModel::initializeSignals(const NetworkManager::Device::Ptr &device)
connect(device.data(), &NetworkManager::Device::ipInterfaceChanged, this, &NetworkModel::ipInterfaceChanged);
connect(device.data(), &NetworkManager::Device::stateChanged, this, &NetworkModel::deviceStateChanged, Qt::UniqueConnection);
auto deviceStatistics = device->deviceStatistics();
connect(deviceStatistics.data(), &NetworkManager::DeviceStatistics::rxBytesChanged, this, [this, device](qulonglong rxBytes) {
for (auto *item : m_list.returnItems(NetworkItemsList::Device, device->uni())) {
item->setRxBytes(rxBytes);
updateItem(item);
}
});
connect(deviceStatistics.data(), &NetworkManager::DeviceStatistics::txBytesChanged, this, [this, device](qulonglong txBytes) {
for (auto *item : m_list.returnItems(NetworkItemsList::Device, device->uni())) {
item->setTxBytes(txBytes);
updateItem(item);
}
});
if (device->type() == NetworkManager::Device::Wifi) {
NetworkManager::WirelessDevice::Ptr wifiDev = device.objectCast<NetworkManager::WirelessDevice>();
connect(wifiDev.data(), &NetworkManager::WirelessDevice::networkAppeared, this, &NetworkModel::wirelessNetworkAppeared, Qt::UniqueConnection);
......@@ -508,6 +528,15 @@ void NetworkModel::onItemUpdated()
}
}
void NetworkModel::setDeviceStatisticsRefreshRateMs(const QString &devicePath, uint refreshRate)
{
NetworkManager::Device::Ptr device = NetworkManager::findNetworkInterface(devicePath);
if (device) {
device->deviceStatistics()->setRefreshRateMs(refreshRate);
}
}
void NetworkModel::updateItem(NetworkModelItem*item)
{
const int row = m_list.indexOf(item);
......
......@@ -67,7 +67,9 @@ public:
UniRole,
UuidRole,
VpnState,
VpnType
VpnType,
RxBytesRole,
TxBytesRole
};
int rowCount(const QModelIndex &parent) const override;
......@@ -76,6 +78,7 @@ public:
public Q_SLOTS:
void onItemUpdated();
void setDeviceStatisticsRefreshRateMs(const QString &devicePath, uint refreshRate);
private Q_SLOTS:
void accessPointSignalStrengthChanged(int signal);
......
......@@ -62,6 +62,8 @@ NetworkModelItem::NetworkModelItem(QObject *parent)
, m_slave(false)
, m_type(NetworkManager::ConnectionSettings::Unknown)
, m_vpnState(NetworkManager::VpnConnection::Unknown)
, m_rxBytes(0)
, m_txBytes(0)
{
}
......@@ -80,6 +82,8 @@ NetworkModelItem::NetworkModelItem(const NetworkModelItem *item, QObject *parent
, m_type(item->type())
, m_uuid(item->uuid())
, m_vpnState(NetworkManager::VpnConnection::Unknown)
, m_rxBytes(0)
, m_txBytes(0)
{
}
......@@ -408,6 +412,26 @@ void NetworkModelItem::setVpnType(const QString &type)
m_vpnType = type;
}
qulonglong NetworkModelItem::rxBytes() const
{
return m_rxBytes;
}
void NetworkModelItem::setRxBytes(qulonglong bytes)
{
m_rxBytes = bytes;
}
qulonglong NetworkModelItem::txBytes() const
{
return m_txBytes;
}
void NetworkModelItem::setTxBytes(qulonglong bytes)
{
m_txBytes = bytes;
}
bool NetworkModelItem::operator==(const NetworkModelItem *item) const
{
if (!item->uuid().isEmpty() && !uuid().isEmpty()) {
......
......@@ -108,6 +108,12 @@ public:
QString vpnType() const;
void setVpnType(const QString &type);
qulonglong rxBytes() const;
void setRxBytes(qulonglong bytes);
qulonglong txBytes() const;
void setTxBytes(qulonglong bytes);
bool operator==(const NetworkModelItem *item) const;
public Q_SLOTS:
......@@ -137,6 +143,8 @@ private:
QString m_uuid;
QString m_vpnType;
NetworkManager::VpnConnection::State m_vpnState;
qulonglong m_rxBytes;
qulonglong m_txBytes;
};
#endif // PLASMA_NM_MODEL_NETWORK_MODEL_ITEM_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment