Files
kde-codex-dashboard/contents/ui/main.qml
T
Matteo Benedetto 3eee01f0f2 feat: dashboard KDE Plasma 6 per utilizzo OpenAI Codex
Widget desktop 224x224 che mostra:
- residuo settimanale con barra e percentuale
- countdown esatto al reset (reset_at dall'API)
- piano (Plus/Free), crediti, stato limiti
- backend bash che chiama https://chatgpt.com/backend-api/wham/usage
- legge il token OAuth da ~/.pi/agent/auth.json (provider openai-codex)
2026-08-20 18:48:51 +02:00

355 lines
14 KiB
QML

/* OpenAI Codex Usage — dashboard desktop per KDE Plasma 6. */
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls as QQC2
import org.kde.plasma.plasmoid
import org.kde.plasma.core as PlasmaCore
import org.kde.plasma.components 3.0 as PC3
import org.kde.plasma.plasma5support as Plasma5Support
import org.kde.kirigami as Kirigami
PlasmoidItem {
id: root
property var data: null // { ok, plan_type, remaining, used_percent, reset_at, limit_window, credits, spend_reached, limit_reached }
property string lastUpdate: ""
property string errorMsg: ""
property bool loading: false
property int clockTick: 0
property int refreshSecs: Plasmoid.configuration.refreshIntervalSec
property int warnThreshold: Plasmoid.configuration.warnThreshold
property int critThreshold: Plasmoid.configuration.critThreshold
Plasmoid.title: i18n("OpenAI Codex")
Plasmoid.icon: "code-context"
Plasmoid.backgroundHints: PlasmaCore.Types.DefaultBackground
toolTipMainText: i18n("Codex — utilizzo settimanale")
toolTipSubText: root.data ? i18n("Residuo: %1% · %2",
Math.round((root.data.remaining || 0) * 100),
root.data.plan_type || "—") : (root.errorMsg || "")
preferredRepresentation: fullRepresentation
switchWidth: 150
switchHeight: 150
Plasma5Support.DataSource {
id: executable
engine: "executable"
connectedSources: []
onNewData: function(sourceName, result) {
root.loading = false
executable.disconnectSource(sourceName)
if (result["exit code"] !== 0) {
root.errorMsg = i18n("comando fallito (exit %1)", result["exit code"])
return
}
try {
const parsed = JSON.parse((result["stdout"] || "").trim())
if (parsed && parsed.ok === false) {
root.errorMsg = parsed.error + (parsed.detail ? " — " + parsed.detail : "")
root.data = null
} else if (parsed && parsed.ok && parsed.remaining !== undefined) {
root.data = parsed
root.errorMsg = ""
root.lastUpdate = new Date().toLocaleTimeString(Qt.locale(), "HH:mm:ss")
} else {
root.errorMsg = i18n("risposta inattesa")
root.data = null
}
} catch (e) {
root.errorMsg = i18n("JSON non valido: %1", e.message)
root.data = null
}
}
}
function refresh() {
if (root.loading) return
root.loading = true
const path = Qt.resolvedUrl("../scripts/usage.sh").toString().replace(/^file:\/\//, "")
executable.connectSource("/bin/bash " + shellQuote(path))
}
function shellQuote(s) { return "'" + String(s).replace(/'/g, "'\"'\"'") + "'" }
function remainingPct() { return root.data ? Math.round((root.data.remaining || 0) * 100) : 0 }
function usedPct() { return root.data ? Math.round(root.data.used_percent || 0) : 0 }
function accentColor() {
const pct = remainingPct()
if (pct <= root.critThreshold) return Kirigami.Theme.negativeTextColor
if (pct <= root.warnThreshold) return Kirigami.Theme.neutralTextColor
return "#7cb342"
}
function resetMoment() {
const tick = root.clockTick
if (!root.data || !root.data.reset_at) return "—"
const d = new Date(root.data.reset_at * 1000)
return d.toLocaleDateString(Qt.locale(), "dd/MM") + " " + d.toLocaleTimeString(Qt.locale(), "HH:mm")
}
function resetRemaining() {
const tick = root.clockTick
if (!root.data || !root.data.reset_at) return "—"
let mins = Math.max(0, Math.floor((root.data.reset_at * 1000 - Date.now()) / 60000))
if (mins === 0) return i18n("ora")
const days = Math.floor(mins / 1440)
mins -= days * 1440
const hours = Math.floor(mins / 60)
mins -= hours * 60
return days > 0 ? i18n("%1g %2h", days, hours) : i18n("%1h %2m", hours, mins)
}
function windowLabel() {
if (!root.data || !root.data.limit_window) return "7D"
const secs = root.data.limit_window
if (secs >= 604800) return "7D"
if (secs >= 3600) return Math.round(secs / 3600) + "H"
return Math.round(secs / 60) + "M"
}
Component.onCompleted: root.refresh()
Timer {
interval: Math.max(30, root.refreshSecs) * 1000
repeat: true
running: true
onTriggered: root.refresh()
}
Timer {
interval: 30000
repeat: true
running: true
onTriggered: root.clockTick += 1
}
compactRepresentation: Component {
Item {
implicitWidth: Kirigami.Units.iconSizes.smallMedium
implicitHeight: Kirigami.Units.iconSizes.smallMedium
Kirigami.Icon {
anchors.fill: parent
source: Plasmoid.icon
color: root.data ? root.accentColor() : Kirigami.Theme.disabledTextColor
}
MouseArea { anchors.fill: parent; onClicked: Plasmoid.expanded = !Plasmoid.expanded }
}
}
fullRepresentation: Component {
Item {
implicitWidth: 224
implicitHeight: 224
Layout.preferredWidth: 224
Layout.preferredHeight: 224
ColumnLayout {
anchors.fill: parent
anchors.margins: 9
spacing: 5
// header
RowLayout {
Layout.fillWidth: true
Kirigami.Icon {
source: Plasmoid.icon
color: "#7cb342"
Layout.preferredWidth: 20
Layout.preferredHeight: 20
}
ColumnLayout {
spacing: 0
PC3.Label {
text: i18n("CODEX")
font.bold: true
font.pointSize: Kirigami.Theme.defaultFont.pointSize + 1
}
PC3.Label {
text: root.data ? (root.data.plan_type || "—").toUpperCase() : "—"
color: "#7cb342"
font.bold: true
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 2
}
}
Item { Layout.fillWidth: true }
Rectangle {
Layout.preferredWidth: 9
Layout.preferredHeight: 9
radius: 5
color: root.errorMsg ? Kirigami.Theme.negativeTextColor : "#7cb342"
}
PC3.Button {
icon.name: "view-refresh"
display: QQC2.AbstractButton.IconOnly
enabled: !root.loading
onClicked: root.refresh()
}
}
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 1
color: Qt.rgba(0.49, 0.76, 0.26, 0.45)
}
PC3.Label {
visible: root.errorMsg.length > 0
Layout.fillWidth: true
wrapMode: Text.Wrap
color: Kirigami.Theme.negativeTextColor
text: i18n("Errore: %1", root.errorMsg)
}
// barra principale
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 72
radius: 6
color: Qt.rgba(0, 0, 0, 0.26)
border.width: 1
border.color: root.accentColor()
ColumnLayout {
anchors.fill: parent
anchors.margins: 7
spacing: 3
RowLayout {
Layout.fillWidth: true
spacing: 5
Rectangle {
Layout.preferredWidth: 24
Layout.preferredHeight: 15
radius: 8
color: root.accentColor()
PC3.Label {
anchors.centerIn: parent
text: root.windowLabel()
color: "#101012"
font.bold: true
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 2
}
}
PC3.Label {
text: i18n("Settimanale")
font.bold: true
Layout.fillWidth: true
}
PC3.Label {
text: root.remainingPct() + "%"
color: root.accentColor()
font.bold: true
font.pointSize: Kirigami.Theme.defaultFont.pointSize + 3
}
}
Item {
Layout.fillWidth: true
Layout.preferredHeight: 8
Rectangle {
anchors.fill: parent
radius: 4
color: Qt.rgba(0, 0, 0, 0.55)
}
Rectangle {
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
width: parent.width * Math.max(0, Math.min(1, root.data ? root.data.remaining : 0))
height: parent.height
radius: 4
color: root.accentColor()
}
}
RowLayout {
Layout.fillWidth: true
PC3.Label {
text: i18n("%1% usato", root.usedPct())
color: Kirigami.Theme.disabledTextColor
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 3
}
Item { Layout.fillWidth: true }
PC3.Label {
visible: Boolean(root.data && root.data.limit_reached)
text: i18n("✕ LIMITE")
color: Kirigami.Theme.negativeTextColor
font.bold: true
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 3
}
}
}
}
// reset + crediti
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 54
radius: 5
color: Qt.rgba(0, 0, 0, 0.28)
ColumnLayout {
anchors.fill: parent
anchors.margins: 5
spacing: 2
RowLayout {
Layout.fillWidth: true
PC3.Label {
text: i18n("RESET")
color: Kirigami.Theme.disabledTextColor
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 3
}
Item { Layout.fillWidth: true }
PC3.Label {
text: root.resetMoment()
color: root.accentColor()
font.bold: true
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 2
}
}
RowLayout {
Layout.fillWidth: true
PC3.Label {
text: root.resetRemaining()
color: "#7cb342"
font.bold: true
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 1
}
Item { Layout.fillWidth: true }
PC3.Label {
text: root.data && root.data.credits ? i18n("Cred. %1", root.data.credits.balance || "0") : ""
color: Kirigami.Theme.disabledTextColor
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 3
}
}
RowLayout {
Layout.fillWidth: true
PC3.Label {
visible: Boolean(root.data && root.data.spend_reached)
text: i18n("Spend control raggiunto")
color: Kirigami.Theme.negativeTextColor
font.bold: true
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 3
}
}
}
}
RowLayout {
Layout.fillWidth: true
PC3.Label {
text: root.lastUpdate ? i18n("SYNC %1", root.lastUpdate) : i18n("SYNC…")
color: Kirigami.Theme.disabledTextColor
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 3
}
Item { Layout.fillWidth: true }
PC3.Label {
text: root.data && root.data.plan_type ? root.data.plan_type.toUpperCase() : ""
color: Kirigami.Theme.disabledTextColor
font.bold: true
font.pointSize: Kirigami.Theme.defaultFont.pointSize - 3
}
}
}
}
}
}