1/*
2 SPDX-FileCopyrightText: 2017 Jan Grulich <jgrulich@redhat.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "devicestatistics_p.h"
8#include "manager_p.h"
9#include "nmdebug.h"
10
11NetworkManager::DeviceStatisticsPrivate::DeviceStatisticsPrivate(const QString &path, DeviceStatistics *q)
12#ifdef NMQT_STATIC
13 : iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::sessionBus())
14#else
15 : iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::systemBus())
16#endif
17 , refreshRateMs(0)
18 , rxBytes(0)
19 , txBytes(0)
20 , q_ptr(q)
21{
22 uni = path;
23}
24
25NetworkManager::DeviceStatistics::DeviceStatistics(const QString &path, QObject *parent)
26 : QObject(parent)
27 , d_ptr(new DeviceStatisticsPrivate(path, this))
28{
29 Q_D(DeviceStatistics);
30
31 // Refresh rate by default is 0,
32 // as soon as the refresh rate is changed, we'll get the rest of properties initialised
33
34 // Get all DeviceStatistics's properties at once
35 QDBusConnection::systemBus().connect(service: NetworkManagerPrivate::DBUS_SERVICE,
36 path: d->uni,
37 interface: NetworkManagerPrivate::FDO_DBUS_PROPERTIES,
38 name: QLatin1String("PropertiesChanged"),
39 receiver: d,
40 SLOT(dbusPropertiesChanged(QString, QVariantMap, QStringList)));
41}
42
43NetworkManager::DeviceStatistics::~DeviceStatistics()
44{
45 Q_D(DeviceStatistics);
46 delete d;
47}
48
49uint NetworkManager::DeviceStatistics::refreshRateMs() const
50{
51 Q_D(const DeviceStatistics);
52 return d->refreshRateMs;
53}
54
55void NetworkManager::DeviceStatistics::setRefreshRateMs(uint refreshRate)
56{
57 Q_D(DeviceStatistics);
58
59 // HACK calling d->iface.setRefreshRateMs does a blocking DBus call as internally it does
60 // setProperty which returns whether the call succeeded, so Qt waits for it.
61 // Since this can occasionally take a quite a while, this is replaced with a manual DBus call.
62
63 QDBusMessage message = QDBusMessage::createMethodCall(destination: NetworkManager::NetworkManagerPrivate::DBUS_SERVICE,
64 path: d->iface.path(),
65 interface: NetworkManager::NetworkManagerPrivate::FDO_DBUS_PROPERTIES,
66 method: QLatin1String("Set"));
67 message << d->iface.staticInterfaceName() << QLatin1String("RefreshRateMs") << QVariant::fromValue(value: QDBusVariant(refreshRate));
68
69 d->iface.connection().call(message, mode: QDBus::NoBlock);
70}
71
72qulonglong NetworkManager::DeviceStatistics::rxBytes() const
73{
74 Q_D(const DeviceStatistics);
75 return d->rxBytes;
76}
77
78qulonglong NetworkManager::DeviceStatistics::txBytes() const
79{
80 Q_D(const DeviceStatistics);
81 return d->txBytes;
82}
83
84void NetworkManager::DeviceStatisticsPrivate::dbusPropertiesChanged(const QString &interfaceName,
85 const QVariantMap &properties,
86 const QStringList &invalidatedProperties)
87{
88 Q_UNUSED(invalidatedProperties);
89 if (interfaceName == QLatin1String("org.freedesktop.NetworkManager.Device.Statistics")) {
90 propertiesChanged(properties);
91 }
92}
93
94void NetworkManager::DeviceStatisticsPrivate::propertiesChanged(const QVariantMap &properties)
95{
96 Q_Q(DeviceStatistics);
97
98 // qCDebug(NMQT) << Q_FUNC_INFO << properties;
99
100 QVariantMap::const_iterator it = properties.constBegin();
101 while (it != properties.constEnd()) {
102 const QString property = it.key();
103 if (property == QLatin1String("RefreshRateMs")) {
104 refreshRateMs = it->toUInt();
105 Q_EMIT q->refreshRateMsChanged(refreshRate: refreshRateMs);
106 } else if (property == QLatin1String("RxBytes")) {
107 rxBytes = it->toULongLong();
108 Q_EMIT q->rxBytesChanged(rxBytes);
109 } else if (property == QLatin1String("TxBytes")) {
110 txBytes = it->toULongLong();
111 Q_EMIT q->txBytesChanged(txBytes);
112 } else {
113 qCWarning(NMQT) << Q_FUNC_INFO << "Unhandled property" << property;
114 }
115 ++it;
116 }
117}
118
119#include "moc_devicestatistics.cpp"
120#include "moc_devicestatistics_p.cpp"
121

source code of networkmanager-qt/src/devicestatistics.cpp