BatteryService: modify setter script to check if first install and call isntaller script if yes

This commit is contained in:
Damian D'Souza
2025-10-09 21:23:25 +02:00
parent 4f6d2a5947
commit e661de9930
3 changed files with 82 additions and 18 deletions
+74
View File
@@ -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 <number>" >&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 <number>" >&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
-17
View File
@@ -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 <number>" >&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 <number>" >&2
exit 1
fi
echo "$1" | pkexec tee ~/test
+8 -1
View File
@@ -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)
}
}
}
}
}