import QtQuick 2.15 import QtQuick.Layouts 1.15 import QtQuick.Controls 2.15 import org.kde.plasma.plasmoid 2.0 import org.kde.plasma.core 2.0 as PlasmaCore import org.kde.plasma.components 2.0 as PlasmaComponents import org.kde.plasma.components 3.0 as PlasmaComponents3 import org.kde.plasma.extras 2.0 as PlasmaExtras Item { width: PlasmaCore.Units.gridUnit * 20 height: PlasmaCore.Units.gridUnit * 40 readonly property string canvasUrl: plasmoid.configuration.canvasUrl readonly property string apiEndpoint: `${canvasUrl.replace(/\/$/, "")}/api/v1` readonly property string oauth2Token: plasmoid.configuration.oauth2Token readonly property string authHeader: `Bearer ${oauth2Token}` function callApi(path, amount, callback) { let xhr = new XMLHttpRequest() xhr.open("GET", `${apiEndpoint}${path}` + (amount ? `?per_page=${amount}` : "")) xhr.setRequestHeader("Authorization", authHeader) xhr.onload = () => { if (xhr.status == 200) { try { let json = JSON.parse(xhr.responseText) if (callback) { callback(json) } } catch (e) { if (e instanceof SyntaxError) { console.error(`Cannot parse response for ${path} as JSON:\n${xhr.responseText}`) } else { throw e } } } else { console.error(`XHR failed when retrieving ${path} (status ${xhr.status}):\n${xhr.responseText}`) } } xhr.send() } function syncCanvas() { // Get all courses callApi("/courses", 20, courses => { for (let course of courses) { callApi(`/courses/${course.id}/activity_stream`, 10, activityStream => { for (let activity of activityStream) { if (activity.type == "Announcement") { announcementsModel.append({course: course.course_code, title: activity.title}) } } }) } }) } Timer { interval: 60 * 1000 running: true; repeat: true; onTriggered: syncCanvas(); } ColumnLayout { id: main anchors.fill: parent width: PlasmaCore.Units.gridUnit * 20 height: PlasmaCore.Units.gridUnit * 40 PlasmaExtras.Heading { level: 1 text: "Kanvas" } PlasmaExtras.Heading { level: 2 text: "Announcements" } ListModel { id: announcementsModel } ScrollView { implicitHeight: PlasmaCore.Units.gridUnit * 20 Layout.margins: PlasmaCore.Units.smallSpacing Layout.fillWidth: true Layout.fillHeight: true ScrollBar.horizontal.policy: ScrollBar.AlwaysOff ListView { Layout.fillWidth: true delegate: Text { text: `[${course}] ${title}` color: PlasmaCore.Theme.textColor } model: announcementsModel } } PlasmaComponents3.Button { icon.name: "view-refresh" text: i18n("Refresh") onClicked: syncCanvas() } } }