diff --git a/daemon/powerdevilcore.h b/daemon/powerdevilcore.h --- a/daemon/powerdevilcore.h +++ b/daemon/powerdevilcore.h @@ -97,6 +97,7 @@ private: void registerActionTimeout(Action *action, int timeout); void unregisterActionTimeouts(Action *action); + void handleLowBattery(int percent); void handleCriticalBattery(int percent); /** @@ -122,6 +123,7 @@ QHash m_peripheralBatteriesPercent; QHash m_batteriesCharged; + QPointer m_lowBatteryNotification; QTimer *m_criticalBatteryTimer; QPointer m_criticalBatteryNotification; diff --git a/daemon/powerdevilcore.cpp b/daemon/powerdevilcore.cpp --- a/daemon/powerdevilcore.cpp +++ b/daemon/powerdevilcore.cpp @@ -228,6 +228,18 @@ // Config reloaded Q_EMIT configurationReloaded(); + + // Check if critical threshold might have changed and cancel the timer if necessary. + if (m_criticalBatteryTimer->isActive() && currentChargePercent() > PowerDevilSettings::batteryCriticalLevel()) { + m_criticalBatteryTimer->stop(); + if (m_criticalBatteryNotification) { + m_criticalBatteryNotification->close(); + } + } + + if (m_lowBatteryNotification && currentChargePercent() > PowerDevilSettings::batteryLowLevel()) { + m_lowBatteryNotification->close(); + } } QString Core::currentProfile() const @@ -447,6 +459,10 @@ // If a new battery has been added, let's clear some pending suspend actions if the new global batteries percentage is // higher than the battery critical level. (See bug 329537) + if (m_lowBatteryNotification && currentChargePercent() > PowerDevilSettings::batteryLowLevel()) { + m_lowBatteryNotification->close(); + } + if (m_criticalBatteryTimer->isActive() && currentChargePercent() > PowerDevilSettings::batteryCriticalLevel()) { m_criticalBatteryTimer->stop(); if (m_criticalBatteryNotification) { @@ -569,13 +585,26 @@ return true; } else if (currentPercent <= PowerDevilSettings::batteryLowLevel() && previousPercent > PowerDevilSettings::batteryLowLevel()) { - emitRichNotification(QStringLiteral("lowbattery"), i18n("Battery Low (%1% Remaining)", currentPercent), - i18n("Battery running low - to continue using your computer, plug it in or shut it down and change the battery.")); + handleLowBattery(currentPercent); return true; } return false; } +void Core::handleLowBattery(int percent) +{ + if (m_lowBatteryNotification) { + return; + } + + m_lowBatteryNotification = new KNotification(QStringLiteral("lowbattery"), KNotification::Persistent, nullptr); + m_lowBatteryNotification->setComponentName(QStringLiteral("powerdevil")); + m_lowBatteryNotification->setTitle(i18n("Battery Low (%1% Remaining)", percent)); + m_lowBatteryNotification->setText(i18n("Battery running low - to continue using your computer, plug it in or shut it down and change the battery.")); + m_lowBatteryNotification->setUrgency(KNotification::CriticalUrgency); + m_lowBatteryNotification->sendEvent(); +} + void Core::handleCriticalBattery(int percent) { // no parent, but it won't leak, since it will be closed both in case of timeout or direct action @@ -624,6 +653,10 @@ if (state == BackendInterface::Plugged) { // If the AC Adaptor has been plugged in, let's clear some pending suspend actions + if (m_lowBatteryNotification) { + m_lowBatteryNotification->close(); + } + if (m_criticalBatteryTimer->isActive()) { m_criticalBatteryTimer->stop(); if (m_criticalBatteryNotification) {