headsetcontrol-battery-widget/package/contents/ui/main.qml
2024-03-07 12:44:01 +01:00

74 lines
2.4 KiB
QML

import QtQuick
import QtQuick.Layouts
import org.kde.plasma.plasmoid
import org.kde.kirigami as Kirigami
import org.kde.plasma.plasma5support as Plasma5Support
import org.kde.notification
import "./lib/helper.js" as Helper
PlasmoidItem {
id: main
property int pollingrate: Plasmoid.configuration.pollingrate
property int batteryheight: Plasmoid.configuration.batteryheight
property string colorEmpty: Plasmoid.configuration.colorEmpty
property string colorHalf: Plasmoid.configuration.colorHalf
property string colorFull: Plasmoid.configuration.colorFull
property bool notifications: Plasmoid.configuration.notifications
property int batteryThreshold: Plasmoid.configuration.batteryThreshold
property bool debug_active: Plasmoid.configuration.debug_active
property int debug_charge: Plasmoid.configuration.debug_charge
property int batteryPercent: -2
property bool notification_sent: false;
Plasma5Support.DataSource {
id: hsSource
engine: "executable"
connectedSources: ["headsetcontrol -bc"]
interval: pollingrate * 1e3
onNewData: {
if(debug_active)
batteryPercent = debug_charge;
else
batteryPercent = data['exit code'] > 0
? -2 // not connected
: +data.stdout;
// send notification if necessary
if(batteryPercent <= batteryThreshold && batteryPercent >= 0 && !notification_sent && notifications) {
notification_sent = true;
notification.text = "Battery Level is low (" + batteryPercent + "%)";
notification.title = "Headset Battery Level Alert";
notification.iconName = "notification-battery-low";
notification.sendEvent();
}
// reset notifications if battery level is over batteryThreshold
if((batteryPercent > batteryThreshold || batteryPercent < 0) && notification_sent && notifications) {
notification_sent = false;
}
return;
}
}
toolTipMainText: "battery level: " + Helper.batteryLevel(batteryPercent)
toolTipSubText: "polling rate: " + pollingrate + " seconds"
preferredRepresentation: compactRepresentation
fullRepresentation: FullRepresentation{}
compactRepresentation: CompactRepresentation{}
Notification {
id: notification
componentName: "plasma_workspace"
eventId: "notification"
flags: Notification.DefaultEvent
urgency: Notification.CriticalUrgency
}
}