QOwnNotesDiscordRPC/main.qml
2024-08-03 04:06:32 +03:00

70 lines
1.8 KiB
QML

import QOwnNotesTypes 1.0
import QtQml 2.0
import QtWebSockets 1.1
Script {
property string scriptDirPath
property QtObject timer
property QtObject delay
property QtObject websocket
function init() {
script.startDetachedProcess("/usr/bin/python3", [scriptDirPath + "/main.py"]);
}
websocket: WebSocket {
id: socket
url: "ws://localhost:52121"
onTextMessageReceived: {
script.log("Received message: " + message);
}
onStatusChanged: {
if (socket.status == WebSocket.Error) {
script.log("Error: " + socket.errorString);
} else if (socket.status == WebSocket.Open) {
script.log("Open!");
} else if (socket.status == WebSocket.Closed) {
this.active = false;
delay.setTimeout(() => {
socket.active = true;
}, 2000);
script.log("Socket closed");
}
}
active: true
}
delay: Timer {
id: timer
function setTimeout(cb, delayTime) {
timer.interval = delayTime;
timer.repeat = false;
timer.triggered.connect(cb);
timer.triggered.connect(function release() {
timer.triggered.disconnect(cb);
timer.triggered.disconnect(release);
});
timer.start();
}
}
timer: Timer {
interval: 1000
running: true
repeat: true
onTriggered: {
if (websocket.active) {
var note = script.currentNote();
websocket.sendTextMessage(JSON.stringify({
"name": note.name,
"cursorPosition": script.noteTextEditCursorPosition()
}));
}
}
}
}