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