Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a notification when the battery is present, not absent #382

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions src/batterywatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,31 @@ BatteryWatcher::BatteryWatcher(QObject *parent) : Watcher(parent)
{
const QList<Solid::Device> 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<Solid::Battery>();
if (battery->type() != Solid::Battery::PrimaryBattery)
{
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);
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions src/batterywatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ private slots:
void setPause(TrayIcon::PAUSE duration);

private:
void chargeLevelAndStatus(bool &discharging, double &chargeLevel);

QList<Solid::Battery*> mBatteries;
QList<TrayIcon*> mTrayIcons;
QTimer mPauseTimer;
Expand Down