headsetcontrol-battery-widget/package/contents/ui/main.qml

74 lines
2.4 KiB
QML
Raw Normal View History

2024-03-07 11:44:01 +00:00
import QtQuick
import QtQuick.Layouts
import org.kde.plasma.plasmoid
import org.kde.kirigami as Kirigami
import org.kde.plasma.plasma5support as Plasma5Support
2024-01-17 19:06:16 +00:00
2024-03-07 11:44:01 +00:00
import org.kde.notification
2024-01-25 15:21:39 +00:00
2024-01-20 02:03:58 +00:00
import "./lib/helper.js" as Helper
2024-03-07 11:44:01 +00:00
PlasmoidItem {
2024-01-17 19:06:16 +00:00
id: main
2024-01-19 00:54:33 +00:00
property int pollingrate: Plasmoid.configuration.pollingrate
2024-01-20 10:36:02 +00:00
property int batteryheight: Plasmoid.configuration.batteryheight
2024-01-20 12:16:48 +00:00
property string colorEmpty: Plasmoid.configuration.colorEmpty
2024-01-20 14:16:18 +00:00
property string colorHalf: Plasmoid.configuration.colorHalf
2024-01-20 12:16:48 +00:00
property string colorFull: Plasmoid.configuration.colorFull
2024-01-22 02:08:31 +00:00
2024-01-25 15:21:39 +00:00
property bool notifications: Plasmoid.configuration.notifications
property int batteryThreshold: Plasmoid.configuration.batteryThreshold
2024-01-22 02:08:31 +00:00
property bool debug_active: Plasmoid.configuration.debug_active
property int debug_charge: Plasmoid.configuration.debug_charge
2024-01-17 19:06:16 +00:00
property int batteryPercent: -2
2024-01-25 15:21:39 +00:00
property bool notification_sent: false;
2024-01-17 19:06:16 +00:00
2024-03-07 11:44:01 +00:00
Plasma5Support.DataSource {
2024-01-17 19:06:16 +00:00
id: hsSource
engine: "executable"
connectedSources: ["headsetcontrol -bc"]
2024-01-19 00:54:33 +00:00
interval: pollingrate * 1e3
2024-01-17 19:06:16 +00:00
onNewData: {
2024-01-22 02:08:31 +00:00
if(debug_active)
2024-01-25 15:21:39 +00:00
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();
}
2024-01-25 15:53:07 +00:00
// reset notifications if battery level is over batteryThreshold
if((batteryPercent > batteryThreshold || batteryPercent < 0) && notification_sent && notifications) {
2024-01-25 15:21:39 +00:00
notification_sent = false;
2024-01-25 15:53:07 +00:00
}
2024-01-25 15:21:39 +00:00
return;
2024-01-17 19:06:16 +00:00
}
}
2024-03-07 11:44:01 +00:00
toolTipMainText: "battery level: " + Helper.batteryLevel(batteryPercent)
toolTipSubText: "polling rate: " + pollingrate + " seconds"
preferredRepresentation: compactRepresentation
fullRepresentation: FullRepresentation{}
compactRepresentation: CompactRepresentation{}
2024-01-25 15:21:39 +00:00
Notification {
id: notification
componentName: "plasma_workspace"
eventId: "notification"
2024-01-25 15:53:07 +00:00
flags: Notification.DefaultEvent
urgency: Notification.CriticalUrgency
2024-01-25 15:21:39 +00:00
}
2024-01-17 19:06:16 +00:00
}