Lomiri
Loading...
Searching...
No Matches
BatteryMonitor.cpp
1/*
2 * Copyright (C) 2023 UBports Foundation
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors: Muhammad
17 */
18
19#include "BatteryMonitor.h"
20
21BatteryMonitor::BatteryMonitor()
22{
23 QDBusConnection::systemBus().connect("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(propertiesChanged(QString, QVariantMap, QStringList)));
24 m_iface = new QDBusInterface("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.DBus.Properties", QDBusConnection::systemBus());
25}
26
27bool BatteryMonitor::hasBattery()
28{
29 QDBusReply<QDBusVariant> reply;
30 uint state;
31
32 reply = m_iface->call(GET, UPOWER_PROPERTIES, "Type");
33 state = reply.value().variant().toUInt();
34
35 if (state == ON_BATTERY) {
36 reply = m_iface->call(GET, UPOWER_PROPERTIES, "PowerSupply");
37 if (reply.value().variant().toBool())
38 return true;
39 else
40 return false;
41 } else
42 return false;
43}
44
45uint BatteryMonitor::state()
46{
47 if (!hasBattery())
48 return UNKNOWN;
49
50 QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES, "State");
51 return reply.value().variant().toUInt();
52}
53
54bool BatteryMonitor::charging()
55{
56 return state() == CHARGING ? true : false;
57}
58
59bool BatteryMonitor::isFullyCharged()
60{
61 if (state() == FULLY_CHARGED)
62 return true;
63
64 QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES, "Percentage");
65 float percentage = reply.value().variant().toFloat();
66
67 if (percentage == 100.0 && charging())
68 return true;
69 else
70 return false;
71}
72
73qint64 BatteryMonitor::timeToFull()
74{
75 if (!hasBattery())
76 return NO_BATTERY;
77
78 QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES, "TimeToFull");
79 if (reply.isValid() && charging()) {
80 uint value = reply.value().variant().toUInt();
81 if (value == 0)
82 return NO_TIMETOFULL;
83
84 return value;
85 }
86
87 return NO_TIMETOFULL;
88}
89
90void BatteryMonitor::propertiesChanged(QString string, QVariantMap map, QStringList list)
91{
92 Q_UNUSED(string)
93 Q_UNUSED(list)
94
95 if (map.contains("State"))
96 Q_EMIT chargingChanged();
97
98 if (map.contains("TimeToFull") && map.contains("Percentage") && charging())
99 Q_EMIT timeToFullChanged();
100
101 if (map.contains("State") || map.contains("Percentage"))
102 Q_EMIT fullyChargedChanged();
103}