From e661de9930b7ce98ab3c15810404c630d8c6aee7 Mon Sep 17 00:00:00 2001 From: Damian D'Souza Date: Thu, 9 Oct 2025 21:23:25 +0200 Subject: [PATCH] BatteryService: modify setter script to check if first install and call isntaller script if yes --- Bin/battery-manager/set-battery-treshold.sh | 74 +++++++++++++++++++++ Bin/set-battery-treshold.sh | 17 ----- Services/BatteryService.qml | 9 ++- 3 files changed, 82 insertions(+), 18 deletions(-) create mode 100755 Bin/battery-manager/set-battery-treshold.sh delete mode 100755 Bin/set-battery-treshold.sh diff --git a/Bin/battery-manager/set-battery-treshold.sh b/Bin/battery-manager/set-battery-treshold.sh new file mode 100755 index 00000000..e4e4c8b7 --- /dev/null +++ b/Bin/battery-manager/set-battery-treshold.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +print_error() { + echo -e "$1" >&2 +} + +print_info() { + echo -e "$1" +} + +send_notification() { + local urgency="$1" + local title="$2" + local message="$3" + + if command -v notify-send >/dev/null 2>&1; then + notify-send -u "$urgency" "$title" "$message" + fi +} + +if [ "$#" -ne 1 ]; then + print_error "Battery level not specified" + echo "Usage: $0 " >&2 + exit 1 +fi + +if ! [[ "$1" =~ ^[0-9]+$ ]] || [ "$1" -gt 100 ] || [ "$1" -lt 0 ]; then + print_error "Battery level must be a number between 0-100" + echo "Usage: $0 " >&2 + exit 1 +fi + +BATTERY_LEVEL="$1" + +CURRENT_USER="$USER" +if [ -z "$CURRENT_USER" ]; then + CURRENT_USER="$(whoami)" +fi + +BATTERY_MANAGER_PATH="/usr/bin/battery-manager-$CURRENT_USER" + +if [ ! -f "$BATTERY_MANAGER_PATH" ]; then + print_error "Battery manager components missing for user $CURRENT_USER!" + send_notification "critical" "Battery Manager Setup Required" \ + "Battery manager needs to be set up for user $CURRENT_USER. Please authenticate when prompted." + + print_info "Running installer (authentication required)..." + + if pkexec "$SCRIPT_DIR/install-battery-manager.sh"; then + print_info "Installation completed successfully!" + send_notification "normal" "Battery Manager Installed" \ + "Battery manager has been set up successfully for $CURRENT_USER." + else + print_error "Installation failed or was cancelled" + send_notification "critical" "Installation Failed" \ + "Battery manager installation failed or was cancelled." + exit 1 + fi +fi + +print_info "Setting battery charging threshold to $BATTERY_LEVEL% for user $CURRENT_USER..." + +if pkexec "$BATTERY_MANAGER_PATH" "$BATTERY_LEVEL"; then + print_info "Battery charging threshold set to $BATTERY_LEVEL%" + send_notification "normal" "Battery Threshold Updated" \ + "Battery charging threshold has been set to $BATTERY_LEVEL%" +else + print_error "Failed to set battery charging threshold" + send_notification "critical" "Battery Threshold Failed" \ + "Failed to set battery charging threshold to $BATTERY_LEVEL%" + exit 1 +fi diff --git a/Bin/set-battery-treshold.sh b/Bin/set-battery-treshold.sh deleted file mode 100755 index 66665826..00000000 --- a/Bin/set-battery-treshold.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env -S bash - -# Check if exectly one argument was provided -if [ "$#" -ne 1 ]; then - echo "Error: Battery level not specified" >&2 - echo "Usage: $0 " >&2 - exit 1 -fi - -# Check if argument is a number -if ! [[ "$1" =~ ^[0-9]+$ ]]; then - echo "Error: Battery level must be a number" >&2 - echo "Usage: $0 " >&2 - exit 1 -fi - -echo "$1" | pkexec tee ~/test diff --git a/Services/BatteryService.qml b/Services/BatteryService.qml index e7f17dd9..bb46d4f6 100644 --- a/Services/BatteryService.qml +++ b/Services/BatteryService.qml @@ -16,7 +16,7 @@ Singleton { } property int chargingMode: BatteryService.ChargingMode.Balanced - readonly property string batteryTresholdScript: Quickshell.shellDir + '/Bin/set-battery-treshold.sh' + readonly property string batteryTresholdScript: Quickshell.shellDir + '/Bin/battery-manager/set-battery-treshold.sh' // Choose icon based on charge and charging state function getIcon(percent, charging, isReady) { @@ -70,5 +70,12 @@ Singleton { } } } + stdout: StdioCollector { + onStreamFinished: { + if (this.text) { + Logger.log("BatteryService", "ChargeLimitProcess stdout:", this.text) + } + } + } } }