1 #include "BatteryMonitor.h"
3 BatteryMonitor::BatteryMonitor()
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());
9 bool BatteryMonitor::hasBattery()
11 QDBusReply<QDBusVariant> reply;
14 reply = m_iface->call(GET, UPOWER_PROPERTIES,
"Type");
15 state = reply.value().variant().toUInt();
17 if (state == ON_BATTERY) {
18 reply = m_iface->call(GET, UPOWER_PROPERTIES,
"PowerSupply");
19 if (reply.value().variant().toBool())
27 uint BatteryMonitor::state()
32 QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES,
"State");
33 return reply.value().variant().toUInt();
36 bool BatteryMonitor::charging()
38 if (state() == CHARGING)
44 qint64 BatteryMonitor::timeToFull()
49 QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES,
"TimeToFull");
50 if (reply.isValid() && charging()) {
51 uint value = reply.value().variant().toUInt();
61 void BatteryMonitor::propertiesChanged(QString
string, QVariantMap map, QStringList list)
65 if (map.contains(
"State"))
66 Q_EMIT chargingChanged();
67 else if (map.contains(
"TimeToFull") && map.contains(
"Percentage") && charging())
68 Q_EMIT timeToFullChanged();