File: upowermanager.cpp

package info (click to toggle)
cutefish-core 0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,076 kB
  • sloc: cpp: 11,327; xml: 443; sh: 29; makefile: 6
file content (115 lines) | stat: -rw-r--r-- 4,056 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
    SPDX-FileCopyrightText: 2020 Reven Martin <revenmartin@gmail.com>
    SPDX-FileCopyrightText: 2010 Michael Zanetti <mzanetti@kde.org>
    SPDX-FileCopyrightText: 2010 Lukas Tinkl <ltinkl@redhat.com>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "upowermanager.h"
#include "upowerdevice.h"
#include "battery.h"
#include "power.h"

#include <QDBusReply>
#include <QDebug>
#include <QDBusMetaType>
#include <QDBusConnectionInterface>

UPowerManager::UPowerManager(QObject *parent)
    : QObject(parent)
    , m_interface(UP_DBUS_SERVICE,
                  UP_DBUS_PATH,
                  UP_DBUS_INTERFACE,
                  QDBusConnection::systemBus())
    , m_onBattery(false)
{
    qDBusRegisterMetaType<QList<QDBusObjectPath>>();
    qDBusRegisterMetaType<QVariantMap>();

    bool serviceFound = m_interface.isValid();
    if (!serviceFound) {
        // find out whether it will be activated automatically
        QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.DBus",
                               "/org/freedesktop/DBus",
                               "org.freedesktop.DBus",
                               "ListActivatableNames");

        QDBusReply<QStringList> reply = QDBusConnection::systemBus().call(message);
        if (reply.isValid() && reply.value().contains(UP_DBUS_SERVICE)) {
            QDBusConnection::systemBus().interface()->startService(UP_DBUS_SERVICE);
            serviceFound = true;
        }
    }

    if (serviceFound) {
        if (m_interface.metaObject()->indexOfSignal("DeviceAdded(QDBusObjectPath)") != -1) {
            // for UPower >= 0.99.0, changed signature :o/
            connect(&m_interface, SIGNAL(DeviceAdded(QDBusObjectPath)),
                    this, SLOT(onDeviceAdded(QDBusObjectPath)));
            connect(&m_interface, SIGNAL(DeviceRemoved(QDBusObjectPath)),
                    this, SLOT(onDeviceRemoved(QDBusObjectPath)));
        } else {
            connect(&m_interface, SIGNAL(DeviceAdded(QString)),
                    this, SIGNAL(deviceAdded(QString)));
            connect(&m_interface, SIGNAL(DeviceRemoved(QString)),
                    this, SIGNAL(deviceRemoved(QString)));
        }

        // On battery
        QDBusConnection::systemBus().connect(UP_DBUS_SERVICE, UP_DBUS_PATH,
                                             "org.freedesktop.DBus.Properties",
                                             "PropertiesChanged", this,
                                             SLOT(onPropertiesChanged(QString, QVariantMap, QStringList)));
        m_onBattery = m_interface.property("OnBattery").toBool();

        // All devices
        QDBusReply<QList<QDBusObjectPath>> reply = m_interface.call("EnumerateDevices");
        if (!reply.isValid()) {
            qWarning() << Q_FUNC_INFO << " error: " << reply.error().name();
            return;
        }

        const auto pathList = reply.value();
        for (const QDBusObjectPath &path : pathList) {
            UPowerDevice *device = new UPowerDevice(path.path());
            Battery *battery = new Battery(device);

            if (battery->type() != Battery::PrimaryBattery) {
                device->deleteLater();
                battery->deleteLater();
            }
        }
    }
}

QString UPowerManager::udiPrefix() const
{
    return UP_UDI_PREFIX;
}

bool UPowerManager::onBattery() const
{
    return m_onBattery;
}

void UPowerManager::onPropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps)
{
    Q_UNUSED(ifaceName);
    Q_UNUSED(changedProps);
    Q_UNUSED(invalidatedProps);

    const bool onBattery = m_interface.property("OnBattery").toBool();
    m_onBattery = onBattery;
    emit onBatteryChanged();
}

void UPowerManager::onDeviceAdded(const QDBusObjectPath &path)
{
    qDebug() << "onDeviceAdded()" << path.path();
}

void UPowerManager::onDeviceRemoved(const QDBusObjectPath &path)
{
    qDebug() << "onDeviceRemoved()" << path.path();
}