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
|
/*
* Copyright (C) 2023 Muhammad <muhammad23012009@hotmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BatteryMonitor.h"
BatteryMonitor::BatteryMonitor()
{
QDBusConnection::systemBus().connect("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(propertiesChanged(QString, QVariantMap, QStringList)));
m_iface = new QDBusInterface("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.DBus.Properties", QDBusConnection::systemBus());
}
bool BatteryMonitor::hasBattery()
{
QDBusReply<QDBusVariant> reply;
uint state;
reply = m_iface->call(GET, UPOWER_PROPERTIES, "Type");
state = reply.value().variant().toUInt();
if (state == ON_BATTERY) {
reply = m_iface->call(GET, UPOWER_PROPERTIES, "PowerSupply");
if (reply.value().variant().toBool())
return true;
else
return false;
} else
return false;
}
uint BatteryMonitor::state()
{
if (!hasBattery())
return UNKNOWN;
QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES, "State");
return reply.value().variant().toUInt();
}
bool BatteryMonitor::charging()
{
return state() == CHARGING ? true : false;
}
bool BatteryMonitor::isFullyCharged()
{
if (state() == FULLY_CHARGED)
return true;
QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES, "Percentage");
float percentage = reply.value().variant().toFloat();
if (percentage == 100.0 && charging())
return true;
else
return false;
}
qint64 BatteryMonitor::timeToFull()
{
if (!hasBattery())
return NO_BATTERY;
QDBusReply<QDBusVariant> reply = m_iface->call(GET, UPOWER_PROPERTIES, "TimeToFull");
if (reply.isValid() && charging()) {
uint value = reply.value().variant().toUInt();
if (value == 0)
return NO_TIMETOFULL;
return value;
}
return NO_TIMETOFULL;
}
void BatteryMonitor::propertiesChanged(QString string, QVariantMap map, QStringList list)
{
Q_UNUSED(string)
Q_UNUSED(list)
if (map.contains("State"))
Q_EMIT chargingChanged();
if (map.contains("TimeToFull") && map.contains("Percentage") && charging())
Q_EMIT timeToFullChanged();
if (map.contains("State") || map.contains("Percentage"))
Q_EMIT fullyChargedChanged();
}
|