diff --git a/src/batterywatcher.cpp b/src/batterywatcher.cpp index cca542a4..dc805afa 100644 --- a/src/batterywatcher.cpp +++ b/src/batterywatcher.cpp @@ -43,13 +43,6 @@ BatteryWatcher::BatteryWatcher(QObject *parent) : Watcher(parent) { 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 *parent) : Watcher(parent) { 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 : qAsConst(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 : qAsConst(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()) diff --git a/src/batterywatcher.h b/src/batterywatcher.h index 60b4bfce..b04d30d6 100644 --- 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;