Compare commits

...

1 commit

Author SHA1 Message Date
Piyush Aggarwal
2aa97e7da4 plugin: enable Battery plugin on Windows 2023-03-25 01:09:13 +05:30
2 changed files with 28 additions and 29 deletions

View file

@ -7,12 +7,7 @@ add_subdirectory(remotecontrol)
add_subdirectory(remotesystemvolume) add_subdirectory(remotesystemvolume)
add_subdirectory(clipboard) add_subdirectory(clipboard)
add_subdirectory(runcommand) add_subdirectory(runcommand)
if(NOT WIN32)
# https://bugs.kde.org/show_bug.cgi?id=444612
add_subdirectory(battery) add_subdirectory(battery)
endif()
if(NOT WIN32 AND NOT APPLE) if(NOT WIN32 AND NOT APPLE)
add_subdirectory(bigscreen) add_subdirectory(bigscreen)

View file

@ -47,19 +47,19 @@ void BatteryPlugin::connected()
QList<Solid::Device> batteries = Solid::Device::listFromQuery(Solid::Predicate(batteryDevice, QStringLiteral("type"), primary)); QList<Solid::Device> batteries = Solid::Device::listFromQuery(Solid::Predicate(batteryDevice, QStringLiteral("type"), primary));
if (batteries.isEmpty()) { if (batteries.isEmpty()) {
// Treat as if we're working with a desktop computer.
qCWarning(KDECONNECT_PLUGIN_BATTERY) << "No Primary Battery detected on this system. This may be a bug."; qCWarning(KDECONNECT_PLUGIN_BATTERY) << "No Primary Battery detected on this system. This may be a bug.";
QList<Solid::Device> allBatteries = Solid::Device::listFromType(batteryDevice); QList<Solid::Device> allBatteries = Solid::Device::listFromType(batteryDevice);
qCWarning(KDECONNECT_PLUGIN_BATTERY) << "Total quantity of batteries found: " << allBatteries.size(); qCWarning(KDECONNECT_PLUGIN_BATTERY) << "Total quantity of batteries found: " << allBatteries.size();
return; // return;
} } else {
// Ok, there's at least one. Let's assume it will remain attached (for most laptops // Ok, there's at least one. Let's assume it will remain attached (for most laptops
// and desktops, this is a safe assumption). // and desktops, this is a safe assumption).
const Solid::Battery *chosen = batteries.first().as<Solid::Battery>(); const Solid::Battery *chosen = batteries.first().as<Solid::Battery>();
connect(chosen, &Solid::Battery::chargeStateChanged, this, &BatteryPlugin::slotChargeChanged); connect(chosen, &Solid::Battery::chargeStateChanged, this, &BatteryPlugin::slotChargeChanged);
connect(chosen, &Solid::Battery::chargePercentChanged, this, &BatteryPlugin::slotChargeChanged); connect(chosen, &Solid::Battery::chargePercentChanged, this, &BatteryPlugin::slotChargeChanged);
}
// Explicitly send the current charge // Explicitly send the current charge
slotChargeChanged(); slotChargeChanged();
} }
@ -75,6 +75,7 @@ void BatteryPlugin::slotChargeChanged()
const auto batteryDevice = Solid::DeviceInterface::Type::Battery; const auto batteryDevice = Solid::DeviceInterface::Type::Battery;
const auto primary = Solid::Battery::BatteryType::PrimaryBattery; const auto primary = Solid::Battery::BatteryType::PrimaryBattery;
int charge = 0;
QList<Solid::Device> batteries = Solid::Device::listFromQuery(Solid::Predicate(batteryDevice, QStringLiteral("type"), primary)); QList<Solid::Device> batteries = Solid::Device::listFromQuery(Solid::Predicate(batteryDevice, QStringLiteral("type"), primary));
@ -91,27 +92,30 @@ void BatteryPlugin::slotChargeChanged()
} }
} }
if (batteryQuantity == 0) {
qCWarning(KDECONNECT_PLUGIN_BATTERY) << "Primary Battery seems to have been removed. Suspending packets until it is reconnected.";
return;
}
// Load a new Battery object to represent the first device in the list
Solid::Battery *chosen = batteries.first().as<Solid::Battery>();
// Prepare an outgoing network packet // Prepare an outgoing network packet
NetworkPacket status(PACKET_TYPE_BATTERY, {{}}); NetworkPacket status(PACKET_TYPE_BATTERY, {{}});
status.set(QStringLiteral("isCharging"), isAnyBatteryCharging); status.set(QStringLiteral("isCharging"), isAnyBatteryCharging);
const int charge = cumulativeCharge / batteryQuantity;
status.set(QStringLiteral("currentCharge"), charge);
// FIXME: In future, we should consider sending an array of battery objects // FIXME: In future, we should consider sending an array of battery objects
status.set(QStringLiteral("batteryQuantity"), batteryQuantity); status.set(QStringLiteral("batteryQuantity"), batteryQuantity);
if (batteryQuantity == 0) {
qCWarning(KDECONNECT_PLUGIN_BATTERY) << "Primary Battery seems to not be available.";
// return;
} else {
// Load a new Battery object to represent the first device in the list
Solid::Battery *chosen = batteries.first().as<Solid::Battery>();
charge = cumulativeCharge / batteryQuantity;
// We consider the primary battery to be low if it's below 15% // We consider the primary battery to be low if it's below 15%
if (charge <= 15 && chosen->chargeState() == Solid::Battery::ChargeState::Discharging) { if (charge <= 15 && chosen->chargeState() == Solid::Battery::ChargeState::Discharging) {
status.set(QStringLiteral("thresholdEvent"), (int)ThresholdBatteryLow); status.set(QStringLiteral("thresholdEvent"), (int)ThresholdBatteryLow);
} else { } else {
status.set(QStringLiteral("thresholdEvent"), (int)ThresholdNone); status.set(QStringLiteral("thresholdEvent"), (int)ThresholdNone);
} }
}
status.set(QStringLiteral("currentCharge"), charge);
sendPacket(status); sendPacket(status);
} }