Lomiri
Loading...
Searching...
No Matches
BatteryMonitor.cpp
1/*
2 * Copyright (C) 2023 Muhammad <muhammad23012009@hotmail.com>
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
17#include "BatteryMonitor.h"
18
19BatteryMonitor::BatteryMonitor()
20{
21 QDBusConnection::systemBus().connect("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(propertiesChanged(QString, QVariantMap, QStringList)));
22 m_iface = new QDBusInterface("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.DBus.Properties", QDBusConnection::systemBus());
23}
24
25bool BatteryMonitor::hasBattery()
26{
27 QDBusReply<QDBusVariant> reply;
28 uint state;
29
30 reply = m_iface->call(GET, UPOWER_PROPERTIES, "Type");
31 state = reply.value().variant().toUInt();
32
33 if (state == ON_BATTERY) {
34 reply = m_iface->call(GET, UPOWER_PROPERTIES, "PowerSupply");
35 if (reply.value().variant().toBool())
36 return true;
37 else
38 return false;
39 } else
40 return false;
41}
42
43uint BatteryMonitor::state()
44{
45 if (!hasBattery())
46 return UNKNOWN;
47
48 QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES, "State");
49 return reply.value().variant().toUInt();
50}
51
52bool BatteryMonitor::charging()
53{
54 return state() == CHARGING ? true : false;
55}
56
57bool BatteryMonitor::isFullyCharged()
58{
59 if (state() == FULLY_CHARGED)
60 return true;
61
62 QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES, "Percentage");
63 float percentage = reply.value().variant().toFloat();
64
65 if (percentage == 100.0 && charging())
66 return true;
67 else
68 return false;
69}
70
71qint64 BatteryMonitor::timeToFull()
72{
73 if (!hasBattery())
74 return NO_BATTERY;
75
76 QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES, "TimeToFull");
77 if (reply.isValid() && charging()) {
78 uint value = reply.value().variant().toUInt();
79 if (value == 0)
80 return NO_TIMETOFULL;
81
82 return value;
83 }
84
85 return NO_TIMETOFULL;
86}
87
88void BatteryMonitor::propertiesChanged(QString string, QVariantMap map, QStringList list)
89{
90 Q_UNUSED(string)
91 Q_UNUSED(list)
92
93 if (map.contains("State"))
94 Q_EMIT chargingChanged();
95
96 if (map.contains("TimeToFull") && map.contains("Percentage") && charging())
97 Q_EMIT timeToFullChanged();
98
99 if (map.contains("State") || map.contains("Percentage"))
100 Q_EMIT fullyChargedChanged();
101}