From 2aa97e7da40e75bf09749c2ef836e450b19834c4 Mon Sep 17 00:00:00 2001 From: Piyush Aggarwal Date: Sat, 25 Mar 2023 01:09:13 +0530 Subject: [PATCH] plugin: enable Battery plugin on Windows --- plugins/CMakeLists.txt | 7 +---- plugins/battery/batteryplugin.cpp | 50 +++++++++++++++++-------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 1e304b88e..eedd9063f 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -7,12 +7,7 @@ add_subdirectory(remotecontrol) add_subdirectory(remotesystemvolume) add_subdirectory(clipboard) add_subdirectory(runcommand) - - -if(NOT WIN32) - # https://bugs.kde.org/show_bug.cgi?id=444612 - add_subdirectory(battery) -endif() +add_subdirectory(battery) if(NOT WIN32 AND NOT APPLE) add_subdirectory(bigscreen) diff --git a/plugins/battery/batteryplugin.cpp b/plugins/battery/batteryplugin.cpp index 12bd38bc4..eb3c00693 100644 --- a/plugins/battery/batteryplugin.cpp +++ b/plugins/battery/batteryplugin.cpp @@ -47,19 +47,19 @@ void BatteryPlugin::connected() QList batteries = Solid::Device::listFromQuery(Solid::Predicate(batteryDevice, QStringLiteral("type"), primary)); 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."; QList allBatteries = Solid::Device::listFromType(batteryDevice); 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 + // and desktops, this is a safe assumption). + const Solid::Battery *chosen = batteries.first().as(); + + connect(chosen, &Solid::Battery::chargeStateChanged, this, &BatteryPlugin::slotChargeChanged); + connect(chosen, &Solid::Battery::chargePercentChanged, this, &BatteryPlugin::slotChargeChanged); } - - // Ok, there's at least one. Let's assume it will remain attached (for most laptops - // and desktops, this is a safe assumption). - const Solid::Battery *chosen = batteries.first().as(); - - connect(chosen, &Solid::Battery::chargeStateChanged, this, &BatteryPlugin::slotChargeChanged); - connect(chosen, &Solid::Battery::chargePercentChanged, this, &BatteryPlugin::slotChargeChanged); - // Explicitly send the current charge slotChargeChanged(); } @@ -75,6 +75,7 @@ void BatteryPlugin::slotChargeChanged() const auto batteryDevice = Solid::DeviceInterface::Type::Battery; const auto primary = Solid::Battery::BatteryType::PrimaryBattery; + int charge = 0; QList 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(); - // Prepare an outgoing network packet NetworkPacket status(PACKET_TYPE_BATTERY, {{}}); 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 status.set(QStringLiteral("batteryQuantity"), batteryQuantity); - // We consider the primary battery to be low if it's below 15% - if (charge <= 15 && chosen->chargeState() == Solid::Battery::ChargeState::Discharging) { - status.set(QStringLiteral("thresholdEvent"), (int)ThresholdBatteryLow); + + if (batteryQuantity == 0) { + qCWarning(KDECONNECT_PLUGIN_BATTERY) << "Primary Battery seems to not be available."; + // return; } else { - status.set(QStringLiteral("thresholdEvent"), (int)ThresholdNone); + // Load a new Battery object to represent the first device in the list + Solid::Battery *chosen = batteries.first().as(); + + charge = cumulativeCharge / batteryQuantity; + + // We consider the primary battery to be low if it's below 15% + if (charge <= 15 && chosen->chargeState() == Solid::Battery::ChargeState::Discharging) { + status.set(QStringLiteral("thresholdEvent"), (int)ThresholdBatteryLow); + } else { + status.set(QStringLiteral("thresholdEvent"), (int)ThresholdNone); + } } + status.set(QStringLiteral("currentCharge"), charge); + sendPacket(status); }