mirror of
https://github.com/zoriya/noctalia-shell.git
synced 2026-06-02 10:37:50 +00:00
BatteryService: add separate process for installing manger, use toast messages instead of notifications
This commit is contained in:
@@ -66,43 +66,10 @@ BATTERY_MANAGER_PATH="/usr/bin/battery-manager-$CURRENT_USER"
|
||||
|
||||
SUCCESS=0
|
||||
MISSING_FILES=2
|
||||
UNSUPPORTED=3
|
||||
|
||||
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)..."
|
||||
|
||||
pkexec "$SCRIPT_DIR/install-battery-manager.sh"
|
||||
INSTALL_RESULT=$?
|
||||
|
||||
case $INSTALL_RESULT in
|
||||
$SUCCESS)
|
||||
print_info "Installation completed successfully!"
|
||||
send_notification "normal" "Battery Manager Installed" \
|
||||
"Battery manager has been set up successfully for $CURRENT_USER."
|
||||
;;
|
||||
$MISSING_FILES)
|
||||
print_error "Installation failed: Required files are missing"
|
||||
send_notification "critical" "Installation Failed" \
|
||||
"Battery manager installation failed: Missing required files"
|
||||
exit $MISSING_FILES
|
||||
;;
|
||||
$UNSUPPORTED)
|
||||
print_error "Installation failed: System not supported"
|
||||
send_notification "critical" "Installation Failed" \
|
||||
"Battery manager installation failed: Your system is not supported"
|
||||
exit $UNSUPPORTED
|
||||
;;
|
||||
*)
|
||||
print_error "Installation failed or was cancelled (error code: $INSTALL_RESULT)"
|
||||
send_notification "critical" "Installation Failed" \
|
||||
"Battery manager installation failed or was cancelled"
|
||||
exit $INSTALL_RESULT
|
||||
;;
|
||||
esac
|
||||
exit $MISSING_FILES
|
||||
fi
|
||||
|
||||
print_info "Setting battery charging threshold to $BATTERY_LEVEL% for user $CURRENT_USER..."
|
||||
|
||||
+87
-22
@@ -16,7 +16,11 @@ Singleton {
|
||||
}
|
||||
|
||||
property int chargingMode: BatteryService.ChargingMode.Balanced
|
||||
readonly property string batteryTresholdScript: Quickshell.shellDir + '/Bin/battery-manager/set-battery-treshold.sh'
|
||||
readonly property string batterySetterScript: Quickshell.shellDir + '/Bin/battery-manager/set-battery-treshold.sh'
|
||||
readonly property string batteryInstallerScript: Quickshell.shellDir + '/Bin/battery-manager/install-battery-manager.sh'
|
||||
|
||||
// This is false when setter is started in init so that a toast isn't shown on every startup
|
||||
property bool hideSuccessToast: true
|
||||
|
||||
// Choose icon based on charge and charging state
|
||||
function getIcon(percent, charging, isReady) {
|
||||
@@ -39,6 +43,17 @@ Singleton {
|
||||
}
|
||||
}
|
||||
|
||||
function getThresholdValue() {
|
||||
switch (BatteryService.chargingMode) {
|
||||
case BatteryService.ChargingMode.Full:
|
||||
return "100"
|
||||
case BatteryService.ChargingMode.Balanced:
|
||||
return "80"
|
||||
case BatteryService.ChargingMode.Conservative:
|
||||
return "60"
|
||||
}
|
||||
}
|
||||
|
||||
function setChargingMode(newMode) {
|
||||
if (newMode !== BatteryService.ChargingMode.Full && newMode !== BatteryService.ChargingMode.Balanced && newMode !== BatteryService.ChargingMode.Conservative) {
|
||||
return
|
||||
@@ -47,27 +62,23 @@ Singleton {
|
||||
BatteryService.applyChargingMode()
|
||||
}
|
||||
|
||||
function applyChargingMode(quiet = false) {
|
||||
let command = [batteryTresholdScript]
|
||||
function applyChargingMode(hideToast = false) {
|
||||
let command = [batterySetterScript]
|
||||
|
||||
if (quiet) {
|
||||
command.push("-q")
|
||||
}
|
||||
// Currently the script sends notifications by default but quickshell
|
||||
// uses toast messages so the flag is passed to supress notifs
|
||||
command.push("-q")
|
||||
|
||||
switch (BatteryService.chargingMode) {
|
||||
case BatteryService.ChargingMode.Full:
|
||||
command.push("100")
|
||||
break
|
||||
case BatteryService.ChargingMode.Balanced:
|
||||
command.push("80")
|
||||
break
|
||||
case BatteryService.ChargingMode.Conservative:
|
||||
command.push("60")
|
||||
break
|
||||
}
|
||||
command.push(BatteryService.getThresholdValue())
|
||||
BatteryService.hideSuccessToast = hideToast
|
||||
|
||||
chargeLimitProcess.command = command
|
||||
chargeLimitProcess.running = true
|
||||
setterProcess.command = command
|
||||
setterProcess.running = true
|
||||
}
|
||||
|
||||
function runInstaller() {
|
||||
installerProcess.command = ["pkexec", batteryInstallerScript]
|
||||
installerProcess.running = true
|
||||
}
|
||||
|
||||
function init() {
|
||||
@@ -76,20 +87,74 @@ Singleton {
|
||||
}
|
||||
|
||||
Process {
|
||||
id: chargeLimitProcess
|
||||
id: setterProcess
|
||||
workingDirectory: Quickshell.shellDir
|
||||
running: false
|
||||
onExited: (exitCode, exitStatus) => {
|
||||
if (exitCode === 0) {
|
||||
Logger.log("BatteryService", "Battery threshold set successfully")
|
||||
if (!BatteryService.hideSuccessToast) {
|
||||
ToastService.showNotice("Battery Manager", `Battery threshold set to ${BatteryService.getThresholdValue()}%`)
|
||||
}
|
||||
} else if (exitCode === 2) {
|
||||
// Initial setup required - show toast and run installer
|
||||
ToastService.showWarning("Battery Manager", "Initial setup required")
|
||||
BatteryService.runInstaller()
|
||||
} else {
|
||||
ToastService.showError("Battery Manager", "Failed to set battery threshold")
|
||||
Logger.error("BatteryService", `Setter process failed with exit code: ${exitCode}`)
|
||||
}
|
||||
}
|
||||
stderr: StdioCollector {
|
||||
onStreamFinished: {
|
||||
if (this.text) {
|
||||
Logger.warn("BatteryService", "ChargeLimitProcess stderr:", this.text)
|
||||
Logger.warn("BatteryService", "SetterProcess stderr:", this.text)
|
||||
}
|
||||
}
|
||||
}
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
if (this.text) {
|
||||
Logger.log("BatteryService", "ChargeLimitProcess stdout:", this.text)
|
||||
Logger.log("BatteryService", "SetterProcess stdout:", this.text)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Installer process - installs battery manager components
|
||||
Process {
|
||||
id: installerProcess
|
||||
workingDirectory: Quickshell.shellDir
|
||||
running: false
|
||||
onExited: (exitCode, exitStatus) => {
|
||||
if (exitCode === 0) {
|
||||
ToastService.showNotice("Battery Manager", "Installed successfully")
|
||||
// Installation successful, retry setting the battery threshold
|
||||
BatteryService.applyChargingMode()
|
||||
} else if (exitCode === 2) {
|
||||
ToastService.showError("Battery Manager", "Required files are missing")
|
||||
} else if (exitCode === 3) {
|
||||
ToastService.showError("Battery Manager", "System is not supported")
|
||||
} else {
|
||||
ToastService.showError("Battery Manager", "Installation failed")
|
||||
}
|
||||
|
||||
if (exitCode !== 0) {
|
||||
// TODO, reset do a null or smth
|
||||
BatteryService.chargingMode = BatteryService.ChargingMode.Balanced
|
||||
}
|
||||
}
|
||||
stderr: StdioCollector {
|
||||
onStreamFinished: {
|
||||
if (this.text) {
|
||||
Logger.warn("BatteryService", "InstallerProcess stderr:", this.text)
|
||||
}
|
||||
}
|
||||
}
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
if (this.text) {
|
||||
Logger.log("BatteryService", "InstallerProcess stdout:", this.text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user