Add remaining time to battery indicator

Add a QDBusConnection to org.freedesktop.UPower to DBusPower in
dbuspower.cpp.

Add a QDBusInterface (m_uPowerInter) to PowerPlugin in powerplugin.h to
retrieve the correct battery device (calling 'EnumerateDevices'). Add a
QDBusInterface (m_uBatteryDeviceInter) to PowerPlugin in powerplugin.h
to gather battery time to empty and to fully recharged, and display it
in the power widget tip.

Add BatteryState enum in powerplugin.h to replace BATTERY_DISCHARED and
BATTERY_FULL macros, and add support for all the battery states provided
by UPower.

Change-Id: Ia4677298a1d13cab1e1a03035dbe4a2e660f994b
This commit is contained in:
Carlo Alberto Barbano 2019-05-02 19:08:17 +02:00
parent 73b39e6846
commit b6fa4335eb
3 changed files with 74 additions and 8 deletions

View File

@ -24,10 +24,12 @@ DBusPower::DBusPower(QObject *parent)
qDBusRegisterMetaType<BatteryPercentageMap>();
QDBusConnection::sessionBus().connect(this->service(), this->path(), "org.freedesktop.DBus.Properties", "PropertiesChanged","sa{sv}as", this, SLOT(__propertyChanged__(QDBusMessage)));
QDBusConnection::systemBus().connect("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(__propertyChanged__(QDBusMessage)));
}
DBusPower::~DBusPower()
{
QDBusConnection::sessionBus().disconnect(service(), path(), "org.freedesktop.DBus.Properties", "PropertiesChanged", "sa{sv}as", this, SLOT(propertyChanged(QDBusMessage)));
QDBusConnection::systemBus().disconnect("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(propertyChanged(QDBusMessage)));
}

View File

@ -30,10 +30,46 @@ PowerPlugin::PowerPlugin(QObject *parent)
: QObject(parent),
m_pluginLoaded(false),
m_tipsLabel(new TipsWidget)
m_tipsLabel(new TipsWidget),
m_uPowerInter(new QDBusInterface("org.freedesktop.UPower",
"/org/freedesktop/UPower",
"org.freedesktop.UPower",
QDBusConnection::systemBus())),
m_uBatteryDeviceInter(nullptr)
{
m_tipsLabel->setVisible(false);
m_tipsLabel->setObjectName("power");
if (!m_uPowerInter->isValid()) {
qDebug() << "DBusConnection to org.freedesktop.UPower is invalid";
return;
}
QDBusReply<QList<QDBusObjectPath>> reply = m_uPowerInter->call("EnumerateDevices");
QList<QDBusObjectPath> paths = reply.value();
QDBusObjectPath batteryPath;
foreach(auto objectPath, paths) {
qDebug() << "EnumerateDevices: " << objectPath.path();
if (objectPath.path().contains("battery")) {
batteryPath = objectPath;
break;
}
}
if (batteryPath.path().isEmpty())
return;
m_uBatteryDeviceInter = new QDBusInterface(
"org.freedesktop.UPower",
batteryPath.path(),
"org.freedesktop.UPower.Device",
QDBusConnection::systemBus()
);
if(!m_uBatteryDeviceInter->isValid()) {
qDebug() << QString("DBusConnection to %1 is invalid").arg(batteryPath.path());
}
}
const QString PowerPlugin::pluginName() const
@ -67,15 +103,32 @@ QWidget *PowerPlugin::itemTipsWidget(const QString &itemKey)
const uint percentage = qMin(100.0, qMax(0.0, data.value("Display")));
const QString value = QString("%1%").arg(std::round(percentage));
const bool charging = !m_powerInter->onBattery();
if (!charging) {
m_tipsLabel->setText(tr("Remaining Capacity %1").arg(value));
qint64 timeToEmpty = -1;
if(m_uBatteryDeviceInter && m_uBatteryDeviceInter->property("TimeToEmpty").isValid())
timeToEmpty = m_uBatteryDeviceInter->property("TimeToEmpty").toInt();
m_tipsLabel->setText(
tr("Remaining Capacity: %1, %2 Until Empty")
.arg(value)
.arg(QDateTime::fromTime_t(timeToEmpty).toUTC().toString("hh:mm:ss"))
);
} else {
const int batteryState = m_powerInter->batteryState()["Display"];
if (batteryState == BATTERY_FULL || percentage == 100.)
m_tipsLabel->setText(tr("Charged %1").arg(value));
else
m_tipsLabel->setText(tr("Charging %1").arg(value));
if (batteryState == BatteryState::FULLY_CHARGED || percentage == 100.)
m_tipsLabel->setText(tr("Charged %1 Battery Is Charged").arg(value));
else {
qint64 timeToFull = -1;
if(m_uBatteryDeviceInter && m_uBatteryDeviceInter->property("TimeToFull").isValid())
timeToFull = m_uBatteryDeviceInter->property("TimeToFull").toInt();
m_tipsLabel->setText(
tr("Charging %1, %2 Until Full")
.arg(value)
.arg(QDateTime::fromTime_t(timeToFull).toUTC().toString("hh:mm:ss"))
);
}
}
return m_tipsLabel;

View File

@ -29,8 +29,17 @@
#include <QLabel>
#define BATTERY_DISCHARED 2
#define BATTERY_FULL 4
// from https://upower.freedesktop.org/docs/Device.html#Device:State
enum BatteryState
{
UNKOWN = 0,
CHARGING = 1,
DISCHARGING = 2,
EMPTY = 3,
FULLY_CHARGED = 4,
PENDING_CHARGE = 5,
PENDING_DISCHARGE = 6
};
class PowerPlugin : public QObject, PluginsItemInterface
{
@ -69,6 +78,8 @@ private:
TipsWidget *m_tipsLabel;
DBusPower *m_powerInter;
QDBusInterface *m_uPowerInter;
QDBusInterface *m_uBatteryDeviceInter;
};
#endif // POWERPLUGIN_H