Description: Show a notification when the battery is present, not absent In virtual machines especially, it is odd to see a message about the battery not being present. This commit moves that message to only display when there is a battery present. Author: Simon Quigley Origin: upstream Forwarded: https://github.com/lxqt/lxqt-powermanagement/pull/382 Last-Update: 2023-10-03 --- This patch header follows DEP-3: http://dep.debian.net/deps/dep3/ --- a/src/batterywatcher.cpp +++ b/src/batterywatcher.cpp @@ -43,13 +43,6 @@ BatteryWatcher::BatteryWatcher(QObject * { const QList devices = Solid::Device::listFromType(Solid::DeviceInterface::Battery, QString()); - if (devices.isEmpty()) - { - LXQt::Notification::notify(tr("No battery!"), - tr("LXQt could not find data about any battery - monitoring disabled"), - QSL("lxqt-powermanagement")); - } - for (Solid::Device device : devices) { Solid::Battery *battery = device.as(); @@ -57,11 +50,24 @@ BatteryWatcher::BatteryWatcher(QObject * { continue; } + mBatteries << battery; connect(battery, &Solid::Battery::energyChanged, this, &BatteryWatcher::batteryChanged); connect(battery, &Solid::Battery::chargeStateChanged, this, &BatteryWatcher::batteryChanged); } + bool discharging; + double chargeLevel; + chargeLevelAndStatus(discharging, chargeLevel); + + if (!devices.isEmpty()) { + QString status = discharging ? QStringLiteral("Discharging") : QStringLiteral("Charging"); + QString message = tr("%1 (%2%)").arg(status).arg(chargeLevel); + LXQt::Notification::notify(tr("Battery Present"), + message, + QSL("lxqt-powermanagement")); + } + mBatteryInfoDialog = new BatteryInfoDialog(mBatteries); connect(&mSettings, &PowerManagementSettings::settingsChanged, this, &BatteryWatcher::settingsChanged); @@ -85,19 +91,10 @@ void BatteryWatcher::batteryChanged() static QTime actionTime; static LXQt::Notification *notification = nullptr; - double totalEnergyFull = 0; - double totalEnergyNow = 0; - bool discharging = true; + bool discharging; double chargeLevel; - for (const Solid::Battery *battery : std::as_const(mBatteries)) - { - totalEnergyFull += battery->energyFull(); - totalEnergyNow += battery->energy(); - discharging &= (battery->chargeState() == Solid::Battery::Discharging); - } - - chargeLevel = 100 * totalEnergyNow / totalEnergyFull; + chargeLevelAndStatus(discharging, chargeLevel); qDebug() << "BatteryChanged" << "discharging:" << discharging @@ -160,6 +157,31 @@ void BatteryWatcher::batteryChanged() } } +void BatteryWatcher::chargeLevelAndStatus(bool &discharging, double &chargeLevel) +{ + double totalEnergyFull = 0; + double totalEnergyNow = 0; + bool batteries = false; + discharging = true; + + for (const Solid::Battery *battery : std::as_const(mBatteries)) + { + batteries = true; + + totalEnergyFull += battery->energyFull(); + totalEnergyNow += battery->energy(); + discharging &= (battery->chargeState() == Solid::Battery::Discharging); + } + + if (!batteries) { + discharging = false; + chargeLevel = 0; + } + else { + chargeLevel = 100 * totalEnergyNow / totalEnergyFull; + } +} + void BatteryWatcher::settingsChanged() { if (!mSettings.isShowIcon()) --- a/src/batterywatcher.h +++ b/src/batterywatcher.h @@ -51,6 +51,8 @@ private slots: void setPause(TrayIcon::PAUSE duration); private: + void chargeLevelAndStatus(bool &discharging, double &chargeLevel); + QList mBatteries; QList mTrayIcons; QTimer mPauseTimer;