File: upowerdevice.cpp

package info (click to toggle)
powerdevil 4%3A6.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,680 kB
  • sloc: cpp: 13,284; xml: 1,911; python: 1,204; sh: 19; makefile: 10
file content (88 lines) | stat: -rw-r--r-- 3,257 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
    SPDX-FileCopyrightText: 2023 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>

    SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include "upowerdevice.h"
#include <powerdevil_debug.h>

#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingReply>

inline constexpr QLatin1StringView UPOWER_SERVICE("org.freedesktop.UPower");
inline constexpr QLatin1StringView UPOWER_IFACE_DEVICE("org.freedesktop.UPower.Device");

UPowerDevice::UPowerDevice(const QString &dbusObjectPath)
{
    qCDebug(POWERDEVIL) << "Creating UPowerDevice for" << dbusObjectPath;
    QDBusConnection::systemBus().connect(UPOWER_SERVICE,
                                         dbusObjectPath,
                                         QStringLiteral("org.freedesktop.DBus.Properties"),
                                         QStringLiteral("PropertiesChanged"),
                                         this,
                                         SLOT(onPropertiesChanged(QString, QVariantMap, QStringList)));

    auto call = QDBusMessage::createMethodCall(UPOWER_SERVICE, dbusObjectPath, QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("GetAll"));
    call << UPOWER_IFACE_DEVICE;

    QDBusPendingReply<QVariantMap> reply = QDBusConnection::systemBus().asyncCall(call);
    reply.waitForFinished();

    if (reply.isValid()) {
        updateProperties(reply.value());
    }
}

void UPowerDevice::onPropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps)
{
    Q_UNUSED(invalidatedProps);
    if (ifaceName != UPOWER_IFACE_DEVICE) {
        return;
    }

    if (updateProperties(changedProps)) {
        Q_EMIT propertiesChanged();
    }
}

bool UPowerDevice::updateProperties(const QVariantMap &properties)
{
    qCDebug(POWERDEVIL) << "Update:" << properties;
    // Set whan any properties affecting charging state are updated
    bool updated = false;

    for (auto it = properties.begin(); it != properties.end(); it++) {
        if (it.key() == u"UpdateTime") {
            m_updateTime = it.value().toULongLong();
        } else if (it.key() == u"Energy") {
            m_energy = it.value().toDouble();
            updated = true;
        } else if (it.key() == u"EnergyRate") {
            m_energyRate = it.value().toDouble();
            updated = true;
        } else if (it.key() == u"EnergyFull") {
            m_energyFull = it.value().toDouble();
            updated = true;
        } else if (it.key() == u"State") {
            auto state = it.value().toInt();
            m_state = (state == 1) ? State::Charging : (state == 2) ? State::Discharging : (state == 4) ? State::FullyCharged : State::Unknown;
            updated = true;
        } else if (it.key() == u"Type") {
            auto type = it.value().toInt();
            m_type = (type == 2) ? Type::Battery : (type == 3) ? Type::Ups : Type::Unknown;
            updated = true;
        } else if (it.key() == u"PowerSupply") {
            m_isPowerSupply = it.value().toBool();
            updated = true;
        } else if (it.key() == u"IsPresent") {
            m_isPresent = it.value().toBool();
            updated = true;
        }
    }

    return updated;
}

#include "moc_upowerdevice.cpp"