2019-07-13 19:39:01 +10:00
|
|
|
import QtQuick 2.12
|
2019-06-27 16:31:03 +10:00
|
|
|
import io.thp.pyotherside 1.5
|
|
|
|
|
|
|
|
Python {
|
|
|
|
id: py
|
2019-12-18 21:16:24 +11:00
|
|
|
Component.onCompleted: {
|
|
|
|
for (var func in privates.eventHandlers) {
|
|
|
|
if (! privates.eventHandlers.hasOwnProperty(func)) continue
|
|
|
|
setHandler(func.replace(/^on/, ""), privates.eventHandlers[func])
|
|
|
|
}
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
addImportPath("src")
|
|
|
|
addImportPath("qrc:/src")
|
2019-12-08 09:33:33 +11:00
|
|
|
|
2019-12-18 21:44:18 +11:00
|
|
|
importNames("backend", ["BRIDGE"], () => {
|
2019-12-18 21:16:24 +11:00
|
|
|
loadSettings(() => {
|
|
|
|
callCoro("saved_accounts.any_saved", [], any => {
|
|
|
|
if (any) { py.callCoro("load_saved_accounts", []) }
|
2019-12-08 09:33:33 +11:00
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
py.startupAnyAccountsSaved = any
|
|
|
|
py.ready = true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-12-08 09:33:33 +11:00
|
|
|
|
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
property bool ready: false
|
|
|
|
property bool startupAnyAccountsSaved: false
|
2019-12-08 09:33:33 +11:00
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
readonly property QtObject privates: QtObject {
|
|
|
|
readonly property var pendingCoroutines: ({})
|
|
|
|
readonly property EventHandlers eventHandlers: EventHandlers {}
|
2019-12-08 09:33:33 +11:00
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
function makeFuture(callback) {
|
|
|
|
return Qt.createComponent("Future.qml")
|
|
|
|
.createObject(py, {bridge: py})
|
2019-12-08 09:33:33 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
|
2019-12-06 23:44:45 +11:00
|
|
|
function setattr(obj, attr, value, callback=null) {
|
|
|
|
py.call(py.getattr(obj, "__setattr__"), [attr, value], callback)
|
|
|
|
}
|
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
|
2019-07-18 17:35:30 +10:00
|
|
|
function callSync(name, args=[]) {
|
2019-12-18 21:44:18 +11:00
|
|
|
return call_sync("BRIDGE.backend." + name, args)
|
2019-07-04 14:24:21 +10:00
|
|
|
}
|
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
|
2019-10-28 21:26:02 +11:00
|
|
|
function callCoro(name, args=[], onSuccess=null, onError=null) {
|
2019-12-04 21:22:04 +11:00
|
|
|
let uuid = name + "." + CppUtils.uuid()
|
2019-07-07 15:37:13 +10:00
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
privates.pendingCoroutines[uuid] = {onSuccess, onError}
|
2019-12-08 09:33:33 +11:00
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
let future = privates.makeFuture()
|
2019-12-08 09:33:33 +11:00
|
|
|
|
2019-12-18 21:44:18 +11:00
|
|
|
call("BRIDGE.call_backend_coro", [name, uuid, args], pyFuture => {
|
2019-12-18 21:16:24 +11:00
|
|
|
future.privates.pythonFuture = pyFuture
|
2019-12-08 09:33:33 +11:00
|
|
|
})
|
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
return future
|
2019-06-27 16:31:03 +10:00
|
|
|
}
|
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
|
2019-10-28 21:26:02 +11:00
|
|
|
function callClientCoro(
|
|
|
|
accountId, name, args=[], onSuccess=null, onError=null
|
|
|
|
) {
|
2019-12-18 21:16:24 +11:00
|
|
|
let future = privates.makeFuture()
|
2019-12-08 09:33:33 +11:00
|
|
|
|
2019-07-21 22:38:49 +10:00
|
|
|
callCoro("wait_until_client_exists", [accountId], () => {
|
2019-12-04 21:22:04 +11:00
|
|
|
let uuid = accountId + "." + name + "." + CppUtils.uuid()
|
2019-06-29 08:12:45 +10:00
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
privates.pendingCoroutines[uuid] = {onSuccess, onError}
|
2019-12-08 09:33:33 +11:00
|
|
|
|
|
|
|
let call_args = [accountId, name, uuid, args]
|
|
|
|
|
2019-12-18 21:44:18 +11:00
|
|
|
call("BRIDGE.call_client_coro", call_args, pyFuture => {
|
2019-12-18 21:16:24 +11:00
|
|
|
future.privates.pythonFuture = pyFuture
|
2019-12-08 09:33:33 +11:00
|
|
|
})
|
2019-07-21 22:38:49 +10:00
|
|
|
})
|
2019-12-08 09:33:33 +11:00
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
return future
|
2019-06-29 08:12:45 +10:00
|
|
|
}
|
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
|
2019-07-21 21:14:16 +10:00
|
|
|
function saveConfig(backend_attribute, data, callback=null) {
|
2019-07-19 11:58:21 +10:00
|
|
|
if (! py.ready) { return } // config not loaded yet
|
2019-12-08 09:33:33 +11:00
|
|
|
return callCoro(backend_attribute + ".write", [data], callback)
|
2019-07-19 11:58:21 +10:00
|
|
|
}
|
|
|
|
|
2019-12-18 21:16:24 +11:00
|
|
|
|
2019-07-25 07:26:40 +10:00
|
|
|
function loadSettings(callback=null) {
|
2019-12-10 04:21:12 +11:00
|
|
|
let func = "load_settings"
|
|
|
|
|
|
|
|
return callCoro(func, [], ([settings, uiState, history, theme]) => {
|
2019-07-25 07:26:40 +10:00
|
|
|
window.settings = settings
|
|
|
|
window.uiState = uiState
|
2019-12-10 04:21:12 +11:00
|
|
|
window.history = history
|
2019-07-25 07:26:40 +10:00
|
|
|
window.theme = Qt.createQmlObject(theme, window, "theme")
|
|
|
|
|
|
|
|
if (callback) { callback(settings, uiState, theme) }
|
|
|
|
})
|
|
|
|
}
|
2019-06-27 16:31:03 +10:00
|
|
|
}
|