File indexing completed on 2024-04-28 05:36:17

0001 /*
0002     SPDX-FileCopyrightText: 2023 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "upowerdevice.h"
0008 #include <powerdevil_debug.h>
0009 
0010 #include <QDBusConnection>
0011 #include <QDBusMessage>
0012 #include <QDBusPendingReply>
0013 
0014 #define UPOWER_SERVICE "org.freedesktop.UPower"
0015 #define UPOWER_IFACE_DEVICE "org.freedesktop.UPower.Device"
0016 
0017 UPowerDevice::UPowerDevice(const QString &dbusObjectPath)
0018 {
0019     qCDebug(POWERDEVIL) << "Creating UPowerDevice for" << dbusObjectPath;
0020     QDBusConnection::systemBus().connect(UPOWER_SERVICE,
0021                                          dbusObjectPath,
0022                                          QStringLiteral("org.freedesktop.DBus.Properties"),
0023                                          QStringLiteral("PropertiesChanged"),
0024                                          this,
0025                                          SLOT(onPropertiesChanged(QString, QVariantMap, QStringList)));
0026 
0027     auto call = QDBusMessage::createMethodCall(UPOWER_SERVICE, dbusObjectPath, QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("GetAll"));
0028     call << QStringLiteral(UPOWER_IFACE_DEVICE);
0029 
0030     QDBusPendingReply<QVariantMap> reply = QDBusConnection::systemBus().asyncCall(call);
0031     reply.waitForFinished();
0032 
0033     if (reply.isValid()) {
0034         updateProperties(reply.value());
0035     }
0036 }
0037 
0038 void UPowerDevice::onPropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps)
0039 {
0040     Q_UNUSED(invalidatedProps);
0041     if (ifaceName != UPOWER_IFACE_DEVICE) {
0042         return;
0043     }
0044 
0045     if (updateProperties(changedProps)) {
0046         Q_EMIT propertiesChanged();
0047     }
0048 }
0049 
0050 bool UPowerDevice::updateProperties(const QVariantMap &properties)
0051 {
0052     qCDebug(POWERDEVIL) << "Update:" << properties;
0053     // Set whan any properties affecting charging state are updated
0054     bool updated = false;
0055 
0056     for (auto it = properties.begin(); it != properties.end(); it++) {
0057         if (it.key() == "UpdateTime") {
0058             m_updateTime = it.value().toULongLong();
0059         } else if (it.key() == "Energy") {
0060             m_energy = it.value().toDouble();
0061             updated = true;
0062         } else if (it.key() == "EnergyRate") {
0063             m_energyRate = it.value().toDouble();
0064             updated = true;
0065         } else if (it.key() == "EnergyFull") {
0066             m_energyFull = it.value().toDouble();
0067             updated = true;
0068         } else if (it.key() == "State") {
0069             auto state = it.value().toInt();
0070             m_state = (state == 1) ? State::Charging : (state == 2) ? State::Discharging : (state == 4) ? State::FullyCharged : State::Unknown;
0071             updated = true;
0072         } else if (it.key() == "Type") {
0073             auto type = it.value().toInt();
0074             m_type = (type == 2) ? Type::Battery : (type == 3) ? Type::Ups : Type::Unknown;
0075             updated = true;
0076         } else if (it.key() == "PowerSupply") {
0077             m_isPowerSupply = it.value().toBool();
0078             updated = true;
0079         } else if (it.key() == "IsPresent") {
0080             m_isPresent = it.value().toBool();
0081             updated = true;
0082         }
0083     }
0084 
0085     return updated;
0086 }
0087 
0088 #include "moc_upowerdevice.cpp"