headsetcontrol-battery-widget/package/contents/ui/main.qml
2024-01-25 16:21:39 +01:00

69 lines
2.1 KiB
QML

import QtQuick 2.15
import QtQuick.Layouts 1.3
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.kirigami 2.14 as Kirigami
import org.kde.notification 1.0
import "./lib/helper.js" as Helper
Item {
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;
PlasmaCore.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();
}
else // reset notifications if battery level is over batteryThreshold
notification_sent = false;
return;
}
}
Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation
Plasmoid.fullRepresentation: FullRepresentation{}
Plasmoid.compactRepresentation: CompactRepresentation{}
Notification {
id: notification
componentName: "plasma_workspace"
eventId: "notification"
flags: Notification.Persistent
urgency: Notification.HighUrgency
}
}