File indexing completed on 2024-04-14 03:57:42

0001 /*
0002     SPDX-FileCopyrightText: 2017 Jan Grulich <jgrulich@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "devicestatistics_p.h"
0008 #include "manager_p.h"
0009 #include "nmdebug.h"
0010 
0011 NetworkManager::DeviceStatisticsPrivate::DeviceStatisticsPrivate(const QString &path, DeviceStatistics *q)
0012 #ifdef NMQT_STATIC
0013     : iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::sessionBus())
0014 #else
0015     : iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::systemBus())
0016 #endif
0017     , refreshRateMs(0)
0018     , rxBytes(0)
0019     , txBytes(0)
0020     , q_ptr(q)
0021 {
0022     uni = path;
0023 }
0024 
0025 NetworkManager::DeviceStatistics::DeviceStatistics(const QString &path, QObject *parent)
0026     : QObject(parent)
0027     , d_ptr(new DeviceStatisticsPrivate(path, this))
0028 {
0029     Q_D(DeviceStatistics);
0030 
0031     // Refresh rate by default is 0,
0032     // as soon as the refresh rate is changed, we'll get the rest of properties initialised
0033 
0034     // Get all DeviceStatistics's properties at once
0035     QDBusConnection::systemBus().connect(NetworkManagerPrivate::DBUS_SERVICE,
0036                                          d->uni,
0037                                          NetworkManagerPrivate::FDO_DBUS_PROPERTIES,
0038                                          QLatin1String("PropertiesChanged"),
0039                                          d,
0040                                          SLOT(dbusPropertiesChanged(QString, QVariantMap, QStringList)));
0041 }
0042 
0043 NetworkManager::DeviceStatistics::~DeviceStatistics()
0044 {
0045     Q_D(DeviceStatistics);
0046     delete d;
0047 }
0048 
0049 uint NetworkManager::DeviceStatistics::refreshRateMs() const
0050 {
0051     Q_D(const DeviceStatistics);
0052     return d->refreshRateMs;
0053 }
0054 
0055 void NetworkManager::DeviceStatistics::setRefreshRateMs(uint refreshRate)
0056 {
0057     Q_D(DeviceStatistics);
0058 
0059     // HACK calling d->iface.setRefreshRateMs does a blocking DBus call as internally it does
0060     // setProperty which returns whether the call succeeded, so Qt waits for it.
0061     // Since this can occasionally take a quite a while, this is replaced with a manual DBus call.
0062 
0063     QDBusMessage message = QDBusMessage::createMethodCall(NetworkManager::NetworkManagerPrivate::DBUS_SERVICE,
0064                                                           d->iface.path(),
0065                                                           NetworkManager::NetworkManagerPrivate::FDO_DBUS_PROPERTIES,
0066                                                           QLatin1String("Set"));
0067     message << d->iface.staticInterfaceName() << QLatin1String("RefreshRateMs") << QVariant::fromValue(QDBusVariant(refreshRate));
0068 
0069     d->iface.connection().call(message, QDBus::NoBlock);
0070 }
0071 
0072 qulonglong NetworkManager::DeviceStatistics::rxBytes() const
0073 {
0074     Q_D(const DeviceStatistics);
0075     return d->rxBytes;
0076 }
0077 
0078 qulonglong NetworkManager::DeviceStatistics::txBytes() const
0079 {
0080     Q_D(const DeviceStatistics);
0081     return d->txBytes;
0082 }
0083 
0084 void NetworkManager::DeviceStatisticsPrivate::dbusPropertiesChanged(const QString &interfaceName,
0085                                                                     const QVariantMap &properties,
0086                                                                     const QStringList &invalidatedProperties)
0087 {
0088     Q_UNUSED(invalidatedProperties);
0089     if (interfaceName == QLatin1String("org.freedesktop.NetworkManager.Device.Statistics")) {
0090         propertiesChanged(properties);
0091     }
0092 }
0093 
0094 void NetworkManager::DeviceStatisticsPrivate::propertiesChanged(const QVariantMap &properties)
0095 {
0096     Q_Q(DeviceStatistics);
0097 
0098     // qCDebug(NMQT) << Q_FUNC_INFO << properties;
0099 
0100     QVariantMap::const_iterator it = properties.constBegin();
0101     while (it != properties.constEnd()) {
0102         const QString property = it.key();
0103         if (property == QLatin1String("RefreshRateMs")) {
0104             refreshRateMs = it->toUInt();
0105             Q_EMIT q->refreshRateMsChanged(refreshRateMs);
0106         } else if (property == QLatin1String("RxBytes")) {
0107             rxBytes = it->toULongLong();
0108             Q_EMIT q->rxBytesChanged(rxBytes);
0109         } else if (property == QLatin1String("TxBytes")) {
0110             txBytes = it->toULongLong();
0111             Q_EMIT q->txBytesChanged(txBytes);
0112         } else {
0113             qCWarning(NMQT) << Q_FUNC_INFO << "Unhandled property" << property;
0114         }
0115         ++it;
0116     }
0117 }
0118 
0119 #include "moc_devicestatistics.cpp"
0120 #include "moc_devicestatistics_p.cpp"